From 9c2ca0c719c9ff19084ec74dae7727460c8acb21 Mon Sep 17 00:00:00 2001
From: Petr Shumilov
Date: Fri, 10 Jan 2025 17:02:21 +0300
Subject: [PATCH] Replace __asan_handle_no_return into
__asan_unpoison_memory_region (#1205)
Signed-off-by: Petr Shumilov
---
server/php-runner.cpp | 9 ++++++---
server/signal-handlers.cpp | 11 ++++++-----
server/signal-handlers.h | 3 +++
3 files changed, 15 insertions(+), 8 deletions(-)
diff --git a/server/php-runner.cpp b/server/php-runner.cpp
index 18880a2d3b..dc1a57598a 100644
--- a/server/php-runner.cpp
+++ b/server/php-runner.cpp
@@ -4,6 +4,7 @@
#include "server/php-runner.h"
+#include
#include
#include
#include
@@ -48,6 +49,9 @@ long long query_stats_id = 1;
std::optional php_script;
+// Memory for alternative signal stack
+extern std::array signal_stack_buffer;
+
namespace {
//TODO: sometimes I need to call old handlers
//TODO: recheck!
@@ -104,9 +108,8 @@ void PhpScript::error(const char *error_message, script_error_t error_type, [[ma
// AddressSanitizer relies on normal function call and return patterns to maintain its internal stack of
// function calls, known as the "shadow stack," which helps it detect stack-related issues like "buffer overflows".
// Functions that do not return, e.g. using setcontext() functionality, can interfere with this, causing ASan to lose track of the actual state of the call stack.
- // By calling __asan_handle_no_return(), we explicitly notify ASan that the current stack frame will not return
- // as expected, allowing it to clean up and adjust its "shadow stack" correctly and avoid false-positive detections.
- __asan_handle_no_return();
+ // By calling ASAN_UNPOISON_MEMORY_REGION, we explicitly clean up and adjust its "shadow stack" correctly and avoid false-positive detections.
+ ASAN_UNPOISON_MEMORY_REGION(&signal_stack_buffer, signal_stack_buffer_size);
__sanitizer_start_switch_fiber(nullptr, main_thread_stack, main_thread_stacksize);
#endif
diff --git a/server/signal-handlers.cpp b/server/signal-handlers.cpp
index f9798db348..422010eb49 100644
--- a/server/signal-handlers.cpp
+++ b/server/signal-handlers.cpp
@@ -4,6 +4,7 @@
#include "server/signal-handlers.h"
+#include
#include
#include
@@ -17,6 +18,9 @@
#include "server/php-engine-vars.h"
#include "server/server-log.h"
+// Memory for alternative signal stack
+std::array signal_stack_buffer;
+
namespace {
void kwrite_str(int fd, const char *s) noexcept {
@@ -237,13 +241,10 @@ void perform_error_if_running(const char *msg, script_error_t error_type, const
//C interface
void init_handlers() {
- constexpr size_t SEGV_STACK_SIZE = 65536;
- static std::array buffer;
-
stack_t segv_stack;
- segv_stack.ss_sp = buffer.data();
+ segv_stack.ss_sp = signal_stack_buffer.data();
segv_stack.ss_flags = 0;
- segv_stack.ss_size = SEGV_STACK_SIZE;
+ segv_stack.ss_size = signal_stack_buffer_size;
sigaltstack(&segv_stack, nullptr);
ksignal(SIGALRM, default_sigalrm_handler);
diff --git a/server/signal-handlers.h b/server/signal-handlers.h
index 1587d953d9..9c8afbed97 100644
--- a/server/signal-handlers.h
+++ b/server/signal-handlers.h
@@ -5,6 +5,9 @@
#include "server/php-runner.h"
#include "server/workers-control.h"
+// The size of buffer for alternative signal stack
+constexpr auto signal_stack_buffer_size = 65536;
+
void perform_error_if_running(const char *msg, script_error_t error_type, const std::optional &triggered_by_signal);
void init_handlers();