frida / frida-gum

Cross-platform instrumentation and introspection library written in C
https://frida.re
Other
753 stars 245 forks source link

Fix #807: recv wait can deadlock on an application thread #808

Closed HexKitchen closed 3 months ago

HexKitchen commented 3 months ago

This is a fix for #807 : recv wait can deadlock on an application thread.

See the issue description for a discussion of the root cause.

The proposed fix here operates by introducing a thread-local variable, event_count_last_seen. After entering the mutex, which ensures a stable reading of core->event_count, the thread enters the wait for event_cond only if core->event_count is equal to event_count_last_seen. In this way, we can guarantee that we won't begin waiting on event_cond at a time when the event we need has already been broadcast, which would produce the deadlock.

oleavr commented 3 months ago

Thanks! Great catch. Will try to get this landed on Monday, in time for 16.4.4.

oleavr commented 3 months ago

Just pushed some changes to try a slightly simpler approach, without thread-local storage. Also pushed some style fixes/tweaks. I'll have to get some sleep now, so I'll land this first thing tomorrow -- but in case you get a chance to test in the meantime please let me know how it goes :)

HexKitchen commented 3 months ago

Super. Agreed this is simpler and solves the deadlock, I was able to confirm in testing as well. Thx!