2019-07-31 18:51:27 +00:00
|
|
|
//===-- sanitizer_common_libcdep.cpp --------------------------------------===//
|
2013-05-17 16:17:19 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00: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
|
2013-05-17 16:17:19 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is shared between AddressSanitizer and ThreadSanitizer
|
|
|
|
// run-time libraries.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-12-02 12:44:38 -08:00
|
|
|
#include "sanitizer_allocator.h"
|
2016-09-14 22:00:58 +00:00
|
|
|
#include "sanitizer_allocator_interface.h"
|
[sanitizer] Split Symbolizer/StackTraces from core RTSanitizerCommon
Summary:
Host symbolizer & stacktraces related code in their own RT:
`RTSanitizerCommonSymbolizer`, which is "libcdep" by nature. Symbolizer &
stacktraces specific code that used to live in common files is moved to a new
file `sanitizer_symbolizer_report.cc` as is.
The purpose of this is the enforce a separation between code that relies on
symbolization and code that doesn't. This saves the inclusion of spurious code
due to the interface functions with default visibility, and the extra data
associated.
The following sanitizers makefiles were modified & tested locally:
- dfsan: doesn't require the new symbolizer RT
- esan: requires it
- hwasan: requires it
- lsan: requires it
- msan: requires it
- safestack: doesn't require it
- xray: doesn't require it
- tsan: requires it
- ubsan: requires it
- ubsan_minimal: doesn't require it
- scudo: requires it (but not for Fuchsia that has a minimal runtime)
This was tested locally on Linux, Android, Fuchsia.
Reviewers: alekseyshl, eugenis, dberris, kubamracek, vitalybuka, dvyukov, mcgrathr
Reviewed By: alekseyshl, vitalybuka
Subscribers: srhines, kubamracek, mgorny, krytarowski, delcypher, llvm-commits, #sanitizers
Differential Revision: https://reviews.llvm.org/D45457
llvm-svn: 330131
2018-04-16 16:32:19 +00:00
|
|
|
#include "sanitizer_common.h"
|
2014-02-26 09:06:59 +00:00
|
|
|
#include "sanitizer_flags.h"
|
2022-02-11 16:11:23 +01:00
|
|
|
#include "sanitizer_interface_internal.h"
|
2017-09-13 06:24:59 +00:00
|
|
|
#include "sanitizer_procmaps.h"
|
2021-11-22 21:24:10 -08:00
|
|
|
#include "sanitizer_stackdepot.h"
|
2013-05-17 16:17:19 +00:00
|
|
|
|
2021-12-09 09:55:49 -08:00
|
|
|
namespace __sanitizer {
|
2021-11-22 21:24:10 -08:00
|
|
|
|
2018-12-29 00:32:07 +00:00
|
|
|
#if (SANITIZER_LINUX || SANITIZER_NETBSD) && !SANITIZER_GO
|
2018-04-10 14:41:40 +00:00
|
|
|
// Weak default implementation for when sanitizer_stackdepot is not linked in.
|
2021-09-28 11:20:18 -07:00
|
|
|
SANITIZER_WEAK_ATTRIBUTE StackDepotStats StackDepotGetStats() { return {}; }
|
2018-04-10 14:41:40 +00:00
|
|
|
|
2020-01-23 13:01:08 -08:00
|
|
|
void *BackgroundThread(void *arg) {
|
2021-12-02 00:41:41 -08:00
|
|
|
VPrintf(1, "%s: Started BackgroundThread\n", SanitizerToolName);
|
2018-04-10 14:41:40 +00:00
|
|
|
const uptr hard_rss_limit_mb = common_flags()->hard_rss_limit_mb;
|
|
|
|
const uptr soft_rss_limit_mb = common_flags()->soft_rss_limit_mb;
|
|
|
|
const bool heap_profile = common_flags()->heap_profile;
|
2014-12-16 19:13:01 +00:00
|
|
|
uptr prev_reported_rss = 0;
|
|
|
|
uptr prev_reported_stack_depot_size = 0;
|
2015-01-06 23:53:32 +00:00
|
|
|
bool reached_soft_rss_limit = false;
|
2016-09-14 22:00:58 +00:00
|
|
|
uptr rss_during_last_reported_profile = 0;
|
2014-12-16 19:13:01 +00:00
|
|
|
while (true) {
|
|
|
|
SleepForMillis(100);
|
2018-04-10 14:41:40 +00:00
|
|
|
const uptr current_rss_mb = GetRSS() >> 20;
|
2015-01-20 13:21:20 +00:00
|
|
|
if (Verbosity()) {
|
2014-12-16 19:13:01 +00:00
|
|
|
// If RSS has grown 10% since last time, print some information.
|
|
|
|
if (prev_reported_rss * 11 / 10 < current_rss_mb) {
|
|
|
|
Printf("%s: RSS: %zdMb\n", SanitizerToolName, current_rss_mb);
|
|
|
|
prev_reported_rss = current_rss_mb;
|
|
|
|
}
|
2018-04-09 19:18:50 +00:00
|
|
|
// If stack depot has grown 10% since last time, print it too.
|
2021-09-28 11:20:18 -07:00
|
|
|
StackDepotStats stack_depot_stats = StackDepotGetStats();
|
|
|
|
if (prev_reported_stack_depot_size * 11 / 10 <
|
|
|
|
stack_depot_stats.allocated) {
|
|
|
|
Printf("%s: StackDepot: %zd ids; %zdM allocated\n", SanitizerToolName,
|
|
|
|
stack_depot_stats.n_uniq_ids, stack_depot_stats.allocated >> 20);
|
|
|
|
prev_reported_stack_depot_size = stack_depot_stats.allocated;
|
2014-12-16 19:13:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check RSS against the limit.
|
|
|
|
if (hard_rss_limit_mb && hard_rss_limit_mb < current_rss_mb) {
|
|
|
|
Report("%s: hard rss limit exhausted (%zdMb vs %zdMb)\n",
|
|
|
|
SanitizerToolName, hard_rss_limit_mb, current_rss_mb);
|
|
|
|
DumpProcessMap();
|
|
|
|
Die();
|
|
|
|
}
|
2015-01-06 23:53:32 +00:00
|
|
|
if (soft_rss_limit_mb) {
|
|
|
|
if (soft_rss_limit_mb < current_rss_mb && !reached_soft_rss_limit) {
|
|
|
|
reached_soft_rss_limit = true;
|
|
|
|
Report("%s: soft rss limit exhausted (%zdMb vs %zdMb)\n",
|
|
|
|
SanitizerToolName, soft_rss_limit_mb, current_rss_mb);
|
2021-12-02 12:44:38 -08:00
|
|
|
SetRssLimitExceeded(true);
|
2015-01-06 23:53:32 +00:00
|
|
|
} else if (soft_rss_limit_mb >= current_rss_mb &&
|
|
|
|
reached_soft_rss_limit) {
|
|
|
|
reached_soft_rss_limit = false;
|
2021-12-02 12:44:38 -08:00
|
|
|
SetRssLimitExceeded(false);
|
2015-01-06 23:53:32 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-15 02:11:07 +00:00
|
|
|
if (heap_profile &&
|
2016-09-14 22:00:58 +00:00
|
|
|
current_rss_mb > rss_during_last_reported_profile * 1.1) {
|
|
|
|
Printf("\n\nHEAP PROFILE at RSS %zdMb\n", current_rss_mb);
|
2017-03-15 23:27:14 +00:00
|
|
|
__sanitizer_print_memory_profile(90, 20);
|
2016-09-14 22:00:58 +00:00
|
|
|
rss_during_last_reported_profile = current_rss_mb;
|
|
|
|
}
|
2014-12-16 19:13:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-07 17:59:39 -08:00
|
|
|
void MaybeStartBackgroudThread() {
|
2021-12-02 14:25:30 -08:00
|
|
|
// Need to implement/test on other platforms.
|
2015-01-06 23:53:32 +00:00
|
|
|
// Start the background thread if one of the rss limits is given.
|
|
|
|
if (!common_flags()->hard_rss_limit_mb &&
|
2016-08-26 23:58:42 +00:00
|
|
|
!common_flags()->soft_rss_limit_mb &&
|
2016-09-14 22:00:58 +00:00
|
|
|
!common_flags()->heap_profile) return;
|
2021-12-02 00:41:41 -08:00
|
|
|
if (!&real_pthread_create) {
|
|
|
|
VPrintf(1, "%s: real_pthread_create undefined\n", SanitizerToolName);
|
|
|
|
return; // Can't spawn the thread anyway.
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool started = false;
|
|
|
|
if (!started) {
|
|
|
|
started = true;
|
|
|
|
internal_start_thread(BackgroundThread, nullptr);
|
|
|
|
}
|
2014-12-16 19:13:01 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 11:53:14 -08:00
|
|
|
# if !SANITIZER_START_BACKGROUND_THREAD_IN_ASAN_INTERNAL
|
2022-05-03 10:19:20 +02:00
|
|
|
# ifdef __clang__
|
2021-12-07 17:59:39 -08:00
|
|
|
# pragma clang diagnostic push
|
2021-12-02 14:25:30 -08:00
|
|
|
// We avoid global-constructors to be sure that globals are ready when
|
|
|
|
// sanitizers need them. This can happend before global constructors executed.
|
|
|
|
// Here we don't mind if thread is started on later stages.
|
2021-12-07 17:59:39 -08:00
|
|
|
# pragma clang diagnostic ignored "-Wglobal-constructors"
|
2022-05-03 10:19:20 +02:00
|
|
|
# endif
|
2021-12-02 14:25:30 -08:00
|
|
|
static struct BackgroudThreadStarted {
|
|
|
|
BackgroudThreadStarted() { MaybeStartBackgroudThread(); }
|
|
|
|
} background_thread_strarter UNUSED;
|
2022-05-03 10:19:20 +02:00
|
|
|
# ifdef __clang__
|
2021-12-07 17:59:39 -08:00
|
|
|
# pragma clang diagnostic pop
|
2022-05-03 10:19:20 +02:00
|
|
|
# endif
|
2021-12-07 17:59:39 -08:00
|
|
|
# endif
|
2021-12-08 12:28:12 -08:00
|
|
|
#else
|
|
|
|
void MaybeStartBackgroudThread() {}
|
2021-12-02 14:25:30 -08:00
|
|
|
#endif
|
|
|
|
|
2021-12-02 13:27:11 -08:00
|
|
|
void WriteToSyslog(const char *msg) {
|
|
|
|
InternalScopedString msg_copy;
|
|
|
|
msg_copy.append("%s", msg);
|
|
|
|
const char *p = msg_copy.data();
|
|
|
|
|
|
|
|
// Print one line at a time.
|
|
|
|
// syslog, at least on Android, has an implicit message length limit.
|
|
|
|
while (char* q = internal_strchr(p, '\n')) {
|
|
|
|
*q = '\0';
|
|
|
|
WriteOneLineToSyslog(p);
|
|
|
|
p = q + 1;
|
|
|
|
}
|
|
|
|
// Print remaining characters, if there are any.
|
|
|
|
// Note that this will add an extra newline at the end.
|
|
|
|
// FIXME: buffer extra output. This would need a thread-local buffer, which
|
|
|
|
// on Android requires plugging into the tools (ex. ASan's) Thread class.
|
|
|
|
if (*p)
|
|
|
|
WriteOneLineToSyslog(p);
|
|
|
|
}
|
|
|
|
|
2018-04-03 18:07:22 +00:00
|
|
|
static void (*sandboxing_callback)();
|
|
|
|
void SetSandboxingCallback(void (*f)()) {
|
|
|
|
sandboxing_callback = f;
|
|
|
|
}
|
|
|
|
|
2020-05-08 16:32:33 -07:00
|
|
|
uptr ReservedAddressRange::InitAligned(uptr size, uptr align,
|
|
|
|
const char *name) {
|
|
|
|
CHECK(IsPowerOfTwo(align));
|
|
|
|
if (align <= GetPageSizeCached())
|
|
|
|
return Init(size, name);
|
|
|
|
uptr start = Init(size + align, name);
|
|
|
|
start += align - (start & (align - 1));
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
2021-06-14 17:52:14 -07:00
|
|
|
#if !SANITIZER_FUCHSIA
|
2020-07-17 14:48:28 -07:00
|
|
|
|
[compiler-rt][asan][hwasan] Refactor shadow setup into sanitizer_common (NFCI)
Summary:
This refactors some common support related to shadow memory setup from
asan and hwasan into sanitizer_common. This should not only reduce code
duplication but also make these facilities available for new compiler-rt
uses (e.g. heap profiling).
In most cases the separate copies of the code were either identical, or
at least functionally identical. A few notes:
In ProtectGap, the asan version checked the address against an upper
bound (kZeroBaseMaxShadowStart, which is (2^18). I have created a copy
of kZeroBaseMaxShadowStart in hwasan_mapping.h, with the same value, as
it isn't clear why that code should not do the same check. If it
shouldn't, I can remove this and guard this check so that it only
happens for asan.
In asan's InitializeShadowMemory, in the dynamic shadow case it was
setting __asan_shadow_memory_dynamic_address to 0 (which then sets both
macro SHADOW_OFFSET as well as macro kLowShadowBeg to 0) before calling
FindDynamicShadowStart(). AFAICT this is only needed because
FindDynamicShadowStart utilizes kHighShadowEnd to
get the shadow size, and kHighShadowEnd is a macro invoking
MEM_TO_SHADOW(kHighMemEnd) which in turn invokes:
(((kHighMemEnd) >> SHADOW_SCALE) + (SHADOW_OFFSET))
I.e. it computes the shadow space needed by kHighMemEnd (the shift), and
adds the offset. Since we only want the shadow space here, the earlier
setting of SHADOW_OFFSET to 0 via __asan_shadow_memory_dynamic_address
accomplishes this. In the hwasan version, it simply gets the shadow
space via "MemToShadowSize(kHighMemEnd)", where MemToShadowSize just
does the shift. I've simplified the asan handling to do the same
thing, and therefore was able to remove the setting of the SHADOW_OFFSET
via __asan_shadow_memory_dynamic_address to 0.
Reviewers: vitalybuka, kcc, eugenis
Subscribers: dberris, #sanitizers, llvm-commits, davidxl
Tags: #sanitizers
Differential Revision: https://reviews.llvm.org/D83247
2020-07-06 11:05:12 -07:00
|
|
|
// Reserve memory range [beg, end].
|
|
|
|
// We need to use inclusive range because end+1 may not be representable.
|
|
|
|
void ReserveShadowMemoryRange(uptr beg, uptr end, const char *name,
|
|
|
|
bool madvise_shadow) {
|
|
|
|
CHECK_EQ((beg % GetMmapGranularity()), 0);
|
|
|
|
CHECK_EQ(((end + 1) % GetMmapGranularity()), 0);
|
|
|
|
uptr size = end - beg + 1;
|
|
|
|
DecreaseTotalMmap(size); // Don't count the shadow against mmap_limit_mb.
|
|
|
|
if (madvise_shadow ? !MmapFixedSuperNoReserve(beg, size, name)
|
|
|
|
: !MmapFixedNoReserve(beg, size, name)) {
|
|
|
|
Report(
|
|
|
|
"ReserveShadowMemoryRange failed while trying to map 0x%zx bytes. "
|
|
|
|
"Perhaps you're using ulimit -v\n",
|
|
|
|
size);
|
|
|
|
Abort();
|
|
|
|
}
|
|
|
|
if (madvise_shadow && common_flags()->use_madv_dontdump)
|
|
|
|
DontDumpShadowMemory(beg, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProtectGap(uptr addr, uptr size, uptr zero_base_shadow_start,
|
|
|
|
uptr zero_base_max_shadow_start) {
|
|
|
|
if (!size)
|
|
|
|
return;
|
|
|
|
void *res = MmapFixedNoAccess(addr, size, "shadow gap");
|
|
|
|
if (addr == (uptr)res)
|
|
|
|
return;
|
|
|
|
// A few pages at the start of the address space can not be protected.
|
|
|
|
// But we really want to protect as much as possible, to prevent this memory
|
|
|
|
// being returned as a result of a non-FIXED mmap().
|
|
|
|
if (addr == zero_base_shadow_start) {
|
|
|
|
uptr step = GetMmapGranularity();
|
|
|
|
while (size > step && addr < zero_base_max_shadow_start) {
|
|
|
|
addr += step;
|
|
|
|
size -= step;
|
|
|
|
void *res = MmapFixedNoAccess(addr, size, "shadow gap");
|
|
|
|
if (addr == (uptr)res)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Report(
|
|
|
|
"ERROR: Failed to protect the shadow gap. "
|
|
|
|
"%s cannot proceed correctly. ABORTING.\n",
|
|
|
|
SanitizerToolName);
|
|
|
|
DumpProcessMap();
|
|
|
|
Die();
|
|
|
|
}
|
|
|
|
|
2021-06-14 17:52:14 -07:00
|
|
|
#endif // !SANITIZER_FUCHSIA
|
2020-07-17 14:48:28 -07:00
|
|
|
|
2021-12-20 18:13:27 -08:00
|
|
|
#if !SANITIZER_WINDOWS && !SANITIZER_GO
|
|
|
|
// Weak default implementation for when sanitizer_stackdepot is not linked in.
|
|
|
|
SANITIZER_WEAK_ATTRIBUTE void StackDepotStopBackgroundThread() {}
|
|
|
|
static void StopStackDepotBackgroundThread() {
|
|
|
|
StackDepotStopBackgroundThread();
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
// SANITIZER_WEAK_ATTRIBUTE is unsupported.
|
|
|
|
static void StopStackDepotBackgroundThread() {}
|
|
|
|
#endif
|
|
|
|
|
2013-05-17 16:17:19 +00:00
|
|
|
} // namespace __sanitizer
|
2014-05-27 12:37:52 +00:00
|
|
|
|
2017-01-31 20:23:14 +00:00
|
|
|
SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_sandbox_on_notify,
|
|
|
|
__sanitizer_sandbox_arguments *args) {
|
2021-12-20 18:13:27 -08:00
|
|
|
__sanitizer::StopStackDepotBackgroundThread();
|
2018-04-03 18:07:22 +00:00
|
|
|
__sanitizer::PlatformPrepareForSandboxing(args);
|
2016-09-15 21:02:18 +00:00
|
|
|
if (__sanitizer::sandboxing_callback)
|
|
|
|
__sanitizer::sandboxing_callback();
|
2014-05-27 12:37:52 +00:00
|
|
|
}
|