From 0067b769b8b2603229a66907d2a7e0669c5c5ff8 Mon Sep 17 00:00:00 2001 From: elikaski <37732584+elikaski@users.noreply.github.com> Date: Thu, 3 Dec 2020 21:45:16 +0200 Subject: [PATCH] Bug fix with post scripts Compiling a simple Hello World program generates assembly code that contains: # (external call) movl (sp), %esp # movl $printf, (external) When using post/xor.py (and other post scripts), the corresponding instructions generated are: # (external call) #xor> movl (sp), %esp # xorl %ebx, %ebx xorl (sp), %ebx xorl %esp # , %ebx xorl %ebx, %esp # The third instruction is the problematic: xorl %esp # , %ebx The root cause is that the comment (# ) is also being identified as the "destination" register My change makes it discard the comment --- post/adc.py | 3 +++ post/add.py | 3 +++ post/andor.py | 3 +++ post/cmpxchgxchg.py | 3 +++ post/mov32.py | 3 +++ post/pushpop.py | 3 +++ post/rand.py | 25 +++++++++++++++++++++++++ post/rereg.py | 6 ++++++ post/risc.py | 11 +++++++++++ post/rrrrr.py | 7 +++++++ post/sbb.py | 4 ++++ post/shuffle.py | 6 ++++++ post/sub.py | 4 ++++ post/xadd.py | 8 ++++++++ post/xor.py | 3 +++ 15 files changed, 92 insertions(+) diff --git a/post/adc.py b/post/adc.py index 696f591..e60e6a2 100644 --- a/post/adc.py +++ b/post/adc.py @@ -39,6 +39,9 @@ tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] if l.startswith("movb"): b = 8 diff --git a/post/add.py b/post/add.py index 390ffea..2ce9c10 100644 --- a/post/add.py +++ b/post/add.py @@ -33,6 +33,9 @@ tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] if l.startswith("movb"): b = 8 diff --git a/post/andor.py b/post/andor.py index 5ae40ed..83dd83f 100644 --- a/post/andor.py +++ b/post/andor.py @@ -33,6 +33,9 @@ tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] if l.startswith("movb"): s = "b" diff --git a/post/cmpxchgxchg.py b/post/cmpxchgxchg.py index baa293d..d721055 100644 --- a/post/cmpxchgxchg.py +++ b/post/cmpxchgxchg.py @@ -34,6 +34,9 @@ tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] # NOTE: requires M/o/Vfuscator to only produce dword constants if source.startswith("$"): diff --git a/post/mov32.py b/post/mov32.py index 369bfb0..00bc3b6 100644 --- a/post/mov32.py +++ b/post/mov32.py @@ -36,6 +36,9 @@ tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] # NOTE: requires M/o/Vfuscator to only produce dword constants if source.startswith("$"): diff --git a/post/pushpop.py b/post/pushpop.py index 3429e45..4de4b39 100644 --- a/post/pushpop.py +++ b/post/pushpop.py @@ -34,6 +34,9 @@ tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] # NOTE: requires M/o/Vfuscator to only produce dword constants if source.startswith("$"): diff --git a/post/rand.py b/post/rand.py index fbe2470..9d164a8 100644 --- a/post/rand.py +++ b/post/rand.py @@ -30,6 +30,9 @@ def o_adc(f, l): tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] if l.startswith("movb"): b = 8 @@ -86,6 +89,9 @@ def o_add(f, l): tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] if l.startswith("movb"): b = 8 @@ -132,6 +138,9 @@ def o_andor(f, l): tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] if l.startswith("movb"): s = "b" @@ -156,6 +165,9 @@ def o_rrrrr(f, l): tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] if l.startswith("movb"): b = 8 @@ -195,6 +207,9 @@ def o_sbb(f, l): tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] if l.startswith("movb"): s = "b" @@ -237,6 +252,9 @@ def o_sub(f, l): tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] if l.startswith("movb"): s = "b" @@ -269,6 +287,9 @@ def o_xadd(f, l): tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] if l.startswith("movb"): b = 8 @@ -320,6 +341,10 @@ def o_xadd(f, l): tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] + # NOTE: requires M/o/Vfuscator to only produce dword constants if source.startswith("$"): diff --git a/post/rereg.py b/post/rereg.py index 8fa931f..6d7bc5b 100644 --- a/post/rereg.py +++ b/post/rereg.py @@ -59,6 +59,9 @@ def genreg(l): tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] if "TR0" in source: source = source.replace("TR0", "r%d" % r0) @@ -124,6 +127,9 @@ def rereg(l, asm, i): tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] source_regs = re.findall(r'r\d+_', source) for r in source_regs: diff --git a/post/risc.py b/post/risc.py index 76dd0a5..2e821d6 100644 --- a/post/risc.py +++ b/post/risc.py @@ -80,6 +80,9 @@ def decompose(term): tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] # NOTE: requires M/o/Vfuscator to only produce dword constants if source.startswith("$"): @@ -120,6 +123,10 @@ def decompose(term): tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] + if l.startswith("movb"): s = "b" @@ -192,6 +199,10 @@ def decompose(term): tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] + # warning: ebp used in a previous pass to load immediates. it's # okay since it was loading 32 bit values, and won't be translated diff --git a/post/rrrrr.py b/post/rrrrr.py index db8645a..1790b8c 100644 --- a/post/rrrrr.py +++ b/post/rrrrr.py @@ -32,6 +32,9 @@ tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] # NOTE: requires M/o/Vfuscator to only produce dword constants if source.startswith("$"): @@ -72,6 +75,10 @@ tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] + if l.startswith("movb"): b = 8 diff --git a/post/sbb.py b/post/sbb.py index f93f3be..422ab53 100644 --- a/post/sbb.py +++ b/post/sbb.py @@ -39,6 +39,10 @@ tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] + if l.startswith("movb"): s = "b" diff --git a/post/shuffle.py b/post/shuffle.py index 0f2c4c1..45cdb0e 100644 --- a/post/shuffle.py +++ b/post/shuffle.py @@ -78,12 +78,18 @@ def can_swap(l1, l2): tok1 = l1.find(",") source1 = l1[l1.index(" "):tok1].strip() dest1 = l1[tok1+1:].strip() + end = dest1.find(" ") + if end != -1: + dest1 = dest1[:end] tok2 = l2.find(",", l2.find(")")) if tok2 == -1: tok2 = l2.find(",") source2 = l2[l2.index(" "):tok2].strip() dest2 = l2[tok2+1:].strip() + end = dest2.find(" ") + if end != -1: + dest2 = dest2[:end] # assumes compiler outputs () around all memory references diff --git a/post/sub.py b/post/sub.py index f7edf5a..268c674 100644 --- a/post/sub.py +++ b/post/sub.py @@ -33,6 +33,10 @@ tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] + if l.startswith("movb"): s = "b" diff --git a/post/xadd.py b/post/xadd.py index 6bdeb08..c879428 100644 --- a/post/xadd.py +++ b/post/xadd.py @@ -32,6 +32,10 @@ tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] + # NOTE: requires M/o/Vfuscator to only produce dword constants if source.startswith("$"): @@ -68,6 +72,10 @@ tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] + if l.startswith("movb"): b = 8 diff --git a/post/xor.py b/post/xor.py index 9a2741e..b884bb7 100644 --- a/post/xor.py +++ b/post/xor.py @@ -33,6 +33,9 @@ tok = l.find(",") source = l[l.index(" "):tok].strip() dest = l[tok+1:].strip() + end = dest.find(" ") + if end != -1: + dest = dest[:end] if l.startswith("movb"): s = "b"