Ejemplo n.º 1
0
    def test_ambiguous_symbol_names(self):
        ir, m = create_test_module(gtirb.Module.FileFormat.ELF,
                                   gtirb.Module.ISA.X64)
        s, bi = add_text_section(m, 0x1000)
        cb = add_code_block(bi, b"\xc3")
        cb2 = add_code_block(bi, b"\xc3")
        s2, bi2 = add_data_section(m, 0x500)
        db = add_data_block(bi2, b"hello")
        add_symbol(m, "f1_disambig_0x1000_0", db)
        add_function(m, "f1", cb)
        add_function(m, "f2", cb)
        add_function(m, "f2", cb)
        add_function(m, "f1", cb2)
        add_symbol(m, "f1", cb)
        asm = pprinter_helpers.run_asm_pprinter(ir)
        print(asm)
        # f1_0x1000 should start counting from 1,
        # since 0 produces a conflict,
        # but f2_0x1000 should start counting from 0
        self.assertIn("f1_disambig_0x1000_1:", asm)
        self.assertIn("f1_disambig_0x1000_2:", asm)
        self.assertIn("f1_disambig_0x1001_0:", asm)

        self.assertIn("f2_disambig_0x1000_0", asm)
        self.assertIn("f2_disambig_0x1000_1", asm)
Ejemplo n.º 2
0
    def test_moffset_mov_ia32_compat(self):
        ir, m = create_test_module(
            gtirb.Module.FileFormat.PE, gtirb.Module.ISA.IA32
        )
        s, bi = add_text_section(m, 0x1000)
        hello_expr = gtirb.SymAddrConst(0, add_symbol(m, "hello"))

        # mov al, byte ptr [hello]
        add_code_block(bi, b"\xA0\x00\x00\x00\x00", {0: hello_expr})
        # mov ax, word ptr [hello]
        add_code_block(bi, b"\x66\xA1\x00\x00\x00\x00", {0: hello_expr})
        # mov eax, dword ptr [hello]
        add_code_block(bi, b"\xA1\x00\x00\x00\x00", {0: hello_expr})
        # mov byte ptr [hello], al
        add_code_block(bi, b"\xA2\x00\x00\x00\x00", {0: hello_expr})
        # mov word ptr [hello], ax
        add_code_block(bi, b"\x66\xA3\x00\x00\x00\x00", {0: hello_expr})
        # mov dword ptr [hello], eax
        add_code_block(bi, b"\xA3\x00\x00\x00\x00", {0: hello_expr})

        asm, output = run_asm_pprinter_with_outputput(ir)
        self.assertIn(self.COMPAT_WARNING_MESSAGE, output)
        self.assertEqual(output.count(self.COMPAT_WARNING_MESSAGE), 1)
        self.assertContains(
            asm_lines(asm),
            (
                "mov AL,BYTE PTR [hello]",
                "mov AX,WORD PTR [hello]",
                "mov EAX,DWORD PTR [hello]",
                "mov BYTE PTR [hello],AL",
                "mov WORD PTR [hello],AX",
                "mov DWORD PTR [hello],EAX",
            ),
        )
Ejemplo n.º 3
0
    def test_nonmoffset_mov(self):
        ir, m = create_test_module(
            gtirb.Module.FileFormat.PE, gtirb.Module.ISA.IA32
        )
        s, bi = add_text_section(m, 0x1000)
        hello_expr = gtirb.SymAddrConst(0, add_symbol(m, "hello"))

        add_code_block(
            bi,
            # mov edi, hello
            b"\x8B\x3D\x00\x00\x00\x00",
            # wrong symbolic offset
            {0: hello_expr},
        )
        add_code_block(
            bi,
            # mov edi, hello
            b"\x8B\x3D\x00\x00\x00\x00",
            # correct symbolic offset
            {2: hello_expr},
        )

        asm, output = run_asm_pprinter_with_outputput(ir)
        self.assertNotIn(self.COMPAT_WARNING_MESSAGE, output)
        self.assertContains(
            asm_lines(asm),
            ("mov EDI,DWORD PTR [0]", "mov EDI,DWORD PTR [hello]"),
        )
Ejemplo n.º 4
0
    def test_code_block_alignment_via_symbol(self):
        """
        Test that code blocks that have exported symbols are aligned by their
        address.
        """
        ir, m = create_test_module(file_format=gtirb.Module.FileFormat.ELF,
                                   isa=gtirb.Module.ISA.X64)
        _, bi = add_text_section(m)

        add_code_block(bi, b"\x90\x90")
        block = add_code_block(bi, b"\xC3")

        sym = add_symbol(m, "hello", block)
        add_elf_symbol_info(m, sym, block.size, "FUNC")

        asm = run_asm_pprinter(ir)
        self.assertContains(
            asm_lines(asm),
            [
                "nop",
                ".align 2",
                ".globl hello",
                ".type hello, @function",
                "hello:",
                "ret",
            ],
        )
Ejemplo n.º 5
0
    def test_data_block_alignment_via_symbol(self):
        """
        Test that data blocks that have exported symbols are *not* aligned at
        all.
        """
        ir, m = create_test_module(file_format=gtirb.Module.FileFormat.ELF,
                                   isa=gtirb.Module.ISA.X64)
        _, bi = add_data_section(m)

        add_data_block(bi, b"\x01\x02")
        block = add_data_block(bi, b"\x03\x04")

        sym = add_symbol(m, "hello", block)
        add_elf_symbol_info(m, sym, block.size, "OBJECT")

        asm = run_asm_pprinter(ir)
        self.assertContains(
            asm_lines(asm),
            [
                ".byte 0x2",
                ".globl hello",
                ".type hello, @object",
                "hello:",
                ".byte 0x3",
            ],
        )
Ejemplo n.º 6
0
    def test_moffset_mov_x64_correct(self):
        ir, m = create_test_module(
            gtirb.Module.FileFormat.PE, gtirb.Module.ISA.X64
        )
        s, bi = add_text_section(m, 0x1000)
        hello_expr = gtirb.SymAddrConst(0, add_symbol(m, "hello"))

        # mov rax, qword ptr [hello]
        add_code_block(
            bi, b"\x48\xA1\x00\x00\x00\x00\x00\x00\x00\x00", {2: hello_expr}
        )
        # mov qword ptr [hello], rax
        add_code_block(
            bi, b"\x48\xA3\x00\x00\x00\x00\x00\x00\x00\x00", {2: hello_expr}
        )

        asm, output = run_asm_pprinter_with_outputput(ir)
        self.assertNotIn(self.COMPAT_WARNING_MESSAGE, output)
        self.assertContains(
            asm_lines(asm),
            ("mov RAX,QWORD PTR [hello]", "mov QWORD PTR [hello],RAX",),
        )
Ejemplo n.º 7
0
from gtirb_helpers import (
    create_test_module,
    add_elf_symbol_info,
    add_text_section,
    add_data_section,
    add_symbol,
    add_code_block,
    add_data_block,
)

ir, m = create_test_module(gtirb.Module.FileFormat.ELF, gtirb.Module.ISA.X64)

# Add .data section.
s, bi = add_data_section(m, 0x4000A8)
block = add_data_block(bi, b"hello world\n")
hello = add_symbol(m, "hello", block)

# Add .text section.
s, bi = add_text_section(m, 0x400080)

# mov eax, 1
block = add_code_block(bi, b"\xB8\x01\x00\x00\x00")
start = add_symbol(m, "_start", block)
add_elf_symbol_info(m, start, block.size, "FUNC")

# mov ebx, 1
add_code_block(bi, b"\xBB\x01\x00\x00\x00")
# mov rsi, hello
operand = gtirb.SymAddrConst(0, hello)
add_code_block(bi, b"\x48\xBE\xA8\x00\x40\x00\x00\x00\x00\x00", {2: operand})
# mov rsi, 13