Skip to content

Commit 3fd73cb

Browse files
authored
fix segfault in backtrace collection (#1707)
1 parent cdb688d commit 3fd73cb

3 files changed

Lines changed: 27 additions & 5 deletions

File tree

‎cmake/utils.cmake‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ function(make_third_party_configuration PIC_ENABLED PROJECT_GENERIC_NAME PROJECT
285285
set(${OUT_LIB_SPECIFIER} ${NO_PIC_LIBRARY_SPECIFIER} PARENT_SCOPE)
286286
endif()
287287

288+
set(EXTRA_COMPILE_FLAGS "${EXTRA_COMPILE_FLAGS} -fno-omit-frame-pointer")
289+
288290
if(COMPILE_RUNTIME_LIGHT)
289291
set(EXTRA_COMPILE_FLAGS "${EXTRA_COMPILE_FLAGS} ${RUNTIME_LIGHT_VISIBILITY}")
290292
endif()

‎common/fast-backtrace.cpp‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <algorithm>
88
#include <cassert>
9+
#include <cstdint>
910
#include <execinfo.h>
1011

1112
#include "common/sanitizer.h"
@@ -43,9 +44,13 @@ int fast_backtrace(void** buffer, int size) {
4344
}
4445

4546
int fast_backtrace_by_bp(void* bp, void* stack_end_, void** buffer, int size) {
46-
stack_frame* current_bp = static_cast<stack_frame*>(bp);
47+
if (stack_end_ == nullptr) {
48+
return 0;
49+
}
50+
const auto last_frame_addr = reinterpret_cast<std::uintptr_t>(stack_end_) - sizeof(stack_frame);
51+
auto* current_bp = static_cast<stack_frame*>(bp);
4752
int i = 0;
48-
while (i < size && reinterpret_cast<char*>(current_bp) <= stack_end_ && !(reinterpret_cast<long>(current_bp) & (sizeof(long) - 1))) {
53+
while (i < size && reinterpret_cast<std::uintptr_t>(current_bp) <= last_frame_addr && !(reinterpret_cast<long>(current_bp) & (sizeof(long) - 1))) {
4954
void* ip = current_bp->ip;
5055
buffer[i++] = ip;
5156
stack_frame* p = current_bp->bp;
@@ -67,9 +72,13 @@ int fast_backtrace_without_recursions(void** buffer, int size) noexcept {
6772
}
6873

6974
int fast_backtrace_without_recursions_by_bp(void* bp, void* stack_end_, void** buffer, int size) noexcept {
70-
stack_frame* current_bp = static_cast<stack_frame*>(bp);
75+
if (stack_end_ == nullptr) {
76+
return 0;
77+
}
78+
const auto last_frame_addr = reinterpret_cast<std::uintptr_t>(stack_end_) - sizeof(stack_frame);
79+
auto* current_bp = static_cast<stack_frame*>(bp);
7180
int i = 0;
72-
while (i < size && reinterpret_cast<char*>(current_bp) <= stack_end_ && !(reinterpret_cast<long>(current_bp) & (sizeof(long) - 1))) {
81+
while (i < size && reinterpret_cast<std::uintptr_t>(current_bp) <= last_frame_addr && !(reinterpret_cast<long>(current_bp) & (sizeof(long) - 1))) {
7382
buffer[i++] = current_bp->ip;
7483
for (int possible_recursion_len = 1; i >= 2 * possible_recursion_len; ++possible_recursion_len) {
7584
if (std::equal(buffer + i - possible_recursion_len, buffer + i, buffer + i - possible_recursion_len * 2)) {

‎runtime-light/stdlib/diagnostics/backtrace.cpp‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Distributed under the GPL v3 License, see LICENSE.notice.txt
44

55
#include <cstddef>
6+
#include <cstdint>
67
#include <iterator>
78
#include <span>
89

@@ -14,14 +15,24 @@
1415

1516
namespace {
1617

18+
bool is_aligned(const void* ptr, std::size_t alignment) noexcept {
19+
return (reinterpret_cast<std::uintptr_t>(ptr) & (alignment - 1)) == 0;
20+
}
21+
1722
size_t sync_frames(std::span<void*> addresses, kphp::coro::stack_frame* start_frame, kphp::coro::stack_frame* stop_frame) noexcept {
1823
kphp::log::assertion(start_frame != nullptr && stop_frame != nullptr);
1924

2025
auto address{addresses.begin()};
21-
for (auto* current_stack_frame{start_frame}; current_stack_frame != stop_frame && address != addresses.end();
26+
for (auto* current_stack_frame{start_frame};
27+
address != addresses.end() && reinterpret_cast<std::uintptr_t>(current_stack_frame) < reinterpret_cast<std::uintptr_t>(stop_frame) &&
28+
is_aligned(current_stack_frame, alignof(void*));
2229
current_stack_frame = current_stack_frame->caller_stack_frame) {
2330
*address = current_stack_frame->return_address;
2431
address = std::next(address);
32+
auto* next{current_stack_frame->caller_stack_frame};
33+
if (reinterpret_cast<std::uintptr_t>(next) <= reinterpret_cast<std::uintptr_t>(current_stack_frame)) {
34+
break;
35+
}
2536
}
2637
return std::distance(addresses.begin(), address);
2738
}

0 commit comments

Comments
 (0)