fuchsia.cpp 7.1 KB

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