| 1 | //===-- sanitizer_haiku.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 | // Haiku-specific functions from sanitizer_libc.h. |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "sanitizer_platform.h" |
| 14 | |
| 15 | #if SANITIZER_HAIKU |
| 16 | |
| 17 | # include "sanitizer_common.h" |
| 18 | # include "sanitizer_flags.h" |
| 19 | # include "sanitizer_getauxval.h" |
| 20 | # include "sanitizer_internal_defs.h" |
| 21 | # include "sanitizer_libc.h" |
| 22 | # include "sanitizer_linux.h" |
| 23 | # include "sanitizer_mutex.h" |
| 24 | # include "sanitizer_placement_new.h" |
| 25 | # include "sanitizer_procmaps.h" |
| 26 | |
| 27 | # include <sys/param.h> |
| 28 | # include <sys/types.h> |
| 29 | |
| 30 | # include <sys/mman.h> |
| 31 | # include <sys/resource.h> |
| 32 | # include <sys/stat.h> |
| 33 | # include <sys/time.h> |
| 34 | |
| 35 | # include <dlfcn.h> |
| 36 | # include <errno.h> |
| 37 | # include <fcntl.h> |
| 38 | # include <limits.h> |
| 39 | # include <link.h> |
| 40 | # include <pthread.h> |
| 41 | # include <sched.h> |
| 42 | # include <signal.h> |
| 43 | # include <unistd.h> |
| 44 | |
| 45 | # include "system/vm_defs.h" |
| 46 | # include "system/syscalls.h" |
| 47 | # include "shared/syscall_utils.h" |
| 48 | |
| 49 | namespace __sanitizer { |
| 50 | |
| 51 | static void *GetRealLibcAddress(const char *symbol) { |
| 52 | void *real = dlsym(RTLD_NEXT, symbol); |
| 53 | if (!real) |
| 54 | real = dlsym(RTLD_DEFAULT, symbol); |
| 55 | if (!real) { |
| 56 | Printf("GetRealLibcAddress failed for symbol=%s" , symbol); |
| 57 | Die(); |
| 58 | } |
| 59 | return real; |
| 60 | } |
| 61 | |
| 62 | # define _REAL(func, ...) real##_##func(__VA_ARGS__) |
| 63 | # define DEFINE__REAL(ret_type, func, ...) \ |
| 64 | static ret_type (*real_##func)(__VA_ARGS__) = NULL; \ |
| 65 | if (!real_##func) { \ |
| 66 | real_##func = (ret_type(*)(__VA_ARGS__))GetRealLibcAddress(#func); \ |
| 67 | } \ |
| 68 | CHECK(real_##func); |
| 69 | |
| 70 | // --------------- sanitizer_libc.h |
| 71 | uptr internal_mmap(void *addr, uptr length, int prot, int flags, int fd, |
| 72 | u64 offset) { |
| 73 | if ((flags & MAP_ANONYMOUS) != 0) |
| 74 | fd = -1; |
| 75 | |
| 76 | int mapping = |
| 77 | (flags & MAP_SHARED) != 0 ? REGION_NO_PRIVATE_MAP : REGION_PRIVATE_MAP; |
| 78 | |
| 79 | uint32 addressSpec; |
| 80 | if ((flags & MAP_FIXED) != 0) |
| 81 | addressSpec = B_EXACT_ADDRESS; |
| 82 | else if (addr != NULL) |
| 83 | addressSpec = B_BASE_ADDRESS; |
| 84 | else |
| 85 | addressSpec = B_RANDOMIZED_ANY_ADDRESS; |
| 86 | |
| 87 | uint32 areaProtection = 0; |
| 88 | if ((prot & PROT_READ) != 0) |
| 89 | areaProtection |= B_READ_AREA; |
| 90 | if ((prot & PROT_WRITE) != 0) |
| 91 | areaProtection |= B_WRITE_AREA; |
| 92 | if ((prot & PROT_EXEC) != 0) |
| 93 | areaProtection |= B_EXECUTE_AREA; |
| 94 | |
| 95 | if ((flags & MAP_NORESERVE) != 0) |
| 96 | areaProtection |= B_OVERCOMMITTING_AREA; |
| 97 | |
| 98 | area_id area = _kern_map_file("sanitizer mmap" , &addr, addressSpec, length, |
| 99 | areaProtection, mapping, true, fd, offset); |
| 100 | if (area < 0) |
| 101 | RETURN_AND_SET_ERRNO(area); |
| 102 | return (uptr)addr; |
| 103 | } |
| 104 | |
| 105 | uptr internal_munmap(void *addr, uptr length) { |
| 106 | DEFINE__REAL(int, munmap, void *a, uptr b); |
| 107 | return _REAL(munmap, addr, length); |
| 108 | } |
| 109 | |
| 110 | uptr internal_mremap(void *old_address, uptr old_size, uptr new_size, int flags, |
| 111 | void *new_address) { |
| 112 | CHECK(false && "internal_mremap is unimplemented on Haiku" ); |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | int internal_mprotect(void *addr, uptr length, int prot) { |
| 117 | DEFINE__REAL(int, mprotect, void *a, uptr b, int c); |
| 118 | return _REAL(mprotect, addr, length, prot); |
| 119 | } |
| 120 | |
| 121 | int internal_madvise(uptr addr, uptr length, int advice) { |
| 122 | DEFINE__REAL(int, madvise, void *a, uptr b, int c); |
| 123 | return _REAL(madvise, (void *)addr, length, advice); |
| 124 | } |
| 125 | |
| 126 | uptr internal_close(fd_t fd) { |
| 127 | CHECK(&_kern_close); |
| 128 | RETURN_AND_SET_ERRNO(_kern_close(fd)); |
| 129 | } |
| 130 | |
| 131 | uptr internal_open(const char *filename, int flags) { |
| 132 | CHECK(&_kern_open); |
| 133 | RETURN_AND_SET_ERRNO(_kern_open(-1, filename, flags, 0)); |
| 134 | } |
| 135 | |
| 136 | uptr internal_open(const char *filename, int flags, u32 mode) { |
| 137 | CHECK(&_kern_open); |
| 138 | RETURN_AND_SET_ERRNO(_kern_open(-1, filename, flags, mode)); |
| 139 | } |
| 140 | |
| 141 | uptr internal_read(fd_t fd, void *buf, uptr count) { |
| 142 | sptr res; |
| 143 | CHECK(&_kern_read); |
| 144 | HANDLE_EINTR(res, (sptr)_kern_read(fd, -1, buf, (size_t)count)); |
| 145 | RETURN_AND_SET_ERRNO(res); |
| 146 | return res; |
| 147 | } |
| 148 | |
| 149 | uptr internal_write(fd_t fd, const void *buf, uptr count) { |
| 150 | sptr res; |
| 151 | CHECK(&_kern_write); |
| 152 | HANDLE_EINTR(res, (sptr)_kern_write(fd, -1, buf, count)); |
| 153 | RETURN_AND_SET_ERRNO(res); |
| 154 | return res; |
| 155 | } |
| 156 | |
| 157 | uptr internal_ftruncate(fd_t fd, uptr size) { |
| 158 | sptr res; |
| 159 | DEFINE__REAL(int, ftruncate, int, off_t); |
| 160 | return _REAL(ftruncate, fd, size); |
| 161 | return res; |
| 162 | } |
| 163 | |
| 164 | uptr internal_stat(const char *path, void *buf) { |
| 165 | DEFINE__REAL(int, _stat_current, const char *a, void *b); |
| 166 | return _REAL(_stat_current, path, buf); |
| 167 | } |
| 168 | |
| 169 | uptr internal_lstat(const char *path, void *buf) { |
| 170 | DEFINE__REAL(int, _lstat_current, const char *a, void *b); |
| 171 | return _REAL(_lstat_current, path, buf); |
| 172 | } |
| 173 | |
| 174 | uptr internal_fstat(fd_t fd, void *buf) { |
| 175 | DEFINE__REAL(int, _fstat_current, int a, void *b); |
| 176 | return _REAL(_fstat_current, fd, buf); |
| 177 | } |
| 178 | |
| 179 | uptr internal_filesize(fd_t fd) { |
| 180 | struct stat st; |
| 181 | if (internal_fstat(fd, &st)) |
| 182 | return -1; |
| 183 | return (uptr)st.st_size; |
| 184 | } |
| 185 | |
| 186 | uptr internal_dup(int oldfd) { |
| 187 | DEFINE__REAL(int, dup, int a); |
| 188 | return _REAL(dup, oldfd); |
| 189 | } |
| 190 | |
| 191 | uptr internal_dup2(int oldfd, int newfd) { |
| 192 | DEFINE__REAL(int, dup2, int a, int b); |
| 193 | return _REAL(dup2, oldfd, newfd); |
| 194 | } |
| 195 | |
| 196 | uptr internal_readlink(const char *path, char *buf, uptr bufsize) { |
| 197 | CHECK(&_kern_read_link); |
| 198 | RETURN_AND_SET_ERRNO(_kern_read_link(-1, path, buf, &bufsize)); |
| 199 | } |
| 200 | |
| 201 | uptr internal_unlink(const char *path) { |
| 202 | DEFINE__REAL(int, unlink, const char *a); |
| 203 | return _REAL(unlink, path); |
| 204 | } |
| 205 | |
| 206 | uptr internal_rename(const char *oldpath, const char *newpath) { |
| 207 | DEFINE__REAL(int, rename, const char *a, const char *b); |
| 208 | return _REAL(rename, oldpath, newpath); |
| 209 | } |
| 210 | |
| 211 | uptr internal_sched_yield() { |
| 212 | CHECK(&_kern_thread_yield); |
| 213 | _kern_thread_yield(); |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | void internal__exit(int exitcode) { |
| 218 | DEFINE__REAL(void, _exit, int a); |
| 219 | _REAL(_exit, exitcode); |
| 220 | Die(); // Unreachable. |
| 221 | } |
| 222 | |
| 223 | void internal_usleep(u64 useconds) { |
| 224 | _kern_snooze_etc(useconds, B_SYSTEM_TIMEBASE, B_RELATIVE_TIMEOUT, NULL); |
| 225 | } |
| 226 | |
| 227 | uptr internal_execve(const char *filename, char *const argv[], |
| 228 | char *const envp[]) { |
| 229 | DEFINE__REAL(int, execve, const char *, char *const[], char *const[]); |
| 230 | return _REAL(execve, filename, argv, envp); |
| 231 | } |
| 232 | |
| 233 | # if 0 |
| 234 | tid_t GetTid() { |
| 235 | DEFINE__REAL(int, _lwp_self); |
| 236 | return _REAL(_lwp_self); |
| 237 | } |
| 238 | |
| 239 | int TgKill(pid_t pid, tid_t tid, int sig) { |
| 240 | DEFINE__REAL(int, _lwp_kill, int a, int b); |
| 241 | (void)pid; |
| 242 | return _REAL(_lwp_kill, tid, sig); |
| 243 | } |
| 244 | |
| 245 | u64 NanoTime() { |
| 246 | timeval tv; |
| 247 | DEFINE__REAL(int, __gettimeofday50, void *a, void *b); |
| 248 | internal_memset(&tv, 0, sizeof(tv)); |
| 249 | _REAL(__gettimeofday50, &tv, 0); |
| 250 | return (u64)tv.tv_sec * 1000 * 1000 * 1000 + tv.tv_usec * 1000; |
| 251 | } |
| 252 | # endif |
| 253 | |
| 254 | uptr internal_clock_gettime(__sanitizer_clockid_t clk_id, void *tp) { |
| 255 | DEFINE__REAL(int, __clock_gettime50, __sanitizer_clockid_t a, void *b); |
| 256 | return _REAL(__clock_gettime50, clk_id, tp); |
| 257 | } |
| 258 | |
| 259 | uptr internal_ptrace(int request, int pid, void *addr, int data) { |
| 260 | DEFINE__REAL(int, ptrace, int a, int b, void *c, int d); |
| 261 | return _REAL(ptrace, request, pid, addr, data); |
| 262 | } |
| 263 | |
| 264 | uptr internal_waitpid(int pid, int *status, int options) { |
| 265 | DEFINE__REAL(int, waitpid, pid_t, int *, int); |
| 266 | return _REAL(waitpid, pid, status, options); |
| 267 | } |
| 268 | |
| 269 | uptr internal_getpid() { |
| 270 | DEFINE__REAL(int, getpid); |
| 271 | return _REAL(getpid); |
| 272 | } |
| 273 | |
| 274 | uptr internal_getppid() { |
| 275 | DEFINE__REAL(int, getppid); |
| 276 | return _REAL(getppid); |
| 277 | } |
| 278 | |
| 279 | int internal_dlinfo(void *handle, int request, void *p) { |
| 280 | DEFINE__REAL(int, dlinfo, void *a, int b, void *c); |
| 281 | return _REAL(dlinfo, handle, request, p); |
| 282 | } |
| 283 | |
| 284 | uptr internal_getdents(fd_t fd, void *dirp, unsigned int count) { |
| 285 | DEFINE__REAL(int, __getdents30, int a, void *b, size_t c); |
| 286 | return _REAL(__getdents30, fd, dirp, count); |
| 287 | } |
| 288 | |
| 289 | uptr internal_lseek(fd_t fd, OFF_T offset, int whence) { |
| 290 | CHECK(&_kern_seek); |
| 291 | off_t result = _kern_seek(fd, offset, whence); |
| 292 | if (result < 0) { |
| 293 | errno = result; |
| 294 | return -1; |
| 295 | } |
| 296 | return result; |
| 297 | } |
| 298 | |
| 299 | uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5) { |
| 300 | Printf("internal_prctl not implemented for Haiku" ); |
| 301 | Die(); |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | uptr internal_sigaltstack(const void *ss, void *oss) { |
| 306 | DEFINE__REAL(int, __sigaltstack14, const void *a, void *b); |
| 307 | return _REAL(__sigaltstack14, ss, oss); |
| 308 | } |
| 309 | |
| 310 | int internal_fork() { |
| 311 | DEFINE__REAL(int, fork); |
| 312 | return _REAL(fork); |
| 313 | } |
| 314 | |
| 315 | # if 0 |
| 316 | int internal_sysctl(const int *name, unsigned int namelen, void *oldp, |
| 317 | uptr *oldlenp, const void *newp, uptr newlen) { |
| 318 | CHECK(&__sysctl); |
| 319 | return __sysctl(name, namelen, oldp, (size_t *)oldlenp, newp, (size_t)newlen); |
| 320 | } |
| 321 | # endif |
| 322 | |
| 323 | int internal_sysctlbyname(const char *sname, void *oldp, uptr *oldlenp, |
| 324 | const void *newp, uptr newlen) { |
| 325 | DEFINE__REAL(int, sysctlbyname, const char *a, void *b, size_t *c, |
| 326 | const void *d, size_t e); |
| 327 | return _REAL(sysctlbyname, sname, oldp, (size_t *)oldlenp, newp, |
| 328 | (size_t)newlen); |
| 329 | } |
| 330 | |
| 331 | uptr internal_sigprocmask(int how, __sanitizer_sigset_t *set, |
| 332 | __sanitizer_sigset_t *oldset) { |
| 333 | CHECK(&_kern_set_signal_mask); |
| 334 | return _kern_set_signal_mask(how, set, oldset); |
| 335 | } |
| 336 | |
| 337 | void internal_sigfillset(__sanitizer_sigset_t *set) { |
| 338 | DEFINE__REAL(int, __sigfillset14, const void *a); |
| 339 | (void)_REAL(__sigfillset14, set); |
| 340 | } |
| 341 | |
| 342 | void internal_sigemptyset(__sanitizer_sigset_t *set) { |
| 343 | DEFINE__REAL(int, __sigemptyset14, const void *a); |
| 344 | (void)_REAL(__sigemptyset14, set); |
| 345 | } |
| 346 | |
| 347 | void internal_sigdelset(__sanitizer_sigset_t *set, int signo) { |
| 348 | DEFINE__REAL(int, __sigdelset14, const void *a, int b); |
| 349 | (void)_REAL(__sigdelset14, set, signo); |
| 350 | } |
| 351 | |
| 352 | uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, |
| 353 | void *arg) { |
| 354 | DEFINE__REAL(int, clone, int (*a)(void *b), void *c, int d, void *e); |
| 355 | |
| 356 | return _REAL(clone, fn, child_stack, flags, arg); |
| 357 | } |
| 358 | |
| 359 | } // namespace __sanitizer |
| 360 | |
| 361 | #endif |
| 362 | |