Esempio n. 1
0
    def test_blocks_at_simple(self, scope):
        ir, m, s, bi = create_interval_etc(address=0x1000, size=4)
        search_in = scope.select(ir, m, s, bi)

        code_block = gtirb.CodeBlock(offset=0, size=1, byte_interval=bi)
        code_block2 = gtirb.CodeBlock(offset=1, size=1, byte_interval=bi)

        found = set(search_in.byte_blocks_at(0x1000))
        self.assertEqual(found, {code_block})

        # Change the offset to verify we update the index
        code_block.offset = 2
        found = set(search_in.byte_blocks_at(0x1000))
        self.assertEqual(found, set())

        found = set(search_in.byte_blocks_at(0x1002))
        self.assertEqual(found, {code_block})

        # Discard the block to verify we update the index
        bi.blocks.discard(code_block)
        found = set(search_in.byte_blocks_at(0x1002))
        self.assertEqual(found, set())

        # Now add it back to verify we update the index
        bi.blocks.add(code_block)
        found = set(search_in.byte_blocks_at(0x1002))
        self.assertEqual(found, {code_block})
Esempio n. 2
0
 def test_cfg(self):
     node = gtirb.CFG()
     node.add(
         gtirb.Edge(
             gtirb.CodeBlock(offset=1, size=2),
             gtirb.CodeBlock(offset=3, size=4),
             gtirb.Edge.Label(
                 type=gtirb.Edge.Type.Fallthrough,
                 conditional=True,
                 direct=False,
             ),
         )
     )
     node.add(
         gtirb.Edge(
             gtirb.CodeBlock(offset=5, size=6),
             gtirb.CodeBlock(offset=7, size=8),
             gtirb.Edge.Label(
                 type=gtirb.Edge.Type.Branch,
                 conditional=True,
                 direct=False,
             ),
         )
     )
     string = repr(node)
     new_node = eval(string)
     self.assertTrue(node.deep_eq(new_node))
Esempio n. 3
0
    def test_blocks_at_no_address(self, scope):
        "Test that byte_blocks_at does nothing if we don't have an address"
        ir, m, s, bi = create_interval_etc(address=None, size=4)
        search_in = scope.select(ir, m, s, bi)

        code_block = gtirb.CodeBlock(offset=0, size=1, byte_interval=bi)
        code_block2 = gtirb.CodeBlock(offset=1, size=1, byte_interval=bi)

        found = set(search_in.byte_blocks_at(0x1000))
        self.assertEqual(found, set())
Esempio n. 4
0
 def test_edge(self):
     node = gtirb.Edge(
         source=gtirb.CodeBlock(offset=1, size=2),
         target=gtirb.CodeBlock(offset=3, size=4),
         label=gtirb.Edge.Label(
             gtirb.Edge.Type.Fallthrough, conditional=True, direct=False
         ),
     )
     string = repr(node)
     new_node = eval(string)
     self.assertEqual(node, new_node)
Esempio n. 5
0
    def test_blocks_on_with_overlapping_blocks(self, scope):
        "Test that byte_blocks_on returns all blocks that overlap an address"
        ir, m, s, bi = create_interval_etc(address=0x1000, size=4)
        search_in = scope.select(ir, m, s, bi)

        code_block = gtirb.CodeBlock(offset=0, size=1, byte_interval=bi)
        code_block2 = gtirb.CodeBlock(offset=0, size=2, byte_interval=bi)
        code_block3 = gtirb.CodeBlock(offset=1, size=1, byte_interval=bi)

        found = set(search_in.byte_blocks_on(0x1001))
        self.assertEqual(found, {code_block2, code_block3})
Esempio n. 6
0
    def test_blocks_at_overlapping(self, scope):
        "Test that byte_blocks_at only looks at starting addresses"
        ir, m, s, bi = create_interval_etc(address=0x1000, size=4)
        search_in = scope.select(ir, m, s, bi)

        code_block = gtirb.CodeBlock(offset=0, size=1, byte_interval=bi)
        code_block2 = gtirb.CodeBlock(offset=0, size=2, byte_interval=bi)
        code_block3 = gtirb.CodeBlock(offset=1, size=1, byte_interval=bi)

        found = set(search_in.byte_blocks_at(0x1001))
        self.assertEqual(found, {code_block3})
Esempio n. 7
0
    def test_blocks_at_zero_sized(self, scope):
        "Test that byte_blocks_at can find zero-sized blocks"
        ir, m, s, bi = create_interval_etc(address=0x1000, size=4)
        search_in = scope.select(ir, m, s, bi)

        code_block = gtirb.CodeBlock(offset=0, size=1, byte_interval=bi)
        code_block2 = gtirb.CodeBlock(offset=1, size=0, byte_interval=bi)
        code_block3 = gtirb.CodeBlock(offset=1, size=1, byte_interval=bi)
        search_in = scope.select(ir, m, s, bi)

        found = set(search_in.byte_blocks_at(0x1001))
        self.assertEqual(found, {code_block2, code_block3})
Esempio n. 8
0
    def test_clear(self):
        cfg = gtirb.CFG([
            gtirb.Edge(gtirb.ProxyBlock(), gtirb.ProxyBlock()),
            gtirb.Edge(
                gtirb.CodeBlock(offset=0, size=1),
                gtirb.CodeBlock(offset=1, size=2),
            ),
        ])
        self.assertEqual(len(cfg), 2)

        cfg.clear()
        self.assertEqual(len(cfg), 0)
Esempio n. 9
0
    def test_blocks_at_simple(self):
        s = gtirb.Section()

        bi1 = gtirb.ByteInterval(address=0x1000, size=4, section=s)
        bi1_block1 = gtirb.CodeBlock(offset=0, size=1, byte_interval=bi1)
        bi1_block2 = gtirb.CodeBlock(offset=1, size=1, byte_interval=bi1)

        bi2 = gtirb.ByteInterval(address=0x1004, size=4, section=s)
        bi2_block1 = gtirb.CodeBlock(offset=0, size=1, byte_interval=bi2)
        bi2_block2 = gtirb.CodeBlock(offset=1, size=1, byte_interval=bi2)

        found = set(s.byte_blocks_at(0x1000))
        self.assertEqual(found, {bi1_block1})
Esempio n. 10
0
    def test_blocks_at_overlapping(self):
        "Test that we find the correct blocks if two byte intervals overlap"
        s = gtirb.Section()

        bi1 = gtirb.ByteInterval(address=0x1000, size=4, section=s)
        bi1_block1 = gtirb.CodeBlock(offset=0, size=1, byte_interval=bi1)
        bi1_block2 = gtirb.CodeBlock(offset=1, size=1, byte_interval=bi1)

        bi2 = gtirb.ByteInterval(address=0x1000, size=4, section=s)
        bi2_block1 = gtirb.CodeBlock(offset=0, size=1, byte_interval=bi2)
        bi2_block2 = gtirb.CodeBlock(offset=1, size=1, byte_interval=bi2)

        found = set(s.byte_blocks_at(0x1000))
        self.assertEqual(found, {bi1_block1, bi2_block1})
Esempio n. 11
0
    def test_blocks_on_with_zero_sized_blocks(self, scope):
        "Test that byte_blocks_on doesn't find zero-sized blocks"
        ir, m, s, bi = create_interval_etc(address=0x1000, size=4)
        search_in = scope.select(ir, m, s, bi)

        code_block = gtirb.CodeBlock(offset=0, size=3, byte_interval=bi)
        code_block2 = gtirb.CodeBlock(offset=1, size=0, byte_interval=bi)
        code_block3 = gtirb.CodeBlock(offset=1, size=1, byte_interval=bi)

        found = set(search_in.byte_blocks_on(0x1001))
        self.assertEqual(found, {code_block, code_block3})

        found = set(search_in.byte_blocks_on(range(0x1001, 0x1002)))
        self.assertEqual(found, {code_block, code_block3})
Esempio n. 12
0
    def test_discard(self):
        b1, b2 = gtirb.ProxyBlock(), gtirb.CodeBlock(offset=0, size=1)
        cfg = gtirb.CFG([
            gtirb.Edge(b1, b2),
            gtirb.Edge(gtirb.ProxyBlock(), gtirb.CodeBlock(offset=1, size=2)),
        ])
        self.assertEqual(len(cfg), 2)

        cfg.discard(gtirb.Edge(b1, b2))
        self.assertEqual(len(cfg), 1)
        self.assertFalse(gtirb.Edge(b1, b2) in cfg)

        cfg.discard(gtirb.Edge(b1, b2))
        self.assertEqual(len(cfg), 1)
        self.assertFalse(gtirb.Edge(b1, b2) in cfg)
Esempio n. 13
0
    def test_blocks_on_with_blocks_outside_bi(self):
        "Tests that we can handle byte intervals with blocks outside"
        s = gtirb.Section()

        bi1 = gtirb.ByteInterval(address=0x1000, size=1, section=s)
        bi1_block1 = gtirb.CodeBlock(offset=0, size=1, byte_interval=bi1)
        bi1_block2 = gtirb.CodeBlock(offset=1, size=1, byte_interval=bi1)

        found = set(s.byte_blocks_at(0x1000))
        self.assertEqual(found, {bi1_block1})

        # These blocks are outside of the byte interval's declared size, so
        # either interpretation is fair game.
        found = set(s.byte_blocks_at(0x1001))
        self.assertTrue(found == set() or found == {bi1_block2})
Esempio n. 14
0
def test_insert_bytes():
    ir = gtirb.IR()
    m = gtirb.Module(isa=gtirb.Module.ISA.X64)
    m.ir = ir
    s = gtirb.Section(name=".text")
    s.module = m
    bi = gtirb.ByteInterval(contents=b"\x00\x01\x02\x03\x04\x05\x06\x07",
                            address=0x1000)
    bi.section = s
    b = gtirb.CodeBlock(offset=2, size=2)
    b.byte_interval = bi
    b2 = gtirb.DataBlock(offset=6, size=2)
    b2.byte_interval = bi
    bi.symbolic_expressions[6] = gtirb.SymAddrConst(0, None)
    ctx = gtirb_capstone.RewritingContext(ir)
    ctx.modify_block_insert(m, b, b"\x08\x09", 1)
    assert bi.address == 0x1000
    assert bi.size == 10
    assert bi.contents == b"\x00\x01\x02\x08\x09\x03\x04\x05\x06\x07"
    assert b.offset == 2
    assert b.size == 4
    assert b2.offset == 8
    assert b2.size == 2
    assert 6 not in bi.symbolic_expressions
    assert 8 in bi.symbolic_expressions
Esempio n. 15
0
 def test_from_uuid_typed(self):
     nodes = (
         gtirb.IR(),
         gtirb.Module(),
         gtirb.CodeBlock(size=0),
         gtirb.DataBlock(size=0),
         gtirb.ProxyBlock(),
         gtirb.Section(name="test"),
         gtirb.Symbol(name="test"),
         gtirb.ByteInterval(),
     )
     for node1 in nodes:
         with self.subTest(type(node1).__name__):
             for node_cls in map(type, nodes):
                 node2 = None
                 try:
                     node2 = node_cls.from_uuid(node1.uuid)
                 except TypeError:
                     if node_cls == type(node1):
                         self.fail(
                             "%s.from_uuid raised error returning node of"
                             "its own type" % node_cls)
                 else:
                     if node_cls != type(node1):
                         self.fail("%s.from_uuid returned despite getting"
                                   "argument for a node of type %s" %
                                   (node_cls, type(node1)))
                     else:
                         self.assertEqual(node1, node2)
Esempio n. 16
0
    def test_insert_bytes(self):
        ir = gtirb.IR()
        m = gtirb.Module()
        m.ir = ir
        s = gtirb.Section(name=".text")
        s.module = m
        bi = gtirb.ByteInterval(contents=b"\x00\x01\x02\x03\x04\x05\x06\x07")
        bi.section = s
        b = gtirb.CodeBlock(offset=2, size=2)
        b.byte_interval = bi
        b2 = gtirb.DataBlock(offset=6, size=2)
        b2.byte_interval = bi
        bi.symbolic_expressions[6] = gtirb.SymAddrConst(0, None)

        ctx = gtirb_capstone.RewritingContext(ir)
        ctx.modify_block_insert(m, b, b"\x08\x09", 1)

        self.assertEqual(bi.size, 10)
        self.assertEqual(
            bi.contents, b"\x00\x01\x02\x08\x09\x03\x04\x05\x06\x07"
        )
        self.assertEqual(b.offset, 2)
        self.assertEqual(b.size, 4)
        self.assertEqual(b2.offset, 8)
        self.assertEqual(b2.size, 2)
        self.assertNotIn(6, bi.symbolic_expressions)
        self.assertIn(8, bi.symbolic_expressions)
Esempio n. 17
0
    def test_symbol(self):
        id1 = uuid.uuid4()
        id2 = uuid.uuid4()

        s1 = gtirb.Symbol(name="name", payload=None, uuid=id1)
        s2 = gtirb.Symbol(name="name", payload=None, uuid=id1)
        self.assertTrue(s1.deep_eq(s2))

        s1 = gtirb.Symbol(name="name", payload=5, uuid=id1)
        s2 = gtirb.Symbol(name="name", payload=5, uuid=id1)
        self.assertTrue(s1.deep_eq(s2))

        s1 = gtirb.Symbol(
            name="name",
            payload=gtirb.CodeBlock(size=1, decode_mode=2, offset=3, uuid=id1),
            uuid=id1,
        )
        s2 = gtirb.Symbol(
            name="name",
            payload=gtirb.CodeBlock(size=1, decode_mode=2, offset=3, uuid=id1),
            uuid=id1,
        )
        self.assertTrue(s1.deep_eq(s2))

        s1 = gtirb.Symbol(name="name1", payload=None, uuid=id1)
        s2 = gtirb.Symbol(name="name2", payload=None, uuid=id1)
        self.assertFalse(s1.deep_eq(s2))

        s1 = gtirb.Symbol(name="name", payload=None, uuid=id1)
        s2 = gtirb.Symbol(name="name", payload=5, uuid=id1)
        self.assertFalse(s1.deep_eq(s2))

        s1 = gtirb.Symbol(
            name="name",
            payload=gtirb.CodeBlock(size=1, decode_mode=2, offset=3, uuid=id1),
            uuid=id1,
        )
        s2 = gtirb.Symbol(
            name="name",
            payload=gtirb.CodeBlock(size=2, decode_mode=2, offset=3, uuid=id1),
            uuid=id1,
        )
        self.assertFalse(s1.deep_eq(s2))

        s1 = gtirb.Symbol(name="name", payload=None, uuid=id1)
        s2 = gtirb.Symbol(name="name", payload=None, uuid=id2)
        self.assertFalse(s1.deep_eq(s2))
Esempio n. 18
0
    def test_blocks_at_zero(self, scope):
        ir, m, s, bi = create_interval_etc(address=0, size=4)
        search_in = scope.select(ir, m, s, bi)

        code_block = gtirb.CodeBlock(offset=0, size=3, byte_interval=bi)

        found = set(search_in.byte_blocks_at(0))
        self.assertEqual(found, {code_block})
Esempio n. 19
0
    def test_byte_blocks(self):
        s = gtirb.Section()

        bi1 = gtirb.ByteInterval(size=4, section=s)
        bi1_code_block = gtirb.CodeBlock(offset=0, size=1, byte_interval=bi1)
        bi1_data_block = gtirb.DataBlock(offset=1, size=1, byte_interval=bi1)

        bi2 = gtirb.ByteInterval(size=4, section=s)
        bi2_code_block = gtirb.CodeBlock(offset=0, size=1, byte_interval=bi2)
        bi2_data_block = gtirb.DataBlock(offset=1, size=1, byte_interval=bi2)

        self.assertEqual(
            set(s.byte_blocks),
            {bi1_code_block, bi1_data_block, bi2_code_block, bi2_data_block},
        )
        self.assertEqual(set(s.code_blocks), {bi1_code_block, bi2_code_block})
        self.assertEqual(set(s.data_blocks), {bi1_data_block, bi2_data_block})
Esempio n. 20
0
    def test_blocks_at_range(self, scope):
        "Test that byte_blocks_at works with ranges"
        ir, m, s, bi = create_interval_etc(address=0x1000, size=4)
        search_in = scope.select(ir, m, s, bi)

        code_block = gtirb.CodeBlock(offset=0, size=1, byte_interval=bi)
        code_block2 = gtirb.CodeBlock(offset=1, size=1, byte_interval=bi)
        code_block3 = gtirb.CodeBlock(offset=2, size=1, byte_interval=bi)

        found = set(search_in.byte_blocks_at(range(0x1000, 0x1001)))
        self.assertEqual(found, {code_block})

        found = set(search_in.byte_blocks_at(range(0x1001, 0x1003)))
        self.assertEqual(found, {code_block2, code_block3})

        # Now try with a range with a step to make sure that we actually
        # respect what the range tells us.
        found = set(search_in.byte_blocks_at(range(0x1000, 0x1004, 2)))
        self.assertEqual(found, {code_block, code_block3})
Esempio n. 21
0
    def test_data_blocks_at(self, scope):
        "Test that data_blocks_at only gives back DataBlocks"
        ir, m, s, bi = create_interval_etc(address=0x1000, size=4)
        search_in = scope.select(ir, m, s, bi)

        code_block = gtirb.CodeBlock(offset=0, size=1, byte_interval=bi)
        data_block = gtirb.DataBlock(offset=0, size=1, byte_interval=bi)

        found = set(search_in.data_blocks_at(0x1000))
        self.assertEqual(found, {data_block})
Esempio n. 22
0
    def test_blocks_on_with_range(self, scope):
        "Test that byte_blocks_on handles ranges"
        ir, m, s, bi = create_interval_etc(address=0x1000, size=4)
        search_in = scope.select(ir, m, s, bi)

        code_block = gtirb.CodeBlock(offset=0, size=2, byte_interval=bi)
        code_block2 = gtirb.CodeBlock(offset=2, size=1, byte_interval=bi)
        code_block3 = gtirb.CodeBlock(offset=3, size=1, byte_interval=bi)

        found = set(search_in.byte_blocks_on(range(0x1000, 0x1004)))
        self.assertEqual(found, {code_block, code_block2, code_block3})

        found = set(search_in.byte_blocks_on(range(0x1002, 0x1002)))
        self.assertEqual(found, set())

        # Passing a different step doesn't make a ton of sense, but it should
        # work.
        found = set(search_in.byte_blocks_on(range(0x1000, 0x1004, 2)))
        self.assertEqual(found, {code_block, code_block2, code_block3})
Esempio n. 23
0
    def test_code_blocks(self):
        b = gtirb.CodeBlock()

        self.assertEquals(b.address, None)
        self.assertEquals(b.contents, b"")
        self.assertEquals(set(b.references), set())
        self.assertEquals(set(b.incoming_edges), set())
        self.assertEquals(set(b.outgoing_edges), set())

        bi = gtirb.ByteInterval(address=1, contents=b"abcd1234")
        b.offset = 2
        b.size = 3
        b.byte_interval = bi

        self.assertEquals(b.address, 3)
        self.assertEquals(b.contents, b"cd1")
        self.assertEquals(set(b.references), set())
        self.assertEquals(set(b.incoming_edges), set())
        self.assertEquals(set(b.outgoing_edges), set())

        s = gtirb.Section()
        bi.section = s
        m = gtirb.Module(name="M")
        sym1 = gtirb.Symbol("test", payload=b)
        sym2 = gtirb.Symbol("test", payload=123)
        sym3 = gtirb.Symbol("test", payload=b)
        m.symbols |= {sym1, sym2, sym3}
        s.module = m

        self.assertEquals(b.address, 3)
        self.assertEquals(b.contents, b"cd1")
        self.assertEquals(set(b.references), {sym1, sym3})
        self.assertEquals(set(b.incoming_edges), set())
        self.assertEquals(set(b.outgoing_edges), set())

        i = gtirb.IR()
        m.ir = i
        p1 = gtirb.ProxyBlock()
        p2 = gtirb.ProxyBlock()
        p3 = gtirb.ProxyBlock()
        p4 = gtirb.ProxyBlock()
        i.cfg.add(gtirb.Edge(b, p1))
        i.cfg.add(gtirb.Edge(p2, b))
        i.cfg.add(gtirb.Edge(p3, p4))
        i.cfg.add(gtirb.Edge(b, b))

        self.assertEquals(b.address, 3)
        self.assertEquals(b.contents, b"cd1")
        self.assertEquals(set(b.references), {sym1, sym3})
        self.assertEquals(
            set((s, t) for s, t, l in b.incoming_edges), {(p2, b), (b, b)}
        )
        self.assertEquals(
            set((s, t) for s, t, l in b.outgoing_edges), {(b, p1), (b, b)}
        )
Esempio n. 24
0
 def test_in_edges(self):
     b1, b2, b3 = gtirb.ProxyBlock(), gtirb.ProxyBlock(), gtirb.ProxyBlock()
     b4 = gtirb.CodeBlock(offset=0, size=1)
     cfg = gtirb.CFG([
         gtirb.Edge(b1, b2, gtirb.Edge.Label(gtirb.Edge.Type.Fallthrough)),
         gtirb.Edge(b3, b2, gtirb.Edge.Label(gtirb.Edge.Type.Branch)),
         gtirb.Edge(b1, b3),
     ])
     self.assertEqual(sum(1 for _ in cfg.in_edges(b1)), 0)
     self.assertEqual(sum(1 for _ in cfg.in_edges(b2)), 2)
     self.assertEqual(sum(1 for _ in cfg.in_edges(b3)), 1)
     self.assertEqual(sum(1 for _ in cfg.in_edges(b4)), 0)
Esempio n. 25
0
 def test_from_uuid(self):
     for node1 in (
             gtirb.IR(),
             gtirb.Module(),
             gtirb.CodeBlock(size=0),
             gtirb.DataBlock(size=0),
             gtirb.ProxyBlock(),
             gtirb.Section(name="test"),
             gtirb.Symbol(name="test"),
             gtirb.ByteInterval(),
     ):
         with self.subTest(type(node1).__name__):
             node2 = gtirb.Node.from_uuid(node1.uuid)
             self.assertEqual(node1, node2)
Esempio n. 26
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        ir = gtirb.IR()
        m = gtirb.Module(
            binary_path="binary_path",
            file_format=gtirb.Module.FileFormat.RAW,
            isa=gtirb.Module.ISA.ValidButUnsupported,
            name="name",
            preferred_addr=1,
            rebase_delta=2,
        )
        m.ir = ir
        s = gtirb.Section(
            name="name",
            flags=(
                gtirb.Section.Flag.Executable,
                gtirb.Section.Flag.Readable,
                gtirb.Section.Flag.Loaded,
                gtirb.Section.Flag.Initialized,
            ),
        )
        s.module = m
        bi = gtirb.ByteInterval(address=0, size=10, contents=b"abcd")
        bi.section = s
        cb = gtirb.CodeBlock(size=4, offset=0, decode_mode=1)
        cb.byte_interval = bi
        db = gtirb.DataBlock(size=6, offset=4)
        db.byte_interval = bi
        sym = gtirb.Symbol(name="name", payload=cb)
        sym.module = m
        sac = gtirb.SymAddrConst(0, sym,
                                 {gtirb.SymbolicExpression.Attribute.Part1})
        bi.symbolic_expressions[2] = sac
        p = gtirb.ProxyBlock()
        p.module = m
        ir.cfg.add(
            gtirb.Edge(
                cb,
                p,
                gtirb.Edge.Label(type=gtirb.Edge.Type.Branch,
                                 conditional=False,
                                 direct=True),
            ))
        ir.cfg.add(gtirb.Edge(p, p))
        m.aux_data["key"] = gtirb.AuxData(gtirb.Offset(s, 777), "Offset")
        ir.aux_data["key"] = gtirb.AuxData("value", "string")

        self.ir = ir
Esempio n. 27
0
    def test_code_blocks(self):
        b = gtirb.CodeBlock()

        self.assertEquals(b.address, None)
        self.assertEquals(b.contents, b"")
        self.assertEquals(set(b.references), set())
        self.assertEquals(set(b.incoming_edges), set())
        self.assertEquals(set(b.outgoing_edges), set())

        bi = gtirb.ByteInterval(address=1, contents=b"abcd1234")
        b.offset = 2
        b.size = 3
        b.byte_interval = bi

        self.assertEquals(b.address, 3)
        self.assertEquals(b.contents, b"cd1")
        self.assertEquals(set(b.references), set())
        self.assertEquals(set(b.incoming_edges), set())
        self.assertEquals(set(b.outgoing_edges), set())

        s = gtirb.Section()
        bi.section = s
        m = gtirb.Module()
        sym1 = gtirb.Symbol("test", payload=b)
        sym2 = gtirb.Symbol("test", payload=123)
        sym3 = gtirb.Symbol("test", payload=b)
        m.symbols |= {sym1, sym2, sym3}
        s.module = m

        self.assertEquals(b.address, 3)
        self.assertEquals(b.contents, b"cd1")
        self.assertEquals(set(b.references), {sym1, sym3})
        self.assertEquals(set(b.incoming_edges), set())
        self.assertEquals(set(b.outgoing_edges), set())

        i = gtirb.IR()
        m.ir = i
        e1 = gtirb.Edge(b, gtirb.ProxyBlock())
        e2 = gtirb.Edge(gtirb.ProxyBlock(), b)
        e3 = gtirb.Edge(gtirb.ProxyBlock(), gtirb.ProxyBlock())
        e4 = gtirb.Edge(b, b)
        i.cfg |= {e1, e2, e3, e4}

        self.assertEquals(b.address, 3)
        self.assertEquals(b.contents, b"cd1")
        self.assertEquals(set(b.references), {sym1, sym3})
        self.assertEquals(set(b.incoming_edges), {e2, e4})
        self.assertEquals(set(b.outgoing_edges), {e1, e4})
Esempio n. 28
0
    def test_code_block(self):
        id1 = uuid.uuid4()
        id2 = uuid.uuid4()

        b1 = gtirb.CodeBlock(size=1, decode_mode=2, offset=3, uuid=id1)
        b2 = gtirb.CodeBlock(size=1, decode_mode=2, offset=3, uuid=id1)
        self.assertTrue(b1.deep_eq(b2))

        b2 = gtirb.CodeBlock(size=5, decode_mode=2, offset=3, uuid=id1)
        self.assertFalse(b1.deep_eq(b2))

        b2 = gtirb.CodeBlock(size=1, decode_mode=5, offset=3, uuid=id1)
        self.assertFalse(b1.deep_eq(b2))

        b2 = gtirb.CodeBlock(size=1, decode_mode=2, offset=5, uuid=id1)
        self.assertFalse(b1.deep_eq(b2))

        b2 = gtirb.CodeBlock(size=1, decode_mode=2, offset=3, uuid=id2)
        self.assertFalse(b1.deep_eq(b2))
Esempio n. 29
0
    def test_get_by_uuid(self):
        ir1 = gtirb.IR()
        ir2 = gtirb.IR()

        # test nodes from one IR don't pollute the cache of other IRs
        self.assertIsNone(ir1.get_by_uuid(ir2.uuid))
        self.assertIsNone(ir2.get_by_uuid(ir1.uuid))
        self.assertEqual(ir1.get_by_uuid(ir1.uuid), ir1)
        self.assertEqual(ir2.get_by_uuid(ir2.uuid), ir2)

        m = gtirb.Module()
        m.ir = ir1
        s = gtirb.Section()
        s.module = m
        bi = gtirb.ByteInterval()
        bi.section = s
        b = gtirb.CodeBlock()
        b.byte_interval = bi
        sym = gtirb.Symbol("test")
        sym.module = m

        # test all nodes are lookupable by any other node type
        self.assertEqual(ir1.get_by_uuid(m.uuid), m)
        self.assertEqual(ir1.get_by_uuid(s.uuid), s)
        self.assertEqual(ir1.get_by_uuid(bi.uuid), bi)
        self.assertEqual(ir1.get_by_uuid(b.uuid), b)
        self.assertEqual(ir1.get_by_uuid(sym.uuid), sym)

        self.assertEqual(m.ir.get_by_uuid(m.uuid), m)
        self.assertEqual(m.ir.get_by_uuid(s.uuid), s)
        self.assertEqual(m.ir.get_by_uuid(bi.uuid), bi)
        self.assertEqual(m.ir.get_by_uuid(b.uuid), b)
        self.assertEqual(m.ir.get_by_uuid(sym.uuid), sym)

        self.assertEqual(s.ir.get_by_uuid(m.uuid), m)
        self.assertEqual(s.ir.get_by_uuid(s.uuid), s)
        self.assertEqual(s.ir.get_by_uuid(bi.uuid), bi)
        self.assertEqual(s.ir.get_by_uuid(b.uuid), b)
        self.assertEqual(s.ir.get_by_uuid(sym.uuid), sym)

        self.assertEqual(bi.ir.get_by_uuid(m.uuid), m)
        self.assertEqual(bi.ir.get_by_uuid(s.uuid), s)
        self.assertEqual(bi.ir.get_by_uuid(bi.uuid), bi)
        self.assertEqual(bi.ir.get_by_uuid(b.uuid), b)
        self.assertEqual(bi.ir.get_by_uuid(sym.uuid), sym)

        self.assertEqual(b.ir.get_by_uuid(m.uuid), m)
        self.assertEqual(b.ir.get_by_uuid(s.uuid), s)
        self.assertEqual(b.ir.get_by_uuid(bi.uuid), bi)
        self.assertEqual(b.ir.get_by_uuid(b.uuid), b)
        self.assertEqual(b.ir.get_by_uuid(sym.uuid), sym)

        self.assertEqual(sym.ir.get_by_uuid(m.uuid), m)
        self.assertEqual(sym.ir.get_by_uuid(s.uuid), s)
        self.assertEqual(sym.ir.get_by_uuid(bi.uuid), bi)
        self.assertEqual(sym.ir.get_by_uuid(b.uuid), b)
        self.assertEqual(sym.ir.get_by_uuid(sym.uuid), sym)

        # test removing a node removes all children as well
        bi.section = None

        self.assertEqual(ir1.get_by_uuid(m.uuid), m)
        self.assertEqual(ir1.get_by_uuid(s.uuid), s)
        self.assertIsNone(ir1.get_by_uuid(bi.uuid))
        self.assertIsNone(ir1.get_by_uuid(b.uuid))
        self.assertEqual(ir1.get_by_uuid(sym.uuid), sym)

        bi.section = s

        self.assertEqual(ir1.get_by_uuid(m.uuid), m)
        self.assertEqual(ir1.get_by_uuid(s.uuid), s)
        self.assertEqual(ir1.get_by_uuid(bi.uuid), bi)
        self.assertEqual(ir1.get_by_uuid(b.uuid), b)
        self.assertEqual(ir1.get_by_uuid(sym.uuid), sym)
Esempio n. 30
0
 def test_block(self):
     node = gtirb.CodeBlock(offset=123, size=456, decode_mode=789)
     string = repr(node)
     new_node = eval(string)
     self.assertTrue(node.deep_eq(new_node))