Skip to content
Snippets Groups Projects
Commit e52eb311 authored by Martin Teichmann's avatar Martin Teichmann
Browse files

support huge immediate values

we were already able to use huge values in assignments, now
we can also use them in operations.
parent e53b45cd
No related branches found
No related tags found
No related merge requests found
......@@ -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)
......
......@@ -487,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()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment