fuchsia.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //===-- fuchsia.cpp ---------------------------------------------*- C++ -*-===//
  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. #include "platform.h"
  9. #if SCUDO_FUCHSIA
  10. #include "common.h"
  11. #include "mutex.h"
  12. #include "string_utils.h"
  13. #error #include <lib/sync/mutex.h> // for sync_mutex_t
  14. #include <stdlib.h> // for getenv()
  15. #error #include <zircon/compiler.h>
  16. #error #include <zircon/sanitizer.h>
  17. #error #include <zircon/syscalls.h>
  18. namespace scudo {
  19. uptr getPageSize() { return _zx_system_get_page_size(); }
  20. void NORETURN die() { __builtin_trap(); }
  21. // We zero-initialize the Extra parameter of map(), make sure this is consistent
  22. // with ZX_HANDLE_INVALID.
  23. static_assert(ZX_HANDLE_INVALID == 0, "");
  24. static void *allocateVmar(uptr Size, MapPlatformData *Data, bool AllowNoMem) {
  25. // Only scenario so far.
  26. DCHECK(Data);
  27. DCHECK_EQ(Data->Vmar, ZX_HANDLE_INVALID);
  28. const zx_status_t Status = _zx_vmar_allocate(
  29. _zx_vmar_root_self(),
  30. ZX_VM_CAN_MAP_READ | ZX_VM_CAN_MAP_WRITE | ZX_VM_CAN_MAP_SPECIFIC, 0,
  31. Size, &Data->Vmar, &Data->VmarBase);
  32. if (UNLIKELY(Status != ZX_OK)) {
  33. if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem)
  34. dieOnMapUnmapError(Status == ZX_ERR_NO_MEMORY ? Size : 0);
  35. return nullptr;
  36. }
  37. return reinterpret_cast<void *>(Data->VmarBase);
  38. }
  39. void *map(void *Addr, uptr Size, const char *Name, uptr Flags,
  40. MapPlatformData *Data) {
  41. DCHECK_EQ(Size % getPageSizeCached(), 0);
  42. const bool AllowNoMem = !!(Flags & MAP_ALLOWNOMEM);
  43. // For MAP_NOACCESS, just allocate a Vmar and return.
  44. if (Flags & MAP_NOACCESS)
  45. return allocateVmar(Size, Data, AllowNoMem);
  46. const zx_handle_t Vmar = Data ? Data->Vmar : _zx_vmar_root_self();
  47. CHECK_NE(Vmar, ZX_HANDLE_INVALID);
  48. zx_status_t Status;
  49. zx_handle_t Vmo;
  50. uint64_t VmoSize = 0;
  51. if (Data && Data->Vmo != ZX_HANDLE_INVALID) {
  52. // If a Vmo was specified, it's a resize operation.
  53. CHECK(Addr);
  54. DCHECK(Flags & MAP_RESIZABLE);
  55. Vmo = Data->Vmo;
  56. VmoSize = Data->VmoSize;
  57. Status = _zx_vmo_set_size(Vmo, VmoSize + Size);
  58. if (Status != ZX_OK) {
  59. if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem)
  60. dieOnMapUnmapError(Status == ZX_ERR_NO_MEMORY ? Size : 0);
  61. return nullptr;
  62. }
  63. } else {
  64. // Otherwise, create a Vmo and set its name.
  65. Status = _zx_vmo_create(Size, ZX_VMO_RESIZABLE, &Vmo);
  66. if (UNLIKELY(Status != ZX_OK)) {
  67. if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem)
  68. dieOnMapUnmapError(Status == ZX_ERR_NO_MEMORY ? Size : 0);
  69. return nullptr;
  70. }
  71. _zx_object_set_property(Vmo, ZX_PROP_NAME, Name, strlen(Name));
  72. }
  73. uintptr_t P;
  74. zx_vm_option_t MapFlags =
  75. ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | ZX_VM_ALLOW_FAULTS;
  76. const uint64_t Offset =
  77. Addr ? reinterpret_cast<uintptr_t>(Addr) - Data->VmarBase : 0;
  78. if (Offset)
  79. MapFlags |= ZX_VM_SPECIFIC;
  80. Status = _zx_vmar_map(Vmar, MapFlags, Offset, Vmo, VmoSize, Size, &P);
  81. // No need to track the Vmo if we don't intend on resizing it. Close it.
  82. if (Flags & MAP_RESIZABLE) {
  83. DCHECK(Data);
  84. if (Data->Vmo == ZX_HANDLE_INVALID)
  85. Data->Vmo = Vmo;
  86. else
  87. DCHECK_EQ(Data->Vmo, Vmo);
  88. } else {
  89. CHECK_EQ(_zx_handle_close(Vmo), ZX_OK);
  90. }
  91. if (UNLIKELY(Status != ZX_OK)) {
  92. if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem)
  93. dieOnMapUnmapError(Status == ZX_ERR_NO_MEMORY ? Size : 0);
  94. return nullptr;
  95. }
  96. if (Data)
  97. Data->VmoSize += Size;
  98. return reinterpret_cast<void *>(P);
  99. }
  100. void unmap(void *Addr, uptr Size, uptr Flags, MapPlatformData *Data) {
  101. if (Flags & UNMAP_ALL) {
  102. DCHECK_NE(Data, nullptr);
  103. const zx_handle_t Vmar = Data->Vmar;
  104. DCHECK_NE(Vmar, _zx_vmar_root_self());
  105. // Destroying the vmar effectively unmaps the whole mapping.
  106. CHECK_EQ(_zx_vmar_destroy(Vmar), ZX_OK);
  107. CHECK_EQ(_zx_handle_close(Vmar), ZX_OK);
  108. } else {
  109. const zx_handle_t Vmar = Data ? Data->Vmar : _zx_vmar_root_self();
  110. const zx_status_t Status =
  111. _zx_vmar_unmap(Vmar, reinterpret_cast<uintptr_t>(Addr), Size);
  112. if (UNLIKELY(Status != ZX_OK))
  113. dieOnMapUnmapError();
  114. }
  115. if (Data) {
  116. if (Data->Vmo != ZX_HANDLE_INVALID)
  117. CHECK_EQ(_zx_handle_close(Data->Vmo), ZX_OK);
  118. memset(Data, 0, sizeof(*Data));
  119. }
  120. }
  121. void setMemoryPermission(UNUSED uptr Addr, UNUSED uptr Size, UNUSED uptr Flags,
  122. UNUSED MapPlatformData *Data) {
  123. const zx_vm_option_t Prot =
  124. (Flags & MAP_NOACCESS) ? 0 : (ZX_VM_PERM_READ | ZX_VM_PERM_WRITE);
  125. DCHECK(Data);
  126. DCHECK_NE(Data->Vmar, ZX_HANDLE_INVALID);
  127. if (_zx_vmar_protect(Data->Vmar, Prot, Addr, Size) != ZX_OK)
  128. dieOnMapUnmapError();
  129. }
  130. void releasePagesToOS(UNUSED uptr BaseAddress, uptr Offset, uptr Size,
  131. MapPlatformData *Data) {
  132. DCHECK(Data);
  133. DCHECK_NE(Data->Vmar, ZX_HANDLE_INVALID);
  134. DCHECK_NE(Data->Vmo, ZX_HANDLE_INVALID);
  135. const zx_status_t Status =
  136. _zx_vmo_op_range(Data->Vmo, ZX_VMO_OP_DECOMMIT, Offset, Size, NULL, 0);
  137. CHECK_EQ(Status, ZX_OK);
  138. }
  139. const char *getEnv(const char *Name) { return getenv(Name); }
  140. // Note: we need to flag these methods with __TA_NO_THREAD_SAFETY_ANALYSIS
  141. // because the Fuchsia implementation of sync_mutex_t has clang thread safety
  142. // annotations. Were we to apply proper capability annotations to the top level
  143. // HybridMutex class itself, they would not be needed. As it stands, the
  144. // thread analysis thinks that we are locking the mutex and accidentally leaving
  145. // it locked on the way out.
  146. bool HybridMutex::tryLock() __TA_NO_THREAD_SAFETY_ANALYSIS {
  147. // Size and alignment must be compatible between both types.
  148. return sync_mutex_trylock(&M) == ZX_OK;
  149. }
  150. void HybridMutex::lockSlow() __TA_NO_THREAD_SAFETY_ANALYSIS {
  151. sync_mutex_lock(&M);
  152. }
  153. void HybridMutex::unlock() __TA_NO_THREAD_SAFETY_ANALYSIS {
  154. sync_mutex_unlock(&M);
  155. }
  156. u64 getMonotonicTime() { return _zx_clock_get_monotonic(); }
  157. u32 getNumberOfCPUs() { return _zx_system_get_num_cpus(); }
  158. u32 getThreadID() { return 0; }
  159. bool getRandom(void *Buffer, uptr Length, UNUSED bool Blocking) {
  160. static_assert(MaxRandomLength <= ZX_CPRNG_DRAW_MAX_LEN, "");
  161. if (UNLIKELY(!Buffer || !Length || Length > MaxRandomLength))
  162. return false;
  163. _zx_cprng_draw(Buffer, Length);
  164. return true;
  165. }
  166. void outputRaw(const char *Buffer) {
  167. __sanitizer_log_write(Buffer, strlen(Buffer));
  168. }
  169. void setAbortMessage(const char *Message) {}
  170. } // namespace scudo
  171. #endif // SCUDO_FUCHSIA