|
| 1 | +import os |
| 2 | +import unittest |
| 3 | +from logging import getLogger, FATAL |
| 4 | +from ptrlib.cpu import CPU |
| 5 | + |
| 6 | +_is_windows = os.name == 'nt' |
| 7 | + |
| 8 | +class TestIntelAssemblerNormalize(unittest.TestCase): |
| 9 | + """Normalization-focused tests for Intel assembler backends. |
| 10 | +
|
| 11 | + These tests ensure that _normalize_assembly() handles: |
| 12 | + - Size specifiers around memory operands (inserting 'ptr' for GAS) |
| 13 | + - Comma and bracket spacing normalization |
| 14 | + - Semicolon-based instruction splitting |
| 15 | + - // line comments and /* block comments */ removal |
| 16 | + - Case-insensitive size specifiers |
| 17 | + """ |
| 18 | + def setUp(self): |
| 19 | + getLogger("ptrlib").setLevel(FATAL) |
| 20 | + |
| 21 | + def test_gcc_size_ptr_insertion_qword(self): |
| 22 | + """qword [mem] vs qword ptr [mem] must assemble identically with GCC.""" |
| 23 | + if _is_windows: |
| 24 | + return |
| 25 | + cpu = CPU('intel', 64) |
| 26 | + cpu.assembler = 'gcc' |
| 27 | + a = 'mov qword [rax], 0x1234' |
| 28 | + b = 'mov qword ptr [rax], 0x1234' |
| 29 | + self.assertEqual(cpu.assemble(a), cpu.assemble(b)) |
| 30 | + |
| 31 | + def test_gcc_size_ptr_insertion_other_sizes(self): |
| 32 | + """byte/word/dword [mem] vs ... ptr [mem] must match with GCC.""" |
| 33 | + if _is_windows: |
| 34 | + return |
| 35 | + cpu = CPU('intel', 64) |
| 36 | + cpu.assembler = 'gcc' |
| 37 | + pairs = [ |
| 38 | + ('mov dword [rax], 0x11223344', 'mov dword ptr [rax], 0x11223344'), |
| 39 | + ('mov word [rax], 0x3344', 'mov word ptr [rax], 0x3344'), |
| 40 | + ('mov byte [rax], 0x22', 'mov byte ptr [rax], 0x22'), |
| 41 | + ] |
| 42 | + for a, b in pairs: |
| 43 | + self.assertEqual(cpu.assemble(a), cpu.assemble(b)) |
| 44 | + |
| 45 | + def test_no_spurious_backslash_produced(self): |
| 46 | + """Ensure 'mov qword [rax], 1' assembles (no '\\[' introduced).""" |
| 47 | + if _is_windows: |
| 48 | + return |
| 49 | + cpu = CPU('intel', 64) |
| 50 | + cpu.assembler = 'gcc' |
| 51 | + # Should not raise OSError |
| 52 | + cpu.assemble('mov qword [rax], 1') |
| 53 | + |
| 54 | + def test_comma_and_bracket_spacing_mov(self): |
| 55 | + """mov rax,[rbx] variants should assemble identically.""" |
| 56 | + if _is_windows: |
| 57 | + return |
| 58 | + for assembler in ('gcc', 'nasm'): |
| 59 | + cpu = CPU('intel', 64) |
| 60 | + cpu.assembler = assembler |
| 61 | + a = 'mov rax,[rbx]' |
| 62 | + b = 'mov rax, [ rbx ]' |
| 63 | + self.assertEqual(cpu.assemble(a), cpu.assemble(b)) |
| 64 | + |
| 65 | + def test_bracket_tightening_with_lea(self): |
| 66 | + """[ rbx + 8 ] -> [rbx + 8] equivalence (no 'ptr' involved).""" |
| 67 | + if _is_windows: |
| 68 | + return |
| 69 | + for assembler in ('gcc', 'nasm'): |
| 70 | + cpu = CPU('intel', 64) |
| 71 | + cpu.assembler = assembler |
| 72 | + a = 'lea rax,[ rbx + 8 ]' |
| 73 | + b = 'lea rax, [rbx+8]' |
| 74 | + self.assertEqual(cpu.assemble(a), cpu.assemble(b)) |
| 75 | + |
| 76 | + def test_line_and_block_comment_stripping(self): |
| 77 | + """'// ...' and '/* ... */' must be ignored by the tokenizer.""" |
| 78 | + if _is_windows: |
| 79 | + return |
| 80 | + for assembler in ('gcc', 'nasm'): |
| 81 | + cpu = CPU('intel', 64) |
| 82 | + cpu.assembler = assembler |
| 83 | + code = 'nop // line\n/* block\n comment */ nop ; nop' |
| 84 | + baseline = 'nop\nnop\nnop' |
| 85 | + self.assertEqual(cpu.assemble(code), cpu.assemble(baseline)) |
| 86 | + |
| 87 | + def test_semicolon_instruction_split(self): |
| 88 | + """'nop; nop ;nop' equals three nops after normalization.""" |
| 89 | + if _is_windows: |
| 90 | + return |
| 91 | + for assembler in ('gcc', 'nasm'): |
| 92 | + cpu = CPU('intel', 64) |
| 93 | + cpu.assembler = assembler |
| 94 | + code = 'nop; nop ; nop' |
| 95 | + baseline = 'nop\nnop\nnop' |
| 96 | + self.assertEqual(cpu.assemble(code), cpu.assemble(baseline)) |
| 97 | + |
| 98 | + def test_whitespace_collapse(self): |
| 99 | + """Multiple spaces/tabs collapse and consistent comma spacing.""" |
| 100 | + if _is_windows: |
| 101 | + return |
| 102 | + for assembler in ('gcc', 'nasm'): |
| 103 | + cpu = CPU('intel', 64) |
| 104 | + cpu.assembler = assembler |
| 105 | + code = ' mov rax , [ rbx + 0x10 ] ' |
| 106 | + baseline = 'mov rax, [rbx+0x10]' |
| 107 | + self.assertEqual(cpu.assemble(code), cpu.assemble(baseline)) |
| 108 | + |
| 109 | + def test_size_keyword_case_insensitive(self): |
| 110 | + """Ensure case-insensitive match for size keywords for GCC path.""" |
| 111 | + if _is_windows: |
| 112 | + return |
| 113 | + cpu = CPU('intel', 64) |
| 114 | + cpu.assembler = 'gcc' |
| 115 | + a = 'mov QWORD [rax], 1' |
| 116 | + b = 'mov qword ptr [rax], 1' |
| 117 | + self.assertEqual(cpu.assemble(a), cpu.assemble(b)) |
| 118 | + |
| 119 | + |
| 120 | + def test_nasm_removes_ptr_keyword(self): |
| 121 | + """In NASM mode, 'ptr' should be removed; both forms must assemble identically.""" |
| 122 | + if _is_windows: |
| 123 | + return |
| 124 | + cpu = CPU('intel', 64) |
| 125 | + cpu.assembler = 'nasm' |
| 126 | + pairs = [ |
| 127 | + ('mov qword [rax], 0x1122334455667788', 'mov qword ptr [rax], 0x1122334455667788'), |
| 128 | + ('mov dword [rax], 0x11223344', 'mov dword ptr [rax], 0x11223344'), |
| 129 | + ('mov word [rax], 0x3344', 'mov word ptr [rax], 0x3344'), |
| 130 | + ('mov byte [rax], 0x22', 'mov byte ptr [rax], 0x22'), |
| 131 | + ] |
| 132 | + for a, b in pairs: |
| 133 | + self.assertEqual(cpu.assemble(a), cpu.assemble(b)) |
| 134 | + |
| 135 | + def test_alias_nasm_function_handles_ptr(self): |
| 136 | + """The nasm() helper should also accept 'ptr' and normalize it away.""" |
| 137 | + if _is_windows: |
| 138 | + return |
| 139 | + from ptrlib.cpu.assembler import nasm as nasm_asm |
| 140 | + a = nasm_asm('mov qword [rax], 1', bits=64) |
| 141 | + b = nasm_asm('mov qword ptr [rax], 1', bits=64) |
| 142 | + self.assertEqual(a, b) |
0 commit comments