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"
|
2020-06-10 02:57:24 +08:00
|
|
|
|
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"
|
2020-06-10 02:57:24 +08:00
|
|
|
#include "hwasan_globals.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 {
|
|
|
|
|
|
|
|
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
|
2020-09-05 10:19:20 +08:00
|
|
|
const char *ubsan_default_options = __ubsan_default_options();
|
2017-12-09 09:31:51 +08:00
|
|
|
ubsan_parser.ParseString(ubsan_default_options);
|
|
|
|
#endif
|
|
|
|
|
2019-06-15 09:37:14 +08:00
|
|
|
parser.ParseStringFromEnv("HWASAN_OPTIONS");
|
2017-12-09 09:31:51 +08:00
|
|
|
#if HWASAN_CONTAINS_UBSAN
|
2019-06-15 09:37:14 +08:00
|
|
|
ubsan_parser.ParseStringFromEnv("UBSAN_OPTIONS");
|
2017-12-09 09:31:51 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
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 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
|
2021-03-17 07:33:04 +08:00
|
|
|
static constexpr uptr kMemoryUsageBufferSize = 4096;
|
|
|
|
|
2018-09-08 08:11:12 +08:00
|
|
|
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();
|
2021-03-17 07:33:04 +08:00
|
|
|
InternalScopedString s;
|
2018-09-08 08:11:12 +08:00
|
|
|
HwasanFormatMemoryUsage(s);
|
|
|
|
internal_strncpy(memory_usage_buffer, s.data(), kMemoryUsageBufferSize - 1);
|
|
|
|
memory_usage_buffer[kMemoryUsageBufferSize - 1] = '\0';
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void UpdateMemoryUsage() {}
|
|
|
|
#endif
|
|
|
|
|
2019-02-01 07:37:12 +08:00
|
|
|
} // namespace __hwasan
|
|
|
|
|
2019-08-07 06:07:29 +08:00
|
|
|
using namespace __hwasan;
|
|
|
|
|
2019-03-02 06:10:49 +08:00
|
|
|
void __sanitizer::BufferedStackTrace::UnwindImpl(
|
|
|
|
uptr pc, uptr bp, void *context, bool request_fast, u32 max_depth) {
|
2019-02-28 06:16:02 +08:00
|
|
|
Thread *t = GetCurrentThread();
|
|
|
|
if (!t) {
|
2020-05-08 07:22:21 +08:00
|
|
|
// The thread is still being created, or has already been destroyed.
|
2019-03-02 06:10:49 +08:00
|
|
|
size = 0;
|
2019-02-28 06:16:02 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-01-17 10:10:03 +08:00
|
|
|
Unwind(max_depth, pc, bp, context, t->stack_top(), t->stack_bottom(),
|
|
|
|
request_fast);
|
2019-02-28 06:16:02 +08:00
|
|
|
}
|
|
|
|
|
2020-06-10 02:57:24 +08:00
|
|
|
static bool InitializeSingleGlobal(const hwasan_global &global) {
|
|
|
|
uptr full_granule_size = RoundDownTo(global.size(), 16);
|
|
|
|
TagMemoryAligned(global.addr(), full_granule_size, global.tag());
|
|
|
|
if (global.size() % 16)
|
|
|
|
TagMemoryAligned(global.addr() + full_granule_size, 16, global.size() % 16);
|
|
|
|
return false;
|
2019-08-07 06:07:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void InitLoadedGlobals() {
|
|
|
|
dl_iterate_phdr(
|
2020-06-10 02:57:24 +08:00
|
|
|
[](dl_phdr_info *info, size_t /* size */, void * /* data */) -> int {
|
|
|
|
for (const hwasan_global &global : HwasanGlobalsFor(
|
|
|
|
info->dlpi_addr, info->dlpi_phdr, info->dlpi_phnum))
|
|
|
|
InitializeSingleGlobal(global);
|
2019-08-07 06:07:29 +08:00
|
|
|
return 0;
|
|
|
|
},
|
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare to run instrumented code on the main thread.
|
|
|
|
static void InitInstrumentation() {
|
|
|
|
if (hwasan_instrumentation_inited) return;
|
|
|
|
|
2019-10-18 04:32:54 +08:00
|
|
|
InitPrctl();
|
|
|
|
|
2019-08-07 06:07:29 +08:00
|
|
|
if (!InitShadow()) {
|
|
|
|
Printf("FATAL: HWAddressSanitizer cannot mmap the shadow memory.\n");
|
|
|
|
DumpProcessMap();
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
|
|
|
InitThreads();
|
|
|
|
hwasanThreadList().CreateCurrentThread();
|
|
|
|
|
|
|
|
hwasan_instrumentation_inited = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interface.
|
2019-02-01 07:37:12 +08:00
|
|
|
|
|
|
|
uptr __hwasan_shadow_memory_dynamic_address; // Global interface symbol.
|
|
|
|
|
2019-06-28 07:16:13 +08:00
|
|
|
// This function was used by the old frame descriptor mechanism. We keep it
|
|
|
|
// around to avoid breaking ABI.
|
|
|
|
void __hwasan_init_frames(uptr beg, uptr end) {}
|
2018-10-23 08:50:40 +08:00
|
|
|
|
2019-02-01 07:37:12 +08:00
|
|
|
void __hwasan_init_static() {
|
|
|
|
InitShadowGOT();
|
|
|
|
InitInstrumentation();
|
2019-08-07 06:07:29 +08:00
|
|
|
|
|
|
|
// In the non-static code path we call dl_iterate_phdr here. But at this point
|
|
|
|
// libc might not have been initialized enough for dl_iterate_phdr to work.
|
|
|
|
// Fortunately, since this is a statically linked executable we can use the
|
|
|
|
// linker-defined symbol __ehdr_start to find the only relevant set of phdrs.
|
|
|
|
extern ElfW(Ehdr) __ehdr_start;
|
2020-06-10 02:57:24 +08:00
|
|
|
for (const hwasan_global &global : HwasanGlobalsFor(
|
|
|
|
/* base */ 0,
|
|
|
|
reinterpret_cast<const ElfW(Phdr) *>(
|
|
|
|
reinterpret_cast<const char *>(&__ehdr_start) +
|
|
|
|
__ehdr_start.e_phoff),
|
|
|
|
__ehdr_start.e_phnum))
|
|
|
|
InitializeSingleGlobal(global);
|
2019-02-01 07:37:12 +08:00
|
|
|
}
|
|
|
|
|
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";
|
|
|
|
|
2021-04-06 07:00:18 +08:00
|
|
|
InitTlsSize();
|
|
|
|
|
2017-12-09 09:31:51 +08:00
|
|
|
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();
|
2019-08-07 06:07:29 +08:00
|
|
|
InitLoadedGlobals();
|
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-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
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-08-07 06:07:29 +08:00
|
|
|
void __hwasan_library_loaded(ElfW(Addr) base, const ElfW(Phdr) * phdr,
|
|
|
|
ElfW(Half) phnum) {
|
2020-06-10 02:57:24 +08:00
|
|
|
for (const hwasan_global &global : HwasanGlobalsFor(base, phdr, phnum))
|
|
|
|
InitializeSingleGlobal(global);
|
2019-08-07 06:07:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void __hwasan_library_unloaded(ElfW(Addr) base, const ElfW(Phdr) * phdr,
|
|
|
|
ElfW(Half) phnum) {
|
|
|
|
for (; phnum != 0; ++phdr, --phnum)
|
|
|
|
if (phdr->p_type == PT_LOAD)
|
|
|
|
TagMemory(base + phdr->p_vaddr, phdr->p_memsz, 0);
|
|
|
|
}
|
|
|
|
|
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() {
|
2021-03-17 07:33:04 +08:00
|
|
|
InternalScopedString s;
|
2018-09-08 08:11:12 +08:00
|
|
|
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"
|