From eb00bea1ffbaa19e9d3b1c5c1813c4bcb146f5ca Mon Sep 17 00:00:00 2001 From: ReinUsesLisp Date: Tue, 29 Sep 2020 16:10:24 -0300 Subject: [PATCH] backend/x64/exception_handler_posix: Fix signal stack memory leak in SigHandler std::malloc was being called inside SigHandler's constructor without a std::free. This doesn't really matter as SigHandler is used as a singleton and the OS will reclaim that memory. That said, properly freeing memory keeps -fsanitize=address quiet. --- src/backend/x64/exception_handler_posix.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/backend/x64/exception_handler_posix.cpp b/src/backend/x64/exception_handler_posix.cpp index e1a3b190..86b89f59 100644 --- a/src/backend/x64/exception_handler_posix.cpp +++ b/src/backend/x64/exception_handler_posix.cpp @@ -35,6 +35,7 @@ struct CodeBlockInfo { class SigHandler { public: SigHandler(); + ~SigHandler(); void AddCodeBlock(CodeBlockInfo info); void RemoveCodeBlock(u64 rip); @@ -48,6 +49,8 @@ private: bool supports_fast_mem = true; + void* signal_stack_memory = nullptr; + std::vector code_block_infos; std::mutex code_block_infos_mutex; @@ -62,8 +65,10 @@ SigHandler sig_handler; SigHandler::SigHandler() { constexpr size_t signal_stack_size = std::max(SIGSTKSZ, 2 * 1024 * 1024); + signal_stack_memory = std::malloc(signal_stack_size); + stack_t signal_stack; - signal_stack.ss_sp = std::malloc(signal_stack_size); + signal_stack.ss_sp = signal_stack_memory; signal_stack.ss_size = signal_stack_size; signal_stack.ss_flags = 0; if (sigaltstack(&signal_stack, nullptr) != 0) { @@ -91,6 +96,10 @@ SigHandler::SigHandler() { #endif } +SigHandler::~SigHandler() { + std::free(signal_stack_memory); +} + void SigHandler::AddCodeBlock(CodeBlockInfo cbi) { std::lock_guard guard(code_block_infos_mutex); if (auto iter = FindCodeBlockInfo(cbi.code_begin); iter != code_block_infos.end()) {