1 //===-- sanitizer_netbsd.cc -----------------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
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"
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>
31 #include <sys/ptrace.h>
32 #include <sys/resource.h>
34 #include <sys/syscall.h>
35 #include <sys/sysctl.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
);
79 real
= dlsym(RTLD_DEFAULT
, symbol
);
81 Printf("GetRealLibcAddress failed for symbol=%s", symbol
);
87 #define _REAL(func, ...) real##_##func(__VA_ARGS__)
88 #define DEFINE__REAL(ret_type, func, ...) \
89 static ret_type (*real_##func)(__VA_ARGS__) = NULL; \
91 real_##func = (ret_type(*)(__VA_ARGS__))GetRealLibcAddress(#func); \
95 // --------------- sanitizer_libc.h
96 uptr
internal_mmap(void *addr
, uptr length
, int prot
, int flags
, int fd
,
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
) {
114 return _sys_close(fd
);
117 uptr
internal_open(const char *filename
, int flags
) {
119 return _sys_open(filename
, flags
);
122 uptr
internal_open(const char *filename
, int flags
, u32 mode
) {
124 return _sys_open(filename
, flags
, mode
);
127 uptr
internal_read(fd_t fd
, void *buf
, uptr count
) {
130 HANDLE_EINTR(res
, (sptr
)_sys_read(fd
, buf
, (size_t)count
));
134 uptr
internal_write(fd_t fd
, const void *buf
, uptr count
) {
137 HANDLE_EINTR(res
, (sptr
)_sys_write(fd
, buf
, count
));
141 uptr
internal_ftruncate(fd_t fd
, uptr size
) {
144 HANDLE_EINTR(res
, __ftruncate(fd
, 0, (s64
)size
));
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
) {
165 if (internal_fstat(fd
, &st
))
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
) {
205 CHECK(&_sys___nanosleep50
);
206 int res
= _sys___nanosleep50(&ts
, &ts
);
212 uptr
internal_execve(const char *filename
, char *const argv
[],
213 char *const envp
[]) {
215 return _sys_execve(filename
, argv
, envp
);
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
);
226 return _REAL(_lwp_kill
, tid
, sig
);
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");
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
) {
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");
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() {
289 int internal_sysctl(const int *name
, unsigned int namelen
, void *oldp
,
290 uptr
*oldlenp
, const void *newp
, uptr newlen
) {
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
,
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");
326 } // namespace __sanitizer