Skip to content

Commit

Permalink
Merge branch 'jranieri/delete_fix' into 'main'
Browse files Browse the repository at this point in the history
Only promote blocks to functionEntries if they are in the same func

See merge request rewriting/gtirb-rewriting!162
  • Loading branch information
jdorn-gt committed May 31, 2024
2 parents b045d8e + cc3e7e8 commit 1e116e6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
13 changes: 9 additions & 4 deletions gtirb_rewriting/_modify/remove.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,21 @@ def _update_functions_aux_data(
return

# If it was previously a function entry block, the next block gets
# promoted into that role.
# promoted into that role -- but only if it's in the same function.
aux_function_entries = _auxdata.function_entries.get(block.module)
if (
aux_function_entries
and block in aux_function_entries[function_uuid]
and isinstance(next_block, gtirb.CodeBlock)
and cache.in_same_function(block, next_block)
):
# TODO: If we have a next block and it's a data block, we might want
# to record that somewhere so that when we delete the data block we
# can promote the next block to an entry.
# TODO: If we are deleting code block B1 that is a function entry,
# then data block B2 that follows it, and there is a code block
# B3 following that, we won't promote B3 to be a function
# entry because we lose track of that property after the first
# deletion because B2 is a data block. If we want to be less
# lossy in this specific case we'd have to keep track of data
# blocks that should be considered entry blocks.
aux_function_entries[function_uuid].add(next_block)

remove_function_block_aux(cache, block)
Expand Down
39 changes: 39 additions & 0 deletions tests/test_deletions.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,3 +835,42 @@ def test_delete_zero_sized_block():
assert b2_sym.referent is b2
assert bi.blocks == {b2}
assert set(ir.cfg) == set()


def test_delete_function_entry():
"""
Test that deleting a function entry block does not promote an unrelated
block to be the function entry.
"""
ir, m = create_test_module(
gtirb.Module.FileFormat.ELF, gtirb.Module.ISA.X64
)
_, bi = add_text_section(m, address=0x1000)

b1 = add_code_block(bi, b"\x90")
b2 = add_code_block(bi, b"\x90")

func1 = add_function_object(m, "func1", b1)
func2 = add_function_object(m, "func2", b2)

assert m.aux_data["functionEntries"].data == {
func1.uuid: {b1},
func2.uuid: {b2},
}
assert m.aux_data["functionBlocks"].data == {
func1.uuid: {b1},
func2.uuid: {b2},
}

# Test that deleting a function entry block does not promote another
# function's block into that role.
ctx = gtirb_rewriting.RewritingContext(m, [func1, func2])
ctx.delete_at(b1, 0, b1.size)
ctx.apply()

assert m.aux_data["functionEntries"].data == {
func2.uuid: {b2},
}
assert m.aux_data["functionBlocks"].data == {
func2.uuid: {b2},
}

0 comments on commit 1e116e6

Please sign in to comment.