sanitizer_mac.cpp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  1. //===-- sanitizer_mac.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 various sanitizers' runtime libraries and
  10. // implements OSX-specific functions.
  11. //===----------------------------------------------------------------------===//
  12. #include "sanitizer_platform.h"
  13. #if SANITIZER_MAC
  14. #include "sanitizer_mac.h"
  15. #include "interception/interception.h"
  16. // Use 64-bit inodes in file operations. ASan does not support OS X 10.5, so
  17. // the clients will most certainly use 64-bit ones as well.
  18. #ifndef _DARWIN_USE_64_BIT_INODE
  19. #define _DARWIN_USE_64_BIT_INODE 1
  20. #endif
  21. #include <stdio.h>
  22. #include "sanitizer_common.h"
  23. #include "sanitizer_file.h"
  24. #include "sanitizer_flags.h"
  25. #include "sanitizer_internal_defs.h"
  26. #include "sanitizer_libc.h"
  27. #include "sanitizer_platform_limits_posix.h"
  28. #include "sanitizer_procmaps.h"
  29. #include "sanitizer_ptrauth.h"
  30. #if !SANITIZER_IOS
  31. #include <crt_externs.h> // for _NSGetEnviron
  32. #else
  33. extern char **environ;
  34. #endif
  35. #if defined(__has_include) && __has_include(<os/trace.h>) && defined(__BLOCKS__)
  36. #define SANITIZER_OS_TRACE 1
  37. #include <os/trace.h>
  38. #else
  39. #define SANITIZER_OS_TRACE 0
  40. #endif
  41. // import new crash reporting api
  42. #if defined(__has_include) && __has_include(<CrashReporterClient.h>)
  43. #define HAVE_CRASHREPORTERCLIENT_H 1
  44. #include <CrashReporterClient.h>
  45. #else
  46. #define HAVE_CRASHREPORTERCLIENT_H 0
  47. #endif
  48. #if !SANITIZER_IOS
  49. #include <crt_externs.h> // for _NSGetArgv and _NSGetEnviron
  50. #else
  51. extern "C" {
  52. extern char ***_NSGetArgv(void);
  53. }
  54. #endif
  55. #include <asl.h>
  56. #include <dlfcn.h> // for dladdr()
  57. #include <errno.h>
  58. #include <fcntl.h>
  59. #include <libkern/OSAtomic.h>
  60. #include <mach-o/dyld.h>
  61. #include <mach/mach.h>
  62. #include <mach/mach_time.h>
  63. #include <mach/vm_statistics.h>
  64. #include <malloc/malloc.h>
  65. #if defined(__has_builtin) && __has_builtin(__builtin_os_log_format)
  66. # include <os/log.h>
  67. #else
  68. /* Without support for __builtin_os_log_format, fall back to the older
  69. method. */
  70. # define OS_LOG_DEFAULT 0
  71. # define os_log_error(A,B,C) \
  72. asl_log(nullptr, nullptr, ASL_LEVEL_ERR, "%s", (C));
  73. #endif
  74. #include <pthread.h>
  75. #include <sched.h>
  76. #include <signal.h>
  77. #include <spawn.h>
  78. #include <stdlib.h>
  79. #include <sys/ioctl.h>
  80. #include <sys/mman.h>
  81. #include <sys/resource.h>
  82. #include <sys/stat.h>
  83. #include <sys/sysctl.h>
  84. #include <sys/types.h>
  85. #include <sys/wait.h>
  86. #include <unistd.h>
  87. #include <util.h>
  88. // From <crt_externs.h>, but we don't have that file on iOS.
  89. extern "C" {
  90. extern char ***_NSGetArgv(void);
  91. extern char ***_NSGetEnviron(void);
  92. }
  93. // From <mach/mach_vm.h>, but we don't have that file on iOS.
  94. extern "C" {
  95. extern kern_return_t mach_vm_region_recurse(
  96. vm_map_t target_task,
  97. mach_vm_address_t *address,
  98. mach_vm_size_t *size,
  99. natural_t *nesting_depth,
  100. vm_region_recurse_info_t info,
  101. mach_msg_type_number_t *infoCnt);
  102. }
  103. namespace __sanitizer {
  104. #include "sanitizer_syscall_generic.inc"
  105. // Direct syscalls, don't call libmalloc hooks (but not available on 10.6).
  106. extern "C" void *__mmap(void *addr, size_t len, int prot, int flags, int fildes,
  107. off_t off) SANITIZER_WEAK_ATTRIBUTE;
  108. extern "C" int __munmap(void *, size_t) SANITIZER_WEAK_ATTRIBUTE;
  109. // ---------------------- sanitizer_libc.h
  110. // From <mach/vm_statistics.h>, but not on older OSs.
  111. #ifndef VM_MEMORY_SANITIZER
  112. #define VM_MEMORY_SANITIZER 99
  113. #endif
  114. // XNU on Darwin provides a mmap flag that optimizes allocation/deallocation of
  115. // giant memory regions (i.e. shadow memory regions).
  116. #define kXnuFastMmapFd 0x4
  117. static size_t kXnuFastMmapThreshold = 2 << 30; // 2 GB
  118. static bool use_xnu_fast_mmap = false;
  119. uptr internal_mmap(void *addr, size_t length, int prot, int flags,
  120. int fd, u64 offset) {
  121. if (fd == -1) {
  122. fd = VM_MAKE_TAG(VM_MEMORY_SANITIZER);
  123. if (length >= kXnuFastMmapThreshold) {
  124. if (use_xnu_fast_mmap) fd |= kXnuFastMmapFd;
  125. }
  126. }
  127. if (&__mmap) return (uptr)__mmap(addr, length, prot, flags, fd, offset);
  128. return (uptr)mmap(addr, length, prot, flags, fd, offset);
  129. }
  130. uptr internal_munmap(void *addr, uptr length) {
  131. if (&__munmap) return __munmap(addr, length);
  132. return munmap(addr, length);
  133. }
  134. uptr internal_mremap(void *old_address, uptr old_size, uptr new_size, int flags,
  135. void *new_address) {
  136. CHECK(false && "internal_mremap is unimplemented on Mac");
  137. return 0;
  138. }
  139. int internal_mprotect(void *addr, uptr length, int prot) {
  140. return mprotect(addr, length, prot);
  141. }
  142. int internal_madvise(uptr addr, uptr length, int advice) {
  143. return madvise((void *)addr, length, advice);
  144. }
  145. uptr internal_close(fd_t fd) {
  146. return close(fd);
  147. }
  148. uptr internal_open(const char *filename, int flags) {
  149. return open(filename, flags);
  150. }
  151. uptr internal_open(const char *filename, int flags, u32 mode) {
  152. return open(filename, flags, mode);
  153. }
  154. uptr internal_read(fd_t fd, void *buf, uptr count) {
  155. return read(fd, buf, count);
  156. }
  157. uptr internal_write(fd_t fd, const void *buf, uptr count) {
  158. return write(fd, buf, count);
  159. }
  160. uptr internal_stat(const char *path, void *buf) {
  161. return stat(path, (struct stat *)buf);
  162. }
  163. uptr internal_lstat(const char *path, void *buf) {
  164. return lstat(path, (struct stat *)buf);
  165. }
  166. uptr internal_fstat(fd_t fd, void *buf) {
  167. return fstat(fd, (struct stat *)buf);
  168. }
  169. uptr internal_filesize(fd_t fd) {
  170. struct stat st;
  171. if (internal_fstat(fd, &st))
  172. return -1;
  173. return (uptr)st.st_size;
  174. }
  175. uptr internal_dup(int oldfd) {
  176. return dup(oldfd);
  177. }
  178. uptr internal_dup2(int oldfd, int newfd) {
  179. return dup2(oldfd, newfd);
  180. }
  181. uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
  182. return readlink(path, buf, bufsize);
  183. }
  184. uptr internal_unlink(const char *path) {
  185. return unlink(path);
  186. }
  187. uptr internal_sched_yield() {
  188. return sched_yield();
  189. }
  190. void internal__exit(int exitcode) {
  191. _exit(exitcode);
  192. }
  193. void internal_usleep(u64 useconds) { usleep(useconds); }
  194. uptr internal_getpid() {
  195. return getpid();
  196. }
  197. int internal_dlinfo(void *handle, int request, void *p) {
  198. UNIMPLEMENTED();
  199. }
  200. int internal_sigaction(int signum, const void *act, void *oldact) {
  201. return sigaction(signum,
  202. (const struct sigaction *)act, (struct sigaction *)oldact);
  203. }
  204. void internal_sigfillset(__sanitizer_sigset_t *set) { sigfillset(set); }
  205. uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
  206. __sanitizer_sigset_t *oldset) {
  207. // Don't use sigprocmask here, because it affects all threads.
  208. return pthread_sigmask(how, set, oldset);
  209. }
  210. // Doesn't call pthread_atfork() handlers (but not available on 10.6).
  211. extern "C" pid_t __fork(void) SANITIZER_WEAK_ATTRIBUTE;
  212. int internal_fork() {
  213. if (&__fork)
  214. return __fork();
  215. return fork();
  216. }
  217. int internal_sysctl(const int *name, unsigned int namelen, void *oldp,
  218. uptr *oldlenp, const void *newp, uptr newlen) {
  219. return sysctl(const_cast<int *>(name), namelen, oldp, (size_t *)oldlenp,
  220. const_cast<void *>(newp), (size_t)newlen);
  221. }
  222. int internal_sysctlbyname(const char *sname, void *oldp, uptr *oldlenp,
  223. const void *newp, uptr newlen) {
  224. return sysctlbyname(sname, oldp, (size_t *)oldlenp, const_cast<void *>(newp),
  225. (size_t)newlen);
  226. }
  227. static fd_t internal_spawn_impl(const char *argv[], const char *envp[],
  228. pid_t *pid) {
  229. fd_t master_fd = kInvalidFd;
  230. fd_t slave_fd = kInvalidFd;
  231. auto fd_closer = at_scope_exit([&] {
  232. internal_close(master_fd);
  233. internal_close(slave_fd);
  234. });
  235. // We need a new pseudoterminal to avoid buffering problems. The 'atos' tool
  236. // in particular detects when it's talking to a pipe and forgets to flush the
  237. // output stream after sending a response.
  238. master_fd = posix_openpt(O_RDWR);
  239. if (master_fd == kInvalidFd) return kInvalidFd;
  240. int res = grantpt(master_fd) || unlockpt(master_fd);
  241. if (res != 0) return kInvalidFd;
  242. // Use TIOCPTYGNAME instead of ptsname() to avoid threading problems.
  243. char slave_pty_name[128];
  244. res = ioctl(master_fd, TIOCPTYGNAME, slave_pty_name);
  245. if (res == -1) return kInvalidFd;
  246. slave_fd = internal_open(slave_pty_name, O_RDWR);
  247. if (slave_fd == kInvalidFd) return kInvalidFd;
  248. // File descriptor actions
  249. posix_spawn_file_actions_t acts;
  250. res = posix_spawn_file_actions_init(&acts);
  251. if (res != 0) return kInvalidFd;
  252. auto acts_cleanup = at_scope_exit([&] {
  253. posix_spawn_file_actions_destroy(&acts);
  254. });
  255. res = posix_spawn_file_actions_adddup2(&acts, slave_fd, STDIN_FILENO) ||
  256. posix_spawn_file_actions_adddup2(&acts, slave_fd, STDOUT_FILENO) ||
  257. posix_spawn_file_actions_addclose(&acts, slave_fd);
  258. if (res != 0) return kInvalidFd;
  259. // Spawn attributes
  260. posix_spawnattr_t attrs;
  261. res = posix_spawnattr_init(&attrs);
  262. if (res != 0) return kInvalidFd;
  263. auto attrs_cleanup = at_scope_exit([&] {
  264. posix_spawnattr_destroy(&attrs);
  265. });
  266. // In the spawned process, close all file descriptors that are not explicitly
  267. // described by the file actions object. This is Darwin-specific extension.
  268. res = posix_spawnattr_setflags(&attrs, POSIX_SPAWN_CLOEXEC_DEFAULT);
  269. if (res != 0) return kInvalidFd;
  270. // posix_spawn
  271. char **argv_casted = const_cast<char **>(argv);
  272. char **envp_casted = const_cast<char **>(envp);
  273. res = posix_spawn(pid, argv[0], &acts, &attrs, argv_casted, envp_casted);
  274. if (res != 0) return kInvalidFd;
  275. // Disable echo in the new terminal, disable CR.
  276. struct termios termflags;
  277. tcgetattr(master_fd, &termflags);
  278. termflags.c_oflag &= ~ONLCR;
  279. termflags.c_lflag &= ~ECHO;
  280. tcsetattr(master_fd, TCSANOW, &termflags);
  281. // On success, do not close master_fd on scope exit.
  282. fd_t fd = master_fd;
  283. master_fd = kInvalidFd;
  284. return fd;
  285. }
  286. fd_t internal_spawn(const char *argv[], const char *envp[], pid_t *pid) {
  287. // The client program may close its stdin and/or stdout and/or stderr thus
  288. // allowing open/posix_openpt to reuse file descriptors 0, 1 or 2. In this
  289. // case the communication is broken if either the parent or the child tries to
  290. // close or duplicate these descriptors. We temporarily reserve these
  291. // descriptors here to prevent this.
  292. fd_t low_fds[3];
  293. size_t count = 0;
  294. for (; count < 3; count++) {
  295. low_fds[count] = posix_openpt(O_RDWR);
  296. if (low_fds[count] >= STDERR_FILENO)
  297. break;
  298. }
  299. fd_t fd = internal_spawn_impl(argv, envp, pid);
  300. for (; count > 0; count--) {
  301. internal_close(low_fds[count]);
  302. }
  303. return fd;
  304. }
  305. uptr internal_rename(const char *oldpath, const char *newpath) {
  306. return rename(oldpath, newpath);
  307. }
  308. uptr internal_ftruncate(fd_t fd, uptr size) {
  309. return ftruncate(fd, size);
  310. }
  311. uptr internal_execve(const char *filename, char *const argv[],
  312. char *const envp[]) {
  313. return execve(filename, argv, envp);
  314. }
  315. uptr internal_waitpid(int pid, int *status, int options) {
  316. return waitpid(pid, status, options);
  317. }
  318. // ----------------- sanitizer_common.h
  319. bool FileExists(const char *filename) {
  320. if (ShouldMockFailureToOpen(filename))
  321. return false;
  322. struct stat st;
  323. if (stat(filename, &st))
  324. return false;
  325. // Sanity check: filename is a regular file.
  326. return S_ISREG(st.st_mode);
  327. }
  328. tid_t GetTid() {
  329. tid_t tid;
  330. pthread_threadid_np(nullptr, &tid);
  331. return tid;
  332. }
  333. void GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top,
  334. uptr *stack_bottom) {
  335. CHECK(stack_top);
  336. CHECK(stack_bottom);
  337. uptr stacksize = pthread_get_stacksize_np(pthread_self());
  338. // pthread_get_stacksize_np() returns an incorrect stack size for the main
  339. // thread on Mavericks. See
  340. // https://github.com/google/sanitizers/issues/261
  341. if ((GetMacosAlignedVersion() >= MacosVersion(10, 9)) && at_initialization &&
  342. stacksize == (1 << 19)) {
  343. struct rlimit rl;
  344. CHECK_EQ(getrlimit(RLIMIT_STACK, &rl), 0);
  345. // Most often rl.rlim_cur will be the desired 8M.
  346. if (rl.rlim_cur < kMaxThreadStackSize) {
  347. stacksize = rl.rlim_cur;
  348. } else {
  349. stacksize = kMaxThreadStackSize;
  350. }
  351. }
  352. void *stackaddr = pthread_get_stackaddr_np(pthread_self());
  353. *stack_top = (uptr)stackaddr;
  354. *stack_bottom = *stack_top - stacksize;
  355. }
  356. char **GetEnviron() {
  357. #if !SANITIZER_IOS
  358. char ***env_ptr = _NSGetEnviron();
  359. if (!env_ptr) {
  360. Report("_NSGetEnviron() returned NULL. Please make sure __asan_init() is "
  361. "called after libSystem_initializer().\n");
  362. CHECK(env_ptr);
  363. }
  364. char **environ = *env_ptr;
  365. #endif
  366. CHECK(environ);
  367. return environ;
  368. }
  369. const char *GetEnv(const char *name) {
  370. char **env = GetEnviron();
  371. uptr name_len = internal_strlen(name);
  372. while (*env != 0) {
  373. uptr len = internal_strlen(*env);
  374. if (len > name_len) {
  375. const char *p = *env;
  376. if (!internal_memcmp(p, name, name_len) &&
  377. p[name_len] == '=') { // Match.
  378. return *env + name_len + 1; // String starting after =.
  379. }
  380. }
  381. env++;
  382. }
  383. return 0;
  384. }
  385. uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) {
  386. CHECK_LE(kMaxPathLength, buf_len);
  387. // On OS X the executable path is saved to the stack by dyld. Reading it
  388. // from there is much faster than calling dladdr, especially for large
  389. // binaries with symbols.
  390. InternalMmapVector<char> exe_path(kMaxPathLength);
  391. uint32_t size = exe_path.size();
  392. if (_NSGetExecutablePath(exe_path.data(), &size) == 0 &&
  393. realpath(exe_path.data(), buf) != 0) {
  394. return internal_strlen(buf);
  395. }
  396. return 0;
  397. }
  398. uptr ReadLongProcessName(/*out*/char *buf, uptr buf_len) {
  399. return ReadBinaryName(buf, buf_len);
  400. }
  401. void ReExec() {
  402. UNIMPLEMENTED();
  403. }
  404. void CheckASLR() {
  405. // Do nothing
  406. }
  407. void CheckMPROTECT() {
  408. // Do nothing
  409. }
  410. uptr GetPageSize() {
  411. return sysconf(_SC_PAGESIZE);
  412. }
  413. extern "C" unsigned malloc_num_zones;
  414. extern "C" malloc_zone_t **malloc_zones;
  415. malloc_zone_t sanitizer_zone;
  416. // We need to make sure that sanitizer_zone is registered as malloc_zones[0]. If
  417. // libmalloc tries to set up a different zone as malloc_zones[0], it will call
  418. // mprotect(malloc_zones, ..., PROT_READ). This interceptor will catch that and
  419. // make sure we are still the first (default) zone.
  420. void MprotectMallocZones(void *addr, int prot) {
  421. if (addr == malloc_zones && prot == PROT_READ) {
  422. if (malloc_num_zones > 1 && malloc_zones[0] != &sanitizer_zone) {
  423. for (unsigned i = 1; i < malloc_num_zones; i++) {
  424. if (malloc_zones[i] == &sanitizer_zone) {
  425. // Swap malloc_zones[0] and malloc_zones[i].
  426. malloc_zones[i] = malloc_zones[0];
  427. malloc_zones[0] = &sanitizer_zone;
  428. break;
  429. }
  430. }
  431. }
  432. }
  433. }
  434. void FutexWait(atomic_uint32_t *p, u32 cmp) {
  435. // FIXME: implement actual blocking.
  436. sched_yield();
  437. }
  438. void FutexWake(atomic_uint32_t *p, u32 count) {}
  439. u64 NanoTime() {
  440. timeval tv;
  441. internal_memset(&tv, 0, sizeof(tv));
  442. gettimeofday(&tv, 0);
  443. return (u64)tv.tv_sec * 1000*1000*1000 + tv.tv_usec * 1000;
  444. }
  445. // This needs to be called during initialization to avoid being racy.
  446. u64 MonotonicNanoTime() {
  447. static mach_timebase_info_data_t timebase_info;
  448. if (timebase_info.denom == 0) mach_timebase_info(&timebase_info);
  449. return (mach_absolute_time() * timebase_info.numer) / timebase_info.denom;
  450. }
  451. uptr GetTlsSize() {
  452. return 0;
  453. }
  454. void InitTlsSize() {
  455. }
  456. uptr TlsBaseAddr() {
  457. uptr segbase = 0;
  458. #if defined(__x86_64__)
  459. asm("movq %%gs:0,%0" : "=r"(segbase));
  460. #elif defined(__i386__)
  461. asm("movl %%gs:0,%0" : "=r"(segbase));
  462. #elif defined(__aarch64__)
  463. asm("mrs %x0, tpidrro_el0" : "=r"(segbase));
  464. segbase &= 0x07ul; // clearing lower bits, cpu id stored there
  465. #endif
  466. return segbase;
  467. }
  468. // The size of the tls on darwin does not appear to be well documented,
  469. // however the vm memory map suggests that it is 1024 uptrs in size,
  470. // with a size of 0x2000 bytes on x86_64 and 0x1000 bytes on i386.
  471. uptr TlsSize() {
  472. #if defined(__x86_64__) || defined(__i386__)
  473. return 1024 * sizeof(uptr);
  474. #else
  475. return 0;
  476. #endif
  477. }
  478. void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
  479. uptr *tls_addr, uptr *tls_size) {
  480. #if !SANITIZER_GO
  481. uptr stack_top, stack_bottom;
  482. GetThreadStackTopAndBottom(main, &stack_top, &stack_bottom);
  483. *stk_addr = stack_bottom;
  484. *stk_size = stack_top - stack_bottom;
  485. *tls_addr = TlsBaseAddr();
  486. *tls_size = TlsSize();
  487. #else
  488. *stk_addr = 0;
  489. *stk_size = 0;
  490. *tls_addr = 0;
  491. *tls_size = 0;
  492. #endif
  493. }
  494. void ListOfModules::init() {
  495. clearOrInit();
  496. MemoryMappingLayout memory_mapping(false);
  497. memory_mapping.DumpListOfModules(&modules_);
  498. }
  499. void ListOfModules::fallbackInit() { clear(); }
  500. static HandleSignalMode GetHandleSignalModeImpl(int signum) {
  501. switch (signum) {
  502. case SIGABRT:
  503. return common_flags()->handle_abort;
  504. case SIGILL:
  505. return common_flags()->handle_sigill;
  506. case SIGTRAP:
  507. return common_flags()->handle_sigtrap;
  508. case SIGFPE:
  509. return common_flags()->handle_sigfpe;
  510. case SIGSEGV:
  511. return common_flags()->handle_segv;
  512. case SIGBUS:
  513. return common_flags()->handle_sigbus;
  514. }
  515. return kHandleSignalNo;
  516. }
  517. HandleSignalMode GetHandleSignalMode(int signum) {
  518. // Handling fatal signals on watchOS and tvOS devices is disallowed.
  519. if ((SANITIZER_WATCHOS || SANITIZER_TVOS) && !(SANITIZER_IOSSIM))
  520. return kHandleSignalNo;
  521. HandleSignalMode result = GetHandleSignalModeImpl(signum);
  522. if (result == kHandleSignalYes && !common_flags()->allow_user_segv_handler)
  523. return kHandleSignalExclusive;
  524. return result;
  525. }
  526. // Offset example:
  527. // XNU 17 -- macOS 10.13 -- iOS 11 -- tvOS 11 -- watchOS 4
  528. constexpr u16 GetOSMajorKernelOffset() {
  529. if (TARGET_OS_OSX) return 4;
  530. if (TARGET_OS_IOS || TARGET_OS_TV) return 6;
  531. if (TARGET_OS_WATCH) return 13;
  532. }
  533. using VersStr = char[64];
  534. static uptr ApproximateOSVersionViaKernelVersion(VersStr vers) {
  535. u16 kernel_major = GetDarwinKernelVersion().major;
  536. u16 offset = GetOSMajorKernelOffset();
  537. CHECK_GE(kernel_major, offset);
  538. u16 os_major = kernel_major - offset;
  539. const char *format = "%d.0";
  540. if (TARGET_OS_OSX) {
  541. if (os_major >= 16) { // macOS 11+
  542. os_major -= 5;
  543. } else { // macOS 10.15 and below
  544. format = "10.%d";
  545. }
  546. }
  547. return internal_snprintf(vers, sizeof(VersStr), format, os_major);
  548. }
  549. static void GetOSVersion(VersStr vers) {
  550. uptr len = sizeof(VersStr);
  551. if (SANITIZER_IOSSIM) {
  552. const char *vers_env = GetEnv("SIMULATOR_RUNTIME_VERSION");
  553. if (!vers_env) {
  554. Report("ERROR: Running in simulator but SIMULATOR_RUNTIME_VERSION env "
  555. "var is not set.\n");
  556. Die();
  557. }
  558. len = internal_strlcpy(vers, vers_env, len);
  559. } else {
  560. int res =
  561. internal_sysctlbyname("kern.osproductversion", vers, &len, nullptr, 0);
  562. // XNU 17 (macOS 10.13) and below do not provide the sysctl
  563. // `kern.osproductversion` entry (res != 0).
  564. bool no_os_version = res != 0;
  565. // For launchd, sanitizer initialization runs before sysctl is setup
  566. // (res == 0 && len != strlen(vers), vers is not a valid version). However,
  567. // the kernel version `kern.osrelease` is available.
  568. bool launchd = (res == 0 && internal_strlen(vers) < 3);
  569. if (launchd) CHECK_EQ(internal_getpid(), 1);
  570. if (no_os_version || launchd) {
  571. len = ApproximateOSVersionViaKernelVersion(vers);
  572. }
  573. }
  574. CHECK_LT(len, sizeof(VersStr));
  575. }
  576. void ParseVersion(const char *vers, u16 *major, u16 *minor) {
  577. // Format: <major>.<minor>[.<patch>]\0
  578. CHECK_GE(internal_strlen(vers), 3);
  579. const char *p = vers;
  580. *major = internal_simple_strtoll(p, &p, /*base=*/10);
  581. CHECK_EQ(*p, '.');
  582. p += 1;
  583. *minor = internal_simple_strtoll(p, &p, /*base=*/10);
  584. }
  585. // Aligned versions example:
  586. // macOS 10.15 -- iOS 13 -- tvOS 13 -- watchOS 6
  587. static void MapToMacos(u16 *major, u16 *minor) {
  588. if (TARGET_OS_OSX)
  589. return;
  590. if (TARGET_OS_IOS || TARGET_OS_TV)
  591. *major += 2;
  592. else if (TARGET_OS_WATCH)
  593. *major += 9;
  594. else
  595. UNREACHABLE("unsupported platform");
  596. if (*major >= 16) { // macOS 11+
  597. *major -= 5;
  598. } else { // macOS 10.15 and below
  599. *minor = *major;
  600. *major = 10;
  601. }
  602. }
  603. static MacosVersion GetMacosAlignedVersionInternal() {
  604. VersStr vers = {};
  605. GetOSVersion(vers);
  606. u16 major, minor;
  607. ParseVersion(vers, &major, &minor);
  608. MapToMacos(&major, &minor);
  609. return MacosVersion(major, minor);
  610. }
  611. static_assert(sizeof(MacosVersion) == sizeof(atomic_uint32_t::Type),
  612. "MacosVersion cache size");
  613. static atomic_uint32_t cached_macos_version;
  614. MacosVersion GetMacosAlignedVersion() {
  615. atomic_uint32_t::Type result =
  616. atomic_load(&cached_macos_version, memory_order_acquire);
  617. if (!result) {
  618. MacosVersion version = GetMacosAlignedVersionInternal();
  619. result = *reinterpret_cast<atomic_uint32_t::Type *>(&version);
  620. atomic_store(&cached_macos_version, result, memory_order_release);
  621. }
  622. return *reinterpret_cast<MacosVersion *>(&result);
  623. }
  624. DarwinKernelVersion GetDarwinKernelVersion() {
  625. VersStr vers = {};
  626. uptr len = sizeof(VersStr);
  627. int res = internal_sysctlbyname("kern.osrelease", vers, &len, nullptr, 0);
  628. CHECK_EQ(res, 0);
  629. CHECK_LT(len, sizeof(VersStr));
  630. u16 major, minor;
  631. ParseVersion(vers, &major, &minor);
  632. return DarwinKernelVersion(major, minor);
  633. }
  634. uptr GetRSS() {
  635. struct task_basic_info info;
  636. unsigned count = TASK_BASIC_INFO_COUNT;
  637. kern_return_t result =
  638. task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&info, &count);
  639. if (UNLIKELY(result != KERN_SUCCESS)) {
  640. Report("Cannot get task info. Error: %d\n", result);
  641. Die();
  642. }
  643. return info.resident_size;
  644. }
  645. void *internal_start_thread(void *(*func)(void *arg), void *arg) {
  646. // Start the thread with signals blocked, otherwise it can steal user signals.
  647. __sanitizer_sigset_t set, old;
  648. internal_sigfillset(&set);
  649. internal_sigprocmask(SIG_SETMASK, &set, &old);
  650. pthread_t th;
  651. pthread_create(&th, 0, func, arg);
  652. internal_sigprocmask(SIG_SETMASK, &old, 0);
  653. return th;
  654. }
  655. void internal_join_thread(void *th) { pthread_join((pthread_t)th, 0); }
  656. #if !SANITIZER_GO
  657. static Mutex syslog_lock;
  658. # endif
  659. void WriteOneLineToSyslog(const char *s) {
  660. #if !SANITIZER_GO
  661. syslog_lock.CheckLocked();
  662. if (GetMacosAlignedVersion() >= MacosVersion(10, 12)) {
  663. os_log_error(OS_LOG_DEFAULT, "%{public}s", s);
  664. } else {
  665. asl_log(nullptr, nullptr, ASL_LEVEL_ERR, "%s", s);
  666. }
  667. #endif
  668. }
  669. // buffer to store crash report application information
  670. static char crashreporter_info_buff[__sanitizer::kErrorMessageBufferSize] = {};
  671. static Mutex crashreporter_info_mutex;
  672. extern "C" {
  673. // Integrate with crash reporter libraries.
  674. #if HAVE_CRASHREPORTERCLIENT_H
  675. CRASH_REPORTER_CLIENT_HIDDEN
  676. struct crashreporter_annotations_t gCRAnnotations
  677. __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION))) = {
  678. CRASHREPORTER_ANNOTATIONS_VERSION,
  679. 0,
  680. 0,
  681. 0,
  682. 0,
  683. 0,
  684. 0,
  685. #if CRASHREPORTER_ANNOTATIONS_VERSION > 4
  686. 0,
  687. #endif
  688. };
  689. #else
  690. // fall back to old crashreporter api
  691. static const char *__crashreporter_info__ __attribute__((__used__)) =
  692. &crashreporter_info_buff[0];
  693. asm(".desc ___crashreporter_info__, 0x10");
  694. #endif
  695. } // extern "C"
  696. static void CRAppendCrashLogMessage(const char *msg) {
  697. Lock l(&crashreporter_info_mutex);
  698. internal_strlcat(crashreporter_info_buff, msg,
  699. sizeof(crashreporter_info_buff));
  700. #if HAVE_CRASHREPORTERCLIENT_H
  701. (void)CRSetCrashLogMessage(crashreporter_info_buff);
  702. #endif
  703. }
  704. void LogMessageOnPrintf(const char *str) {
  705. // Log all printf output to CrashLog.
  706. if (common_flags()->abort_on_error)
  707. CRAppendCrashLogMessage(str);
  708. }
  709. void LogFullErrorReport(const char *buffer) {
  710. #if !SANITIZER_GO
  711. // Log with os_trace. This will make it into the crash log.
  712. #if SANITIZER_OS_TRACE
  713. if (GetMacosAlignedVersion() >= MacosVersion(10, 10)) {
  714. // os_trace requires the message (format parameter) to be a string literal.
  715. if (internal_strncmp(SanitizerToolName, "AddressSanitizer",
  716. sizeof("AddressSanitizer") - 1) == 0)
  717. os_trace("Address Sanitizer reported a failure.");
  718. else if (internal_strncmp(SanitizerToolName, "UndefinedBehaviorSanitizer",
  719. sizeof("UndefinedBehaviorSanitizer") - 1) == 0)
  720. os_trace("Undefined Behavior Sanitizer reported a failure.");
  721. else if (internal_strncmp(SanitizerToolName, "ThreadSanitizer",
  722. sizeof("ThreadSanitizer") - 1) == 0)
  723. os_trace("Thread Sanitizer reported a failure.");
  724. else
  725. os_trace("Sanitizer tool reported a failure.");
  726. if (common_flags()->log_to_syslog)
  727. os_trace("Consult syslog for more information.");
  728. }
  729. #endif
  730. // Log to syslog.
  731. // The logging on OS X may call pthread_create so we need the threading
  732. // environment to be fully initialized. Also, this should never be called when
  733. // holding the thread registry lock since that may result in a deadlock. If
  734. // the reporting thread holds the thread registry mutex, and asl_log waits
  735. // for GCD to dispatch a new thread, the process will deadlock, because the
  736. // pthread_create wrapper needs to acquire the lock as well.
  737. Lock l(&syslog_lock);
  738. if (common_flags()->log_to_syslog)
  739. WriteToSyslog(buffer);
  740. // The report is added to CrashLog as part of logging all of Printf output.
  741. #endif
  742. }
  743. SignalContext::WriteFlag SignalContext::GetWriteFlag() const {
  744. #if defined(__x86_64__) || defined(__i386__)
  745. ucontext_t *ucontext = static_cast<ucontext_t*>(context);
  746. return ucontext->uc_mcontext->__es.__err & 2 /*T_PF_WRITE*/ ? WRITE : READ;
  747. #else
  748. return UNKNOWN;
  749. #endif
  750. }
  751. bool SignalContext::IsTrueFaultingAddress() const {
  752. auto si = static_cast<const siginfo_t *>(siginfo);
  753. // "Real" SIGSEGV codes (e.g., SEGV_MAPERR, SEGV_MAPERR) are non-zero.
  754. return si->si_signo == SIGSEGV && si->si_code != 0;
  755. }
  756. #if defined(__aarch64__) && defined(arm_thread_state64_get_sp)
  757. #define AARCH64_GET_REG(r) \
  758. (uptr)ptrauth_strip( \
  759. (void *)arm_thread_state64_get_##r(ucontext->uc_mcontext->__ss), 0)
  760. #else
  761. #define AARCH64_GET_REG(r) ucontext->uc_mcontext->__ss.__##r
  762. #endif
  763. static void GetPcSpBp(void *context, uptr *pc, uptr *sp, uptr *bp) {
  764. ucontext_t *ucontext = (ucontext_t*)context;
  765. # if defined(__aarch64__)
  766. *pc = AARCH64_GET_REG(pc);
  767. # if defined(__IPHONE_8_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0
  768. *bp = AARCH64_GET_REG(fp);
  769. # else
  770. *bp = AARCH64_GET_REG(lr);
  771. # endif
  772. *sp = AARCH64_GET_REG(sp);
  773. # elif defined(__x86_64__)
  774. *pc = ucontext->uc_mcontext->__ss.__rip;
  775. *bp = ucontext->uc_mcontext->__ss.__rbp;
  776. *sp = ucontext->uc_mcontext->__ss.__rsp;
  777. # elif defined(__arm__)
  778. *pc = ucontext->uc_mcontext->__ss.__pc;
  779. *bp = ucontext->uc_mcontext->__ss.__r[7];
  780. *sp = ucontext->uc_mcontext->__ss.__sp;
  781. # elif defined(__i386__)
  782. *pc = ucontext->uc_mcontext->__ss.__eip;
  783. *bp = ucontext->uc_mcontext->__ss.__ebp;
  784. *sp = ucontext->uc_mcontext->__ss.__esp;
  785. # else
  786. # error "Unknown architecture"
  787. # endif
  788. }
  789. void SignalContext::InitPcSpBp() {
  790. addr = (uptr)ptrauth_strip((void *)addr, 0);
  791. GetPcSpBp(context, &pc, &sp, &bp);
  792. }
  793. // ASan/TSan use mmap in a way that creates “deallocation gaps” which triggers
  794. // EXC_GUARD exceptions on macOS 10.15+ (XNU 19.0+).
  795. static void DisableMmapExcGuardExceptions() {
  796. using task_exc_guard_behavior_t = uint32_t;
  797. using task_set_exc_guard_behavior_t =
  798. kern_return_t(task_t task, task_exc_guard_behavior_t behavior);
  799. auto *set_behavior = (task_set_exc_guard_behavior_t *)dlsym(
  800. RTLD_DEFAULT, "task_set_exc_guard_behavior");
  801. if (set_behavior == nullptr) return;
  802. const task_exc_guard_behavior_t task_exc_guard_none = 0;
  803. set_behavior(mach_task_self(), task_exc_guard_none);
  804. }
  805. void InitializePlatformEarly() {
  806. // Only use xnu_fast_mmap when on x86_64 and the kernel supports it.
  807. use_xnu_fast_mmap =
  808. #if defined(__x86_64__)
  809. GetDarwinKernelVersion() >= DarwinKernelVersion(17, 5);
  810. #else
  811. false;
  812. #endif
  813. if (GetDarwinKernelVersion() >= DarwinKernelVersion(19, 0))
  814. DisableMmapExcGuardExceptions();
  815. }
  816. #if !SANITIZER_GO
  817. static const char kDyldInsertLibraries[] = "DYLD_INSERT_LIBRARIES";
  818. LowLevelAllocator allocator_for_env;
  819. // Change the value of the env var |name|, leaking the original value.
  820. // If |name_value| is NULL, the variable is deleted from the environment,
  821. // otherwise the corresponding "NAME=value" string is replaced with
  822. // |name_value|.
  823. void LeakyResetEnv(const char *name, const char *name_value) {
  824. char **env = GetEnviron();
  825. uptr name_len = internal_strlen(name);
  826. while (*env != 0) {
  827. uptr len = internal_strlen(*env);
  828. if (len > name_len) {
  829. const char *p = *env;
  830. if (!internal_memcmp(p, name, name_len) && p[name_len] == '=') {
  831. // Match.
  832. if (name_value) {
  833. // Replace the old value with the new one.
  834. *env = const_cast<char*>(name_value);
  835. } else {
  836. // Shift the subsequent pointers back.
  837. char **del = env;
  838. do {
  839. del[0] = del[1];
  840. } while (*del++);
  841. }
  842. }
  843. }
  844. env++;
  845. }
  846. }
  847. SANITIZER_WEAK_CXX_DEFAULT_IMPL
  848. bool ReexecDisabled() {
  849. return false;
  850. }
  851. static bool DyldNeedsEnvVariable() {
  852. // If running on OS X 10.11+ or iOS 9.0+, dyld will interpose even if
  853. // DYLD_INSERT_LIBRARIES is not set.
  854. return GetMacosAlignedVersion() < MacosVersion(10, 11);
  855. }
  856. void MaybeReexec() {
  857. // FIXME: This should really live in some "InitializePlatform" method.
  858. MonotonicNanoTime();
  859. if (ReexecDisabled()) return;
  860. // Make sure the dynamic runtime library is preloaded so that the
  861. // wrappers work. If it is not, set DYLD_INSERT_LIBRARIES and re-exec
  862. // ourselves.
  863. Dl_info info;
  864. RAW_CHECK(dladdr((void*)((uptr)&__sanitizer_report_error_summary), &info));
  865. char *dyld_insert_libraries =
  866. const_cast<char*>(GetEnv(kDyldInsertLibraries));
  867. uptr old_env_len = dyld_insert_libraries ?
  868. internal_strlen(dyld_insert_libraries) : 0;
  869. uptr fname_len = internal_strlen(info.dli_fname);
  870. const char *dylib_name = StripModuleName(info.dli_fname);
  871. uptr dylib_name_len = internal_strlen(dylib_name);
  872. bool lib_is_in_env = dyld_insert_libraries &&
  873. internal_strstr(dyld_insert_libraries, dylib_name);
  874. if (DyldNeedsEnvVariable() && !lib_is_in_env) {
  875. // DYLD_INSERT_LIBRARIES is not set or does not contain the runtime
  876. // library.
  877. InternalMmapVector<char> program_name(1024);
  878. uint32_t buf_size = program_name.size();
  879. _NSGetExecutablePath(program_name.data(), &buf_size);
  880. char *new_env = const_cast<char*>(info.dli_fname);
  881. if (dyld_insert_libraries) {
  882. // Append the runtime dylib name to the existing value of
  883. // DYLD_INSERT_LIBRARIES.
  884. new_env = (char*)allocator_for_env.Allocate(old_env_len + fname_len + 2);
  885. internal_strncpy(new_env, dyld_insert_libraries, old_env_len);
  886. new_env[old_env_len] = ':';
  887. // Copy fname_len and add a trailing zero.
  888. internal_strncpy(new_env + old_env_len + 1, info.dli_fname,
  889. fname_len + 1);
  890. // Ok to use setenv() since the wrappers don't depend on the value of
  891. // asan_inited.
  892. setenv(kDyldInsertLibraries, new_env, /*overwrite*/1);
  893. } else {
  894. // Set DYLD_INSERT_LIBRARIES equal to the runtime dylib name.
  895. setenv(kDyldInsertLibraries, info.dli_fname, /*overwrite*/0);
  896. }
  897. VReport(1, "exec()-ing the program with\n");
  898. VReport(1, "%s=%s\n", kDyldInsertLibraries, new_env);
  899. VReport(1, "to enable wrappers.\n");
  900. execv(program_name.data(), *_NSGetArgv());
  901. // We get here only if execv() failed.
  902. Report("ERROR: The process is launched without DYLD_INSERT_LIBRARIES, "
  903. "which is required for the sanitizer to work. We tried to set the "
  904. "environment variable and re-execute itself, but execv() failed, "
  905. "possibly because of sandbox restrictions. Make sure to launch the "
  906. "executable with:\n%s=%s\n", kDyldInsertLibraries, new_env);
  907. RAW_CHECK("execv failed" && 0);
  908. }
  909. // Verify that interceptors really work. We'll use dlsym to locate
  910. // "pthread_create", if interceptors are working, it should really point to
  911. // "wrap_pthread_create" within our own dylib.
  912. Dl_info info_pthread_create;
  913. void *dlopen_addr = dlsym(RTLD_DEFAULT, "pthread_create");
  914. RAW_CHECK(dladdr(dlopen_addr, &info_pthread_create));
  915. if (internal_strcmp(info.dli_fname, info_pthread_create.dli_fname) != 0) {
  916. Report(
  917. "ERROR: Interceptors are not working. This may be because %s is "
  918. "loaded too late (e.g. via dlopen). Please launch the executable "
  919. "with:\n%s=%s\n",
  920. SanitizerToolName, kDyldInsertLibraries, info.dli_fname);
  921. RAW_CHECK("interceptors not installed" && 0);
  922. }
  923. if (!lib_is_in_env)
  924. return;
  925. if (!common_flags()->strip_env)
  926. return;
  927. // DYLD_INSERT_LIBRARIES is set and contains the runtime library. Let's remove
  928. // the dylib from the environment variable, because interceptors are installed
  929. // and we don't want our children to inherit the variable.
  930. uptr env_name_len = internal_strlen(kDyldInsertLibraries);
  931. // Allocate memory to hold the previous env var name, its value, the '='
  932. // sign and the '\0' char.
  933. char *new_env = (char*)allocator_for_env.Allocate(
  934. old_env_len + 2 + env_name_len);
  935. RAW_CHECK(new_env);
  936. internal_memset(new_env, '\0', old_env_len + 2 + env_name_len);
  937. internal_strncpy(new_env, kDyldInsertLibraries, env_name_len);
  938. new_env[env_name_len] = '=';
  939. char *new_env_pos = new_env + env_name_len + 1;
  940. // Iterate over colon-separated pieces of |dyld_insert_libraries|.
  941. char *piece_start = dyld_insert_libraries;
  942. char *piece_end = NULL;
  943. char *old_env_end = dyld_insert_libraries + old_env_len;
  944. do {
  945. if (piece_start[0] == ':') piece_start++;
  946. piece_end = internal_strchr(piece_start, ':');
  947. if (!piece_end) piece_end = dyld_insert_libraries + old_env_len;
  948. if ((uptr)(piece_start - dyld_insert_libraries) > old_env_len) break;
  949. uptr piece_len = piece_end - piece_start;
  950. char *filename_start =
  951. (char *)internal_memrchr(piece_start, '/', piece_len);
  952. uptr filename_len = piece_len;
  953. if (filename_start) {
  954. filename_start += 1;
  955. filename_len = piece_len - (filename_start - piece_start);
  956. } else {
  957. filename_start = piece_start;
  958. }
  959. // If the current piece isn't the runtime library name,
  960. // append it to new_env.
  961. if ((dylib_name_len != filename_len) ||
  962. (internal_memcmp(filename_start, dylib_name, dylib_name_len) != 0)) {
  963. if (new_env_pos != new_env + env_name_len + 1) {
  964. new_env_pos[0] = ':';
  965. new_env_pos++;
  966. }
  967. internal_strncpy(new_env_pos, piece_start, piece_len);
  968. new_env_pos += piece_len;
  969. }
  970. // Move on to the next piece.
  971. piece_start = piece_end;
  972. } while (piece_start < old_env_end);
  973. // Can't use setenv() here, because it requires the allocator to be
  974. // initialized.
  975. // FIXME: instead of filtering DYLD_INSERT_LIBRARIES here, do it in
  976. // a separate function called after InitializeAllocator().
  977. if (new_env_pos == new_env + env_name_len + 1) new_env = NULL;
  978. LeakyResetEnv(kDyldInsertLibraries, new_env);
  979. }
  980. #endif // SANITIZER_GO
  981. char **GetArgv() {
  982. return *_NSGetArgv();
  983. }
  984. #if SANITIZER_IOS && !SANITIZER_IOSSIM
  985. // The task_vm_info struct is normally provided by the macOS SDK, but we need
  986. // fields only available in 10.12+. Declare the struct manually to be able to
  987. // build against older SDKs.
  988. struct __sanitizer_task_vm_info {
  989. mach_vm_size_t virtual_size;
  990. integer_t region_count;
  991. integer_t page_size;
  992. mach_vm_size_t resident_size;
  993. mach_vm_size_t resident_size_peak;
  994. mach_vm_size_t device;
  995. mach_vm_size_t device_peak;
  996. mach_vm_size_t internal;
  997. mach_vm_size_t internal_peak;
  998. mach_vm_size_t external;
  999. mach_vm_size_t external_peak;
  1000. mach_vm_size_t reusable;
  1001. mach_vm_size_t reusable_peak;
  1002. mach_vm_size_t purgeable_volatile_pmap;
  1003. mach_vm_size_t purgeable_volatile_resident;
  1004. mach_vm_size_t purgeable_volatile_virtual;
  1005. mach_vm_size_t compressed;
  1006. mach_vm_size_t compressed_peak;
  1007. mach_vm_size_t compressed_lifetime;
  1008. mach_vm_size_t phys_footprint;
  1009. mach_vm_address_t min_address;
  1010. mach_vm_address_t max_address;
  1011. };
  1012. #define __SANITIZER_TASK_VM_INFO_COUNT ((mach_msg_type_number_t) \
  1013. (sizeof(__sanitizer_task_vm_info) / sizeof(natural_t)))
  1014. static uptr GetTaskInfoMaxAddress() {
  1015. __sanitizer_task_vm_info vm_info = {} /* zero initialize */;
  1016. mach_msg_type_number_t count = __SANITIZER_TASK_VM_INFO_COUNT;
  1017. int err = task_info(mach_task_self(), TASK_VM_INFO, (int *)&vm_info, &count);
  1018. return err ? 0 : vm_info.max_address;
  1019. }
  1020. uptr GetMaxUserVirtualAddress() {
  1021. static uptr max_vm = GetTaskInfoMaxAddress();
  1022. if (max_vm != 0) {
  1023. const uptr ret_value = max_vm - 1;
  1024. CHECK_LE(ret_value, SANITIZER_MMAP_RANGE_SIZE);
  1025. return ret_value;
  1026. }
  1027. // xnu cannot provide vm address limit
  1028. # if SANITIZER_WORDSIZE == 32
  1029. constexpr uptr fallback_max_vm = 0xffe00000 - 1;
  1030. # else
  1031. constexpr uptr fallback_max_vm = 0x200000000 - 1;
  1032. # endif
  1033. static_assert(fallback_max_vm <= SANITIZER_MMAP_RANGE_SIZE,
  1034. "Max virtual address must be less than mmap range size.");
  1035. return fallback_max_vm;
  1036. }
  1037. #else // !SANITIZER_IOS
  1038. uptr GetMaxUserVirtualAddress() {
  1039. # if SANITIZER_WORDSIZE == 64
  1040. constexpr uptr max_vm = (1ULL << 47) - 1; // 0x00007fffffffffffUL;
  1041. # else // SANITIZER_WORDSIZE == 32
  1042. static_assert(SANITIZER_WORDSIZE == 32, "Wrong wordsize");
  1043. constexpr uptr max_vm = (1ULL << 32) - 1; // 0xffffffff;
  1044. # endif
  1045. static_assert(max_vm <= SANITIZER_MMAP_RANGE_SIZE,
  1046. "Max virtual address must be less than mmap range size.");
  1047. return max_vm;
  1048. }
  1049. #endif
  1050. uptr GetMaxVirtualAddress() {
  1051. return GetMaxUserVirtualAddress();
  1052. }
  1053. uptr MapDynamicShadow(uptr shadow_size_bytes, uptr shadow_scale,
  1054. uptr min_shadow_base_alignment, uptr &high_mem_end) {
  1055. const uptr granularity = GetMmapGranularity();
  1056. const uptr alignment =
  1057. Max<uptr>(granularity << shadow_scale, 1ULL << min_shadow_base_alignment);
  1058. const uptr left_padding =
  1059. Max<uptr>(granularity, 1ULL << min_shadow_base_alignment);
  1060. uptr space_size = shadow_size_bytes + left_padding;
  1061. uptr largest_gap_found = 0;
  1062. uptr max_occupied_addr = 0;
  1063. VReport(2, "FindDynamicShadowStart, space_size = %p\n", space_size);
  1064. uptr shadow_start =
  1065. FindAvailableMemoryRange(space_size, alignment, granularity,
  1066. &largest_gap_found, &max_occupied_addr);
  1067. // If the shadow doesn't fit, restrict the address space to make it fit.
  1068. if (shadow_start == 0) {
  1069. VReport(
  1070. 2,
  1071. "Shadow doesn't fit, largest_gap_found = %p, max_occupied_addr = %p\n",
  1072. largest_gap_found, max_occupied_addr);
  1073. uptr new_max_vm = RoundDownTo(largest_gap_found << shadow_scale, alignment);
  1074. if (new_max_vm < max_occupied_addr) {
  1075. Report("Unable to find a memory range for dynamic shadow.\n");
  1076. Report(
  1077. "space_size = %p, largest_gap_found = %p, max_occupied_addr = %p, "
  1078. "new_max_vm = %p\n",
  1079. space_size, largest_gap_found, max_occupied_addr, new_max_vm);
  1080. CHECK(0 && "cannot place shadow");
  1081. }
  1082. RestrictMemoryToMaxAddress(new_max_vm);
  1083. high_mem_end = new_max_vm - 1;
  1084. space_size = (high_mem_end >> shadow_scale) + left_padding;
  1085. VReport(2, "FindDynamicShadowStart, space_size = %p\n", space_size);
  1086. shadow_start = FindAvailableMemoryRange(space_size, alignment, granularity,
  1087. nullptr, nullptr);
  1088. if (shadow_start == 0) {
  1089. Report("Unable to find a memory range after restricting VM.\n");
  1090. CHECK(0 && "cannot place shadow after restricting vm");
  1091. }
  1092. }
  1093. CHECK_NE((uptr)0, shadow_start);
  1094. CHECK(IsAligned(shadow_start, alignment));
  1095. return shadow_start;
  1096. }
  1097. uptr MapDynamicShadowAndAliases(uptr shadow_size, uptr alias_size,
  1098. uptr num_aliases, uptr ring_buffer_size) {
  1099. CHECK(false && "HWASan aliasing is unimplemented on Mac");
  1100. return 0;
  1101. }
  1102. uptr FindAvailableMemoryRange(uptr size, uptr alignment, uptr left_padding,
  1103. uptr *largest_gap_found,
  1104. uptr *max_occupied_addr) {
  1105. typedef vm_region_submap_short_info_data_64_t RegionInfo;
  1106. enum { kRegionInfoSize = VM_REGION_SUBMAP_SHORT_INFO_COUNT_64 };
  1107. // Start searching for available memory region past PAGEZERO, which is
  1108. // 4KB on 32-bit and 4GB on 64-bit.
  1109. mach_vm_address_t start_address =
  1110. (SANITIZER_WORDSIZE == 32) ? 0x000000001000 : 0x000100000000;
  1111. mach_vm_address_t address = start_address;
  1112. mach_vm_address_t free_begin = start_address;
  1113. kern_return_t kr = KERN_SUCCESS;
  1114. if (largest_gap_found) *largest_gap_found = 0;
  1115. if (max_occupied_addr) *max_occupied_addr = 0;
  1116. while (kr == KERN_SUCCESS) {
  1117. mach_vm_size_t vmsize = 0;
  1118. natural_t depth = 0;
  1119. RegionInfo vminfo;
  1120. mach_msg_type_number_t count = kRegionInfoSize;
  1121. kr = mach_vm_region_recurse(mach_task_self(), &address, &vmsize, &depth,
  1122. (vm_region_info_t)&vminfo, &count);
  1123. if (kr == KERN_INVALID_ADDRESS) {
  1124. // No more regions beyond "address", consider the gap at the end of VM.
  1125. address = GetMaxVirtualAddress() + 1;
  1126. vmsize = 0;
  1127. } else {
  1128. if (max_occupied_addr) *max_occupied_addr = address + vmsize;
  1129. }
  1130. if (free_begin != address) {
  1131. // We found a free region [free_begin..address-1].
  1132. uptr gap_start = RoundUpTo((uptr)free_begin + left_padding, alignment);
  1133. uptr gap_end = RoundDownTo((uptr)address, alignment);
  1134. uptr gap_size = gap_end > gap_start ? gap_end - gap_start : 0;
  1135. if (size < gap_size) {
  1136. return gap_start;
  1137. }
  1138. if (largest_gap_found && *largest_gap_found < gap_size) {
  1139. *largest_gap_found = gap_size;
  1140. }
  1141. }
  1142. // Move to the next region.
  1143. address += vmsize;
  1144. free_begin = address;
  1145. }
  1146. // We looked at all free regions and could not find one large enough.
  1147. return 0;
  1148. }
  1149. // FIXME implement on this platform.
  1150. void GetMemoryProfile(fill_profile_f cb, uptr *stats) {}
  1151. void SignalContext::DumpAllRegisters(void *context) {
  1152. Report("Register values:\n");
  1153. ucontext_t *ucontext = (ucontext_t*)context;
  1154. # define DUMPREG64(r) \
  1155. Printf("%s = 0x%016llx ", #r, ucontext->uc_mcontext->__ss.__ ## r);
  1156. # define DUMPREGA64(r) \
  1157. Printf(" %s = 0x%016llx ", #r, AARCH64_GET_REG(r));
  1158. # define DUMPREG32(r) \
  1159. Printf("%s = 0x%08x ", #r, ucontext->uc_mcontext->__ss.__ ## r);
  1160. # define DUMPREG_(r) Printf(" "); DUMPREG(r);
  1161. # define DUMPREG__(r) Printf(" "); DUMPREG(r);
  1162. # define DUMPREG___(r) Printf(" "); DUMPREG(r);
  1163. # if defined(__x86_64__)
  1164. # define DUMPREG(r) DUMPREG64(r)
  1165. DUMPREG(rax); DUMPREG(rbx); DUMPREG(rcx); DUMPREG(rdx); Printf("\n");
  1166. DUMPREG(rdi); DUMPREG(rsi); DUMPREG(rbp); DUMPREG(rsp); Printf("\n");
  1167. DUMPREG_(r8); DUMPREG_(r9); DUMPREG(r10); DUMPREG(r11); Printf("\n");
  1168. DUMPREG(r12); DUMPREG(r13); DUMPREG(r14); DUMPREG(r15); Printf("\n");
  1169. # elif defined(__i386__)
  1170. # define DUMPREG(r) DUMPREG32(r)
  1171. DUMPREG(eax); DUMPREG(ebx); DUMPREG(ecx); DUMPREG(edx); Printf("\n");
  1172. DUMPREG(edi); DUMPREG(esi); DUMPREG(ebp); DUMPREG(esp); Printf("\n");
  1173. # elif defined(__aarch64__)
  1174. # define DUMPREG(r) DUMPREG64(r)
  1175. DUMPREG_(x[0]); DUMPREG_(x[1]); DUMPREG_(x[2]); DUMPREG_(x[3]); Printf("\n");
  1176. DUMPREG_(x[4]); DUMPREG_(x[5]); DUMPREG_(x[6]); DUMPREG_(x[7]); Printf("\n");
  1177. DUMPREG_(x[8]); DUMPREG_(x[9]); DUMPREG(x[10]); DUMPREG(x[11]); Printf("\n");
  1178. DUMPREG(x[12]); DUMPREG(x[13]); DUMPREG(x[14]); DUMPREG(x[15]); Printf("\n");
  1179. DUMPREG(x[16]); DUMPREG(x[17]); DUMPREG(x[18]); DUMPREG(x[19]); Printf("\n");
  1180. DUMPREG(x[20]); DUMPREG(x[21]); DUMPREG(x[22]); DUMPREG(x[23]); Printf("\n");
  1181. DUMPREG(x[24]); DUMPREG(x[25]); DUMPREG(x[26]); DUMPREG(x[27]); Printf("\n");
  1182. DUMPREG(x[28]); DUMPREGA64(fp); DUMPREGA64(lr); DUMPREGA64(sp); Printf("\n");
  1183. # elif defined(__arm__)
  1184. # define DUMPREG(r) DUMPREG32(r)
  1185. DUMPREG_(r[0]); DUMPREG_(r[1]); DUMPREG_(r[2]); DUMPREG_(r[3]); Printf("\n");
  1186. DUMPREG_(r[4]); DUMPREG_(r[5]); DUMPREG_(r[6]); DUMPREG_(r[7]); Printf("\n");
  1187. DUMPREG_(r[8]); DUMPREG_(r[9]); DUMPREG(r[10]); DUMPREG(r[11]); Printf("\n");
  1188. DUMPREG(r[12]); DUMPREG___(sp); DUMPREG___(lr); DUMPREG___(pc); Printf("\n");
  1189. # else
  1190. # error "Unknown architecture"
  1191. # endif
  1192. # undef DUMPREG64
  1193. # undef DUMPREG32
  1194. # undef DUMPREG_
  1195. # undef DUMPREG__
  1196. # undef DUMPREG___
  1197. # undef DUMPREG
  1198. }
  1199. static inline bool CompareBaseAddress(const LoadedModule &a,
  1200. const LoadedModule &b) {
  1201. return a.base_address() < b.base_address();
  1202. }
  1203. void FormatUUID(char *out, uptr size, const u8 *uuid) {
  1204. internal_snprintf(out, size,
  1205. "<%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-"
  1206. "%02X%02X%02X%02X%02X%02X>",
  1207. uuid[0], uuid[1], uuid[2], uuid[3], uuid[4], uuid[5],
  1208. uuid[6], uuid[7], uuid[8], uuid[9], uuid[10], uuid[11],
  1209. uuid[12], uuid[13], uuid[14], uuid[15]);
  1210. }
  1211. void DumpProcessMap() {
  1212. Printf("Process module map:\n");
  1213. MemoryMappingLayout memory_mapping(false);
  1214. InternalMmapVector<LoadedModule> modules;
  1215. modules.reserve(128);
  1216. memory_mapping.DumpListOfModules(&modules);
  1217. Sort(modules.data(), modules.size(), CompareBaseAddress);
  1218. for (uptr i = 0; i < modules.size(); ++i) {
  1219. char uuid_str[128];
  1220. FormatUUID(uuid_str, sizeof(uuid_str), modules[i].uuid());
  1221. Printf("0x%zx-0x%zx %s (%s) %s\n", modules[i].base_address(),
  1222. modules[i].max_executable_address(), modules[i].full_name(),
  1223. ModuleArchToString(modules[i].arch()), uuid_str);
  1224. }
  1225. Printf("End of module map.\n");
  1226. }
  1227. void CheckNoDeepBind(const char *filename, int flag) {
  1228. // Do nothing.
  1229. }
  1230. bool GetRandom(void *buffer, uptr length, bool blocking) {
  1231. if (!buffer || !length || length > 256)
  1232. return false;
  1233. // arc4random never fails.
  1234. REAL(arc4random_buf)(buffer, length);
  1235. return true;
  1236. }
  1237. u32 GetNumberOfCPUs() {
  1238. return (u32)sysconf(_SC_NPROCESSORS_ONLN);
  1239. }
  1240. void InitializePlatformCommonFlags(CommonFlags *cf) {}
  1241. } // namespace __sanitizer
  1242. #endif // SANITIZER_MAC