sanitizer_linux.cc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. //===-- sanitizer_linux.cc ------------------------------------------------===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // This file is shared between AddressSanitizer and ThreadSanitizer
  9. // run-time libraries and implements linux-specific functions from
  10. // sanitizer_libc.h.
  11. //===----------------------------------------------------------------------===//
  12. #include "sanitizer_platform.h"
  13. #if SANITIZER_FREEBSD || SANITIZER_LINUX
  14. #include "sanitizer_common.h"
  15. #include "sanitizer_flags.h"
  16. #include "sanitizer_internal_defs.h"
  17. #include "sanitizer_libc.h"
  18. #include "sanitizer_linux.h"
  19. #include "sanitizer_mutex.h"
  20. #include "sanitizer_placement_new.h"
  21. #include "sanitizer_procmaps.h"
  22. #include "sanitizer_stacktrace.h"
  23. #include "sanitizer_symbolizer.h"
  24. #if !SANITIZER_FREEBSD
  25. #include <asm/param.h>
  26. #endif
  27. #include <dlfcn.h>
  28. #include <errno.h>
  29. #include <fcntl.h>
  30. #if !SANITIZER_ANDROID
  31. #include <link.h>
  32. #endif
  33. #include <pthread.h>
  34. #include <sched.h>
  35. #include <sys/mman.h>
  36. #include <sys/ptrace.h>
  37. #include <sys/resource.h>
  38. #include <sys/stat.h>
  39. #include <sys/syscall.h>
  40. #include <sys/time.h>
  41. #include <sys/types.h>
  42. #include <unistd.h>
  43. #if SANITIZER_FREEBSD
  44. #include <sys/sysctl.h>
  45. #include <machine/atomic.h>
  46. extern "C" {
  47. // <sys/umtx.h> must be included after <errno.h> and <sys/types.h> on
  48. // FreeBSD 9.2 and 10.0.
  49. #include <sys/umtx.h>
  50. }
  51. extern char **environ; // provided by crt1
  52. #endif // SANITIZER_FREEBSD
  53. #if !SANITIZER_ANDROID
  54. #include <sys/signal.h>
  55. #endif
  56. #if SANITIZER_ANDROID
  57. #include <android/log.h>
  58. #include <sys/system_properties.h>
  59. #endif
  60. #if SANITIZER_LINUX
  61. // <linux/time.h>
  62. struct kernel_timeval {
  63. long tv_sec;
  64. long tv_usec;
  65. };
  66. // <linux/futex.h> is broken on some linux distributions.
  67. const int FUTEX_WAIT = 0;
  68. const int FUTEX_WAKE = 1;
  69. #endif // SANITIZER_LINUX
  70. // Are we using 32-bit or 64-bit Linux syscalls?
  71. // x32 (which defines __x86_64__) has SANITIZER_WORDSIZE == 32
  72. // but it still needs to use 64-bit syscalls.
  73. #if SANITIZER_LINUX && (defined(__x86_64__) || SANITIZER_WORDSIZE == 64)
  74. # define SANITIZER_LINUX_USES_64BIT_SYSCALLS 1
  75. #else
  76. # define SANITIZER_LINUX_USES_64BIT_SYSCALLS 0
  77. #endif
  78. namespace __sanitizer {
  79. #if SANITIZER_LINUX && defined(__x86_64__)
  80. #include "sanitizer_syscall_linux_x86_64.inc"
  81. #else
  82. #include "sanitizer_syscall_generic.inc"
  83. #endif
  84. // --------------- sanitizer_libc.h
  85. uptr internal_mmap(void *addr, uptr length, int prot, int flags,
  86. int fd, u64 offset) {
  87. #if SANITIZER_FREEBSD || SANITIZER_LINUX_USES_64BIT_SYSCALLS
  88. return internal_syscall(SYSCALL(mmap), (uptr)addr, length, prot, flags, fd,
  89. offset);
  90. #else
  91. return internal_syscall(SYSCALL(mmap2), addr, length, prot, flags, fd,
  92. offset);
  93. #endif
  94. }
  95. uptr internal_munmap(void *addr, uptr length) {
  96. return internal_syscall(SYSCALL(munmap), (uptr)addr, length);
  97. }
  98. uptr internal_close(fd_t fd) {
  99. return internal_syscall(SYSCALL(close), fd);
  100. }
  101. uptr internal_open(const char *filename, int flags) {
  102. #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
  103. return internal_syscall(SYSCALL(openat), AT_FDCWD, (uptr)filename, flags);
  104. #else
  105. return internal_syscall(SYSCALL(open), (uptr)filename, flags);
  106. #endif
  107. }
  108. uptr internal_open(const char *filename, int flags, u32 mode) {
  109. #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
  110. return internal_syscall(SYSCALL(openat), AT_FDCWD, (uptr)filename, flags,
  111. mode);
  112. #else
  113. return internal_syscall(SYSCALL(open), (uptr)filename, flags, mode);
  114. #endif
  115. }
  116. uptr OpenFile(const char *filename, bool write) {
  117. return internal_open(filename,
  118. write ? O_RDWR | O_CREAT /*| O_CLOEXEC*/ : O_RDONLY, 0660);
  119. }
  120. uptr internal_read(fd_t fd, void *buf, uptr count) {
  121. sptr res;
  122. HANDLE_EINTR(res, (sptr)internal_syscall(SYSCALL(read), fd, (uptr)buf,
  123. count));
  124. return res;
  125. }
  126. uptr internal_write(fd_t fd, const void *buf, uptr count) {
  127. sptr res;
  128. HANDLE_EINTR(res, (sptr)internal_syscall(SYSCALL(write), fd, (uptr)buf,
  129. count));
  130. return res;
  131. }
  132. uptr internal_ftruncate(fd_t fd, uptr size) {
  133. sptr res;
  134. HANDLE_EINTR(res, (sptr)internal_syscall(SYSCALL(ftruncate), fd, size));
  135. return res;
  136. }
  137. #if !SANITIZER_LINUX_USES_64BIT_SYSCALLS && !SANITIZER_FREEBSD
  138. static void stat64_to_stat(struct stat64 *in, struct stat *out) {
  139. internal_memset(out, 0, sizeof(*out));
  140. out->st_dev = in->st_dev;
  141. out->st_ino = in->st_ino;
  142. out->st_mode = in->st_mode;
  143. out->st_nlink = in->st_nlink;
  144. out->st_uid = in->st_uid;
  145. out->st_gid = in->st_gid;
  146. out->st_rdev = in->st_rdev;
  147. out->st_size = in->st_size;
  148. out->st_blksize = in->st_blksize;
  149. out->st_blocks = in->st_blocks;
  150. out->st_atime = in->st_atime;
  151. out->st_mtime = in->st_mtime;
  152. out->st_ctime = in->st_ctime;
  153. out->st_ino = in->st_ino;
  154. }
  155. #endif
  156. uptr internal_stat(const char *path, void *buf) {
  157. #if SANITIZER_FREEBSD
  158. return internal_syscall(SYSCALL(stat), path, buf);
  159. #elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
  160. return internal_syscall(SYSCALL(newfstatat), AT_FDCWD, (uptr)path,
  161. (uptr)buf, 0);
  162. #elif SANITIZER_LINUX_USES_64BIT_SYSCALLS
  163. return internal_syscall(SYSCALL(stat), (uptr)path, (uptr)buf);
  164. #else
  165. struct stat64 buf64;
  166. int res = internal_syscall(SYSCALL(stat64), path, &buf64);
  167. stat64_to_stat(&buf64, (struct stat *)buf);
  168. return res;
  169. #endif
  170. }
  171. uptr internal_lstat(const char *path, void *buf) {
  172. #if SANITIZER_FREEBSD
  173. return internal_syscall(SYSCALL(lstat), path, buf);
  174. #elif SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
  175. return internal_syscall(SYSCALL(newfstatat), AT_FDCWD, (uptr)path,
  176. (uptr)buf, AT_SYMLINK_NOFOLLOW);
  177. #elif SANITIZER_LINUX_USES_64BIT_SYSCALLS
  178. return internal_syscall(SYSCALL(lstat), (uptr)path, (uptr)buf);
  179. #else
  180. struct stat64 buf64;
  181. int res = internal_syscall(SYSCALL(lstat64), path, &buf64);
  182. stat64_to_stat(&buf64, (struct stat *)buf);
  183. return res;
  184. #endif
  185. }
  186. uptr internal_fstat(fd_t fd, void *buf) {
  187. #if SANITIZER_FREEBSD || SANITIZER_LINUX_USES_64BIT_SYSCALLS
  188. return internal_syscall(SYSCALL(fstat), fd, (uptr)buf);
  189. #else
  190. struct stat64 buf64;
  191. int res = internal_syscall(SYSCALL(fstat64), fd, &buf64);
  192. stat64_to_stat(&buf64, (struct stat *)buf);
  193. return res;
  194. #endif
  195. }
  196. uptr internal_filesize(fd_t fd) {
  197. struct stat st;
  198. if (internal_fstat(fd, &st))
  199. return -1;
  200. return (uptr)st.st_size;
  201. }
  202. uptr internal_dup2(int oldfd, int newfd) {
  203. #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
  204. return internal_syscall(SYSCALL(dup3), oldfd, newfd, 0);
  205. #else
  206. return internal_syscall(SYSCALL(dup2), oldfd, newfd);
  207. #endif
  208. }
  209. uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
  210. #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
  211. return internal_syscall(SYSCALL(readlinkat), AT_FDCWD,
  212. (uptr)path, (uptr)buf, bufsize);
  213. #else
  214. return internal_syscall(SYSCALL(readlink), (uptr)path, (uptr)buf, bufsize);
  215. #endif
  216. }
  217. uptr internal_unlink(const char *path) {
  218. #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
  219. return internal_syscall(SYSCALL(unlinkat), AT_FDCWD, (uptr)path, 0);
  220. #else
  221. return internal_syscall(SYSCALL(unlink), (uptr)path);
  222. #endif
  223. }
  224. uptr internal_rename(const char *oldpath, const char *newpath) {
  225. #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
  226. return internal_syscall(SYSCALL(renameat), AT_FDCWD, (uptr)oldpath, AT_FDCWD,
  227. (uptr)newpath);
  228. #else
  229. return internal_syscall(SYSCALL(rename), (uptr)oldpath, (uptr)newpath);
  230. #endif
  231. }
  232. uptr internal_sched_yield() {
  233. return internal_syscall(SYSCALL(sched_yield));
  234. }
  235. void internal__exit(int exitcode) {
  236. #if SANITIZER_FREEBSD
  237. internal_syscall(SYSCALL(exit), exitcode);
  238. #else
  239. internal_syscall(SYSCALL(exit_group), exitcode);
  240. #endif
  241. Die(); // Unreachable.
  242. }
  243. uptr internal_execve(const char *filename, char *const argv[],
  244. char *const envp[]) {
  245. return internal_syscall(SYSCALL(execve), (uptr)filename, (uptr)argv,
  246. (uptr)envp);
  247. }
  248. // ----------------- sanitizer_common.h
  249. bool FileExists(const char *filename) {
  250. struct stat st;
  251. #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
  252. if (internal_syscall(SYSCALL(newfstatat), AT_FDCWD, filename, &st, 0))
  253. #else
  254. if (internal_stat(filename, &st))
  255. #endif
  256. return false;
  257. // Sanity check: filename is a regular file.
  258. return S_ISREG(st.st_mode);
  259. }
  260. uptr GetTid() {
  261. #if SANITIZER_FREEBSD
  262. return (uptr)pthread_self();
  263. #else
  264. return internal_syscall(SYSCALL(gettid));
  265. #endif
  266. }
  267. u64 NanoTime() {
  268. #if SANITIZER_FREEBSD
  269. timeval tv;
  270. #else
  271. kernel_timeval tv;
  272. #endif
  273. internal_memset(&tv, 0, sizeof(tv));
  274. internal_syscall(SYSCALL(gettimeofday), (uptr)&tv, 0);
  275. return (u64)tv.tv_sec * 1000*1000*1000 + tv.tv_usec * 1000;
  276. }
  277. // Like getenv, but reads env directly from /proc (on Linux) or parses the
  278. // 'environ' array (on FreeBSD) and does not use libc. This function should be
  279. // called first inside __asan_init.
  280. const char *GetEnv(const char *name) {
  281. #if SANITIZER_FREEBSD
  282. if (::environ != 0) {
  283. uptr NameLen = internal_strlen(name);
  284. for (char **Env = ::environ; *Env != 0; Env++) {
  285. if (internal_strncmp(*Env, name, NameLen) == 0 && (*Env)[NameLen] == '=')
  286. return (*Env) + NameLen + 1;
  287. }
  288. }
  289. return 0; // Not found.
  290. #elif SANITIZER_LINUX
  291. static char *environ;
  292. static uptr len;
  293. static bool inited;
  294. if (!inited) {
  295. inited = true;
  296. uptr environ_size;
  297. len = ReadFileToBuffer("/proc/self/environ",
  298. &environ, &environ_size, 1 << 26);
  299. }
  300. if (!environ || len == 0) return 0;
  301. uptr namelen = internal_strlen(name);
  302. const char *p = environ;
  303. while (*p != '\0') { // will happen at the \0\0 that terminates the buffer
  304. // proc file has the format NAME=value\0NAME=value\0NAME=value\0...
  305. const char* endp =
  306. (char*)internal_memchr(p, '\0', len - (p - environ));
  307. if (endp == 0) // this entry isn't NUL terminated
  308. return 0;
  309. else if (!internal_memcmp(p, name, namelen) && p[namelen] == '=') // Match.
  310. return p + namelen + 1; // point after =
  311. p = endp + 1;
  312. }
  313. return 0; // Not found.
  314. #else
  315. #error "Unsupported platform"
  316. #endif
  317. }
  318. extern "C" {
  319. SANITIZER_WEAK_ATTRIBUTE extern void *__libc_stack_end;
  320. }
  321. #if !SANITIZER_GO
  322. static void ReadNullSepFileToArray(const char *path, char ***arr,
  323. int arr_size) {
  324. char *buff;
  325. uptr buff_size = 0;
  326. *arr = (char **)MmapOrDie(arr_size * sizeof(char *), "NullSepFileArray");
  327. ReadFileToBuffer(path, &buff, &buff_size, 1024 * 1024);
  328. (*arr)[0] = buff;
  329. int count, i;
  330. for (count = 1, i = 1; ; i++) {
  331. if (buff[i] == 0) {
  332. if (buff[i+1] == 0) break;
  333. (*arr)[count] = &buff[i+1];
  334. CHECK_LE(count, arr_size - 1); // FIXME: make this more flexible.
  335. count++;
  336. }
  337. }
  338. (*arr)[count] = 0;
  339. }
  340. #endif
  341. static void GetArgsAndEnv(char*** argv, char*** envp) {
  342. #if !SANITIZER_GO
  343. if (&__libc_stack_end) {
  344. #endif
  345. uptr* stack_end = (uptr*)__libc_stack_end;
  346. int argc = *stack_end;
  347. *argv = (char**)(stack_end + 1);
  348. *envp = (char**)(stack_end + argc + 2);
  349. #if !SANITIZER_GO
  350. } else {
  351. static const int kMaxArgv = 2000, kMaxEnvp = 2000;
  352. ReadNullSepFileToArray("/proc/self/cmdline", argv, kMaxArgv);
  353. ReadNullSepFileToArray("/proc/self/environ", envp, kMaxEnvp);
  354. }
  355. #endif
  356. }
  357. void ReExec() {
  358. char **argv, **envp;
  359. GetArgsAndEnv(&argv, &envp);
  360. uptr rv = internal_execve("/proc/self/exe", argv, envp);
  361. int rverrno;
  362. CHECK_EQ(internal_iserror(rv, &rverrno), true);
  363. Printf("execve failed, errno %d\n", rverrno);
  364. Die();
  365. }
  366. // Stub implementation of GetThreadStackAndTls for Go.
  367. #if SANITIZER_GO
  368. void GetThreadStackAndTls(bool main, uptr *stk_addr, uptr *stk_size,
  369. uptr *tls_addr, uptr *tls_size) {
  370. *stk_addr = 0;
  371. *stk_size = 0;
  372. *tls_addr = 0;
  373. *tls_size = 0;
  374. }
  375. #endif // SANITIZER_GO
  376. enum MutexState {
  377. MtxUnlocked = 0,
  378. MtxLocked = 1,
  379. MtxSleeping = 2
  380. };
  381. BlockingMutex::BlockingMutex(LinkerInitialized) {
  382. CHECK_EQ(owner_, 0);
  383. }
  384. BlockingMutex::BlockingMutex() {
  385. internal_memset(this, 0, sizeof(*this));
  386. }
  387. void BlockingMutex::Lock() {
  388. atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
  389. if (atomic_exchange(m, MtxLocked, memory_order_acquire) == MtxUnlocked)
  390. return;
  391. while (atomic_exchange(m, MtxSleeping, memory_order_acquire) != MtxUnlocked) {
  392. #if SANITIZER_FREEBSD
  393. _umtx_op(m, UMTX_OP_WAIT_UINT, MtxSleeping, 0, 0);
  394. #else
  395. internal_syscall(SYSCALL(futex), (uptr)m, FUTEX_WAIT, MtxSleeping, 0, 0, 0);
  396. #endif
  397. }
  398. }
  399. void BlockingMutex::Unlock() {
  400. atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
  401. u32 v = atomic_exchange(m, MtxUnlocked, memory_order_relaxed);
  402. CHECK_NE(v, MtxUnlocked);
  403. if (v == MtxSleeping) {
  404. #if SANITIZER_FREEBSD
  405. _umtx_op(m, UMTX_OP_WAKE, 1, 0, 0);
  406. #else
  407. internal_syscall(SYSCALL(futex), (uptr)m, FUTEX_WAKE, 1, 0, 0, 0);
  408. #endif
  409. }
  410. }
  411. void BlockingMutex::CheckLocked() {
  412. atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
  413. CHECK_NE(MtxUnlocked, atomic_load(m, memory_order_relaxed));
  414. }
  415. // ----------------- sanitizer_linux.h
  416. // The actual size of this structure is specified by d_reclen.
  417. // Note that getdents64 uses a different structure format. We only provide the
  418. // 32-bit syscall here.
  419. struct linux_dirent {
  420. #if SANITIZER_X32
  421. u64 d_ino;
  422. u64 d_off;
  423. #else
  424. unsigned long d_ino;
  425. unsigned long d_off;
  426. #endif
  427. unsigned short d_reclen;
  428. char d_name[256];
  429. };
  430. // Syscall wrappers.
  431. uptr internal_ptrace(int request, int pid, void *addr, void *data) {
  432. return internal_syscall(SYSCALL(ptrace), request, pid, (uptr)addr,
  433. (uptr)data);
  434. }
  435. uptr internal_waitpid(int pid, int *status, int options) {
  436. return internal_syscall(SYSCALL(wait4), pid, (uptr)status, options,
  437. 0 /* rusage */);
  438. }
  439. uptr internal_getpid() {
  440. return internal_syscall(SYSCALL(getpid));
  441. }
  442. uptr internal_getppid() {
  443. return internal_syscall(SYSCALL(getppid));
  444. }
  445. uptr internal_getdents(fd_t fd, struct linux_dirent *dirp, unsigned int count) {
  446. #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
  447. return internal_syscall(SYSCALL(getdents64), fd, (uptr)dirp, count);
  448. #else
  449. return internal_syscall(SYSCALL(getdents), fd, (uptr)dirp, count);
  450. #endif
  451. }
  452. uptr internal_lseek(fd_t fd, OFF_T offset, int whence) {
  453. return internal_syscall(SYSCALL(lseek), fd, offset, whence);
  454. }
  455. #if SANITIZER_LINUX
  456. uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5) {
  457. return internal_syscall(SYSCALL(prctl), option, arg2, arg3, arg4, arg5);
  458. }
  459. #endif
  460. uptr internal_sigaltstack(const struct sigaltstack *ss,
  461. struct sigaltstack *oss) {
  462. return internal_syscall(SYSCALL(sigaltstack), (uptr)ss, (uptr)oss);
  463. }
  464. int internal_fork() {
  465. #if SANITIZER_USES_CANONICAL_LINUX_SYSCALLS
  466. return internal_syscall(SYSCALL(clone), SIGCHLD, 0);
  467. #else
  468. return internal_syscall(SYSCALL(fork));
  469. #endif
  470. }
  471. #if SANITIZER_LINUX
  472. // Doesn't set sa_restorer, use with caution (see below).
  473. int internal_sigaction_norestorer(int signum, const void *act, void *oldact) {
  474. __sanitizer_kernel_sigaction_t k_act, k_oldact;
  475. internal_memset(&k_act, 0, sizeof(__sanitizer_kernel_sigaction_t));
  476. internal_memset(&k_oldact, 0, sizeof(__sanitizer_kernel_sigaction_t));
  477. const __sanitizer_sigaction *u_act = (__sanitizer_sigaction *)act;
  478. __sanitizer_sigaction *u_oldact = (__sanitizer_sigaction *)oldact;
  479. if (u_act) {
  480. k_act.handler = u_act->handler;
  481. k_act.sigaction = u_act->sigaction;
  482. internal_memcpy(&k_act.sa_mask, &u_act->sa_mask,
  483. sizeof(__sanitizer_kernel_sigset_t));
  484. k_act.sa_flags = u_act->sa_flags;
  485. // FIXME: most often sa_restorer is unset, however the kernel requires it
  486. // to point to a valid signal restorer that calls the rt_sigreturn syscall.
  487. // If sa_restorer passed to the kernel is NULL, the program may crash upon
  488. // signal delivery or fail to unwind the stack in the signal handler.
  489. // libc implementation of sigaction() passes its own restorer to
  490. // rt_sigaction, so we need to do the same (we'll need to reimplement the
  491. // restorers; for x86_64 the restorer address can be obtained from
  492. // oldact->sa_restorer upon a call to sigaction(xxx, NULL, oldact).
  493. k_act.sa_restorer = u_act->sa_restorer;
  494. }
  495. uptr result = internal_syscall(SYSCALL(rt_sigaction), (uptr)signum,
  496. (uptr)(u_act ? &k_act : NULL),
  497. (uptr)(u_oldact ? &k_oldact : NULL),
  498. (uptr)sizeof(__sanitizer_kernel_sigset_t));
  499. if ((result == 0) && u_oldact) {
  500. u_oldact->handler = k_oldact.handler;
  501. u_oldact->sigaction = k_oldact.sigaction;
  502. internal_memcpy(&u_oldact->sa_mask, &k_oldact.sa_mask,
  503. sizeof(__sanitizer_kernel_sigset_t));
  504. u_oldact->sa_flags = k_oldact.sa_flags;
  505. u_oldact->sa_restorer = k_oldact.sa_restorer;
  506. }
  507. return result;
  508. }
  509. #endif // SANITIZER_LINUX
  510. uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
  511. __sanitizer_sigset_t *oldset) {
  512. #if SANITIZER_FREEBSD
  513. return internal_syscall(SYSCALL(sigprocmask), how, set, oldset);
  514. #else
  515. __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
  516. __sanitizer_kernel_sigset_t *k_oldset = (__sanitizer_kernel_sigset_t *)oldset;
  517. return internal_syscall(SYSCALL(rt_sigprocmask), (uptr)how,
  518. (uptr)&k_set->sig[0], (uptr)&k_oldset->sig[0],
  519. sizeof(__sanitizer_kernel_sigset_t));
  520. #endif
  521. }
  522. void internal_sigfillset(__sanitizer_sigset_t *set) {
  523. internal_memset(set, 0xff, sizeof(*set));
  524. }
  525. #if SANITIZER_LINUX
  526. void internal_sigdelset(__sanitizer_sigset_t *set, int signum) {
  527. signum -= 1;
  528. CHECK_GE(signum, 0);
  529. CHECK_LT(signum, sizeof(*set) * 8);
  530. __sanitizer_kernel_sigset_t *k_set = (__sanitizer_kernel_sigset_t *)set;
  531. const uptr idx = signum / (sizeof(k_set->sig[0]) * 8);
  532. const uptr bit = signum % (sizeof(k_set->sig[0]) * 8);
  533. k_set->sig[idx] &= ~(1 << bit);
  534. }
  535. #endif // SANITIZER_LINUX
  536. // ThreadLister implementation.
  537. ThreadLister::ThreadLister(int pid)
  538. : pid_(pid),
  539. descriptor_(-1),
  540. buffer_(4096),
  541. error_(true),
  542. entry_((struct linux_dirent *)buffer_.data()),
  543. bytes_read_(0) {
  544. char task_directory_path[80];
  545. internal_snprintf(task_directory_path, sizeof(task_directory_path),
  546. "/proc/%d/task/", pid);
  547. uptr openrv = internal_open(task_directory_path, O_RDONLY | O_DIRECTORY);
  548. if (internal_iserror(openrv)) {
  549. error_ = true;
  550. Report("Can't open /proc/%d/task for reading.\n", pid);
  551. } else {
  552. error_ = false;
  553. descriptor_ = openrv;
  554. }
  555. }
  556. int ThreadLister::GetNextTID() {
  557. int tid = -1;
  558. do {
  559. if (error_)
  560. return -1;
  561. if ((char *)entry_ >= &buffer_[bytes_read_] && !GetDirectoryEntries())
  562. return -1;
  563. if (entry_->d_ino != 0 && entry_->d_name[0] >= '0' &&
  564. entry_->d_name[0] <= '9') {
  565. // Found a valid tid.
  566. tid = (int)internal_atoll(entry_->d_name);
  567. }
  568. entry_ = (struct linux_dirent *)(((char *)entry_) + entry_->d_reclen);
  569. } while (tid < 0);
  570. return tid;
  571. }
  572. void ThreadLister::Reset() {
  573. if (error_ || descriptor_ < 0)
  574. return;
  575. internal_lseek(descriptor_, 0, SEEK_SET);
  576. }
  577. ThreadLister::~ThreadLister() {
  578. if (descriptor_ >= 0)
  579. internal_close(descriptor_);
  580. }
  581. bool ThreadLister::error() { return error_; }
  582. bool ThreadLister::GetDirectoryEntries() {
  583. CHECK_GE(descriptor_, 0);
  584. CHECK_NE(error_, true);
  585. bytes_read_ = internal_getdents(descriptor_,
  586. (struct linux_dirent *)buffer_.data(),
  587. buffer_.size());
  588. if (internal_iserror(bytes_read_)) {
  589. Report("Can't read directory entries from /proc/%d/task.\n", pid_);
  590. error_ = true;
  591. return false;
  592. } else if (bytes_read_ == 0) {
  593. return false;
  594. }
  595. entry_ = (struct linux_dirent *)buffer_.data();
  596. return true;
  597. }
  598. uptr GetPageSize() {
  599. #if SANITIZER_LINUX && (defined(__x86_64__) || defined(__i386__))
  600. return EXEC_PAGESIZE;
  601. #else
  602. return sysconf(_SC_PAGESIZE); // EXEC_PAGESIZE may not be trustworthy.
  603. #endif
  604. }
  605. static char proc_self_exe_cache_str[kMaxPathLength];
  606. static uptr proc_self_exe_cache_len = 0;
  607. uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) {
  608. if (proc_self_exe_cache_len > 0) {
  609. // If available, use the cached module name.
  610. uptr module_name_len =
  611. internal_snprintf(buf, buf_len, "%s", proc_self_exe_cache_str);
  612. CHECK_LT(module_name_len, buf_len);
  613. return module_name_len;
  614. }
  615. #if SANITIZER_FREEBSD
  616. const int Mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
  617. size_t Size = buf_len;
  618. bool IsErr = (sysctl(Mib, 4, buf, &Size, NULL, 0) != 0);
  619. int readlink_error = IsErr ? errno : 0;
  620. uptr module_name_len = Size;
  621. #else
  622. uptr module_name_len = internal_readlink(
  623. "/proc/self/exe", buf, buf_len);
  624. int readlink_error;
  625. bool IsErr = internal_iserror(module_name_len, &readlink_error);
  626. #endif
  627. if (IsErr) {
  628. // We can't read /proc/self/exe for some reason, assume the name of the
  629. // binary is unknown.
  630. Report("WARNING: readlink(\"/proc/self/exe\") failed with errno %d, "
  631. "some stack frames may not be symbolized\n", readlink_error);
  632. module_name_len = internal_snprintf(buf, buf_len, "/proc/self/exe");
  633. CHECK_LT(module_name_len, buf_len);
  634. }
  635. return module_name_len;
  636. }
  637. void CacheBinaryName() {
  638. if (!proc_self_exe_cache_len) {
  639. proc_self_exe_cache_len =
  640. ReadBinaryName(proc_self_exe_cache_str, kMaxPathLength);
  641. }
  642. }
  643. // Match full names of the form /path/to/base_name{-,.}*
  644. bool LibraryNameIs(const char *full_name, const char *base_name) {
  645. const char *name = full_name;
  646. // Strip path.
  647. while (*name != '\0') name++;
  648. while (name > full_name && *name != '/') name--;
  649. if (*name == '/') name++;
  650. uptr base_name_length = internal_strlen(base_name);
  651. if (internal_strncmp(name, base_name, base_name_length)) return false;
  652. return (name[base_name_length] == '-' || name[base_name_length] == '.');
  653. }
  654. #if !SANITIZER_ANDROID
  655. // Call cb for each region mapped by map.
  656. void ForEachMappedRegion(link_map *map, void (*cb)(const void *, uptr)) {
  657. #if !SANITIZER_FREEBSD
  658. typedef ElfW(Phdr) Elf_Phdr;
  659. typedef ElfW(Ehdr) Elf_Ehdr;
  660. #endif // !SANITIZER_FREEBSD
  661. char *base = (char *)map->l_addr;
  662. Elf_Ehdr *ehdr = (Elf_Ehdr *)base;
  663. char *phdrs = base + ehdr->e_phoff;
  664. char *phdrs_end = phdrs + ehdr->e_phnum * ehdr->e_phentsize;
  665. // Find the segment with the minimum base so we can "relocate" the p_vaddr
  666. // fields. Typically ET_DYN objects (DSOs) have base of zero and ET_EXEC
  667. // objects have a non-zero base.
  668. uptr preferred_base = (uptr)-1;
  669. for (char *iter = phdrs; iter != phdrs_end; iter += ehdr->e_phentsize) {
  670. Elf_Phdr *phdr = (Elf_Phdr *)iter;
  671. if (phdr->p_type == PT_LOAD && preferred_base > (uptr)phdr->p_vaddr)
  672. preferred_base = (uptr)phdr->p_vaddr;
  673. }
  674. // Compute the delta from the real base to get a relocation delta.
  675. sptr delta = (uptr)base - preferred_base;
  676. // Now we can figure out what the loader really mapped.
  677. for (char *iter = phdrs; iter != phdrs_end; iter += ehdr->e_phentsize) {
  678. Elf_Phdr *phdr = (Elf_Phdr *)iter;
  679. if (phdr->p_type == PT_LOAD) {
  680. uptr seg_start = phdr->p_vaddr + delta;
  681. uptr seg_end = seg_start + phdr->p_memsz;
  682. // None of these values are aligned. We consider the ragged edges of the
  683. // load command as defined, since they are mapped from the file.
  684. seg_start = RoundDownTo(seg_start, GetPageSizeCached());
  685. seg_end = RoundUpTo(seg_end, GetPageSizeCached());
  686. cb((void *)seg_start, seg_end - seg_start);
  687. }
  688. }
  689. }
  690. #endif
  691. #if defined(__x86_64__) && SANITIZER_LINUX
  692. // We cannot use glibc's clone wrapper, because it messes with the child
  693. // task's TLS. It writes the PID and TID of the child task to its thread
  694. // descriptor, but in our case the child task shares the thread descriptor with
  695. // the parent (because we don't know how to allocate a new thread
  696. // descriptor to keep glibc happy). So the stock version of clone(), when
  697. // used with CLONE_VM, would end up corrupting the parent's thread descriptor.
  698. uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
  699. int *parent_tidptr, void *newtls, int *child_tidptr) {
  700. long long res;
  701. if (!fn || !child_stack)
  702. return -EINVAL;
  703. CHECK_EQ(0, (uptr)child_stack % 16);
  704. child_stack = (char *)child_stack - 2 * sizeof(unsigned long long);
  705. ((unsigned long long *)child_stack)[0] = (uptr)fn;
  706. ((unsigned long long *)child_stack)[1] = (uptr)arg;
  707. register void *r8 __asm__("r8") = newtls;
  708. register int *r10 __asm__("r10") = child_tidptr;
  709. __asm__ __volatile__(
  710. /* %rax = syscall(%rax = SYSCALL(clone),
  711. * %rdi = flags,
  712. * %rsi = child_stack,
  713. * %rdx = parent_tidptr,
  714. * %r8 = new_tls,
  715. * %r10 = child_tidptr)
  716. */
  717. "syscall\n"
  718. /* if (%rax != 0)
  719. * return;
  720. */
  721. "testq %%rax,%%rax\n"
  722. "jnz 1f\n"
  723. /* In the child. Terminate unwind chain. */
  724. // XXX: We should also terminate the CFI unwind chain
  725. // here. Unfortunately clang 3.2 doesn't support the
  726. // necessary CFI directives, so we skip that part.
  727. "xorq %%rbp,%%rbp\n"
  728. /* Call "fn(arg)". */
  729. "popq %%rax\n"
  730. "popq %%rdi\n"
  731. "call *%%rax\n"
  732. /* Call _exit(%rax). */
  733. "movq %%rax,%%rdi\n"
  734. "movq %2,%%rax\n"
  735. "syscall\n"
  736. /* Return to parent. */
  737. "1:\n"
  738. : "=a" (res)
  739. : "a"(SYSCALL(clone)), "i"(SYSCALL(exit)),
  740. "S"(child_stack),
  741. "D"(flags),
  742. "d"(parent_tidptr),
  743. "r"(r8),
  744. "r"(r10)
  745. : "rsp", "memory", "r11", "rcx");
  746. return res;
  747. }
  748. #endif // defined(__x86_64__) && SANITIZER_LINUX
  749. #if SANITIZER_ANDROID
  750. static atomic_uint8_t android_log_initialized;
  751. void AndroidLogInit() {
  752. atomic_store(&android_log_initialized, 1, memory_order_release);
  753. }
  754. // This thing is not, strictly speaking, async signal safe, but it does not seem
  755. // to cause any issues. Alternative is writing to log devices directly, but
  756. // their location and message format might change in the future, so we'd really
  757. // like to avoid that.
  758. void AndroidLogWrite(const char *buffer) {
  759. if (!atomic_load(&android_log_initialized, memory_order_acquire))
  760. return;
  761. char *copy = internal_strdup(buffer);
  762. char *p = copy;
  763. char *q;
  764. // __android_log_write has an implicit message length limit.
  765. // Print one line at a time.
  766. do {
  767. q = internal_strchr(p, '\n');
  768. if (q) *q = '\0';
  769. __android_log_write(ANDROID_LOG_INFO, NULL, p);
  770. if (q) p = q + 1;
  771. } while (q);
  772. InternalFree(copy);
  773. }
  774. void GetExtraActivationFlags(char *buf, uptr size) {
  775. CHECK(size > PROP_VALUE_MAX);
  776. __system_property_get("asan.options", buf);
  777. }
  778. #endif
  779. bool IsDeadlySignal(int signum) {
  780. return (signum == SIGSEGV) && common_flags()->handle_segv;
  781. }
  782. } // namespace __sanitizer
  783. #endif // SANITIZER_FREEBSD || SANITIZER_LINUX