1 //===-- sanitizer_fuchsia.cpp ---------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file is shared between AddressSanitizer and other sanitizer 10 // run-time libraries and implements Fuchsia-specific functions from 11 // sanitizer_common.h. 12 //===----------------------------------------------------------------------===// 13 14 #include "sanitizer_fuchsia.h" 15 #if SANITIZER_FUCHSIA 16 17 #include <pthread.h> 18 #include <stdlib.h> 19 #include <unistd.h> 20 #include <zircon/errors.h> 21 #include <zircon/process.h> 22 #include <zircon/syscalls.h> 23 #include <zircon/utc.h> 24 25 #include "sanitizer_common.h" 26 #include "sanitizer_libc.h" 27 #include "sanitizer_mutex.h" 28 29 namespace __sanitizer { 30 31 void NORETURN internal__exit(int exitcode) { _zx_process_exit(exitcode); } 32 33 uptr internal_sched_yield() { 34 zx_status_t status = _zx_nanosleep(0); 35 CHECK_EQ(status, ZX_OK); 36 return 0; // Why doesn't this return void? 37 } 38 39 void internal_usleep(u64 useconds) { 40 zx_status_t status = _zx_nanosleep(_zx_deadline_after(ZX_USEC(useconds))); 41 CHECK_EQ(status, ZX_OK); 42 } 43 44 u64 NanoTime() { 45 zx_handle_t utc_clock = _zx_utc_reference_get(); 46 CHECK_NE(utc_clock, ZX_HANDLE_INVALID); 47 zx_time_t time; 48 zx_status_t status = _zx_clock_read(utc_clock, &time); 49 CHECK_EQ(status, ZX_OK); 50 return time; 51 } 52 53 u64 MonotonicNanoTime() { return _zx_clock_get_monotonic(); } 54 55 uptr internal_getpid() { 56 zx_info_handle_basic_t info; 57 zx_status_t status = 58 _zx_object_get_info(_zx_process_self(), ZX_INFO_HANDLE_BASIC, &info, 59 sizeof(info), NULL, NULL); 60 CHECK_EQ(status, ZX_OK); 61 uptr pid = static_cast<uptr>(info.koid); 62 CHECK_EQ(pid, info.koid); 63 return pid; 64 } 65 66 int internal_dlinfo(void *handle, int request, void *p) { UNIMPLEMENTED(); } 67 68 uptr GetThreadSelf() { return reinterpret_cast<uptr>(thrd_current()); } 69 70 tid_t GetTid() { return GetThreadSelf(); } 71 72 void Abort() { abort(); } 73 74 int Atexit(void (*function)(void)) { return atexit(function); } 75 76 void GetThreadStackTopAndBottom(bool, uptr *stack_top, uptr *stack_bottom) { 77 pthread_attr_t attr; 78 CHECK_EQ(pthread_getattr_np(pthread_self(), &attr), 0); 79 void *base; 80 size_t size; 81 CHECK_EQ(pthread_attr_getstack(&attr, &base, &size), 0); 82 CHECK_EQ(pthread_attr_destroy(&attr), 0); 83 84 *stack_bottom = reinterpret_cast<uptr>(base); 85 *stack_top = *stack_bottom + size; 86 } 87 88 void InitializePlatformEarly() {} 89 void MaybeReexec() {} 90 void CheckASLR() {} 91 void CheckMPROTECT() {} 92 void PlatformPrepareForSandboxing(__sanitizer_sandbox_arguments *args) {} 93 void DisableCoreDumperIfNecessary() {} 94 void InstallDeadlySignalHandlers(SignalHandlerType handler) {} 95 void SetAlternateSignalStack() {} 96 void UnsetAlternateSignalStack() {} 97 void InitTlsSize() {} 98 99 bool SignalContext::IsStackOverflow() const { return false; } 100 void SignalContext::DumpAllRegisters(void *context) { UNIMPLEMENTED(); } 101 const char *SignalContext::Describe() const { UNIMPLEMENTED(); } 102 103 void FutexWait(atomic_uint32_t *p, u32 cmp) { 104 zx_status_t status = _zx_futex_wait(reinterpret_cast<zx_futex_t *>(p), cmp, 105 ZX_HANDLE_INVALID, ZX_TIME_INFINITE); 106 if (status != ZX_ERR_BAD_STATE) // Normal race. 107 CHECK_EQ(status, ZX_OK); 108 } 109 110 void FutexWake(atomic_uint32_t *p, u32 count) { 111 zx_status_t status = _zx_futex_wake(reinterpret_cast<zx_futex_t *>(p), count); 112 CHECK_EQ(status, ZX_OK); 113 } 114 115 uptr GetPageSize() { return _zx_system_get_page_size(); } 116 117 uptr GetMmapGranularity() { return _zx_system_get_page_size(); } 118 119 sanitizer_shadow_bounds_t ShadowBounds; 120 121 void InitShadowBounds() { ShadowBounds = __sanitizer_shadow_bounds(); } 122 123 uptr GetMaxUserVirtualAddress() { 124 InitShadowBounds(); 125 return ShadowBounds.memory_limit - 1; 126 } 127 128 uptr GetMaxVirtualAddress() { return GetMaxUserVirtualAddress(); } 129 130 static void *DoAnonymousMmapOrDie(uptr size, const char *mem_type, 131 bool raw_report, bool die_for_nomem) { 132 size = RoundUpTo(size, GetPageSize()); 133 134 zx_handle_t vmo; 135 zx_status_t status = _zx_vmo_create(size, 0, &vmo); 136 if (status != ZX_OK) { 137 if (status != ZX_ERR_NO_MEMORY || die_for_nomem) 138 ReportMmapFailureAndDie(size, mem_type, "zx_vmo_create", status, 139 raw_report); 140 return nullptr; 141 } 142 _zx_object_set_property(vmo, ZX_PROP_NAME, mem_type, 143 internal_strlen(mem_type)); 144 145 // TODO(mcgrathr): Maybe allocate a VMAR for all sanitizer heap and use that? 146 uintptr_t addr; 147 status = 148 _zx_vmar_map(_zx_vmar_root_self(), ZX_VM_PERM_READ | ZX_VM_PERM_WRITE, 0, 149 vmo, 0, size, &addr); 150 _zx_handle_close(vmo); 151 152 if (status != ZX_OK) { 153 if (status != ZX_ERR_NO_MEMORY || die_for_nomem) 154 ReportMmapFailureAndDie(size, mem_type, "zx_vmar_map", status, 155 raw_report); 156 return nullptr; 157 } 158 159 IncreaseTotalMmap(size); 160 161 return reinterpret_cast<void *>(addr); 162 } 163 164 void *MmapOrDie(uptr size, const char *mem_type, bool raw_report) { 165 return DoAnonymousMmapOrDie(size, mem_type, raw_report, true); 166 } 167 168 void *MmapNoReserveOrDie(uptr size, const char *mem_type) { 169 return MmapOrDie(size, mem_type); 170 } 171 172 void *MmapOrDieOnFatalError(uptr size, const char *mem_type) { 173 return DoAnonymousMmapOrDie(size, mem_type, false, false); 174 } 175 176 uptr ReservedAddressRange::Init(uptr init_size, const char *name, 177 uptr fixed_addr) { 178 init_size = RoundUpTo(init_size, GetPageSize()); 179 DCHECK_EQ(os_handle_, ZX_HANDLE_INVALID); 180 uintptr_t base; 181 zx_handle_t vmar; 182 zx_status_t status = _zx_vmar_allocate( 183 _zx_vmar_root_self(), 184 ZX_VM_CAN_MAP_READ | ZX_VM_CAN_MAP_WRITE | ZX_VM_CAN_MAP_SPECIFIC, 0, 185 init_size, &vmar, &base); 186 if (status != ZX_OK) 187 ReportMmapFailureAndDie(init_size, name, "zx_vmar_allocate", status); 188 base_ = reinterpret_cast<void *>(base); 189 size_ = init_size; 190 name_ = name; 191 os_handle_ = vmar; 192 193 return reinterpret_cast<uptr>(base_); 194 } 195 196 static uptr DoMmapFixedOrDie(zx_handle_t vmar, uptr fixed_addr, uptr map_size, 197 void *base, const char *name, bool die_for_nomem) { 198 uptr offset = fixed_addr - reinterpret_cast<uptr>(base); 199 map_size = RoundUpTo(map_size, GetPageSize()); 200 zx_handle_t vmo; 201 zx_status_t status = _zx_vmo_create(map_size, 0, &vmo); 202 if (status != ZX_OK) { 203 if (status != ZX_ERR_NO_MEMORY || die_for_nomem) 204 ReportMmapFailureAndDie(map_size, name, "zx_vmo_create", status); 205 return 0; 206 } 207 _zx_object_set_property(vmo, ZX_PROP_NAME, name, internal_strlen(name)); 208 DCHECK_GE(base + size_, map_size + offset); 209 uintptr_t addr; 210 211 status = 212 _zx_vmar_map(vmar, ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | ZX_VM_SPECIFIC, 213 offset, vmo, 0, map_size, &addr); 214 _zx_handle_close(vmo); 215 if (status != ZX_OK) { 216 if (status != ZX_ERR_NO_MEMORY || die_for_nomem) { 217 ReportMmapFailureAndDie(map_size, name, "zx_vmar_map", status); 218 } 219 return 0; 220 } 221 IncreaseTotalMmap(map_size); 222 return addr; 223 } 224 225 uptr ReservedAddressRange::Map(uptr fixed_addr, uptr map_size, 226 const char *name) { 227 return DoMmapFixedOrDie(os_handle_, fixed_addr, map_size, base_, name_, 228 false); 229 } 230 231 uptr ReservedAddressRange::MapOrDie(uptr fixed_addr, uptr map_size, 232 const char *name) { 233 return DoMmapFixedOrDie(os_handle_, fixed_addr, map_size, base_, name_, true); 234 } 235 236 void UnmapOrDieVmar(void *addr, uptr size, zx_handle_t target_vmar) { 237 if (!addr || !size) 238 return; 239 size = RoundUpTo(size, GetPageSize()); 240 241 zx_status_t status = 242 _zx_vmar_unmap(target_vmar, reinterpret_cast<uintptr_t>(addr), size); 243 if (status != ZX_OK) { 244 Report("ERROR: %s failed to deallocate 0x%zx (%zd) bytes at address %p\n", 245 SanitizerToolName, size, size, addr); 246 CHECK("unable to unmap" && 0); 247 } 248 249 DecreaseTotalMmap(size); 250 } 251 252 void ReservedAddressRange::Unmap(uptr addr, uptr size) { 253 CHECK_LE(size, size_); 254 const zx_handle_t vmar = static_cast<zx_handle_t>(os_handle_); 255 if (addr == reinterpret_cast<uptr>(base_)) { 256 if (size == size_) { 257 // Destroying the vmar effectively unmaps the whole mapping. 258 _zx_vmar_destroy(vmar); 259 _zx_handle_close(vmar); 260 os_handle_ = static_cast<uptr>(ZX_HANDLE_INVALID); 261 DecreaseTotalMmap(size); 262 return; 263 } 264 } else { 265 CHECK_EQ(addr + size, reinterpret_cast<uptr>(base_) + size_); 266 } 267 // Partial unmapping does not affect the fact that the initial range is still 268 // reserved, and the resulting unmapped memory can't be reused. 269 UnmapOrDieVmar(reinterpret_cast<void *>(addr), size, vmar); 270 } 271 272 // This should never be called. 273 void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name) { 274 UNIMPLEMENTED(); 275 } 276 277 bool MprotectNoAccess(uptr addr, uptr size) { 278 return _zx_vmar_protect(_zx_vmar_root_self(), 0, addr, size) == ZX_OK; 279 } 280 281 bool MprotectReadOnly(uptr addr, uptr size) { 282 return _zx_vmar_protect(_zx_vmar_root_self(), ZX_VM_PERM_READ, addr, size) == 283 ZX_OK; 284 } 285 286 void *MmapAlignedOrDieOnFatalError(uptr size, uptr alignment, 287 const char *mem_type) { 288 CHECK_GE(size, GetPageSize()); 289 CHECK(IsPowerOfTwo(size)); 290 CHECK(IsPowerOfTwo(alignment)); 291 292 zx_handle_t vmo; 293 zx_status_t status = _zx_vmo_create(size, 0, &vmo); 294 if (status != ZX_OK) { 295 if (status != ZX_ERR_NO_MEMORY) 296 ReportMmapFailureAndDie(size, mem_type, "zx_vmo_create", status, false); 297 return nullptr; 298 } 299 _zx_object_set_property(vmo, ZX_PROP_NAME, mem_type, 300 internal_strlen(mem_type)); 301 302 // TODO(mcgrathr): Maybe allocate a VMAR for all sanitizer heap and use that? 303 304 // Map a larger size to get a chunk of address space big enough that 305 // it surely contains an aligned region of the requested size. Then 306 // overwrite the aligned middle portion with a mapping from the 307 // beginning of the VMO, and unmap the excess before and after. 308 size_t map_size = size + alignment; 309 uintptr_t addr; 310 status = 311 _zx_vmar_map(_zx_vmar_root_self(), ZX_VM_PERM_READ | ZX_VM_PERM_WRITE, 0, 312 vmo, 0, map_size, &addr); 313 if (status == ZX_OK) { 314 uintptr_t map_addr = addr; 315 uintptr_t map_end = map_addr + map_size; 316 addr = RoundUpTo(map_addr, alignment); 317 uintptr_t end = addr + size; 318 if (addr != map_addr) { 319 zx_info_vmar_t info; 320 status = _zx_object_get_info(_zx_vmar_root_self(), ZX_INFO_VMAR, &info, 321 sizeof(info), NULL, NULL); 322 if (status == ZX_OK) { 323 uintptr_t new_addr; 324 status = _zx_vmar_map( 325 _zx_vmar_root_self(), 326 ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | ZX_VM_SPECIFIC_OVERWRITE, 327 addr - info.base, vmo, 0, size, &new_addr); 328 if (status == ZX_OK) 329 CHECK_EQ(new_addr, addr); 330 } 331 } 332 if (status == ZX_OK && addr != map_addr) 333 status = _zx_vmar_unmap(_zx_vmar_root_self(), map_addr, addr - map_addr); 334 if (status == ZX_OK && end != map_end) 335 status = _zx_vmar_unmap(_zx_vmar_root_self(), end, map_end - end); 336 } 337 _zx_handle_close(vmo); 338 339 if (status != ZX_OK) { 340 if (status != ZX_ERR_NO_MEMORY) 341 ReportMmapFailureAndDie(size, mem_type, "zx_vmar_map", status, false); 342 return nullptr; 343 } 344 345 IncreaseTotalMmap(size); 346 347 return reinterpret_cast<void *>(addr); 348 } 349 350 void UnmapOrDie(void *addr, uptr size) { 351 UnmapOrDieVmar(addr, size, _zx_vmar_root_self()); 352 } 353 354 void ReleaseMemoryPagesToOS(uptr beg, uptr end) { 355 uptr beg_aligned = RoundUpTo(beg, GetPageSize()); 356 uptr end_aligned = RoundDownTo(end, GetPageSize()); 357 if (beg_aligned < end_aligned) { 358 zx_handle_t root_vmar = _zx_vmar_root_self(); 359 CHECK_NE(root_vmar, ZX_HANDLE_INVALID); 360 zx_status_t status = 361 _zx_vmar_op_range(root_vmar, ZX_VMAR_OP_DECOMMIT, beg_aligned, 362 end_aligned - beg_aligned, nullptr, 0); 363 CHECK_EQ(status, ZX_OK); 364 } 365 } 366 367 void DumpProcessMap() { 368 // TODO(mcgrathr): write it 369 return; 370 } 371 372 bool IsAccessibleMemoryRange(uptr beg, uptr size) { 373 // TODO(mcgrathr): Figure out a better way. 374 zx_handle_t vmo; 375 zx_status_t status = _zx_vmo_create(size, 0, &vmo); 376 if (status == ZX_OK) { 377 status = _zx_vmo_write(vmo, reinterpret_cast<const void *>(beg), 0, size); 378 _zx_handle_close(vmo); 379 } 380 return status == ZX_OK; 381 } 382 383 // FIXME implement on this platform. 384 void GetMemoryProfile(fill_profile_f cb, uptr *stats) {} 385 386 bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size, 387 uptr *read_len, uptr max_len, error_t *errno_p) { 388 zx_handle_t vmo; 389 zx_status_t status = __sanitizer_get_configuration(file_name, &vmo); 390 if (status == ZX_OK) { 391 uint64_t vmo_size; 392 status = _zx_vmo_get_size(vmo, &vmo_size); 393 if (status == ZX_OK) { 394 if (vmo_size < max_len) 395 max_len = vmo_size; 396 size_t map_size = RoundUpTo(max_len, GetPageSize()); 397 uintptr_t addr; 398 status = _zx_vmar_map(_zx_vmar_root_self(), ZX_VM_PERM_READ, 0, vmo, 0, 399 map_size, &addr); 400 if (status == ZX_OK) { 401 *buff = reinterpret_cast<char *>(addr); 402 *buff_size = map_size; 403 *read_len = max_len; 404 } 405 } 406 _zx_handle_close(vmo); 407 } 408 if (status != ZX_OK && errno_p) 409 *errno_p = status; 410 return status == ZX_OK; 411 } 412 413 void RawWrite(const char *buffer) { 414 constexpr size_t size = 128; 415 static _Thread_local char line[size]; 416 static _Thread_local size_t lastLineEnd = 0; 417 static _Thread_local size_t cur = 0; 418 419 while (*buffer) { 420 if (cur >= size) { 421 if (lastLineEnd == 0) 422 lastLineEnd = size; 423 __sanitizer_log_write(line, lastLineEnd); 424 internal_memmove(line, line + lastLineEnd, cur - lastLineEnd); 425 cur = cur - lastLineEnd; 426 lastLineEnd = 0; 427 } 428 if (*buffer == '\n') 429 lastLineEnd = cur + 1; 430 line[cur++] = *buffer++; 431 } 432 // Flush all complete lines before returning. 433 if (lastLineEnd != 0) { 434 __sanitizer_log_write(line, lastLineEnd); 435 internal_memmove(line, line + lastLineEnd, cur - lastLineEnd); 436 cur = cur - lastLineEnd; 437 lastLineEnd = 0; 438 } 439 } 440 441 void CatastrophicErrorWrite(const char *buffer, uptr length) { 442 __sanitizer_log_write(buffer, length); 443 } 444 445 char **StoredArgv; 446 char **StoredEnviron; 447 448 char **GetArgv() { return StoredArgv; } 449 char **GetEnviron() { return StoredEnviron; } 450 451 const char *GetEnv(const char *name) { 452 if (StoredEnviron) { 453 uptr NameLen = internal_strlen(name); 454 for (char **Env = StoredEnviron; *Env != 0; Env++) { 455 if (internal_strncmp(*Env, name, NameLen) == 0 && (*Env)[NameLen] == '=') 456 return (*Env) + NameLen + 1; 457 } 458 } 459 return nullptr; 460 } 461 462 uptr ReadBinaryName(/*out*/ char *buf, uptr buf_len) { 463 const char *argv0 = "<UNKNOWN>"; 464 if (StoredArgv && StoredArgv[0]) { 465 argv0 = StoredArgv[0]; 466 } 467 internal_strncpy(buf, argv0, buf_len); 468 return internal_strlen(buf); 469 } 470 471 uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len) { 472 return ReadBinaryName(buf, buf_len); 473 } 474 475 uptr MainThreadStackBase, MainThreadStackSize; 476 477 bool GetRandom(void *buffer, uptr length, bool blocking) { 478 CHECK_LE(length, ZX_CPRNG_DRAW_MAX_LEN); 479 _zx_cprng_draw(buffer, length); 480 return true; 481 } 482 483 u32 GetNumberOfCPUs() { return zx_system_get_num_cpus(); } 484 485 uptr GetRSS() { UNIMPLEMENTED(); } 486 487 void *internal_start_thread(void *(*func)(void *arg), void *arg) { return 0; } 488 void internal_join_thread(void *th) {} 489 490 void InitializePlatformCommonFlags(CommonFlags *cf) {} 491 492 } // namespace __sanitizer 493 494 using namespace __sanitizer; 495 496 extern "C" { 497 void __sanitizer_startup_hook(int argc, char **argv, char **envp, 498 void *stack_base, size_t stack_size) { 499 __sanitizer::StoredArgv = argv; 500 __sanitizer::StoredEnviron = envp; 501 __sanitizer::MainThreadStackBase = reinterpret_cast<uintptr_t>(stack_base); 502 __sanitizer::MainThreadStackSize = stack_size; 503 } 504 505 void __sanitizer_set_report_path(const char *path) { 506 // Handle the initialization code in each sanitizer, but no other calls. 507 // This setting is never consulted on Fuchsia. 508 DCHECK_EQ(path, common_flags()->log_path); 509 } 510 511 void __sanitizer_set_report_fd(void *fd) { 512 UNREACHABLE("not available on Fuchsia"); 513 } 514 515 const char *__sanitizer_get_report_path() { 516 UNREACHABLE("not available on Fuchsia"); 517 } 518 } // extern "C" 519 520 #endif // SANITIZER_FUCHSIA 521