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

i#1921 native sig: Add handling for signals for temp-native threads #4603

Merged
merged 5 commits into from
Dec 14, 2020

Conversation

derekbruening
Copy link
Contributor

Adds a new execute_native_handler() feature where DR delivers a signal
to a currently-native thread. For a native thread, a signal frame is
set up on the appropriate stack, blocked signals are set natively
rather than for emulation, and control is directly sent to the
application's handler.

Adds handling of asynchronous signals by not recording them for
delivery later but delivering immediately.

Adds a test that uses the client interface DR_EMIT_GO_NATIVE to send a
thread native. The app then generates a synchronous and an
asynchronous signal.

Initial attempts are made to also handle a pure-native thread with no
dcontex, such as late in detach, but that portion is not fully
finished and will be completed separately. Handling deault-action
signals is also not fully tested and likely still has some issues to
be worked out.

Fixes several small bugs in the go-native path hit by the test:

  • Moves dynamo_thread_not_under_dynamo() to dispatch_enter_native() to
    avoid problems unlocking bb_building_lock on the path back through
    dispatch.
  • Relaxes a TRY_EXCEPT assert to support a currently-native thread
    with no dcontext in TLS.
  • Suppresses a curiosity from a still-temporarily-native thread at exit.

Issue: #1921

Adds a new execute_native_handler() feature where DR delivers a signal
to a currently-native thread.  For a native thread, a signal frame is
set up on the appropriate stack, blocked signals are set natively
rather than for emulation, and control is directly sent to the
application's handler.

Adds handling of asynchronous signals by not recording them for
delivery later but delivering immediately.

Adds a test that uses the client interface DR_EMIT_GO_NATIVE to send a
thread native.  The app then generates a synchronous and an
asynchronous signal.

Initial attempts are made to also handle a pure-native thread with no
dcontex, such as late in detach, but that portion is not fully
finished and will be completed separately.  Handling deault-action
signals is also not fully tested and likely still has some issues to
be worked out.

Fixes several small bugs in the go-native path hit by the test:
+ Moves dynamo_thread_not_under_dynamo() to dispatch_enter_native() to
  avoid problems unlocking bb_building_lock on the path back through
  dispatch.
+ Relaxes a TRY_EXCEPT assert to support a currently-native thread
  with no dcontext in TLS.
+ Suppresses a curiosity from a still-temporarily-native thread at exit.

Issue: #1921
@derekbruening
Copy link
Contributor Author

Huh, works locally. Looks like compiler complaining about a decl ordering issue or sthg, plus OSX which has a different struct. Shouldn't affect the review -- will tweak to get green later.

@derekbruening derekbruening merged commit 586ea28 into master Dec 14, 2020
@derekbruening derekbruening deleted the i1921-native-signal branch December 14, 2020 17:59
derekbruening added a commit that referenced this pull request Dec 23, 2020
When a signal arrives in an completely unmanaged thread with no
dcontext, typically because DR is detaching, we now deliver the signal
if the application has a handler for it.  This requires adding support
for no dcontext to several parts of the frame setup code even beyond
what was added in PR #4603 for temporarily-native threads.

We have to save the app's handler when we detach a thread so we know
where to send a native signal.  Full support is complex when we're
cleaning up and have no dynamic storage, so we use a single global
handler per signal.  We detect whether multiple handlers are in
operation in this single DR instance (quite rare: only custom
non-pthread clones have this behavior) and in that case we abort like
before on a native signal.  Adds ATOMIC_READ_1BYTE() to complement the
existing atomic operations for a cleaner read of the new multi-handler
flag.

Delivering the frame often overlaps with DR's frame and even DR's
stack usage while delivering, if the app has no sigaltstack.  We add
logic to detect this overlap and avoid clobbering the stack memory.

Alarm signals are still dropped, since they can arrive mid-thread-init
when it is even harder to deliver.

Adds a new test api.detach_signal which creates 10 threads who all sit
in a loop sending 4 different alternating signals (SIGSEGV, SIGBUS,
SIGURG, SIGALRM) while the main thread attaches and then detaches.
When run in debug build, many many signals arrive in post-detach
threads, since detach takes a while to do debug cleanup, exercising
the new code.

Adds a new RSTAT for native signals so we can identify when this
happens in release build.  Exports the stat to the API and uses it to
check that at least some signals were delivered natively in the new
test.

Removes the fatal error on a signal arriving with no dcontext.  But,
non-ignore default signal actions when no handler is present are still
not fully handled, along with multi-sighand-processes as mentioned,
and the fatal error remains in those cases.  For default actions,
since the process is going to terminate anyway, the only shortcoming
of this is whether a core is generated and whether the proper process
exit code is raised.

Issue: #1921
derekbruening added a commit that referenced this pull request Dec 23, 2020
When a signal arrives in an completely unmanaged thread with no
dcontext, typically because DR is detaching, we now deliver the signal
if the application has a handler for it.  This requires adding support
for no dcontext to several parts of the frame setup code even beyond
what was added in PR #4603 for temporarily-native threads.

We have to save the app's handler when we detach a thread so we know
where to send a native signal.  Full support is complex when we're
cleaning up and have no dynamic storage, so we use a single global
handler per signal.  We detect whether multiple handlers are in
operation in this single DR instance (quite rare: only custom
non-pthread clones have this behavior) and in that case we abort like
before on a native signal.  Adds ATOMIC_READ_1BYTE() to complement the
existing atomic operations for a cleaner read of the new multi-handler
flag.

Delivering the frame often overlaps with DR's frame and even DR's
stack usage while delivering, if the app has no sigaltstack.  We add
logic to detect this overlap and avoid clobbering the stack memory.

Alarm signals are still dropped, since they can arrive mid-thread-init
when it is even harder to deliver.

Adds a new test api.detach_signal which creates 10 threads who all sit
in a loop sending 4 different alternating signals (SIGSEGV, SIGBUS,
SIGURG, SIGALRM) while the main thread attaches and then detaches.
When run in debug build, many many signals arrive in post-detach
threads, since detach takes a while to do debug cleanup, exercising
the new code.

Adds a new RSTAT for native signals so we can identify when this
happens in release build.  Exports the stat to the API and uses it to
check that at least some signals were delivered natively in the new
test.

Removes the fatal error on a signal arriving with no dcontext.  But,
non-ignore default signal actions when no handler is present are still
not fully handled, along with multi-sighand-processes as mentioned,
and the fatal error remains in those cases.  For default actions,
since the process is going to terminate anyway, the only shortcoming
of this is whether a core is generated and whether the proper process
exit code is raised.

Issue: #1921
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.

2 participants