]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_solaris.cc
Merge ACPICA 20180810.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_solaris.cc
1 //===-- sanitizer_solaris.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 various sanitizers' runtime libraries and
9 // implements Solaris-specific functions.
10 //===----------------------------------------------------------------------===//
11
12 #include "sanitizer_platform.h"
13 #if SANITIZER_SOLARIS
14
15 #include <stdio.h>
16
17 #include "sanitizer_common.h"
18 #include "sanitizer_flags.h"
19 #include "sanitizer_internal_defs.h"
20 #include "sanitizer_libc.h"
21 #include "sanitizer_placement_new.h"
22 #include "sanitizer_platform_limits_posix.h"
23 #include "sanitizer_procmaps.h"
24
25 #include <fcntl.h>
26 #include <pthread.h>
27 #include <sched.h>
28 #include <thread.h>
29 #include <synch.h>
30 #include <signal.h>
31 #include <sys/mman.h>
32 #include <sys/resource.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <dirent.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #include <stdlib.h>
39
40 namespace __sanitizer {
41
42 //#include "sanitizer_syscall_generic.inc"
43
44 #define _REAL(func) _ ## func
45 #define DECLARE__REAL(ret_type, func, ...) \
46   extern "C" ret_type _REAL(func)(__VA_ARGS__)
47 #define DECLARE__REAL_AND_INTERNAL(ret_type, func, ...) \
48   DECLARE__REAL(ret_type, func, __VA_ARGS__); \
49   ret_type internal_ ## func(__VA_ARGS__)
50
51 // ---------------------- sanitizer_libc.h
52 DECLARE__REAL_AND_INTERNAL(uptr, mmap, void *addr, uptr /*size_t*/ length,
53                            int prot, int flags, int fd, OFF_T offset) {
54   return (uptr)_REAL(mmap)(addr, length, prot, flags, fd, offset);
55 }
56
57 DECLARE__REAL_AND_INTERNAL(uptr, munmap, void *addr, uptr length) {
58   return _REAL(munmap)(addr, length);
59 }
60
61 DECLARE__REAL_AND_INTERNAL(int, mprotect, void *addr, uptr length, int prot) {
62   return _REAL(mprotect)(addr, length, prot);
63 }
64
65 DECLARE__REAL_AND_INTERNAL(uptr, close, fd_t fd) {
66   return _REAL(close)(fd);
67 }
68
69 extern "C" int _REAL(open)(const char *, int, ...);
70
71 uptr internal_open(const char *filename, int flags) {
72   return _REAL(open)(filename, flags);
73 }
74
75 uptr internal_open(const char *filename, int flags, u32 mode) {
76   return _REAL(open)(filename, flags, mode);
77 }
78
79 uptr OpenFile(const char *filename, bool write) {
80   return internal_open(filename,
81       write ? O_WRONLY | O_CREAT : O_RDONLY, 0660);
82 }
83
84 DECLARE__REAL_AND_INTERNAL(uptr, read, fd_t fd, void *buf, uptr count) {
85   return _REAL(read)(fd, buf, count);
86 }
87
88 DECLARE__REAL_AND_INTERNAL(uptr, write, fd_t fd, const void *buf, uptr count) {
89   return _REAL(write)(fd, buf, count);
90 }
91
92 // FIXME: There's only _ftruncate64 beginning with Solaris 11.
93 DECLARE__REAL_AND_INTERNAL(uptr, ftruncate, fd_t fd, uptr size) {
94   return ftruncate(fd, size);
95 }
96
97 DECLARE__REAL_AND_INTERNAL(uptr, stat, const char *path, void *buf) {
98   return _REAL(stat)(path, (struct stat *)buf);
99 }
100
101 DECLARE__REAL_AND_INTERNAL(uptr, lstat, const char *path, void *buf) {
102   return _REAL(lstat)(path, (struct stat *)buf);
103 }
104
105 DECLARE__REAL_AND_INTERNAL(uptr, fstat, fd_t fd, void *buf) {
106   return _REAL(fstat)(fd, (struct stat *)buf);
107 }
108
109 uptr internal_filesize(fd_t fd) {
110   struct stat st;
111   if (internal_fstat(fd, &st))
112     return -1;
113   return (uptr)st.st_size;
114 }
115
116 DECLARE__REAL_AND_INTERNAL(uptr, dup2, int oldfd, int newfd) {
117   return _REAL(dup2)(oldfd, newfd);
118 }
119
120 DECLARE__REAL_AND_INTERNAL(uptr, readlink, const char *path, char *buf,
121                            uptr bufsize) {
122   return _REAL(readlink)(path, buf, bufsize);
123 }
124
125 DECLARE__REAL_AND_INTERNAL(uptr, unlink, const char *path) {
126   return _REAL(unlink)(path);
127 }
128
129 DECLARE__REAL_AND_INTERNAL(uptr, rename, const char *oldpath,
130                            const char *newpath) {
131   return _REAL(rename)(oldpath, newpath);
132 }
133
134 DECLARE__REAL_AND_INTERNAL(uptr, sched_yield, void) {
135   return sched_yield();
136 }
137
138 DECLARE__REAL_AND_INTERNAL(void, _exit, int exitcode) {
139   _exit(exitcode);
140 }
141
142 DECLARE__REAL_AND_INTERNAL(uptr, execve, const char *filename,
143                            char *const argv[], char *const envp[]) {
144   return _REAL(execve)(filename, argv, envp);
145 }
146
147 DECLARE__REAL_AND_INTERNAL(uptr, waitpid, int pid, int *status, int options) {
148   return _REAL(waitpid)(pid, status, options);
149 }
150
151 DECLARE__REAL_AND_INTERNAL(uptr, getpid, void) {
152   return _REAL(getpid)();
153 }
154
155 // FIXME: This might be wrong: _getdents doesn't take a struct linux_dirent *.
156 DECLARE__REAL_AND_INTERNAL(uptr, getdents, fd_t fd, struct linux_dirent *dirp,
157                            unsigned int count) {
158   return _REAL(getdents)(fd, dirp, count);
159 }
160
161 DECLARE__REAL_AND_INTERNAL(uptr, lseek, fd_t fd, OFF_T offset, int whence) {
162   return _REAL(lseek)(fd, offset, whence);
163 }
164
165 // FIXME: This might be wrong: _sigfillset doesn't take a
166 // __sanitizer_sigset_t *.
167 DECLARE__REAL_AND_INTERNAL(void, sigfillset, __sanitizer_sigset_t *set) {
168   _REAL(sigfillset)(set);
169 }
170
171 // FIXME: This might be wrong: _sigprocmask doesn't take __sanitizer_sigset_t *.
172 DECLARE__REAL_AND_INTERNAL(uptr, sigprocmask, int how,
173                            __sanitizer_sigset_t *set,
174                            __sanitizer_sigset_t *oldset) {
175   return _REAL(sigprocmask)(how, set, oldset);
176 }
177
178 DECLARE__REAL_AND_INTERNAL(int, fork, void) {
179   // TODO(glider): this may call user's pthread_atfork() handlers which is bad.
180   return _REAL(fork)();
181 }
182
183 u64 NanoTime() {
184   return gethrtime();
185 }
186
187 uptr internal_clock_gettime(__sanitizer_clockid_t clk_id, void *tp) {
188   // FIXME: No internal variant.
189   return clock_gettime(clk_id, (timespec *)tp);
190 }
191
192 // ----------------- sanitizer_common.h
193 BlockingMutex::BlockingMutex() {
194   CHECK(sizeof(mutex_t) <= sizeof(opaque_storage_));
195   internal_memset(this, 0, sizeof(*this));
196   CHECK_EQ(mutex_init((mutex_t *)&opaque_storage_, USYNC_THREAD, NULL), 0);
197 }
198
199 void BlockingMutex::Lock() {
200   CHECK(sizeof(mutex_t) <= sizeof(opaque_storage_));
201   CHECK_NE(owner_, (uptr)thr_self());
202   CHECK_EQ(mutex_lock((mutex_t *)&opaque_storage_), 0);
203   CHECK(!owner_);
204   owner_ = (uptr)thr_self();
205 }
206
207 void BlockingMutex::Unlock() {
208   CHECK(owner_ == (uptr)thr_self());
209   owner_ = 0;
210   CHECK_EQ(mutex_unlock((mutex_t *)&opaque_storage_), 0);
211 }
212
213 void BlockingMutex::CheckLocked() {
214   CHECK_EQ((uptr)thr_self(), owner_);
215 }
216
217 }  // namespace __sanitizer
218
219 #endif  // SANITIZER_SOLARIS