Summary: optional/options_parser and optional/backtrace_sanitizer_common are logically separate components. They both use sanitizer-common to power their functionality, but there was an unstated implicit dependency that in order for backtrace_sanitizer_common to function correctly, one had to also use options_parser. This was because options_parser called __sanitizer::InitialiseCommonFlags. This is a requirement for backtrace_sanitizer_common to work, as the sanitizer unwinder uses the sanitizer_common flags and will SEGV on a null page if they're not initialised correctly. This patch removes this hidden dependency. You can now use backtrace_sanitizer_common without the requirements of options_parser. This patch also makes the GWP-ASan unit tests only have a soft dependency on sanitizer-common. The unit tests previously explicitly used __sanitizer::Printf, which is now provided under tests/optional/printf_sanitizer_common. This allows Android to build the unit tests using their own signal-safe printf(). Reviewers: eugenis Reviewed By: eugenis Subscribers: srhines, mgorny, #sanitizers, llvm-commits, vlad.tsyrklevich, morehouse Tags: #sanitizers, #llvm Differential Revision: https://reviews.llvm.org/D66684 llvm-svn: 369825
79 lines
2.9 KiB
C++
79 lines
2.9 KiB
C++
//===-- backtrace_sanitizer_common.cpp --------------------------*- C++ -*-===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include <assert.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
#include "gwp_asan/optional/backtrace.h"
|
|
#include "gwp_asan/options.h"
|
|
#include "sanitizer_common/sanitizer_common.h"
|
|
#include "sanitizer_common/sanitizer_flag_parser.h"
|
|
#include "sanitizer_common/sanitizer_flags.h"
|
|
#include "sanitizer_common/sanitizer_stacktrace.h"
|
|
|
|
void __sanitizer::BufferedStackTrace::UnwindImpl(uptr pc, uptr bp,
|
|
void *context,
|
|
bool request_fast,
|
|
u32 max_depth) {
|
|
if (!StackTrace::WillUseFastUnwind(request_fast)) {
|
|
return Unwind(max_depth, pc, bp, context, 0, 0, request_fast);
|
|
}
|
|
Unwind(max_depth, pc, 0, context, 0, 0, false);
|
|
}
|
|
|
|
namespace {
|
|
size_t Backtrace(uintptr_t *TraceBuffer, size_t Size) {
|
|
__sanitizer::BufferedStackTrace Trace;
|
|
Trace.Reset();
|
|
if (Size > __sanitizer::kStackTraceMax)
|
|
Size = __sanitizer::kStackTraceMax;
|
|
|
|
Trace.Unwind((__sanitizer::uptr)__builtin_return_address(0),
|
|
(__sanitizer::uptr)__builtin_frame_address(0),
|
|
/* ucontext */ nullptr,
|
|
/* fast unwind */ true, Size - 1);
|
|
|
|
memcpy(TraceBuffer, Trace.trace, Trace.size * sizeof(uintptr_t));
|
|
return Trace.size;
|
|
}
|
|
|
|
static void PrintBacktrace(uintptr_t *Trace, size_t TraceLength,
|
|
gwp_asan::options::Printf_t Printf) {
|
|
__sanitizer::StackTrace StackTrace;
|
|
StackTrace.trace = reinterpret_cast<__sanitizer::uptr *>(Trace);
|
|
StackTrace.size = TraceLength;
|
|
|
|
if (StackTrace.size == 0) {
|
|
Printf(" <unknown (does your allocator support backtracing?)>\n\n");
|
|
return;
|
|
}
|
|
|
|
StackTrace.Print();
|
|
}
|
|
} // anonymous namespace
|
|
|
|
namespace gwp_asan {
|
|
namespace options {
|
|
// This function is thread-compatible. It must be synchronised in respect to any
|
|
// other calls to getBacktraceFunction(), calls to getPrintBacktraceFunction(),
|
|
// and calls to either of the functions that they return. Furthermore, this may
|
|
// require synchronisation with any calls to sanitizer_common that use flags.
|
|
// Generally, this function will be called during the initialisation of the
|
|
// allocator, which is done in a thread-compatible manner.
|
|
Backtrace_t getBacktraceFunction() {
|
|
// The unwinder requires the default flags to be set.
|
|
__sanitizer::SetCommonFlagsDefaults();
|
|
__sanitizer::InitializeCommonFlags();
|
|
return Backtrace;
|
|
}
|
|
PrintBacktrace_t getPrintBacktraceFunction() { return PrintBacktrace; }
|
|
} // namespace options
|
|
} // namespace gwp_asan
|