Skip to content

Commit

Permalink
Replace __asan_handle_no_return into __asan_unpoison_memory_region (#…
Browse files Browse the repository at this point in the history
…1205)

Signed-off-by: Petr Shumilov <p.shumilov@vkteam.ru>
  • Loading branch information
PetrShumilov authored Jan 10, 2025
1 parent 4c27b83 commit 9c2ca0c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
9 changes: 6 additions & 3 deletions server/php-runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "server/php-runner.h"

#include <array>
#include <cassert>
#include <cerrno>
#include <cstdlib>
Expand Down Expand Up @@ -48,6 +49,9 @@ long long query_stats_id = 1;

std::optional<PhpScript> php_script;

// Memory for alternative signal stack
extern std::array<char, signal_stack_buffer_size> signal_stack_buffer;

namespace {
//TODO: sometimes I need to call old handlers
//TODO: recheck!
Expand Down Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions server/signal-handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "server/signal-handlers.h"

#include <array>
#include <execinfo.h>
#include <sys/time.h>

Expand All @@ -17,6 +18,9 @@
#include "server/php-engine-vars.h"
#include "server/server-log.h"

// Memory for alternative signal stack
std::array<char, signal_stack_buffer_size> signal_stack_buffer;

namespace {

void kwrite_str(int fd, const char *s) noexcept {
Expand Down Expand Up @@ -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<char, SEGV_STACK_SIZE> 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);
Expand Down
3 changes: 3 additions & 0 deletions server/signal-handlers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> &triggered_by_signal);

void init_handlers();
Expand Down

0 comments on commit 9c2ca0c

Please sign in to comment.