]> The Tcpdump Group git mirrors - libpcap/commitdiff
Mind all-zeroes mask in gen_mcmp().
authorDenis Ovsienko <[email protected]>
Fri, 28 Mar 2025 23:18:28 +0000 (23:18 +0000)
committerDenis Ovsienko <[email protected]>
Tue, 8 Apr 2025 10:38:16 +0000 (11:38 +0100)
In this function mask 0 is an edge case, handle it appropriately and
take a shortcut to either gen_true() or gan_false().

All tested optimized filter programs remain the same.  IPv4 netmask /0
and IPv6 netmasks shorter than /96 now have shorter unoptimized filter
programs: the number of always-true comparisons stays the same, but each
takes two instructions instead of three.

gencode.c
testprogs/TESTrun

index af3e0945b774f93e6b4a095f58052325959432aa..d36abbeb52d6197b48d64e83485b40ae98bd90cd 100644 (file)
--- a/gencode.c
+++ b/gencode.c
@@ -1459,6 +1459,19 @@ static struct block *
 gen_mcmp(compiler_state_t *cstate, enum e_offrel offrel, u_int offset,
     u_int size, bpf_u_int32 v, bpf_u_int32 mask)
 {
+       /*
+        * For any A: if mask == 0, it means A & mask == 0, so the result is
+        * true iff v == 0.  In this case ideally the caller should have
+        * skipped this invocation and have fewer statement blocks to juggle.
+        * If the caller could have skipped, but has not, produce a block with
+        * fewer statements.
+        *
+        * This could be done in gen_ncmp() in a more generic way, but this
+        * function is the only code path that can have mask == 0.
+        */
+       if (mask == 0)
+               return v ? gen_false(cstate) : gen_true(cstate);
+
        return gen_ncmp(cstate, offrel, offset, size, mask, BPF_JEQ, 0, v);
 }
 
index 3fe263cdb09e9d0b0bd603a9b921824152e5a886..d88367864d514a137ccd2b09eea0fe20075bc87c 100755 (executable)
@@ -9191,12 +9191,11 @@ my @accept_blocks = (
                unopt => '
                        (000) ldb      [0]
                        (001) and      #0xf0
-                       (002) jeq      #0x40            jt 3    jf 7
-                       (003) ld       [16]
-                       (004) and      #0x0
-                       (005) jeq      #0x0             jt 6    jf 7
-                       (006) ret      #2000
-                       (007) ret      #0
+                       (002) jeq      #0x40            jt 3    jf 6
+                       (003) ld       #0x0
+                       (004) jeq      #0x0             jt 5    jf 6
+                       (005) ret      #2000
+                       (006) ret      #0
                        ',
        }, # ip_dst_net_addr_0
        {
@@ -11635,21 +11634,17 @@ my @accept_blocks = (
                unopt => '
                        (000) ldb      [0]
                        (001) and      #0xf0
-                       (002) jeq      #0x60            jt 3    jf 16
-                       (003) ld       [24]
-                       (004) and      #0x0
-                       (005) jeq      #0x0             jt 6    jf 16
-                       (006) ld       [28]
-                       (007) and      #0x0
-                       (008) jeq      #0x0             jt 9    jf 16
-                       (009) ld       [32]
-                       (010) and      #0x0
-                       (011) jeq      #0x0             jt 12   jf 16
-                       (012) ld       [36]
-                       (013) and      #0x0
-                       (014) jeq      #0x0             jt 15   jf 16
-                       (015) ret      #262144
-                       (016) ret      #0
+                       (002) jeq      #0x60            jt 3    jf 12
+                       (003) ld       #0x0
+                       (004) jeq      #0x0             jt 5    jf 12
+                       (005) ld       #0x0
+                       (006) jeq      #0x0             jt 7    jf 12
+                       (007) ld       #0x0
+                       (008) jeq      #0x0             jt 9    jf 12
+                       (009) ld       #0x0
+                       (010) jeq      #0x0             jt 11   jf 12
+                       (011) ret      #262144
+                       (012) ret      #0
                        ',
        }, # ip6_dst_net_0
        {
@@ -11670,21 +11665,18 @@ my @accept_blocks = (
                unopt => '
                        (000) ldb      [0]
                        (001) and      #0xf0
-                       (002) jeq      #0x60            jt 3    jf 16
+                       (002) jeq      #0x60            jt 3    jf 13
                        (003) ld       [24]
                        (004) and      #0xff000000
-                       (005) jeq      #0xff000000      jt 6    jf 16
-                       (006) ld       [28]
-                       (007) and      #0x0
-                       (008) jeq      #0x0             jt 9    jf 16
-                       (009) ld       [32]
-                       (010) and      #0x0
-                       (011) jeq      #0x0             jt 12   jf 16
-                       (012) ld       [36]
-                       (013) and      #0x0
-                       (014) jeq      #0x0             jt 15   jf 16
-                       (015) ret      #262144
-                       (016) ret      #0
+                       (005) jeq      #0xff000000      jt 6    jf 13
+                       (006) ld       #0x0
+                       (007) jeq      #0x0             jt 8    jf 13
+                       (008) ld       #0x0
+                       (009) jeq      #0x0             jt 10   jf 13
+                       (010) ld       #0x0
+                       (011) jeq      #0x0             jt 12   jf 13
+                       (012) ret      #262144
+                       (013) ret      #0
                        ',
        }, # ip6_dst_net_8
        {
@@ -11707,20 +11699,18 @@ my @accept_blocks = (
                unopt => '
                        (000) ldb      [0]
                        (001) and      #0xf0
-                       (002) jeq      #0x60            jt 3    jf 15
+                       (002) jeq      #0x60            jt 3    jf 13
                        (003) ld       [24]
-                       (004) jeq      #0xff112233      jt 5    jf 15
+                       (004) jeq      #0xff112233      jt 5    jf 13
                        (005) ld       [28]
                        (006) and      #0xff000000
-                       (007) jeq      #0x44000000      jt 8    jf 15
-                       (008) ld       [32]
-                       (009) and      #0x0
-                       (010) jeq      #0x0             jt 11   jf 15
-                       (011) ld       [36]
-                       (012) and      #0x0
-                       (013) jeq      #0x0             jt 14   jf 15
-                       (014) ret      #262144
-                       (015) ret      #0
+                       (007) jeq      #0x44000000      jt 8    jf 13
+                       (008) ld       #0x0
+                       (009) jeq      #0x0             jt 10   jf 13
+                       (010) ld       #0x0
+                       (011) jeq      #0x0             jt 12   jf 13
+                       (012) ret      #262144
+                       (013) ret      #0
                        ',
        }, # ip6_dst_net_40
        {
@@ -11745,19 +11735,18 @@ my @accept_blocks = (
                unopt => '
                        (000) ldb      [0]
                        (001) and      #0xf0
-                       (002) jeq      #0x60            jt 3    jf 14
+                       (002) jeq      #0x60            jt 3    jf 13
                        (003) ld       [24]
-                       (004) jeq      #0xff112233      jt 5    jf 14
+                       (004) jeq      #0xff112233      jt 5    jf 13
                        (005) ld       [28]
-                       (006) jeq      #0x44556677      jt 7    jf 14
+                       (006) jeq      #0x44556677      jt 7    jf 13
                        (007) ld       [32]
                        (008) and      #0xffff0000
-                       (009) jeq      #0x88990000      jt 10   jf 14
-                       (010) ld       [36]
-                       (011) and      #0x0
-                       (012) jeq      #0x0             jt 13   jf 14
-                       (013) ret      #262144
-                       (014) ret      #0
+                       (009) jeq      #0x88990000      jt 10   jf 13
+                       (010) ld       #0x0
+                       (011) jeq      #0x0             jt 12   jf 13
+                       (012) ret      #262144
+                       (013) ret      #0
                        ',
        }, # ip6_dst_net_80
        {