/src/libpcap-1.9.1/optimize.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 1988, 1989, 1990, 1991, 1993, 1994, 1995, 1996 |
3 | | * The Regents of the University of California. All rights reserved. |
4 | | * |
5 | | * Redistribution and use in source and binary forms, with or without |
6 | | * modification, are permitted provided that: (1) source code distributions |
7 | | * retain the above copyright notice and this paragraph in its entirety, (2) |
8 | | * distributions including binary code include the above copyright notice and |
9 | | * this paragraph in its entirety in the documentation or other materials |
10 | | * provided with the distribution, and (3) all advertising materials mentioning |
11 | | * features or use of this software display the following acknowledgement: |
12 | | * ``This product includes software developed by the University of California, |
13 | | * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of |
14 | | * the University nor the names of its contributors may be used to endorse |
15 | | * or promote products derived from this software without specific prior |
16 | | * written permission. |
17 | | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED |
18 | | * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
19 | | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
20 | | * |
21 | | * Optimization module for BPF code intermediate representation. |
22 | | */ |
23 | | |
24 | | #ifdef HAVE_CONFIG_H |
25 | | #include <config.h> |
26 | | #endif |
27 | | |
28 | | #include <pcap-types.h> |
29 | | |
30 | | #include <stdio.h> |
31 | | #include <stdlib.h> |
32 | | #include <memory.h> |
33 | | #include <setjmp.h> |
34 | | #include <string.h> |
35 | | |
36 | | #include <errno.h> |
37 | | |
38 | | #include "pcap-int.h" |
39 | | |
40 | | #include "gencode.h" |
41 | | #include "optimize.h" |
42 | | |
43 | | #ifdef HAVE_OS_PROTO_H |
44 | | #include "os-proto.h" |
45 | | #endif |
46 | | |
47 | | #ifdef BDEBUG |
48 | | /* |
49 | | * The internal "debug printout" flag for the filter expression optimizer. |
50 | | * The code to print that stuff is present only if BDEBUG is defined, so |
51 | | * the flag, and the routine to set it, are defined only if BDEBUG is |
52 | | * defined. |
53 | | */ |
54 | | static int pcap_optimizer_debug; |
55 | | |
56 | | /* |
57 | | * Routine to set that flag. |
58 | | * |
59 | | * This is intended for libpcap developers, not for general use. |
60 | | * If you want to set these in a program, you'll have to declare this |
61 | | * routine yourself, with the appropriate DLL import attribute on Windows; |
62 | | * it's not declared in any header file, and won't be declared in any |
63 | | * header file provided by libpcap. |
64 | | */ |
65 | | PCAP_API void pcap_set_optimizer_debug(int value); |
66 | | |
67 | | PCAP_API_DEF void |
68 | | pcap_set_optimizer_debug(int value) |
69 | | { |
70 | | pcap_optimizer_debug = value; |
71 | | } |
72 | | |
73 | | /* |
74 | | * The internal "print dot graph" flag for the filter expression optimizer. |
75 | | * The code to print that stuff is present only if BDEBUG is defined, so |
76 | | * the flag, and the routine to set it, are defined only if BDEBUG is |
77 | | * defined. |
78 | | */ |
79 | | static int pcap_print_dot_graph; |
80 | | |
81 | | /* |
82 | | * Routine to set that flag. |
83 | | * |
84 | | * This is intended for libpcap developers, not for general use. |
85 | | * If you want to set these in a program, you'll have to declare this |
86 | | * routine yourself, with the appropriate DLL import attribute on Windows; |
87 | | * it's not declared in any header file, and won't be declared in any |
88 | | * header file provided by libpcap. |
89 | | */ |
90 | | PCAP_API void pcap_set_print_dot_graph(int value); |
91 | | |
92 | | PCAP_API_DEF void |
93 | | pcap_set_print_dot_graph(int value) |
94 | | { |
95 | | pcap_print_dot_graph = value; |
96 | | } |
97 | | |
98 | | #endif |
99 | | |
100 | | /* |
101 | | * lowest_set_bit(). |
102 | | * |
103 | | * Takes a 32-bit integer as an argument. |
104 | | * |
105 | | * If handed a non-zero value, returns the index of the lowest set bit, |
106 | | * counting upwards fro zero. |
107 | | * |
108 | | * If handed zero, the results are platform- and compiler-dependent. |
109 | | * Keep it out of the light, don't give it any water, don't feed it |
110 | | * after midnight, and don't pass zero to it. |
111 | | * |
112 | | * This is the same as the count of trailing zeroes in the word. |
113 | | */ |
114 | | #if PCAP_IS_AT_LEAST_GNUC_VERSION(3,4) |
115 | | /* |
116 | | * GCC 3.4 and later; we have __builtin_ctz(). |
117 | | */ |
118 | 0 | #define lowest_set_bit(mask) __builtin_ctz(mask) |
119 | | #elif defined(_MSC_VER) |
120 | | /* |
121 | | * Visual Studio; we support only 2005 and later, so use |
122 | | * _BitScanForward(). |
123 | | */ |
124 | | #include <intrin.h> |
125 | | |
126 | | #ifndef __clang__ |
127 | | #pragma intrinsic(_BitScanForward) |
128 | | #endif |
129 | | |
130 | | static __forceinline int |
131 | | lowest_set_bit(int mask) |
132 | | { |
133 | | unsigned long bit; |
134 | | |
135 | | /* |
136 | | * Don't sign-extend mask if long is longer than int. |
137 | | * (It's currently not, in MSVC, even on 64-bit platforms, but....) |
138 | | */ |
139 | | if (_BitScanForward(&bit, (unsigned int)mask) == 0) |
140 | | return -1; /* mask is zero */ |
141 | | return (int)bit; |
142 | | } |
143 | | #elif defined(MSDOS) && defined(__DJGPP__) |
144 | | /* |
145 | | * MS-DOS with DJGPP, which declares ffs() in <string.h>, which |
146 | | * we've already included. |
147 | | */ |
148 | | #define lowest_set_bit(mask) (ffs((mask)) - 1) |
149 | | #elif (defined(MSDOS) && defined(__WATCOMC__)) || defined(STRINGS_H_DECLARES_FFS) |
150 | | /* |
151 | | * MS-DOS with Watcom C, which has <strings.h> and declares ffs() there, |
152 | | * or some other platform (UN*X conforming to a sufficient recent version |
153 | | * of the Single UNIX Specification). |
154 | | */ |
155 | | #include <strings.h> |
156 | | #define lowest_set_bit(mask) (ffs((mask)) - 1) |
157 | | #else |
158 | | /* |
159 | | * None of the above. |
160 | | * Use a perfect-hash-function-based function. |
161 | | */ |
162 | | static int |
163 | | lowest_set_bit(int mask) |
164 | | { |
165 | | unsigned int v = (unsigned int)mask; |
166 | | |
167 | | static const int MultiplyDeBruijnBitPosition[32] = { |
168 | | 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, |
169 | | 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9 |
170 | | }; |
171 | | |
172 | | /* |
173 | | * We strip off all but the lowermost set bit (v & ~v), |
174 | | * and perform a minimal perfect hash on it to look up the |
175 | | * number of low-order zero bits in a table. |
176 | | * |
177 | | * See: |
178 | | * |
179 | | * http://7ooo.mooo.com/text/ComputingTrailingZerosHOWTO.pdf |
180 | | * |
181 | | * http://supertech.csail.mit.edu/papers/debruijn.pdf |
182 | | */ |
183 | | return (MultiplyDeBruijnBitPosition[((v & -v) * 0x077CB531U) >> 27]); |
184 | | } |
185 | | #endif |
186 | | |
187 | | /* |
188 | | * Represents a deleted instruction. |
189 | | */ |
190 | 0 | #define NOP -1 |
191 | | |
192 | | /* |
193 | | * Register numbers for use-def values. |
194 | | * 0 through BPF_MEMWORDS-1 represent the corresponding scratch memory |
195 | | * location. A_ATOM is the accumulator and X_ATOM is the index |
196 | | * register. |
197 | | */ |
198 | 0 | #define A_ATOM BPF_MEMWORDS |
199 | 0 | #define X_ATOM (BPF_MEMWORDS+1) |
200 | | |
201 | | /* |
202 | | * This define is used to represent *both* the accumulator and |
203 | | * x register in use-def computations. |
204 | | * Currently, the use-def code assumes only one definition per instruction. |
205 | | */ |
206 | 0 | #define AX_ATOM N_ATOMS |
207 | | |
208 | | /* |
209 | | * These data structures are used in a Cocke and Shwarz style |
210 | | * value numbering scheme. Since the flowgraph is acyclic, |
211 | | * exit values can be propagated from a node's predecessors |
212 | | * provided it is uniquely defined. |
213 | | */ |
214 | | struct valnode { |
215 | | int code; |
216 | | int v0, v1; |
217 | | int val; |
218 | | struct valnode *next; |
219 | | }; |
220 | | |
221 | | /* Integer constants mapped with the load immediate opcode. */ |
222 | 0 | #define K(i) F(opt_state, BPF_LD|BPF_IMM|BPF_W, i, 0L) |
223 | | |
224 | | struct vmapinfo { |
225 | | int is_const; |
226 | | bpf_int32 const_val; |
227 | | }; |
228 | | |
229 | | typedef struct { |
230 | | /* |
231 | | * Place to longjmp to on an error. |
232 | | */ |
233 | | jmp_buf top_ctx; |
234 | | |
235 | | /* |
236 | | * The buffer into which to put error message. |
237 | | */ |
238 | | char *errbuf; |
239 | | |
240 | | /* |
241 | | * A flag to indicate that further optimization is needed. |
242 | | * Iterative passes are continued until a given pass yields no |
243 | | * branch movement. |
244 | | */ |
245 | | int done; |
246 | | |
247 | | int n_blocks; |
248 | | struct block **blocks; |
249 | | int n_edges; |
250 | | struct edge **edges; |
251 | | |
252 | | /* |
253 | | * A bit vector set representation of the dominators. |
254 | | * We round up the set size to the next power of two. |
255 | | */ |
256 | | int nodewords; |
257 | | int edgewords; |
258 | | struct block **levels; |
259 | | bpf_u_int32 *space; |
260 | | |
261 | 0 | #define BITS_PER_WORD (8*sizeof(bpf_u_int32)) |
262 | | /* |
263 | | * True if a is in uset {p} |
264 | | */ |
265 | 0 | #define SET_MEMBER(p, a) \ |
266 | 0 | ((p)[(unsigned)(a) / BITS_PER_WORD] & ((bpf_u_int32)1 << ((unsigned)(a) % BITS_PER_WORD))) |
267 | | |
268 | | /* |
269 | | * Add 'a' to uset p. |
270 | | */ |
271 | 0 | #define SET_INSERT(p, a) \ |
272 | 0 | (p)[(unsigned)(a) / BITS_PER_WORD] |= ((bpf_u_int32)1 << ((unsigned)(a) % BITS_PER_WORD)) |
273 | | |
274 | | /* |
275 | | * Delete 'a' from uset p. |
276 | | */ |
277 | | #define SET_DELETE(p, a) \ |
278 | | (p)[(unsigned)(a) / BITS_PER_WORD] &= ~((bpf_u_int32)1 << ((unsigned)(a) % BITS_PER_WORD)) |
279 | | |
280 | | /* |
281 | | * a := a intersect b |
282 | | */ |
283 | 0 | #define SET_INTERSECT(a, b, n)\ |
284 | 0 | {\ |
285 | 0 | register bpf_u_int32 *_x = a, *_y = b;\ |
286 | 0 | register int _n = n;\ |
287 | 0 | while (--_n >= 0) *_x++ &= *_y++;\ |
288 | 0 | } |
289 | | |
290 | | /* |
291 | | * a := a - b |
292 | | */ |
293 | | #define SET_SUBTRACT(a, b, n)\ |
294 | | {\ |
295 | | register bpf_u_int32 *_x = a, *_y = b;\ |
296 | | register int _n = n;\ |
297 | | while (--_n >= 0) *_x++ &=~ *_y++;\ |
298 | | } |
299 | | |
300 | | /* |
301 | | * a := a union b |
302 | | */ |
303 | 0 | #define SET_UNION(a, b, n)\ |
304 | 0 | {\ |
305 | 0 | register bpf_u_int32 *_x = a, *_y = b;\ |
306 | 0 | register int _n = n;\ |
307 | 0 | while (--_n >= 0) *_x++ |= *_y++;\ |
308 | 0 | } |
309 | | |
310 | | uset all_dom_sets; |
311 | | uset all_closure_sets; |
312 | | uset all_edge_sets; |
313 | | |
314 | 0 | #define MODULUS 213 |
315 | | struct valnode *hashtbl[MODULUS]; |
316 | | int curval; |
317 | | int maxval; |
318 | | |
319 | | struct vmapinfo *vmap; |
320 | | struct valnode *vnode_base; |
321 | | struct valnode *next_vnode; |
322 | | } opt_state_t; |
323 | | |
324 | | typedef struct { |
325 | | /* |
326 | | * Place to longjmp to on an error. |
327 | | */ |
328 | | jmp_buf top_ctx; |
329 | | |
330 | | /* |
331 | | * The buffer into which to put error message. |
332 | | */ |
333 | | char *errbuf; |
334 | | |
335 | | /* |
336 | | * Some pointers used to convert the basic block form of the code, |
337 | | * into the array form that BPF requires. 'fstart' will point to |
338 | | * the malloc'd array while 'ftail' is used during the recursive |
339 | | * traversal. |
340 | | */ |
341 | | struct bpf_insn *fstart; |
342 | | struct bpf_insn *ftail; |
343 | | } conv_state_t; |
344 | | |
345 | | static void opt_init(opt_state_t *, struct icode *); |
346 | | static void opt_cleanup(opt_state_t *); |
347 | | static void PCAP_NORETURN opt_error(opt_state_t *, const char *, ...) |
348 | | PCAP_PRINTFLIKE(2, 3); |
349 | | |
350 | | static void intern_blocks(opt_state_t *, struct icode *); |
351 | | |
352 | | static void find_inedges(opt_state_t *, struct block *); |
353 | | #ifdef BDEBUG |
354 | | static void opt_dump(opt_state_t *, struct icode *); |
355 | | #endif |
356 | | |
357 | | #ifndef MAX |
358 | 0 | #define MAX(a,b) ((a)>(b)?(a):(b)) |
359 | | #endif |
360 | | |
361 | | static void |
362 | | find_levels_r(opt_state_t *opt_state, struct icode *ic, struct block *b) |
363 | 0 | { |
364 | 0 | int level; |
365 | |
|
366 | 0 | if (isMarked(ic, b)) |
367 | 0 | return; |
368 | | |
369 | 0 | Mark(ic, b); |
370 | 0 | b->link = 0; |
371 | |
|
372 | 0 | if (JT(b)) { |
373 | 0 | find_levels_r(opt_state, ic, JT(b)); |
374 | 0 | find_levels_r(opt_state, ic, JF(b)); |
375 | 0 | level = MAX(JT(b)->level, JF(b)->level) + 1; |
376 | 0 | } else |
377 | 0 | level = 0; |
378 | 0 | b->level = level; |
379 | 0 | b->link = opt_state->levels[level]; |
380 | 0 | opt_state->levels[level] = b; |
381 | 0 | } |
382 | | |
383 | | /* |
384 | | * Level graph. The levels go from 0 at the leaves to |
385 | | * N_LEVELS at the root. The opt_state->levels[] array points to the |
386 | | * first node of the level list, whose elements are linked |
387 | | * with the 'link' field of the struct block. |
388 | | */ |
389 | | static void |
390 | | find_levels(opt_state_t *opt_state, struct icode *ic) |
391 | 0 | { |
392 | 0 | memset((char *)opt_state->levels, 0, opt_state->n_blocks * sizeof(*opt_state->levels)); |
393 | 0 | unMarkAll(ic); |
394 | 0 | find_levels_r(opt_state, ic, ic->root); |
395 | 0 | } |
396 | | |
397 | | /* |
398 | | * Find dominator relationships. |
399 | | * Assumes graph has been leveled. |
400 | | */ |
401 | | static void |
402 | | find_dom(opt_state_t *opt_state, struct block *root) |
403 | 0 | { |
404 | 0 | int i; |
405 | 0 | struct block *b; |
406 | 0 | bpf_u_int32 *x; |
407 | | |
408 | | /* |
409 | | * Initialize sets to contain all nodes. |
410 | | */ |
411 | 0 | x = opt_state->all_dom_sets; |
412 | 0 | i = opt_state->n_blocks * opt_state->nodewords; |
413 | 0 | while (--i >= 0) |
414 | 0 | *x++ = 0xFFFFFFFFU; |
415 | | /* Root starts off empty. */ |
416 | 0 | for (i = opt_state->nodewords; --i >= 0;) |
417 | 0 | root->dom[i] = 0; |
418 | | |
419 | | /* root->level is the highest level no found. */ |
420 | 0 | for (i = root->level; i >= 0; --i) { |
421 | 0 | for (b = opt_state->levels[i]; b; b = b->link) { |
422 | 0 | SET_INSERT(b->dom, b->id); |
423 | 0 | if (JT(b) == 0) |
424 | 0 | continue; |
425 | 0 | SET_INTERSECT(JT(b)->dom, b->dom, opt_state->nodewords); |
426 | 0 | SET_INTERSECT(JF(b)->dom, b->dom, opt_state->nodewords); |
427 | 0 | } |
428 | 0 | } |
429 | 0 | } |
430 | | |
431 | | static void |
432 | | propedom(opt_state_t *opt_state, struct edge *ep) |
433 | 0 | { |
434 | 0 | SET_INSERT(ep->edom, ep->id); |
435 | 0 | if (ep->succ) { |
436 | 0 | SET_INTERSECT(ep->succ->et.edom, ep->edom, opt_state->edgewords); |
437 | 0 | SET_INTERSECT(ep->succ->ef.edom, ep->edom, opt_state->edgewords); |
438 | 0 | } |
439 | 0 | } |
440 | | |
441 | | /* |
442 | | * Compute edge dominators. |
443 | | * Assumes graph has been leveled and predecessors established. |
444 | | */ |
445 | | static void |
446 | | find_edom(opt_state_t *opt_state, struct block *root) |
447 | 0 | { |
448 | 0 | int i; |
449 | 0 | uset x; |
450 | 0 | struct block *b; |
451 | |
|
452 | 0 | x = opt_state->all_edge_sets; |
453 | 0 | for (i = opt_state->n_edges * opt_state->edgewords; --i >= 0; ) |
454 | 0 | x[i] = 0xFFFFFFFFU; |
455 | | |
456 | | /* root->level is the highest level no found. */ |
457 | 0 | memset(root->et.edom, 0, opt_state->edgewords * sizeof(*(uset)0)); |
458 | 0 | memset(root->ef.edom, 0, opt_state->edgewords * sizeof(*(uset)0)); |
459 | 0 | for (i = root->level; i >= 0; --i) { |
460 | 0 | for (b = opt_state->levels[i]; b != 0; b = b->link) { |
461 | 0 | propedom(opt_state, &b->et); |
462 | 0 | propedom(opt_state, &b->ef); |
463 | 0 | } |
464 | 0 | } |
465 | 0 | } |
466 | | |
467 | | /* |
468 | | * Find the backwards transitive closure of the flow graph. These sets |
469 | | * are backwards in the sense that we find the set of nodes that reach |
470 | | * a given node, not the set of nodes that can be reached by a node. |
471 | | * |
472 | | * Assumes graph has been leveled. |
473 | | */ |
474 | | static void |
475 | | find_closure(opt_state_t *opt_state, struct block *root) |
476 | 0 | { |
477 | 0 | int i; |
478 | 0 | struct block *b; |
479 | | |
480 | | /* |
481 | | * Initialize sets to contain no nodes. |
482 | | */ |
483 | 0 | memset((char *)opt_state->all_closure_sets, 0, |
484 | 0 | opt_state->n_blocks * opt_state->nodewords * sizeof(*opt_state->all_closure_sets)); |
485 | | |
486 | | /* root->level is the highest level no found. */ |
487 | 0 | for (i = root->level; i >= 0; --i) { |
488 | 0 | for (b = opt_state->levels[i]; b; b = b->link) { |
489 | 0 | SET_INSERT(b->closure, b->id); |
490 | 0 | if (JT(b) == 0) |
491 | 0 | continue; |
492 | 0 | SET_UNION(JT(b)->closure, b->closure, opt_state->nodewords); |
493 | 0 | SET_UNION(JF(b)->closure, b->closure, opt_state->nodewords); |
494 | 0 | } |
495 | 0 | } |
496 | 0 | } |
497 | | |
498 | | /* |
499 | | * Return the register number that is used by s. If A and X are both |
500 | | * used, return AX_ATOM. If no register is used, return -1. |
501 | | * |
502 | | * The implementation should probably change to an array access. |
503 | | */ |
504 | | static int |
505 | | atomuse(struct stmt *s) |
506 | 0 | { |
507 | 0 | register int c = s->code; |
508 | |
|
509 | 0 | if (c == NOP) |
510 | 0 | return -1; |
511 | | |
512 | 0 | switch (BPF_CLASS(c)) { |
513 | | |
514 | 0 | case BPF_RET: |
515 | 0 | return (BPF_RVAL(c) == BPF_A) ? A_ATOM : |
516 | 0 | (BPF_RVAL(c) == BPF_X) ? X_ATOM : -1; |
517 | | |
518 | 0 | case BPF_LD: |
519 | 0 | case BPF_LDX: |
520 | 0 | return (BPF_MODE(c) == BPF_IND) ? X_ATOM : |
521 | 0 | (BPF_MODE(c) == BPF_MEM) ? s->k : -1; |
522 | | |
523 | 0 | case BPF_ST: |
524 | 0 | return A_ATOM; |
525 | | |
526 | 0 | case BPF_STX: |
527 | 0 | return X_ATOM; |
528 | | |
529 | 0 | case BPF_JMP: |
530 | 0 | case BPF_ALU: |
531 | 0 | if (BPF_SRC(c) == BPF_X) |
532 | 0 | return AX_ATOM; |
533 | 0 | return A_ATOM; |
534 | | |
535 | 0 | case BPF_MISC: |
536 | 0 | return BPF_MISCOP(c) == BPF_TXA ? X_ATOM : A_ATOM; |
537 | 0 | } |
538 | 0 | abort(); |
539 | | /* NOTREACHED */ |
540 | 0 | } |
541 | | |
542 | | /* |
543 | | * Return the register number that is defined by 's'. We assume that |
544 | | * a single stmt cannot define more than one register. If no register |
545 | | * is defined, return -1. |
546 | | * |
547 | | * The implementation should probably change to an array access. |
548 | | */ |
549 | | static int |
550 | | atomdef(struct stmt *s) |
551 | 0 | { |
552 | 0 | if (s->code == NOP) |
553 | 0 | return -1; |
554 | | |
555 | 0 | switch (BPF_CLASS(s->code)) { |
556 | | |
557 | 0 | case BPF_LD: |
558 | 0 | case BPF_ALU: |
559 | 0 | return A_ATOM; |
560 | | |
561 | 0 | case BPF_LDX: |
562 | 0 | return X_ATOM; |
563 | | |
564 | 0 | case BPF_ST: |
565 | 0 | case BPF_STX: |
566 | 0 | return s->k; |
567 | | |
568 | 0 | case BPF_MISC: |
569 | 0 | return BPF_MISCOP(s->code) == BPF_TAX ? X_ATOM : A_ATOM; |
570 | 0 | } |
571 | 0 | return -1; |
572 | 0 | } |
573 | | |
574 | | /* |
575 | | * Compute the sets of registers used, defined, and killed by 'b'. |
576 | | * |
577 | | * "Used" means that a statement in 'b' uses the register before any |
578 | | * statement in 'b' defines it, i.e. it uses the value left in |
579 | | * that register by a predecessor block of this block. |
580 | | * "Defined" means that a statement in 'b' defines it. |
581 | | * "Killed" means that a statement in 'b' defines it before any |
582 | | * statement in 'b' uses it, i.e. it kills the value left in that |
583 | | * register by a predecessor block of this block. |
584 | | */ |
585 | | static void |
586 | | compute_local_ud(struct block *b) |
587 | 0 | { |
588 | 0 | struct slist *s; |
589 | 0 | atomset def = 0, use = 0, killed = 0; |
590 | 0 | int atom; |
591 | |
|
592 | 0 | for (s = b->stmts; s; s = s->next) { |
593 | 0 | if (s->s.code == NOP) |
594 | 0 | continue; |
595 | 0 | atom = atomuse(&s->s); |
596 | 0 | if (atom >= 0) { |
597 | 0 | if (atom == AX_ATOM) { |
598 | 0 | if (!ATOMELEM(def, X_ATOM)) |
599 | 0 | use |= ATOMMASK(X_ATOM); |
600 | 0 | if (!ATOMELEM(def, A_ATOM)) |
601 | 0 | use |= ATOMMASK(A_ATOM); |
602 | 0 | } |
603 | 0 | else if (atom < N_ATOMS) { |
604 | 0 | if (!ATOMELEM(def, atom)) |
605 | 0 | use |= ATOMMASK(atom); |
606 | 0 | } |
607 | 0 | else |
608 | 0 | abort(); |
609 | 0 | } |
610 | 0 | atom = atomdef(&s->s); |
611 | 0 | if (atom >= 0) { |
612 | 0 | if (!ATOMELEM(use, atom)) |
613 | 0 | killed |= ATOMMASK(atom); |
614 | 0 | def |= ATOMMASK(atom); |
615 | 0 | } |
616 | 0 | } |
617 | 0 | if (BPF_CLASS(b->s.code) == BPF_JMP) { |
618 | | /* |
619 | | * XXX - what about RET? |
620 | | */ |
621 | 0 | atom = atomuse(&b->s); |
622 | 0 | if (atom >= 0) { |
623 | 0 | if (atom == AX_ATOM) { |
624 | 0 | if (!ATOMELEM(def, X_ATOM)) |
625 | 0 | use |= ATOMMASK(X_ATOM); |
626 | 0 | if (!ATOMELEM(def, A_ATOM)) |
627 | 0 | use |= ATOMMASK(A_ATOM); |
628 | 0 | } |
629 | 0 | else if (atom < N_ATOMS) { |
630 | 0 | if (!ATOMELEM(def, atom)) |
631 | 0 | use |= ATOMMASK(atom); |
632 | 0 | } |
633 | 0 | else |
634 | 0 | abort(); |
635 | 0 | } |
636 | 0 | } |
637 | | |
638 | 0 | b->def = def; |
639 | 0 | b->kill = killed; |
640 | 0 | b->in_use = use; |
641 | 0 | } |
642 | | |
643 | | /* |
644 | | * Assume graph is already leveled. |
645 | | */ |
646 | | static void |
647 | | find_ud(opt_state_t *opt_state, struct block *root) |
648 | 0 | { |
649 | 0 | int i, maxlevel; |
650 | 0 | struct block *p; |
651 | | |
652 | | /* |
653 | | * root->level is the highest level no found; |
654 | | * count down from there. |
655 | | */ |
656 | 0 | maxlevel = root->level; |
657 | 0 | for (i = maxlevel; i >= 0; --i) |
658 | 0 | for (p = opt_state->levels[i]; p; p = p->link) { |
659 | 0 | compute_local_ud(p); |
660 | 0 | p->out_use = 0; |
661 | 0 | } |
662 | |
|
663 | 0 | for (i = 1; i <= maxlevel; ++i) { |
664 | 0 | for (p = opt_state->levels[i]; p; p = p->link) { |
665 | 0 | p->out_use |= JT(p)->in_use | JF(p)->in_use; |
666 | 0 | p->in_use |= p->out_use &~ p->kill; |
667 | 0 | } |
668 | 0 | } |
669 | 0 | } |
670 | | static void |
671 | | init_val(opt_state_t *opt_state) |
672 | 0 | { |
673 | 0 | opt_state->curval = 0; |
674 | 0 | opt_state->next_vnode = opt_state->vnode_base; |
675 | 0 | memset((char *)opt_state->vmap, 0, opt_state->maxval * sizeof(*opt_state->vmap)); |
676 | 0 | memset((char *)opt_state->hashtbl, 0, sizeof opt_state->hashtbl); |
677 | 0 | } |
678 | | |
679 | | /* Because we really don't have an IR, this stuff is a little messy. */ |
680 | | static int |
681 | | F(opt_state_t *opt_state, int code, int v0, int v1) |
682 | 0 | { |
683 | 0 | u_int hash; |
684 | 0 | int val; |
685 | 0 | struct valnode *p; |
686 | |
|
687 | 0 | hash = (u_int)code ^ ((u_int)v0 << 4) ^ ((u_int)v1 << 8); |
688 | 0 | hash %= MODULUS; |
689 | |
|
690 | 0 | for (p = opt_state->hashtbl[hash]; p; p = p->next) |
691 | 0 | if (p->code == code && p->v0 == v0 && p->v1 == v1) |
692 | 0 | return p->val; |
693 | | |
694 | 0 | val = ++opt_state->curval; |
695 | 0 | if (BPF_MODE(code) == BPF_IMM && |
696 | 0 | (BPF_CLASS(code) == BPF_LD || BPF_CLASS(code) == BPF_LDX)) { |
697 | 0 | opt_state->vmap[val].const_val = v0; |
698 | 0 | opt_state->vmap[val].is_const = 1; |
699 | 0 | } |
700 | 0 | p = opt_state->next_vnode++; |
701 | 0 | p->val = val; |
702 | 0 | p->code = code; |
703 | 0 | p->v0 = v0; |
704 | 0 | p->v1 = v1; |
705 | 0 | p->next = opt_state->hashtbl[hash]; |
706 | 0 | opt_state->hashtbl[hash] = p; |
707 | |
|
708 | 0 | return val; |
709 | 0 | } |
710 | | |
711 | | static inline void |
712 | | vstore(struct stmt *s, int *valp, int newval, int alter) |
713 | 0 | { |
714 | 0 | if (alter && newval != VAL_UNKNOWN && *valp == newval) |
715 | 0 | s->code = NOP; |
716 | 0 | else |
717 | 0 | *valp = newval; |
718 | 0 | } |
719 | | |
720 | | /* |
721 | | * Do constant-folding on binary operators. |
722 | | * (Unary operators are handled elsewhere.) |
723 | | */ |
724 | | static void |
725 | | fold_op(opt_state_t *opt_state, struct stmt *s, int v0, int v1) |
726 | 0 | { |
727 | 0 | bpf_u_int32 a, b; |
728 | |
|
729 | 0 | a = opt_state->vmap[v0].const_val; |
730 | 0 | b = opt_state->vmap[v1].const_val; |
731 | |
|
732 | 0 | switch (BPF_OP(s->code)) { |
733 | 0 | case BPF_ADD: |
734 | 0 | a += b; |
735 | 0 | break; |
736 | | |
737 | 0 | case BPF_SUB: |
738 | 0 | a -= b; |
739 | 0 | break; |
740 | | |
741 | 0 | case BPF_MUL: |
742 | 0 | a *= b; |
743 | 0 | break; |
744 | | |
745 | 0 | case BPF_DIV: |
746 | 0 | if (b == 0) |
747 | 0 | opt_error(opt_state, "division by zero"); |
748 | 0 | a /= b; |
749 | 0 | break; |
750 | | |
751 | 0 | case BPF_MOD: |
752 | 0 | if (b == 0) |
753 | 0 | opt_error(opt_state, "modulus by zero"); |
754 | 0 | a %= b; |
755 | 0 | break; |
756 | | |
757 | 0 | case BPF_AND: |
758 | 0 | a &= b; |
759 | 0 | break; |
760 | | |
761 | 0 | case BPF_OR: |
762 | 0 | a |= b; |
763 | 0 | break; |
764 | | |
765 | 0 | case BPF_XOR: |
766 | 0 | a ^= b; |
767 | 0 | break; |
768 | | |
769 | 0 | case BPF_LSH: |
770 | | /* |
771 | | * A left shift of more than the width of the type |
772 | | * is undefined in C; we'll just treat it as shifting |
773 | | * all the bits out. |
774 | | * |
775 | | * XXX - the BPF interpreter doesn't check for this, |
776 | | * so its behavior is dependent on the behavior of |
777 | | * the processor on which it's running. There are |
778 | | * processors on which it shifts all the bits out |
779 | | * and processors on which it does no shift. |
780 | | */ |
781 | 0 | if (b < 32) |
782 | 0 | a <<= b; |
783 | 0 | else |
784 | 0 | a = 0; |
785 | 0 | break; |
786 | | |
787 | 0 | case BPF_RSH: |
788 | | /* |
789 | | * A right shift of more than the width of the type |
790 | | * is undefined in C; we'll just treat it as shifting |
791 | | * all the bits out. |
792 | | * |
793 | | * XXX - the BPF interpreter doesn't check for this, |
794 | | * so its behavior is dependent on the behavior of |
795 | | * the processor on which it's running. There are |
796 | | * processors on which it shifts all the bits out |
797 | | * and processors on which it does no shift. |
798 | | */ |
799 | 0 | if (b < 32) |
800 | 0 | a >>= b; |
801 | 0 | else |
802 | 0 | a = 0; |
803 | 0 | break; |
804 | | |
805 | 0 | default: |
806 | 0 | abort(); |
807 | 0 | } |
808 | 0 | s->k = a; |
809 | 0 | s->code = BPF_LD|BPF_IMM; |
810 | 0 | opt_state->done = 0; |
811 | 0 | } |
812 | | |
813 | | static inline struct slist * |
814 | | this_op(struct slist *s) |
815 | 0 | { |
816 | 0 | while (s != 0 && s->s.code == NOP) |
817 | 0 | s = s->next; |
818 | 0 | return s; |
819 | 0 | } |
820 | | |
821 | | static void |
822 | | opt_not(struct block *b) |
823 | 0 | { |
824 | 0 | struct block *tmp = JT(b); |
825 | |
|
826 | 0 | JT(b) = JF(b); |
827 | 0 | JF(b) = tmp; |
828 | 0 | } |
829 | | |
830 | | static void |
831 | | opt_peep(opt_state_t *opt_state, struct block *b) |
832 | 0 | { |
833 | 0 | struct slist *s; |
834 | 0 | struct slist *next, *last; |
835 | 0 | int val; |
836 | |
|
837 | 0 | s = b->stmts; |
838 | 0 | if (s == 0) |
839 | 0 | return; |
840 | | |
841 | 0 | last = s; |
842 | 0 | for (/*empty*/; /*empty*/; s = next) { |
843 | | /* |
844 | | * Skip over nops. |
845 | | */ |
846 | 0 | s = this_op(s); |
847 | 0 | if (s == 0) |
848 | 0 | break; /* nothing left in the block */ |
849 | | |
850 | | /* |
851 | | * Find the next real instruction after that one |
852 | | * (skipping nops). |
853 | | */ |
854 | 0 | next = this_op(s->next); |
855 | 0 | if (next == 0) |
856 | 0 | break; /* no next instruction */ |
857 | 0 | last = next; |
858 | | |
859 | | /* |
860 | | * st M[k] --> st M[k] |
861 | | * ldx M[k] tax |
862 | | */ |
863 | 0 | if (s->s.code == BPF_ST && |
864 | 0 | next->s.code == (BPF_LDX|BPF_MEM) && |
865 | 0 | s->s.k == next->s.k) { |
866 | 0 | opt_state->done = 0; |
867 | 0 | next->s.code = BPF_MISC|BPF_TAX; |
868 | 0 | } |
869 | | /* |
870 | | * ld #k --> ldx #k |
871 | | * tax txa |
872 | | */ |
873 | 0 | if (s->s.code == (BPF_LD|BPF_IMM) && |
874 | 0 | next->s.code == (BPF_MISC|BPF_TAX)) { |
875 | 0 | s->s.code = BPF_LDX|BPF_IMM; |
876 | 0 | next->s.code = BPF_MISC|BPF_TXA; |
877 | 0 | opt_state->done = 0; |
878 | 0 | } |
879 | | /* |
880 | | * This is an ugly special case, but it happens |
881 | | * when you say tcp[k] or udp[k] where k is a constant. |
882 | | */ |
883 | 0 | if (s->s.code == (BPF_LD|BPF_IMM)) { |
884 | 0 | struct slist *add, *tax, *ild; |
885 | | |
886 | | /* |
887 | | * Check that X isn't used on exit from this |
888 | | * block (which the optimizer might cause). |
889 | | * We know the code generator won't generate |
890 | | * any local dependencies. |
891 | | */ |
892 | 0 | if (ATOMELEM(b->out_use, X_ATOM)) |
893 | 0 | continue; |
894 | | |
895 | | /* |
896 | | * Check that the instruction following the ldi |
897 | | * is an addx, or it's an ldxms with an addx |
898 | | * following it (with 0 or more nops between the |
899 | | * ldxms and addx). |
900 | | */ |
901 | 0 | if (next->s.code != (BPF_LDX|BPF_MSH|BPF_B)) |
902 | 0 | add = next; |
903 | 0 | else |
904 | 0 | add = this_op(next->next); |
905 | 0 | if (add == 0 || add->s.code != (BPF_ALU|BPF_ADD|BPF_X)) |
906 | 0 | continue; |
907 | | |
908 | | /* |
909 | | * Check that a tax follows that (with 0 or more |
910 | | * nops between them). |
911 | | */ |
912 | 0 | tax = this_op(add->next); |
913 | 0 | if (tax == 0 || tax->s.code != (BPF_MISC|BPF_TAX)) |
914 | 0 | continue; |
915 | | |
916 | | /* |
917 | | * Check that an ild follows that (with 0 or more |
918 | | * nops between them). |
919 | | */ |
920 | 0 | ild = this_op(tax->next); |
921 | 0 | if (ild == 0 || BPF_CLASS(ild->s.code) != BPF_LD || |
922 | 0 | BPF_MODE(ild->s.code) != BPF_IND) |
923 | 0 | continue; |
924 | | /* |
925 | | * We want to turn this sequence: |
926 | | * |
927 | | * (004) ldi #0x2 {s} |
928 | | * (005) ldxms [14] {next} -- optional |
929 | | * (006) addx {add} |
930 | | * (007) tax {tax} |
931 | | * (008) ild [x+0] {ild} |
932 | | * |
933 | | * into this sequence: |
934 | | * |
935 | | * (004) nop |
936 | | * (005) ldxms [14] |
937 | | * (006) nop |
938 | | * (007) nop |
939 | | * (008) ild [x+2] |
940 | | * |
941 | | * XXX We need to check that X is not |
942 | | * subsequently used, because we want to change |
943 | | * what'll be in it after this sequence. |
944 | | * |
945 | | * We know we can eliminate the accumulator |
946 | | * modifications earlier in the sequence since |
947 | | * it is defined by the last stmt of this sequence |
948 | | * (i.e., the last statement of the sequence loads |
949 | | * a value into the accumulator, so we can eliminate |
950 | | * earlier operations on the accumulator). |
951 | | */ |
952 | 0 | ild->s.k += s->s.k; |
953 | 0 | s->s.code = NOP; |
954 | 0 | add->s.code = NOP; |
955 | 0 | tax->s.code = NOP; |
956 | 0 | opt_state->done = 0; |
957 | 0 | } |
958 | 0 | } |
959 | | /* |
960 | | * If the comparison at the end of a block is an equality |
961 | | * comparison against a constant, and nobody uses the value |
962 | | * we leave in the A register at the end of a block, and |
963 | | * the operation preceding the comparison is an arithmetic |
964 | | * operation, we can sometime optimize it away. |
965 | | */ |
966 | 0 | if (b->s.code == (BPF_JMP|BPF_JEQ|BPF_K) && |
967 | 0 | !ATOMELEM(b->out_use, A_ATOM)) { |
968 | | /* |
969 | | * We can optimize away certain subtractions of the |
970 | | * X register. |
971 | | */ |
972 | 0 | if (last->s.code == (BPF_ALU|BPF_SUB|BPF_X)) { |
973 | 0 | val = b->val[X_ATOM]; |
974 | 0 | if (opt_state->vmap[val].is_const) { |
975 | | /* |
976 | | * If we have a subtract to do a comparison, |
977 | | * and the X register is a known constant, |
978 | | * we can merge this value into the |
979 | | * comparison: |
980 | | * |
981 | | * sub x -> nop |
982 | | * jeq #y jeq #(x+y) |
983 | | */ |
984 | 0 | b->s.k += opt_state->vmap[val].const_val; |
985 | 0 | last->s.code = NOP; |
986 | 0 | opt_state->done = 0; |
987 | 0 | } else if (b->s.k == 0) { |
988 | | /* |
989 | | * If the X register isn't a constant, |
990 | | * and the comparison in the test is |
991 | | * against 0, we can compare with the |
992 | | * X register, instead: |
993 | | * |
994 | | * sub x -> nop |
995 | | * jeq #0 jeq x |
996 | | */ |
997 | 0 | last->s.code = NOP; |
998 | 0 | b->s.code = BPF_JMP|BPF_JEQ|BPF_X; |
999 | 0 | opt_state->done = 0; |
1000 | 0 | } |
1001 | 0 | } |
1002 | | /* |
1003 | | * Likewise, a constant subtract can be simplified: |
1004 | | * |
1005 | | * sub #x -> nop |
1006 | | * jeq #y -> jeq #(x+y) |
1007 | | */ |
1008 | 0 | else if (last->s.code == (BPF_ALU|BPF_SUB|BPF_K)) { |
1009 | 0 | last->s.code = NOP; |
1010 | 0 | b->s.k += last->s.k; |
1011 | 0 | opt_state->done = 0; |
1012 | 0 | } |
1013 | | /* |
1014 | | * And, similarly, a constant AND can be simplified |
1015 | | * if we're testing against 0, i.e.: |
1016 | | * |
1017 | | * and #k nop |
1018 | | * jeq #0 -> jset #k |
1019 | | */ |
1020 | 0 | else if (last->s.code == (BPF_ALU|BPF_AND|BPF_K) && |
1021 | 0 | b->s.k == 0) { |
1022 | 0 | b->s.k = last->s.k; |
1023 | 0 | b->s.code = BPF_JMP|BPF_K|BPF_JSET; |
1024 | 0 | last->s.code = NOP; |
1025 | 0 | opt_state->done = 0; |
1026 | 0 | opt_not(b); |
1027 | 0 | } |
1028 | 0 | } |
1029 | | /* |
1030 | | * jset #0 -> never |
1031 | | * jset #ffffffff -> always |
1032 | | */ |
1033 | 0 | if (b->s.code == (BPF_JMP|BPF_K|BPF_JSET)) { |
1034 | 0 | if (b->s.k == 0) |
1035 | 0 | JT(b) = JF(b); |
1036 | 0 | if ((u_int)b->s.k == 0xffffffffU) |
1037 | 0 | JF(b) = JT(b); |
1038 | 0 | } |
1039 | | /* |
1040 | | * If we're comparing against the index register, and the index |
1041 | | * register is a known constant, we can just compare against that |
1042 | | * constant. |
1043 | | */ |
1044 | 0 | val = b->val[X_ATOM]; |
1045 | 0 | if (opt_state->vmap[val].is_const && BPF_SRC(b->s.code) == BPF_X) { |
1046 | 0 | bpf_int32 v = opt_state->vmap[val].const_val; |
1047 | 0 | b->s.code &= ~BPF_X; |
1048 | 0 | b->s.k = v; |
1049 | 0 | } |
1050 | | /* |
1051 | | * If the accumulator is a known constant, we can compute the |
1052 | | * comparison result. |
1053 | | */ |
1054 | 0 | val = b->val[A_ATOM]; |
1055 | 0 | if (opt_state->vmap[val].is_const && BPF_SRC(b->s.code) == BPF_K) { |
1056 | 0 | bpf_int32 v = opt_state->vmap[val].const_val; |
1057 | 0 | switch (BPF_OP(b->s.code)) { |
1058 | | |
1059 | 0 | case BPF_JEQ: |
1060 | 0 | v = v == b->s.k; |
1061 | 0 | break; |
1062 | | |
1063 | 0 | case BPF_JGT: |
1064 | 0 | v = (unsigned)v > (unsigned)b->s.k; |
1065 | 0 | break; |
1066 | | |
1067 | 0 | case BPF_JGE: |
1068 | 0 | v = (unsigned)v >= (unsigned)b->s.k; |
1069 | 0 | break; |
1070 | | |
1071 | 0 | case BPF_JSET: |
1072 | 0 | v &= b->s.k; |
1073 | 0 | break; |
1074 | | |
1075 | 0 | default: |
1076 | 0 | abort(); |
1077 | 0 | } |
1078 | 0 | if (JF(b) != JT(b)) |
1079 | 0 | opt_state->done = 0; |
1080 | 0 | if (v) |
1081 | 0 | JF(b) = JT(b); |
1082 | 0 | else |
1083 | 0 | JT(b) = JF(b); |
1084 | 0 | } |
1085 | 0 | } |
1086 | | |
1087 | | /* |
1088 | | * Compute the symbolic value of expression of 's', and update |
1089 | | * anything it defines in the value table 'val'. If 'alter' is true, |
1090 | | * do various optimizations. This code would be cleaner if symbolic |
1091 | | * evaluation and code transformations weren't folded together. |
1092 | | */ |
1093 | | static void |
1094 | | opt_stmt(opt_state_t *opt_state, struct stmt *s, int val[], int alter) |
1095 | 0 | { |
1096 | 0 | int op; |
1097 | 0 | int v; |
1098 | |
|
1099 | 0 | switch (s->code) { |
1100 | | |
1101 | 0 | case BPF_LD|BPF_ABS|BPF_W: |
1102 | 0 | case BPF_LD|BPF_ABS|BPF_H: |
1103 | 0 | case BPF_LD|BPF_ABS|BPF_B: |
1104 | 0 | v = F(opt_state, s->code, s->k, 0L); |
1105 | 0 | vstore(s, &val[A_ATOM], v, alter); |
1106 | 0 | break; |
1107 | | |
1108 | 0 | case BPF_LD|BPF_IND|BPF_W: |
1109 | 0 | case BPF_LD|BPF_IND|BPF_H: |
1110 | 0 | case BPF_LD|BPF_IND|BPF_B: |
1111 | 0 | v = val[X_ATOM]; |
1112 | 0 | if (alter && opt_state->vmap[v].is_const) { |
1113 | 0 | s->code = BPF_LD|BPF_ABS|BPF_SIZE(s->code); |
1114 | 0 | s->k += opt_state->vmap[v].const_val; |
1115 | 0 | v = F(opt_state, s->code, s->k, 0L); |
1116 | 0 | opt_state->done = 0; |
1117 | 0 | } |
1118 | 0 | else |
1119 | 0 | v = F(opt_state, s->code, s->k, v); |
1120 | 0 | vstore(s, &val[A_ATOM], v, alter); |
1121 | 0 | break; |
1122 | | |
1123 | 0 | case BPF_LD|BPF_LEN: |
1124 | 0 | v = F(opt_state, s->code, 0L, 0L); |
1125 | 0 | vstore(s, &val[A_ATOM], v, alter); |
1126 | 0 | break; |
1127 | | |
1128 | 0 | case BPF_LD|BPF_IMM: |
1129 | 0 | v = K(s->k); |
1130 | 0 | vstore(s, &val[A_ATOM], v, alter); |
1131 | 0 | break; |
1132 | | |
1133 | 0 | case BPF_LDX|BPF_IMM: |
1134 | 0 | v = K(s->k); |
1135 | 0 | vstore(s, &val[X_ATOM], v, alter); |
1136 | 0 | break; |
1137 | | |
1138 | 0 | case BPF_LDX|BPF_MSH|BPF_B: |
1139 | 0 | v = F(opt_state, s->code, s->k, 0L); |
1140 | 0 | vstore(s, &val[X_ATOM], v, alter); |
1141 | 0 | break; |
1142 | | |
1143 | 0 | case BPF_ALU|BPF_NEG: |
1144 | 0 | if (alter && opt_state->vmap[val[A_ATOM]].is_const) { |
1145 | 0 | s->code = BPF_LD|BPF_IMM; |
1146 | | /* |
1147 | | * Do this negation as unsigned arithmetic; that's |
1148 | | * what modern BPF engines do, and it guarantees |
1149 | | * that all possible values can be negated. (Yeah, |
1150 | | * negating 0x80000000, the minimum signed 32-bit |
1151 | | * two's-complement value, results in 0x80000000, |
1152 | | * so it's still negative, but we *should* be doing |
1153 | | * all unsigned arithmetic here, to match what |
1154 | | * modern BPF engines do.) |
1155 | | * |
1156 | | * Express it as 0U - (unsigned value) so that we |
1157 | | * don't get compiler warnings about negating an |
1158 | | * unsigned value and don't get UBSan warnings |
1159 | | * about the result of negating 0x80000000 being |
1160 | | * undefined. |
1161 | | */ |
1162 | 0 | s->k = 0U - (bpf_u_int32)(opt_state->vmap[val[A_ATOM]].const_val); |
1163 | 0 | val[A_ATOM] = K(s->k); |
1164 | 0 | } |
1165 | 0 | else |
1166 | 0 | val[A_ATOM] = F(opt_state, s->code, val[A_ATOM], 0L); |
1167 | 0 | break; |
1168 | | |
1169 | 0 | case BPF_ALU|BPF_ADD|BPF_K: |
1170 | 0 | case BPF_ALU|BPF_SUB|BPF_K: |
1171 | 0 | case BPF_ALU|BPF_MUL|BPF_K: |
1172 | 0 | case BPF_ALU|BPF_DIV|BPF_K: |
1173 | 0 | case BPF_ALU|BPF_MOD|BPF_K: |
1174 | 0 | case BPF_ALU|BPF_AND|BPF_K: |
1175 | 0 | case BPF_ALU|BPF_OR|BPF_K: |
1176 | 0 | case BPF_ALU|BPF_XOR|BPF_K: |
1177 | 0 | case BPF_ALU|BPF_LSH|BPF_K: |
1178 | 0 | case BPF_ALU|BPF_RSH|BPF_K: |
1179 | 0 | op = BPF_OP(s->code); |
1180 | 0 | if (alter) { |
1181 | 0 | if (s->k == 0) { |
1182 | | /* |
1183 | | * Optimize operations where the constant |
1184 | | * is zero. |
1185 | | * |
1186 | | * Don't optimize away "sub #0" |
1187 | | * as it may be needed later to |
1188 | | * fixup the generated math code. |
1189 | | * |
1190 | | * Fail if we're dividing by zero or taking |
1191 | | * a modulus by zero. |
1192 | | */ |
1193 | 0 | if (op == BPF_ADD || |
1194 | 0 | op == BPF_LSH || op == BPF_RSH || |
1195 | 0 | op == BPF_OR || op == BPF_XOR) { |
1196 | 0 | s->code = NOP; |
1197 | 0 | break; |
1198 | 0 | } |
1199 | 0 | if (op == BPF_MUL || op == BPF_AND) { |
1200 | 0 | s->code = BPF_LD|BPF_IMM; |
1201 | 0 | val[A_ATOM] = K(s->k); |
1202 | 0 | break; |
1203 | 0 | } |
1204 | 0 | if (op == BPF_DIV) |
1205 | 0 | opt_error(opt_state, |
1206 | 0 | "division by zero"); |
1207 | 0 | if (op == BPF_MOD) |
1208 | 0 | opt_error(opt_state, |
1209 | 0 | "modulus by zero"); |
1210 | 0 | } |
1211 | 0 | if (opt_state->vmap[val[A_ATOM]].is_const) { |
1212 | 0 | fold_op(opt_state, s, val[A_ATOM], K(s->k)); |
1213 | 0 | val[A_ATOM] = K(s->k); |
1214 | 0 | break; |
1215 | 0 | } |
1216 | 0 | } |
1217 | 0 | val[A_ATOM] = F(opt_state, s->code, val[A_ATOM], K(s->k)); |
1218 | 0 | break; |
1219 | | |
1220 | 0 | case BPF_ALU|BPF_ADD|BPF_X: |
1221 | 0 | case BPF_ALU|BPF_SUB|BPF_X: |
1222 | 0 | case BPF_ALU|BPF_MUL|BPF_X: |
1223 | 0 | case BPF_ALU|BPF_DIV|BPF_X: |
1224 | 0 | case BPF_ALU|BPF_MOD|BPF_X: |
1225 | 0 | case BPF_ALU|BPF_AND|BPF_X: |
1226 | 0 | case BPF_ALU|BPF_OR|BPF_X: |
1227 | 0 | case BPF_ALU|BPF_XOR|BPF_X: |
1228 | 0 | case BPF_ALU|BPF_LSH|BPF_X: |
1229 | 0 | case BPF_ALU|BPF_RSH|BPF_X: |
1230 | 0 | op = BPF_OP(s->code); |
1231 | 0 | if (alter && opt_state->vmap[val[X_ATOM]].is_const) { |
1232 | 0 | if (opt_state->vmap[val[A_ATOM]].is_const) { |
1233 | 0 | fold_op(opt_state, s, val[A_ATOM], val[X_ATOM]); |
1234 | 0 | val[A_ATOM] = K(s->k); |
1235 | 0 | } |
1236 | 0 | else { |
1237 | 0 | s->code = BPF_ALU|BPF_K|op; |
1238 | 0 | s->k = opt_state->vmap[val[X_ATOM]].const_val; |
1239 | | /* |
1240 | | * XXX - we need to make up our minds |
1241 | | * as to what integers are signed and |
1242 | | * what integers are unsigned in BPF |
1243 | | * programs and in our IR. |
1244 | | */ |
1245 | 0 | if ((op == BPF_LSH || op == BPF_RSH) && |
1246 | 0 | (s->k < 0 || s->k > 31)) |
1247 | 0 | opt_error(opt_state, |
1248 | 0 | "shift by more than 31 bits"); |
1249 | 0 | opt_state->done = 0; |
1250 | 0 | val[A_ATOM] = |
1251 | 0 | F(opt_state, s->code, val[A_ATOM], K(s->k)); |
1252 | 0 | } |
1253 | 0 | break; |
1254 | 0 | } |
1255 | | /* |
1256 | | * Check if we're doing something to an accumulator |
1257 | | * that is 0, and simplify. This may not seem like |
1258 | | * much of a simplification but it could open up further |
1259 | | * optimizations. |
1260 | | * XXX We could also check for mul by 1, etc. |
1261 | | */ |
1262 | 0 | if (alter && opt_state->vmap[val[A_ATOM]].is_const |
1263 | 0 | && opt_state->vmap[val[A_ATOM]].const_val == 0) { |
1264 | 0 | if (op == BPF_ADD || op == BPF_OR || op == BPF_XOR) { |
1265 | 0 | s->code = BPF_MISC|BPF_TXA; |
1266 | 0 | vstore(s, &val[A_ATOM], val[X_ATOM], alter); |
1267 | 0 | break; |
1268 | 0 | } |
1269 | 0 | else if (op == BPF_MUL || op == BPF_DIV || op == BPF_MOD || |
1270 | 0 | op == BPF_AND || op == BPF_LSH || op == BPF_RSH) { |
1271 | 0 | s->code = BPF_LD|BPF_IMM; |
1272 | 0 | s->k = 0; |
1273 | 0 | vstore(s, &val[A_ATOM], K(s->k), alter); |
1274 | 0 | break; |
1275 | 0 | } |
1276 | 0 | else if (op == BPF_NEG) { |
1277 | 0 | s->code = NOP; |
1278 | 0 | break; |
1279 | 0 | } |
1280 | 0 | } |
1281 | 0 | val[A_ATOM] = F(opt_state, s->code, val[A_ATOM], val[X_ATOM]); |
1282 | 0 | break; |
1283 | | |
1284 | 0 | case BPF_MISC|BPF_TXA: |
1285 | 0 | vstore(s, &val[A_ATOM], val[X_ATOM], alter); |
1286 | 0 | break; |
1287 | | |
1288 | 0 | case BPF_LD|BPF_MEM: |
1289 | 0 | v = val[s->k]; |
1290 | 0 | if (alter && opt_state->vmap[v].is_const) { |
1291 | 0 | s->code = BPF_LD|BPF_IMM; |
1292 | 0 | s->k = opt_state->vmap[v].const_val; |
1293 | 0 | opt_state->done = 0; |
1294 | 0 | } |
1295 | 0 | vstore(s, &val[A_ATOM], v, alter); |
1296 | 0 | break; |
1297 | | |
1298 | 0 | case BPF_MISC|BPF_TAX: |
1299 | 0 | vstore(s, &val[X_ATOM], val[A_ATOM], alter); |
1300 | 0 | break; |
1301 | | |
1302 | 0 | case BPF_LDX|BPF_MEM: |
1303 | 0 | v = val[s->k]; |
1304 | 0 | if (alter && opt_state->vmap[v].is_const) { |
1305 | 0 | s->code = BPF_LDX|BPF_IMM; |
1306 | 0 | s->k = opt_state->vmap[v].const_val; |
1307 | 0 | opt_state->done = 0; |
1308 | 0 | } |
1309 | 0 | vstore(s, &val[X_ATOM], v, alter); |
1310 | 0 | break; |
1311 | | |
1312 | 0 | case BPF_ST: |
1313 | 0 | vstore(s, &val[s->k], val[A_ATOM], alter); |
1314 | 0 | break; |
1315 | | |
1316 | 0 | case BPF_STX: |
1317 | 0 | vstore(s, &val[s->k], val[X_ATOM], alter); |
1318 | 0 | break; |
1319 | 0 | } |
1320 | 0 | } |
1321 | | |
1322 | | static void |
1323 | | deadstmt(opt_state_t *opt_state, register struct stmt *s, register struct stmt *last[]) |
1324 | 0 | { |
1325 | 0 | register int atom; |
1326 | |
|
1327 | 0 | atom = atomuse(s); |
1328 | 0 | if (atom >= 0) { |
1329 | 0 | if (atom == AX_ATOM) { |
1330 | 0 | last[X_ATOM] = 0; |
1331 | 0 | last[A_ATOM] = 0; |
1332 | 0 | } |
1333 | 0 | else |
1334 | 0 | last[atom] = 0; |
1335 | 0 | } |
1336 | 0 | atom = atomdef(s); |
1337 | 0 | if (atom >= 0) { |
1338 | 0 | if (last[atom]) { |
1339 | 0 | opt_state->done = 0; |
1340 | 0 | last[atom]->code = NOP; |
1341 | 0 | } |
1342 | 0 | last[atom] = s; |
1343 | 0 | } |
1344 | 0 | } |
1345 | | |
1346 | | static void |
1347 | | opt_deadstores(opt_state_t *opt_state, register struct block *b) |
1348 | 0 | { |
1349 | 0 | register struct slist *s; |
1350 | 0 | register int atom; |
1351 | 0 | struct stmt *last[N_ATOMS]; |
1352 | |
|
1353 | 0 | memset((char *)last, 0, sizeof last); |
1354 | |
|
1355 | 0 | for (s = b->stmts; s != 0; s = s->next) |
1356 | 0 | deadstmt(opt_state, &s->s, last); |
1357 | 0 | deadstmt(opt_state, &b->s, last); |
1358 | |
|
1359 | 0 | for (atom = 0; atom < N_ATOMS; ++atom) |
1360 | 0 | if (last[atom] && !ATOMELEM(b->out_use, atom)) { |
1361 | 0 | last[atom]->code = NOP; |
1362 | 0 | opt_state->done = 0; |
1363 | 0 | } |
1364 | 0 | } |
1365 | | |
1366 | | static void |
1367 | | opt_blk(opt_state_t *opt_state, struct block *b, int do_stmts) |
1368 | 0 | { |
1369 | 0 | struct slist *s; |
1370 | 0 | struct edge *p; |
1371 | 0 | int i; |
1372 | 0 | bpf_int32 aval, xval; |
1373 | |
|
1374 | | #if 0 |
1375 | | for (s = b->stmts; s && s->next; s = s->next) |
1376 | | if (BPF_CLASS(s->s.code) == BPF_JMP) { |
1377 | | do_stmts = 0; |
1378 | | break; |
1379 | | } |
1380 | | #endif |
1381 | | |
1382 | | /* |
1383 | | * Initialize the atom values. |
1384 | | */ |
1385 | 0 | p = b->in_edges; |
1386 | 0 | if (p == 0) { |
1387 | | /* |
1388 | | * We have no predecessors, so everything is undefined |
1389 | | * upon entry to this block. |
1390 | | */ |
1391 | 0 | memset((char *)b->val, 0, sizeof(b->val)); |
1392 | 0 | } else { |
1393 | | /* |
1394 | | * Inherit values from our predecessors. |
1395 | | * |
1396 | | * First, get the values from the predecessor along the |
1397 | | * first edge leading to this node. |
1398 | | */ |
1399 | 0 | memcpy((char *)b->val, (char *)p->pred->val, sizeof(b->val)); |
1400 | | /* |
1401 | | * Now look at all the other nodes leading to this node. |
1402 | | * If, for the predecessor along that edge, a register |
1403 | | * has a different value from the one we have (i.e., |
1404 | | * control paths are merging, and the merging paths |
1405 | | * assign different values to that register), give the |
1406 | | * register the undefined value of 0. |
1407 | | */ |
1408 | 0 | while ((p = p->next) != NULL) { |
1409 | 0 | for (i = 0; i < N_ATOMS; ++i) |
1410 | 0 | if (b->val[i] != p->pred->val[i]) |
1411 | 0 | b->val[i] = 0; |
1412 | 0 | } |
1413 | 0 | } |
1414 | 0 | aval = b->val[A_ATOM]; |
1415 | 0 | xval = b->val[X_ATOM]; |
1416 | 0 | for (s = b->stmts; s; s = s->next) |
1417 | 0 | opt_stmt(opt_state, &s->s, b->val, do_stmts); |
1418 | | |
1419 | | /* |
1420 | | * This is a special case: if we don't use anything from this |
1421 | | * block, and we load the accumulator or index register with a |
1422 | | * value that is already there, or if this block is a return, |
1423 | | * eliminate all the statements. |
1424 | | * |
1425 | | * XXX - what if it does a store? |
1426 | | * |
1427 | | * XXX - why does it matter whether we use anything from this |
1428 | | * block? If the accumulator or index register doesn't change |
1429 | | * its value, isn't that OK even if we use that value? |
1430 | | * |
1431 | | * XXX - if we load the accumulator with a different value, |
1432 | | * and the block ends with a conditional branch, we obviously |
1433 | | * can't eliminate it, as the branch depends on that value. |
1434 | | * For the index register, the conditional branch only depends |
1435 | | * on the index register value if the test is against the index |
1436 | | * register value rather than a constant; if nothing uses the |
1437 | | * value we put into the index register, and we're not testing |
1438 | | * against the index register's value, and there aren't any |
1439 | | * other problems that would keep us from eliminating this |
1440 | | * block, can we eliminate it? |
1441 | | */ |
1442 | 0 | if (do_stmts && |
1443 | 0 | ((b->out_use == 0 && |
1444 | 0 | aval != VAL_UNKNOWN && b->val[A_ATOM] == aval && |
1445 | 0 | xval != VAL_UNKNOWN && b->val[X_ATOM] == xval) || |
1446 | 0 | BPF_CLASS(b->s.code) == BPF_RET)) { |
1447 | 0 | if (b->stmts != 0) { |
1448 | 0 | b->stmts = 0; |
1449 | 0 | opt_state->done = 0; |
1450 | 0 | } |
1451 | 0 | } else { |
1452 | 0 | opt_peep(opt_state, b); |
1453 | 0 | opt_deadstores(opt_state, b); |
1454 | 0 | } |
1455 | | /* |
1456 | | * Set up values for branch optimizer. |
1457 | | */ |
1458 | 0 | if (BPF_SRC(b->s.code) == BPF_K) |
1459 | 0 | b->oval = K(b->s.k); |
1460 | 0 | else |
1461 | 0 | b->oval = b->val[X_ATOM]; |
1462 | 0 | b->et.code = b->s.code; |
1463 | 0 | b->ef.code = -b->s.code; |
1464 | 0 | } |
1465 | | |
1466 | | /* |
1467 | | * Return true if any register that is used on exit from 'succ', has |
1468 | | * an exit value that is different from the corresponding exit value |
1469 | | * from 'b'. |
1470 | | */ |
1471 | | static int |
1472 | | use_conflict(struct block *b, struct block *succ) |
1473 | 0 | { |
1474 | 0 | int atom; |
1475 | 0 | atomset use = succ->out_use; |
1476 | |
|
1477 | 0 | if (use == 0) |
1478 | 0 | return 0; |
1479 | | |
1480 | 0 | for (atom = 0; atom < N_ATOMS; ++atom) |
1481 | 0 | if (ATOMELEM(use, atom)) |
1482 | 0 | if (b->val[atom] != succ->val[atom]) |
1483 | 0 | return 1; |
1484 | 0 | return 0; |
1485 | 0 | } |
1486 | | |
1487 | | static struct block * |
1488 | | fold_edge(struct block *child, struct edge *ep) |
1489 | 0 | { |
1490 | 0 | int sense; |
1491 | 0 | int aval0, aval1, oval0, oval1; |
1492 | 0 | int code = ep->code; |
1493 | |
|
1494 | 0 | if (code < 0) { |
1495 | 0 | code = -code; |
1496 | 0 | sense = 0; |
1497 | 0 | } else |
1498 | 0 | sense = 1; |
1499 | |
|
1500 | 0 | if (child->s.code != code) |
1501 | 0 | return 0; |
1502 | | |
1503 | 0 | aval0 = child->val[A_ATOM]; |
1504 | 0 | oval0 = child->oval; |
1505 | 0 | aval1 = ep->pred->val[A_ATOM]; |
1506 | 0 | oval1 = ep->pred->oval; |
1507 | |
|
1508 | 0 | if (aval0 != aval1) |
1509 | 0 | return 0; |
1510 | | |
1511 | 0 | if (oval0 == oval1) |
1512 | | /* |
1513 | | * The operands of the branch instructions are |
1514 | | * identical, so the result is true if a true |
1515 | | * branch was taken to get here, otherwise false. |
1516 | | */ |
1517 | 0 | return sense ? JT(child) : JF(child); |
1518 | | |
1519 | 0 | if (sense && code == (BPF_JMP|BPF_JEQ|BPF_K)) |
1520 | | /* |
1521 | | * At this point, we only know the comparison if we |
1522 | | * came down the true branch, and it was an equality |
1523 | | * comparison with a constant. |
1524 | | * |
1525 | | * I.e., if we came down the true branch, and the branch |
1526 | | * was an equality comparison with a constant, we know the |
1527 | | * accumulator contains that constant. If we came down |
1528 | | * the false branch, or the comparison wasn't with a |
1529 | | * constant, we don't know what was in the accumulator. |
1530 | | * |
1531 | | * We rely on the fact that distinct constants have distinct |
1532 | | * value numbers. |
1533 | | */ |
1534 | 0 | return JF(child); |
1535 | | |
1536 | 0 | return 0; |
1537 | 0 | } |
1538 | | |
1539 | | static void |
1540 | | opt_j(opt_state_t *opt_state, struct edge *ep) |
1541 | 0 | { |
1542 | 0 | register int i, k; |
1543 | 0 | register struct block *target; |
1544 | |
|
1545 | 0 | if (JT(ep->succ) == 0) |
1546 | 0 | return; |
1547 | | |
1548 | 0 | if (JT(ep->succ) == JF(ep->succ)) { |
1549 | | /* |
1550 | | * Common branch targets can be eliminated, provided |
1551 | | * there is no data dependency. |
1552 | | */ |
1553 | 0 | if (!use_conflict(ep->pred, ep->succ->et.succ)) { |
1554 | 0 | opt_state->done = 0; |
1555 | 0 | ep->succ = JT(ep->succ); |
1556 | 0 | } |
1557 | 0 | } |
1558 | | /* |
1559 | | * For each edge dominator that matches the successor of this |
1560 | | * edge, promote the edge successor to the its grandchild. |
1561 | | * |
1562 | | * XXX We violate the set abstraction here in favor a reasonably |
1563 | | * efficient loop. |
1564 | | */ |
1565 | 0 | top: |
1566 | 0 | for (i = 0; i < opt_state->edgewords; ++i) { |
1567 | 0 | register bpf_u_int32 x = ep->edom[i]; |
1568 | |
|
1569 | 0 | while (x != 0) { |
1570 | 0 | k = lowest_set_bit(x); |
1571 | 0 | x &=~ ((bpf_u_int32)1 << k); |
1572 | 0 | k += i * BITS_PER_WORD; |
1573 | |
|
1574 | 0 | target = fold_edge(ep->succ, opt_state->edges[k]); |
1575 | | /* |
1576 | | * Check that there is no data dependency between |
1577 | | * nodes that will be violated if we move the edge. |
1578 | | */ |
1579 | 0 | if (target != 0 && !use_conflict(ep->pred, target)) { |
1580 | 0 | opt_state->done = 0; |
1581 | 0 | ep->succ = target; |
1582 | 0 | if (JT(target) != 0) |
1583 | | /* |
1584 | | * Start over unless we hit a leaf. |
1585 | | */ |
1586 | 0 | goto top; |
1587 | 0 | return; |
1588 | 0 | } |
1589 | 0 | } |
1590 | 0 | } |
1591 | 0 | } |
1592 | | |
1593 | | |
1594 | | static void |
1595 | | or_pullup(opt_state_t *opt_state, struct block *b) |
1596 | 0 | { |
1597 | 0 | int val, at_top; |
1598 | 0 | struct block *pull; |
1599 | 0 | struct block **diffp, **samep; |
1600 | 0 | struct edge *ep; |
1601 | |
|
1602 | 0 | ep = b->in_edges; |
1603 | 0 | if (ep == 0) |
1604 | 0 | return; |
1605 | | |
1606 | | /* |
1607 | | * Make sure each predecessor loads the same value. |
1608 | | * XXX why? |
1609 | | */ |
1610 | 0 | val = ep->pred->val[A_ATOM]; |
1611 | 0 | for (ep = ep->next; ep != 0; ep = ep->next) |
1612 | 0 | if (val != ep->pred->val[A_ATOM]) |
1613 | 0 | return; |
1614 | | |
1615 | 0 | if (JT(b->in_edges->pred) == b) |
1616 | 0 | diffp = &JT(b->in_edges->pred); |
1617 | 0 | else |
1618 | 0 | diffp = &JF(b->in_edges->pred); |
1619 | |
|
1620 | 0 | at_top = 1; |
1621 | 0 | for (;;) { |
1622 | 0 | if (*diffp == 0) |
1623 | 0 | return; |
1624 | | |
1625 | 0 | if (JT(*diffp) != JT(b)) |
1626 | 0 | return; |
1627 | | |
1628 | 0 | if (!SET_MEMBER((*diffp)->dom, b->id)) |
1629 | 0 | return; |
1630 | | |
1631 | 0 | if ((*diffp)->val[A_ATOM] != val) |
1632 | 0 | break; |
1633 | | |
1634 | 0 | diffp = &JF(*diffp); |
1635 | 0 | at_top = 0; |
1636 | 0 | } |
1637 | 0 | samep = &JF(*diffp); |
1638 | 0 | for (;;) { |
1639 | 0 | if (*samep == 0) |
1640 | 0 | return; |
1641 | | |
1642 | 0 | if (JT(*samep) != JT(b)) |
1643 | 0 | return; |
1644 | | |
1645 | 0 | if (!SET_MEMBER((*samep)->dom, b->id)) |
1646 | 0 | return; |
1647 | | |
1648 | 0 | if ((*samep)->val[A_ATOM] == val) |
1649 | 0 | break; |
1650 | | |
1651 | | /* XXX Need to check that there are no data dependencies |
1652 | | between dp0 and dp1. Currently, the code generator |
1653 | | will not produce such dependencies. */ |
1654 | 0 | samep = &JF(*samep); |
1655 | 0 | } |
1656 | | #ifdef notdef |
1657 | | /* XXX This doesn't cover everything. */ |
1658 | | for (i = 0; i < N_ATOMS; ++i) |
1659 | | if ((*samep)->val[i] != pred->val[i]) |
1660 | | return; |
1661 | | #endif |
1662 | | /* Pull up the node. */ |
1663 | 0 | pull = *samep; |
1664 | 0 | *samep = JF(pull); |
1665 | 0 | JF(pull) = *diffp; |
1666 | | |
1667 | | /* |
1668 | | * At the top of the chain, each predecessor needs to point at the |
1669 | | * pulled up node. Inside the chain, there is only one predecessor |
1670 | | * to worry about. |
1671 | | */ |
1672 | 0 | if (at_top) { |
1673 | 0 | for (ep = b->in_edges; ep != 0; ep = ep->next) { |
1674 | 0 | if (JT(ep->pred) == b) |
1675 | 0 | JT(ep->pred) = pull; |
1676 | 0 | else |
1677 | 0 | JF(ep->pred) = pull; |
1678 | 0 | } |
1679 | 0 | } |
1680 | 0 | else |
1681 | 0 | *diffp = pull; |
1682 | |
|
1683 | 0 | opt_state->done = 0; |
1684 | 0 | } |
1685 | | |
1686 | | static void |
1687 | | and_pullup(opt_state_t *opt_state, struct block *b) |
1688 | 0 | { |
1689 | 0 | int val, at_top; |
1690 | 0 | struct block *pull; |
1691 | 0 | struct block **diffp, **samep; |
1692 | 0 | struct edge *ep; |
1693 | |
|
1694 | 0 | ep = b->in_edges; |
1695 | 0 | if (ep == 0) |
1696 | 0 | return; |
1697 | | |
1698 | | /* |
1699 | | * Make sure each predecessor loads the same value. |
1700 | | */ |
1701 | 0 | val = ep->pred->val[A_ATOM]; |
1702 | 0 | for (ep = ep->next; ep != 0; ep = ep->next) |
1703 | 0 | if (val != ep->pred->val[A_ATOM]) |
1704 | 0 | return; |
1705 | | |
1706 | 0 | if (JT(b->in_edges->pred) == b) |
1707 | 0 | diffp = &JT(b->in_edges->pred); |
1708 | 0 | else |
1709 | 0 | diffp = &JF(b->in_edges->pred); |
1710 | |
|
1711 | 0 | at_top = 1; |
1712 | 0 | for (;;) { |
1713 | 0 | if (*diffp == 0) |
1714 | 0 | return; |
1715 | | |
1716 | 0 | if (JF(*diffp) != JF(b)) |
1717 | 0 | return; |
1718 | | |
1719 | 0 | if (!SET_MEMBER((*diffp)->dom, b->id)) |
1720 | 0 | return; |
1721 | | |
1722 | 0 | if ((*diffp)->val[A_ATOM] != val) |
1723 | 0 | break; |
1724 | | |
1725 | 0 | diffp = &JT(*diffp); |
1726 | 0 | at_top = 0; |
1727 | 0 | } |
1728 | 0 | samep = &JT(*diffp); |
1729 | 0 | for (;;) { |
1730 | 0 | if (*samep == 0) |
1731 | 0 | return; |
1732 | | |
1733 | 0 | if (JF(*samep) != JF(b)) |
1734 | 0 | return; |
1735 | | |
1736 | 0 | if (!SET_MEMBER((*samep)->dom, b->id)) |
1737 | 0 | return; |
1738 | | |
1739 | 0 | if ((*samep)->val[A_ATOM] == val) |
1740 | 0 | break; |
1741 | | |
1742 | | /* XXX Need to check that there are no data dependencies |
1743 | | between diffp and samep. Currently, the code generator |
1744 | | will not produce such dependencies. */ |
1745 | 0 | samep = &JT(*samep); |
1746 | 0 | } |
1747 | | #ifdef notdef |
1748 | | /* XXX This doesn't cover everything. */ |
1749 | | for (i = 0; i < N_ATOMS; ++i) |
1750 | | if ((*samep)->val[i] != pred->val[i]) |
1751 | | return; |
1752 | | #endif |
1753 | | /* Pull up the node. */ |
1754 | 0 | pull = *samep; |
1755 | 0 | *samep = JT(pull); |
1756 | 0 | JT(pull) = *diffp; |
1757 | | |
1758 | | /* |
1759 | | * At the top of the chain, each predecessor needs to point at the |
1760 | | * pulled up node. Inside the chain, there is only one predecessor |
1761 | | * to worry about. |
1762 | | */ |
1763 | 0 | if (at_top) { |
1764 | 0 | for (ep = b->in_edges; ep != 0; ep = ep->next) { |
1765 | 0 | if (JT(ep->pred) == b) |
1766 | 0 | JT(ep->pred) = pull; |
1767 | 0 | else |
1768 | 0 | JF(ep->pred) = pull; |
1769 | 0 | } |
1770 | 0 | } |
1771 | 0 | else |
1772 | 0 | *diffp = pull; |
1773 | |
|
1774 | 0 | opt_state->done = 0; |
1775 | 0 | } |
1776 | | |
1777 | | static void |
1778 | | opt_blks(opt_state_t *opt_state, struct icode *ic, int do_stmts) |
1779 | 0 | { |
1780 | 0 | int i, maxlevel; |
1781 | 0 | struct block *p; |
1782 | |
|
1783 | 0 | init_val(opt_state); |
1784 | 0 | maxlevel = ic->root->level; |
1785 | |
|
1786 | 0 | find_inedges(opt_state, ic->root); |
1787 | 0 | for (i = maxlevel; i >= 0; --i) |
1788 | 0 | for (p = opt_state->levels[i]; p; p = p->link) |
1789 | 0 | opt_blk(opt_state, p, do_stmts); |
1790 | |
|
1791 | 0 | if (do_stmts) |
1792 | | /* |
1793 | | * No point trying to move branches; it can't possibly |
1794 | | * make a difference at this point. |
1795 | | */ |
1796 | 0 | return; |
1797 | | |
1798 | 0 | for (i = 1; i <= maxlevel; ++i) { |
1799 | 0 | for (p = opt_state->levels[i]; p; p = p->link) { |
1800 | 0 | opt_j(opt_state, &p->et); |
1801 | 0 | opt_j(opt_state, &p->ef); |
1802 | 0 | } |
1803 | 0 | } |
1804 | |
|
1805 | 0 | find_inedges(opt_state, ic->root); |
1806 | 0 | for (i = 1; i <= maxlevel; ++i) { |
1807 | 0 | for (p = opt_state->levels[i]; p; p = p->link) { |
1808 | 0 | or_pullup(opt_state, p); |
1809 | 0 | and_pullup(opt_state, p); |
1810 | 0 | } |
1811 | 0 | } |
1812 | 0 | } |
1813 | | |
1814 | | static inline void |
1815 | | link_inedge(struct edge *parent, struct block *child) |
1816 | 0 | { |
1817 | 0 | parent->next = child->in_edges; |
1818 | 0 | child->in_edges = parent; |
1819 | 0 | } |
1820 | | |
1821 | | static void |
1822 | | find_inedges(opt_state_t *opt_state, struct block *root) |
1823 | 0 | { |
1824 | 0 | int i; |
1825 | 0 | struct block *b; |
1826 | |
|
1827 | 0 | for (i = 0; i < opt_state->n_blocks; ++i) |
1828 | 0 | opt_state->blocks[i]->in_edges = 0; |
1829 | | |
1830 | | /* |
1831 | | * Traverse the graph, adding each edge to the predecessor |
1832 | | * list of its successors. Skip the leaves (i.e. level 0). |
1833 | | */ |
1834 | 0 | for (i = root->level; i > 0; --i) { |
1835 | 0 | for (b = opt_state->levels[i]; b != 0; b = b->link) { |
1836 | 0 | link_inedge(&b->et, JT(b)); |
1837 | 0 | link_inedge(&b->ef, JF(b)); |
1838 | 0 | } |
1839 | 0 | } |
1840 | 0 | } |
1841 | | |
1842 | | static void |
1843 | | opt_root(struct block **b) |
1844 | 0 | { |
1845 | 0 | struct slist *tmp, *s; |
1846 | |
|
1847 | 0 | s = (*b)->stmts; |
1848 | 0 | (*b)->stmts = 0; |
1849 | 0 | while (BPF_CLASS((*b)->s.code) == BPF_JMP && JT(*b) == JF(*b)) |
1850 | 0 | *b = JT(*b); |
1851 | |
|
1852 | 0 | tmp = (*b)->stmts; |
1853 | 0 | if (tmp != 0) |
1854 | 0 | sappend(s, tmp); |
1855 | 0 | (*b)->stmts = s; |
1856 | | |
1857 | | /* |
1858 | | * If the root node is a return, then there is no |
1859 | | * point executing any statements (since the bpf machine |
1860 | | * has no side effects). |
1861 | | */ |
1862 | 0 | if (BPF_CLASS((*b)->s.code) == BPF_RET) |
1863 | 0 | (*b)->stmts = 0; |
1864 | 0 | } |
1865 | | |
1866 | | static void |
1867 | | opt_loop(opt_state_t *opt_state, struct icode *ic, int do_stmts) |
1868 | 0 | { |
1869 | |
|
1870 | | #ifdef BDEBUG |
1871 | | if (pcap_optimizer_debug > 1 || pcap_print_dot_graph) { |
1872 | | printf("opt_loop(root, %d) begin\n", do_stmts); |
1873 | | opt_dump(opt_state, ic); |
1874 | | } |
1875 | | #endif |
1876 | 0 | do { |
1877 | 0 | opt_state->done = 1; |
1878 | 0 | find_levels(opt_state, ic); |
1879 | 0 | find_dom(opt_state, ic->root); |
1880 | 0 | find_closure(opt_state, ic->root); |
1881 | 0 | find_ud(opt_state, ic->root); |
1882 | 0 | find_edom(opt_state, ic->root); |
1883 | 0 | opt_blks(opt_state, ic, do_stmts); |
1884 | | #ifdef BDEBUG |
1885 | | if (pcap_optimizer_debug > 1 || pcap_print_dot_graph) { |
1886 | | printf("opt_loop(root, %d) bottom, done=%d\n", do_stmts, opt_state->done); |
1887 | | opt_dump(opt_state, ic); |
1888 | | } |
1889 | | #endif |
1890 | 0 | } while (!opt_state->done); |
1891 | 0 | } |
1892 | | |
1893 | | /* |
1894 | | * Optimize the filter code in its dag representation. |
1895 | | * Return 0 on success, -1 on error. |
1896 | | */ |
1897 | | int |
1898 | | bpf_optimize(struct icode *ic, char *errbuf) |
1899 | 0 | { |
1900 | 0 | opt_state_t opt_state; |
1901 | |
|
1902 | 0 | memset(&opt_state, 0, sizeof(opt_state)); |
1903 | 0 | opt_state.errbuf = errbuf; |
1904 | 0 | if (setjmp(opt_state.top_ctx)) { |
1905 | 0 | opt_cleanup(&opt_state); |
1906 | 0 | return -1; |
1907 | 0 | } |
1908 | 0 | opt_init(&opt_state, ic); |
1909 | 0 | opt_loop(&opt_state, ic, 0); |
1910 | 0 | opt_loop(&opt_state, ic, 1); |
1911 | 0 | intern_blocks(&opt_state, ic); |
1912 | | #ifdef BDEBUG |
1913 | | if (pcap_optimizer_debug > 1 || pcap_print_dot_graph) { |
1914 | | printf("after intern_blocks()\n"); |
1915 | | opt_dump(&opt_state, ic); |
1916 | | } |
1917 | | #endif |
1918 | 0 | opt_root(&ic->root); |
1919 | | #ifdef BDEBUG |
1920 | | if (pcap_optimizer_debug > 1 || pcap_print_dot_graph) { |
1921 | | printf("after opt_root()\n"); |
1922 | | opt_dump(&opt_state, ic); |
1923 | | } |
1924 | | #endif |
1925 | 0 | opt_cleanup(&opt_state); |
1926 | 0 | return 0; |
1927 | 0 | } |
1928 | | |
1929 | | static void |
1930 | | make_marks(struct icode *ic, struct block *p) |
1931 | 0 | { |
1932 | 0 | if (!isMarked(ic, p)) { |
1933 | 0 | Mark(ic, p); |
1934 | 0 | if (BPF_CLASS(p->s.code) != BPF_RET) { |
1935 | 0 | make_marks(ic, JT(p)); |
1936 | 0 | make_marks(ic, JF(p)); |
1937 | 0 | } |
1938 | 0 | } |
1939 | 0 | } |
1940 | | |
1941 | | /* |
1942 | | * Mark code array such that isMarked(ic->cur_mark, i) is true |
1943 | | * only for nodes that are alive. |
1944 | | */ |
1945 | | static void |
1946 | | mark_code(struct icode *ic) |
1947 | 0 | { |
1948 | 0 | ic->cur_mark += 1; |
1949 | 0 | make_marks(ic, ic->root); |
1950 | 0 | } |
1951 | | |
1952 | | /* |
1953 | | * True iff the two stmt lists load the same value from the packet into |
1954 | | * the accumulator. |
1955 | | */ |
1956 | | static int |
1957 | | eq_slist(struct slist *x, struct slist *y) |
1958 | 0 | { |
1959 | 0 | for (;;) { |
1960 | 0 | while (x && x->s.code == NOP) |
1961 | 0 | x = x->next; |
1962 | 0 | while (y && y->s.code == NOP) |
1963 | 0 | y = y->next; |
1964 | 0 | if (x == 0) |
1965 | 0 | return y == 0; |
1966 | 0 | if (y == 0) |
1967 | 0 | return x == 0; |
1968 | 0 | if (x->s.code != y->s.code || x->s.k != y->s.k) |
1969 | 0 | return 0; |
1970 | 0 | x = x->next; |
1971 | 0 | y = y->next; |
1972 | 0 | } |
1973 | 0 | } |
1974 | | |
1975 | | static inline int |
1976 | | eq_blk(struct block *b0, struct block *b1) |
1977 | 0 | { |
1978 | 0 | if (b0->s.code == b1->s.code && |
1979 | 0 | b0->s.k == b1->s.k && |
1980 | 0 | b0->et.succ == b1->et.succ && |
1981 | 0 | b0->ef.succ == b1->ef.succ) |
1982 | 0 | return eq_slist(b0->stmts, b1->stmts); |
1983 | 0 | return 0; |
1984 | 0 | } |
1985 | | |
1986 | | static void |
1987 | | intern_blocks(opt_state_t *opt_state, struct icode *ic) |
1988 | 0 | { |
1989 | 0 | struct block *p; |
1990 | 0 | int i, j; |
1991 | 0 | int done1; /* don't shadow global */ |
1992 | 0 | top: |
1993 | 0 | done1 = 1; |
1994 | 0 | for (i = 0; i < opt_state->n_blocks; ++i) |
1995 | 0 | opt_state->blocks[i]->link = 0; |
1996 | |
|
1997 | 0 | mark_code(ic); |
1998 | |
|
1999 | 0 | for (i = opt_state->n_blocks - 1; --i >= 0; ) { |
2000 | 0 | if (!isMarked(ic, opt_state->blocks[i])) |
2001 | 0 | continue; |
2002 | 0 | for (j = i + 1; j < opt_state->n_blocks; ++j) { |
2003 | 0 | if (!isMarked(ic, opt_state->blocks[j])) |
2004 | 0 | continue; |
2005 | 0 | if (eq_blk(opt_state->blocks[i], opt_state->blocks[j])) { |
2006 | 0 | opt_state->blocks[i]->link = opt_state->blocks[j]->link ? |
2007 | 0 | opt_state->blocks[j]->link : opt_state->blocks[j]; |
2008 | 0 | break; |
2009 | 0 | } |
2010 | 0 | } |
2011 | 0 | } |
2012 | 0 | for (i = 0; i < opt_state->n_blocks; ++i) { |
2013 | 0 | p = opt_state->blocks[i]; |
2014 | 0 | if (JT(p) == 0) |
2015 | 0 | continue; |
2016 | 0 | if (JT(p)->link) { |
2017 | 0 | done1 = 0; |
2018 | 0 | JT(p) = JT(p)->link; |
2019 | 0 | } |
2020 | 0 | if (JF(p)->link) { |
2021 | 0 | done1 = 0; |
2022 | 0 | JF(p) = JF(p)->link; |
2023 | 0 | } |
2024 | 0 | } |
2025 | 0 | if (!done1) |
2026 | 0 | goto top; |
2027 | 0 | } |
2028 | | |
2029 | | static void |
2030 | | opt_cleanup(opt_state_t *opt_state) |
2031 | 0 | { |
2032 | 0 | free((void *)opt_state->vnode_base); |
2033 | 0 | free((void *)opt_state->vmap); |
2034 | 0 | free((void *)opt_state->edges); |
2035 | 0 | free((void *)opt_state->space); |
2036 | 0 | free((void *)opt_state->levels); |
2037 | 0 | free((void *)opt_state->blocks); |
2038 | 0 | } |
2039 | | |
2040 | | /* |
2041 | | * For optimizer errors. |
2042 | | */ |
2043 | | static void PCAP_NORETURN |
2044 | | opt_error(opt_state_t *opt_state, const char *fmt, ...) |
2045 | 0 | { |
2046 | 0 | va_list ap; |
2047 | |
|
2048 | 0 | if (opt_state->errbuf != NULL) { |
2049 | 0 | va_start(ap, fmt); |
2050 | 0 | (void)pcap_vsnprintf(opt_state->errbuf, |
2051 | 0 | PCAP_ERRBUF_SIZE, fmt, ap); |
2052 | 0 | va_end(ap); |
2053 | 0 | } |
2054 | 0 | longjmp(opt_state->top_ctx, 1); |
2055 | | /* NOTREACHED */ |
2056 | 0 | } |
2057 | | |
2058 | | /* |
2059 | | * Return the number of stmts in 's'. |
2060 | | */ |
2061 | | static u_int |
2062 | | slength(struct slist *s) |
2063 | 0 | { |
2064 | 0 | u_int n = 0; |
2065 | |
|
2066 | 0 | for (; s; s = s->next) |
2067 | 0 | if (s->s.code != NOP) |
2068 | 0 | ++n; |
2069 | 0 | return n; |
2070 | 0 | } |
2071 | | |
2072 | | /* |
2073 | | * Return the number of nodes reachable by 'p'. |
2074 | | * All nodes should be initially unmarked. |
2075 | | */ |
2076 | | static int |
2077 | | count_blocks(struct icode *ic, struct block *p) |
2078 | 0 | { |
2079 | 0 | if (p == 0 || isMarked(ic, p)) |
2080 | 0 | return 0; |
2081 | 0 | Mark(ic, p); |
2082 | 0 | return count_blocks(ic, JT(p)) + count_blocks(ic, JF(p)) + 1; |
2083 | 0 | } |
2084 | | |
2085 | | /* |
2086 | | * Do a depth first search on the flow graph, numbering the |
2087 | | * the basic blocks, and entering them into the 'blocks' array.` |
2088 | | */ |
2089 | | static void |
2090 | | number_blks_r(opt_state_t *opt_state, struct icode *ic, struct block *p) |
2091 | 0 | { |
2092 | 0 | int n; |
2093 | |
|
2094 | 0 | if (p == 0 || isMarked(ic, p)) |
2095 | 0 | return; |
2096 | | |
2097 | 0 | Mark(ic, p); |
2098 | 0 | n = opt_state->n_blocks++; |
2099 | 0 | p->id = n; |
2100 | 0 | opt_state->blocks[n] = p; |
2101 | |
|
2102 | 0 | number_blks_r(opt_state, ic, JT(p)); |
2103 | 0 | number_blks_r(opt_state, ic, JF(p)); |
2104 | 0 | } |
2105 | | |
2106 | | /* |
2107 | | * Return the number of stmts in the flowgraph reachable by 'p'. |
2108 | | * The nodes should be unmarked before calling. |
2109 | | * |
2110 | | * Note that "stmts" means "instructions", and that this includes |
2111 | | * |
2112 | | * side-effect statements in 'p' (slength(p->stmts)); |
2113 | | * |
2114 | | * statements in the true branch from 'p' (count_stmts(JT(p))); |
2115 | | * |
2116 | | * statements in the false branch from 'p' (count_stmts(JF(p))); |
2117 | | * |
2118 | | * the conditional jump itself (1); |
2119 | | * |
2120 | | * an extra long jump if the true branch requires it (p->longjt); |
2121 | | * |
2122 | | * an extra long jump if the false branch requires it (p->longjf). |
2123 | | */ |
2124 | | static u_int |
2125 | | count_stmts(struct icode *ic, struct block *p) |
2126 | 0 | { |
2127 | 0 | u_int n; |
2128 | |
|
2129 | 0 | if (p == 0 || isMarked(ic, p)) |
2130 | 0 | return 0; |
2131 | 0 | Mark(ic, p); |
2132 | 0 | n = count_stmts(ic, JT(p)) + count_stmts(ic, JF(p)); |
2133 | 0 | return slength(p->stmts) + n + 1 + p->longjt + p->longjf; |
2134 | 0 | } |
2135 | | |
2136 | | /* |
2137 | | * Allocate memory. All allocation is done before optimization |
2138 | | * is begun. A linear bound on the size of all data structures is computed |
2139 | | * from the total number of blocks and/or statements. |
2140 | | */ |
2141 | | static void |
2142 | | opt_init(opt_state_t *opt_state, struct icode *ic) |
2143 | 0 | { |
2144 | 0 | bpf_u_int32 *p; |
2145 | 0 | int i, n, max_stmts; |
2146 | | |
2147 | | /* |
2148 | | * First, count the blocks, so we can malloc an array to map |
2149 | | * block number to block. Then, put the blocks into the array. |
2150 | | */ |
2151 | 0 | unMarkAll(ic); |
2152 | 0 | n = count_blocks(ic, ic->root); |
2153 | 0 | opt_state->blocks = (struct block **)calloc(n, sizeof(*opt_state->blocks)); |
2154 | 0 | if (opt_state->blocks == NULL) |
2155 | 0 | opt_error(opt_state, "malloc"); |
2156 | 0 | unMarkAll(ic); |
2157 | 0 | opt_state->n_blocks = 0; |
2158 | 0 | number_blks_r(opt_state, ic, ic->root); |
2159 | |
|
2160 | 0 | opt_state->n_edges = 2 * opt_state->n_blocks; |
2161 | 0 | opt_state->edges = (struct edge **)calloc(opt_state->n_edges, sizeof(*opt_state->edges)); |
2162 | 0 | if (opt_state->edges == NULL) { |
2163 | 0 | opt_error(opt_state, "malloc"); |
2164 | 0 | } |
2165 | | |
2166 | | /* |
2167 | | * The number of levels is bounded by the number of nodes. |
2168 | | */ |
2169 | 0 | opt_state->levels = (struct block **)calloc(opt_state->n_blocks, sizeof(*opt_state->levels)); |
2170 | 0 | if (opt_state->levels == NULL) { |
2171 | 0 | opt_error(opt_state, "malloc"); |
2172 | 0 | } |
2173 | | |
2174 | 0 | opt_state->edgewords = opt_state->n_edges / (8 * sizeof(bpf_u_int32)) + 1; |
2175 | 0 | opt_state->nodewords = opt_state->n_blocks / (8 * sizeof(bpf_u_int32)) + 1; |
2176 | | |
2177 | | /* XXX */ |
2178 | 0 | opt_state->space = (bpf_u_int32 *)malloc(2 * opt_state->n_blocks * opt_state->nodewords * sizeof(*opt_state->space) |
2179 | 0 | + opt_state->n_edges * opt_state->edgewords * sizeof(*opt_state->space)); |
2180 | 0 | if (opt_state->space == NULL) { |
2181 | 0 | opt_error(opt_state, "malloc"); |
2182 | 0 | } |
2183 | 0 | p = opt_state->space; |
2184 | 0 | opt_state->all_dom_sets = p; |
2185 | 0 | for (i = 0; i < n; ++i) { |
2186 | 0 | opt_state->blocks[i]->dom = p; |
2187 | 0 | p += opt_state->nodewords; |
2188 | 0 | } |
2189 | 0 | opt_state->all_closure_sets = p; |
2190 | 0 | for (i = 0; i < n; ++i) { |
2191 | 0 | opt_state->blocks[i]->closure = p; |
2192 | 0 | p += opt_state->nodewords; |
2193 | 0 | } |
2194 | 0 | opt_state->all_edge_sets = p; |
2195 | 0 | for (i = 0; i < n; ++i) { |
2196 | 0 | register struct block *b = opt_state->blocks[i]; |
2197 | |
|
2198 | 0 | b->et.edom = p; |
2199 | 0 | p += opt_state->edgewords; |
2200 | 0 | b->ef.edom = p; |
2201 | 0 | p += opt_state->edgewords; |
2202 | 0 | b->et.id = i; |
2203 | 0 | opt_state->edges[i] = &b->et; |
2204 | 0 | b->ef.id = opt_state->n_blocks + i; |
2205 | 0 | opt_state->edges[opt_state->n_blocks + i] = &b->ef; |
2206 | 0 | b->et.pred = b; |
2207 | 0 | b->ef.pred = b; |
2208 | 0 | } |
2209 | 0 | max_stmts = 0; |
2210 | 0 | for (i = 0; i < n; ++i) |
2211 | 0 | max_stmts += slength(opt_state->blocks[i]->stmts) + 1; |
2212 | | /* |
2213 | | * We allocate at most 3 value numbers per statement, |
2214 | | * so this is an upper bound on the number of valnodes |
2215 | | * we'll need. |
2216 | | */ |
2217 | 0 | opt_state->maxval = 3 * max_stmts; |
2218 | 0 | opt_state->vmap = (struct vmapinfo *)calloc(opt_state->maxval, sizeof(*opt_state->vmap)); |
2219 | 0 | if (opt_state->vmap == NULL) { |
2220 | 0 | opt_error(opt_state, "malloc"); |
2221 | 0 | } |
2222 | 0 | opt_state->vnode_base = (struct valnode *)calloc(opt_state->maxval, sizeof(*opt_state->vnode_base)); |
2223 | 0 | if (opt_state->vnode_base == NULL) { |
2224 | 0 | opt_error(opt_state, "malloc"); |
2225 | 0 | } |
2226 | 0 | } |
2227 | | |
2228 | | /* |
2229 | | * This is only used when supporting optimizer debugging. It is |
2230 | | * global state, so do *not* do more than one compile in parallel |
2231 | | * and expect it to provide meaningful information. |
2232 | | */ |
2233 | | #ifdef BDEBUG |
2234 | | int bids[NBIDS]; |
2235 | | #endif |
2236 | | |
2237 | | static void PCAP_NORETURN conv_error(conv_state_t *, const char *, ...) |
2238 | | PCAP_PRINTFLIKE(2, 3); |
2239 | | |
2240 | | /* |
2241 | | * Returns true if successful. Returns false if a branch has |
2242 | | * an offset that is too large. If so, we have marked that |
2243 | | * branch so that on a subsequent iteration, it will be treated |
2244 | | * properly. |
2245 | | */ |
2246 | | static int |
2247 | | convert_code_r(conv_state_t *conv_state, struct icode *ic, struct block *p) |
2248 | 0 | { |
2249 | 0 | struct bpf_insn *dst; |
2250 | 0 | struct slist *src; |
2251 | 0 | u_int slen; |
2252 | 0 | u_int off; |
2253 | 0 | u_int extrajmps; /* number of extra jumps inserted */ |
2254 | 0 | struct slist **offset = NULL; |
2255 | |
|
2256 | 0 | if (p == 0 || isMarked(ic, p)) |
2257 | 0 | return (1); |
2258 | 0 | Mark(ic, p); |
2259 | |
|
2260 | 0 | if (convert_code_r(conv_state, ic, JF(p)) == 0) |
2261 | 0 | return (0); |
2262 | 0 | if (convert_code_r(conv_state, ic, JT(p)) == 0) |
2263 | 0 | return (0); |
2264 | | |
2265 | 0 | slen = slength(p->stmts); |
2266 | 0 | dst = conv_state->ftail -= (slen + 1 + p->longjt + p->longjf); |
2267 | | /* inflate length by any extra jumps */ |
2268 | |
|
2269 | 0 | p->offset = (int)(dst - conv_state->fstart); |
2270 | | |
2271 | | /* generate offset[] for convenience */ |
2272 | 0 | if (slen) { |
2273 | 0 | offset = (struct slist **)calloc(slen, sizeof(struct slist *)); |
2274 | 0 | if (!offset) { |
2275 | 0 | conv_error(conv_state, "not enough core"); |
2276 | | /*NOTREACHED*/ |
2277 | 0 | } |
2278 | 0 | } |
2279 | 0 | src = p->stmts; |
2280 | 0 | for (off = 0; off < slen && src; off++) { |
2281 | | #if 0 |
2282 | | printf("off=%d src=%x\n", off, src); |
2283 | | #endif |
2284 | 0 | offset[off] = src; |
2285 | 0 | src = src->next; |
2286 | 0 | } |
2287 | |
|
2288 | 0 | off = 0; |
2289 | 0 | for (src = p->stmts; src; src = src->next) { |
2290 | 0 | if (src->s.code == NOP) |
2291 | 0 | continue; |
2292 | 0 | dst->code = (u_short)src->s.code; |
2293 | 0 | dst->k = src->s.k; |
2294 | | |
2295 | | /* fill block-local relative jump */ |
2296 | 0 | if (BPF_CLASS(src->s.code) != BPF_JMP || src->s.code == (BPF_JMP|BPF_JA)) { |
2297 | | #if 0 |
2298 | | if (src->s.jt || src->s.jf) { |
2299 | | free(offset); |
2300 | | conv_error(conv_state, "illegal jmp destination"); |
2301 | | /*NOTREACHED*/ |
2302 | | } |
2303 | | #endif |
2304 | 0 | goto filled; |
2305 | 0 | } |
2306 | 0 | if (off == slen - 2) /*???*/ |
2307 | 0 | goto filled; |
2308 | | |
2309 | 0 | { |
2310 | 0 | u_int i; |
2311 | 0 | int jt, jf; |
2312 | 0 | const char ljerr[] = "%s for block-local relative jump: off=%d"; |
2313 | |
|
2314 | | #if 0 |
2315 | | printf("code=%x off=%d %x %x\n", src->s.code, |
2316 | | off, src->s.jt, src->s.jf); |
2317 | | #endif |
2318 | |
|
2319 | 0 | if (!src->s.jt || !src->s.jf) { |
2320 | 0 | free(offset); |
2321 | 0 | conv_error(conv_state, ljerr, "no jmp destination", off); |
2322 | | /*NOTREACHED*/ |
2323 | 0 | } |
2324 | | |
2325 | 0 | jt = jf = 0; |
2326 | 0 | for (i = 0; i < slen; i++) { |
2327 | 0 | if (offset[i] == src->s.jt) { |
2328 | 0 | if (jt) { |
2329 | 0 | free(offset); |
2330 | 0 | conv_error(conv_state, ljerr, "multiple matches", off); |
2331 | | /*NOTREACHED*/ |
2332 | 0 | } |
2333 | | |
2334 | 0 | if (i - off - 1 >= 256) { |
2335 | 0 | free(offset); |
2336 | 0 | conv_error(conv_state, ljerr, "out-of-range jump", off); |
2337 | | /*NOTREACHED*/ |
2338 | 0 | } |
2339 | 0 | dst->jt = (u_char)(i - off - 1); |
2340 | 0 | jt++; |
2341 | 0 | } |
2342 | 0 | if (offset[i] == src->s.jf) { |
2343 | 0 | if (jf) { |
2344 | 0 | free(offset); |
2345 | 0 | conv_error(conv_state, ljerr, "multiple matches", off); |
2346 | | /*NOTREACHED*/ |
2347 | 0 | } |
2348 | 0 | if (i - off - 1 >= 256) { |
2349 | 0 | free(offset); |
2350 | 0 | conv_error(conv_state, ljerr, "out-of-range jump", off); |
2351 | | /*NOTREACHED*/ |
2352 | 0 | } |
2353 | 0 | dst->jf = (u_char)(i - off - 1); |
2354 | 0 | jf++; |
2355 | 0 | } |
2356 | 0 | } |
2357 | 0 | if (!jt || !jf) { |
2358 | 0 | free(offset); |
2359 | 0 | conv_error(conv_state, ljerr, "no destination found", off); |
2360 | | /*NOTREACHED*/ |
2361 | 0 | } |
2362 | 0 | } |
2363 | 0 | filled: |
2364 | 0 | ++dst; |
2365 | 0 | ++off; |
2366 | 0 | } |
2367 | 0 | if (offset) |
2368 | 0 | free(offset); |
2369 | |
|
2370 | | #ifdef BDEBUG |
2371 | | if (dst - conv_state->fstart < NBIDS) |
2372 | | bids[dst - conv_state->fstart] = p->id + 1; |
2373 | | #endif |
2374 | 0 | dst->code = (u_short)p->s.code; |
2375 | 0 | dst->k = p->s.k; |
2376 | 0 | if (JT(p)) { |
2377 | 0 | extrajmps = 0; |
2378 | 0 | off = JT(p)->offset - (p->offset + slen) - 1; |
2379 | 0 | if (off >= 256) { |
2380 | | /* offset too large for branch, must add a jump */ |
2381 | 0 | if (p->longjt == 0) { |
2382 | | /* mark this instruction and retry */ |
2383 | 0 | p->longjt++; |
2384 | 0 | return(0); |
2385 | 0 | } |
2386 | | /* branch if T to following jump */ |
2387 | 0 | if (extrajmps >= 256) { |
2388 | 0 | conv_error(conv_state, "too many extra jumps"); |
2389 | | /*NOTREACHED*/ |
2390 | 0 | } |
2391 | 0 | dst->jt = (u_char)extrajmps; |
2392 | 0 | extrajmps++; |
2393 | 0 | dst[extrajmps].code = BPF_JMP|BPF_JA; |
2394 | 0 | dst[extrajmps].k = off - extrajmps; |
2395 | 0 | } |
2396 | 0 | else |
2397 | 0 | dst->jt = (u_char)off; |
2398 | 0 | off = JF(p)->offset - (p->offset + slen) - 1; |
2399 | 0 | if (off >= 256) { |
2400 | | /* offset too large for branch, must add a jump */ |
2401 | 0 | if (p->longjf == 0) { |
2402 | | /* mark this instruction and retry */ |
2403 | 0 | p->longjf++; |
2404 | 0 | return(0); |
2405 | 0 | } |
2406 | | /* branch if F to following jump */ |
2407 | | /* if two jumps are inserted, F goes to second one */ |
2408 | 0 | if (extrajmps >= 256) { |
2409 | 0 | conv_error(conv_state, "too many extra jumps"); |
2410 | | /*NOTREACHED*/ |
2411 | 0 | } |
2412 | 0 | dst->jf = (u_char)extrajmps; |
2413 | 0 | extrajmps++; |
2414 | 0 | dst[extrajmps].code = BPF_JMP|BPF_JA; |
2415 | 0 | dst[extrajmps].k = off - extrajmps; |
2416 | 0 | } |
2417 | 0 | else |
2418 | 0 | dst->jf = (u_char)off; |
2419 | 0 | } |
2420 | 0 | return (1); |
2421 | 0 | } |
2422 | | |
2423 | | |
2424 | | /* |
2425 | | * Convert flowgraph intermediate representation to the |
2426 | | * BPF array representation. Set *lenp to the number of instructions. |
2427 | | * |
2428 | | * This routine does *NOT* leak the memory pointed to by fp. It *must |
2429 | | * not* do free(fp) before returning fp; doing so would make no sense, |
2430 | | * as the BPF array pointed to by the return value of icode_to_fcode() |
2431 | | * must be valid - it's being returned for use in a bpf_program structure. |
2432 | | * |
2433 | | * If it appears that icode_to_fcode() is leaking, the problem is that |
2434 | | * the program using pcap_compile() is failing to free the memory in |
2435 | | * the BPF program when it's done - the leak is in the program, not in |
2436 | | * the routine that happens to be allocating the memory. (By analogy, if |
2437 | | * a program calls fopen() without ever calling fclose() on the FILE *, |
2438 | | * it will leak the FILE structure; the leak is not in fopen(), it's in |
2439 | | * the program.) Change the program to use pcap_freecode() when it's |
2440 | | * done with the filter program. See the pcap man page. |
2441 | | */ |
2442 | | struct bpf_insn * |
2443 | | icode_to_fcode(struct icode *ic, struct block *root, u_int *lenp, |
2444 | | char *errbuf) |
2445 | 0 | { |
2446 | 0 | u_int n; |
2447 | 0 | struct bpf_insn *fp; |
2448 | 0 | conv_state_t conv_state; |
2449 | |
|
2450 | 0 | conv_state.fstart = NULL; |
2451 | 0 | conv_state.errbuf = errbuf; |
2452 | 0 | if (setjmp(conv_state.top_ctx) != 0) { |
2453 | 0 | free(conv_state.fstart); |
2454 | 0 | return NULL; |
2455 | 0 | } |
2456 | | |
2457 | | /* |
2458 | | * Loop doing convert_code_r() until no branches remain |
2459 | | * with too-large offsets. |
2460 | | */ |
2461 | 0 | for (;;) { |
2462 | 0 | unMarkAll(ic); |
2463 | 0 | n = *lenp = count_stmts(ic, root); |
2464 | |
|
2465 | 0 | fp = (struct bpf_insn *)malloc(sizeof(*fp) * n); |
2466 | 0 | if (fp == NULL) { |
2467 | 0 | (void)pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, |
2468 | 0 | "malloc"); |
2469 | 0 | free(fp); |
2470 | 0 | return NULL; |
2471 | 0 | } |
2472 | 0 | memset((char *)fp, 0, sizeof(*fp) * n); |
2473 | 0 | conv_state.fstart = fp; |
2474 | 0 | conv_state.ftail = fp + n; |
2475 | |
|
2476 | 0 | unMarkAll(ic); |
2477 | 0 | if (convert_code_r(&conv_state, ic, root)) |
2478 | 0 | break; |
2479 | 0 | free(fp); |
2480 | 0 | } |
2481 | | |
2482 | 0 | return fp; |
2483 | 0 | } |
2484 | | |
2485 | | /* |
2486 | | * For iconv_to_fconv() errors. |
2487 | | */ |
2488 | | static void PCAP_NORETURN |
2489 | | conv_error(conv_state_t *conv_state, const char *fmt, ...) |
2490 | 0 | { |
2491 | 0 | va_list ap; |
2492 | |
|
2493 | 0 | va_start(ap, fmt); |
2494 | 0 | (void)pcap_vsnprintf(conv_state->errbuf, |
2495 | 0 | PCAP_ERRBUF_SIZE, fmt, ap); |
2496 | 0 | va_end(ap); |
2497 | 0 | longjmp(conv_state->top_ctx, 1); |
2498 | | /* NOTREACHED */ |
2499 | 0 | } |
2500 | | |
2501 | | /* |
2502 | | * Make a copy of a BPF program and put it in the "fcode" member of |
2503 | | * a "pcap_t". |
2504 | | * |
2505 | | * If we fail to allocate memory for the copy, fill in the "errbuf" |
2506 | | * member of the "pcap_t" with an error message, and return -1; |
2507 | | * otherwise, return 0. |
2508 | | */ |
2509 | | int |
2510 | | install_bpf_program(pcap_t *p, struct bpf_program *fp) |
2511 | 0 | { |
2512 | 0 | size_t prog_size; |
2513 | | |
2514 | | /* |
2515 | | * Validate the program. |
2516 | | */ |
2517 | 0 | if (!bpf_validate(fp->bf_insns, fp->bf_len)) { |
2518 | 0 | pcap_snprintf(p->errbuf, sizeof(p->errbuf), |
2519 | 0 | "BPF program is not valid"); |
2520 | 0 | return (-1); |
2521 | 0 | } |
2522 | | |
2523 | | /* |
2524 | | * Free up any already installed program. |
2525 | | */ |
2526 | 0 | pcap_freecode(&p->fcode); |
2527 | |
|
2528 | 0 | prog_size = sizeof(*fp->bf_insns) * fp->bf_len; |
2529 | 0 | p->fcode.bf_len = fp->bf_len; |
2530 | 0 | p->fcode.bf_insns = (struct bpf_insn *)malloc(prog_size); |
2531 | 0 | if (p->fcode.bf_insns == NULL) { |
2532 | 0 | pcap_fmt_errmsg_for_errno(p->errbuf, sizeof(p->errbuf), |
2533 | 0 | errno, "malloc"); |
2534 | 0 | return (-1); |
2535 | 0 | } |
2536 | 0 | memcpy(p->fcode.bf_insns, fp->bf_insns, prog_size); |
2537 | 0 | return (0); |
2538 | 0 | } |
2539 | | |
2540 | | #ifdef BDEBUG |
2541 | | static void |
2542 | | dot_dump_node(struct icode *ic, struct block *block, struct bpf_program *prog, |
2543 | | FILE *out) |
2544 | | { |
2545 | | int icount, noffset; |
2546 | | int i; |
2547 | | |
2548 | | if (block == NULL || isMarked(ic, block)) |
2549 | | return; |
2550 | | Mark(ic, block); |
2551 | | |
2552 | | icount = slength(block->stmts) + 1 + block->longjt + block->longjf; |
2553 | | noffset = min(block->offset + icount, (int)prog->bf_len); |
2554 | | |
2555 | | fprintf(out, "\tblock%d [shape=ellipse, id=\"block-%d\" label=\"BLOCK%d\\n", block->id, block->id, block->id); |
2556 | | for (i = block->offset; i < noffset; i++) { |
2557 | | fprintf(out, "\\n%s", bpf_image(prog->bf_insns + i, i)); |
2558 | | } |
2559 | | fprintf(out, "\" tooltip=\""); |
2560 | | for (i = 0; i < BPF_MEMWORDS; i++) |
2561 | | if (block->val[i] != VAL_UNKNOWN) |
2562 | | fprintf(out, "val[%d]=%d ", i, block->val[i]); |
2563 | | fprintf(out, "val[A]=%d ", block->val[A_ATOM]); |
2564 | | fprintf(out, "val[X]=%d", block->val[X_ATOM]); |
2565 | | fprintf(out, "\""); |
2566 | | if (JT(block) == NULL) |
2567 | | fprintf(out, ", peripheries=2"); |
2568 | | fprintf(out, "];\n"); |
2569 | | |
2570 | | dot_dump_node(ic, JT(block), prog, out); |
2571 | | dot_dump_node(ic, JF(block), prog, out); |
2572 | | } |
2573 | | |
2574 | | static void |
2575 | | dot_dump_edge(struct icode *ic, struct block *block, FILE *out) |
2576 | | { |
2577 | | if (block == NULL || isMarked(ic, block)) |
2578 | | return; |
2579 | | Mark(ic, block); |
2580 | | |
2581 | | if (JT(block)) { |
2582 | | fprintf(out, "\t\"block%d\":se -> \"block%d\":n [label=\"T\"]; \n", |
2583 | | block->id, JT(block)->id); |
2584 | | fprintf(out, "\t\"block%d\":sw -> \"block%d\":n [label=\"F\"]; \n", |
2585 | | block->id, JF(block)->id); |
2586 | | } |
2587 | | dot_dump_edge(ic, JT(block), out); |
2588 | | dot_dump_edge(ic, JF(block), out); |
2589 | | } |
2590 | | |
2591 | | /* Output the block CFG using graphviz/DOT language |
2592 | | * In the CFG, block's code, value index for each registers at EXIT, |
2593 | | * and the jump relationship is show. |
2594 | | * |
2595 | | * example DOT for BPF `ip src host 1.1.1.1' is: |
2596 | | digraph BPF { |
2597 | | block0 [shape=ellipse, id="block-0" label="BLOCK0\n\n(000) ldh [12]\n(001) jeq #0x800 jt 2 jf 5" tooltip="val[A]=0 val[X]=0"]; |
2598 | | block1 [shape=ellipse, id="block-1" label="BLOCK1\n\n(002) ld [26]\n(003) jeq #0x1010101 jt 4 jf 5" tooltip="val[A]=0 val[X]=0"]; |
2599 | | block2 [shape=ellipse, id="block-2" label="BLOCK2\n\n(004) ret #68" tooltip="val[A]=0 val[X]=0", peripheries=2]; |
2600 | | block3 [shape=ellipse, id="block-3" label="BLOCK3\n\n(005) ret #0" tooltip="val[A]=0 val[X]=0", peripheries=2]; |
2601 | | "block0":se -> "block1":n [label="T"]; |
2602 | | "block0":sw -> "block3":n [label="F"]; |
2603 | | "block1":se -> "block2":n [label="T"]; |
2604 | | "block1":sw -> "block3":n [label="F"]; |
2605 | | } |
2606 | | * |
2607 | | * After install graphviz on http://www.graphviz.org/, save it as bpf.dot |
2608 | | * and run `dot -Tpng -O bpf.dot' to draw the graph. |
2609 | | */ |
2610 | | static int |
2611 | | dot_dump(struct icode *ic, char *errbuf) |
2612 | | { |
2613 | | struct bpf_program f; |
2614 | | FILE *out = stdout; |
2615 | | |
2616 | | memset(bids, 0, sizeof bids); |
2617 | | f.bf_insns = icode_to_fcode(ic, ic->root, &f.bf_len, errbuf); |
2618 | | if (f.bf_insns == NULL) |
2619 | | return -1; |
2620 | | |
2621 | | fprintf(out, "digraph BPF {\n"); |
2622 | | unMarkAll(ic); |
2623 | | dot_dump_node(ic, ic->root, &f, out); |
2624 | | unMarkAll(ic); |
2625 | | dot_dump_edge(ic, ic->root, out); |
2626 | | fprintf(out, "}\n"); |
2627 | | |
2628 | | free((char *)f.bf_insns); |
2629 | | return 0; |
2630 | | } |
2631 | | |
2632 | | static int |
2633 | | plain_dump(struct icode *ic, char *errbuf) |
2634 | | { |
2635 | | struct bpf_program f; |
2636 | | |
2637 | | memset(bids, 0, sizeof bids); |
2638 | | f.bf_insns = icode_to_fcode(ic, ic->root, &f.bf_len, errbuf); |
2639 | | if (f.bf_insns == NULL) |
2640 | | return -1; |
2641 | | bpf_dump(&f, 1); |
2642 | | putchar('\n'); |
2643 | | free((char *)f.bf_insns); |
2644 | | return 0; |
2645 | | } |
2646 | | |
2647 | | static void |
2648 | | opt_dump(opt_state_t *opt_state, struct icode *ic) |
2649 | | { |
2650 | | int status; |
2651 | | char errbuf[PCAP_ERRBUF_SIZE]; |
2652 | | |
2653 | | /* |
2654 | | * If the CFG, in DOT format, is requested, output it rather than |
2655 | | * the code that would be generated from that graph. |
2656 | | */ |
2657 | | if (pcap_print_dot_graph) |
2658 | | status = dot_dump(ic, errbuf); |
2659 | | else |
2660 | | status = plain_dump(ic, errbuf); |
2661 | | if (status == -1) |
2662 | | opt_error(opt_state, "opt_dump: icode_to_fcode failed: %s", errbuf); |
2663 | | } |
2664 | | #endif |