コード例 #1
0
    def test_keep_function(self):
        ir, m = create_test_module(
            file_format=gtirb.Module.FileFormat.ELF,
            isa=gtirb.Module.ISA.X64,
            binary_type=["DYN"],
        )
        _, _ = add_section(m, ".dynamic")
        _, bi = add_text_section(m)
        add_function(m, "_start", add_code_block(bi, b"\xC3"))

        asm = run_asm_pprinter(ir)
        self.assertNotContains(asm_lines(asm), ["_start:", "ret"])

        asm = run_asm_pprinter(ir, ["--keep-function", "_start"])
        self.assertContains(asm_lines(asm), ["_start:", "ret"])
コード例 #2
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]"),
        )
コード例 #3
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",
            ),
        )
コード例 #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",
            ],
        )
コード例 #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",
            ],
        )
コード例 #6
0
    def test_block_alignment_via_address_fallback(self):
        ir, m = create_test_module(file_format=gtirb.Module.FileFormat.ELF,
                                   isa=gtirb.Module.ISA.X64)
        _, bi = add_text_section(m, address=0x1004)
        add_code_block(bi, b"\xC3")

        asm = run_asm_pprinter(ir)
        self.assertContains(asm_lines(asm), [".align 4", "ret"])
コード例 #7
0
    def test_windows_includelib(self):
        ir, m = create_test_module(
            file_format=gtirb.Module.FileFormat.PE,
            isa=gtirb.Module.ISA.X64,
            binary_type=["EXEC", "EXE", "WINDOWS_CUI"],
        )

        _, bi = add_text_section(m)
        m.aux_data["libraries"].data.append(("WINSPOOL.DRV"))
        m.aux_data["libraries"].data.append(("USER32.DLL"))

        asm = run_asm_pprinter(ir)

        self.assertContains(asm_lines(asm), ["INCLUDELIB WINSPOOL.lib"])
        self.assertContains(asm_lines(asm), ["INCLUDELIB USER32.lib"])

        self.assertNotContains(asm_lines(asm), ["INCLUDELIB WINSPOOL.DRV"])
        self.assertNotContains(asm_lines(asm), ["INCLUDELIB USER32.DLL"])
コード例 #8
0
    def test_block_alignment_via_array_section_fallback_x64(self):
        # This tests the changes in MR 362.
        ir, m = create_test_module(file_format=gtirb.Module.FileFormat.ELF,
                                   isa=gtirb.Module.ISA.X64)
        _, bi = add_section(m, ".init_array")
        add_data_block(bi, b"\x00\x00\x00\x00\x00\x00\x00\x00")

        asm = run_asm_pprinter(ir, ["--policy=dynamic"])
        self.assertContains(asm_lines(asm), [".align 8", ".zero 8"])
コード例 #9
0
    def test_block_alignment_via_section_in_aux_data(self):
        ir, m = create_test_module(file_format=gtirb.Module.FileFormat.ELF,
                                   isa=gtirb.Module.ISA.X64)
        s, bi = add_text_section(m)
        add_code_block(bi, b"\xC3")

        m.aux_data["alignment"].data[s] = 32

        asm = run_asm_pprinter(ir)
        self.assertContains(asm_lines(asm), [".align 32", "ret"])
コード例 #10
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",),
        )