Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: hooks are global and may come into effect earlier than expected, can deadlock? #44

Open
ndrewh opened this issue Aug 26, 2024 · 1 comment
Labels
bug Something isn't working
Milestone

Comments

@ndrewh
Copy link
Owner

ndrewh commented Aug 26, 2024

We currently allow any thread to modify hooks under most circumstances (e.g. when the thread is blocked after a run_until, or inside a hook implementation). Currently, hooks are shared between all threads (we could use dynamorio's -thread-private option instead, but this comes with higher memory overhead).

Luckily for us, these accesses all occur within Python and are thus subject to the GIL. This should mean our data structures do not need an additional lock, but there is the problem that all hooks are flushed at an arbitrary point in the future i.e. when the next thread returns from a hook (or several other circumstances where we flush all hooks).

We could implement some mechanism by which hooks are committed by a thread, and thus go into effect at a predictable time (although we cannot guarantee they go into effect at the same time in a multithreaded context).

@ndrewh ndrewh added the bug Something isn't working label Aug 26, 2024
Repository owner deleted a comment Aug 26, 2024
Repository owner deleted a comment Aug 26, 2024
@ndrewh
Copy link
Owner Author

ndrewh commented Dec 6, 2024

Luckily for us, these accesses all occur within Python and are thus subject to the GIL.

This assumption is actually wrong. Hooks are flushed after return from a hook, but we release the GIL before flushing. Locking here is maybe a bit tricky since we can't hold any locks around a dr_flush_region call (since all other threads, including threads which may be in Python code, MUST be able to exit the flushed region even if within a clean-call).

I haven't come up with a design that avoids the following deadlock:

  • Thread A enters hook at A1
  • Thread B enters hook at A2
  • Thread A flushes A2
  • ... now A must wait for B to exit (releases GIL so B can run)
  • Thread B flushes A1
  • ... now B must wait for A to exit

We could eliminate the guarantees here by using dr_delay_flush_region (guaranteeing forward progress), but I'd rather have some guarantees about when hooks are actually installed.

This pattern (A2 flushes A1, A2 flushes A1) may be uncommon in actual usage of Pyda (frequent hook re-registration is slow anyway). So maybe we just add some accounting to flush only the hooks that changed, and then I think this becomes a non-issue. We could maybe think about some detection of these cycles, but it's not trivial to detect since it doesn't have to be a perfect address match to cause a cycle (basic block match good enough).

@ndrewh ndrewh changed the title bug: hooks are global and may come into effect earlier than expected bug: hooks are global and may come into effect earlier than expected, can deadlock Dec 6, 2024
@ndrewh ndrewh changed the title bug: hooks are global and may come into effect earlier than expected, can deadlock bug: hooks are global and may come into effect earlier than expected, can deadlock? Dec 6, 2024
@ndrewh ndrewh added this to the 0.4.1 milestone Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant