File: dfsan_interceptors.cpp

package info (click to toggle)
llvm-toolchain-13 1%3A13.0.1-6~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,418,812 kB
  • sloc: cpp: 5,290,827; ansic: 996,570; asm: 544,593; python: 188,212; objc: 72,027; lisp: 30,291; f90: 25,395; sh: 24,900; javascript: 9,780; pascal: 9,398; perl: 7,484; ml: 5,432; awk: 3,523; makefile: 2,892; xml: 953; cs: 573; fortran: 539
file content (245 lines) | stat: -rw-r--r-- 8,128 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//===-- dfsan_interceptors.cpp --------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file is a part of DataFlowSanitizer.
//
// Interceptors for standard library functions.
//===----------------------------------------------------------------------===//

#include <sys/syscall.h>
#include <unistd.h>

#include "dfsan/dfsan.h"
#include "dfsan/dfsan_thread.h"
#include "interception/interception.h"
#include "sanitizer_common/sanitizer_allocator_interface.h"
#include "sanitizer_common/sanitizer_common.h"
#include "sanitizer_common/sanitizer_errno.h"
#include "sanitizer_common/sanitizer_platform_limits_posix.h"
#include "sanitizer_common/sanitizer_posix.h"
#include "sanitizer_common/sanitizer_tls_get_addr.h"

using namespace __sanitizer;

namespace {

bool interceptors_initialized;

}  // namespace

INTERCEPTOR(void *, reallocarray, void *ptr, SIZE_T nmemb, SIZE_T size) {
  return __dfsan::dfsan_reallocarray(ptr, nmemb, size);
}

INTERCEPTOR(void *, __libc_memalign, SIZE_T alignment, SIZE_T size) {
  void *ptr = __dfsan::dfsan_memalign(alignment, size);
  if (ptr)
    DTLS_on_libc_memalign(ptr, size);
  return ptr;
}

INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) {
  return __dfsan::dfsan_aligned_alloc(alignment, size);
}

static uptr allocated_for_dlsym;
static const uptr kDlsymAllocPoolSize = 1024;
static uptr alloc_memory_for_dlsym[kDlsymAllocPoolSize];

static bool IsInDlsymAllocPool(const void *ptr) {
  uptr off = (uptr)ptr - (uptr)alloc_memory_for_dlsym;
  return off < sizeof(alloc_memory_for_dlsym);
}

static void *AllocateFromLocalPool(uptr size_in_bytes) {
  uptr size_in_words = RoundUpTo(size_in_bytes, kWordSize) / kWordSize;
  void *mem = (void *)&alloc_memory_for_dlsym[allocated_for_dlsym];
  allocated_for_dlsym += size_in_words;
  CHECK_LT(allocated_for_dlsym, kDlsymAllocPoolSize);
  return mem;
}

INTERCEPTOR(void *, calloc, SIZE_T nmemb, SIZE_T size) {
  if (UNLIKELY(!__dfsan::dfsan_inited))
    // Hack: dlsym calls calloc before REAL(calloc) is retrieved from dlsym.
    return AllocateFromLocalPool(nmemb * size);
  return __dfsan::dfsan_calloc(nmemb, size);
}

INTERCEPTOR(void *, realloc, void *ptr, SIZE_T size) {
  if (UNLIKELY(IsInDlsymAllocPool(ptr))) {
    uptr offset = (uptr)ptr - (uptr)alloc_memory_for_dlsym;
    uptr copy_size = Min(size, kDlsymAllocPoolSize - offset);
    void *new_ptr;
    if (UNLIKELY(!__dfsan::dfsan_inited)) {
      new_ptr = AllocateFromLocalPool(copy_size);
    } else {
      copy_size = size;
      new_ptr = __dfsan::dfsan_malloc(copy_size);
    }
    internal_memcpy(new_ptr, ptr, copy_size);
    return new_ptr;
  }
  return __dfsan::dfsan_realloc(ptr, size);
}

INTERCEPTOR(void *, malloc, SIZE_T size) {
  if (UNLIKELY(!__dfsan::dfsan_inited))
    // Hack: dlsym calls malloc before REAL(malloc) is retrieved from dlsym.
    return AllocateFromLocalPool(size);
  return __dfsan::dfsan_malloc(size);
}

INTERCEPTOR(void, free, void *ptr) {
  if (!ptr || UNLIKELY(IsInDlsymAllocPool(ptr)))
    return;
  return __dfsan::dfsan_deallocate(ptr);
}

INTERCEPTOR(void, cfree, void *ptr) {
  if (!ptr || UNLIKELY(IsInDlsymAllocPool(ptr)))
    return;
  return __dfsan::dfsan_deallocate(ptr);
}

INTERCEPTOR(int, posix_memalign, void **memptr, SIZE_T alignment, SIZE_T size) {
  CHECK_NE(memptr, 0);
  int res = __dfsan::dfsan_posix_memalign(memptr, alignment, size);
  if (!res)
    dfsan_set_label(0, memptr, sizeof(*memptr));
  return res;
}

INTERCEPTOR(void *, memalign, SIZE_T alignment, SIZE_T size) {
  return __dfsan::dfsan_memalign(alignment, size);
}

INTERCEPTOR(void *, valloc, SIZE_T size) { return __dfsan::dfsan_valloc(size); }

INTERCEPTOR(void *, pvalloc, SIZE_T size) {
  return __dfsan::dfsan_pvalloc(size);
}

INTERCEPTOR(void, mallinfo, __sanitizer_struct_mallinfo *sret) {
  internal_memset(sret, 0, sizeof(*sret));
  dfsan_set_label(0, sret, sizeof(*sret));
}

INTERCEPTOR(int, mallopt, int cmd, int value) { return 0; }

INTERCEPTOR(void, malloc_stats, void) {
  // FIXME: implement, but don't call REAL(malloc_stats)!
}

INTERCEPTOR(uptr, malloc_usable_size, void *ptr) {
  return __sanitizer_get_allocated_size(ptr);
}

#define ENSURE_DFSAN_INITED()               \
  do {                                      \
    CHECK(!__dfsan::dfsan_init_is_running); \
    if (!__dfsan::dfsan_inited) {           \
      __dfsan::dfsan_init();                \
    }                                       \
  } while (0)

#define COMMON_INTERCEPTOR_ENTER(func, ...) \
  if (__dfsan::dfsan_init_is_running)       \
    return REAL(func)(__VA_ARGS__);         \
  ENSURE_DFSAN_INITED();                    \
  dfsan_set_label(0, __errno_location(), sizeof(int)); /* NOLINT */

INTERCEPTOR(void *, mmap, void *addr, SIZE_T length, int prot, int flags,
            int fd, OFF_T offset) {
  if (common_flags()->detect_write_exec)
    ReportMmapWriteExec(prot);
  if (!__dfsan::dfsan_inited)
    return (void *)internal_mmap(addr, length, prot, flags, fd, offset);
  COMMON_INTERCEPTOR_ENTER(mmap, addr, length, prot, flags, fd, offset);
  void *res = REAL(mmap)(addr, length, prot, flags, fd, offset);
  if (res != (void *)-1) {
    dfsan_set_label(0, res, RoundUpTo(length, GetPageSizeCached()));
  }
  return res;
}

INTERCEPTOR(void *, mmap64, void *addr, SIZE_T length, int prot, int flags,
            int fd, OFF64_T offset) {
  if (common_flags()->detect_write_exec)
    ReportMmapWriteExec(prot);
  if (!__dfsan::dfsan_inited)
    return (void *)internal_mmap(addr, length, prot, flags, fd, offset);
  COMMON_INTERCEPTOR_ENTER(mmap64, addr, length, prot, flags, fd, offset);
  void *res = REAL(mmap64)(addr, length, prot, flags, fd, offset);
  if (res != (void *)-1) {
    dfsan_set_label(0, res, RoundUpTo(length, GetPageSizeCached()));
  }
  return res;
}

INTERCEPTOR(int, munmap, void *addr, SIZE_T length) {
  if (!__dfsan::dfsan_inited)
    return internal_munmap(addr, length);
  COMMON_INTERCEPTOR_ENTER(munmap, addr, length);
  int res = REAL(munmap)(addr, length);
  if (res != -1)
    dfsan_set_label(0, addr, RoundUpTo(length, GetPageSizeCached()));
  return res;
}

#define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end)           \
  if (__dfsan::DFsanThread *t = __dfsan::GetCurrentThread()) { \
    *begin = t->tls_begin();                                   \
    *end = t->tls_end();                                       \
  } else {                                                     \
    *begin = *end = 0;                                         \
  }
#define COMMON_INTERCEPTOR_INITIALIZE_RANGE(ptr, size) \
  dfsan_set_label(0, ptr, size)

INTERCEPTOR(void *, __tls_get_addr, void *arg) {
  COMMON_INTERCEPTOR_ENTER(__tls_get_addr, arg);
  void *res = REAL(__tls_get_addr)(arg);
  uptr tls_begin, tls_end;
  COMMON_INTERCEPTOR_GET_TLS_RANGE(&tls_begin, &tls_end);
  DTLS::DTV *dtv = DTLS_on_tls_get_addr(arg, res, tls_begin, tls_end);
  if (dtv) {
    // New DTLS block has been allocated.
    COMMON_INTERCEPTOR_INITIALIZE_RANGE((void *)dtv->beg, dtv->size);
  }
  return res;
}

namespace __dfsan {
void initialize_interceptors() {
  CHECK(!interceptors_initialized);

  INTERCEPT_FUNCTION(aligned_alloc);
  INTERCEPT_FUNCTION(calloc);
  INTERCEPT_FUNCTION(cfree);
  INTERCEPT_FUNCTION(free);
  INTERCEPT_FUNCTION(mallinfo);
  INTERCEPT_FUNCTION(malloc);
  INTERCEPT_FUNCTION(malloc_stats);
  INTERCEPT_FUNCTION(malloc_usable_size);
  INTERCEPT_FUNCTION(mallopt);
  INTERCEPT_FUNCTION(memalign);
  INTERCEPT_FUNCTION(mmap);
  INTERCEPT_FUNCTION(mmap64);
  INTERCEPT_FUNCTION(munmap);
  INTERCEPT_FUNCTION(posix_memalign);
  INTERCEPT_FUNCTION(pvalloc);
  INTERCEPT_FUNCTION(realloc);
  INTERCEPT_FUNCTION(reallocarray);
  INTERCEPT_FUNCTION(valloc);
  INTERCEPT_FUNCTION(__tls_get_addr);
  INTERCEPT_FUNCTION(__libc_memalign);

  interceptors_initialized = true;
}
}  // namespace __dfsan