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})
def test_symbolic_expressions_at(self, scope): ir, m, s, bi = create_interval_etc(address=0x1000, size=5) search_in = scope.select(ir, m, s, bi) sym = gtirb.Symbol(name="hello") expr = gtirb.SymAddrConst(0, sym) bi.symbolic_expressions[1] = expr bi.symbolic_expressions[2] = expr bi.symbolic_expressions[3] = expr bi.symbolic_expressions[4] = expr found = set(search_in.symbolic_expressions_at(0x1001)) self.assertEqual(found, {(bi, 1, expr)}) found = set(search_in.symbolic_expressions_at(range(0x1000, 0x1004))) self.assertEqual(found, {(bi, 1, expr), (bi, 2, expr), (bi, 3, expr)}) found = set( search_in.symbolic_expressions_at(range(0x1000, 0x1004, 2)) ) self.assertEqual(found, {(bi, 2, expr)}) # Now just verify that the index updates correctly when deleting del bi.symbolic_expressions[1] found = set(search_in.symbolic_expressions_at(0x1001)) self.assertEqual(found, set())
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})
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())
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})
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})
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})
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})
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})
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})
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})