Skip to content

Another optimization bug #38

Description

@guyharris

Converted from SourceForge issue 860815, submitted by guy_harris

While looking at bug 766013, I tested with theexpression

ether proto 0x86dd and
(ether[38:4] == 0x12345678 and ether[42:4] == 0x9abcdef0 and
 ether[46:4] == 0xfeedbabe and ether[50:4] == 0xdeadbeef) or
(ether[38:4] == 0x11112222 and ether[42:4] == 0x33334444 and
 ether[46:4] == 0x55556666 and ether[50:4] == 0x77778888)

That also generates bad code:

(000) ldh      [12]
(001) jeq      #0x86dd          jt 2    jf 10
(002) ld       [38]
(003) jeq      #0x12345678      jt 4    jf 10
(004) ld       [42]
(005) jeq      #0x9abcdef0      jt 6    jf 19
(006) ld       [46]
(007) jeq      #0xfeedbabe      jt 8    jf 19
(008) ld       [50]
(009) jeq      #0xdeadbeef      jt 18   jf 19
(010) ld       [38]
(011) jeq      #0x11112222      jt 12   jf 19
(012) ld       [42]
(013) jeq      #0x33334444      jt 14   jf 19
(014) ld       [46]
(015) jeq      #0x55556666      jt 16   jf 19
(016) ld       [50]
(017) jeq      #0x77778888      jt 18   jf 19
(018) ret      #68
(019) ret      #0

but only in the current CVS version - 0.7.x doesn't have that bug. It was introduced in revision 1.73 of
optimize.c - the change of

if (add == 0 || add->s.code != (BPF_ALU|BPF_ADD|BPF_X))
    break;

to

if (add == 0 || add->s.code != (BPF_ALU|BPF_ADD|BPF_X))
   continue;

introduced it.

Unfortunately, removing that change causes "1 & len == 1" to generate

(000) ld       #pktlen
(001) tax
(002) ld       #0x1
(003) and      x
(004) sub      #1
(005) jeq      #0x0             jt 6    jf 7
(006) ret      #68
(007) ret      #0

which, although correct, is a bit bogus, rather than generating

(000) ld       #pktlen
(001) tax      
(002) ld       #0x1
(003) and      x
(004) jeq      #0x1             jt 5    jf 6
(005) ret      #68
(006) ret      #0

and causes "0 - len == 1" to generate

(000) ld       #pktlen
(001) tax      
(002) ld       #0xffffffff
(003) jeq      x                jt 4    jf 5
(004) ret      #68
(005) ret      #0

rather than

(000) ld       #pktlen
(001) tax      
(002) ld       #0x0
(003) sub      x
(004) jeq      #0x1             jt 5    jf 6
(005) ret      #68
(006) ret      #0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions