diff --git a/lib/seccomp-tools/asm/compiler.rb b/lib/seccomp-tools/asm/compiler.rb index f187aaf..5235cf4 100644 --- a/lib/seccomp-tools/asm/compiler.rb +++ b/lib/seccomp-tools/asm/compiler.rb @@ -178,7 +178,7 @@ def emit_ret(val) def emit_cmp(cmp, jt, jf) jop, jt, jf = convert_jmp_op(cmp, jt, jf) - return emit(:jmp, jop, 0, jt: 0, jf: 0, k: jt) if jop == :ja || jt == jf + return emit(:jmp, :none, 0, jt: 0, jf: 0, k: jt) if jop == :ja || jt == jf val = cmp[1] src = val.x? ? :x : :k diff --git a/spec/asm/compiler_spec.rb b/spec/asm/compiler_spec.rb index 9f4d696..aac5c03 100644 --- a/spec/asm/compiler_spec.rb +++ b/spec/asm/compiler_spec.rb @@ -186,6 +186,25 @@ A = arch EOS end + + it 'optimizes jump targets' do + compiler = described_class.new(<<-EOS, nil, :amd64) +A = args[0] +X = A +if (A == X) goto out +else goto out +A = X +out: return ALLOW + EOS + + expect(compiler.compile!.map(&:decompile).join("\n")).to eq <<-EOS.strip +A = args[0] +X = A +goto 0004 +A = X +return ALLOW + EOS + end end describe 'label' do