function.c (assign_parm_adjust_stack_rtl): Revise STRICT_ALIGNMENT check to use targe...
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_fuchsia.cc
blob6602f97b40bac7e5c87e7ef4c5ec39d64470ec4a
1 //===-- sanitizer_fuchsia.cc ----------------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is shared between AddressSanitizer and other sanitizer
9 // run-time libraries and implements Fuchsia-specific functions from
10 // sanitizer_common.h.
11 //===----------------------------------------------------------------------===//
13 #include "sanitizer_fuchsia.h"
14 #if SANITIZER_FUCHSIA
16 #include "sanitizer_common.h"
17 #include "sanitizer_libc.h"
18 #include "sanitizer_mutex.h"
20 #include <limits.h>
21 #include <pthread.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <zircon/errors.h>
25 #include <zircon/process.h>
26 #include <zircon/syscalls.h>
28 namespace __sanitizer {
30 void NORETURN internal__exit(int exitcode) { _zx_process_exit(exitcode); }
32 uptr internal_sched_yield() {
33 zx_status_t status = _zx_nanosleep(0);
34 CHECK_EQ(status, ZX_OK);
35 return 0; // Why doesn't this return void?
38 static void internal_nanosleep(zx_time_t ns) {
39 zx_status_t status = _zx_nanosleep(_zx_deadline_after(ns));
40 CHECK_EQ(status, ZX_OK);
43 unsigned int internal_sleep(unsigned int seconds) {
44 internal_nanosleep(ZX_SEC(seconds));
45 return 0;
48 u64 NanoTime() { return _zx_clock_get(ZX_CLOCK_UTC); }
50 u64 MonotonicNanoTime() { return _zx_clock_get(ZX_CLOCK_MONOTONIC); }
52 uptr internal_getpid() {
53 zx_info_handle_basic_t info;
54 zx_status_t status =
55 _zx_object_get_info(_zx_process_self(), ZX_INFO_HANDLE_BASIC, &info,
56 sizeof(info), NULL, NULL);
57 CHECK_EQ(status, ZX_OK);
58 uptr pid = static_cast<uptr>(info.koid);
59 CHECK_EQ(pid, info.koid);
60 return pid;
63 uptr GetThreadSelf() { return reinterpret_cast<uptr>(thrd_current()); }
65 tid_t GetTid() { return GetThreadSelf(); }
67 void Abort() { abort(); }
69 int Atexit(void (*function)(void)) { return atexit(function); }
71 void SleepForSeconds(int seconds) { internal_sleep(seconds); }
73 void SleepForMillis(int millis) { internal_nanosleep(ZX_MSEC(millis)); }
75 void GetThreadStackTopAndBottom(bool, uptr *stack_top, uptr *stack_bottom) {
76 pthread_attr_t attr;
77 CHECK_EQ(pthread_getattr_np(pthread_self(), &attr), 0);
78 void *base;
79 size_t size;
80 CHECK_EQ(pthread_attr_getstack(&attr, &base, &size), 0);
81 CHECK_EQ(pthread_attr_destroy(&attr), 0);
83 *stack_bottom = reinterpret_cast<uptr>(base);
84 *stack_top = *stack_bottom + size;
87 void MaybeReexec() {}
88 void CheckASLR() {}
89 void PlatformPrepareForSandboxing(__sanitizer_sandbox_arguments *args) {}
90 void DisableCoreDumperIfNecessary() {}
91 void InstallDeadlySignalHandlers(SignalHandlerType handler) {}
92 void SetAlternateSignalStack() {}
93 void UnsetAlternateSignalStack() {}
94 void InitTlsSize() {}
96 void PrintModuleMap() {}
98 bool SignalContext::IsStackOverflow() const { return false; }
99 void SignalContext::DumpAllRegisters(void *context) { UNIMPLEMENTED(); }
100 const char *SignalContext::Describe() const { UNIMPLEMENTED(); }
102 enum MutexState : int { MtxUnlocked = 0, MtxLocked = 1, MtxSleeping = 2 };
104 BlockingMutex::BlockingMutex() {
105 // NOTE! It's important that this use internal_memset, because plain
106 // memset might be intercepted (e.g., actually be __asan_memset).
107 // Defining this so the compiler initializes each field, e.g.:
108 // BlockingMutex::BlockingMutex() : BlockingMutex(LINKER_INITIALIZED) {}
109 // might result in the compiler generating a call to memset, which would
110 // have the same problem.
111 internal_memset(this, 0, sizeof(*this));
114 void BlockingMutex::Lock() {
115 CHECK_EQ(owner_, 0);
116 atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
117 if (atomic_exchange(m, MtxLocked, memory_order_acquire) == MtxUnlocked)
118 return;
119 while (atomic_exchange(m, MtxSleeping, memory_order_acquire) != MtxUnlocked) {
120 zx_status_t status = _zx_futex_wait(reinterpret_cast<zx_futex_t *>(m),
121 MtxSleeping, ZX_TIME_INFINITE);
122 if (status != ZX_ERR_BAD_STATE) // Normal race.
123 CHECK_EQ(status, ZX_OK);
127 void BlockingMutex::Unlock() {
128 atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
129 u32 v = atomic_exchange(m, MtxUnlocked, memory_order_release);
130 CHECK_NE(v, MtxUnlocked);
131 if (v == MtxSleeping) {
132 zx_status_t status = _zx_futex_wake(reinterpret_cast<zx_futex_t *>(m), 1);
133 CHECK_EQ(status, ZX_OK);
137 void BlockingMutex::CheckLocked() {
138 atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
139 CHECK_NE(MtxUnlocked, atomic_load(m, memory_order_relaxed));
142 uptr GetPageSize() { return PAGE_SIZE; }
144 uptr GetMmapGranularity() { return PAGE_SIZE; }
146 sanitizer_shadow_bounds_t ShadowBounds;
148 uptr GetMaxUserVirtualAddress() {
149 ShadowBounds = __sanitizer_shadow_bounds();
150 return ShadowBounds.memory_limit - 1;
153 uptr GetMaxVirtualAddress() { return GetMaxUserVirtualAddress(); }
155 static void *DoAnonymousMmapOrDie(uptr size, const char *mem_type,
156 bool raw_report, bool die_for_nomem) {
157 size = RoundUpTo(size, PAGE_SIZE);
159 zx_handle_t vmo;
160 zx_status_t status = _zx_vmo_create(size, 0, &vmo);
161 if (status != ZX_OK) {
162 if (status != ZX_ERR_NO_MEMORY || die_for_nomem)
163 ReportMmapFailureAndDie(size, mem_type, "zx_vmo_create", status,
164 raw_report);
165 return nullptr;
167 _zx_object_set_property(vmo, ZX_PROP_NAME, mem_type,
168 internal_strlen(mem_type));
170 // TODO(mcgrathr): Maybe allocate a VMAR for all sanitizer heap and use that?
171 uintptr_t addr;
172 status =
173 _zx_vmar_map(_zx_vmar_root_self(), ZX_VM_PERM_READ | ZX_VM_PERM_WRITE, 0,
174 vmo, 0, size, &addr);
175 _zx_handle_close(vmo);
177 if (status != ZX_OK) {
178 if (status != ZX_ERR_NO_MEMORY || die_for_nomem)
179 ReportMmapFailureAndDie(size, mem_type, "zx_vmar_map", status,
180 raw_report);
181 return nullptr;
184 IncreaseTotalMmap(size);
186 return reinterpret_cast<void *>(addr);
189 void *MmapOrDie(uptr size, const char *mem_type, bool raw_report) {
190 return DoAnonymousMmapOrDie(size, mem_type, raw_report, true);
193 void *MmapNoReserveOrDie(uptr size, const char *mem_type) {
194 return MmapOrDie(size, mem_type);
197 void *MmapOrDieOnFatalError(uptr size, const char *mem_type) {
198 return DoAnonymousMmapOrDie(size, mem_type, false, false);
201 uptr ReservedAddressRange::Init(uptr init_size, const char *name,
202 uptr fixed_addr) {
203 init_size = RoundUpTo(init_size, PAGE_SIZE);
204 DCHECK_EQ(os_handle_, ZX_HANDLE_INVALID);
205 uintptr_t base;
206 zx_handle_t vmar;
207 zx_status_t status =
208 _zx_vmar_allocate_old(_zx_vmar_root_self(), 0, init_size,
209 ZX_VM_FLAG_CAN_MAP_READ | ZX_VM_FLAG_CAN_MAP_WRITE |
210 ZX_VM_FLAG_CAN_MAP_SPECIFIC,
211 &vmar, &base);
212 if (status != ZX_OK)
213 ReportMmapFailureAndDie(init_size, name, "zx_vmar_allocate", status);
214 base_ = reinterpret_cast<void *>(base);
215 size_ = init_size;
216 name_ = name;
217 os_handle_ = vmar;
219 return reinterpret_cast<uptr>(base_);
222 static uptr DoMmapFixedOrDie(zx_handle_t vmar, uptr fixed_addr, uptr map_size,
223 void *base, const char *name, bool die_for_nomem) {
224 uptr offset = fixed_addr - reinterpret_cast<uptr>(base);
225 map_size = RoundUpTo(map_size, PAGE_SIZE);
226 zx_handle_t vmo;
227 zx_status_t status = _zx_vmo_create(map_size, 0, &vmo);
228 if (status != ZX_OK) {
229 if (status != ZX_ERR_NO_MEMORY || die_for_nomem)
230 ReportMmapFailureAndDie(map_size, name, "zx_vmo_create", status);
231 return 0;
233 _zx_object_set_property(vmo, ZX_PROP_NAME, name, internal_strlen(name));
234 DCHECK_GE(base + size_, map_size + offset);
235 uintptr_t addr;
237 status =
238 _zx_vmar_map(vmar, ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | ZX_VM_SPECIFIC,
239 offset, vmo, 0, map_size, &addr);
240 _zx_handle_close(vmo);
241 if (status != ZX_OK) {
242 if (status != ZX_ERR_NO_MEMORY || die_for_nomem) {
243 ReportMmapFailureAndDie(map_size, name, "zx_vmar_map", status);
245 return 0;
247 IncreaseTotalMmap(map_size);
248 return addr;
251 uptr ReservedAddressRange::Map(uptr fixed_addr, uptr map_size) {
252 return DoMmapFixedOrDie(os_handle_, fixed_addr, map_size, base_,
253 name_, false);
256 uptr ReservedAddressRange::MapOrDie(uptr fixed_addr, uptr map_size) {
257 return DoMmapFixedOrDie(os_handle_, fixed_addr, map_size, base_,
258 name_, true);
261 void UnmapOrDieVmar(void *addr, uptr size, zx_handle_t target_vmar) {
262 if (!addr || !size) return;
263 size = RoundUpTo(size, PAGE_SIZE);
265 zx_status_t status =
266 _zx_vmar_unmap(target_vmar, reinterpret_cast<uintptr_t>(addr), size);
267 if (status != ZX_OK) {
268 Report("ERROR: %s failed to deallocate 0x%zx (%zd) bytes at address %p\n",
269 SanitizerToolName, size, size, addr);
270 CHECK("unable to unmap" && 0);
273 DecreaseTotalMmap(size);
276 void ReservedAddressRange::Unmap(uptr addr, uptr size) {
277 CHECK_LE(size, size_);
278 const zx_handle_t vmar = static_cast<zx_handle_t>(os_handle_);
279 if (addr == reinterpret_cast<uptr>(base_)) {
280 if (size == size_) {
281 // Destroying the vmar effectively unmaps the whole mapping.
282 _zx_vmar_destroy(vmar);
283 _zx_handle_close(vmar);
284 os_handle_ = static_cast<uptr>(ZX_HANDLE_INVALID);
285 DecreaseTotalMmap(size);
286 return;
288 } else {
289 CHECK_EQ(addr + size, reinterpret_cast<uptr>(base_) + size_);
291 // Partial unmapping does not affect the fact that the initial range is still
292 // reserved, and the resulting unmapped memory can't be reused.
293 UnmapOrDieVmar(reinterpret_cast<void *>(addr), size, vmar);
296 // This should never be called.
297 void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name) {
298 UNIMPLEMENTED();
301 void *MmapAlignedOrDieOnFatalError(uptr size, uptr alignment,
302 const char *mem_type) {
303 CHECK_GE(size, PAGE_SIZE);
304 CHECK(IsPowerOfTwo(size));
305 CHECK(IsPowerOfTwo(alignment));
307 zx_handle_t vmo;
308 zx_status_t status = _zx_vmo_create(size, 0, &vmo);
309 if (status != ZX_OK) {
310 if (status != ZX_ERR_NO_MEMORY)
311 ReportMmapFailureAndDie(size, mem_type, "zx_vmo_create", status, false);
312 return nullptr;
314 _zx_object_set_property(vmo, ZX_PROP_NAME, mem_type,
315 internal_strlen(mem_type));
317 // TODO(mcgrathr): Maybe allocate a VMAR for all sanitizer heap and use that?
319 // Map a larger size to get a chunk of address space big enough that
320 // it surely contains an aligned region of the requested size. Then
321 // overwrite the aligned middle portion with a mapping from the
322 // beginning of the VMO, and unmap the excess before and after.
323 size_t map_size = size + alignment;
324 uintptr_t addr;
325 status =
326 _zx_vmar_map(_zx_vmar_root_self(), ZX_VM_PERM_READ | ZX_VM_PERM_WRITE, 0,
327 vmo, 0, map_size, &addr);
328 if (status == ZX_OK) {
329 uintptr_t map_addr = addr;
330 uintptr_t map_end = map_addr + map_size;
331 addr = RoundUpTo(map_addr, alignment);
332 uintptr_t end = addr + size;
333 if (addr != map_addr) {
334 zx_info_vmar_t info;
335 status = _zx_object_get_info(_zx_vmar_root_self(), ZX_INFO_VMAR, &info,
336 sizeof(info), NULL, NULL);
337 if (status == ZX_OK) {
338 uintptr_t new_addr;
339 status = _zx_vmar_map(
340 _zx_vmar_root_self(),
341 ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | ZX_VM_SPECIFIC_OVERWRITE,
342 addr - info.base, vmo, 0, size, &new_addr);
343 if (status == ZX_OK) CHECK_EQ(new_addr, addr);
346 if (status == ZX_OK && addr != map_addr)
347 status = _zx_vmar_unmap(_zx_vmar_root_self(), map_addr, addr - map_addr);
348 if (status == ZX_OK && end != map_end)
349 status = _zx_vmar_unmap(_zx_vmar_root_self(), end, map_end - end);
351 _zx_handle_close(vmo);
353 if (status != ZX_OK) {
354 if (status != ZX_ERR_NO_MEMORY)
355 ReportMmapFailureAndDie(size, mem_type, "zx_vmar_map", status, false);
356 return nullptr;
359 IncreaseTotalMmap(size);
361 return reinterpret_cast<void *>(addr);
364 void UnmapOrDie(void *addr, uptr size) {
365 UnmapOrDieVmar(addr, size, _zx_vmar_root_self());
368 // This is used on the shadow mapping, which cannot be changed.
369 // Zircon doesn't have anything like MADV_DONTNEED.
370 void ReleaseMemoryPagesToOS(uptr beg, uptr end) {}
372 void DumpProcessMap() {
373 // TODO(mcgrathr): write it
374 return;
377 bool IsAccessibleMemoryRange(uptr beg, uptr size) {
378 // TODO(mcgrathr): Figure out a better way.
379 zx_handle_t vmo;
380 zx_status_t status = _zx_vmo_create(size, 0, &vmo);
381 if (status == ZX_OK) {
382 status = _zx_vmo_write(vmo, reinterpret_cast<const void *>(beg), 0, size);
383 _zx_handle_close(vmo);
385 return status == ZX_OK;
388 // FIXME implement on this platform.
389 void GetMemoryProfile(fill_profile_f cb, uptr *stats, uptr stats_size) {}
391 bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size,
392 uptr *read_len, uptr max_len, error_t *errno_p) {
393 zx_handle_t vmo;
394 zx_status_t status = __sanitizer_get_configuration(file_name, &vmo);
395 if (status == ZX_OK) {
396 uint64_t vmo_size;
397 status = _zx_vmo_get_size(vmo, &vmo_size);
398 if (status == ZX_OK) {
399 if (vmo_size < max_len) max_len = vmo_size;
400 size_t map_size = RoundUpTo(max_len, PAGE_SIZE);
401 uintptr_t addr;
402 status = _zx_vmar_map(_zx_vmar_root_self(), ZX_VM_PERM_READ, 0, vmo, 0,
403 map_size, &addr);
404 if (status == ZX_OK) {
405 *buff = reinterpret_cast<char *>(addr);
406 *buff_size = map_size;
407 *read_len = max_len;
410 _zx_handle_close(vmo);
412 if (status != ZX_OK && errno_p) *errno_p = status;
413 return status == ZX_OK;
416 void RawWrite(const char *buffer) {
417 constexpr size_t size = 128;
418 static _Thread_local char line[size];
419 static _Thread_local size_t lastLineEnd = 0;
420 static _Thread_local size_t cur = 0;
422 while (*buffer) {
423 if (cur >= size) {
424 if (lastLineEnd == 0)
425 lastLineEnd = size;
426 __sanitizer_log_write(line, lastLineEnd);
427 internal_memmove(line, line + lastLineEnd, cur - lastLineEnd);
428 cur = cur - lastLineEnd;
429 lastLineEnd = 0;
431 if (*buffer == '\n')
432 lastLineEnd = cur + 1;
433 line[cur++] = *buffer++;
435 // Flush all complete lines before returning.
436 if (lastLineEnd != 0) {
437 __sanitizer_log_write(line, lastLineEnd);
438 internal_memmove(line, line + lastLineEnd, cur - lastLineEnd);
439 cur = cur - lastLineEnd;
440 lastLineEnd = 0;
444 void CatastrophicErrorWrite(const char *buffer, uptr length) {
445 __sanitizer_log_write(buffer, length);
448 char **StoredArgv;
449 char **StoredEnviron;
451 char **GetArgv() { return StoredArgv; }
453 const char *GetEnv(const char *name) {
454 if (StoredEnviron) {
455 uptr NameLen = internal_strlen(name);
456 for (char **Env = StoredEnviron; *Env != 0; Env++) {
457 if (internal_strncmp(*Env, name, NameLen) == 0 && (*Env)[NameLen] == '=')
458 return (*Env) + NameLen + 1;
461 return nullptr;
464 uptr ReadBinaryName(/*out*/ char *buf, uptr buf_len) {
465 const char *argv0 = "<UNKNOWN>";
466 if (StoredArgv && StoredArgv[0]) {
467 argv0 = StoredArgv[0];
469 internal_strncpy(buf, argv0, buf_len);
470 return internal_strlen(buf);
473 uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len) {
474 return ReadBinaryName(buf, buf_len);
477 uptr MainThreadStackBase, MainThreadStackSize;
479 bool GetRandom(void *buffer, uptr length, bool blocking) {
480 CHECK_LE(length, ZX_CPRNG_DRAW_MAX_LEN);
481 _zx_cprng_draw(buffer, length);
482 return true;
485 u32 GetNumberOfCPUs() {
486 return zx_system_get_num_cpus();
489 uptr GetRSS() { UNIMPLEMENTED(); }
491 } // namespace __sanitizer
493 using namespace __sanitizer; // NOLINT
495 extern "C" {
496 void __sanitizer_startup_hook(int argc, char **argv, char **envp,
497 void *stack_base, size_t stack_size) {
498 __sanitizer::StoredArgv = argv;
499 __sanitizer::StoredEnviron = envp;
500 __sanitizer::MainThreadStackBase = reinterpret_cast<uintptr_t>(stack_base);
501 __sanitizer::MainThreadStackSize = stack_size;
504 void __sanitizer_set_report_path(const char *path) {
505 // Handle the initialization code in each sanitizer, but no other calls.
506 // This setting is never consulted on Fuchsia.
507 DCHECK_EQ(path, common_flags()->log_path);
510 void __sanitizer_set_report_fd(void *fd) {
511 UNREACHABLE("not available on Fuchsia");
513 } // extern "C"
515 #endif // SANITIZER_FUCHSIA