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
|
//===-- tysan_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 TypeSanitizer.
//
// Interceptors for standard library functions.
//===----------------------------------------------------------------------===//
#include "interception/interception.h"
#include "sanitizer_common/sanitizer_allocator_dlsym.h"
#include "sanitizer_common/sanitizer_common.h"
#include "tysan/tysan.h"
#if SANITIZER_LINUX && !SANITIZER_ANDROID
#define TYSAN_INTERCEPT___STRDUP 1
#else
#define TYSAN_INTERCEPT___STRDUP 0
#endif
#if SANITIZER_LINUX
extern "C" int mallopt(int param, int value);
#endif
using namespace __sanitizer;
using namespace __tysan;
namespace {
struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> {
static bool UseImpl() { return !tysan_inited; }
};
} // namespace
INTERCEPTOR(void *, memset, void *dst, int v, uptr size) {
if (!tysan_inited && REAL(memset) == nullptr)
return internal_memset(dst, v, size);
void *res = REAL(memset)(dst, v, size);
tysan_set_type_unknown(dst, size);
return res;
}
INTERCEPTOR(void *, memmove, void *dst, const void *src, uptr size) {
if (!tysan_inited && REAL(memmove) == nullptr)
return internal_memmove(dst, src, size);
void *res = REAL(memmove)(dst, src, size);
tysan_copy_types(dst, src, size);
return res;
}
INTERCEPTOR(void *, memcpy, void *dst, const void *src, uptr size) {
if (!tysan_inited && REAL(memcpy) == nullptr) {
// memmove is used here because on some platforms this will also
// intercept the memmove implementation.
return internal_memmove(dst, src, size);
}
void *res = REAL(memcpy)(dst, src, size);
tysan_copy_types(dst, src, size);
return res;
}
INTERCEPTOR(void *, mmap, void *addr, SIZE_T length, int prot, int flags,
int fd, OFF_T offset) {
void *res = REAL(mmap)(addr, length, prot, flags, fd, offset);
if (res != (void *)-1)
tysan_set_type_unknown(res, RoundUpTo(length, GetPageSize()));
return res;
}
#if !SANITIZER_APPLE
INTERCEPTOR(void *, mmap64, void *addr, SIZE_T length, int prot, int flags,
int fd, OFF64_T offset) {
void *res = REAL(mmap64)(addr, length, prot, flags, fd, offset);
if (res != (void *)-1)
tysan_set_type_unknown(res, RoundUpTo(length, GetPageSize()));
return res;
}
#endif
INTERCEPTOR(char *, strdup, const char *s) {
char *res = REAL(strdup)(s);
if (res)
tysan_copy_types(res, const_cast<char *>(s), internal_strlen(s));
return res;
}
#if TYSAN_INTERCEPT___STRDUP
INTERCEPTOR(char *, __strdup, const char *s) {
char *res = REAL(__strdup)(s);
if (res)
tysan_copy_types(res, const_cast<char *>(s), internal_strlen(s));
return res;
}
#endif // TYSAN_INTERCEPT___STRDUP
INTERCEPTOR(void *, malloc, uptr size) {
if (DlsymAlloc::Use())
return DlsymAlloc::Allocate(size);
void *res = REAL(malloc)(size);
if (res)
tysan_set_type_unknown(res, size);
return res;
}
#if SANITIZER_APPLE
INTERCEPTOR(uptr, malloc_size, void *ptr) {
if (DlsymAlloc::PointerIsMine(ptr))
return DlsymAlloc::GetSize(ptr);
return REAL(malloc_size)(ptr);
}
#endif
INTERCEPTOR(void *, realloc, void *ptr, uptr size) {
if (DlsymAlloc::Use() || DlsymAlloc::PointerIsMine(ptr))
return DlsymAlloc::Realloc(ptr, size);
void *res = REAL(realloc)(ptr, size);
// We might want to copy the types from the original allocation (although
// that would require that we knew its size).
if (res)
tysan_set_type_unknown(res, size);
return res;
}
INTERCEPTOR(void *, calloc, uptr nmemb, uptr size) {
if (DlsymAlloc::Use())
return DlsymAlloc::Callocate(nmemb, size);
void *res = REAL(calloc)(nmemb, size);
if (res)
tysan_set_type_unknown(res, nmemb * size);
return res;
}
INTERCEPTOR(void, free, void *ptr) {
if (DlsymAlloc::PointerIsMine(ptr))
return DlsymAlloc::Free(ptr);
REAL(free)(ptr);
}
INTERCEPTOR(void *, valloc, uptr size) {
void *res = REAL(valloc)(size);
if (res)
tysan_set_type_unknown(res, size);
return res;
}
#if SANITIZER_INTERCEPT_MEMALIGN
INTERCEPTOR(void *, memalign, uptr alignment, uptr size) {
void *res = REAL(memalign)(alignment, size);
if (res)
tysan_set_type_unknown(res, size);
return res;
}
#define TYSAN_MAYBE_INTERCEPT_MEMALIGN INTERCEPT_FUNCTION(memalign)
#else
#define TYSAN_MAYBE_INTERCEPT_MEMALIGN
#endif // SANITIZER_INTERCEPT_MEMALIGN
#if SANITIZER_INTERCEPT___LIBC_MEMALIGN
INTERCEPTOR(void *, __libc_memalign, uptr alignment, uptr size) {
void *res = REAL(__libc_memalign)(alignment, size);
if (res)
tysan_set_type_unknown(res, size);
return res;
}
#define TYSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN \
INTERCEPT_FUNCTION(__libc_memalign)
#else
#define TYSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN
#endif // SANITIZER_INTERCEPT___LIBC_MEMALIGN
#if SANITIZER_INTERCEPT_PVALLOC
INTERCEPTOR(void *, pvalloc, uptr size) {
void *res = REAL(pvalloc)(size);
if (res)
tysan_set_type_unknown(res, size);
return res;
}
#define TYSAN_MAYBE_INTERCEPT_PVALLOC INTERCEPT_FUNCTION(pvalloc)
#else
#define TYSAN_MAYBE_INTERCEPT_PVALLOC
#endif // SANITIZER_INTERCEPT_PVALLOC
#if SANITIZER_INTERCEPT_ALIGNED_ALLOC
INTERCEPTOR(void *, aligned_alloc, uptr alignment, uptr size) {
void *res = REAL(aligned_alloc)(alignment, size);
if (res)
tysan_set_type_unknown(res, size);
return res;
}
#define TYSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC INTERCEPT_FUNCTION(aligned_alloc)
#else
#define TYSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC
#endif
INTERCEPTOR(int, posix_memalign, void **memptr, uptr alignment, uptr size) {
int res = REAL(posix_memalign)(memptr, alignment, size);
if (res == 0 && *memptr)
tysan_set_type_unknown(*memptr, size);
return res;
}
namespace __tysan {
void InitializeInterceptors() {
static int inited = 0;
CHECK_EQ(inited, 0);
// Instruct libc malloc to consume less memory.
#if SANITIZER_LINUX
mallopt(1, 0); // M_MXFAST
mallopt(-3, 32 * 1024); // M_MMAP_THRESHOLD
#endif
INTERCEPT_FUNCTION(mmap);
INTERCEPT_FUNCTION(mmap64);
INTERCEPT_FUNCTION(strdup);
#if TYSAN_INTERCEPT___STRDUP
INTERCEPT_FUNCTION(__strdup);
#endif
INTERCEPT_FUNCTION(malloc);
INTERCEPT_FUNCTION(calloc);
INTERCEPT_FUNCTION(free);
INTERCEPT_FUNCTION(realloc);
INTERCEPT_FUNCTION(valloc);
TYSAN_MAYBE_INTERCEPT_MEMALIGN;
TYSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN;
TYSAN_MAYBE_INTERCEPT_PVALLOC;
TYSAN_MAYBE_INTERCEPT_ALIGNED_ALLOC
INTERCEPT_FUNCTION(posix_memalign);
INTERCEPT_FUNCTION(memset);
INTERCEPT_FUNCTION(memmove);
INTERCEPT_FUNCTION(memcpy);
inited = 1;
}
} // namespace __tysan
|