Skip to content

fix: no-op when signal handlers are called on threads not managed by … #9766

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

Merged
merged 2 commits into from
Oct 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ PHP NEWS
. SA_ONSTACK is now set for signal handlers to be friendlier to other
in-process code such as Go's cgo. (Kévin Dunglas)
. SA_ONSTACK is now set when signals are disabled. (Kévin Dunglas)
. Fix GH-9649: Signal handlers now do a no-op instead of crashing when
executed on threads not managed by TSRM. (Kévin Dunglas)

- Fileinfo:
. Upgrade bundled libmagic to 5.43. (Anatol)
Expand Down
5 changes: 5 additions & 0 deletions TSRM/TSRM.c
Original file line number Diff line number Diff line change
Expand Up @@ -779,4 +779,9 @@ TSRM_API const char *tsrm_api_name(void)
#endif
}/*}}}*/

TSRM_API bool tsrm_is_managed_thread()
{/*{{{*/
return tsrm_tls_get() ? true : false;
}/*}}}*/

#endif /* ZTS */
1 change: 1 addition & 0 deletions TSRM/TSRM.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ TSRM_API size_t tsrm_get_ls_cache_tcb_offset(void);
TSRM_API bool tsrm_is_main_thread(void);
TSRM_API bool tsrm_is_shutdown(void);
TSRM_API const char *tsrm_api_name(void);
TSRM_API bool tsrm_is_managed_thread(void);

#ifdef TSRM_WIN32
# define TSRM_TLS __declspec(thread)
Expand Down
8 changes: 7 additions & 1 deletion Zend/zend_execute_API.c
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,13 @@ ZEND_API ZEND_NORETURN void ZEND_FASTCALL zend_timeout(void) /* {{{ */
#ifndef ZEND_WIN32
static void zend_timeout_handler(int dummy) /* {{{ */
{
#ifndef ZTS
#ifdef ZTS
if (!tsrm_is_managed_thread()) {
fprintf(stderr, "zend_timeout_handler() called in a thread not managed by PHP. The expected signal handler will not be called. This is probably a bug.\n");

return;
}
#else
if (zend_atomic_bool_load_ex(&EG(timed_out))) {
/* Die on hard timeout */
const char *error_filename = NULL;
Expand Down
6 changes: 6 additions & 0 deletions Zend/zend_signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ void zend_signal_handler_defer(int signo, siginfo_t *siginfo, void *context)
zend_signal_queue_t *queue, *qtmp;

#ifdef ZTS
if (!tsrm_is_managed_thread()) {
fprintf(stderr, "zend_signal_handler_defer() called in a thread not managed by PHP. The expected signal handler will not be called. This is probably a bug.\n");

return;
}

/* A signal could hit after TSRM shutdown, in this case globals are already freed. */
if (tsrm_is_shutdown()) {
/* Forward to default handler handler */
Expand Down