2019-08-01 02:51:27 +08:00
|
|
|
//===-- sanitizer_fuchsia.cpp ---------------------------------------------===//
|
2017-08-02 06:22:25 +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-08-02 06:22:25 +08:00
|
|
|
//
|
[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-17 00:32:19 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2017-08-02 06:22:25 +08:00
|
|
|
//
|
|
|
|
// This file is shared between AddressSanitizer and other sanitizer
|
|
|
|
// run-time libraries and implements Fuchsia-specific functions from
|
|
|
|
// sanitizer_common.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-17 00:32:19 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2017-08-02 06:22:25 +08:00
|
|
|
|
|
|
|
#include "sanitizer_fuchsia.h"
|
|
|
|
#if SANITIZER_FUCHSIA
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2017-09-13 09:18:15 +08:00
|
|
|
#include <zircon/errors.h>
|
|
|
|
#include <zircon/process.h>
|
|
|
|
#include <zircon/syscalls.h>
|
2020-10-30 01:51:01 +08:00
|
|
|
#include <zircon/utc.h>
|
|
|
|
|
|
|
|
#include "sanitizer_common.h"
|
|
|
|
#include "sanitizer_libc.h"
|
|
|
|
#include "sanitizer_mutex.h"
|
2017-08-02 06:22:25 +08:00
|
|
|
|
|
|
|
namespace __sanitizer {
|
|
|
|
|
2017-09-13 09:18:15 +08:00
|
|
|
void NORETURN internal__exit(int exitcode) { _zx_process_exit(exitcode); }
|
2017-08-02 06:22:25 +08:00
|
|
|
|
|
|
|
uptr internal_sched_yield() {
|
2017-09-13 09:18:15 +08:00
|
|
|
zx_status_t status = _zx_nanosleep(0);
|
|
|
|
CHECK_EQ(status, ZX_OK);
|
2017-08-02 06:22:25 +08:00
|
|
|
return 0; // Why doesn't this return void?
|
|
|
|
}
|
|
|
|
|
2017-09-13 09:18:15 +08:00
|
|
|
static void internal_nanosleep(zx_time_t ns) {
|
|
|
|
zx_status_t status = _zx_nanosleep(_zx_deadline_after(ns));
|
|
|
|
CHECK_EQ(status, ZX_OK);
|
2017-08-02 06:22:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int internal_sleep(unsigned int seconds) {
|
2017-09-13 09:18:15 +08:00
|
|
|
internal_nanosleep(ZX_SEC(seconds));
|
2017-08-02 06:22:25 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-05-10 07:42:58 +08:00
|
|
|
u64 NanoTime() {
|
2020-10-30 01:51:01 +08:00
|
|
|
zx_handle_t utc_clock = _zx_utc_reference_get();
|
|
|
|
CHECK_NE(utc_clock, ZX_HANDLE_INVALID);
|
2019-05-10 07:42:58 +08:00
|
|
|
zx_time_t time;
|
2020-10-30 01:51:01 +08:00
|
|
|
zx_status_t status = _zx_clock_read(utc_clock, &time);
|
2019-05-10 07:42:58 +08:00
|
|
|
CHECK_EQ(status, ZX_OK);
|
|
|
|
return time;
|
|
|
|
}
|
2017-08-02 06:22:25 +08:00
|
|
|
|
2019-05-10 07:42:58 +08:00
|
|
|
u64 MonotonicNanoTime() { return _zx_clock_get_monotonic(); }
|
2017-12-14 00:23:54 +08:00
|
|
|
|
2017-08-02 06:22:25 +08:00
|
|
|
uptr internal_getpid() {
|
2017-09-13 09:18:15 +08:00
|
|
|
zx_info_handle_basic_t info;
|
|
|
|
zx_status_t status =
|
|
|
|
_zx_object_get_info(_zx_process_self(), ZX_INFO_HANDLE_BASIC, &info,
|
2017-08-02 06:22:25 +08:00
|
|
|
sizeof(info), NULL, NULL);
|
2017-09-13 09:18:15 +08:00
|
|
|
CHECK_EQ(status, ZX_OK);
|
2017-08-02 06:22:25 +08:00
|
|
|
uptr pid = static_cast<uptr>(info.koid);
|
|
|
|
CHECK_EQ(pid, info.koid);
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
[Sanitizers] Get link map on FreeBSD and NetBSD via documented API
Summary:
Instead of hand-crafting an offset into the structure returned by
dlopen(3) to get at the link map, use the documented API. This is
described in dlinfo(3): by calling it with `RTLD_DI_LINKMAP`, the
dynamic linker ensures the right address is returned.
This is a recommit of 92e267a94dc4272511be674062f8a3e8897b7083, with
dlinfo(3) expliclity being referenced only for FreeBSD, non-Android
Linux, NetBSD and Solaris. Other OSes will have to add their own
implementation.
Reviewers: devnexen, emaste, MaskRay, krytarowski
Reviewed By: krytarowski
Subscribers: krytarowski, vitalybuka, #sanitizers, llvm-commits
Tags: #sanitizers, #llvm
Differential Revision: https://reviews.llvm.org/D73990
2020-02-11 06:43:12 +08:00
|
|
|
int internal_dlinfo(void *handle, int request, void *p) {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
2017-08-02 06:22:25 +08:00
|
|
|
uptr GetThreadSelf() { return reinterpret_cast<uptr>(thrd_current()); }
|
|
|
|
|
2018-03-06 10:01:32 +08:00
|
|
|
tid_t GetTid() { return GetThreadSelf(); }
|
2017-08-02 06:22:25 +08:00
|
|
|
|
|
|
|
void Abort() { abort(); }
|
|
|
|
|
|
|
|
int Atexit(void (*function)(void)) { return atexit(function); }
|
|
|
|
|
|
|
|
void SleepForSeconds(int seconds) { internal_sleep(seconds); }
|
|
|
|
|
2017-09-13 09:18:15 +08:00
|
|
|
void SleepForMillis(int millis) { internal_nanosleep(ZX_MSEC(millis)); }
|
2017-08-02 06:22:25 +08:00
|
|
|
|
|
|
|
void GetThreadStackTopAndBottom(bool, uptr *stack_top, uptr *stack_bottom) {
|
|
|
|
pthread_attr_t attr;
|
|
|
|
CHECK_EQ(pthread_getattr_np(pthread_self(), &attr), 0);
|
|
|
|
void *base;
|
|
|
|
size_t size;
|
|
|
|
CHECK_EQ(pthread_attr_getstack(&attr, &base, &size), 0);
|
|
|
|
CHECK_EQ(pthread_attr_destroy(&attr), 0);
|
|
|
|
|
|
|
|
*stack_bottom = reinterpret_cast<uptr>(base);
|
|
|
|
*stack_top = *stack_bottom + size;
|
|
|
|
}
|
|
|
|
|
2018-11-07 03:55:19 +08:00
|
|
|
void InitializePlatformEarly() {}
|
2017-08-02 06:22:25 +08:00
|
|
|
void MaybeReexec() {}
|
2018-06-05 15:29:23 +08:00
|
|
|
void CheckASLR() {}
|
2018-12-23 23:09:28 +08:00
|
|
|
void CheckMPROTECT() {}
|
2018-04-04 02:07:22 +08:00
|
|
|
void PlatformPrepareForSandboxing(__sanitizer_sandbox_arguments *args) {}
|
2017-08-02 06:22:25 +08:00
|
|
|
void DisableCoreDumperIfNecessary() {}
|
|
|
|
void InstallDeadlySignalHandlers(SignalHandlerType handler) {}
|
|
|
|
void SetAlternateSignalStack() {}
|
|
|
|
void UnsetAlternateSignalStack() {}
|
|
|
|
void InitTlsSize() {}
|
|
|
|
|
2017-09-14 11:23:02 +08:00
|
|
|
bool SignalContext::IsStackOverflow() const { return false; }
|
2017-08-09 08:21:45 +08:00
|
|
|
void SignalContext::DumpAllRegisters(void *context) { UNIMPLEMENTED(); }
|
2017-09-14 02:30:16 +08:00
|
|
|
const char *SignalContext::Describe() const { UNIMPLEMENTED(); }
|
2017-08-09 08:21:45 +08:00
|
|
|
|
2017-08-02 06:22:25 +08:00
|
|
|
enum MutexState : int { MtxUnlocked = 0, MtxLocked = 1, MtxSleeping = 2 };
|
|
|
|
|
|
|
|
BlockingMutex::BlockingMutex() {
|
|
|
|
// NOTE! It's important that this use internal_memset, because plain
|
|
|
|
// memset might be intercepted (e.g., actually be __asan_memset).
|
|
|
|
// Defining this so the compiler initializes each field, e.g.:
|
|
|
|
// BlockingMutex::BlockingMutex() : BlockingMutex(LINKER_INITIALIZED) {}
|
|
|
|
// might result in the compiler generating a call to memset, which would
|
|
|
|
// have the same problem.
|
|
|
|
internal_memset(this, 0, sizeof(*this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockingMutex::Lock() {
|
|
|
|
CHECK_EQ(owner_, 0);
|
|
|
|
atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
|
|
|
|
if (atomic_exchange(m, MtxLocked, memory_order_acquire) == MtxUnlocked)
|
|
|
|
return;
|
|
|
|
while (atomic_exchange(m, MtxSleeping, memory_order_acquire) != MtxUnlocked) {
|
2018-12-04 12:07:43 +08:00
|
|
|
zx_status_t status =
|
|
|
|
_zx_futex_wait(reinterpret_cast<zx_futex_t *>(m), MtxSleeping,
|
|
|
|
ZX_HANDLE_INVALID, ZX_TIME_INFINITE);
|
2017-09-13 09:18:15 +08:00
|
|
|
if (status != ZX_ERR_BAD_STATE) // Normal race.
|
|
|
|
CHECK_EQ(status, ZX_OK);
|
2017-08-02 06:22:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockingMutex::Unlock() {
|
|
|
|
atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
|
|
|
|
u32 v = atomic_exchange(m, MtxUnlocked, memory_order_release);
|
|
|
|
CHECK_NE(v, MtxUnlocked);
|
|
|
|
if (v == MtxSleeping) {
|
2017-09-13 09:18:15 +08:00
|
|
|
zx_status_t status = _zx_futex_wake(reinterpret_cast<zx_futex_t *>(m), 1);
|
|
|
|
CHECK_EQ(status, ZX_OK);
|
2017-08-02 06:22:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockingMutex::CheckLocked() {
|
|
|
|
atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
|
|
|
|
CHECK_NE(MtxUnlocked, atomic_load(m, memory_order_relaxed));
|
|
|
|
}
|
|
|
|
|
|
|
|
uptr GetPageSize() { return PAGE_SIZE; }
|
|
|
|
|
|
|
|
uptr GetMmapGranularity() { return PAGE_SIZE; }
|
|
|
|
|
|
|
|
sanitizer_shadow_bounds_t ShadowBounds;
|
|
|
|
|
2017-11-08 07:51:22 +08:00
|
|
|
uptr GetMaxUserVirtualAddress() {
|
2017-08-02 06:22:25 +08:00
|
|
|
ShadowBounds = __sanitizer_shadow_bounds();
|
|
|
|
return ShadowBounds.memory_limit - 1;
|
|
|
|
}
|
|
|
|
|
2017-11-28 03:53:53 +08:00
|
|
|
uptr GetMaxVirtualAddress() { return GetMaxUserVirtualAddress(); }
|
2017-11-21 01:41:57 +08:00
|
|
|
|
2017-08-02 06:22:25 +08:00
|
|
|
static void *DoAnonymousMmapOrDie(uptr size, const char *mem_type,
|
|
|
|
bool raw_report, bool die_for_nomem) {
|
|
|
|
size = RoundUpTo(size, PAGE_SIZE);
|
|
|
|
|
2017-09-13 09:18:15 +08:00
|
|
|
zx_handle_t vmo;
|
|
|
|
zx_status_t status = _zx_vmo_create(size, 0, &vmo);
|
|
|
|
if (status != ZX_OK) {
|
|
|
|
if (status != ZX_ERR_NO_MEMORY || die_for_nomem)
|
|
|
|
ReportMmapFailureAndDie(size, mem_type, "zx_vmo_create", status,
|
2017-08-02 06:22:25 +08:00
|
|
|
raw_report);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2017-09-13 09:18:15 +08:00
|
|
|
_zx_object_set_property(vmo, ZX_PROP_NAME, mem_type,
|
2017-08-02 06:22:25 +08:00
|
|
|
internal_strlen(mem_type));
|
|
|
|
|
|
|
|
// TODO(mcgrathr): Maybe allocate a VMAR for all sanitizer heap and use that?
|
|
|
|
uintptr_t addr;
|
2018-07-24 10:28:54 +08:00
|
|
|
status =
|
2018-08-30 09:27:26 +08:00
|
|
|
_zx_vmar_map(_zx_vmar_root_self(), ZX_VM_PERM_READ | ZX_VM_PERM_WRITE, 0,
|
|
|
|
vmo, 0, size, &addr);
|
2017-09-13 09:18:15 +08:00
|
|
|
_zx_handle_close(vmo);
|
2017-08-02 06:22:25 +08:00
|
|
|
|
2017-09-13 09:18:15 +08:00
|
|
|
if (status != ZX_OK) {
|
|
|
|
if (status != ZX_ERR_NO_MEMORY || die_for_nomem)
|
|
|
|
ReportMmapFailureAndDie(size, mem_type, "zx_vmar_map", status,
|
2017-08-02 06:22:25 +08:00
|
|
|
raw_report);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
IncreaseTotalMmap(size);
|
|
|
|
|
|
|
|
return reinterpret_cast<void *>(addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *MmapOrDie(uptr size, const char *mem_type, bool raw_report) {
|
|
|
|
return DoAnonymousMmapOrDie(size, mem_type, raw_report, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *MmapNoReserveOrDie(uptr size, const char *mem_type) {
|
|
|
|
return MmapOrDie(size, mem_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *MmapOrDieOnFatalError(uptr size, const char *mem_type) {
|
|
|
|
return DoAnonymousMmapOrDie(size, mem_type, false, false);
|
|
|
|
}
|
|
|
|
|
2017-11-28 03:53:53 +08:00
|
|
|
uptr ReservedAddressRange::Init(uptr init_size, const char *name,
|
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
|
|
|
uptr fixed_addr) {
|
2017-11-28 03:53:53 +08:00
|
|
|
init_size = RoundUpTo(init_size, PAGE_SIZE);
|
|
|
|
DCHECK_EQ(os_handle_, ZX_HANDLE_INVALID);
|
2017-08-02 06:22:25 +08:00
|
|
|
uintptr_t base;
|
2017-11-28 03:53:53 +08:00
|
|
|
zx_handle_t vmar;
|
2017-09-13 09:18:15 +08:00
|
|
|
zx_status_t status =
|
2019-01-06 13:19:05 +08:00
|
|
|
_zx_vmar_allocate(
|
|
|
|
_zx_vmar_root_self(),
|
|
|
|
ZX_VM_CAN_MAP_READ | ZX_VM_CAN_MAP_WRITE | ZX_VM_CAN_MAP_SPECIFIC,
|
|
|
|
0, init_size, &vmar, &base);
|
2017-09-13 09:18:15 +08:00
|
|
|
if (status != ZX_OK)
|
2017-11-28 03:53:53 +08:00
|
|
|
ReportMmapFailureAndDie(init_size, name, "zx_vmar_allocate", status);
|
|
|
|
base_ = reinterpret_cast<void *>(base);
|
|
|
|
size_ = init_size;
|
|
|
|
name_ = name;
|
|
|
|
os_handle_ = vmar;
|
2017-08-02 06:22:25 +08:00
|
|
|
|
2017-11-28 03:53:53 +08:00
|
|
|
return reinterpret_cast<uptr>(base_);
|
2017-08-02 06:22:25 +08:00
|
|
|
}
|
|
|
|
|
2017-11-28 03:53:53 +08:00
|
|
|
static uptr DoMmapFixedOrDie(zx_handle_t vmar, uptr fixed_addr, uptr map_size,
|
|
|
|
void *base, const char *name, bool die_for_nomem) {
|
|
|
|
uptr offset = fixed_addr - reinterpret_cast<uptr>(base);
|
|
|
|
map_size = RoundUpTo(map_size, PAGE_SIZE);
|
2017-09-13 09:18:15 +08:00
|
|
|
zx_handle_t vmo;
|
2017-11-28 03:53:53 +08:00
|
|
|
zx_status_t status = _zx_vmo_create(map_size, 0, &vmo);
|
2017-09-13 09:18:15 +08:00
|
|
|
if (status != ZX_OK) {
|
|
|
|
if (status != ZX_ERR_NO_MEMORY || die_for_nomem)
|
2017-11-28 03:53:53 +08:00
|
|
|
ReportMmapFailureAndDie(map_size, name, "zx_vmo_create", status);
|
|
|
|
return 0;
|
2017-08-02 06:22:25 +08:00
|
|
|
}
|
2018-04-12 02:55:26 +08:00
|
|
|
_zx_object_set_property(vmo, ZX_PROP_NAME, name, internal_strlen(name));
|
2017-11-28 03:53:53 +08:00
|
|
|
DCHECK_GE(base + size_, map_size + offset);
|
2017-08-02 06:22:25 +08:00
|
|
|
uintptr_t addr;
|
2017-11-28 03:53:53 +08:00
|
|
|
|
2018-08-30 09:27:26 +08:00
|
|
|
status =
|
|
|
|
_zx_vmar_map(vmar, ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | ZX_VM_SPECIFIC,
|
|
|
|
offset, vmo, 0, map_size, &addr);
|
2017-09-13 09:18:15 +08:00
|
|
|
_zx_handle_close(vmo);
|
|
|
|
if (status != ZX_OK) {
|
2017-11-28 03:53:53 +08:00
|
|
|
if (status != ZX_ERR_NO_MEMORY || die_for_nomem) {
|
|
|
|
ReportMmapFailureAndDie(map_size, name, "zx_vmar_map", status);
|
|
|
|
}
|
|
|
|
return 0;
|
2017-08-02 06:22:25 +08:00
|
|
|
}
|
2017-11-28 03:53:53 +08:00
|
|
|
IncreaseTotalMmap(map_size);
|
|
|
|
return addr;
|
|
|
|
}
|
2017-08-02 06:22:25 +08:00
|
|
|
|
2019-02-06 09:58:23 +08:00
|
|
|
uptr ReservedAddressRange::Map(uptr fixed_addr, uptr map_size,
|
|
|
|
const char *name) {
|
2017-11-28 03:53:53 +08:00
|
|
|
return DoMmapFixedOrDie(os_handle_, fixed_addr, map_size, base_,
|
|
|
|
name_, false);
|
|
|
|
}
|
2017-08-02 06:22:25 +08:00
|
|
|
|
2019-02-06 09:58:23 +08:00
|
|
|
uptr ReservedAddressRange::MapOrDie(uptr fixed_addr, uptr map_size,
|
|
|
|
const char *name) {
|
2017-11-28 03:53:53 +08:00
|
|
|
return DoMmapFixedOrDie(os_handle_, fixed_addr, map_size, base_,
|
|
|
|
name_, true);
|
2017-08-02 06:22:25 +08:00
|
|
|
}
|
|
|
|
|
2017-11-28 03:53:53 +08:00
|
|
|
void UnmapOrDieVmar(void *addr, uptr size, zx_handle_t target_vmar) {
|
|
|
|
if (!addr || !size) return;
|
|
|
|
size = RoundUpTo(size, PAGE_SIZE);
|
|
|
|
|
|
|
|
zx_status_t status =
|
|
|
|
_zx_vmar_unmap(target_vmar, reinterpret_cast<uintptr_t>(addr), size);
|
|
|
|
if (status != ZX_OK) {
|
|
|
|
Report("ERROR: %s failed to deallocate 0x%zx (%zd) bytes at address %p\n",
|
|
|
|
SanitizerToolName, size, size, addr);
|
|
|
|
CHECK("unable to unmap" && 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
DecreaseTotalMmap(size);
|
2017-08-02 06:22:25 +08:00
|
|
|
}
|
|
|
|
|
2018-04-20 02:38:15 +08:00
|
|
|
void ReservedAddressRange::Unmap(uptr addr, uptr size) {
|
2017-11-28 03:53:53 +08:00
|
|
|
CHECK_LE(size, size_);
|
[sanitizer][fuchsia] Fix VMAR leak
Summary:
Destroy and close a range's vmar if all its memory was unmapped.
This addresses some performance regression due to the proliferation of vmars
when Secondary backed allocations are concerned with Scudo on Fuchsia.
When a Secondary backed allocation was freed, the associated
`ReservedAddressRange` was going away after unmapping the entirety of the
mapping, but without getting rid of the associated vmar properly (which
was created specifically for that mapping). This resulted in an increase of
defunct vmars, that in turn slowed down further new vmar allocations.
This appears to solve ZX-2560/ZX-2642, at least on QEMU.
Reviewers: flowerhack, mcgrathr, phosek, mseaborn
Reviewed By: mcgrathr
Subscribers: kubamracek, delcypher, #sanitizers, llvm-commits
Differential Revision: https://reviews.llvm.org/D52242
llvm-svn: 342584
2018-09-20 03:50:35 +08:00
|
|
|
const zx_handle_t vmar = static_cast<zx_handle_t>(os_handle_);
|
|
|
|
if (addr == reinterpret_cast<uptr>(base_)) {
|
|
|
|
if (size == size_) {
|
|
|
|
// Destroying the vmar effectively unmaps the whole mapping.
|
|
|
|
_zx_vmar_destroy(vmar);
|
|
|
|
_zx_handle_close(vmar);
|
|
|
|
os_handle_ = static_cast<uptr>(ZX_HANDLE_INVALID);
|
|
|
|
DecreaseTotalMmap(size);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2018-04-20 02:38:15 +08:00
|
|
|
CHECK_EQ(addr + size, reinterpret_cast<uptr>(base_) + size_);
|
[sanitizer][fuchsia] Fix VMAR leak
Summary:
Destroy and close a range's vmar if all its memory was unmapped.
This addresses some performance regression due to the proliferation of vmars
when Secondary backed allocations are concerned with Scudo on Fuchsia.
When a Secondary backed allocation was freed, the associated
`ReservedAddressRange` was going away after unmapping the entirety of the
mapping, but without getting rid of the associated vmar properly (which
was created specifically for that mapping). This resulted in an increase of
defunct vmars, that in turn slowed down further new vmar allocations.
This appears to solve ZX-2560/ZX-2642, at least on QEMU.
Reviewers: flowerhack, mcgrathr, phosek, mseaborn
Reviewed By: mcgrathr
Subscribers: kubamracek, delcypher, #sanitizers, llvm-commits
Differential Revision: https://reviews.llvm.org/D52242
llvm-svn: 342584
2018-09-20 03:50:35 +08:00
|
|
|
}
|
|
|
|
// Partial unmapping does not affect the fact that the initial range is still
|
|
|
|
// reserved, and the resulting unmapped memory can't be reused.
|
|
|
|
UnmapOrDieVmar(reinterpret_cast<void *>(addr), size, vmar);
|
2017-08-02 06:22:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This should never be called.
|
|
|
|
void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name) {
|
|
|
|
UNIMPLEMENTED();
|
|
|
|
}
|
|
|
|
|
|
|
|
void *MmapAlignedOrDieOnFatalError(uptr size, uptr alignment,
|
|
|
|
const char *mem_type) {
|
|
|
|
CHECK_GE(size, PAGE_SIZE);
|
|
|
|
CHECK(IsPowerOfTwo(size));
|
|
|
|
CHECK(IsPowerOfTwo(alignment));
|
|
|
|
|
2017-09-13 09:18:15 +08:00
|
|
|
zx_handle_t vmo;
|
|
|
|
zx_status_t status = _zx_vmo_create(size, 0, &vmo);
|
|
|
|
if (status != ZX_OK) {
|
|
|
|
if (status != ZX_ERR_NO_MEMORY)
|
|
|
|
ReportMmapFailureAndDie(size, mem_type, "zx_vmo_create", status, false);
|
2017-08-02 06:22:25 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-09-13 09:18:15 +08:00
|
|
|
_zx_object_set_property(vmo, ZX_PROP_NAME, mem_type,
|
2017-08-02 06:22:25 +08:00
|
|
|
internal_strlen(mem_type));
|
|
|
|
|
|
|
|
// TODO(mcgrathr): Maybe allocate a VMAR for all sanitizer heap and use that?
|
|
|
|
|
|
|
|
// Map a larger size to get a chunk of address space big enough that
|
|
|
|
// it surely contains an aligned region of the requested size. Then
|
|
|
|
// overwrite the aligned middle portion with a mapping from the
|
|
|
|
// beginning of the VMO, and unmap the excess before and after.
|
|
|
|
size_t map_size = size + alignment;
|
|
|
|
uintptr_t addr;
|
2018-07-24 10:28:54 +08:00
|
|
|
status =
|
2018-08-30 09:27:26 +08:00
|
|
|
_zx_vmar_map(_zx_vmar_root_self(), ZX_VM_PERM_READ | ZX_VM_PERM_WRITE, 0,
|
|
|
|
vmo, 0, map_size, &addr);
|
2017-09-13 09:18:15 +08:00
|
|
|
if (status == ZX_OK) {
|
2017-08-02 06:22:25 +08:00
|
|
|
uintptr_t map_addr = addr;
|
|
|
|
uintptr_t map_end = map_addr + map_size;
|
|
|
|
addr = RoundUpTo(map_addr, alignment);
|
|
|
|
uintptr_t end = addr + size;
|
|
|
|
if (addr != map_addr) {
|
2017-09-13 09:18:15 +08:00
|
|
|
zx_info_vmar_t info;
|
|
|
|
status = _zx_object_get_info(_zx_vmar_root_self(), ZX_INFO_VMAR, &info,
|
2017-08-02 06:22:25 +08:00
|
|
|
sizeof(info), NULL, NULL);
|
2017-09-13 09:18:15 +08:00
|
|
|
if (status == ZX_OK) {
|
2017-08-02 06:22:25 +08:00
|
|
|
uintptr_t new_addr;
|
2018-08-30 09:27:26 +08:00
|
|
|
status = _zx_vmar_map(
|
|
|
|
_zx_vmar_root_self(),
|
|
|
|
ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | ZX_VM_SPECIFIC_OVERWRITE,
|
|
|
|
addr - info.base, vmo, 0, size, &new_addr);
|
2017-09-13 09:18:15 +08:00
|
|
|
if (status == ZX_OK) CHECK_EQ(new_addr, addr);
|
2017-08-02 06:22:25 +08:00
|
|
|
}
|
|
|
|
}
|
2017-09-13 09:18:15 +08:00
|
|
|
if (status == ZX_OK && addr != map_addr)
|
|
|
|
status = _zx_vmar_unmap(_zx_vmar_root_self(), map_addr, addr - map_addr);
|
|
|
|
if (status == ZX_OK && end != map_end)
|
|
|
|
status = _zx_vmar_unmap(_zx_vmar_root_self(), end, map_end - end);
|
2017-08-02 06:22:25 +08:00
|
|
|
}
|
2017-09-13 09:18:15 +08:00
|
|
|
_zx_handle_close(vmo);
|
2017-08-02 06:22:25 +08:00
|
|
|
|
2017-09-13 09:18:15 +08:00
|
|
|
if (status != ZX_OK) {
|
|
|
|
if (status != ZX_ERR_NO_MEMORY)
|
|
|
|
ReportMmapFailureAndDie(size, mem_type, "zx_vmar_map", status, false);
|
2017-08-02 06:22:25 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
IncreaseTotalMmap(size);
|
|
|
|
|
|
|
|
return reinterpret_cast<void *>(addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnmapOrDie(void *addr, uptr size) {
|
2017-11-28 03:53:53 +08:00
|
|
|
UnmapOrDieVmar(addr, size, _zx_vmar_root_self());
|
2017-08-02 06:22:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is used on the shadow mapping, which cannot be changed.
|
2017-09-13 09:18:15 +08:00
|
|
|
// Zircon doesn't have anything like MADV_DONTNEED.
|
2017-08-02 06:22:25 +08:00
|
|
|
void ReleaseMemoryPagesToOS(uptr beg, uptr end) {}
|
|
|
|
|
|
|
|
void DumpProcessMap() {
|
2017-11-28 03:53:53 +08:00
|
|
|
// TODO(mcgrathr): write it
|
|
|
|
return;
|
2017-08-02 06:22:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsAccessibleMemoryRange(uptr beg, uptr size) {
|
|
|
|
// TODO(mcgrathr): Figure out a better way.
|
2017-09-13 09:18:15 +08:00
|
|
|
zx_handle_t vmo;
|
|
|
|
zx_status_t status = _zx_vmo_create(size, 0, &vmo);
|
|
|
|
if (status == ZX_OK) {
|
2018-03-23 07:58:37 +08:00
|
|
|
status = _zx_vmo_write(vmo, reinterpret_cast<const void *>(beg), 0, size);
|
2017-09-13 09:18:15 +08:00
|
|
|
_zx_handle_close(vmo);
|
2017-08-02 06:22:25 +08:00
|
|
|
}
|
2017-09-13 09:18:15 +08:00
|
|
|
return status == ZX_OK;
|
2017-08-02 06:22:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME implement on this platform.
|
|
|
|
void GetMemoryProfile(fill_profile_f cb, uptr *stats, uptr stats_size) {}
|
|
|
|
|
|
|
|
bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size,
|
|
|
|
uptr *read_len, uptr max_len, error_t *errno_p) {
|
2017-09-13 09:18:15 +08:00
|
|
|
zx_handle_t vmo;
|
|
|
|
zx_status_t status = __sanitizer_get_configuration(file_name, &vmo);
|
|
|
|
if (status == ZX_OK) {
|
2017-08-02 06:22:25 +08:00
|
|
|
uint64_t vmo_size;
|
2017-09-13 09:18:15 +08:00
|
|
|
status = _zx_vmo_get_size(vmo, &vmo_size);
|
|
|
|
if (status == ZX_OK) {
|
2017-08-02 06:22:25 +08:00
|
|
|
if (vmo_size < max_len) max_len = vmo_size;
|
|
|
|
size_t map_size = RoundUpTo(max_len, PAGE_SIZE);
|
|
|
|
uintptr_t addr;
|
2018-08-30 09:27:26 +08:00
|
|
|
status = _zx_vmar_map(_zx_vmar_root_self(), ZX_VM_PERM_READ, 0, vmo, 0,
|
|
|
|
map_size, &addr);
|
2017-09-13 09:18:15 +08:00
|
|
|
if (status == ZX_OK) {
|
2017-08-02 06:22:25 +08:00
|
|
|
*buff = reinterpret_cast<char *>(addr);
|
|
|
|
*buff_size = map_size;
|
|
|
|
*read_len = max_len;
|
|
|
|
}
|
|
|
|
}
|
2017-09-13 09:18:15 +08:00
|
|
|
_zx_handle_close(vmo);
|
2017-08-02 06:22:25 +08:00
|
|
|
}
|
2017-09-13 09:18:15 +08:00
|
|
|
if (status != ZX_OK && errno_p) *errno_p = status;
|
|
|
|
return status == ZX_OK;
|
2017-08-02 06:22:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void RawWrite(const char *buffer) {
|
2018-05-24 06:27:12 +08:00
|
|
|
constexpr size_t size = 128;
|
|
|
|
static _Thread_local char line[size];
|
|
|
|
static _Thread_local size_t lastLineEnd = 0;
|
|
|
|
static _Thread_local size_t cur = 0;
|
|
|
|
|
|
|
|
while (*buffer) {
|
|
|
|
if (cur >= size) {
|
|
|
|
if (lastLineEnd == 0)
|
|
|
|
lastLineEnd = size;
|
|
|
|
__sanitizer_log_write(line, lastLineEnd);
|
|
|
|
internal_memmove(line, line + lastLineEnd, cur - lastLineEnd);
|
|
|
|
cur = cur - lastLineEnd;
|
|
|
|
lastLineEnd = 0;
|
|
|
|
}
|
|
|
|
if (*buffer == '\n')
|
|
|
|
lastLineEnd = cur + 1;
|
|
|
|
line[cur++] = *buffer++;
|
|
|
|
}
|
|
|
|
// Flush all complete lines before returning.
|
|
|
|
if (lastLineEnd != 0) {
|
|
|
|
__sanitizer_log_write(line, lastLineEnd);
|
|
|
|
internal_memmove(line, line + lastLineEnd, cur - lastLineEnd);
|
|
|
|
cur = cur - lastLineEnd;
|
|
|
|
lastLineEnd = 0;
|
|
|
|
}
|
2017-08-02 06:22:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CatastrophicErrorWrite(const char *buffer, uptr length) {
|
|
|
|
__sanitizer_log_write(buffer, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
char **StoredArgv;
|
|
|
|
char **StoredEnviron;
|
|
|
|
|
|
|
|
char **GetArgv() { return StoredArgv; }
|
2018-11-07 03:23:12 +08:00
|
|
|
char **GetEnviron() { return StoredEnviron; }
|
2017-08-02 06:22:25 +08:00
|
|
|
|
|
|
|
const char *GetEnv(const char *name) {
|
|
|
|
if (StoredEnviron) {
|
|
|
|
uptr NameLen = internal_strlen(name);
|
|
|
|
for (char **Env = StoredEnviron; *Env != 0; Env++) {
|
|
|
|
if (internal_strncmp(*Env, name, NameLen) == 0 && (*Env)[NameLen] == '=')
|
|
|
|
return (*Env) + NameLen + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
uptr ReadBinaryName(/*out*/ char *buf, uptr buf_len) {
|
2018-05-03 02:08:47 +08:00
|
|
|
const char *argv0 = "<UNKNOWN>";
|
|
|
|
if (StoredArgv && StoredArgv[0]) {
|
|
|
|
argv0 = StoredArgv[0];
|
|
|
|
}
|
2017-08-02 06:22:25 +08:00
|
|
|
internal_strncpy(buf, argv0, buf_len);
|
|
|
|
return internal_strlen(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len) {
|
|
|
|
return ReadBinaryName(buf, buf_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
uptr MainThreadStackBase, MainThreadStackSize;
|
|
|
|
|
2017-08-30 05:52:56 +08:00
|
|
|
bool GetRandom(void *buffer, uptr length, bool blocking) {
|
2017-09-13 09:18:15 +08:00
|
|
|
CHECK_LE(length, ZX_CPRNG_DRAW_MAX_LEN);
|
2018-06-28 05:25:21 +08:00
|
|
|
_zx_cprng_draw(buffer, length);
|
2017-08-30 05:52:56 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-22 05:14:00 +08:00
|
|
|
u32 GetNumberOfCPUs() {
|
|
|
|
return zx_system_get_num_cpus();
|
|
|
|
}
|
|
|
|
|
2017-11-28 03:53:53 +08:00
|
|
|
uptr GetRSS() { UNIMPLEMENTED(); }
|
|
|
|
|
2020-11-06 02:37:08 +08:00
|
|
|
void InitializePlatformCommonFlags(CommonFlags *cf) {}
|
|
|
|
|
2017-08-02 06:22:25 +08:00
|
|
|
} // namespace __sanitizer
|
|
|
|
|
2019-09-12 07:19:48 +08:00
|
|
|
using namespace __sanitizer;
|
2017-08-02 06:22:25 +08:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
void __sanitizer_startup_hook(int argc, char **argv, char **envp,
|
|
|
|
void *stack_base, size_t stack_size) {
|
|
|
|
__sanitizer::StoredArgv = argv;
|
|
|
|
__sanitizer::StoredEnviron = envp;
|
|
|
|
__sanitizer::MainThreadStackBase = reinterpret_cast<uintptr_t>(stack_base);
|
|
|
|
__sanitizer::MainThreadStackSize = stack_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void __sanitizer_set_report_path(const char *path) {
|
|
|
|
// Handle the initialization code in each sanitizer, but no other calls.
|
|
|
|
// This setting is never consulted on Fuchsia.
|
|
|
|
DCHECK_EQ(path, common_flags()->log_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void __sanitizer_set_report_fd(void *fd) {
|
|
|
|
UNREACHABLE("not available on Fuchsia");
|
|
|
|
}
|
2020-11-19 13:11:55 +08:00
|
|
|
|
|
|
|
const char *__sanitizer_get_report_path() {
|
|
|
|
UNREACHABLE("not available on Fuchsia");
|
|
|
|
}
|
2017-08-02 06:22:25 +08:00
|
|
|
} // extern "C"
|
|
|
|
|
|
|
|
#endif // SANITIZER_FUCHSIA
|