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 void *MmapAlignedOrDieOnFatalError(uptr size, uptr alignment, 278 const char *mem_type) { 279 CHECK_GE(size, GetPageSize()); 280 CHECK(IsPowerOfTwo(size)); 281 CHECK(IsPowerOfTwo(alignment)); 282 283 zx_handle_t vmo; 284 zx_status_t status = _zx_vmo_create(size, 0, &vmo); 285 if (status != ZX_OK) { 286 if (status != ZX_ERR_NO_MEMORY) 287 ReportMmapFailureAndDie(size, mem_type, "zx_vmo_create", status, false); 288 return nullptr; 289 } 290 _zx_object_set_property(vmo, ZX_PROP_NAME, mem_type, 291 internal_strlen(mem_type)); 292 293 // TODO(mcgrathr): Maybe allocate a VMAR for all sanitizer heap and use that? 294 295 // Map a larger size to get a chunk of address space big enough that 296 // it surely contains an aligned region of the requested size. Then 297 // overwrite the aligned middle portion with a mapping from the 298 // beginning of the VMO, and unmap the excess before and after. 299 size_t map_size = size + alignment; 300 uintptr_t addr; 301 status = 302 _zx_vmar_map(_zx_vmar_root_self(), ZX_VM_PERM_READ | ZX_VM_PERM_WRITE, 0, 303 vmo, 0, map_size, &addr); 304 if (status == ZX_OK) { 305 uintptr_t map_addr = addr; 306 uintptr_t map_end = map_addr + map_size; 307 addr = RoundUpTo(map_addr, alignment); 308 uintptr_t end = addr + size; 309 if (addr != map_addr) { 310 zx_info_vmar_t info; 311 status = _zx_object_get_info(_zx_vmar_root_self(), ZX_INFO_VMAR, &info, 312 sizeof(info), NULL, NULL); 313 if (status == ZX_OK) { 314 uintptr_t new_addr; 315 status = _zx_vmar_map( 316 _zx_vmar_root_self(), 317 ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | ZX_VM_SPECIFIC_OVERWRITE, 318 addr - info.base, vmo, 0, size, &new_addr); 319 if (status == ZX_OK) 320 CHECK_EQ(new_addr, addr); 321 } 322 } 323 if (status == ZX_OK && addr != map_addr) 324 status = _zx_vmar_unmap(_zx_vmar_root_self(), map_addr, addr - map_addr); 325 if (status == ZX_OK && end != map_end) 326 status = _zx_vmar_unmap(_zx_vmar_root_self(), end, map_end - end); 327 } 328 _zx_handle_close(vmo); 329 330 if (status != ZX_OK) { 331 if (status != ZX_ERR_NO_MEMORY) 332 ReportMmapFailureAndDie(size, mem_type, "zx_vmar_map", status, false); 333 return nullptr; 334 } 335 336 IncreaseTotalMmap(size); 337 338 return reinterpret_cast<void *>(addr); 339 } 340 341 void UnmapOrDie(void *addr, uptr size) { 342 UnmapOrDieVmar(addr, size, _zx_vmar_root_self()); 343 } 344 345 void ReleaseMemoryPagesToOS(uptr beg, uptr end) { 346 uptr beg_aligned = RoundUpTo(beg, GetPageSize()); 347 uptr end_aligned = RoundDownTo(end, GetPageSize()); 348 if (beg_aligned < end_aligned) { 349 zx_handle_t root_vmar = _zx_vmar_root_self(); 350 CHECK_NE(root_vmar, ZX_HANDLE_INVALID); 351 zx_status_t status = 352 _zx_vmar_op_range(root_vmar, ZX_VMAR_OP_DECOMMIT, beg_aligned, 353 end_aligned - beg_aligned, nullptr, 0); 354 CHECK_EQ(status, ZX_OK); 355 } 356 } 357 358 void DumpProcessMap() { 359 // TODO(mcgrathr): write it 360 return; 361 } 362 363 bool IsAccessibleMemoryRange(uptr beg, uptr size) { 364 // TODO(mcgrathr): Figure out a better way. 365 zx_handle_t vmo; 366 zx_status_t status = _zx_vmo_create(size, 0, &vmo); 367 if (status == ZX_OK) { 368 status = _zx_vmo_write(vmo, reinterpret_cast<const void *>(beg), 0, size); 369 _zx_handle_close(vmo); 370 } 371 return status == ZX_OK; 372 } 373 374 // FIXME implement on this platform. 375 void GetMemoryProfile(fill_profile_f cb, uptr *stats) {} 376 377 bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size, 378 uptr *read_len, uptr max_len, error_t *errno_p) { 379 zx_handle_t vmo; 380 zx_status_t status = __sanitizer_get_configuration(file_name, &vmo); 381 if (status == ZX_OK) { 382 uint64_t vmo_size; 383 status = _zx_vmo_get_size(vmo, &vmo_size); 384 if (status == ZX_OK) { 385 if (vmo_size < max_len) 386 max_len = vmo_size; 387 size_t map_size = RoundUpTo(max_len, GetPageSize()); 388 uintptr_t addr; 389 status = _zx_vmar_map(_zx_vmar_root_self(), ZX_VM_PERM_READ, 0, vmo, 0, 390 map_size, &addr); 391 if (status == ZX_OK) { 392 *buff = reinterpret_cast<char *>(addr); 393 *buff_size = map_size; 394 *read_len = max_len; 395 } 396 } 397 _zx_handle_close(vmo); 398 } 399 if (status != ZX_OK && errno_p) 400 *errno_p = status; 401 return status == ZX_OK; 402 } 403 404 void RawWrite(const char *buffer) { 405 constexpr size_t size = 128; 406 static _Thread_local char line[size]; 407 static _Thread_local size_t lastLineEnd = 0; 408 static _Thread_local size_t cur = 0; 409 410 while (*buffer) { 411 if (cur >= size) { 412 if (lastLineEnd == 0) 413 lastLineEnd = size; 414 __sanitizer_log_write(line, lastLineEnd); 415 internal_memmove(line, line + lastLineEnd, cur - lastLineEnd); 416 cur = cur - lastLineEnd; 417 lastLineEnd = 0; 418 } 419 if (*buffer == '\n') 420 lastLineEnd = cur + 1; 421 line[cur++] = *buffer++; 422 } 423 // Flush all complete lines before returning. 424 if (lastLineEnd != 0) { 425 __sanitizer_log_write(line, lastLineEnd); 426 internal_memmove(line, line + lastLineEnd, cur - lastLineEnd); 427 cur = cur - lastLineEnd; 428 lastLineEnd = 0; 429 } 430 } 431 432 void CatastrophicErrorWrite(const char *buffer, uptr length) { 433 __sanitizer_log_write(buffer, length); 434 } 435 436 char **StoredArgv; 437 char **StoredEnviron; 438 439 char **GetArgv() { return StoredArgv; } 440 char **GetEnviron() { return StoredEnviron; } 441 442 const char *GetEnv(const char *name) { 443 if (StoredEnviron) { 444 uptr NameLen = internal_strlen(name); 445 for (char **Env = StoredEnviron; *Env != 0; Env++) { 446 if (internal_strncmp(*Env, name, NameLen) == 0 && (*Env)[NameLen] == '=') 447 return (*Env) + NameLen + 1; 448 } 449 } 450 return nullptr; 451 } 452 453 uptr ReadBinaryName(/*out*/ char *buf, uptr buf_len) { 454 const char *argv0 = "<UNKNOWN>"; 455 if (StoredArgv && StoredArgv[0]) { 456 argv0 = StoredArgv[0]; 457 } 458 internal_strncpy(buf, argv0, buf_len); 459 return internal_strlen(buf); 460 } 461 462 uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len) { 463 return ReadBinaryName(buf, buf_len); 464 } 465 466 uptr MainThreadStackBase, MainThreadStackSize; 467 468 bool GetRandom(void *buffer, uptr length, bool blocking) { 469 CHECK_LE(length, ZX_CPRNG_DRAW_MAX_LEN); 470 _zx_cprng_draw(buffer, length); 471 return true; 472 } 473 474 u32 GetNumberOfCPUs() { return zx_system_get_num_cpus(); } 475 476 uptr GetRSS() { UNIMPLEMENTED(); } 477 478 void InitializePlatformCommonFlags(CommonFlags *cf) {} 479 480 } // namespace __sanitizer 481 482 using namespace __sanitizer; 483 484 extern "C" { 485 void __sanitizer_startup_hook(int argc, char **argv, char **envp, 486 void *stack_base, size_t stack_size) { 487 __sanitizer::StoredArgv = argv; 488 __sanitizer::StoredEnviron = envp; 489 __sanitizer::MainThreadStackBase = reinterpret_cast<uintptr_t>(stack_base); 490 __sanitizer::MainThreadStackSize = stack_size; 491 } 492 493 void __sanitizer_set_report_path(const char *path) { 494 // Handle the initialization code in each sanitizer, but no other calls. 495 // This setting is never consulted on Fuchsia. 496 DCHECK_EQ(path, common_flags()->log_path); 497 } 498 499 void __sanitizer_set_report_fd(void *fd) { 500 UNREACHABLE("not available on Fuchsia"); 501 } 502 503 const char *__sanitizer_get_report_path() { 504 UNREACHABLE("not available on Fuchsia"); 505 } 506 } // extern "C" 507 508 #endif // SANITIZER_FUCHSIA 509