2012-06-06 14:47:26 +08:00
|
|
|
//===-- sanitizer_common.h --------------------------------------*- C++ -*-===//
|
|
|
|
//
|
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
|
2012-06-06 14:47:26 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2015-02-19 23:15:33 +08:00
|
|
|
// This file is shared between run-time libraries of sanitizers.
|
|
|
|
//
|
2012-06-06 17:26:25 +08:00
|
|
|
// It declares common functions and classes that are used in both runtimes.
|
2012-06-06 14:47:26 +08:00
|
|
|
// Implementation of some functions are provided in sanitizer_common, while
|
|
|
|
// others must be defined by run-time library itself.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SANITIZER_COMMON_H
|
|
|
|
#define SANITIZER_COMMON_H
|
|
|
|
|
2015-01-09 06:03:05 +08:00
|
|
|
#include "sanitizer_flags.h"
|
2015-02-19 23:15:33 +08:00
|
|
|
#include "sanitizer_interface_internal.h"
|
2012-06-06 14:47:26 +08:00
|
|
|
#include "sanitizer_internal_defs.h"
|
2013-02-26 21:30:27 +08:00
|
|
|
#include "sanitizer_libc.h"
|
2015-01-09 06:03:05 +08:00
|
|
|
#include "sanitizer_list.h"
|
2013-04-05 15:30:29 +08:00
|
|
|
#include "sanitizer_mutex.h"
|
2012-06-06 14:47:26 +08:00
|
|
|
|
2016-04-02 01:09:08 +08:00
|
|
|
#if defined(_MSC_VER) && !defined(__clang__)
|
2015-07-02 10:06:59 +08:00
|
|
|
extern "C" void _ReadWriteBarrier();
|
|
|
|
#pragma intrinsic(_ReadWriteBarrier)
|
|
|
|
#endif
|
|
|
|
|
2012-06-06 14:47:26 +08:00
|
|
|
namespace __sanitizer {
|
2017-09-12 08:44:23 +08:00
|
|
|
|
2015-02-27 10:29:25 +08:00
|
|
|
struct AddressInfo;
|
2017-09-15 06:44:03 +08:00
|
|
|
struct BufferedStackTrace;
|
2017-09-12 08:44:23 +08:00
|
|
|
struct SignalContext;
|
|
|
|
struct StackTrace;
|
2012-06-06 14:47:26 +08:00
|
|
|
|
2012-06-06 17:26:25 +08:00
|
|
|
// Constants.
|
2012-11-21 20:38:58 +08:00
|
|
|
const uptr kWordSize = SANITIZER_WORDSIZE / 8;
|
2012-06-06 17:26:25 +08:00
|
|
|
const uptr kWordSizeInBits = 8 * kWordSize;
|
2012-11-24 13:03:11 +08:00
|
|
|
|
2018-03-13 01:18:26 +08:00
|
|
|
const uptr kCacheLineSize = SANITIZER_CACHE_LINE_SIZE;
|
2012-06-06 17:26:25 +08:00
|
|
|
|
2014-11-26 09:48:39 +08:00
|
|
|
const uptr kMaxPathLength = 4096;
|
2013-06-11 16:13:36 +08:00
|
|
|
|
2014-02-04 00:42:29 +08:00
|
|
|
const uptr kMaxThreadStackSize = 1 << 30; // 1Gb
|
|
|
|
|
2015-11-21 02:42:01 +08:00
|
|
|
static const uptr kErrorMessageBufferSize = 1 << 16;
|
|
|
|
|
2015-06-24 21:04:12 +08:00
|
|
|
// Denotes fake PC values that come from JIT/JAVA/etc.
|
2018-03-21 16:44:14 +08:00
|
|
|
// For such PC values __tsan_symbolize_external_ex() will be called.
|
2015-06-24 21:27:56 +08:00
|
|
|
const u64 kExternalPCBit = 1ULL << 60;
|
2015-06-24 21:04:12 +08:00
|
|
|
|
2013-01-31 22:11:21 +08:00
|
|
|
extern const char *SanitizerToolName; // Can be changed by the tool.
|
|
|
|
|
2015-01-20 21:21:20 +08:00
|
|
|
extern atomic_uint32_t current_verbosity;
|
2020-09-17 22:04:50 +08:00
|
|
|
inline void SetVerbosity(int verbosity) {
|
2015-01-20 21:21:20 +08:00
|
|
|
atomic_store(¤t_verbosity, verbosity, memory_order_relaxed);
|
|
|
|
}
|
2020-09-17 22:04:50 +08:00
|
|
|
inline int Verbosity() {
|
2015-01-20 21:21:20 +08:00
|
|
|
return atomic_load(¤t_verbosity, memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
2019-01-12 08:09:24 +08:00
|
|
|
#if SANITIZER_ANDROID
|
2020-09-17 22:04:50 +08:00
|
|
|
inline uptr GetPageSize() {
|
2019-01-12 08:09:24 +08:00
|
|
|
// Android post-M sysconf(_SC_PAGESIZE) crashes if called from .preinit_array.
|
|
|
|
return 4096;
|
|
|
|
}
|
2020-09-17 22:04:50 +08:00
|
|
|
inline uptr GetPageSizeCached() {
|
2019-01-12 08:09:24 +08:00
|
|
|
return 4096;
|
|
|
|
}
|
|
|
|
#else
|
2012-11-23 23:38:49 +08:00
|
|
|
uptr GetPageSize();
|
2016-06-27 23:32:18 +08:00
|
|
|
extern uptr PageSizeCached;
|
2020-09-17 22:04:50 +08:00
|
|
|
inline uptr GetPageSizeCached() {
|
2016-06-27 23:32:18 +08:00
|
|
|
if (!PageSizeCached)
|
|
|
|
PageSizeCached = GetPageSize();
|
|
|
|
return PageSizeCached;
|
|
|
|
}
|
2019-01-12 08:09:24 +08:00
|
|
|
#endif
|
2012-11-23 23:38:49 +08:00
|
|
|
uptr GetMmapGranularity();
|
2017-11-21 01:41:57 +08:00
|
|
|
uptr GetMaxVirtualAddress();
|
2017-11-08 07:51:22 +08:00
|
|
|
uptr GetMaxUserVirtualAddress();
|
2012-06-07 15:13:46 +08:00
|
|
|
// Threads
|
2017-04-18 02:17:38 +08:00
|
|
|
tid_t GetTid();
|
2018-08-15 04:28:58 +08:00
|
|
|
int TgKill(pid_t pid, tid_t tid, int sig);
|
2012-06-15 14:37:34 +08:00
|
|
|
uptr GetThreadSelf();
|
2012-06-07 15:32:00 +08:00
|
|
|
void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
|
2012-06-07 15:13:46 +08:00
|
|
|
uptr *stack_bottom);
|
2013-05-07 22:41:43 +08:00
|
|
|
void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
|
|
|
|
uptr *tls_addr, uptr *tls_size);
|
2012-06-07 00:15:07 +08:00
|
|
|
|
|
|
|
// Memory management
|
2015-11-21 02:42:01 +08:00
|
|
|
void *MmapOrDie(uptr size, const char *mem_type, bool raw_report = false);
|
2020-09-17 22:04:50 +08:00
|
|
|
inline void *MmapOrDieQuietly(uptr size, const char *mem_type) {
|
2015-11-21 02:42:01 +08:00
|
|
|
return MmapOrDie(size, mem_type, /*raw_report*/ true);
|
|
|
|
}
|
2012-06-06 17:26:25 +08:00
|
|
|
void UnmapOrDie(void *addr, uptr size);
|
2017-06-17 02:48:08 +08:00
|
|
|
// Behaves just like MmapOrDie, but tolerates out of memory condition, in that
|
|
|
|
// case returns nullptr.
|
|
|
|
void *MmapOrDieOnFatalError(uptr size, const char *mem_type);
|
2018-07-20 16:33:41 +08:00
|
|
|
bool MmapFixedNoReserve(uptr fixed_addr, uptr size, const char *name = nullptr)
|
|
|
|
WARN_UNUSED_RESULT;
|
2019-08-27 12:24:19 +08:00
|
|
|
bool MmapFixedSuperNoReserve(uptr fixed_addr, uptr size,
|
2019-08-27 14:59:57 +08:00
|
|
|
const char *name = nullptr) WARN_UNUSED_RESULT;
|
2013-12-13 23:03:49 +08:00
|
|
|
void *MmapNoReserveOrDie(uptr size, const char *mem_type);
|
2019-02-06 09:14:50 +08:00
|
|
|
void *MmapFixedOrDie(uptr fixed_addr, uptr size, const char *name = nullptr);
|
2017-06-27 06:54:10 +08:00
|
|
|
// Behaves just like MmapFixedOrDie, but tolerates out of memory condition, in
|
|
|
|
// that case returns nullptr.
|
2019-02-06 09:14:50 +08:00
|
|
|
void *MmapFixedOrDieOnFatalError(uptr fixed_addr, uptr size,
|
|
|
|
const char *name = nullptr);
|
2016-04-23 07:46:53 +08:00
|
|
|
void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name = nullptr);
|
2020-07-17 15:08:45 +08:00
|
|
|
void *MmapNoAccess(uptr size);
|
2012-12-06 14:10:31 +08:00
|
|
|
// Map aligned chunk of address space; size and alignment are powers of two.
|
2017-06-22 08:02:37 +08:00
|
|
|
// Dies on all but out of memory errors, in the latter case returns nullptr.
|
|
|
|
void *MmapAlignedOrDieOnFatalError(uptr size, uptr alignment,
|
|
|
|
const char *mem_type);
|
2016-04-23 07:46:53 +08:00
|
|
|
// Disallow access to a memory range. Use MmapFixedNoAccess to allocate an
|
2015-04-10 23:02:19 +08:00
|
|
|
// unaccessible memory.
|
|
|
|
bool MprotectNoAccess(uptr addr, uptr size);
|
2016-01-27 04:53:09 +08:00
|
|
|
bool MprotectReadOnly(uptr addr, uptr size);
|
2015-04-10 23:02:19 +08:00
|
|
|
|
2018-03-24 15:45:24 +08:00
|
|
|
void MprotectMallocZones(void *addr, int prot);
|
|
|
|
|
2020-07-17 05:42:00 +08:00
|
|
|
#if SANITIZER_LINUX
|
|
|
|
// Unmap memory. Currently only used on Linux.
|
|
|
|
void UnmapFromTo(uptr from, uptr to);
|
|
|
|
#endif
|
[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-07 02:05:12 +08:00
|
|
|
|
|
|
|
// Maps shadow_size_bytes of shadow memory and returns shadow address. It will
|
|
|
|
// be aligned to the mmap granularity * 2^shadow_scale, or to
|
|
|
|
// 2^min_shadow_base_alignment if that is larger. The returned address will
|
|
|
|
// have max(2^min_shadow_base_alignment, mmap granularity) on the left, and
|
2020-08-08 06:52:50 +08:00
|
|
|
// shadow_size_bytes bytes on the right, which on linux is mapped no access.
|
[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-07 02:05:12 +08:00
|
|
|
// The high_mem_end may be updated if the original shadow size doesn't fit.
|
|
|
|
uptr MapDynamicShadow(uptr shadow_size_bytes, uptr shadow_scale,
|
|
|
|
uptr min_shadow_base_alignment, uptr &high_mem_end);
|
|
|
|
|
|
|
|
// Reserve memory range [beg, end]. If madvise_shadow is true then apply
|
|
|
|
// madvise (e.g. hugepages, core dumping) requested by options.
|
|
|
|
void ReserveShadowMemoryRange(uptr beg, uptr end, const char *name,
|
|
|
|
bool madvise_shadow = true);
|
|
|
|
|
|
|
|
// Protect size bytes of memory starting at addr. Also try to protect
|
|
|
|
// several pages at the start of the address space as specified by
|
|
|
|
// zero_base_shadow_start, at most up to the size or zero_base_max_shadow_start.
|
|
|
|
void ProtectGap(uptr addr, uptr size, uptr zero_base_shadow_start,
|
|
|
|
uptr zero_base_max_shadow_start);
|
|
|
|
|
2016-10-01 01:47:34 +08:00
|
|
|
// Find an available address space.
|
2017-07-13 07:29:21 +08:00
|
|
|
uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding,
|
2018-02-27 02:33:21 +08:00
|
|
|
uptr *largest_gap_found, uptr *max_occupied_addr);
|
2016-10-01 01:47:34 +08:00
|
|
|
|
2012-06-15 15:29:14 +08:00
|
|
|
// Used to check if we can map shadow memory to a fixed location.
|
|
|
|
bool MemoryRangeIsAvailable(uptr range_start, uptr range_end);
|
2016-12-01 04:41:59 +08:00
|
|
|
// Releases memory pages entirely within the [beg, end] address range. Noop if
|
|
|
|
// the provided range does not contain at least one entire page.
|
|
|
|
void ReleaseMemoryPagesToOS(uptr beg, uptr end);
|
2014-04-14 22:51:01 +08:00
|
|
|
void IncreaseTotalMmap(uptr size);
|
|
|
|
void DecreaseTotalMmap(uptr size);
|
2014-12-09 09:22:59 +08:00
|
|
|
uptr GetRSS();
|
2019-08-07 05:30:03 +08:00
|
|
|
void SetShadowRegionHugePageMode(uptr addr, uptr length);
|
2018-06-14 01:18:41 +08:00
|
|
|
bool DontDumpShadowMemory(uptr addr, uptr length);
|
2015-09-11 21:55:00 +08:00
|
|
|
// Check if the built VMA size matches the runtime one.
|
|
|
|
void CheckVMASize();
|
2016-06-17 04:06:06 +08:00
|
|
|
void RunMallocHooks(const void *ptr, uptr size);
|
|
|
|
void RunFreeHooks(const void *ptr);
|
2012-06-14 22:42:58 +08:00
|
|
|
|
Introduce ReservedAddressRange to sanitizer_common.
Summary:
Fixed version of https://reviews.llvm.org/D38437 (fixes Win/Fuchsia failures).
Creating a new revision, since the old one was getting a bit old/crowded.
In Fuchsia, MmapNoAccess/MmapFixedOrDie are implemented using a global
VMAR, which means that MmapNoAccess can only be called once. This works
for the sanitizer allocator but *not* for the Scudo allocator.
Hence, this changeset introduces a new ReservedAddressRange object to
serve as the new API for these calls. In this changeset, the object
still calls into the old Mmap implementations.
The next changeset two changesets will convert the sanitizer and scudo
allocators to use the new APIs, respectively. (ReservedAddressRange will
replace the SecondaryHeader in Scudo.)
Finally, a last changeset will update the Fuchsia implementation.
Reviewers: alekseyshl, cryptoad, phosek
Reviewed By: alekseyshl, cryptoad
Subscribers: kubamracek
Differential Revision: https://reviews.llvm.org/D39072
llvm-svn: 316934
2017-10-31 01:56:24 +08:00
|
|
|
class ReservedAddressRange {
|
|
|
|
public:
|
|
|
|
uptr Init(uptr size, const char *name = nullptr, uptr fixed_addr = 0);
|
2020-05-09 07:32:33 +08:00
|
|
|
uptr InitAligned(uptr size, uptr align, const char *name = nullptr);
|
2019-02-06 09:14:50 +08:00
|
|
|
uptr Map(uptr fixed_addr, uptr size, const char *name = nullptr);
|
|
|
|
uptr MapOrDie(uptr fixed_addr, uptr size, const char *name = nullptr);
|
Introduce ReservedAddressRange to sanitizer_common.
Summary:
Fixed version of https://reviews.llvm.org/D38437 (fixes Win/Fuchsia failures).
Creating a new revision, since the old one was getting a bit old/crowded.
In Fuchsia, MmapNoAccess/MmapFixedOrDie are implemented using a global
VMAR, which means that MmapNoAccess can only be called once. This works
for the sanitizer allocator but *not* for the Scudo allocator.
Hence, this changeset introduces a new ReservedAddressRange object to
serve as the new API for these calls. In this changeset, the object
still calls into the old Mmap implementations.
The next changeset two changesets will convert the sanitizer and scudo
allocators to use the new APIs, respectively. (ReservedAddressRange will
replace the SecondaryHeader in Scudo.)
Finally, a last changeset will update the Fuchsia implementation.
Reviewers: alekseyshl, cryptoad, phosek
Reviewed By: alekseyshl, cryptoad
Subscribers: kubamracek
Differential Revision: https://reviews.llvm.org/D39072
llvm-svn: 316934
2017-10-31 01:56:24 +08:00
|
|
|
void Unmap(uptr addr, uptr size);
|
|
|
|
void *base() const { return base_; }
|
|
|
|
uptr size() const { return size_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void* base_;
|
|
|
|
uptr size_;
|
|
|
|
const char* name_;
|
2017-11-28 03:53:53 +08:00
|
|
|
uptr os_handle_;
|
Introduce ReservedAddressRange to sanitizer_common.
Summary:
Fixed version of https://reviews.llvm.org/D38437 (fixes Win/Fuchsia failures).
Creating a new revision, since the old one was getting a bit old/crowded.
In Fuchsia, MmapNoAccess/MmapFixedOrDie are implemented using a global
VMAR, which means that MmapNoAccess can only be called once. This works
for the sanitizer allocator but *not* for the Scudo allocator.
Hence, this changeset introduces a new ReservedAddressRange object to
serve as the new API for these calls. In this changeset, the object
still calls into the old Mmap implementations.
The next changeset two changesets will convert the sanitizer and scudo
allocators to use the new APIs, respectively. (ReservedAddressRange will
replace the SecondaryHeader in Scudo.)
Finally, a last changeset will update the Fuchsia implementation.
Reviewers: alekseyshl, cryptoad, phosek
Reviewed By: alekseyshl, cryptoad
Subscribers: kubamracek
Differential Revision: https://reviews.llvm.org/D39072
llvm-svn: 316934
2017-10-31 01:56:24 +08:00
|
|
|
};
|
|
|
|
|
2017-09-26 04:48:51 +08:00
|
|
|
typedef void (*fill_profile_f)(uptr start, uptr rss, bool file,
|
|
|
|
/*out*/uptr *stats, uptr stats_size);
|
|
|
|
|
|
|
|
// Parse the contents of /proc/self/smaps and generate a memory profile.
|
|
|
|
// |cb| is a tool-specific callback that fills the |stats| array containing
|
|
|
|
// |stats_size| elements.
|
|
|
|
void GetMemoryProfile(fill_profile_f cb, uptr *stats, uptr stats_size);
|
|
|
|
|
2012-08-27 22:51:36 +08:00
|
|
|
// Simple low-level (mmap-based) allocator for internal use. Doesn't have
|
|
|
|
// constructor, so all instances of LowLevelAllocator should be
|
|
|
|
// linker initialized.
|
2012-08-27 17:30:58 +08:00
|
|
|
class LowLevelAllocator {
|
|
|
|
public:
|
2012-08-27 22:51:36 +08:00
|
|
|
// Requires an external lock.
|
2012-08-27 17:30:58 +08:00
|
|
|
void *Allocate(uptr size);
|
|
|
|
private:
|
|
|
|
char *allocated_end_;
|
|
|
|
char *allocated_current_;
|
|
|
|
};
|
2017-11-21 09:01:32 +08:00
|
|
|
// Set the min alignment of LowLevelAllocator to at least alignment.
|
|
|
|
void SetLowLevelAllocateMinAlignment(uptr alignment);
|
2012-08-27 17:30:58 +08:00
|
|
|
typedef void (*LowLevelAllocateCallback)(uptr ptr, uptr size);
|
|
|
|
// Allows to register tool-specific callbacks for LowLevelAllocator.
|
|
|
|
// Passing NULL removes the callback.
|
|
|
|
void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback);
|
|
|
|
|
2012-06-14 22:42:58 +08:00
|
|
|
// IO
|
2017-07-22 09:46:40 +08:00
|
|
|
void CatastrophicErrorWrite(const char *buffer, uptr length);
|
2012-06-07 15:13:46 +08:00
|
|
|
void RawWrite(const char *buffer);
|
2014-02-26 17:06:59 +08:00
|
|
|
bool ColorizeReports();
|
Reapply: [asan] On OS X, log reports to syslog and os_trace
When ASan currently detects a bug, by default it will only print out the text
of the report to stderr. This patch changes this behavior and writes the full
text of the report to syslog before we terminate the process. It also calls
os_trace (Activity Tracing available on OS X and iOS) with a message saying
that the report is available in syslog. This is useful, because this message
will be shown in the crash log.
For this to work, the patch makes sure we store the full report into
error_message_buffer unconditionally, and it also strips out ANSI escape
sequences from the report (they are used when producing colored reports).
I've initially tried to log to syslog during printing, which is done on Android
right now. The advantage is that if we crash during error reporting or the
produced error does not go through ScopedInErrorReport, we would still get a
(partial) message in the syslog. However, that solution is very problematic on
OS X. One issue is that the logging routine uses GCD, which may spawn a new
thread on its behalf. In many cases, the reporting logic locks threadRegistry,
which leads to deadlocks.
Reviewed at http://reviews.llvm.org/D13452
(In addition, add sanitizer_common_libcdep.cc to buildgo.sh to avoid
build failures on Linux.)
llvm-svn: 253688
2015-11-21 02:41:44 +08:00
|
|
|
void RemoveANSIEscapeSequencesFromString(char *buffer);
|
2012-06-06 21:58:39 +08:00
|
|
|
void Printf(const char *format, ...);
|
|
|
|
void Report(const char *format, ...);
|
2012-08-28 19:34:40 +08:00
|
|
|
void SetPrintfAndReportCallback(void (*callback)(const char *));
|
2013-12-05 20:04:51 +08:00
|
|
|
#define VReport(level, ...) \
|
|
|
|
do { \
|
2015-01-20 21:21:20 +08:00
|
|
|
if ((uptr)Verbosity() >= (level)) Report(__VA_ARGS__); \
|
2013-12-05 20:04:51 +08:00
|
|
|
} while (0)
|
|
|
|
#define VPrintf(level, ...) \
|
|
|
|
do { \
|
2015-01-20 21:21:20 +08:00
|
|
|
if ((uptr)Verbosity() >= (level)) Printf(__VA_ARGS__); \
|
2013-12-05 20:04:51 +08:00
|
|
|
} while (0)
|
2013-11-14 17:41:24 +08:00
|
|
|
|
2017-09-23 02:32:05 +08:00
|
|
|
// Lock sanitizer error reporting and protects against nested errors.
|
|
|
|
class ScopedErrorReportLock {
|
|
|
|
public:
|
2017-09-23 06:36:21 +08:00
|
|
|
ScopedErrorReportLock();
|
2017-09-23 02:32:05 +08:00
|
|
|
~ScopedErrorReportLock();
|
2017-09-23 10:47:21 +08:00
|
|
|
|
|
|
|
static void CheckLocked();
|
2017-09-23 02:32:05 +08:00
|
|
|
};
|
|
|
|
|
2013-12-04 22:37:01 +08:00
|
|
|
extern uptr stoptheworld_tracer_pid;
|
|
|
|
extern uptr stoptheworld_tracer_ppid;
|
2012-06-06 21:11:29 +08:00
|
|
|
|
2014-09-18 01:56:15 +08:00
|
|
|
bool IsAccessibleMemoryRange(uptr beg, uptr size);
|
|
|
|
|
2013-10-04 16:55:03 +08:00
|
|
|
// Error report formatting.
|
|
|
|
const char *StripPathPrefix(const char *filepath,
|
|
|
|
const char *strip_file_prefix);
|
2014-11-05 03:34:29 +08:00
|
|
|
// Strip the directories from the module name.
|
|
|
|
const char *StripModuleName(const char *module);
|
2013-10-04 16:55:03 +08:00
|
|
|
|
2012-09-17 17:12:39 +08:00
|
|
|
// OS
|
2015-06-04 15:29:43 +08:00
|
|
|
uptr ReadBinaryName(/*out*/char *buf, uptr buf_len);
|
2015-06-29 23:58:16 +08:00
|
|
|
uptr ReadBinaryNameCached(/*out*/char *buf, uptr buf_len);
|
2021-01-13 07:06:19 +08:00
|
|
|
uptr ReadBinaryDir(/*out*/ char *buf, uptr buf_len);
|
2015-07-29 05:01:42 +08:00
|
|
|
uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len);
|
2015-07-29 04:27:51 +08:00
|
|
|
const char *GetProcessName();
|
|
|
|
void UpdateProcessName();
|
2015-06-04 15:29:43 +08:00
|
|
|
void CacheBinaryName();
|
2014-08-13 06:31:19 +08:00
|
|
|
void DisableCoreDumperIfNecessary();
|
2012-09-17 17:12:39 +08:00
|
|
|
void DumpProcessMap();
|
2012-06-14 22:07:21 +08:00
|
|
|
const char *GetEnv(const char *name);
|
2015-01-31 09:27:18 +08:00
|
|
|
bool SetEnv(const char *name, const char *value);
|
2015-02-27 11:12:19 +08:00
|
|
|
|
2013-02-18 15:17:12 +08:00
|
|
|
u32 GetUid();
|
2012-09-17 17:12:39 +08:00
|
|
|
void ReExec();
|
2018-06-05 15:29:23 +08:00
|
|
|
void CheckASLR();
|
2018-12-23 23:09:28 +08:00
|
|
|
void CheckMPROTECT();
|
2016-01-18 15:55:12 +08:00
|
|
|
char **GetArgv();
|
2018-11-07 03:23:12 +08:00
|
|
|
char **GetEnviron();
|
2016-01-18 15:55:12 +08:00
|
|
|
void PrintCmdline();
|
2012-09-17 17:12:39 +08:00
|
|
|
bool StackSizeIsUnlimited();
|
|
|
|
void SetStackSizeLimitInBytes(uptr limit);
|
2014-08-13 06:31:19 +08:00
|
|
|
bool AddressSpaceIsUnlimited();
|
|
|
|
void SetAddressSpaceUnlimited();
|
2014-02-24 16:53:26 +08:00
|
|
|
void AdjustStackSize(void *attr);
|
2018-04-04 02:07:22 +08:00
|
|
|
void PlatformPrepareForSandboxing(__sanitizer_sandbox_arguments *args);
|
2014-04-24 21:09:17 +08:00
|
|
|
void SetSandboxingCallback(void (*f)());
|
2012-06-07 13:38:26 +08:00
|
|
|
|
2014-12-26 20:32:32 +08:00
|
|
|
void InitializeCoverage(bool enabled, const char *coverage_dir);
|
|
|
|
|
2013-03-13 16:19:53 +08:00
|
|
|
void InitTlsSize();
|
|
|
|
uptr GetTlsSize();
|
|
|
|
|
2012-06-15 14:08:19 +08:00
|
|
|
// Other
|
2012-06-15 14:37:34 +08:00
|
|
|
void SleepForSeconds(int seconds);
|
2012-06-18 16:44:30 +08:00
|
|
|
void SleepForMillis(int millis);
|
2013-03-21 14:24:31 +08:00
|
|
|
u64 NanoTime();
|
2017-12-14 00:23:54 +08:00
|
|
|
u64 MonotonicNanoTime();
|
2012-06-15 14:37:34 +08:00
|
|
|
int Atexit(void (*function)(void));
|
2015-02-20 06:56:47 +08:00
|
|
|
bool TemplateMatch(const char *templ, const char *str);
|
2012-06-15 14:08:19 +08:00
|
|
|
|
2012-09-11 17:44:48 +08:00
|
|
|
// Exit
|
|
|
|
void NORETURN Abort();
|
|
|
|
void NORETURN Die();
|
2013-08-13 19:42:45 +08:00
|
|
|
void NORETURN
|
2012-09-11 17:44:48 +08:00
|
|
|
CheckFailed(const char *file, int line, const char *cond, u64 v1, u64 v2);
|
2015-08-13 07:55:38 +08:00
|
|
|
void NORETURN ReportMmapFailureAndDie(uptr size, const char *mem_type,
|
2015-11-21 02:42:01 +08:00
|
|
|
const char *mmap_type, error_t err,
|
|
|
|
bool raw_report = false);
|
2012-09-11 17:44:48 +08:00
|
|
|
|
|
|
|
// Specific tools may override behavior of "Die" and "CheckFailed" functions
|
|
|
|
// to do tool-specific job.
|
2013-08-26 21:20:31 +08:00
|
|
|
typedef void (*DieCallbackType)(void);
|
2015-08-25 06:21:44 +08:00
|
|
|
|
|
|
|
// It's possible to add several callbacks that would be run when "Die" is
|
|
|
|
// called. The callbacks will be run in the opposite order. The tools are
|
|
|
|
// strongly recommended to setup all callbacks during initialization, when there
|
|
|
|
// is only a single thread.
|
|
|
|
bool AddDieCallback(DieCallbackType callback);
|
|
|
|
bool RemoveDieCallback(DieCallbackType callback);
|
|
|
|
|
|
|
|
void SetUserDieCallback(DieCallbackType callback);
|
|
|
|
|
2012-09-11 17:44:48 +08:00
|
|
|
typedef void (*CheckFailedCallbackType)(const char *, int, const char *,
|
|
|
|
u64, u64);
|
|
|
|
void SetCheckFailedCallback(CheckFailedCallbackType callback);
|
|
|
|
|
2015-01-07 07:53:32 +08:00
|
|
|
// Callback will be called if soft_rss_limit_mb is given and the limit is
|
|
|
|
// exceeded (exceeded==true) or if rss went down below the limit
|
|
|
|
// (exceeded==false).
|
|
|
|
// The callback should be registered once at the tool init time.
|
|
|
|
void SetSoftRssLimitExceededCallback(void (*Callback)(bool exceeded));
|
|
|
|
|
2014-01-28 19:12:29 +08:00
|
|
|
// Functions related to signal handling.
|
2014-01-31 21:10:07 +08:00
|
|
|
typedef void (*SignalHandlerType)(int, void *, void *);
|
2017-05-26 07:42:33 +08:00
|
|
|
HandleSignalMode GetHandleSignalMode(int signum);
|
2014-01-31 21:10:07 +08:00
|
|
|
void InstallDeadlySignalHandlers(SignalHandlerType handler);
|
2017-09-23 06:57:48 +08:00
|
|
|
|
2017-09-13 12:46:37 +08:00
|
|
|
// Signal reporting.
|
2017-09-15 06:44:03 +08:00
|
|
|
// Each sanitizer uses slightly different implementation of stack unwinding.
|
|
|
|
typedef void (*UnwindSignalStackCallbackType)(const SignalContext &sig,
|
|
|
|
const void *callback_context,
|
|
|
|
BufferedStackTrace *stack);
|
2017-09-23 06:57:48 +08:00
|
|
|
// Print deadly signal report and die.
|
|
|
|
void HandleDeadlySignal(void *siginfo, void *context, u32 tid,
|
|
|
|
UnwindSignalStackCallbackType unwind,
|
|
|
|
const void *unwind_context);
|
|
|
|
|
|
|
|
// Part of HandleDeadlySignal, exposed for asan.
|
|
|
|
void StartReportDeadlySignal();
|
|
|
|
// Part of HandleDeadlySignal, exposed for asan.
|
2017-09-15 06:44:03 +08:00
|
|
|
void ReportDeadlySignal(const SignalContext &sig, u32 tid,
|
|
|
|
UnwindSignalStackCallbackType unwind,
|
|
|
|
const void *unwind_context);
|
2017-09-23 06:57:48 +08:00
|
|
|
|
2014-01-31 21:10:07 +08:00
|
|
|
// Alternative signal stack (POSIX-only).
|
2014-01-28 19:12:29 +08:00
|
|
|
void SetAlternateSignalStack();
|
|
|
|
void UnsetAlternateSignalStack();
|
|
|
|
|
2013-11-02 01:02:14 +08:00
|
|
|
// We don't want a summary too long.
|
|
|
|
const int kMaxSummaryLength = 1024;
|
|
|
|
// Construct a one-line string:
|
|
|
|
// SUMMARY: SanitizerToolName: error_message
|
|
|
|
// and pass it to __sanitizer_report_error_summary.
|
2017-04-15 02:24:35 +08:00
|
|
|
// If alt_tool_name is provided, it's used in place of SanitizerToolName.
|
|
|
|
void ReportErrorSummary(const char *error_message,
|
|
|
|
const char *alt_tool_name = nullptr);
|
2013-11-02 01:02:14 +08:00
|
|
|
// Same as above, but construct error_message as:
|
2015-02-27 10:29:25 +08:00
|
|
|
// error_type file:line[:column][ function]
|
2017-04-15 02:24:35 +08:00
|
|
|
void ReportErrorSummary(const char *error_type, const AddressInfo &info,
|
|
|
|
const char *alt_tool_name = nullptr);
|
2015-02-27 10:29:25 +08:00
|
|
|
// Same as above, but obtains AddressInfo by symbolizing top stack trace frame.
|
2017-04-15 02:24:35 +08:00
|
|
|
void ReportErrorSummary(const char *error_type, const StackTrace *trace,
|
|
|
|
const char *alt_tool_name = nullptr);
|
2013-02-06 20:36:49 +08:00
|
|
|
|
2018-03-24 15:45:24 +08:00
|
|
|
void ReportMmapWriteExec(int prot);
|
2018-03-22 05:25:07 +08:00
|
|
|
|
2012-06-15 15:00:31 +08:00
|
|
|
// Math
|
2013-06-10 18:02:02 +08:00
|
|
|
#if SANITIZER_WINDOWS && !defined(__clang__) && !defined(__GNUC__)
|
2013-02-08 19:45:04 +08:00
|
|
|
extern "C" {
|
2019-09-12 07:19:48 +08:00
|
|
|
unsigned char _BitScanForward(unsigned long *index, unsigned long mask);
|
|
|
|
unsigned char _BitScanReverse(unsigned long *index, unsigned long mask);
|
2013-02-08 19:45:04 +08:00
|
|
|
#if defined(_WIN64)
|
2019-09-12 07:19:48 +08:00
|
|
|
unsigned char _BitScanForward64(unsigned long *index, unsigned __int64 mask);
|
|
|
|
unsigned char _BitScanReverse64(unsigned long *index, unsigned __int64 mask);
|
2013-02-08 19:45:04 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-09-17 22:04:50 +08:00
|
|
|
inline uptr MostSignificantSetBitIndex(uptr x) {
|
2013-02-26 20:59:06 +08:00
|
|
|
CHECK_NE(x, 0U);
|
2019-09-12 07:19:48 +08:00
|
|
|
unsigned long up;
|
2013-06-10 18:02:02 +08:00
|
|
|
#if !SANITIZER_WINDOWS || defined(__clang__) || defined(__GNUC__)
|
2015-07-13 08:26:03 +08:00
|
|
|
# ifdef _WIN64
|
|
|
|
up = SANITIZER_WORDSIZE - 1 - __builtin_clzll(x);
|
|
|
|
# else
|
2013-02-08 19:45:04 +08:00
|
|
|
up = SANITIZER_WORDSIZE - 1 - __builtin_clzl(x);
|
2015-07-13 08:26:03 +08:00
|
|
|
# endif
|
2013-02-08 19:45:04 +08:00
|
|
|
#elif defined(_WIN64)
|
|
|
|
_BitScanReverse64(&up, x);
|
|
|
|
#else
|
|
|
|
_BitScanReverse(&up, x);
|
|
|
|
#endif
|
|
|
|
return up;
|
|
|
|
}
|
|
|
|
|
2020-09-17 22:04:50 +08:00
|
|
|
inline uptr LeastSignificantSetBitIndex(uptr x) {
|
2014-02-13 23:59:00 +08:00
|
|
|
CHECK_NE(x, 0U);
|
2019-09-12 07:19:48 +08:00
|
|
|
unsigned long up;
|
2014-02-13 23:59:00 +08:00
|
|
|
#if !SANITIZER_WINDOWS || defined(__clang__) || defined(__GNUC__)
|
2015-07-13 08:26:03 +08:00
|
|
|
# ifdef _WIN64
|
|
|
|
up = __builtin_ctzll(x);
|
|
|
|
# else
|
2014-02-13 23:59:00 +08:00
|
|
|
up = __builtin_ctzl(x);
|
2015-07-13 08:26:03 +08:00
|
|
|
# endif
|
2014-02-13 23:59:00 +08:00
|
|
|
#elif defined(_WIN64)
|
|
|
|
_BitScanForward64(&up, x);
|
|
|
|
#else
|
|
|
|
_BitScanForward(&up, x);
|
|
|
|
#endif
|
|
|
|
return up;
|
|
|
|
}
|
|
|
|
|
2020-09-17 22:04:50 +08:00
|
|
|
inline bool IsPowerOfTwo(uptr x) {
|
2012-06-06 17:26:25 +08:00
|
|
|
return (x & (x - 1)) == 0;
|
|
|
|
}
|
2013-02-08 19:45:04 +08:00
|
|
|
|
2020-09-17 22:04:50 +08:00
|
|
|
inline uptr RoundUpToPowerOfTwo(uptr size) {
|
2013-02-08 19:45:04 +08:00
|
|
|
CHECK(size);
|
|
|
|
if (IsPowerOfTwo(size)) return size;
|
|
|
|
|
|
|
|
uptr up = MostSignificantSetBitIndex(size);
|
2016-09-13 04:39:13 +08:00
|
|
|
CHECK_LT(size, (1ULL << (up + 1)));
|
|
|
|
CHECK_GT(size, (1ULL << up));
|
2015-07-02 09:44:34 +08:00
|
|
|
return 1ULL << (up + 1);
|
2013-02-08 19:45:04 +08:00
|
|
|
}
|
|
|
|
|
2020-09-17 22:04:50 +08:00
|
|
|
inline uptr RoundUpTo(uptr size, uptr boundary) {
|
2015-11-21 02:42:01 +08:00
|
|
|
RAW_CHECK(IsPowerOfTwo(boundary));
|
2012-06-06 17:26:25 +08:00
|
|
|
return (size + boundary - 1) & ~(boundary - 1);
|
|
|
|
}
|
2013-02-08 19:45:04 +08:00
|
|
|
|
2020-09-17 22:04:50 +08:00
|
|
|
inline uptr RoundDownTo(uptr x, uptr boundary) {
|
2012-12-14 20:15:09 +08:00
|
|
|
return x & ~(boundary - 1);
|
|
|
|
}
|
2013-02-08 19:45:04 +08:00
|
|
|
|
2020-09-17 22:04:50 +08:00
|
|
|
inline bool IsAligned(uptr a, uptr alignment) {
|
2012-12-11 22:41:31 +08:00
|
|
|
return (a & (alignment - 1)) == 0;
|
|
|
|
}
|
2013-02-08 19:45:04 +08:00
|
|
|
|
2020-09-17 22:04:50 +08:00
|
|
|
inline uptr Log2(uptr x) {
|
2013-02-08 19:45:04 +08:00
|
|
|
CHECK(IsPowerOfTwo(x));
|
2015-07-13 08:26:03 +08:00
|
|
|
return LeastSignificantSetBitIndex(x);
|
2013-02-08 19:45:04 +08:00
|
|
|
}
|
|
|
|
|
2012-07-16 19:27:17 +08:00
|
|
|
// Don't use std::min, std::max or std::swap, to minimize dependency
|
|
|
|
// on libstdc++.
|
2012-06-15 15:00:31 +08:00
|
|
|
template<class T> T Min(T a, T b) { return a < b ? a : b; }
|
|
|
|
template<class T> T Max(T a, T b) { return a > b ? a : b; }
|
2012-07-16 19:27:17 +08:00
|
|
|
template<class T> void Swap(T& a, T& b) {
|
|
|
|
T tmp = a;
|
|
|
|
a = b;
|
|
|
|
b = tmp;
|
|
|
|
}
|
2012-06-06 14:47:26 +08:00
|
|
|
|
2012-06-15 21:09:52 +08:00
|
|
|
// Char handling
|
2020-09-17 22:04:50 +08:00
|
|
|
inline bool IsSpace(int c) {
|
2012-06-15 21:09:52 +08:00
|
|
|
return (c == ' ') || (c == '\n') || (c == '\t') ||
|
|
|
|
(c == '\f') || (c == '\r') || (c == '\v');
|
|
|
|
}
|
2020-09-17 22:04:50 +08:00
|
|
|
inline bool IsDigit(int c) {
|
2012-06-15 21:09:52 +08:00
|
|
|
return (c >= '0') && (c <= '9');
|
|
|
|
}
|
2020-09-17 22:04:50 +08:00
|
|
|
inline int ToLower(int c) {
|
2012-06-15 21:09:52 +08:00
|
|
|
return (c >= 'A' && c <= 'Z') ? (c + 'a' - 'A') : c;
|
|
|
|
}
|
|
|
|
|
2013-02-26 21:30:27 +08:00
|
|
|
// A low-level vector based on mmap. May incur a significant memory overhead for
|
|
|
|
// small vectors.
|
|
|
|
// WARNING: The current implementation supports only POD types.
|
|
|
|
template<typename T>
|
2014-12-31 07:16:12 +08:00
|
|
|
class InternalMmapVectorNoCtor {
|
2013-02-26 21:30:27 +08:00
|
|
|
public:
|
2020-12-29 09:16:49 +08:00
|
|
|
using value_type = T;
|
2014-12-31 07:16:12 +08:00
|
|
|
void Initialize(uptr initial_capacity) {
|
2018-05-10 07:31:05 +08:00
|
|
|
capacity_bytes_ = 0;
|
2013-02-26 21:30:27 +08:00
|
|
|
size_ = 0;
|
2018-05-10 07:31:05 +08:00
|
|
|
data_ = 0;
|
|
|
|
reserve(initial_capacity);
|
2013-02-26 21:30:27 +08:00
|
|
|
}
|
2018-05-10 07:31:05 +08:00
|
|
|
void Destroy() { UnmapOrDie(data_, capacity_bytes_); }
|
2013-02-26 21:30:27 +08:00
|
|
|
T &operator[](uptr i) {
|
2013-03-29 16:03:01 +08:00
|
|
|
CHECK_LT(i, size_);
|
2013-03-28 23:37:11 +08:00
|
|
|
return data_[i];
|
|
|
|
}
|
|
|
|
const T &operator[](uptr i) const {
|
2013-03-29 16:03:01 +08:00
|
|
|
CHECK_LT(i, size_);
|
2013-02-26 21:30:27 +08:00
|
|
|
return data_[i];
|
|
|
|
}
|
|
|
|
void push_back(const T &element) {
|
2018-05-10 07:31:05 +08:00
|
|
|
CHECK_LE(size_, capacity());
|
|
|
|
if (size_ == capacity()) {
|
2013-02-26 21:30:27 +08:00
|
|
|
uptr new_capacity = RoundUpToPowerOfTwo(size_ + 1);
|
2018-05-09 01:59:44 +08:00
|
|
|
Realloc(new_capacity);
|
2013-02-26 21:30:27 +08:00
|
|
|
}
|
2016-02-26 02:12:30 +08:00
|
|
|
internal_memcpy(&data_[size_++], &element, sizeof(T));
|
2013-02-26 21:30:27 +08:00
|
|
|
}
|
2013-03-05 19:58:25 +08:00
|
|
|
T &back() {
|
|
|
|
CHECK_GT(size_, 0);
|
|
|
|
return data_[size_ - 1];
|
|
|
|
}
|
|
|
|
void pop_back() {
|
|
|
|
CHECK_GT(size_, 0);
|
|
|
|
size_--;
|
|
|
|
}
|
2013-03-28 23:37:11 +08:00
|
|
|
uptr size() const {
|
2013-02-26 21:30:27 +08:00
|
|
|
return size_;
|
|
|
|
}
|
2013-04-01 21:55:34 +08:00
|
|
|
const T *data() const {
|
|
|
|
return data_;
|
|
|
|
}
|
2015-03-18 08:23:44 +08:00
|
|
|
T *data() {
|
|
|
|
return data_;
|
|
|
|
}
|
2018-05-10 07:31:05 +08:00
|
|
|
uptr capacity() const { return capacity_bytes_ / sizeof(T); }
|
2018-05-07 13:56:24 +08:00
|
|
|
void reserve(uptr new_size) {
|
2018-05-09 01:59:44 +08:00
|
|
|
// Never downsize internal buffer.
|
|
|
|
if (new_size > capacity())
|
|
|
|
Realloc(new_size);
|
2018-05-07 13:56:24 +08:00
|
|
|
}
|
2016-12-13 07:45:38 +08:00
|
|
|
void resize(uptr new_size) {
|
|
|
|
if (new_size > size_) {
|
2018-05-09 01:59:44 +08:00
|
|
|
reserve(new_size);
|
2016-12-13 07:45:38 +08:00
|
|
|
internal_memset(&data_[size_], 0, sizeof(T) * (new_size - size_));
|
|
|
|
}
|
|
|
|
size_ = new_size;
|
|
|
|
}
|
2013-02-26 21:30:27 +08:00
|
|
|
|
2013-11-15 15:18:15 +08:00
|
|
|
void clear() { size_ = 0; }
|
2015-03-05 07:41:55 +08:00
|
|
|
bool empty() const { return size() == 0; }
|
2013-11-15 15:18:15 +08:00
|
|
|
|
2016-01-16 08:31:29 +08:00
|
|
|
const T *begin() const {
|
|
|
|
return data();
|
|
|
|
}
|
|
|
|
T *begin() {
|
|
|
|
return data();
|
|
|
|
}
|
|
|
|
const T *end() const {
|
|
|
|
return data() + size();
|
|
|
|
}
|
|
|
|
T *end() {
|
|
|
|
return data() + size();
|
|
|
|
}
|
|
|
|
|
2018-05-08 12:57:08 +08:00
|
|
|
void swap(InternalMmapVectorNoCtor &other) {
|
|
|
|
Swap(data_, other.data_);
|
2018-05-10 07:31:05 +08:00
|
|
|
Swap(capacity_bytes_, other.capacity_bytes_);
|
2018-05-08 12:57:08 +08:00
|
|
|
Swap(size_, other.size_);
|
|
|
|
}
|
|
|
|
|
2013-02-26 21:30:27 +08:00
|
|
|
private:
|
2018-05-09 01:59:44 +08:00
|
|
|
void Realloc(uptr new_capacity) {
|
2013-02-26 21:30:27 +08:00
|
|
|
CHECK_GT(new_capacity, 0);
|
|
|
|
CHECK_LE(size_, new_capacity);
|
2018-05-10 07:31:05 +08:00
|
|
|
uptr new_capacity_bytes =
|
|
|
|
RoundUpTo(new_capacity * sizeof(T), GetPageSizeCached());
|
|
|
|
T *new_data = (T *)MmapOrDie(new_capacity_bytes, "InternalMmapVector");
|
2013-02-26 21:30:27 +08:00
|
|
|
internal_memcpy(new_data, data_, size_ * sizeof(T));
|
2018-05-10 07:31:05 +08:00
|
|
|
UnmapOrDie(data_, capacity_bytes_);
|
2013-02-26 21:30:27 +08:00
|
|
|
data_ = new_data;
|
2018-05-10 07:31:05 +08:00
|
|
|
capacity_bytes_ = new_capacity_bytes;
|
2013-02-26 21:30:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
T *data_;
|
2018-05-10 07:31:05 +08:00
|
|
|
uptr capacity_bytes_;
|
2013-02-26 21:30:27 +08:00
|
|
|
uptr size_;
|
|
|
|
};
|
2013-05-13 19:58:48 +08:00
|
|
|
|
2018-05-07 14:14:12 +08:00
|
|
|
template <typename T>
|
|
|
|
bool operator==(const InternalMmapVectorNoCtor<T> &lhs,
|
|
|
|
const InternalMmapVectorNoCtor<T> &rhs) {
|
|
|
|
if (lhs.size() != rhs.size()) return false;
|
|
|
|
return internal_memcmp(lhs.data(), rhs.data(), lhs.size() * sizeof(T)) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool operator!=(const InternalMmapVectorNoCtor<T> &lhs,
|
|
|
|
const InternalMmapVectorNoCtor<T> &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
2014-12-31 07:16:12 +08:00
|
|
|
template<typename T>
|
|
|
|
class InternalMmapVector : public InternalMmapVectorNoCtor<T> {
|
|
|
|
public:
|
2019-12-11 18:37:24 +08:00
|
|
|
InternalMmapVector() { InternalMmapVectorNoCtor<T>::Initialize(0); }
|
2018-05-07 13:56:36 +08:00
|
|
|
explicit InternalMmapVector(uptr cnt) {
|
|
|
|
InternalMmapVectorNoCtor<T>::Initialize(cnt);
|
|
|
|
this->resize(cnt);
|
|
|
|
}
|
2014-12-31 07:16:12 +08:00
|
|
|
~InternalMmapVector() { InternalMmapVectorNoCtor<T>::Destroy(); }
|
2018-05-07 13:56:12 +08:00
|
|
|
// Disallow copies and moves.
|
|
|
|
InternalMmapVector(const InternalMmapVector &) = delete;
|
|
|
|
InternalMmapVector &operator=(const InternalMmapVector &) = delete;
|
|
|
|
InternalMmapVector(InternalMmapVector &&) = delete;
|
|
|
|
InternalMmapVector &operator=(InternalMmapVector &&) = delete;
|
|
|
|
};
|
|
|
|
|
2021-03-16 18:55:29 +08:00
|
|
|
class InternalScopedString {
|
2018-05-07 13:56:12 +08:00
|
|
|
public:
|
|
|
|
explicit InternalScopedString(uptr max_length)
|
2021-03-16 18:55:29 +08:00
|
|
|
: buffer_(max_length), length_(0) {
|
|
|
|
buffer_[0] = '\0';
|
2018-05-07 13:56:12 +08:00
|
|
|
}
|
2021-03-16 18:55:29 +08:00
|
|
|
uptr size() const { return buffer_.size(); }
|
|
|
|
uptr length() const { return length_; }
|
2018-05-07 13:56:12 +08:00
|
|
|
void clear() {
|
|
|
|
(*this)[0] = '\0';
|
|
|
|
length_ = 0;
|
|
|
|
}
|
|
|
|
void append(const char *format, ...);
|
2021-03-16 18:55:29 +08:00
|
|
|
char *data() { return buffer_.data(); }
|
|
|
|
const char *data() const { return buffer_.data(); }
|
|
|
|
char &operator[](uptr i) { return buffer_[i]; }
|
|
|
|
const char &operator[](uptr i) const { return buffer_[i]; }
|
2018-05-07 13:56:12 +08:00
|
|
|
|
|
|
|
private:
|
2021-03-16 18:55:29 +08:00
|
|
|
InternalMmapVector<char> buffer_;
|
2018-05-07 13:56:12 +08:00
|
|
|
uptr length_;
|
2014-12-31 07:16:12 +08:00
|
|
|
};
|
|
|
|
|
2018-05-10 04:42:11 +08:00
|
|
|
template <class T>
|
|
|
|
struct CompareLess {
|
|
|
|
bool operator()(const T &a, const T &b) const { return a < b; }
|
|
|
|
};
|
|
|
|
|
2013-06-14 17:59:40 +08:00
|
|
|
// HeapSort for arrays and InternalMmapVector.
|
2018-05-10 04:42:11 +08:00
|
|
|
template <class T, class Compare = CompareLess<T>>
|
|
|
|
void Sort(T *v, uptr size, Compare comp = {}) {
|
2013-05-13 19:58:48 +08:00
|
|
|
if (size < 2)
|
|
|
|
return;
|
|
|
|
// Stage 1: insert elements to the heap.
|
|
|
|
for (uptr i = 1; i < size; i++) {
|
|
|
|
uptr j, p;
|
|
|
|
for (j = i; j > 0; j = p) {
|
|
|
|
p = (j - 1) / 2;
|
2018-05-10 04:42:11 +08:00
|
|
|
if (comp(v[p], v[j]))
|
|
|
|
Swap(v[j], v[p]);
|
2013-05-13 19:58:48 +08:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Stage 2: swap largest element with the last one,
|
|
|
|
// and sink the new top.
|
|
|
|
for (uptr i = size - 1; i > 0; i--) {
|
2018-05-10 04:42:11 +08:00
|
|
|
Swap(v[0], v[i]);
|
2013-05-13 19:58:48 +08:00
|
|
|
uptr j, max_ind;
|
|
|
|
for (j = 0; j < i; j = max_ind) {
|
|
|
|
uptr left = 2 * j + 1;
|
|
|
|
uptr right = 2 * j + 2;
|
|
|
|
max_ind = j;
|
2018-05-10 04:42:11 +08:00
|
|
|
if (left < i && comp(v[max_ind], v[left]))
|
2013-05-13 19:58:48 +08:00
|
|
|
max_ind = left;
|
2018-05-10 04:42:11 +08:00
|
|
|
if (right < i && comp(v[max_ind], v[right]))
|
2013-05-13 19:58:48 +08:00
|
|
|
max_ind = right;
|
|
|
|
if (max_ind != j)
|
2018-05-10 04:42:11 +08:00
|
|
|
Swap(v[j], v[max_ind]);
|
2013-05-13 19:58:48 +08:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-16 12:03:27 +08:00
|
|
|
// Works like std::lower_bound: finds the first element that is not less
|
|
|
|
// than the val.
|
2020-12-29 09:16:49 +08:00
|
|
|
template <class Container,
|
|
|
|
class Compare = CompareLess<typename Container::value_type>>
|
|
|
|
uptr InternalLowerBound(const Container &v,
|
|
|
|
const typename Container::value_type &val,
|
|
|
|
Compare comp = {}) {
|
|
|
|
uptr first = 0;
|
|
|
|
uptr last = v.size();
|
2016-11-16 12:03:27 +08:00
|
|
|
while (last > first) {
|
2013-08-26 21:24:43 +08:00
|
|
|
uptr mid = (first + last) / 2;
|
|
|
|
if (comp(v[mid], val))
|
|
|
|
first = mid + 1;
|
|
|
|
else
|
2016-11-16 12:03:27 +08:00
|
|
|
last = mid;
|
2013-08-26 21:24:43 +08:00
|
|
|
}
|
2016-11-16 12:03:27 +08:00
|
|
|
return first;
|
2013-08-26 21:24:43 +08:00
|
|
|
}
|
|
|
|
|
2016-12-03 05:27:14 +08:00
|
|
|
enum ModuleArch {
|
|
|
|
kModuleArchUnknown,
|
|
|
|
kModuleArchI386,
|
|
|
|
kModuleArchX86_64,
|
|
|
|
kModuleArchX86_64H,
|
|
|
|
kModuleArchARMV6,
|
|
|
|
kModuleArchARMV7,
|
|
|
|
kModuleArchARMV7S,
|
|
|
|
kModuleArchARMV7K,
|
2020-10-04 17:38:06 +08:00
|
|
|
kModuleArchARM64,
|
|
|
|
kModuleArchRISCV64
|
2016-12-03 05:27:14 +08:00
|
|
|
};
|
|
|
|
|
2020-12-29 09:45:54 +08:00
|
|
|
// Sorts and removes duplicates from the container.
|
|
|
|
template <class Container,
|
|
|
|
class Compare = CompareLess<typename Container::value_type>>
|
|
|
|
void SortAndDedup(Container &v, Compare comp = {}) {
|
|
|
|
Sort(v.data(), v.size(), comp);
|
|
|
|
uptr size = v.size();
|
|
|
|
if (size < 2)
|
|
|
|
return;
|
|
|
|
uptr last = 0;
|
|
|
|
for (uptr i = 1; i < size; ++i) {
|
|
|
|
if (comp(v[last], v[i])) {
|
|
|
|
++last;
|
|
|
|
if (last != i)
|
|
|
|
v[last] = v[i];
|
|
|
|
} else {
|
|
|
|
CHECK(!comp(v[i], v[last]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
v.resize(last + 1);
|
|
|
|
}
|
|
|
|
|
2018-05-09 02:35:11 +08:00
|
|
|
// Opens the file 'file_name" and reads up to 'max_len' bytes.
|
|
|
|
// The resulting buffer is mmaped and stored in '*buff'.
|
2018-06-07 04:53:43 +08:00
|
|
|
// Returns true if file was successfully opened and read.
|
|
|
|
bool ReadFileToVector(const char *file_name,
|
|
|
|
InternalMmapVectorNoCtor<char> *buff,
|
|
|
|
uptr max_len = 1 << 26, error_t *errno_p = nullptr);
|
|
|
|
|
|
|
|
// Opens the file 'file_name" and reads up to 'max_len' bytes.
|
|
|
|
// This function is less I/O efficient than ReadFileToVector as it may reread
|
|
|
|
// file multiple times to avoid mmap during read attempts. It's used to read
|
|
|
|
// procmap, so short reads with mmap in between can produce inconsistent result.
|
|
|
|
// The resulting buffer is mmaped and stored in '*buff'.
|
2018-05-09 02:35:11 +08:00
|
|
|
// The size of the mmaped region is stored in '*buff_size'.
|
|
|
|
// The total number of read bytes is stored in '*read_len'.
|
|
|
|
// Returns true if file was successfully opened and read.
|
|
|
|
bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size,
|
|
|
|
uptr *read_len, uptr max_len = 1 << 26,
|
|
|
|
error_t *errno_p = nullptr);
|
|
|
|
|
2017-01-07 04:57:47 +08:00
|
|
|
// When adding a new architecture, don't forget to also update
|
2019-08-01 02:51:27 +08:00
|
|
|
// script/asan_symbolize.py and sanitizer_symbolizer_libcdep.cpp.
|
2017-01-07 04:57:47 +08:00
|
|
|
inline const char *ModuleArchToString(ModuleArch arch) {
|
|
|
|
switch (arch) {
|
|
|
|
case kModuleArchUnknown:
|
|
|
|
return "";
|
|
|
|
case kModuleArchI386:
|
|
|
|
return "i386";
|
|
|
|
case kModuleArchX86_64:
|
|
|
|
return "x86_64";
|
|
|
|
case kModuleArchX86_64H:
|
|
|
|
return "x86_64h";
|
|
|
|
case kModuleArchARMV6:
|
|
|
|
return "armv6";
|
|
|
|
case kModuleArchARMV7:
|
|
|
|
return "armv7";
|
|
|
|
case kModuleArchARMV7S:
|
|
|
|
return "armv7s";
|
|
|
|
case kModuleArchARMV7K:
|
|
|
|
return "armv7k";
|
|
|
|
case kModuleArchARM64:
|
|
|
|
return "arm64";
|
2020-10-04 17:38:06 +08:00
|
|
|
case kModuleArchRISCV64:
|
|
|
|
return "riscv64";
|
2017-01-07 04:57:47 +08:00
|
|
|
}
|
|
|
|
CHECK(0 && "Invalid module arch");
|
2017-01-11 09:12:53 +08:00
|
|
|
return "";
|
2017-01-07 04:57:47 +08:00
|
|
|
}
|
|
|
|
|
2016-12-03 05:27:14 +08:00
|
|
|
const uptr kModuleUUIDSize = 16;
|
2017-07-26 02:16:58 +08:00
|
|
|
const uptr kMaxSegName = 16;
|
2016-12-03 05:27:14 +08:00
|
|
|
|
2013-09-10 22:36:16 +08:00
|
|
|
// Represents a binary loaded into virtual memory (e.g. this can be an
|
|
|
|
// executable or a shared object).
|
|
|
|
class LoadedModule {
|
|
|
|
public:
|
2016-12-03 05:27:14 +08:00
|
|
|
LoadedModule()
|
2017-01-07 03:34:54 +08:00
|
|
|
: full_name_(nullptr),
|
|
|
|
base_address_(0),
|
2017-01-07 04:57:47 +08:00
|
|
|
max_executable_address_(0),
|
2017-01-07 03:34:54 +08:00
|
|
|
arch_(kModuleArchUnknown),
|
|
|
|
instrumented_(false) {
|
2016-12-03 05:27:14 +08:00
|
|
|
internal_memset(uuid_, 0, kModuleUUIDSize);
|
|
|
|
ranges_.clear();
|
|
|
|
}
|
2015-04-06 20:49:30 +08:00
|
|
|
void set(const char *module_name, uptr base_address);
|
2016-12-03 05:27:14 +08:00
|
|
|
void set(const char *module_name, uptr base_address, ModuleArch arch,
|
2017-01-07 03:34:54 +08:00
|
|
|
u8 uuid[kModuleUUIDSize], bool instrumented);
|
2015-01-09 06:31:14 +08:00
|
|
|
void clear();
|
2017-07-26 02:16:58 +08:00
|
|
|
void addAddressRange(uptr beg, uptr end, bool executable, bool writable,
|
|
|
|
const char *name = nullptr);
|
2013-09-10 22:36:16 +08:00
|
|
|
bool containsAddress(uptr address) const;
|
|
|
|
|
|
|
|
const char *full_name() const { return full_name_; }
|
|
|
|
uptr base_address() const { return base_address_; }
|
2017-01-07 04:57:47 +08:00
|
|
|
uptr max_executable_address() const { return max_executable_address_; }
|
2016-12-03 05:27:14 +08:00
|
|
|
ModuleArch arch() const { return arch_; }
|
|
|
|
const u8 *uuid() const { return uuid_; }
|
2017-01-07 03:34:54 +08:00
|
|
|
bool instrumented() const { return instrumented_; }
|
2013-09-10 22:36:16 +08:00
|
|
|
|
|
|
|
struct AddressRange {
|
2015-01-09 06:03:05 +08:00
|
|
|
AddressRange *next;
|
2013-09-10 22:36:16 +08:00
|
|
|
uptr beg;
|
|
|
|
uptr end;
|
2015-01-09 06:03:05 +08:00
|
|
|
bool executable;
|
2017-05-19 21:34:02 +08:00
|
|
|
bool writable;
|
2017-07-26 02:16:58 +08:00
|
|
|
char name[kMaxSegName];
|
2017-04-18 00:34:38 +08:00
|
|
|
|
2017-07-26 02:16:58 +08:00
|
|
|
AddressRange(uptr beg, uptr end, bool executable, bool writable,
|
|
|
|
const char *name)
|
2017-04-18 00:34:38 +08:00
|
|
|
: next(nullptr),
|
|
|
|
beg(beg),
|
|
|
|
end(end),
|
|
|
|
executable(executable),
|
2017-07-26 02:16:58 +08:00
|
|
|
writable(writable) {
|
|
|
|
internal_strncpy(this->name, (name ? name : ""), ARRAY_SIZE(this->name));
|
|
|
|
}
|
2013-09-10 22:36:16 +08:00
|
|
|
};
|
2015-01-09 06:03:05 +08:00
|
|
|
|
2016-01-15 10:19:20 +08:00
|
|
|
const IntrusiveList<AddressRange> &ranges() const { return ranges_; }
|
2015-01-09 06:03:05 +08:00
|
|
|
|
|
|
|
private:
|
2015-01-09 06:31:14 +08:00
|
|
|
char *full_name_; // Owned.
|
2013-09-10 22:36:16 +08:00
|
|
|
uptr base_address_;
|
2017-01-07 04:57:47 +08:00
|
|
|
uptr max_executable_address_;
|
2016-12-03 05:27:14 +08:00
|
|
|
ModuleArch arch_;
|
|
|
|
u8 uuid_[kModuleUUIDSize];
|
2017-01-07 03:34:54 +08:00
|
|
|
bool instrumented_;
|
2015-01-09 06:03:05 +08:00
|
|
|
IntrusiveList<AddressRange> ranges_;
|
2013-09-10 22:36:16 +08:00
|
|
|
};
|
|
|
|
|
2016-02-23 02:52:51 +08:00
|
|
|
// List of LoadedModules. OS-dependent implementation is responsible for
|
|
|
|
// filling this information.
|
|
|
|
class ListOfModules {
|
|
|
|
public:
|
2017-09-30 04:55:06 +08:00
|
|
|
ListOfModules() : initialized(false) {}
|
2016-02-23 02:52:51 +08:00
|
|
|
~ListOfModules() { clear(); }
|
|
|
|
void init();
|
2017-10-03 04:22:16 +08:00
|
|
|
void fallbackInit(); // Uses fallback init if available, otherwise clears
|
2016-02-23 02:52:51 +08:00
|
|
|
const LoadedModule *begin() const { return modules_.begin(); }
|
|
|
|
LoadedModule *begin() { return modules_.begin(); }
|
|
|
|
const LoadedModule *end() const { return modules_.end(); }
|
|
|
|
LoadedModule *end() { return modules_.end(); }
|
|
|
|
uptr size() const { return modules_.size(); }
|
|
|
|
const LoadedModule &operator[](uptr i) const {
|
|
|
|
CHECK_LT(i, modules_.size());
|
|
|
|
return modules_[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void clear() {
|
|
|
|
for (auto &module : modules_) module.clear();
|
|
|
|
modules_.clear();
|
|
|
|
}
|
2017-09-30 04:55:06 +08:00
|
|
|
void clearOrInit() {
|
|
|
|
initialized ? clear() : modules_.Initialize(kInitialCapacity);
|
|
|
|
initialized = true;
|
|
|
|
}
|
2016-02-23 02:52:51 +08:00
|
|
|
|
2017-09-30 04:55:06 +08:00
|
|
|
InternalMmapVectorNoCtor<LoadedModule> modules_;
|
2016-02-23 02:52:51 +08:00
|
|
|
// We rarely have more than 16K loaded modules.
|
|
|
|
static const uptr kInitialCapacity = 1 << 14;
|
2017-09-30 04:55:06 +08:00
|
|
|
bool initialized;
|
2016-02-23 02:52:51 +08:00
|
|
|
};
|
2013-09-10 22:36:16 +08:00
|
|
|
|
2013-10-14 22:04:50 +08:00
|
|
|
// Callback type for iterating over a set of memory ranges.
|
|
|
|
typedef void (*RangeIteratorCallback)(uptr begin, uptr end, void *arg);
|
2013-12-13 21:13:46 +08:00
|
|
|
|
2015-06-30 04:28:55 +08:00
|
|
|
enum AndroidApiLevel {
|
|
|
|
ANDROID_NOT_ANDROID = 0,
|
|
|
|
ANDROID_KITKAT = 19,
|
|
|
|
ANDROID_LOLLIPOP_MR1 = 22,
|
|
|
|
ANDROID_POST_LOLLIPOP = 23
|
|
|
|
};
|
|
|
|
|
Reapply: [asan] On OS X, log reports to syslog and os_trace
When ASan currently detects a bug, by default it will only print out the text
of the report to stderr. This patch changes this behavior and writes the full
text of the report to syslog before we terminate the process. It also calls
os_trace (Activity Tracing available on OS X and iOS) with a message saying
that the report is available in syslog. This is useful, because this message
will be shown in the crash log.
For this to work, the patch makes sure we store the full report into
error_message_buffer unconditionally, and it also strips out ANSI escape
sequences from the report (they are used when producing colored reports).
I've initially tried to log to syslog during printing, which is done on Android
right now. The advantage is that if we crash during error reporting or the
produced error does not go through ScopedInErrorReport, we would still get a
(partial) message in the syslog. However, that solution is very problematic on
OS X. One issue is that the logging routine uses GCD, which may spawn a new
thread on its behalf. In many cases, the reporting logic locks threadRegistry,
which leads to deadlocks.
Reviewed at http://reviews.llvm.org/D13452
(In addition, add sanitizer_common_libcdep.cc to buildgo.sh to avoid
build failures on Linux.)
llvm-svn: 253688
2015-11-21 02:41:44 +08:00
|
|
|
void WriteToSyslog(const char *buffer);
|
|
|
|
|
2019-03-05 20:50:06 +08:00
|
|
|
#if defined(SANITIZER_WINDOWS) && defined(_MSC_VER) && !defined(__clang__)
|
2019-03-02 08:46:54 +08:00
|
|
|
#define SANITIZER_WIN_TRACE 1
|
|
|
|
#else
|
|
|
|
#define SANITIZER_WIN_TRACE 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if SANITIZER_MAC || SANITIZER_WIN_TRACE
|
2015-11-21 02:42:01 +08:00
|
|
|
void LogFullErrorReport(const char *buffer);
|
Reapply: [asan] On OS X, log reports to syslog and os_trace
When ASan currently detects a bug, by default it will only print out the text
of the report to stderr. This patch changes this behavior and writes the full
text of the report to syslog before we terminate the process. It also calls
os_trace (Activity Tracing available on OS X and iOS) with a message saying
that the report is available in syslog. This is useful, because this message
will be shown in the crash log.
For this to work, the patch makes sure we store the full report into
error_message_buffer unconditionally, and it also strips out ANSI escape
sequences from the report (they are used when producing colored reports).
I've initially tried to log to syslog during printing, which is done on Android
right now. The advantage is that if we crash during error reporting or the
produced error does not go through ScopedInErrorReport, we would still get a
(partial) message in the syslog. However, that solution is very problematic on
OS X. One issue is that the logging routine uses GCD, which may spawn a new
thread on its behalf. In many cases, the reporting logic locks threadRegistry,
which leads to deadlocks.
Reviewed at http://reviews.llvm.org/D13452
(In addition, add sanitizer_common_libcdep.cc to buildgo.sh to avoid
build failures on Linux.)
llvm-svn: 253688
2015-11-21 02:41:44 +08:00
|
|
|
#else
|
2020-09-17 22:04:50 +08:00
|
|
|
inline void LogFullErrorReport(const char *buffer) {}
|
Reapply: [asan] On OS X, log reports to syslog and os_trace
When ASan currently detects a bug, by default it will only print out the text
of the report to stderr. This patch changes this behavior and writes the full
text of the report to syslog before we terminate the process. It also calls
os_trace (Activity Tracing available on OS X and iOS) with a message saying
that the report is available in syslog. This is useful, because this message
will be shown in the crash log.
For this to work, the patch makes sure we store the full report into
error_message_buffer unconditionally, and it also strips out ANSI escape
sequences from the report (they are used when producing colored reports).
I've initially tried to log to syslog during printing, which is done on Android
right now. The advantage is that if we crash during error reporting or the
produced error does not go through ScopedInErrorReport, we would still get a
(partial) message in the syslog. However, that solution is very problematic on
OS X. One issue is that the logging routine uses GCD, which may spawn a new
thread on its behalf. In many cases, the reporting logic locks threadRegistry,
which leads to deadlocks.
Reviewed at http://reviews.llvm.org/D13452
(In addition, add sanitizer_common_libcdep.cc to buildgo.sh to avoid
build failures on Linux.)
llvm-svn: 253688
2015-11-21 02:41:44 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if SANITIZER_LINUX || SANITIZER_MAC
|
|
|
|
void WriteOneLineToSyslog(const char *s);
|
2016-01-07 07:15:01 +08:00
|
|
|
void LogMessageOnPrintf(const char *str);
|
Reapply: [asan] On OS X, log reports to syslog and os_trace
When ASan currently detects a bug, by default it will only print out the text
of the report to stderr. This patch changes this behavior and writes the full
text of the report to syslog before we terminate the process. It also calls
os_trace (Activity Tracing available on OS X and iOS) with a message saying
that the report is available in syslog. This is useful, because this message
will be shown in the crash log.
For this to work, the patch makes sure we store the full report into
error_message_buffer unconditionally, and it also strips out ANSI escape
sequences from the report (they are used when producing colored reports).
I've initially tried to log to syslog during printing, which is done on Android
right now. The advantage is that if we crash during error reporting or the
produced error does not go through ScopedInErrorReport, we would still get a
(partial) message in the syslog. However, that solution is very problematic on
OS X. One issue is that the logging routine uses GCD, which may spawn a new
thread on its behalf. In many cases, the reporting logic locks threadRegistry,
which leads to deadlocks.
Reviewed at http://reviews.llvm.org/D13452
(In addition, add sanitizer_common_libcdep.cc to buildgo.sh to avoid
build failures on Linux.)
llvm-svn: 253688
2015-11-21 02:41:44 +08:00
|
|
|
#else
|
2020-09-17 22:04:50 +08:00
|
|
|
inline void WriteOneLineToSyslog(const char *s) {}
|
|
|
|
inline void LogMessageOnPrintf(const char *str) {}
|
Reapply: [asan] On OS X, log reports to syslog and os_trace
When ASan currently detects a bug, by default it will only print out the text
of the report to stderr. This patch changes this behavior and writes the full
text of the report to syslog before we terminate the process. It also calls
os_trace (Activity Tracing available on OS X and iOS) with a message saying
that the report is available in syslog. This is useful, because this message
will be shown in the crash log.
For this to work, the patch makes sure we store the full report into
error_message_buffer unconditionally, and it also strips out ANSI escape
sequences from the report (they are used when producing colored reports).
I've initially tried to log to syslog during printing, which is done on Android
right now. The advantage is that if we crash during error reporting or the
produced error does not go through ScopedInErrorReport, we would still get a
(partial) message in the syslog. However, that solution is very problematic on
OS X. One issue is that the logging routine uses GCD, which may spawn a new
thread on its behalf. In many cases, the reporting logic locks threadRegistry,
which leads to deadlocks.
Reviewed at http://reviews.llvm.org/D13452
(In addition, add sanitizer_common_libcdep.cc to buildgo.sh to avoid
build failures on Linux.)
llvm-svn: 253688
2015-11-21 02:41:44 +08:00
|
|
|
#endif
|
|
|
|
|
2019-03-02 08:46:54 +08:00
|
|
|
#if SANITIZER_LINUX || SANITIZER_WIN_TRACE
|
2014-09-15 19:37:40 +08:00
|
|
|
// Initialize Android logging. Any writes before this are silently lost.
|
|
|
|
void AndroidLogInit();
|
2017-06-24 07:38:20 +08:00
|
|
|
void SetAbortMessage(const char *);
|
2015-07-24 06:05:20 +08:00
|
|
|
#else
|
2020-09-17 22:04:50 +08:00
|
|
|
inline void AndroidLogInit() {}
|
2017-06-24 07:38:20 +08:00
|
|
|
// FIXME: MacOS implementation could use CRSetCrashLogMessage.
|
2020-09-17 22:04:50 +08:00
|
|
|
inline void SetAbortMessage(const char *) {}
|
2015-07-24 06:05:20 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if SANITIZER_ANDROID
|
2014-02-11 21:38:57 +08:00
|
|
|
void SanitizerInitializeUnwinder();
|
2015-06-30 04:28:55 +08:00
|
|
|
AndroidApiLevel AndroidGetApiLevel();
|
2014-01-23 19:34:41 +08:00
|
|
|
#else
|
2020-09-17 22:04:50 +08:00
|
|
|
inline void AndroidLogWrite(const char *buffer_unused) {}
|
|
|
|
inline void SanitizerInitializeUnwinder() {}
|
|
|
|
inline AndroidApiLevel AndroidGetApiLevel() { return ANDROID_NOT_ANDROID; }
|
2014-01-23 19:34:41 +08:00
|
|
|
#endif
|
2014-11-22 05:25:09 +08:00
|
|
|
|
2020-09-17 22:04:50 +08:00
|
|
|
inline uptr GetPthreadDestructorIterations() {
|
2015-06-30 04:28:55 +08:00
|
|
|
#if SANITIZER_ANDROID
|
|
|
|
return (AndroidGetApiLevel() == ANDROID_LOLLIPOP_MR1) ? 8 : 4;
|
|
|
|
#elif SANITIZER_POSIX
|
|
|
|
return 4;
|
|
|
|
#else
|
|
|
|
// Unused on Windows.
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-01-24 05:01:08 +08:00
|
|
|
void *internal_start_thread(void *(*func)(void*), void *arg);
|
2014-12-17 03:13:01 +08:00
|
|
|
void internal_join_thread(void *th);
|
|
|
|
void MaybeStartBackgroudThread();
|
|
|
|
|
2014-11-22 05:25:09 +08:00
|
|
|
// Make the compiler think that something is going on there.
|
|
|
|
// Use this inside a loop that looks like memset/memcpy/etc to prevent the
|
|
|
|
// compiler from recognising it and turning it into an actual call to
|
|
|
|
// memset/memcpy/etc.
|
|
|
|
static inline void SanitizerBreakOptimization(void *arg) {
|
2016-04-02 01:09:08 +08:00
|
|
|
#if defined(_MSC_VER) && !defined(__clang__)
|
2015-07-02 09:44:34 +08:00
|
|
|
_ReadWriteBarrier();
|
2014-11-22 05:25:09 +08:00
|
|
|
#else
|
|
|
|
__asm__ __volatile__("" : : "r" (arg) : "memory");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-03-03 01:36:02 +08:00
|
|
|
struct SignalContext {
|
2017-09-14 02:30:06 +08:00
|
|
|
void *siginfo;
|
2015-03-03 01:36:02 +08:00
|
|
|
void *context;
|
|
|
|
uptr addr;
|
|
|
|
uptr pc;
|
|
|
|
uptr sp;
|
|
|
|
uptr bp;
|
2016-02-04 10:02:09 +08:00
|
|
|
bool is_memory_access;
|
2016-02-09 06:50:25 +08:00
|
|
|
enum WriteFlag { UNKNOWN, READ, WRITE } write_flag;
|
2015-03-03 01:36:02 +08:00
|
|
|
|
2019-10-11 01:19:58 +08:00
|
|
|
// In some cases the kernel cannot provide the true faulting address; `addr`
|
|
|
|
// will be zero then. This field allows to distinguish between these cases
|
|
|
|
// and dereferences of null.
|
|
|
|
bool is_true_faulting_addr;
|
|
|
|
|
2017-09-14 02:30:16 +08:00
|
|
|
// VS2013 doesn't implement unrestricted unions, so we need a trivial default
|
|
|
|
// constructor
|
|
|
|
SignalContext() = default;
|
2017-09-14 10:48:41 +08:00
|
|
|
|
|
|
|
// Creates signal context in a platform-specific manner.
|
2017-09-14 02:30:06 +08:00
|
|
|
// SignalContext is going to keep pointers to siginfo and context without
|
|
|
|
// owning them.
|
2017-09-14 10:48:41 +08:00
|
|
|
SignalContext(void *siginfo, void *context)
|
2017-09-14 02:30:06 +08:00
|
|
|
: siginfo(siginfo),
|
|
|
|
context(context),
|
2017-09-14 10:48:41 +08:00
|
|
|
addr(GetAddress()),
|
|
|
|
is_memory_access(IsMemoryAccess()),
|
2019-10-11 01:19:58 +08:00
|
|
|
write_flag(GetWriteFlag()),
|
|
|
|
is_true_faulting_addr(IsTrueFaultingAddress()) {
|
2017-09-14 10:48:41 +08:00
|
|
|
InitPcSpBp();
|
|
|
|
}
|
2015-03-03 01:36:02 +08:00
|
|
|
|
2016-11-26 08:50:08 +08:00
|
|
|
static void DumpAllRegisters(void *context);
|
|
|
|
|
2017-09-14 02:30:06 +08:00
|
|
|
// Type of signal e.g. SIGSEGV or EXCEPTION_ACCESS_VIOLATION.
|
|
|
|
int GetType() const;
|
|
|
|
|
|
|
|
// String description of the signal.
|
2017-09-14 02:30:16 +08:00
|
|
|
const char *Describe() const;
|
2015-03-03 01:36:02 +08:00
|
|
|
|
2017-09-14 11:23:02 +08:00
|
|
|
// Returns true if signal is stack overflow.
|
|
|
|
bool IsStackOverflow() const;
|
|
|
|
|
2017-09-14 10:48:41 +08:00
|
|
|
private:
|
|
|
|
// Platform specific initialization.
|
|
|
|
void InitPcSpBp();
|
|
|
|
uptr GetAddress() const;
|
|
|
|
WriteFlag GetWriteFlag() const;
|
|
|
|
bool IsMemoryAccess() const;
|
2019-10-11 01:19:58 +08:00
|
|
|
bool IsTrueFaultingAddress() const;
|
2017-09-14 10:48:41 +08:00
|
|
|
};
|
2015-03-03 01:36:02 +08:00
|
|
|
|
2018-11-07 03:55:19 +08:00
|
|
|
void InitializePlatformEarly();
|
2015-12-03 18:39:43 +08:00
|
|
|
void MaybeReexec();
|
|
|
|
|
2016-01-27 04:10:01 +08:00
|
|
|
template <typename Fn>
|
|
|
|
class RunOnDestruction {
|
|
|
|
public:
|
|
|
|
explicit RunOnDestruction(Fn fn) : fn_(fn) {}
|
|
|
|
~RunOnDestruction() { fn_(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Fn fn_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A simple scope guard. Usage:
|
|
|
|
// auto cleanup = at_scope_exit([]{ do_cleanup; });
|
|
|
|
template <typename Fn>
|
|
|
|
RunOnDestruction<Fn> at_scope_exit(Fn fn) {
|
|
|
|
return RunOnDestruction<Fn>(fn);
|
|
|
|
}
|
|
|
|
|
[sanitizer] [SystemZ] Abort if the kernel might be vulnerable to CVE-2016-2143.
In short, CVE-2016-2143 will crash the machine if a process uses both >4TB
virtual addresses and fork(). ASan, TSan, and MSan will, by necessity, map
a sizable chunk of virtual address space, which is much larger than 4TB.
Even worse, sanitizers will always use fork() for llvm-symbolizer when a bug
is detected. Disable all three by aborting on process initialization if
the running kernel version is not known to contain a fix.
Unfortunately, there's no reliable way to detect the fix without crashing
the kernel. So, we rely on whitelisting - I've included a list of upstream
kernel versions that will work. In case someone uses a distribution kernel
or applied the fix themselves, an override switch is also included.
Differential Revision: http://reviews.llvm.org/D19576
llvm-svn: 267747
2016-04-28 01:42:00 +08:00
|
|
|
// Linux on 64-bit s390 had a nasty bug that crashes the whole machine
|
|
|
|
// if a process uses virtual memory over 4TB (as many sanitizers like
|
|
|
|
// to do). This function will abort the process if running on a kernel
|
|
|
|
// that looks vulnerable.
|
|
|
|
#if SANITIZER_LINUX && SANITIZER_S390_64
|
|
|
|
void AvoidCVE_2016_2143();
|
|
|
|
#else
|
2020-09-17 22:04:50 +08:00
|
|
|
inline void AvoidCVE_2016_2143() {}
|
[sanitizer] [SystemZ] Abort if the kernel might be vulnerable to CVE-2016-2143.
In short, CVE-2016-2143 will crash the machine if a process uses both >4TB
virtual addresses and fork(). ASan, TSan, and MSan will, by necessity, map
a sizable chunk of virtual address space, which is much larger than 4TB.
Even worse, sanitizers will always use fork() for llvm-symbolizer when a bug
is detected. Disable all three by aborting on process initialization if
the running kernel version is not known to contain a fix.
Unfortunately, there's no reliable way to detect the fix without crashing
the kernel. So, we rely on whitelisting - I've included a list of upstream
kernel versions that will work. In case someone uses a distribution kernel
or applied the fix themselves, an override switch is also included.
Differential Revision: http://reviews.llvm.org/D19576
llvm-svn: 267747
2016-04-28 01:42:00 +08:00
|
|
|
#endif
|
|
|
|
|
2016-09-16 05:02:18 +08:00
|
|
|
struct StackDepotStats {
|
|
|
|
uptr n_uniq_ids;
|
|
|
|
uptr allocated;
|
|
|
|
};
|
|
|
|
|
2016-11-29 08:22:50 +08:00
|
|
|
// The default value for allocator_release_to_os_interval_ms common flag to
|
|
|
|
// indicate that sanitizer allocator should not attempt to release memory to OS.
|
|
|
|
const s32 kReleaseToOSIntervalNever = -1;
|
|
|
|
|
2017-03-09 18:47:38 +08:00
|
|
|
void CheckNoDeepBind(const char *filename, int flag);
|
|
|
|
|
[sanitizer] Add a function to gather random bytes
Summary:
AFAICT compiler-rt doesn't have a function that would return 'good' random
bytes to seed a PRNG. Currently, the `SizeClassAllocator64` uses addresses
returned by `mmap` to seed its PRNG, which is not ideal, and
`SizeClassAllocator32` doesn't benefit from the entropy offered by its 64-bit
counterpart address space, so right now it has nothing. This function aims at
solving this, allowing to implement good 32-bit chunk randomization. Scudo also
has a function that does this for Cookie purposes, which would go away in a
later CL once this lands.
This function will try the `getrandom` syscall if available, and fallback to
`/dev/urandom` if not.
Unfortunately, I do not have a way to implement and test a Mac and Windows
version, so those are unimplemented as of now. Note that `kRandomShuffleChunks`
is only used on Linux for now.
Reviewers: alekseyshl
Reviewed By: alekseyshl
Subscribers: zturner, rnk, llvm-commits, kubamracek
Differential Revision: https://reviews.llvm.org/D34412
llvm-svn: 305922
2017-06-21 23:56:03 +08:00
|
|
|
// Returns the requested amount of random data (up to 256 bytes) that can then
|
2017-08-14 22:53:47 +08:00
|
|
|
// be used to seed a PRNG. Defaults to blocking like the underlying syscall.
|
|
|
|
bool GetRandom(void *buffer, uptr length, bool blocking = true);
|
[sanitizer] Add a function to gather random bytes
Summary:
AFAICT compiler-rt doesn't have a function that would return 'good' random
bytes to seed a PRNG. Currently, the `SizeClassAllocator64` uses addresses
returned by `mmap` to seed its PRNG, which is not ideal, and
`SizeClassAllocator32` doesn't benefit from the entropy offered by its 64-bit
counterpart address space, so right now it has nothing. This function aims at
solving this, allowing to implement good 32-bit chunk randomization. Scudo also
has a function that does this for Cookie purposes, which would go away in a
later CL once this lands.
This function will try the `getrandom` syscall if available, and fallback to
`/dev/urandom` if not.
Unfortunately, I do not have a way to implement and test a Mac and Windows
version, so those are unimplemented as of now. Note that `kRandomShuffleChunks`
is only used on Linux for now.
Reviewers: alekseyshl
Reviewed By: alekseyshl
Subscribers: zturner, rnk, llvm-commits, kubamracek
Differential Revision: https://reviews.llvm.org/D34412
llvm-svn: 305922
2017-06-21 23:56:03 +08:00
|
|
|
|
2017-11-22 05:14:00 +08:00
|
|
|
// Returns the number of logical processors on the system.
|
|
|
|
u32 GetNumberOfCPUs();
|
|
|
|
extern u32 NumberOfCPUsCached;
|
2020-09-17 22:04:50 +08:00
|
|
|
inline u32 GetNumberOfCPUsCached() {
|
2017-11-22 05:14:00 +08:00
|
|
|
if (!NumberOfCPUsCached)
|
|
|
|
NumberOfCPUsCached = GetNumberOfCPUs();
|
|
|
|
return NumberOfCPUsCached;
|
|
|
|
}
|
|
|
|
|
2020-06-10 02:57:24 +08:00
|
|
|
template <typename T>
|
|
|
|
class ArrayRef {
|
|
|
|
public:
|
|
|
|
ArrayRef() {}
|
|
|
|
ArrayRef(T *begin, T *end) : begin_(begin), end_(end) {}
|
|
|
|
|
|
|
|
T *begin() { return begin_; }
|
|
|
|
T *end() { return end_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
T *begin_ = nullptr;
|
|
|
|
T *end_ = nullptr;
|
|
|
|
};
|
|
|
|
|
2012-06-06 14:47:26 +08:00
|
|
|
} // namespace __sanitizer
|
|
|
|
|
2013-10-24 14:23:39 +08:00
|
|
|
inline void *operator new(__sanitizer::operator_new_size_type size,
|
2019-09-12 05:33:06 +08:00
|
|
|
__sanitizer::LowLevelAllocator &alloc) { // NOLINT
|
2013-10-24 14:23:39 +08:00
|
|
|
return alloc.Allocate(size);
|
|
|
|
}
|
|
|
|
|
2012-06-06 14:47:26 +08:00
|
|
|
#endif // SANITIZER_COMMON_H
|