From 72bb422440983f0ca85ac03aa9d280cfff3a32ad Mon Sep 17 00:00:00 2001 From: Martin Teichmann <martin.teichmann@gmail.com> Date: Wed, 15 Feb 2023 08:40:02 +0000 Subject: [PATCH] support huge immediate values we were already able to use huge values in assignments, now we can also use them in operations. --- ebpfcat/ebpf.py | 10 ++++++++-- ebpfcat/ebpf_test.py | 13 ++++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/ebpfcat/ebpf.py b/ebpfcat/ebpf.py index d0a5d05..39abe09 100644 --- a/ebpfcat/ebpf.py +++ b/ebpfcat/ebpf.py @@ -579,9 +579,15 @@ class Binary(Expression): + Opcode.LONG * ((r_long or l_long) if long is None else long), dst, src, 0, 0) - else: + elif -0x80000000 <= self.right < 0x100000000: self.ebpf.append(self.operator + Opcode.LONG * long, dst, 0, 0, self.right) + else: + with self.ebpf.get_free_register(None) as src: + self.ebpf._load_value(src, self.right) + self.ebpf.append( + self.operator + Opcode.REG + Opcode.LONG, + dst, src, 0, 0) if orig_dst is None or orig_dst == dst: yield dst, long return @@ -1203,7 +1209,7 @@ class EBPF: raise AssembleError("not enough registers") def _load_value(self, no, value): - if -0x80000000 <= value < 0x80000000: + if -0x80000000 <= value < 0x100000000: self.append(Opcode.MOV + Opcode.LONG, no, 0, 0, value) else: self.append(Opcode.DW, no, 0, 0, value & 0xffffffff) diff --git a/ebpfcat/ebpf_test.py b/ebpfcat/ebpf_test.py index b837afa..7b19d5f 100644 --- a/ebpfcat/ebpf_test.py +++ b/ebpfcat/ebpf_test.py @@ -447,7 +447,6 @@ class Tests(TestCase): with Else: e.r3 = 7 e.r4 = 3 - self.maxDiff = None self.assertEqual(e.opcodes, [ Instruction(opcode=O.JGT, dst=2, src=0, off=1, imm=3), Instruction(opcode=O.JLE, dst=3, src=0, off=1, imm=2), @@ -488,11 +487,20 @@ class Tests(TestCase): e = EBPF() e.r3 = 0x1234567890 e.r4 = e.get_fd(7) + e.r3 = e.r4 + 0x1234567890 + e.r3 = 0x90000000 + self.assertEqual(e.opcodes, [ Instruction(opcode=24, dst=3, src=0, off=0, imm=878082192), Instruction(opcode=0, dst=0, src=0, off=0, imm=18), Instruction(opcode=24, dst=4, src=1, off=0, imm=7), - Instruction(opcode=0, dst=0, src=0, off=0, imm=0)]) + Instruction(opcode=0, dst=0, src=0, off=0, imm=0), + Instruction(opcode=O.REG+O.LONG+O.MOV, dst=3, src=4, off=0, imm=0), + Instruction(opcode=O.DW, dst=0, src=0, off=0, imm=878082192), + Instruction(opcode=O.W, dst=0, src=0, off=0, imm=18), + Instruction(opcode=O.LONG+O.REG+O.ADD, dst=3, src=0, off=0, imm=0), + Instruction(opcode=O.LONG+O.MOV, dst=3, src=0, off=0, imm=2415919104), + ]) def test_simple_binary(self): e = EBPF() @@ -649,7 +657,6 @@ class Tests(TestCase): e.r8 = e.r1 def test_binary_alloc(self): - self.maxDiff = None e = EBPF() e.r3 = e.r1 - (2 * e.r10) e.mH[e.r10 - 10] = 2 * e.r3 -- GitLab