sanitizer_posix.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. //===-- sanitizer_posix.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 ThreadSanitizer
  10. // run-time libraries and implements POSIX-specific functions from
  11. // sanitizer_posix.h.
  12. //===----------------------------------------------------------------------===//
  13. #include "sanitizer_platform.h"
  14. #if SANITIZER_POSIX
  15. #include "sanitizer_common.h"
  16. #include "sanitizer_file.h"
  17. #include "sanitizer_flags.h"
  18. #include "sanitizer_libc.h"
  19. #include "sanitizer_posix.h"
  20. #include "sanitizer_procmaps.h"
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #include <signal.h>
  24. #include <sys/mman.h>
  25. #if SANITIZER_FREEBSD
  26. // The MAP_NORESERVE define has been removed in FreeBSD 11.x, and even before
  27. // that, it was never implemented. So just define it to zero.
  28. #undef MAP_NORESERVE
  29. #define MAP_NORESERVE 0
  30. #endif
  31. namespace __sanitizer {
  32. // ------------- sanitizer_common.h
  33. uptr GetMmapGranularity() {
  34. return GetPageSize();
  35. }
  36. bool ErrorIsOOM(error_t err) { return err == ENOMEM; }
  37. void *MmapOrDie(uptr size, const char *mem_type, bool raw_report) {
  38. size = RoundUpTo(size, GetPageSizeCached());
  39. uptr res = MmapNamed(nullptr, size, PROT_READ | PROT_WRITE,
  40. MAP_PRIVATE | MAP_ANON, mem_type);
  41. int reserrno;
  42. if (UNLIKELY(internal_iserror(res, &reserrno)))
  43. ReportMmapFailureAndDie(size, mem_type, "allocate", reserrno, raw_report);
  44. IncreaseTotalMmap(size);
  45. return (void *)res;
  46. }
  47. void UnmapOrDie(void *addr, uptr size) {
  48. if (!addr || !size) return;
  49. uptr res = internal_munmap(addr, size);
  50. int reserrno;
  51. if (UNLIKELY(internal_iserror(res, &reserrno)))
  52. ReportMunmapFailureAndDie(addr, size, reserrno);
  53. DecreaseTotalMmap(size);
  54. }
  55. void *MmapOrDieOnFatalError(uptr size, const char *mem_type) {
  56. size = RoundUpTo(size, GetPageSizeCached());
  57. uptr res = MmapNamed(nullptr, size, PROT_READ | PROT_WRITE,
  58. MAP_PRIVATE | MAP_ANON, mem_type);
  59. int reserrno;
  60. if (UNLIKELY(internal_iserror(res, &reserrno))) {
  61. if (reserrno == ENOMEM)
  62. return nullptr;
  63. ReportMmapFailureAndDie(size, mem_type, "allocate", reserrno);
  64. }
  65. IncreaseTotalMmap(size);
  66. return (void *)res;
  67. }
  68. // We want to map a chunk of address space aligned to 'alignment'.
  69. // We do it by mapping a bit more and then unmapping redundant pieces.
  70. // We probably can do it with fewer syscalls in some OS-dependent way.
  71. void *MmapAlignedOrDieOnFatalError(uptr size, uptr alignment,
  72. const char *mem_type) {
  73. CHECK(IsPowerOfTwo(size));
  74. CHECK(IsPowerOfTwo(alignment));
  75. uptr map_size = size + alignment;
  76. // mmap maps entire pages and rounds up map_size needs to be a an integral
  77. // number of pages.
  78. // We need to be aware of this size for calculating end and for unmapping
  79. // fragments before and after the alignment region.
  80. map_size = RoundUpTo(map_size, GetPageSizeCached());
  81. uptr map_res = (uptr)MmapOrDieOnFatalError(map_size, mem_type);
  82. if (UNLIKELY(!map_res))
  83. return nullptr;
  84. uptr res = map_res;
  85. if (!IsAligned(res, alignment)) {
  86. res = (map_res + alignment - 1) & ~(alignment - 1);
  87. UnmapOrDie((void*)map_res, res - map_res);
  88. }
  89. uptr map_end = map_res + map_size;
  90. uptr end = res + size;
  91. end = RoundUpTo(end, GetPageSizeCached());
  92. if (end != map_end) {
  93. CHECK_LT(end, map_end);
  94. UnmapOrDie((void*)end, map_end - end);
  95. }
  96. return (void*)res;
  97. }
  98. void *MmapNoReserveOrDie(uptr size, const char *mem_type) {
  99. size = RoundUpTo(size, GetPageSizeCached());
  100. uptr p = MmapNamed(nullptr, size, PROT_READ | PROT_WRITE,
  101. MAP_PRIVATE | MAP_ANON | MAP_NORESERVE, mem_type);
  102. int reserrno;
  103. if (UNLIKELY(internal_iserror(p, &reserrno)))
  104. ReportMmapFailureAndDie(size, mem_type, "allocate noreserve", reserrno);
  105. IncreaseTotalMmap(size);
  106. return (void *)p;
  107. }
  108. static void *MmapFixedImpl(uptr fixed_addr, uptr size, bool tolerate_enomem,
  109. const char *name) {
  110. size = RoundUpTo(size, GetPageSizeCached());
  111. fixed_addr = RoundDownTo(fixed_addr, GetPageSizeCached());
  112. uptr p = MmapNamed((void *)fixed_addr, size, PROT_READ | PROT_WRITE,
  113. MAP_PRIVATE | MAP_ANON | MAP_FIXED, name);
  114. int reserrno;
  115. if (UNLIKELY(internal_iserror(p, &reserrno))) {
  116. if (tolerate_enomem && reserrno == ENOMEM)
  117. return nullptr;
  118. char mem_type[40];
  119. internal_snprintf(mem_type, sizeof(mem_type), "memory at address 0x%zx",
  120. fixed_addr);
  121. ReportMmapFailureAndDie(size, mem_type, "allocate", reserrno);
  122. }
  123. IncreaseTotalMmap(size);
  124. return (void *)p;
  125. }
  126. void *MmapFixedOrDie(uptr fixed_addr, uptr size, const char *name) {
  127. return MmapFixedImpl(fixed_addr, size, false /*tolerate_enomem*/, name);
  128. }
  129. void *MmapFixedOrDieOnFatalError(uptr fixed_addr, uptr size, const char *name) {
  130. return MmapFixedImpl(fixed_addr, size, true /*tolerate_enomem*/, name);
  131. }
  132. bool MprotectNoAccess(uptr addr, uptr size) {
  133. return 0 == internal_mprotect((void*)addr, size, PROT_NONE);
  134. }
  135. bool MprotectReadOnly(uptr addr, uptr size) {
  136. return 0 == internal_mprotect((void *)addr, size, PROT_READ);
  137. }
  138. bool MprotectReadWrite(uptr addr, uptr size) {
  139. return 0 == internal_mprotect((void *)addr, size, PROT_READ | PROT_WRITE);
  140. }
  141. #if !SANITIZER_APPLE
  142. void MprotectMallocZones(void *addr, int prot) {}
  143. #endif
  144. fd_t OpenFile(const char *filename, FileAccessMode mode, error_t *errno_p) {
  145. if (ShouldMockFailureToOpen(filename))
  146. return kInvalidFd;
  147. int flags;
  148. switch (mode) {
  149. case RdOnly: flags = O_RDONLY; break;
  150. case WrOnly: flags = O_WRONLY | O_CREAT | O_TRUNC; break;
  151. case RdWr: flags = O_RDWR | O_CREAT; break;
  152. }
  153. fd_t res = internal_open(filename, flags, 0660);
  154. if (internal_iserror(res, errno_p))
  155. return kInvalidFd;
  156. return ReserveStandardFds(res);
  157. }
  158. void CloseFile(fd_t fd) {
  159. internal_close(fd);
  160. }
  161. bool ReadFromFile(fd_t fd, void *buff, uptr buff_size, uptr *bytes_read,
  162. error_t *error_p) {
  163. uptr res = internal_read(fd, buff, buff_size);
  164. if (internal_iserror(res, error_p))
  165. return false;
  166. if (bytes_read)
  167. *bytes_read = res;
  168. return true;
  169. }
  170. bool WriteToFile(fd_t fd, const void *buff, uptr buff_size, uptr *bytes_written,
  171. error_t *error_p) {
  172. uptr res = internal_write(fd, buff, buff_size);
  173. if (internal_iserror(res, error_p))
  174. return false;
  175. if (bytes_written)
  176. *bytes_written = res;
  177. return true;
  178. }
  179. void *MapFileToMemory(const char *file_name, uptr *buff_size) {
  180. fd_t fd = OpenFile(file_name, RdOnly);
  181. CHECK(fd != kInvalidFd);
  182. uptr fsize = internal_filesize(fd);
  183. CHECK_NE(fsize, (uptr)-1);
  184. CHECK_GT(fsize, 0);
  185. *buff_size = RoundUpTo(fsize, GetPageSizeCached());
  186. uptr map = internal_mmap(nullptr, *buff_size, PROT_READ, MAP_PRIVATE, fd, 0);
  187. return internal_iserror(map) ? nullptr : (void *)map;
  188. }
  189. void *MapWritableFileToMemory(void *addr, uptr size, fd_t fd, OFF_T offset) {
  190. uptr flags = MAP_SHARED;
  191. if (addr) flags |= MAP_FIXED;
  192. uptr p = internal_mmap(addr, size, PROT_READ | PROT_WRITE, flags, fd, offset);
  193. int mmap_errno = 0;
  194. if (internal_iserror(p, &mmap_errno)) {
  195. Printf("could not map writable file (%d, %lld, %zu): %zd, errno: %d\n",
  196. fd, (long long)offset, size, p, mmap_errno);
  197. return nullptr;
  198. }
  199. return (void *)p;
  200. }
  201. static inline bool IntervalsAreSeparate(uptr start1, uptr end1,
  202. uptr start2, uptr end2) {
  203. CHECK(start1 <= end1);
  204. CHECK(start2 <= end2);
  205. return (end1 < start2) || (end2 < start1);
  206. }
  207. // FIXME: this is thread-unsafe, but should not cause problems most of the time.
  208. // When the shadow is mapped only a single thread usually exists (plus maybe
  209. // several worker threads on Mac, which aren't expected to map big chunks of
  210. // memory).
  211. bool MemoryRangeIsAvailable(uptr range_start, uptr range_end) {
  212. MemoryMappingLayout proc_maps(/*cache_enabled*/true);
  213. if (proc_maps.Error())
  214. return true; // and hope for the best
  215. MemoryMappedSegment segment;
  216. while (proc_maps.Next(&segment)) {
  217. if (segment.start == segment.end) continue; // Empty range.
  218. CHECK_NE(0, segment.end);
  219. if (!IntervalsAreSeparate(segment.start, segment.end - 1, range_start,
  220. range_end))
  221. return false;
  222. }
  223. return true;
  224. }
  225. #if !SANITIZER_APPLE
  226. void DumpProcessMap() {
  227. MemoryMappingLayout proc_maps(/*cache_enabled*/true);
  228. const sptr kBufSize = 4095;
  229. char *filename = (char*)MmapOrDie(kBufSize, __func__);
  230. MemoryMappedSegment segment(filename, kBufSize);
  231. Report("Process memory map follows:\n");
  232. while (proc_maps.Next(&segment)) {
  233. Printf("\t%p-%p\t%s\n", (void *)segment.start, (void *)segment.end,
  234. segment.filename);
  235. }
  236. Report("End of process memory map.\n");
  237. UnmapOrDie(filename, kBufSize);
  238. }
  239. #endif
  240. const char *GetPwd() {
  241. return GetEnv("PWD");
  242. }
  243. bool IsPathSeparator(const char c) {
  244. return c == '/';
  245. }
  246. bool IsAbsolutePath(const char *path) {
  247. return path != nullptr && IsPathSeparator(path[0]);
  248. }
  249. void ReportFile::Write(const char *buffer, uptr length) {
  250. SpinMutexLock l(mu);
  251. ReopenIfNecessary();
  252. internal_write(fd, buffer, length);
  253. }
  254. bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end) {
  255. MemoryMappingLayout proc_maps(/*cache_enabled*/false);
  256. InternalMmapVector<char> buff(kMaxPathLength);
  257. MemoryMappedSegment segment(buff.data(), buff.size());
  258. while (proc_maps.Next(&segment)) {
  259. if (segment.IsExecutable() &&
  260. internal_strcmp(module, segment.filename) == 0) {
  261. *start = segment.start;
  262. *end = segment.end;
  263. return true;
  264. }
  265. }
  266. return false;
  267. }
  268. uptr SignalContext::GetAddress() const {
  269. auto si = static_cast<const siginfo_t *>(siginfo);
  270. return (uptr)si->si_addr;
  271. }
  272. bool SignalContext::IsMemoryAccess() const {
  273. auto si = static_cast<const siginfo_t *>(siginfo);
  274. return si->si_signo == SIGSEGV || si->si_signo == SIGBUS;
  275. }
  276. int SignalContext::GetType() const {
  277. return static_cast<const siginfo_t *>(siginfo)->si_signo;
  278. }
  279. const char *SignalContext::Describe() const {
  280. switch (GetType()) {
  281. case SIGFPE:
  282. return "FPE";
  283. case SIGILL:
  284. return "ILL";
  285. case SIGABRT:
  286. return "ABRT";
  287. case SIGSEGV:
  288. return "SEGV";
  289. case SIGBUS:
  290. return "BUS";
  291. case SIGTRAP:
  292. return "TRAP";
  293. }
  294. return "UNKNOWN SIGNAL";
  295. }
  296. fd_t ReserveStandardFds(fd_t fd) {
  297. CHECK_GE(fd, 0);
  298. if (fd > 2)
  299. return fd;
  300. bool used[3];
  301. internal_memset(used, 0, sizeof(used));
  302. while (fd <= 2) {
  303. used[fd] = true;
  304. fd = internal_dup(fd);
  305. }
  306. for (int i = 0; i <= 2; ++i)
  307. if (used[i])
  308. internal_close(i);
  309. return fd;
  310. }
  311. bool ShouldMockFailureToOpen(const char *path) {
  312. return common_flags()->test_only_emulate_no_memorymap &&
  313. internal_strncmp(path, "/proc/", 6) == 0;
  314. }
  315. #if SANITIZER_LINUX && !SANITIZER_ANDROID && !SANITIZER_GO
  316. int GetNamedMappingFd(const char *name, uptr size, int *flags) {
  317. if (!common_flags()->decorate_proc_maps || !name)
  318. return -1;
  319. char shmname[200];
  320. CHECK(internal_strlen(name) < sizeof(shmname) - 10);
  321. internal_snprintf(shmname, sizeof(shmname), "/dev/shm/%zu [%s]",
  322. internal_getpid(), name);
  323. int o_cloexec = 0;
  324. #if defined(O_CLOEXEC)
  325. o_cloexec = O_CLOEXEC;
  326. #endif
  327. int fd = ReserveStandardFds(
  328. internal_open(shmname, O_RDWR | O_CREAT | O_TRUNC | o_cloexec, S_IRWXU));
  329. CHECK_GE(fd, 0);
  330. int res = internal_ftruncate(fd, size);
  331. #if !defined(O_CLOEXEC)
  332. res = fcntl(fd, F_SETFD, FD_CLOEXEC);
  333. CHECK_EQ(0, res);
  334. #endif
  335. CHECK_EQ(0, res);
  336. res = internal_unlink(shmname);
  337. CHECK_EQ(0, res);
  338. *flags &= ~(MAP_ANON | MAP_ANONYMOUS);
  339. return fd;
  340. }
  341. #else
  342. int GetNamedMappingFd(const char *name, uptr size, int *flags) {
  343. return -1;
  344. }
  345. #endif
  346. #if SANITIZER_ANDROID
  347. #define PR_SET_VMA 0x53564d41
  348. #define PR_SET_VMA_ANON_NAME 0
  349. void DecorateMapping(uptr addr, uptr size, const char *name) {
  350. if (!common_flags()->decorate_proc_maps || !name)
  351. return;
  352. internal_prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, addr, size, (uptr)name);
  353. }
  354. #else
  355. void DecorateMapping(uptr addr, uptr size, const char *name) {
  356. }
  357. #endif
  358. uptr MmapNamed(void *addr, uptr length, int prot, int flags, const char *name) {
  359. int fd = GetNamedMappingFd(name, length, &flags);
  360. uptr res = internal_mmap(addr, length, prot, flags, fd, 0);
  361. if (!internal_iserror(res))
  362. DecorateMapping(res, length, name);
  363. return res;
  364. }
  365. } // namespace __sanitizer
  366. #endif // SANITIZER_POSIX