|
11 | 11 | // Interceptors for standard library functions.
|
12 | 12 | //===----------------------------------------------------------------------===//
|
13 | 13 |
|
| 14 | +#include <sys/syscall.h> |
| 15 | +#include <unistd.h> |
| 16 | + |
14 | 17 | #include "dfsan/dfsan.h"
|
15 | 18 | #include "interception/interception.h"
|
16 | 19 | #include "sanitizer_common/sanitizer_common.h"
|
17 | 20 |
|
18 | 21 | using namespace __sanitizer;
|
19 | 22 |
|
| 23 | +static bool interceptors_initialized; |
| 24 | + |
20 | 25 | INTERCEPTOR(void *, mmap, void *addr, SIZE_T length, int prot, int flags,
|
21 | 26 | int fd, OFF_T offset) {
|
22 |
| - void *res = REAL(mmap)(addr, length, prot, flags, fd, offset); |
23 |
| - if (res != (void*)-1) |
| 27 | + void *res; |
| 28 | + |
| 29 | + // interceptors_initialized is set to true during preinit_array, when we're |
| 30 | + // single-threaded. So we don't need to worry about accessing it atomically. |
| 31 | + if (!interceptors_initialized) |
| 32 | + res = (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset); |
| 33 | + else |
| 34 | + res = REAL(mmap)(addr, length, prot, flags, fd, offset); |
| 35 | + |
| 36 | + if (res != (void *)-1) |
24 | 37 | dfsan_set_label(0, res, RoundUpTo(length, GetPageSize()));
|
25 | 38 | return res;
|
26 | 39 | }
|
27 | 40 |
|
28 | 41 | INTERCEPTOR(void *, mmap64, void *addr, SIZE_T length, int prot, int flags,
|
29 | 42 | int fd, OFF64_T offset) {
|
30 | 43 | void *res = REAL(mmap64)(addr, length, prot, flags, fd, offset);
|
31 |
| - if (res != (void*)-1) |
| 44 | + if (res != (void *)-1) |
32 | 45 | dfsan_set_label(0, res, RoundUpTo(length, GetPageSize()));
|
33 | 46 | return res;
|
34 | 47 | }
|
35 | 48 |
|
36 | 49 | namespace __dfsan {
|
37 | 50 | void InitializeInterceptors() {
|
38 |
| - static int inited = 0; |
39 |
| - CHECK_EQ(inited, 0); |
| 51 | + CHECK(!interceptors_initialized); |
40 | 52 |
|
41 | 53 | INTERCEPT_FUNCTION(mmap);
|
42 | 54 | INTERCEPT_FUNCTION(mmap64);
|
43 |
| - inited = 1; |
| 55 | + |
| 56 | + interceptors_initialized = true; |
44 | 57 | }
|
45 | 58 | } // namespace __dfsan
|
0 commit comments