Skip to content

Keep IRSB.extend() from corrupting the appended block and dropping state - #608

Open
NopeCharon wants to merge 2 commits into
angr:masterfrom
NopeCharon:fix/irsb-extend-state
Open

NopeCharon wants to merge 2 commits into
angr:masterfrom
NopeCharon:fix/irsb-extend-state

Conversation

@NopeCharon

Copy link
Copy Markdown
Contributor

Fixes #187.

IRSB.extend() renumbered the tmps of the appended block's statements in place. That left the appended IRSB pointing at tmps outside its own type environment, so appending it to a second block failed with IndexError in IRTypeEnv.lookup(). This is the case from the issue. Extending an empty IRSB also shared the appended block's statement list and type environment, so a later extend() grew the original block too.

The joined block also kept stale state from the first block. instruction_addresses, default_exit_target and a cached exit_statements were never updated, and the appended block's data_refs and const_vals were dropped (the # TODO: Change exit_statements, data_references, etc.). lift() joins blocks this way whenever a gym spotter lifts an instruction that libVEX cannot decode, so this affects normal lifting. For mov rax, [rip+0x100]; swapgs; mov rbx, [rip+0x200]; jz 0x1000 on AMD64, master gives:

instruction_addresses  (0x400000, 0x400007)        # the swapgs, the second mov and the jz are missing
constant_jump_targets  {0x1000}                    # fallthrough missing, default_exit_target is None
data_refs              [0x400107 @ 0x400000]       # the load at 0x40000a is missing

With load_from_ro_regions/const_prop, an IAT call qword ptr [rip+disp] after a spotter-lifted instruction also loses its folded target in const_vals.

Changes:

  • extend() now renumbers copies of the appended statements, so the appended IRSB is left unchanged and can be reused. The empty-block path no longer shares the statement list or the type environment.
  • The joined block concatenates instruction_addresses, takes the appended block's default_exit_target, and recomputes exit_statements on demand. It merges data_refs and const_vals, offsetting their statement indices and renumbering the const_vals tmps. is_noop_block is true only if both blocks are no-ops.
  • lift() discards the block it appends, so it calls a private _extend(..., copy_statements=False) and skips the copy. That keeps the in-place statement reuse mentioned in the issue thread, and the spotter path is as fast as before (a deep copy there made it about 2.3x slower in a quick benchmark).
  • I removed the dead convert_expr() calls on Dirty args. Their result was discarded, and the args are already converted through stmt_.expressions.

Test: tests/test_irsb_extend.py covers:

  • appending one IRSB to two blocks (the issue's case)
  • the empty-block aliasing
  • exit_statements cached before an extend
  • the spotter path's instruction addresses, exits and data references
  • an IAT call's folded const_vals after a spotter-lifted instruction

All five tests fail on master. The full test suite passes with the change.

extend() renumbered the tmps of the appended IRSB's statements in place,
so that IRSB was left referring to tmps outside its own type
environment. Appending the same IRSB to a second block then failed with
an IndexError in IRTypeEnv.lookup(). Extending an empty IRSB also shared
the appended block's statement list and type environment, so a later
extend() grew the original block as well.

The joined block also kept stale derived state from the first block:
instruction_addresses, default_exit_target and a cached exit_statements
were not updated, and the data_refs and const_vals of the appended block
were dropped. lift() joins blocks this way whenever a gym spotter lifts
an instruction libVEX cannot decode, so such blocks were missing
instruction addresses, the constant default exit target, the data
references after the spotter-lifted instruction, and the read-only
region constants (for example a folded IAT call target).

extend() now renumbers copies of the appended statements, so the
appended IRSB is left untouched and can be reused. The joined block
concatenates instruction_addresses, takes the appended block's
default_exit_target, recomputes exit_statements on demand, and merges
data_refs and const_vals with their statement indices offset and their
tmps renumbered. lift() discards the block it appends, so it skips the
copy through a private _extend() to keep that path as fast as before.

Fixes angr#187.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error when extending IRSBs

1 participant