sanitizer_solaris.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //===-- sanitizer_solaris.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 various sanitizers' runtime libraries and
  10. // implements Solaris-specific functions.
  11. //===----------------------------------------------------------------------===//
  12. #include "sanitizer_platform.h"
  13. #if SANITIZER_SOLARIS
  14. #include <stdio.h>
  15. #include "sanitizer_common.h"
  16. #include "sanitizer_flags.h"
  17. #include "sanitizer_internal_defs.h"
  18. #include "sanitizer_libc.h"
  19. #include "sanitizer_placement_new.h"
  20. #include "sanitizer_platform_limits_posix.h"
  21. #include "sanitizer_procmaps.h"
  22. #include <fcntl.h>
  23. #include <pthread.h>
  24. #include <sched.h>
  25. #include <thread.h>
  26. #error #include <synch.h>
  27. #include <signal.h>
  28. #include <sys/mman.h>
  29. #include <sys/resource.h>
  30. #include <sys/stat.h>
  31. #include <sys/types.h>
  32. #include <dirent.h>
  33. #include <unistd.h>
  34. #include <errno.h>
  35. #include <stdlib.h>
  36. namespace __sanitizer {
  37. //#include "sanitizer_syscall_generic.inc"
  38. #define _REAL(func) _ ## func
  39. #define DECLARE__REAL(ret_type, func, ...) \
  40. extern "C" ret_type _REAL(func)(__VA_ARGS__)
  41. #define DECLARE__REAL_AND_INTERNAL(ret_type, func, ...) \
  42. DECLARE__REAL(ret_type, func, __VA_ARGS__); \
  43. ret_type internal_ ## func(__VA_ARGS__)
  44. #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
  45. #define _REAL64(func) _ ## func ## 64
  46. #else
  47. #define _REAL64(func) _REAL(func)
  48. #endif
  49. #define DECLARE__REAL64(ret_type, func, ...) \
  50. extern "C" ret_type _REAL64(func)(__VA_ARGS__)
  51. #define DECLARE__REAL_AND_INTERNAL64(ret_type, func, ...) \
  52. DECLARE__REAL64(ret_type, func, __VA_ARGS__); \
  53. ret_type internal_ ## func(__VA_ARGS__)
  54. // ---------------------- sanitizer_libc.h
  55. DECLARE__REAL_AND_INTERNAL64(uptr, mmap, void *addr, uptr /*size_t*/ length,
  56. int prot, int flags, int fd, OFF_T offset) {
  57. return (uptr)_REAL64(mmap)(addr, length, prot, flags, fd, offset);
  58. }
  59. DECLARE__REAL_AND_INTERNAL(uptr, munmap, void *addr, uptr length) {
  60. return _REAL(munmap)(addr, length);
  61. }
  62. DECLARE__REAL_AND_INTERNAL(int, mprotect, void *addr, uptr length, int prot) {
  63. return _REAL(mprotect)(addr, length, prot);
  64. }
  65. // Illumos' declaration of madvise cannot be made visible if _XOPEN_SOURCE
  66. // is defined as g++ does on Solaris.
  67. //
  68. // This declaration is consistent with Solaris 11.4. Both Illumos and Solaris
  69. // versions older than 11.4 declared madvise with a caddr_t as the first
  70. // argument, but we don't currently support Solaris versions older than 11.4,
  71. // and as mentioned above the declaration is not visible on Illumos so we can
  72. // use any declaration we like on Illumos.
  73. extern "C" int madvise(void *, size_t, int);
  74. int internal_madvise(uptr addr, uptr length, int advice) {
  75. return madvise((void *)addr, length, advice);
  76. }
  77. DECLARE__REAL_AND_INTERNAL(uptr, close, fd_t fd) {
  78. return _REAL(close)(fd);
  79. }
  80. extern "C" int _REAL64(open)(const char *, int, ...);
  81. uptr internal_open(const char *filename, int flags) {
  82. return _REAL64(open)(filename, flags);
  83. }
  84. uptr internal_open(const char *filename, int flags, u32 mode) {
  85. return _REAL64(open)(filename, flags, mode);
  86. }
  87. DECLARE__REAL_AND_INTERNAL(uptr, read, fd_t fd, void *buf, uptr count) {
  88. return _REAL(read)(fd, buf, count);
  89. }
  90. DECLARE__REAL_AND_INTERNAL(uptr, write, fd_t fd, const void *buf, uptr count) {
  91. return _REAL(write)(fd, buf, count);
  92. }
  93. // FIXME: There's only _ftruncate64 beginning with Solaris 11.
  94. DECLARE__REAL_AND_INTERNAL(uptr, ftruncate, fd_t fd, uptr size) {
  95. return ftruncate(fd, size);
  96. }
  97. DECLARE__REAL_AND_INTERNAL64(uptr, stat, const char *path, void *buf) {
  98. return _REAL64(stat)(path, (struct stat *)buf);
  99. }
  100. DECLARE__REAL_AND_INTERNAL64(uptr, lstat, const char *path, void *buf) {
  101. return _REAL64(lstat)(path, (struct stat *)buf);
  102. }
  103. DECLARE__REAL_AND_INTERNAL64(uptr, fstat, fd_t fd, void *buf) {
  104. return _REAL64(fstat)(fd, (struct stat *)buf);
  105. }
  106. uptr internal_filesize(fd_t fd) {
  107. struct stat st;
  108. if (internal_fstat(fd, &st))
  109. return -1;
  110. return (uptr)st.st_size;
  111. }
  112. DECLARE__REAL_AND_INTERNAL(uptr, dup, int oldfd) {
  113. return _REAL(dup)(oldfd);
  114. }
  115. DECLARE__REAL_AND_INTERNAL(uptr, dup2, int oldfd, int newfd) {
  116. return _REAL(dup2)(oldfd, newfd);
  117. }
  118. DECLARE__REAL_AND_INTERNAL(uptr, readlink, const char *path, char *buf,
  119. uptr bufsize) {
  120. return _REAL(readlink)(path, buf, bufsize);
  121. }
  122. DECLARE__REAL_AND_INTERNAL(uptr, unlink, const char *path) {
  123. return _REAL(unlink)(path);
  124. }
  125. DECLARE__REAL_AND_INTERNAL(uptr, rename, const char *oldpath,
  126. const char *newpath) {
  127. return _REAL(rename)(oldpath, newpath);
  128. }
  129. DECLARE__REAL_AND_INTERNAL(uptr, sched_yield, void) {
  130. return sched_yield();
  131. }
  132. DECLARE__REAL_AND_INTERNAL(void, usleep, u64 useconds) {
  133. struct timespec ts;
  134. ts.tv_sec = useconds / 1000000;
  135. ts.tv_nsec = (useconds % 1000000) * 1000;
  136. nanosleep(&ts, nullptr);
  137. }
  138. DECLARE__REAL_AND_INTERNAL(uptr, execve, const char *filename,
  139. char *const argv[], char *const envp[]) {
  140. return _REAL(execve)(filename, argv, envp);
  141. }
  142. DECLARE__REAL_AND_INTERNAL(uptr, waitpid, int pid, int *status, int options) {
  143. return _REAL(waitpid)(pid, status, options);
  144. }
  145. DECLARE__REAL_AND_INTERNAL(uptr, getpid, void) {
  146. return _REAL(getpid)();
  147. }
  148. // FIXME: This might be wrong: _getdents doesn't take a struct linux_dirent *.
  149. DECLARE__REAL_AND_INTERNAL64(uptr, getdents, fd_t fd, struct linux_dirent *dirp,
  150. unsigned int count) {
  151. return _REAL64(getdents)(fd, dirp, count);
  152. }
  153. DECLARE__REAL_AND_INTERNAL64(uptr, lseek, fd_t fd, OFF_T offset, int whence) {
  154. return _REAL64(lseek)(fd, offset, whence);
  155. }
  156. // FIXME: This might be wrong: _sigfillset doesn't take a
  157. // __sanitizer_sigset_t *.
  158. DECLARE__REAL_AND_INTERNAL(void, sigfillset, __sanitizer_sigset_t *set) {
  159. _REAL(sigfillset)(set);
  160. }
  161. // FIXME: This might be wrong: _sigprocmask doesn't take __sanitizer_sigset_t *.
  162. DECLARE__REAL_AND_INTERNAL(uptr, sigprocmask, int how,
  163. __sanitizer_sigset_t *set,
  164. __sanitizer_sigset_t *oldset) {
  165. return _REAL(sigprocmask)(how, set, oldset);
  166. }
  167. DECLARE__REAL_AND_INTERNAL(int, fork, void) {
  168. // TODO(glider): this may call user's pthread_atfork() handlers which is bad.
  169. return _REAL(fork)();
  170. }
  171. u64 NanoTime() {
  172. return gethrtime();
  173. }
  174. uptr internal_clock_gettime(__sanitizer_clockid_t clk_id, void *tp) {
  175. // FIXME: No internal variant.
  176. return clock_gettime(clk_id, (timespec *)tp);
  177. }
  178. // ----------------- sanitizer_common.h
  179. void FutexWait(atomic_uint32_t *p, u32 cmp) {
  180. // FIXME: implement actual blocking.
  181. sched_yield();
  182. }
  183. void FutexWake(atomic_uint32_t *p, u32 count) {}
  184. } // namespace __sanitizer
  185. #endif // SANITIZER_SOLARIS