From af2905968cdf623bfd80f7d69b8fbc80d6d00c9e Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Fri, 7 May 2021 11:56:31 -0500 Subject: [PATCH] Add sanitizer fiber switching support --- Zend/zend_fibers.c | 32 +++++++++++++++++++++++++++++++- Zend/zend_fibers.h | 5 +++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/Zend/zend_fibers.c b/Zend/zend_fibers.c index 2c10a267359..3167238c518 100644 --- a/Zend/zend_fibers.c +++ b/Zend/zend_fibers.c @@ -39,6 +39,10 @@ # include #endif +#ifdef __SANITIZE_ADDRESS__ +# include +#endif + ZEND_API zend_class_entry *zend_ce_fiber; static zend_class_entry *zend_ce_fiber_error; @@ -180,13 +184,21 @@ static ZEND_NORETURN void zend_fiber_trampoline(transfer_t transfer) { zend_fiber_context *context = transfer.data; +#ifdef __SANITIZE_ADDRESS__ + __sanitizer_finish_switch_fiber(NULL, &context->stack.bottom, &context->stack.capacity); +#endif + context->caller = transfer.context; context->function(context); context->self = NULL; - zend_fiber_suspend_context(context); +#ifdef __SANITIZE_ADDRESS__ + __sanitizer_start_switch_fiber(NULL, context->stack.bottom, context->stack.capacity); +#endif + + jump_fcontext(context->caller, NULL); abort(); } @@ -222,8 +234,17 @@ ZEND_API void zend_fiber_switch_context(zend_fiber_context *to) { ZEND_ASSERT(to && to->self && to->stack.pointer && "Invalid fiber context"); +#ifdef __SANITIZE_ADDRESS__ + void *fake_stack; + __sanitizer_start_switch_fiber(&fake_stack, to->stack.pointer, to->stack.size); +#endif + transfer_t transfer = jump_fcontext(to->self, to); +#ifdef __SANITIZE_ADDRESS__ + __sanitizer_finish_switch_fiber(fake_stack, &to->stack.bottom, &to->stack.capacity); +#endif + to->self = transfer.context; } @@ -231,8 +252,17 @@ ZEND_API void zend_fiber_suspend_context(zend_fiber_context *current) { ZEND_ASSERT(current && current->caller && current->stack.pointer && "Invalid fiber context"); +#ifdef __SANITIZE_ADDRESS__ + void *fake_stack; + __sanitizer_start_switch_fiber(&fake_stack, current->stack.bottom, current->stack.capacity); +#endif + transfer_t transfer = jump_fcontext(current->caller, NULL); +#ifdef __SANITIZE_ADDRESS__ + __sanitizer_finish_switch_fiber(fake_stack, ¤t->stack.bottom, ¤t->stack.capacity); +#endif + current->caller = transfer.context; } diff --git a/Zend/zend_fibers.h b/Zend/zend_fibers.h index 13ff4649b59..4c9cd0b8ba7 100644 --- a/Zend/zend_fibers.h +++ b/Zend/zend_fibers.h @@ -41,6 +41,11 @@ typedef struct _zend_fiber_stack { #ifdef HAVE_VALGRIND int valgrind; #endif + +#ifdef __SANITIZE_ADDRESS__ + const void *bottom; + size_t capacity; +#endif } zend_fiber_stack; typedef struct _zend_fiber_context {