fuchsia.cpp 7.7 KB

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