2019-02-27 23:44:03 +08:00
|
|
|
//===-- hwasan.cpp --------------------------------------------------------===//
|
2017-12-09 09:31:51 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2017-12-09 09:31:51 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of HWAddressSanitizer.
|
|
|
|
//
|
|
|
|
// HWAddressSanitizer runtime.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "hwasan.h"
|
2018-12-20 17:10:03 +08:00
|
|
|
#include "hwasan_checks.h"
|
2019-02-01 07:37:12 +08:00
|
|
|
#include "hwasan_dynamic_shadow.h"
|
2017-12-09 09:31:51 +08:00
|
|
|
#include "hwasan_poisoning.h"
|
2018-06-08 07:33:33 +08:00
|
|
|
#include "hwasan_report.h"
|
|
|
|
#include "hwasan_thread.h"
|
2018-09-25 07:03:34 +08:00
|
|
|
#include "hwasan_thread_list.h"
|
2017-12-09 09:31:51 +08:00
|
|
|
#include "sanitizer_common/sanitizer_atomic.h"
|
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
|
|
|
#include "sanitizer_common/sanitizer_flag_parser.h"
|
2018-12-20 17:10:03 +08:00
|
|
|
#include "sanitizer_common/sanitizer_flags.h"
|
2017-12-09 09:31:51 +08:00
|
|
|
#include "sanitizer_common/sanitizer_libc.h"
|
|
|
|
#include "sanitizer_common/sanitizer_procmaps.h"
|
2018-12-20 17:10:03 +08:00
|
|
|
#include "sanitizer_common/sanitizer_stackdepot.h"
|
2017-12-09 09:31:51 +08:00
|
|
|
#include "sanitizer_common/sanitizer_stacktrace.h"
|
|
|
|
#include "sanitizer_common/sanitizer_symbolizer.h"
|
|
|
|
#include "ubsan/ubsan_flags.h"
|
|
|
|
#include "ubsan/ubsan_init.h"
|
|
|
|
|
|
|
|
// ACHTUNG! No system header includes in this file.
|
|
|
|
|
|
|
|
using namespace __sanitizer;
|
|
|
|
|
|
|
|
namespace __hwasan {
|
|
|
|
|
|
|
|
void EnterSymbolizer() {
|
2018-08-30 08:13:20 +08:00
|
|
|
Thread *t = GetCurrentThread();
|
2017-12-09 09:31:51 +08:00
|
|
|
CHECK(t);
|
|
|
|
t->EnterSymbolizer();
|
|
|
|
}
|
|
|
|
void ExitSymbolizer() {
|
2018-08-30 08:13:20 +08:00
|
|
|
Thread *t = GetCurrentThread();
|
2017-12-09 09:31:51 +08:00
|
|
|
CHECK(t);
|
|
|
|
t->LeaveSymbolizer();
|
|
|
|
}
|
|
|
|
bool IsInSymbolizer() {
|
2018-08-30 08:13:20 +08:00
|
|
|
Thread *t = GetCurrentThread();
|
2017-12-09 09:31:51 +08:00
|
|
|
return t && t->InSymbolizer();
|
|
|
|
}
|
|
|
|
|
|
|
|
static Flags hwasan_flags;
|
|
|
|
|
|
|
|
Flags *flags() {
|
|
|
|
return &hwasan_flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hwasan_inited = 0;
|
2019-02-01 07:37:12 +08:00
|
|
|
int hwasan_instrumentation_inited = 0;
|
2017-12-09 09:31:51 +08:00
|
|
|
bool hwasan_init_is_running;
|
|
|
|
|
|
|
|
int hwasan_report_count = 0;
|
|
|
|
|
|
|
|
void Flags::SetDefaults() {
|
|
|
|
#define HWASAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
|
|
|
|
#include "hwasan_flags.inc"
|
|
|
|
#undef HWASAN_FLAG
|
|
|
|
}
|
|
|
|
|
|
|
|
static void RegisterHwasanFlags(FlagParser *parser, Flags *f) {
|
|
|
|
#define HWASAN_FLAG(Type, Name, DefaultValue, Description) \
|
|
|
|
RegisterFlag(parser, #Name, Description, &f->Name);
|
|
|
|
#include "hwasan_flags.inc"
|
|
|
|
#undef HWASAN_FLAG
|
|
|
|
}
|
|
|
|
|
|
|
|
static void InitializeFlags() {
|
|
|
|
SetCommonFlagsDefaults();
|
|
|
|
{
|
|
|
|
CommonFlags cf;
|
|
|
|
cf.CopyFrom(*common_flags());
|
|
|
|
cf.external_symbolizer_path = GetEnv("HWASAN_SYMBOLIZER_PATH");
|
|
|
|
cf.malloc_context_size = 20;
|
|
|
|
cf.handle_ioctl = true;
|
|
|
|
// FIXME: test and enable.
|
|
|
|
cf.check_printf = false;
|
|
|
|
cf.intercept_tls_get_addr = true;
|
|
|
|
cf.exitcode = 99;
|
2019-01-19 09:54:09 +08:00
|
|
|
// 8 shadow pages ~512kB, small enough to cover common stack sizes.
|
|
|
|
cf.clear_shadow_mmap_threshold = 4096 * (SANITIZER_ANDROID ? 2 : 8);
|
2018-09-06 06:46:19 +08:00
|
|
|
// Sigtrap is used in error reporting.
|
2018-02-22 03:52:23 +08:00
|
|
|
cf.handle_sigtrap = kHandleSignalExclusive;
|
2018-09-06 06:46:19 +08:00
|
|
|
|
|
|
|
#if SANITIZER_ANDROID
|
|
|
|
// Let platform handle other signals. It is better at reporting them then we
|
|
|
|
// are.
|
|
|
|
cf.handle_segv = kHandleSignalNo;
|
|
|
|
cf.handle_sigbus = kHandleSignalNo;
|
|
|
|
cf.handle_abort = kHandleSignalNo;
|
|
|
|
cf.handle_sigill = kHandleSignalNo;
|
|
|
|
cf.handle_sigfpe = kHandleSignalNo;
|
|
|
|
#endif
|
2017-12-09 09:31:51 +08:00
|
|
|
OverrideCommonFlags(cf);
|
|
|
|
}
|
|
|
|
|
|
|
|
Flags *f = flags();
|
|
|
|
f->SetDefaults();
|
|
|
|
|
|
|
|
FlagParser parser;
|
|
|
|
RegisterHwasanFlags(&parser, f);
|
|
|
|
RegisterCommonFlags(&parser);
|
|
|
|
|
|
|
|
#if HWASAN_CONTAINS_UBSAN
|
|
|
|
__ubsan::Flags *uf = __ubsan::flags();
|
|
|
|
uf->SetDefaults();
|
|
|
|
|
|
|
|
FlagParser ubsan_parser;
|
|
|
|
__ubsan::RegisterUbsanFlags(&ubsan_parser, uf);
|
|
|
|
RegisterCommonFlags(&ubsan_parser);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Override from user-specified string.
|
|
|
|
if (__hwasan_default_options)
|
|
|
|
parser.ParseString(__hwasan_default_options());
|
|
|
|
#if HWASAN_CONTAINS_UBSAN
|
|
|
|
const char *ubsan_default_options = __ubsan::MaybeCallUbsanDefaultOptions();
|
|
|
|
ubsan_parser.ParseString(ubsan_default_options);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const char *hwasan_options = GetEnv("HWASAN_OPTIONS");
|
|
|
|
parser.ParseString(hwasan_options);
|
|
|
|
#if HWASAN_CONTAINS_UBSAN
|
|
|
|
ubsan_parser.ParseString(GetEnv("UBSAN_OPTIONS"));
|
|
|
|
#endif
|
2018-08-30 06:23:34 +08:00
|
|
|
VPrintf(1, "HWASAN_OPTIONS: %s\n",
|
|
|
|
hwasan_options ? hwasan_options : "<empty>");
|
2017-12-09 09:31:51 +08:00
|
|
|
|
|
|
|
InitializeCommonFlags();
|
|
|
|
|
|
|
|
if (Verbosity()) ReportUnrecognizedFlags();
|
|
|
|
|
|
|
|
if (common_flags()->help) parser.PrintFlagDescriptions();
|
|
|
|
}
|
|
|
|
|
[HWASan] Port HWASan to Linux x86-64 (compiler-rt)
Summary:
Porting HWASan to Linux x86-64, first of the three patches, compiler-rt part.
The approach is similar to ARM case, trap signal is used to communicate
memory tag check failure. int3 instruction is used to generate a signal,
access parameters are stored in nop [eax + offset] instruction immediately
following the int3 one
Had to add HWASan init on malloc because, due to much less interceptors
defined (most other sanitizers intercept much more and get initalized
via one of those interceptors or don't care about malloc), HWASan was not
initialized yet when libstdc++ was trying to allocate memory for its own
fixed-size heap, which led to CHECK-fail in AllocateFromLocalPool.
Also added the CHECK() failure handler with more detailed message and
stack reporting.
Reviewers: eugenis
Subscribers: kubamracek, dberris, mgorny, kristof.beyls, delcypher, #sanitizers, llvm-commits
Differential Revision: https://reviews.llvm.org/D44705
llvm-svn: 328385
2018-03-24 07:38:04 +08:00
|
|
|
static void HWAsanCheckFailed(const char *file, int line, const char *cond,
|
|
|
|
u64 v1, u64 v2) {
|
|
|
|
Report("HWAddressSanitizer CHECK failed: %s:%d \"%s\" (0x%zx, 0x%zx)\n", file,
|
|
|
|
line, cond, (uptr)v1, (uptr)v2);
|
|
|
|
PRINT_CURRENT_STACK_CHECK();
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2018-09-08 08:11:12 +08:00
|
|
|
static constexpr uptr kMemoryUsageBufferSize = 4096;
|
|
|
|
|
|
|
|
static void HwasanFormatMemoryUsage(InternalScopedString &s) {
|
2018-09-25 07:03:34 +08:00
|
|
|
HwasanThreadList &thread_list = hwasanThreadList();
|
|
|
|
auto thread_stats = thread_list.GetThreadStats();
|
2018-09-07 06:08:41 +08:00
|
|
|
auto *sds = StackDepotGetStats();
|
|
|
|
AllocatorStatCounters asc;
|
|
|
|
GetAllocatorStats(asc);
|
2018-09-08 08:11:12 +08:00
|
|
|
s.append(
|
2018-09-07 06:08:41 +08:00
|
|
|
"HWASAN pid: %d rss: %zd threads: %zd stacks: %zd"
|
|
|
|
" thr_aux: %zd stack_depot: %zd uniq_stacks: %zd"
|
2018-09-08 08:11:12 +08:00
|
|
|
" heap: %zd",
|
2018-09-07 06:08:41 +08:00
|
|
|
internal_getpid(), GetRSS(), thread_stats.n_live_threads,
|
|
|
|
thread_stats.total_stack_size,
|
2018-09-25 07:03:34 +08:00
|
|
|
thread_stats.n_live_threads * thread_list.MemoryUsedPerThread(),
|
2018-09-08 08:11:12 +08:00
|
|
|
sds->allocated, sds->n_uniq_ids, asc[AllocatorStatMapped]);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if SANITIZER_ANDROID
|
|
|
|
static char *memory_usage_buffer = nullptr;
|
|
|
|
|
|
|
|
static void InitMemoryUsage() {
|
|
|
|
memory_usage_buffer =
|
|
|
|
(char *)MmapOrDie(kMemoryUsageBufferSize, "memory usage string");
|
|
|
|
CHECK(memory_usage_buffer);
|
|
|
|
memory_usage_buffer[0] = '\0';
|
2019-02-06 09:14:50 +08:00
|
|
|
DecorateMapping((uptr)memory_usage_buffer, kMemoryUsageBufferSize,
|
|
|
|
memory_usage_buffer);
|
2018-09-07 06:08:41 +08:00
|
|
|
}
|
|
|
|
|
2018-09-08 08:11:12 +08:00
|
|
|
void UpdateMemoryUsage() {
|
|
|
|
if (!flags()->export_memory_stats)
|
|
|
|
return;
|
|
|
|
if (!memory_usage_buffer)
|
|
|
|
InitMemoryUsage();
|
|
|
|
InternalScopedString s(kMemoryUsageBufferSize);
|
|
|
|
HwasanFormatMemoryUsage(s);
|
|
|
|
internal_strncpy(memory_usage_buffer, s.data(), kMemoryUsageBufferSize - 1);
|
|
|
|
memory_usage_buffer[kMemoryUsageBufferSize - 1] = '\0';
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void UpdateMemoryUsage() {}
|
|
|
|
#endif
|
|
|
|
|
2018-10-23 08:50:40 +08:00
|
|
|
struct FrameDescription {
|
|
|
|
uptr PC;
|
|
|
|
const char *Descr;
|
|
|
|
};
|
|
|
|
|
2018-10-24 09:35:50 +08:00
|
|
|
struct FrameDescriptionArray {
|
|
|
|
FrameDescription *beg, *end;
|
|
|
|
};
|
|
|
|
|
|
|
|
static InternalMmapVectorNoCtor<FrameDescriptionArray> AllFrames;
|
|
|
|
|
2018-10-23 08:50:40 +08:00
|
|
|
void InitFrameDescriptors(uptr b, uptr e) {
|
|
|
|
FrameDescription *beg = reinterpret_cast<FrameDescription *>(b);
|
|
|
|
FrameDescription *end = reinterpret_cast<FrameDescription *>(e);
|
2018-12-18 08:48:07 +08:00
|
|
|
if (beg == end)
|
|
|
|
return;
|
2018-10-24 09:35:50 +08:00
|
|
|
AllFrames.push_back({beg, end});
|
|
|
|
if (Verbosity())
|
2018-10-23 08:50:40 +08:00
|
|
|
for (FrameDescription *frame_descr = beg; frame_descr < end; frame_descr++)
|
|
|
|
Printf("Frame: %p %s\n", frame_descr->PC, frame_descr->Descr);
|
2018-10-24 09:35:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *GetStackFrameDescr(uptr pc) {
|
|
|
|
for (uptr i = 0, n = AllFrames.size(); i < n; i++)
|
|
|
|
for (auto p = AllFrames[i].beg; p < AllFrames[i].end; p++)
|
|
|
|
if (p->PC == pc)
|
|
|
|
return p->Descr;
|
|
|
|
return nullptr;
|
2018-10-23 08:50:40 +08:00
|
|
|
}
|
|
|
|
|
2019-02-01 07:37:12 +08:00
|
|
|
// Prepare to run instrumented code on the main thread.
|
|
|
|
void InitInstrumentation() {
|
|
|
|
if (hwasan_instrumentation_inited) return;
|
2018-04-24 02:19:23 +08:00
|
|
|
|
2018-08-14 03:57:11 +08:00
|
|
|
if (!InitShadow()) {
|
|
|
|
Printf("FATAL: HWAddressSanitizer cannot mmap the shadow memory.\n");
|
|
|
|
DumpProcessMap();
|
|
|
|
Die();
|
|
|
|
}
|
2019-02-01 07:37:12 +08:00
|
|
|
|
|
|
|
InitThreads();
|
|
|
|
hwasanThreadList().CreateCurrentThread();
|
|
|
|
|
|
|
|
hwasan_instrumentation_inited = 1;
|
2018-08-14 03:57:11 +08:00
|
|
|
}
|
|
|
|
|
2019-02-01 07:37:12 +08:00
|
|
|
} // namespace __hwasan
|
|
|
|
|
2019-02-28 06:16:02 +08:00
|
|
|
void __sanitizer::GetStackTrace(BufferedStackTrace *stack, uptr max_s, uptr pc,
|
|
|
|
uptr bp, void *context,
|
|
|
|
bool request_fast_unwind) {
|
|
|
|
using namespace __hwasan;
|
|
|
|
Thread *t = GetCurrentThread();
|
|
|
|
if (!t) {
|
|
|
|
// the thread is still being created.
|
|
|
|
stack->size = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!StackTrace::WillUseFastUnwind(request_fast_unwind)) {
|
|
|
|
// Block reports from our interceptors during _Unwind_Backtrace.
|
|
|
|
SymbolizerScope sym_scope;
|
|
|
|
return stack->Unwind(max_s, pc, bp, context, 0, 0, request_fast_unwind);
|
|
|
|
}
|
|
|
|
if (StackTrace::WillUseFastUnwind(request_fast_unwind))
|
|
|
|
stack->Unwind(max_s, pc, bp, nullptr, t->stack_top(), t->stack_bottom(),
|
|
|
|
true);
|
|
|
|
else
|
|
|
|
stack->Unwind(max_s, pc, 0, context, 0, 0, false);
|
|
|
|
}
|
|
|
|
|
2019-02-01 07:37:12 +08:00
|
|
|
// Interface.
|
|
|
|
|
|
|
|
using namespace __hwasan;
|
|
|
|
|
|
|
|
uptr __hwasan_shadow_memory_dynamic_address; // Global interface symbol.
|
|
|
|
|
2018-10-23 08:50:40 +08:00
|
|
|
void __hwasan_init_frames(uptr beg, uptr end) {
|
|
|
|
InitFrameDescriptors(beg, end);
|
|
|
|
}
|
|
|
|
|
2019-02-01 07:37:12 +08:00
|
|
|
void __hwasan_init_static() {
|
|
|
|
InitShadowGOT();
|
|
|
|
InitInstrumentation();
|
|
|
|
}
|
|
|
|
|
2017-12-09 09:31:51 +08:00
|
|
|
void __hwasan_init() {
|
|
|
|
CHECK(!hwasan_init_is_running);
|
|
|
|
if (hwasan_inited) return;
|
|
|
|
hwasan_init_is_running = 1;
|
|
|
|
SanitizerToolName = "HWAddressSanitizer";
|
|
|
|
|
|
|
|
InitTlsSize();
|
|
|
|
|
|
|
|
CacheBinaryName();
|
|
|
|
InitializeFlags();
|
|
|
|
|
[HWASan] Port HWASan to Linux x86-64 (compiler-rt)
Summary:
Porting HWASan to Linux x86-64, first of the three patches, compiler-rt part.
The approach is similar to ARM case, trap signal is used to communicate
memory tag check failure. int3 instruction is used to generate a signal,
access parameters are stored in nop [eax + offset] instruction immediately
following the int3 one
Had to add HWASan init on malloc because, due to much less interceptors
defined (most other sanitizers intercept much more and get initalized
via one of those interceptors or don't care about malloc), HWASan was not
initialized yet when libstdc++ was trying to allocate memory for its own
fixed-size heap, which led to CHECK-fail in AllocateFromLocalPool.
Also added the CHECK() failure handler with more detailed message and
stack reporting.
Reviewers: eugenis
Subscribers: kubamracek, dberris, mgorny, kristof.beyls, delcypher, #sanitizers, llvm-commits
Differential Revision: https://reviews.llvm.org/D44705
llvm-svn: 328385
2018-03-24 07:38:04 +08:00
|
|
|
// Install tool-specific callbacks in sanitizer_common.
|
|
|
|
SetCheckFailedCallback(HWAsanCheckFailed);
|
|
|
|
|
2017-12-09 09:31:51 +08:00
|
|
|
__sanitizer_set_report_path(common_flags()->log_path);
|
|
|
|
|
2018-12-13 06:10:52 +08:00
|
|
|
AndroidTestTlsSlot();
|
|
|
|
|
2017-12-09 09:31:51 +08:00
|
|
|
DisableCoreDumperIfNecessary();
|
2018-09-25 07:03:34 +08:00
|
|
|
|
2019-02-01 07:37:12 +08:00
|
|
|
InitInstrumentation();
|
2018-09-25 07:03:34 +08:00
|
|
|
|
2019-02-01 07:37:12 +08:00
|
|
|
// Needs to be called here because flags()->random_tags might not have been
|
|
|
|
// initialized when InitInstrumentation() was called.
|
|
|
|
GetCurrentThread()->InitRandomState();
|
2018-09-25 07:03:34 +08:00
|
|
|
|
2018-08-14 03:57:11 +08:00
|
|
|
MadviseShadow();
|
2017-12-09 09:31:51 +08:00
|
|
|
|
2018-11-10 05:54:03 +08:00
|
|
|
SetPrintfAndReportCallback(AppendToErrorMessageBuffer);
|
2018-08-17 05:56:04 +08:00
|
|
|
// This may call libc -> needs initialized shadow.
|
|
|
|
AndroidLogInit();
|
|
|
|
|
2018-06-26 01:27:13 +08:00
|
|
|
InitializeInterceptors();
|
|
|
|
InstallDeadlySignalHandlers(HwasanOnDeadlySignal);
|
|
|
|
InstallAtExitHandler(); // Needs __cxa_atexit interceptor.
|
|
|
|
|
2017-12-09 09:31:51 +08:00
|
|
|
Symbolizer::GetOrInit()->AddHooks(EnterSymbolizer, ExitSymbolizer);
|
|
|
|
|
|
|
|
InitializeCoverage(common_flags()->coverage, common_flags()->coverage_dir);
|
|
|
|
|
[hwasan] Add a (almost) no-interceptor mode.
Summary:
The idea behind this change is to allow sanitization of libc. We are prototyping on Bionic,
but the tool interface will be general enough (or at least generalizable) to support any other libc.
When libc depends on libclang_rt.hwasan, the latter can not interpose libc functions.
In fact, majority of interceptors become unnecessary when libc code is instrumented.
This change gets rid of most hwasan interceptors and provides interface for libc to notify
hwasan about thread creation and destruction events. Some interceptors (pthread_create)
are kept under #ifdef to enable testing with uninstrumented libc. They are expressed in
terms of the new libc interface.
The new cmake switch, COMPILER_RT_HWASAN_WITH_INTERCEPTORS, ON by default, builds testing
version of the library with the aforementioned pthread_create interceptor.
With the OFF setting, the library becomes more of a libc plugin.
Reviewers: vitalybuka, kcc, jfb
Subscribers: srhines, kubamracek, mgorny, jfb, llvm-commits
Differential Revision: https://reviews.llvm.org/D50922
llvm-svn: 340216
2018-08-21 05:49:15 +08:00
|
|
|
HwasanTSDInit();
|
2018-09-25 07:03:34 +08:00
|
|
|
HwasanTSDThreadInit();
|
2017-12-09 09:31:51 +08:00
|
|
|
|
|
|
|
HwasanAllocatorInit();
|
|
|
|
|
|
|
|
#if HWASAN_CONTAINS_UBSAN
|
|
|
|
__ubsan::InitAsPlugin();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
VPrintf(1, "HWAddressSanitizer init done\n");
|
|
|
|
|
|
|
|
hwasan_init_is_running = 0;
|
|
|
|
hwasan_inited = 1;
|
|
|
|
}
|
|
|
|
|
2018-08-15 08:39:35 +08:00
|
|
|
void __hwasan_print_shadow(const void *p, uptr sz) {
|
2018-08-30 06:21:22 +08:00
|
|
|
uptr ptr_raw = UntagAddr(reinterpret_cast<uptr>(p));
|
2018-08-30 06:42:16 +08:00
|
|
|
uptr shadow_first = MemToShadow(ptr_raw);
|
|
|
|
uptr shadow_last = MemToShadow(ptr_raw + sz - 1);
|
2018-08-15 08:39:35 +08:00
|
|
|
Printf("HWASan shadow map for %zx .. %zx (pointer tag %x)\n", ptr_raw,
|
|
|
|
ptr_raw + sz, GetTagFromPointer((uptr)p));
|
|
|
|
for (uptr s = shadow_first; s <= shadow_last; ++s)
|
2018-08-30 06:42:16 +08:00
|
|
|
Printf(" %zx: %x\n", ShadowToMem(s), *(tag_t *)s);
|
2017-12-09 09:31:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sptr __hwasan_test_shadow(const void *p, uptr sz) {
|
|
|
|
if (sz == 0)
|
|
|
|
return -1;
|
|
|
|
tag_t ptr_tag = GetTagFromPointer((uptr)p);
|
2018-08-30 06:21:22 +08:00
|
|
|
uptr ptr_raw = UntagAddr(reinterpret_cast<uptr>(p));
|
2018-08-30 06:42:16 +08:00
|
|
|
uptr shadow_first = MemToShadow(ptr_raw);
|
|
|
|
uptr shadow_last = MemToShadow(ptr_raw + sz - 1);
|
2017-12-09 09:31:51 +08:00
|
|
|
for (uptr s = shadow_first; s <= shadow_last; ++s)
|
2019-01-21 17:51:10 +08:00
|
|
|
if (*(tag_t *)s != ptr_tag) {
|
|
|
|
sptr offset = ShadowToMem(s) - ptr_raw;
|
|
|
|
return offset < 0 ? 0 : offset;
|
|
|
|
}
|
2017-12-09 09:31:51 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
u16 __sanitizer_unaligned_load16(const uu16 *p) {
|
|
|
|
return *p;
|
|
|
|
}
|
|
|
|
u32 __sanitizer_unaligned_load32(const uu32 *p) {
|
|
|
|
return *p;
|
|
|
|
}
|
|
|
|
u64 __sanitizer_unaligned_load64(const uu64 *p) {
|
|
|
|
return *p;
|
|
|
|
}
|
|
|
|
void __sanitizer_unaligned_store16(uu16 *p, u16 x) {
|
|
|
|
*p = x;
|
|
|
|
}
|
|
|
|
void __sanitizer_unaligned_store32(uu32 *p, u32 x) {
|
|
|
|
*p = x;
|
|
|
|
}
|
|
|
|
void __sanitizer_unaligned_store64(uu64 *p, u64 x) {
|
|
|
|
*p = x;
|
|
|
|
}
|
|
|
|
|
2018-04-19 06:05:18 +08:00
|
|
|
void __hwasan_loadN(uptr p, uptr sz) {
|
2017-12-21 03:05:44 +08:00
|
|
|
CheckAddressSized<ErrorAction::Abort, AccessType::Load>(p, sz);
|
|
|
|
}
|
|
|
|
void __hwasan_load1(uptr p) {
|
|
|
|
CheckAddress<ErrorAction::Abort, AccessType::Load, 0>(p);
|
|
|
|
}
|
|
|
|
void __hwasan_load2(uptr p) {
|
|
|
|
CheckAddress<ErrorAction::Abort, AccessType::Load, 1>(p);
|
|
|
|
}
|
|
|
|
void __hwasan_load4(uptr p) {
|
|
|
|
CheckAddress<ErrorAction::Abort, AccessType::Load, 2>(p);
|
|
|
|
}
|
|
|
|
void __hwasan_load8(uptr p) {
|
|
|
|
CheckAddress<ErrorAction::Abort, AccessType::Load, 3>(p);
|
|
|
|
}
|
|
|
|
void __hwasan_load16(uptr p) {
|
|
|
|
CheckAddress<ErrorAction::Abort, AccessType::Load, 4>(p);
|
|
|
|
}
|
|
|
|
|
2018-04-19 06:05:18 +08:00
|
|
|
void __hwasan_loadN_noabort(uptr p, uptr sz) {
|
2017-12-21 03:05:44 +08:00
|
|
|
CheckAddressSized<ErrorAction::Recover, AccessType::Load>(p, sz);
|
|
|
|
}
|
|
|
|
void __hwasan_load1_noabort(uptr p) {
|
|
|
|
CheckAddress<ErrorAction::Recover, AccessType::Load, 0>(p);
|
|
|
|
}
|
|
|
|
void __hwasan_load2_noabort(uptr p) {
|
|
|
|
CheckAddress<ErrorAction::Recover, AccessType::Load, 1>(p);
|
|
|
|
}
|
|
|
|
void __hwasan_load4_noabort(uptr p) {
|
|
|
|
CheckAddress<ErrorAction::Recover, AccessType::Load, 2>(p);
|
|
|
|
}
|
|
|
|
void __hwasan_load8_noabort(uptr p) {
|
|
|
|
CheckAddress<ErrorAction::Recover, AccessType::Load, 3>(p);
|
|
|
|
}
|
|
|
|
void __hwasan_load16_noabort(uptr p) {
|
|
|
|
CheckAddress<ErrorAction::Recover, AccessType::Load, 4>(p);
|
|
|
|
}
|
|
|
|
|
2018-04-19 06:05:18 +08:00
|
|
|
void __hwasan_storeN(uptr p, uptr sz) {
|
2017-12-21 03:05:44 +08:00
|
|
|
CheckAddressSized<ErrorAction::Abort, AccessType::Store>(p, sz);
|
|
|
|
}
|
|
|
|
void __hwasan_store1(uptr p) {
|
|
|
|
CheckAddress<ErrorAction::Abort, AccessType::Store, 0>(p);
|
|
|
|
}
|
|
|
|
void __hwasan_store2(uptr p) {
|
|
|
|
CheckAddress<ErrorAction::Abort, AccessType::Store, 1>(p);
|
|
|
|
}
|
|
|
|
void __hwasan_store4(uptr p) {
|
|
|
|
CheckAddress<ErrorAction::Abort, AccessType::Store, 2>(p);
|
|
|
|
}
|
|
|
|
void __hwasan_store8(uptr p) {
|
|
|
|
CheckAddress<ErrorAction::Abort, AccessType::Store, 3>(p);
|
|
|
|
}
|
|
|
|
void __hwasan_store16(uptr p) {
|
|
|
|
CheckAddress<ErrorAction::Abort, AccessType::Store, 4>(p);
|
|
|
|
}
|
|
|
|
|
2018-04-19 06:05:18 +08:00
|
|
|
void __hwasan_storeN_noabort(uptr p, uptr sz) {
|
2017-12-21 03:05:44 +08:00
|
|
|
CheckAddressSized<ErrorAction::Recover, AccessType::Store>(p, sz);
|
|
|
|
}
|
|
|
|
void __hwasan_store1_noabort(uptr p) {
|
|
|
|
CheckAddress<ErrorAction::Recover, AccessType::Store, 0>(p);
|
|
|
|
}
|
|
|
|
void __hwasan_store2_noabort(uptr p) {
|
|
|
|
CheckAddress<ErrorAction::Recover, AccessType::Store, 1>(p);
|
|
|
|
}
|
|
|
|
void __hwasan_store4_noabort(uptr p) {
|
|
|
|
CheckAddress<ErrorAction::Recover, AccessType::Store, 2>(p);
|
|
|
|
}
|
|
|
|
void __hwasan_store8_noabort(uptr p) {
|
|
|
|
CheckAddress<ErrorAction::Recover, AccessType::Store, 3>(p);
|
|
|
|
}
|
|
|
|
void __hwasan_store16_noabort(uptr p) {
|
|
|
|
CheckAddress<ErrorAction::Recover, AccessType::Store, 4>(p);
|
|
|
|
}
|
2017-12-09 09:31:51 +08:00
|
|
|
|
2018-01-12 06:53:30 +08:00
|
|
|
void __hwasan_tag_memory(uptr p, u8 tag, uptr sz) {
|
|
|
|
TagMemoryAligned(p, sz, tag);
|
|
|
|
}
|
|
|
|
|
2018-08-15 08:39:35 +08:00
|
|
|
uptr __hwasan_tag_pointer(uptr p, u8 tag) {
|
|
|
|
return AddTagToPointer(p, tag);
|
|
|
|
}
|
|
|
|
|
2018-08-17 07:17:14 +08:00
|
|
|
void __hwasan_handle_longjmp(const void *sp_dst) {
|
|
|
|
uptr dst = (uptr)sp_dst;
|
|
|
|
// HWASan does not support tagged SP.
|
|
|
|
CHECK(GetTagFromPointer(dst) == 0);
|
|
|
|
|
|
|
|
uptr sp = (uptr)__builtin_frame_address(0);
|
|
|
|
static const uptr kMaxExpectedCleanupSize = 64 << 20; // 64M
|
|
|
|
if (dst < sp || dst - sp > kMaxExpectedCleanupSize) {
|
|
|
|
Report(
|
|
|
|
"WARNING: HWASan is ignoring requested __hwasan_handle_longjmp: "
|
|
|
|
"stack top: %p; target %p; distance: %p (%zd)\n"
|
|
|
|
"False positive error reports may follow\n",
|
|
|
|
(void *)sp, (void *)dst, dst - sp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
TagMemory(sp, dst - sp, 0);
|
|
|
|
}
|
|
|
|
|
[hwasan, asan] Intercept vfork.
Summary:
Intercept vfork on arm, aarch64, i386 and x86_64.
Reviewers: pcc, vitalybuka
Subscribers: kubamracek, mgorny, javed.absar, krytarowski, kristof.beyls, #sanitizers, llvm-commits
Tags: #sanitizers, #llvm
Differential Revision: https://reviews.llvm.org/D58533
llvm-svn: 355030
2019-02-28 05:11:50 +08:00
|
|
|
void __hwasan_handle_vfork(const void *sp_dst) {
|
|
|
|
uptr sp = (uptr)sp_dst;
|
|
|
|
Thread *t = GetCurrentThread();
|
|
|
|
CHECK(t);
|
|
|
|
uptr top = t->stack_top();
|
|
|
|
uptr bottom = t->stack_bottom();
|
2019-03-01 04:43:24 +08:00
|
|
|
if (top == 0 || bottom == 0 || sp < bottom || sp >= top) {
|
[hwasan, asan] Intercept vfork.
Summary:
Intercept vfork on arm, aarch64, i386 and x86_64.
Reviewers: pcc, vitalybuka
Subscribers: kubamracek, mgorny, javed.absar, krytarowski, kristof.beyls, #sanitizers, llvm-commits
Tags: #sanitizers, #llvm
Differential Revision: https://reviews.llvm.org/D58533
llvm-svn: 355030
2019-02-28 05:11:50 +08:00
|
|
|
Report(
|
|
|
|
"WARNING: HWASan is ignoring requested __hwasan_handle_vfork: "
|
|
|
|
"stack top: %zx; current %zx; bottom: %zx \n"
|
|
|
|
"False positive error reports may follow\n",
|
|
|
|
top, sp, bottom);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
TagMemory(bottom, sp - bottom, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void *__hwasan_extra_spill_area() {
|
|
|
|
Thread *t = GetCurrentThread();
|
|
|
|
return &t->vfork_spill();
|
|
|
|
}
|
|
|
|
|
2018-09-08 08:11:12 +08:00
|
|
|
void __hwasan_print_memory_usage() {
|
|
|
|
InternalScopedString s(kMemoryUsageBufferSize);
|
|
|
|
HwasanFormatMemoryUsage(s);
|
|
|
|
Printf("%s\n", s.data());
|
|
|
|
}
|
2018-09-07 06:08:41 +08:00
|
|
|
|
2018-01-13 09:32:15 +08:00
|
|
|
static const u8 kFallbackTag = 0xBB;
|
|
|
|
|
|
|
|
u8 __hwasan_generate_tag() {
|
2018-08-30 08:13:20 +08:00
|
|
|
Thread *t = GetCurrentThread();
|
2018-01-13 09:32:15 +08:00
|
|
|
if (!t) return kFallbackTag;
|
|
|
|
return t->GenerateRandomTag();
|
|
|
|
}
|
|
|
|
|
2017-12-09 09:31:51 +08:00
|
|
|
#if !SANITIZER_SUPPORTS_WEAK_HOOKS
|
|
|
|
extern "C" {
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
|
|
|
|
const char* __hwasan_default_options() { return ""; }
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
SANITIZER_INTERFACE_ATTRIBUTE
|
|
|
|
void __sanitizer_print_stack_trace() {
|
|
|
|
GET_FATAL_STACK_TRACE_PC_BP(StackTrace::GetCurrentPc(), GET_CURRENT_FRAME());
|
|
|
|
stack.Print();
|
|
|
|
}
|
|
|
|
} // extern "C"
|