Skip to content

fix: Prevent re-registraion of instruction hooks in gen_blocks_calls#3849

Open
hsh814 wants to merge 2 commits into
AFLplusplus:mainfrom
hsh814:fix
Open

fix: Prevent re-registraion of instruction hooks in gen_blocks_calls#3849
hsh814 wants to merge 2 commits into
AFLplusplus:mainfrom
hsh814:fix

Conversation

@hsh814

@hsh814 hsh814 commented Jun 24, 2026

Copy link
Copy Markdown

Description

Bug

If I register multiple instruction hooks for the same address in libafl_qemu module, it only executes the last registered hook, ignoring others.

Reproduce

fn on_target_loc_covered_1<ET, I, S>(
    _qemu: Qemu,
    _emulator_modules: &mut libafl_qemu::EmulatorModules<ET, I, S>,
    _state: Option<&mut S>,
    _pc: GuestAddr,
) where
    ET: libafl_qemu::modules::EmulatorModuleTuple<I, S>,
    I: Unpin,
    S: Unpin,
{
    log_test("on_target_loc_covered_1");
}

fn on_target_loc_covered_2<ET, I, S>(
    _qemu: Qemu,
    _emulator_modules: &mut libafl_qemu::EmulatorModules<ET, I, S>,
    _state: Option<&mut S>,
    _pc: GuestAddr,
) where
    ET: libafl_qemu::modules::EmulatorModuleTuple<I, S>,
    I: Unpin,
    S: Unpin,
{
    log_test("on_target_loc_covered_2");
}

fn run() -> Result<(), Box<dyn std::error::Error>> {
  // emulator init
  let addr;
  emulator.modules_mut().instruction_function(
      addr,
      on_target_loc_covered_1::<_, NopInput, State>,
      true,
  );
  emulator.modules_mut().instruction_function(
      addr,
      on_target_loc_covered_2::<_, NopInput, State>,
      true,
  );

Output:

on_target_loc_covered_2

Expected:

on_target_loc_covered_2
on_target_loc_covered_1

Patch

Patch is done on qemu-libafl-bridge and LibAFL's libafl_qemu.

  • In qemu-libafl-bridge side, I added PR to patch libafl_qemu_hook_instruction_run(). Handle multiple instruction hooks registered at same address qemu-libafl-bridge#137
  • In LibAFL side, CallTracerModule registers the same hook multiple times when TCG translation block is retranslated. Previously, this issue was hidden because only the most recently registered hook was executed. After patch on qemu-libafl-bridge, it caused duplicate executions of callback functions. So, use HashSet to prevent re-registering instruction hooks for addresses that have already been registered.

Checklist

  • I have run ./scripts/precommit.sh and addressed all comments

@domenukk domenukk requested a review from rmalmain June 30, 2026 15:16
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.

1 participant