From a33f2efb376ec5a95da7445ca053951015f65f9f Mon Sep 17 00:00:00 2001
From: Martin Teichmann <martin.teichmann@xfel.eu>
Date: Mon, 28 Dec 2020 23:02:20 +0000
Subject: [PATCH] add test for proper deallocation in reverse binary

---
 ebpf.py      |  2 ++
 ebpf_test.py | 14 +++++++++-----
 2 files changed, 11 insertions(+), 5 deletions(-)

diff --git a/ebpf.py b/ebpf.py
index 2c39e95..8e36a9b 100644
--- a/ebpf.py
+++ b/ebpf.py
@@ -352,6 +352,8 @@ class ReverseBinary(Expression):
         src, long, _, rfree = self.right.calculate(None, long, None)
         self.ebpf.append(operator + Opcode.LONG * long + Opcode.REG,
                          dst, src, 0, 0)
+        if rfree:
+            self.ebpf.owners.discard(src)
         return dst, long, signed, free
 
     def contains(self, no):
diff --git a/ebpf_test.py b/ebpf_test.py
index 5653185..1ab6791 100644
--- a/ebpf_test.py
+++ b/ebpf_test.py
@@ -345,25 +345,29 @@ class Tests(TestCase):
     def test_reverse_binary(self):
         e = EBPF()
         e.owners = {0, 1, 2, 3}
-        e.r3 = 7 / e.r2
+        e.r3 = 7 / (e.r2 + 2)
         e.r3 = 7 << e.r2
-        e.r3 = 7 % e.r2
+        e.r3 = 7 % (e.r2 + 3)
         e.r3 = 7 >> e.r2
         e.r3 = -7 >> e.r2
         self.assertEqual(e.opcodes, [
             Instruction(opcode=O.MOV+O.LONG, dst=3, src=0, off=0, imm=7),
-            Instruction(opcode=O.REG+O.LONG+O.DIV, dst=3, src=2, off=0, imm=0),
+            Instruction(opcode=O.REG+O.LONG+O.MOV, dst=4, src=2, off=0, imm=0),
+            Instruction(opcode=O.ADD+O.LONG, dst=4, src=0, off=0, imm=2),
+            Instruction(opcode=O.REG+O.LONG+O.DIV, dst=3, src=4, off=0, imm=0),
             Instruction(opcode=O.MOV+O.LONG, dst=3, src=0, off=0, imm=7),
             Instruction(opcode=O.LSH+O.REG+O.LONG, dst=3, src=2, off=0, imm=0),
             Instruction(opcode=O.MOV+O.LONG, dst=3, src=0, off=0, imm=7),
-            Instruction(opcode=O.REG+O.MOD+O.LONG, dst=3, src=2, off=0, imm=0),
+            Instruction(opcode=O.MOV+O.LONG+O.REG, dst=4, src=2, off=0, imm=0),
+            Instruction(opcode=O.ADD+O.LONG, dst=4, src=0, off=0, imm=3),
+            Instruction(opcode=O.REG+O.MOD+O.LONG, dst=3, src=4, off=0, imm=0),
             Instruction(opcode=O.MOV+O.LONG, dst=3, src=0, off=0, imm=7),
             Instruction(opcode=O.REG+O.RSH+O.LONG, dst=3, src=2, off=0, imm=0),
             Instruction(opcode=O.MOV+O.LONG, dst=3, src=0, off=0, imm=-7),
             Instruction(opcode=O.REG+O.LONG+O.ARSH, dst=3, src=2, off=0, imm=0)
             ])
 
-    def test_reverse_binary(self):
+    def test_negation(self):
         e = EBPF()
         e.r7 = -e.r1
         self.assertEqual(e.opcodes, [
-- 
GitLab