sanitizer_netbsd.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. //===-- sanitizer_netbsd.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 Sanitizer run-time libraries and implements
  10. // NetBSD-specific functions from sanitizer_libc.h.
  11. //===----------------------------------------------------------------------===//
  12. #include "sanitizer_platform.h"
  13. #if SANITIZER_NETBSD
  14. #include "sanitizer_common.h"
  15. #include "sanitizer_flags.h"
  16. #include "sanitizer_getauxval.h"
  17. #include "sanitizer_internal_defs.h"
  18. #include "sanitizer_libc.h"
  19. #include "sanitizer_linux.h"
  20. #include "sanitizer_mutex.h"
  21. #include "sanitizer_placement_new.h"
  22. #include "sanitizer_procmaps.h"
  23. #include <sys/param.h>
  24. #include <sys/types.h>
  25. #include <sys/exec.h>
  26. #include <sys/mman.h>
  27. #include <sys/ptrace.h>
  28. #include <sys/resource.h>
  29. #include <sys/stat.h>
  30. #include <sys/syscall.h>
  31. #include <sys/sysctl.h>
  32. #include <sys/time.h>
  33. #include <dlfcn.h>
  34. #include <errno.h>
  35. #include <fcntl.h>
  36. #include <limits.h>
  37. #include <link.h>
  38. #include <lwp.h>
  39. #include <pthread.h>
  40. #include <sched.h>
  41. #include <signal.h>
  42. #include <ucontext.h>
  43. #include <unistd.h>
  44. extern "C" void *__mmap(void *, size_t, int, int, int, int,
  45. off_t) SANITIZER_WEAK_ATTRIBUTE;
  46. extern "C" int __sysctl(const int *, unsigned int, void *, size_t *,
  47. const void *, size_t) SANITIZER_WEAK_ATTRIBUTE;
  48. extern "C" int _sys_close(int) SANITIZER_WEAK_ATTRIBUTE;
  49. extern "C" int _sys_open(const char *, int, ...) SANITIZER_WEAK_ATTRIBUTE;
  50. extern "C" ssize_t _sys_read(int, void *, size_t) SANITIZER_WEAK_ATTRIBUTE;
  51. extern "C" ssize_t _sys_write(int, const void *,
  52. size_t) SANITIZER_WEAK_ATTRIBUTE;
  53. extern "C" int __ftruncate(int, int, off_t) SANITIZER_WEAK_ATTRIBUTE;
  54. extern "C" ssize_t _sys_readlink(const char *, char *,
  55. size_t) SANITIZER_WEAK_ATTRIBUTE;
  56. extern "C" int _sys_sched_yield() SANITIZER_WEAK_ATTRIBUTE;
  57. extern "C" int _sys___nanosleep50(const void *,
  58. void *) SANITIZER_WEAK_ATTRIBUTE;
  59. extern "C" int _sys_execve(const char *, char *const[],
  60. char *const[]) SANITIZER_WEAK_ATTRIBUTE;
  61. extern "C" off_t __lseek(int, int, off_t, int) SANITIZER_WEAK_ATTRIBUTE;
  62. extern "C" int __fork() SANITIZER_WEAK_ATTRIBUTE;
  63. extern "C" int _sys___sigprocmask14(int, const void *,
  64. void *) SANITIZER_WEAK_ATTRIBUTE;
  65. extern "C" int _sys___wait450(int wpid, int *, int,
  66. void *) SANITIZER_WEAK_ATTRIBUTE;
  67. namespace __sanitizer {
  68. static void *GetRealLibcAddress(const char *symbol) {
  69. void *real = dlsym(RTLD_NEXT, symbol);
  70. if (!real)
  71. real = dlsym(RTLD_DEFAULT, symbol);
  72. if (!real) {
  73. Printf("GetRealLibcAddress failed for symbol=%s", symbol);
  74. Die();
  75. }
  76. return real;
  77. }
  78. #define _REAL(func, ...) real##_##func(__VA_ARGS__)
  79. #define DEFINE__REAL(ret_type, func, ...) \
  80. static ret_type (*real_##func)(__VA_ARGS__) = NULL; \
  81. if (!real_##func) { \
  82. real_##func = (ret_type(*)(__VA_ARGS__))GetRealLibcAddress(#func); \
  83. } \
  84. CHECK(real_##func);
  85. // --------------- sanitizer_libc.h
  86. uptr internal_mmap(void *addr, uptr length, int prot, int flags, int fd,
  87. u64 offset) {
  88. CHECK(&__mmap);
  89. return (uptr)__mmap(addr, length, prot, flags, fd, 0, offset);
  90. }
  91. uptr internal_munmap(void *addr, uptr length) {
  92. DEFINE__REAL(int, munmap, void *a, uptr b);
  93. return _REAL(munmap, addr, length);
  94. }
  95. uptr internal_mremap(void *old_address, uptr old_size, uptr new_size, int flags,
  96. void *new_address) {
  97. CHECK(false && "internal_mremap is unimplemented on NetBSD");
  98. return 0;
  99. }
  100. int internal_mprotect(void *addr, uptr length, int prot) {
  101. DEFINE__REAL(int, mprotect, void *a, uptr b, int c);
  102. return _REAL(mprotect, addr, length, prot);
  103. }
  104. int internal_madvise(uptr addr, uptr length, int advice) {
  105. DEFINE__REAL(int, madvise, void *a, uptr b, int c);
  106. return _REAL(madvise, (void *)addr, length, advice);
  107. }
  108. uptr internal_close(fd_t fd) {
  109. CHECK(&_sys_close);
  110. return _sys_close(fd);
  111. }
  112. uptr internal_open(const char *filename, int flags) {
  113. CHECK(&_sys_open);
  114. return _sys_open(filename, flags);
  115. }
  116. uptr internal_open(const char *filename, int flags, u32 mode) {
  117. CHECK(&_sys_open);
  118. return _sys_open(filename, flags, mode);
  119. }
  120. uptr internal_read(fd_t fd, void *buf, uptr count) {
  121. sptr res;
  122. CHECK(&_sys_read);
  123. HANDLE_EINTR(res, (sptr)_sys_read(fd, buf, (size_t)count));
  124. return res;
  125. }
  126. uptr internal_write(fd_t fd, const void *buf, uptr count) {
  127. sptr res;
  128. CHECK(&_sys_write);
  129. HANDLE_EINTR(res, (sptr)_sys_write(fd, buf, count));
  130. return res;
  131. }
  132. uptr internal_ftruncate(fd_t fd, uptr size) {
  133. sptr res;
  134. CHECK(&__ftruncate);
  135. HANDLE_EINTR(res, __ftruncate(fd, 0, (s64)size));
  136. return res;
  137. }
  138. uptr internal_stat(const char *path, void *buf) {
  139. DEFINE__REAL(int, __stat50, const char *a, void *b);
  140. return _REAL(__stat50, path, buf);
  141. }
  142. uptr internal_lstat(const char *path, void *buf) {
  143. DEFINE__REAL(int, __lstat50, const char *a, void *b);
  144. return _REAL(__lstat50, path, buf);
  145. }
  146. uptr internal_fstat(fd_t fd, void *buf) {
  147. DEFINE__REAL(int, __fstat50, int a, void *b);
  148. return _REAL(__fstat50, fd, buf);
  149. }
  150. uptr internal_filesize(fd_t fd) {
  151. struct stat st;
  152. if (internal_fstat(fd, &st))
  153. return -1;
  154. return (uptr)st.st_size;
  155. }
  156. uptr internal_dup(int oldfd) {
  157. DEFINE__REAL(int, dup, int a);
  158. return _REAL(dup, oldfd);
  159. }
  160. uptr internal_dup2(int oldfd, int newfd) {
  161. DEFINE__REAL(int, dup2, int a, int b);
  162. return _REAL(dup2, oldfd, newfd);
  163. }
  164. uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
  165. CHECK(&_sys_readlink);
  166. return (uptr)_sys_readlink(path, buf, bufsize);
  167. }
  168. uptr internal_unlink(const char *path) {
  169. DEFINE__REAL(int, unlink, const char *a);
  170. return _REAL(unlink, path);
  171. }
  172. uptr internal_rename(const char *oldpath, const char *newpath) {
  173. DEFINE__REAL(int, rename, const char *a, const char *b);
  174. return _REAL(rename, oldpath, newpath);
  175. }
  176. uptr internal_sched_yield() {
  177. CHECK(&_sys_sched_yield);
  178. return _sys_sched_yield();
  179. }
  180. void internal__exit(int exitcode) {
  181. DEFINE__REAL(void, _exit, int a);
  182. _REAL(_exit, exitcode);
  183. Die(); // Unreachable.
  184. }
  185. void internal_usleep(u64 useconds) {
  186. struct timespec ts;
  187. ts.tv_sec = useconds / 1000000;
  188. ts.tv_nsec = (useconds % 1000000) * 1000;
  189. CHECK(&_sys___nanosleep50);
  190. _sys___nanosleep50(&ts, &ts);
  191. }
  192. uptr internal_execve(const char *filename, char *const argv[],
  193. char *const envp[]) {
  194. CHECK(&_sys_execve);
  195. return _sys_execve(filename, argv, envp);
  196. }
  197. tid_t GetTid() {
  198. DEFINE__REAL(int, _lwp_self);
  199. return _REAL(_lwp_self);
  200. }
  201. int TgKill(pid_t pid, tid_t tid, int sig) {
  202. DEFINE__REAL(int, _lwp_kill, int a, int b);
  203. (void)pid;
  204. return _REAL(_lwp_kill, tid, sig);
  205. }
  206. u64 NanoTime() {
  207. timeval tv;
  208. DEFINE__REAL(int, __gettimeofday50, void *a, void *b);
  209. internal_memset(&tv, 0, sizeof(tv));
  210. _REAL(__gettimeofday50, &tv, 0);
  211. return (u64)tv.tv_sec * 1000 * 1000 * 1000 + tv.tv_usec * 1000;
  212. }
  213. uptr internal_clock_gettime(__sanitizer_clockid_t clk_id, void *tp) {
  214. DEFINE__REAL(int, __clock_gettime50, __sanitizer_clockid_t a, void *b);
  215. return _REAL(__clock_gettime50, clk_id, tp);
  216. }
  217. uptr internal_ptrace(int request, int pid, void *addr, int data) {
  218. DEFINE__REAL(int, ptrace, int a, int b, void *c, int d);
  219. return _REAL(ptrace, request, pid, addr, data);
  220. }
  221. uptr internal_waitpid(int pid, int *status, int options) {
  222. CHECK(&_sys___wait450);
  223. return _sys___wait450(pid, status, options, 0 /* rusage */);
  224. }
  225. uptr internal_getpid() {
  226. DEFINE__REAL(int, getpid);
  227. return _REAL(getpid);
  228. }
  229. uptr internal_getppid() {
  230. DEFINE__REAL(int, getppid);
  231. return _REAL(getppid);
  232. }
  233. int internal_dlinfo(void *handle, int request, void *p) {
  234. DEFINE__REAL(int, dlinfo, void *a, int b, void *c);
  235. return _REAL(dlinfo, handle, request, p);
  236. }
  237. uptr internal_getdents(fd_t fd, void *dirp, unsigned int count) {
  238. DEFINE__REAL(int, __getdents30, int a, void *b, size_t c);
  239. return _REAL(__getdents30, fd, dirp, count);
  240. }
  241. uptr internal_lseek(fd_t fd, OFF_T offset, int whence) {
  242. CHECK(&__lseek);
  243. return __lseek(fd, 0, offset, whence);
  244. }
  245. uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5) {
  246. Printf("internal_prctl not implemented for NetBSD");
  247. Die();
  248. return 0;
  249. }
  250. uptr internal_sigaltstack(const void *ss, void *oss) {
  251. DEFINE__REAL(int, __sigaltstack14, const void *a, void *b);
  252. return _REAL(__sigaltstack14, ss, oss);
  253. }
  254. int internal_fork() {
  255. CHECK(&__fork);
  256. return __fork();
  257. }
  258. int internal_sysctl(const int *name, unsigned int namelen, void *oldp,
  259. uptr *oldlenp, const void *newp, uptr newlen) {
  260. CHECK(&__sysctl);
  261. return __sysctl(name, namelen, oldp, (size_t *)oldlenp, newp, (size_t)newlen);
  262. }
  263. int internal_sysctlbyname(const char *sname, void *oldp, uptr *oldlenp,
  264. const void *newp, uptr newlen) {
  265. DEFINE__REAL(int, sysctlbyname, const char *a, void *b, size_t *c,
  266. const void *d, size_t e);
  267. return _REAL(sysctlbyname, sname, oldp, (size_t *)oldlenp, newp,
  268. (size_t)newlen);
  269. }
  270. uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
  271. __sanitizer_sigset_t *oldset) {
  272. CHECK(&_sys___sigprocmask14);
  273. return _sys___sigprocmask14(how, set, oldset);
  274. }
  275. void internal_sigfillset(__sanitizer_sigset_t *set) {
  276. DEFINE__REAL(int, __sigfillset14, const void *a);
  277. (void)_REAL(__sigfillset14, set);
  278. }
  279. void internal_sigemptyset(__sanitizer_sigset_t *set) {
  280. DEFINE__REAL(int, __sigemptyset14, const void *a);
  281. (void)_REAL(__sigemptyset14, set);
  282. }
  283. void internal_sigdelset(__sanitizer_sigset_t *set, int signo) {
  284. DEFINE__REAL(int, __sigdelset14, const void *a, int b);
  285. (void)_REAL(__sigdelset14, set, signo);
  286. }
  287. uptr internal_clone(int (*fn)(void *), void *child_stack, int flags,
  288. void *arg) {
  289. DEFINE__REAL(int, clone, int (*a)(void *b), void *c, int d, void *e);
  290. return _REAL(clone, fn, child_stack, flags, arg);
  291. }
  292. } // namespace __sanitizer
  293. #endif