2018-11-16 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_netbsd.cc
blobd0df94d1f950737bb82569bc015365d0d9931d5d
1 //===-- sanitizer_netbsd.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 Sanitizer run-time libraries and implements
9 // NetBSD-specific functions from sanitizer_libc.h.
10 //===----------------------------------------------------------------------===//
12 #include "sanitizer_platform.h"
14 #if SANITIZER_NETBSD
16 #include "sanitizer_common.h"
17 #include "sanitizer_flags.h"
18 #include "sanitizer_getauxval.h"
19 #include "sanitizer_internal_defs.h"
20 #include "sanitizer_libc.h"
21 #include "sanitizer_linux.h"
22 #include "sanitizer_mutex.h"
23 #include "sanitizer_placement_new.h"
24 #include "sanitizer_procmaps.h"
26 #include <sys/param.h>
27 #include <sys/types.h>
29 #include <sys/exec.h>
30 #include <sys/mman.h>
31 #include <sys/ptrace.h>
32 #include <sys/resource.h>
33 #include <sys/stat.h>
34 #include <sys/syscall.h>
35 #include <sys/sysctl.h>
36 #include <sys/time.h>
38 #include <dlfcn.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <limits.h>
42 #include <link.h>
43 #include <lwp.h>
44 #include <pthread.h>
45 #include <sched.h>
46 #include <signal.h>
47 #include <ucontext.h>
48 #include <unistd.h>
50 extern "C" void *__mmap(void *, size_t, int, int, int, int,
51 off_t) SANITIZER_WEAK_ATTRIBUTE;
52 extern "C" int __sysctl(const int *, unsigned int, void *, size_t *,
53 const void *, size_t) SANITIZER_WEAK_ATTRIBUTE;
54 extern "C" int _sys_close(int) SANITIZER_WEAK_ATTRIBUTE;
55 extern "C" int _sys_open(const char *, int, ...) SANITIZER_WEAK_ATTRIBUTE;
56 extern "C" ssize_t _sys_read(int, void *, size_t) SANITIZER_WEAK_ATTRIBUTE;
57 extern "C" ssize_t _sys_write(int, const void *,
58 size_t) SANITIZER_WEAK_ATTRIBUTE;
59 extern "C" int __ftruncate(int, int, off_t) SANITIZER_WEAK_ATTRIBUTE;
60 extern "C" ssize_t _sys_readlink(const char *, char *,
61 size_t) SANITIZER_WEAK_ATTRIBUTE;
62 extern "C" int _sys_sched_yield() SANITIZER_WEAK_ATTRIBUTE;
63 extern "C" int _sys___nanosleep50(const void *,
64 void *) SANITIZER_WEAK_ATTRIBUTE;
65 extern "C" int _sys_execve(const char *, char *const[],
66 char *const[]) SANITIZER_WEAK_ATTRIBUTE;
67 extern "C" off_t __lseek(int, int, off_t, int) SANITIZER_WEAK_ATTRIBUTE;
68 extern "C" int __fork() SANITIZER_WEAK_ATTRIBUTE;
69 extern "C" int _sys___sigprocmask14(int, const void *,
70 void *) SANITIZER_WEAK_ATTRIBUTE;
71 extern "C" int _sys___wait450(int wpid, int *, int,
72 void *) SANITIZER_WEAK_ATTRIBUTE;
74 namespace __sanitizer {
76 static void *GetRealLibcAddress(const char *symbol) {
77 void *real = dlsym(RTLD_NEXT, symbol);
78 if (!real)
79 real = dlsym(RTLD_DEFAULT, symbol);
80 if (!real) {
81 Printf("GetRealLibcAddress failed for symbol=%s", symbol);
82 Die();
84 return real;
87 #define _REAL(func, ...) real##_##func(__VA_ARGS__)
88 #define DEFINE__REAL(ret_type, func, ...) \
89 static ret_type (*real_##func)(__VA_ARGS__) = NULL; \
90 if (!real_##func) { \
91 real_##func = (ret_type(*)(__VA_ARGS__))GetRealLibcAddress(#func); \
92 } \
93 CHECK(real_##func);
95 // --------------- sanitizer_libc.h
96 uptr internal_mmap(void *addr, uptr length, int prot, int flags, int fd,
97 OFF_T offset) {
98 CHECK(&__mmap);
99 return (uptr)__mmap(addr, length, prot, flags, fd, 0, offset);
102 uptr internal_munmap(void *addr, uptr length) {
103 DEFINE__REAL(int, munmap, void *a, uptr b);
104 return _REAL(munmap, addr, length);
107 int internal_mprotect(void *addr, uptr length, int prot) {
108 DEFINE__REAL(int, mprotect, void *a, uptr b, int c);
109 return _REAL(mprotect, addr, length, prot);
112 uptr internal_close(fd_t fd) {
113 CHECK(&_sys_close);
114 return _sys_close(fd);
117 uptr internal_open(const char *filename, int flags) {
118 CHECK(&_sys_open);
119 return _sys_open(filename, flags);
122 uptr internal_open(const char *filename, int flags, u32 mode) {
123 CHECK(&_sys_open);
124 return _sys_open(filename, flags, mode);
127 uptr internal_read(fd_t fd, void *buf, uptr count) {
128 sptr res;
129 CHECK(&_sys_read);
130 HANDLE_EINTR(res, (sptr)_sys_read(fd, buf, (size_t)count));
131 return res;
134 uptr internal_write(fd_t fd, const void *buf, uptr count) {
135 sptr res;
136 CHECK(&_sys_write);
137 HANDLE_EINTR(res, (sptr)_sys_write(fd, buf, count));
138 return res;
141 uptr internal_ftruncate(fd_t fd, uptr size) {
142 sptr res;
143 CHECK(&__ftruncate);
144 HANDLE_EINTR(res, __ftruncate(fd, 0, (s64)size));
145 return res;
148 uptr internal_stat(const char *path, void *buf) {
149 DEFINE__REAL(int, __stat50, const char *a, void *b);
150 return _REAL(__stat50, path, buf);
153 uptr internal_lstat(const char *path, void *buf) {
154 DEFINE__REAL(int, __lstat50, const char *a, void *b);
155 return _REAL(__lstat50, path, buf);
158 uptr internal_fstat(fd_t fd, void *buf) {
159 DEFINE__REAL(int, __fstat50, int a, void *b);
160 return _REAL(__fstat50, fd, buf);
163 uptr internal_filesize(fd_t fd) {
164 struct stat st;
165 if (internal_fstat(fd, &st))
166 return -1;
167 return (uptr)st.st_size;
170 uptr internal_dup2(int oldfd, int newfd) {
171 DEFINE__REAL(int, dup2, int a, int b);
172 return _REAL(dup2, oldfd, newfd);
175 uptr internal_readlink(const char *path, char *buf, uptr bufsize) {
176 CHECK(&_sys_readlink);
177 return (uptr)_sys_readlink(path, buf, bufsize);
180 uptr internal_unlink(const char *path) {
181 DEFINE__REAL(int, unlink, const char *a);
182 return _REAL(unlink, path);
185 uptr internal_rename(const char *oldpath, const char *newpath) {
186 DEFINE__REAL(int, rename, const char *a, const char *b);
187 return _REAL(rename, oldpath, newpath);
190 uptr internal_sched_yield() {
191 CHECK(&_sys_sched_yield);
192 return _sys_sched_yield();
195 void internal__exit(int exitcode) {
196 DEFINE__REAL(void, _exit, int a);
197 _REAL(_exit, exitcode);
198 Die(); // Unreachable.
201 unsigned int internal_sleep(unsigned int seconds) {
202 struct timespec ts;
203 ts.tv_sec = 1;
204 ts.tv_nsec = 0;
205 CHECK(&_sys___nanosleep50);
206 int res = _sys___nanosleep50(&ts, &ts);
207 if (res)
208 return ts.tv_sec;
209 return 0;
212 uptr internal_execve(const char *filename, char *const argv[],
213 char *const envp[]) {
214 CHECK(&_sys_execve);
215 return _sys_execve(filename, argv, envp);
218 tid_t GetTid() {
219 DEFINE__REAL(int, _lwp_self);
220 return _REAL(_lwp_self);
223 int TgKill(pid_t pid, tid_t tid, int sig) {
224 DEFINE__REAL(int, _lwp_kill, int a, int b);
225 (void)pid;
226 return _REAL(_lwp_kill, tid, sig);
229 u64 NanoTime() {
230 timeval tv;
231 DEFINE__REAL(int, __gettimeofday50, void *a, void *b);
232 internal_memset(&tv, 0, sizeof(tv));
233 _REAL(__gettimeofday50, &tv, 0);
234 return (u64)tv.tv_sec * 1000 * 1000 * 1000 + tv.tv_usec * 1000;
237 uptr internal_clock_gettime(__sanitizer_clockid_t clk_id, void *tp) {
238 DEFINE__REAL(int, __clock_gettime50, __sanitizer_clockid_t a, void *b);
239 return _REAL(__clock_gettime50, clk_id, tp);
242 uptr internal_ptrace(int request, int pid, void *addr, void *data) {
243 Printf("internal_ptrace not implemented for NetBSD");
244 Die();
245 return 0;
248 uptr internal_waitpid(int pid, int *status, int options) {
249 CHECK(&_sys___wait450);
250 return _sys___wait450(pid, status, options, 0 /* rusage */);
253 uptr internal_getpid() {
254 DEFINE__REAL(int, getpid);
255 return _REAL(getpid);
258 uptr internal_getppid() {
259 DEFINE__REAL(int, getppid);
260 return _REAL(getppid);
263 uptr internal_getdents(fd_t fd, void *dirp, unsigned int count) {
264 DEFINE__REAL(int, __getdents30, int a, void *b, size_t c);
265 return _REAL(__getdents30, fd, dirp, count);
268 uptr internal_lseek(fd_t fd, OFF_T offset, int whence) {
269 CHECK(&__lseek);
270 return __lseek(fd, 0, offset, whence);
273 uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5) {
274 Printf("internal_prctl not implemented for NetBSD");
275 Die();
276 return 0;
279 uptr internal_sigaltstack(const void *ss, void *oss) {
280 DEFINE__REAL(int, __sigaltstack14, const void *a, void *b);
281 return _REAL(__sigaltstack14, ss, oss);
284 int internal_fork() {
285 CHECK(&__fork);
286 return __fork();
289 int internal_sysctl(const int *name, unsigned int namelen, void *oldp,
290 uptr *oldlenp, const void *newp, uptr newlen) {
291 CHECK(&__sysctl);
292 return __sysctl(name, namelen, oldp, (size_t *)oldlenp, newp, (size_t)newlen);
295 int internal_sysctlbyname(const char *sname, void *oldp, uptr *oldlenp,
296 const void *newp, uptr newlen) {
297 DEFINE__REAL(int, sysctlbyname, const char *a, void *b, size_t *c,
298 const void *d, size_t e);
299 return _REAL(sysctlbyname, sname, oldp, (size_t *)oldlenp, newp,
300 (size_t)newlen);
303 uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set,
304 __sanitizer_sigset_t *oldset) {
305 CHECK(&_sys___sigprocmask14);
306 return _sys___sigprocmask14(how, set, oldset);
309 void internal_sigfillset(__sanitizer_sigset_t *set) {
310 DEFINE__REAL(int, __sigfillset14, const void *a);
311 (void)_REAL(__sigfillset14, set);
314 void internal_sigemptyset(__sanitizer_sigset_t *set) {
315 DEFINE__REAL(int, __sigemptyset14, const void *a);
316 (void)_REAL(__sigemptyset14, set);
319 uptr intrnal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg,
320 int *parent_tidptr, void *newtls, int *child_tidptr) {
321 Printf("internal_clone not implemented for NetBSD");
322 Die();
323 return 0;
326 } // namespace __sanitizer
328 #endif