| 1 | //===-- msan_interceptors.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 a part of MemorySanitizer. |
| 10 | // |
| 11 | // Interceptors for standard library functions. |
| 12 | // |
| 13 | // FIXME: move as many interceptors as possible into |
| 14 | // sanitizer_common/sanitizer_common_interceptors.h |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | #define SANITIZER_COMMON_NO_REDEFINE_BUILTINS |
| 18 | |
| 19 | #include "interception/interception.h" |
| 20 | #include "msan.h" |
| 21 | #include "msan_chained_origin_depot.h" |
| 22 | #include "msan_dl.h" |
| 23 | #include "msan_origin.h" |
| 24 | #include "msan_poisoning.h" |
| 25 | #include "msan_report.h" |
| 26 | #include "msan_thread.h" |
| 27 | #include "sanitizer_common/sanitizer_allocator.h" |
| 28 | #include "sanitizer_common/sanitizer_allocator_dlsym.h" |
| 29 | #include "sanitizer_common/sanitizer_allocator_interface.h" |
| 30 | #include "sanitizer_common/sanitizer_atomic.h" |
| 31 | #include "sanitizer_common/sanitizer_common.h" |
| 32 | #include "sanitizer_common/sanitizer_errno.h" |
| 33 | #include "sanitizer_common/sanitizer_errno_codes.h" |
| 34 | #include "sanitizer_common/sanitizer_glibc_version.h" |
| 35 | #include "sanitizer_common/sanitizer_libc.h" |
| 36 | #include "sanitizer_common/sanitizer_linux.h" |
| 37 | #include "sanitizer_common/sanitizer_platform_interceptors.h" |
| 38 | #include "sanitizer_common/sanitizer_platform_limits_netbsd.h" |
| 39 | #include "sanitizer_common/sanitizer_platform_limits_posix.h" |
| 40 | #include "sanitizer_common/sanitizer_stackdepot.h" |
| 41 | #include "sanitizer_common/sanitizer_vector.h" |
| 42 | |
| 43 | #if SANITIZER_NETBSD |
| 44 | #define fstat __fstat50 |
| 45 | #define gettimeofday __gettimeofday50 |
| 46 | #define getrusage __getrusage50 |
| 47 | #define tzset __tzset50 |
| 48 | #endif |
| 49 | |
| 50 | #include <stdarg.h> |
| 51 | // ACHTUNG! No other system header includes in this file. |
| 52 | // Ideally, we should get rid of stdarg.h as well. |
| 53 | |
| 54 | using namespace __msan; |
| 55 | |
| 56 | using __sanitizer::memory_order; |
| 57 | using __sanitizer::atomic_load; |
| 58 | using __sanitizer::atomic_store; |
| 59 | using __sanitizer::atomic_uintptr_t; |
| 60 | |
| 61 | DECLARE_REAL(SIZE_T, strlen, const char *s) |
| 62 | DECLARE_REAL(SIZE_T, strnlen, const char *s, SIZE_T maxlen) |
| 63 | DECLARE_REAL(void *, memcpy, void *dest, const void *src, SIZE_T n) |
| 64 | DECLARE_REAL(void *, memset, void *dest, int c, SIZE_T n) |
| 65 | |
| 66 | // True if this is a nested interceptor. |
| 67 | static THREADLOCAL int in_interceptor_scope; |
| 68 | |
| 69 | void __msan_scoped_disable_interceptor_checks() { ++in_interceptor_scope; } |
| 70 | void __msan_scoped_enable_interceptor_checks() { --in_interceptor_scope; } |
| 71 | |
| 72 | struct InterceptorScope { |
| 73 | InterceptorScope() { ++in_interceptor_scope; } |
| 74 | ~InterceptorScope() { --in_interceptor_scope; } |
| 75 | }; |
| 76 | |
| 77 | bool IsInInterceptorScope() { |
| 78 | return in_interceptor_scope; |
| 79 | } |
| 80 | |
| 81 | struct DlsymAlloc : public DlSymAllocator<DlsymAlloc> { |
| 82 | static bool UseImpl() { return !msan_inited; } |
| 83 | }; |
| 84 | |
| 85 | #define ENSURE_MSAN_INITED() do { \ |
| 86 | CHECK(!msan_init_is_running); \ |
| 87 | if (!msan_inited) { \ |
| 88 | __msan_init(); \ |
| 89 | } \ |
| 90 | } while (0) |
| 91 | |
| 92 | // Check that [x, x+n) range is unpoisoned. |
| 93 | #define CHECK_UNPOISONED_0(x, n) \ |
| 94 | do { \ |
| 95 | sptr __offset = __msan_test_shadow(x, n); \ |
| 96 | if (__msan::IsInSymbolizerOrUnwider()) \ |
| 97 | break; \ |
| 98 | if (__offset >= 0 && __msan::flags()->report_umrs) { \ |
| 99 | GET_CALLER_PC_BP; \ |
| 100 | ReportUMRInsideAddressRange(__func__, x, n, __offset); \ |
| 101 | __msan::PrintWarningWithOrigin( \ |
| 102 | pc, bp, __msan_get_origin((const char *)x + __offset)); \ |
| 103 | if (__msan::flags()->halt_on_error) { \ |
| 104 | Printf("Exiting\n"); \ |
| 105 | Die(); \ |
| 106 | } \ |
| 107 | } \ |
| 108 | } while (0) |
| 109 | |
| 110 | // Check that [x, x+n) range is unpoisoned unless we are in a nested |
| 111 | // interceptor. |
| 112 | #define CHECK_UNPOISONED(x, n) \ |
| 113 | do { \ |
| 114 | if (!IsInInterceptorScope()) CHECK_UNPOISONED_0(x, n); \ |
| 115 | } while (0) |
| 116 | |
| 117 | #define CHECK_UNPOISONED_STRING_OF_LEN(x, len, n) \ |
| 118 | CHECK_UNPOISONED((x), \ |
| 119 | common_flags()->strict_string_checks ? (len) + 1 : (n) ) |
| 120 | |
| 121 | #define CHECK_UNPOISONED_STRING(x, n) \ |
| 122 | CHECK_UNPOISONED_STRING_OF_LEN((x), internal_strlen(x), (n)) |
| 123 | |
| 124 | #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD |
| 125 | INTERCEPTOR(SIZE_T, fread_unlocked, void *ptr, SIZE_T size, SIZE_T nmemb, |
| 126 | void *file) { |
| 127 | ENSURE_MSAN_INITED(); |
| 128 | SIZE_T res = REAL(fread_unlocked)(ptr, size, nmemb, file); |
| 129 | if (res > 0) |
| 130 | __msan_unpoison(a: ptr, size: res *size); |
| 131 | return res; |
| 132 | } |
| 133 | #define MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED INTERCEPT_FUNCTION(fread_unlocked) |
| 134 | #else |
| 135 | #define MSAN_MAYBE_INTERCEPT_FREAD_UNLOCKED |
| 136 | #endif |
| 137 | |
| 138 | #if !SANITIZER_NETBSD |
| 139 | INTERCEPTOR(void *, mempcpy, void *dest, const void *src, SIZE_T n) { |
| 140 | return (char *)__msan_memcpy(dst: dest, src, size: n) + n; |
| 141 | } |
| 142 | #define MSAN_MAYBE_INTERCEPT_MEMPCPY INTERCEPT_FUNCTION(mempcpy) |
| 143 | #else |
| 144 | #define MSAN_MAYBE_INTERCEPT_MEMPCPY |
| 145 | #endif |
| 146 | |
| 147 | INTERCEPTOR(void *, memccpy, void *dest, const void *src, int c, SIZE_T n) { |
| 148 | ENSURE_MSAN_INITED(); |
| 149 | void *res = REAL(memccpy)(dest, src, c, n); |
| 150 | CHECK(!res || (res >= dest && res <= (char *)dest + n)); |
| 151 | SIZE_T sz = res ? (char *)res - (char *)dest : n; |
| 152 | CHECK_UNPOISONED(src, sz); |
| 153 | __msan_unpoison(a: dest, size: sz); |
| 154 | return res; |
| 155 | } |
| 156 | |
| 157 | INTERCEPTOR(void *, bcopy, const void *src, void *dest, SIZE_T n) { |
| 158 | return __msan_memmove(dest, src, n); |
| 159 | } |
| 160 | |
| 161 | INTERCEPTOR(int, posix_memalign, void **memptr, SIZE_T alignment, SIZE_T size) { |
| 162 | GET_MALLOC_STACK_TRACE; |
| 163 | CHECK_NE(memptr, 0); |
| 164 | int res = msan_posix_memalign(memptr, alignment, size, stack: &stack); |
| 165 | if (!res) |
| 166 | __msan_unpoison(a: memptr, size: sizeof(*memptr)); |
| 167 | return res; |
| 168 | } |
| 169 | |
| 170 | #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD |
| 171 | INTERCEPTOR(void *, memalign, SIZE_T alignment, SIZE_T size) { |
| 172 | GET_MALLOC_STACK_TRACE; |
| 173 | return msan_memalign(alignment, size, stack: &stack); |
| 174 | } |
| 175 | #define MSAN_MAYBE_INTERCEPT_MEMALIGN INTERCEPT_FUNCTION(memalign) |
| 176 | #else |
| 177 | #define MSAN_MAYBE_INTERCEPT_MEMALIGN |
| 178 | #endif |
| 179 | |
| 180 | INTERCEPTOR(void *, aligned_alloc, SIZE_T alignment, SIZE_T size) { |
| 181 | GET_MALLOC_STACK_TRACE; |
| 182 | return msan_aligned_alloc(alignment, size, stack: &stack); |
| 183 | } |
| 184 | |
| 185 | #if !SANITIZER_NETBSD |
| 186 | INTERCEPTOR(void *, __libc_memalign, SIZE_T alignment, SIZE_T size) { |
| 187 | GET_MALLOC_STACK_TRACE; |
| 188 | return msan_memalign(alignment, size, stack: &stack); |
| 189 | } |
| 190 | #define MSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN INTERCEPT_FUNCTION(__libc_memalign) |
| 191 | #else |
| 192 | #define MSAN_MAYBE_INTERCEPT___LIBC_MEMALIGN |
| 193 | #endif |
| 194 | |
| 195 | INTERCEPTOR(void *, valloc, SIZE_T size) { |
| 196 | GET_MALLOC_STACK_TRACE; |
| 197 | return msan_valloc(size, stack: &stack); |
| 198 | } |
| 199 | |
| 200 | #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD |
| 201 | INTERCEPTOR(void *, pvalloc, SIZE_T size) { |
| 202 | GET_MALLOC_STACK_TRACE; |
| 203 | return msan_pvalloc(size, stack: &stack); |
| 204 | } |
| 205 | #define MSAN_MAYBE_INTERCEPT_PVALLOC INTERCEPT_FUNCTION(pvalloc) |
| 206 | #else |
| 207 | #define MSAN_MAYBE_INTERCEPT_PVALLOC |
| 208 | #endif |
| 209 | |
| 210 | INTERCEPTOR(void, free, void *ptr) { |
| 211 | if (UNLIKELY(!ptr)) |
| 212 | return; |
| 213 | if (DlsymAlloc::PointerIsMine(ptr)) |
| 214 | return DlsymAlloc::Free(ptr); |
| 215 | GET_MALLOC_STACK_TRACE; |
| 216 | MsanDeallocate(stack: &stack, ptr); |
| 217 | } |
| 218 | |
| 219 | #if SANITIZER_INTERCEPT_FREE_SIZED |
| 220 | INTERCEPTOR(void, free_sized, void *ptr, uptr size) { |
| 221 | if (UNLIKELY(!ptr)) |
| 222 | return; |
| 223 | if (DlsymAlloc::PointerIsMine(ptr)) |
| 224 | return DlsymAlloc::Free(ptr); |
| 225 | GET_MALLOC_STACK_TRACE; |
| 226 | MsanDeallocate(stack: &stack, ptr); |
| 227 | } |
| 228 | # define MSAN_MAYBE_INTERCEPT_FREE_SIZED INTERCEPT_FUNCTION(free_sized) |
| 229 | #else |
| 230 | # define MSAN_MAYBE_INTERCEPT_FREE_SIZED |
| 231 | #endif |
| 232 | |
| 233 | #if SANITIZER_INTERCEPT_FREE_ALIGNED_SIZED |
| 234 | INTERCEPTOR(void, free_aligned_sized, void *ptr, uptr alignment, uptr size) { |
| 235 | if (UNLIKELY(!ptr)) |
| 236 | return; |
| 237 | if (DlsymAlloc::PointerIsMine(ptr)) |
| 238 | return DlsymAlloc::Free(ptr); |
| 239 | GET_MALLOC_STACK_TRACE; |
| 240 | MsanDeallocate(stack: &stack, ptr); |
| 241 | } |
| 242 | # define MSAN_MAYBE_INTERCEPT_FREE_ALIGNED_SIZED \ |
| 243 | INTERCEPT_FUNCTION(free_aligned_sized) |
| 244 | #else |
| 245 | # define MSAN_MAYBE_INTERCEPT_FREE_ALIGNED_SIZED |
| 246 | #endif |
| 247 | |
| 248 | #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD |
| 249 | INTERCEPTOR(void, cfree, void *ptr) { |
| 250 | if (UNLIKELY(!ptr)) |
| 251 | return; |
| 252 | if (DlsymAlloc::PointerIsMine(ptr)) |
| 253 | return DlsymAlloc::Free(ptr); |
| 254 | GET_MALLOC_STACK_TRACE; |
| 255 | MsanDeallocate(stack: &stack, ptr); |
| 256 | } |
| 257 | # define MSAN_MAYBE_INTERCEPT_CFREE INTERCEPT_FUNCTION(cfree) |
| 258 | #else |
| 259 | #define MSAN_MAYBE_INTERCEPT_CFREE |
| 260 | #endif |
| 261 | |
| 262 | #if !SANITIZER_NETBSD |
| 263 | INTERCEPTOR(uptr, malloc_usable_size, void *ptr) { |
| 264 | return __sanitizer_get_allocated_size(p: ptr); |
| 265 | } |
| 266 | #define MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE \ |
| 267 | INTERCEPT_FUNCTION(malloc_usable_size) |
| 268 | #else |
| 269 | #define MSAN_MAYBE_INTERCEPT_MALLOC_USABLE_SIZE |
| 270 | #endif |
| 271 | |
| 272 | #if (!SANITIZER_FREEBSD && !SANITIZER_NETBSD) || __GLIBC_PREREQ(2, 33) |
| 273 | template <class T> |
| 274 | static NOINLINE void clear_mallinfo(T *sret) { |
| 275 | ENSURE_MSAN_INITED(); |
| 276 | internal_memset(sret, 0, sizeof(*sret)); |
| 277 | __msan_unpoison(sret, sizeof(*sret)); |
| 278 | } |
| 279 | #endif |
| 280 | |
| 281 | #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD |
| 282 | // Interceptors use NRVO and assume that sret will be pre-allocated in |
| 283 | // caller frame. |
| 284 | INTERCEPTOR(__sanitizer_struct_mallinfo, mallinfo,) { |
| 285 | __sanitizer_struct_mallinfo sret; |
| 286 | clear_mallinfo(sret: &sret); |
| 287 | return sret; |
| 288 | } |
| 289 | # define MSAN_MAYBE_INTERCEPT_MALLINFO INTERCEPT_FUNCTION(mallinfo) |
| 290 | #else |
| 291 | # define MSAN_MAYBE_INTERCEPT_MALLINFO |
| 292 | #endif |
| 293 | |
| 294 | #if __GLIBC_PREREQ(2, 33) |
| 295 | INTERCEPTOR(__sanitizer_struct_mallinfo2, mallinfo2) { |
| 296 | __sanitizer_struct_mallinfo2 sret; |
| 297 | clear_mallinfo(sret: &sret); |
| 298 | return sret; |
| 299 | } |
| 300 | # define MSAN_MAYBE_INTERCEPT_MALLINFO2 INTERCEPT_FUNCTION(mallinfo2) |
| 301 | #else |
| 302 | # define MSAN_MAYBE_INTERCEPT_MALLINFO2 |
| 303 | #endif |
| 304 | |
| 305 | #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD |
| 306 | INTERCEPTOR(int, mallopt, int cmd, int value) { |
| 307 | return 0; |
| 308 | } |
| 309 | #define MSAN_MAYBE_INTERCEPT_MALLOPT INTERCEPT_FUNCTION(mallopt) |
| 310 | #else |
| 311 | #define MSAN_MAYBE_INTERCEPT_MALLOPT |
| 312 | #endif |
| 313 | |
| 314 | #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD |
| 315 | INTERCEPTOR(void, malloc_stats, void) { |
| 316 | // FIXME: implement, but don't call REAL(malloc_stats)! |
| 317 | } |
| 318 | #define MSAN_MAYBE_INTERCEPT_MALLOC_STATS INTERCEPT_FUNCTION(malloc_stats) |
| 319 | #else |
| 320 | #define MSAN_MAYBE_INTERCEPT_MALLOC_STATS |
| 321 | #endif |
| 322 | |
| 323 | INTERCEPTOR(char *, strcpy, char *dest, const char *src) { |
| 324 | ENSURE_MSAN_INITED(); |
| 325 | GET_STORE_STACK_TRACE; |
| 326 | SIZE_T n = internal_strlen(s: src); |
| 327 | CHECK_UNPOISONED_STRING(src + n, 0); |
| 328 | char *res = REAL(strcpy)(dest, src); |
| 329 | CopyShadowAndOrigin(dst: dest, src, size: n + 1, stack: &stack); |
| 330 | return res; |
| 331 | } |
| 332 | |
| 333 | INTERCEPTOR(char *, strncpy, char *dest, const char *src, SIZE_T n) { |
| 334 | ENSURE_MSAN_INITED(); |
| 335 | GET_STORE_STACK_TRACE; |
| 336 | SIZE_T copy_size = internal_strnlen(s: src, maxlen: n); |
| 337 | if (copy_size < n) |
| 338 | copy_size++; // trailing \0 |
| 339 | char *res = REAL(strncpy)(dest, src, n); |
| 340 | CopyShadowAndOrigin(dst: dest, src, size: copy_size, stack: &stack); |
| 341 | __msan_unpoison(a: dest + copy_size, size: n - copy_size); |
| 342 | return res; |
| 343 | } |
| 344 | |
| 345 | #if !SANITIZER_NETBSD |
| 346 | INTERCEPTOR(char *, stpcpy, char *dest, const char *src) { |
| 347 | ENSURE_MSAN_INITED(); |
| 348 | GET_STORE_STACK_TRACE; |
| 349 | SIZE_T n = internal_strlen(s: src); |
| 350 | CHECK_UNPOISONED_STRING(src + n, 0); |
| 351 | char *res = REAL(stpcpy)(dest, src); |
| 352 | CopyShadowAndOrigin(dst: dest, src, size: n + 1, stack: &stack); |
| 353 | return res; |
| 354 | } |
| 355 | |
| 356 | INTERCEPTOR(char *, stpncpy, char *dest, const char *src, SIZE_T n) { |
| 357 | ENSURE_MSAN_INITED(); |
| 358 | GET_STORE_STACK_TRACE; |
| 359 | SIZE_T copy_size = Min(a: n, b: internal_strnlen(s: src, maxlen: n) + 1); |
| 360 | char *res = REAL(stpncpy)(dest, src, n); |
| 361 | CopyShadowAndOrigin(dst: dest, src, size: copy_size, stack: &stack); |
| 362 | __msan_unpoison(a: dest + copy_size, size: n - copy_size); |
| 363 | return res; |
| 364 | } |
| 365 | # define MSAN_MAYBE_INTERCEPT_STPCPY INTERCEPT_FUNCTION(stpcpy) |
| 366 | # define MSAN_MAYBE_INTERCEPT_STPNCPY INTERCEPT_FUNCTION(stpncpy) |
| 367 | #else |
| 368 | #define MSAN_MAYBE_INTERCEPT_STPCPY |
| 369 | # define MSAN_MAYBE_INTERCEPT_STPNCPY |
| 370 | #endif |
| 371 | |
| 372 | INTERCEPTOR(char *, strdup, char *src) { |
| 373 | ENSURE_MSAN_INITED(); |
| 374 | GET_STORE_STACK_TRACE; |
| 375 | // On FreeBSD strdup() leverages strlen(). |
| 376 | InterceptorScope interceptor_scope; |
| 377 | SIZE_T n = internal_strlen(s: src); |
| 378 | CHECK_UNPOISONED_STRING(src + n, 0); |
| 379 | char *res = REAL(strdup)(src); |
| 380 | CopyShadowAndOrigin(dst: res, src, size: n + 1, stack: &stack); |
| 381 | return res; |
| 382 | } |
| 383 | |
| 384 | #if !SANITIZER_FREEBSD && !SANITIZER_NETBSD |
| 385 | INTERCEPTOR(char *, __strdup, char *src) { |
| 386 | ENSURE_MSAN_INITED(); |
| 387 | GET_STORE_STACK_TRACE; |
| 388 | SIZE_T n = internal_strlen(s: src); |
| 389 | CHECK_UNPOISONED_STRING(src + n, 0); |
| 390 | char *res = REAL(__strdup)(src); |
| 391 | CopyShadowAndOrigin(dst: res, src, size: n + 1, stack: &stack); |
| 392 | return res; |
| 393 | } |
| 394 | #define MSAN_MAYBE_INTERCEPT___STRDUP INTERCEPT_FUNCTION(__strdup) |
| 395 | #else |
| 396 | #define MSAN_MAYBE_INTERCEPT___STRDUP |
| 397 | #endif |
| 398 | |
| 399 | #if !SANITIZER_NETBSD |
| 400 | INTERCEPTOR(char *, gcvt, double number, SIZE_T ndigit, char *buf) { |
| 401 | ENSURE_MSAN_INITED(); |
| 402 | char *res = REAL(gcvt)(number, ndigit, buf); |
| 403 | SIZE_T n = internal_strlen(s: buf); |
| 404 | __msan_unpoison(a: buf, size: n + 1); |
| 405 | return res; |
| 406 | } |
| 407 | #define MSAN_MAYBE_INTERCEPT_GCVT INTERCEPT_FUNCTION(gcvt) |
| 408 | #else |
| 409 | #define MSAN_MAYBE_INTERCEPT_GCVT |
| 410 | #endif |
| 411 | |
| 412 | INTERCEPTOR(char *, strcat, char *dest, const char *src) { |
| 413 | ENSURE_MSAN_INITED(); |
| 414 | GET_STORE_STACK_TRACE; |
| 415 | SIZE_T src_size = internal_strlen(s: src); |
| 416 | SIZE_T dest_size = internal_strlen(s: dest); |
| 417 | CHECK_UNPOISONED_STRING(src + src_size, 0); |
| 418 | CHECK_UNPOISONED_STRING(dest + dest_size, 0); |
| 419 | char *res = REAL(strcat)(dest, src); |
| 420 | CopyShadowAndOrigin(dst: dest + dest_size, src, size: src_size + 1, stack: &stack); |
| 421 | return res; |
| 422 | } |
| 423 | |
| 424 | INTERCEPTOR(char *, strncat, char *dest, const char *src, SIZE_T n) { |
| 425 | ENSURE_MSAN_INITED(); |
| 426 | GET_STORE_STACK_TRACE; |
| 427 | SIZE_T dest_size = internal_strlen(s: dest); |
| 428 | SIZE_T copy_size = internal_strnlen(s: src, maxlen: n); |
| 429 | CHECK_UNPOISONED_STRING(dest + dest_size, 0); |
| 430 | char *res = REAL(strncat)(dest, src, n); |
| 431 | CopyShadowAndOrigin(dst: dest + dest_size, src, size: copy_size, stack: &stack); |
| 432 | __msan_unpoison(a: dest + dest_size + copy_size, size: 1); // \0 |
| 433 | return res; |
| 434 | } |
| 435 | |
| 436 | // Hack: always pass nptr and endptr as part of __VA_ARGS_ to avoid having to |
| 437 | // deal with empty __VA_ARGS__ in the case of INTERCEPTOR_STRTO. |
| 438 | #define INTERCEPTOR_STRTO_BODY(ret_type, func, ...) \ |
| 439 | ENSURE_MSAN_INITED(); \ |
| 440 | ret_type res = REAL(func)(__VA_ARGS__); \ |
| 441 | __msan_unpoison(endptr, sizeof(*endptr)); \ |
| 442 | return res; |
| 443 | |
| 444 | // On s390x, long double return values are passed via implicit reference, |
| 445 | // which needs to be unpoisoned. We make the implicit pointer explicit. |
| 446 | #define INTERCEPTOR_STRTO_SRET_BODY(func, sret, ...) \ |
| 447 | ENSURE_MSAN_INITED(); \ |
| 448 | REAL(func)(sret, __VA_ARGS__); \ |
| 449 | __msan_unpoison(sret, sizeof(*sret)); \ |
| 450 | __msan_unpoison(endptr, sizeof(*endptr)); |
| 451 | |
| 452 | #define INTERCEPTOR_STRTO(ret_type, func, char_type) \ |
| 453 | INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr) { \ |
| 454 | INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr); \ |
| 455 | } |
| 456 | |
| 457 | #define INTERCEPTOR_STRTO_SRET(ret_type, func, char_type) \ |
| 458 | INTERCEPTOR(void, func, ret_type *sret, const char_type *nptr, \ |
| 459 | char_type **endptr) { \ |
| 460 | INTERCEPTOR_STRTO_SRET_BODY(func, sret, nptr, endptr); \ |
| 461 | } |
| 462 | |
| 463 | #define INTERCEPTOR_STRTO_BASE(ret_type, func, char_type) \ |
| 464 | INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \ |
| 465 | int base) { \ |
| 466 | INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, base); \ |
| 467 | } |
| 468 | |
| 469 | #define INTERCEPTOR_STRTO_LOC(ret_type, func, char_type) \ |
| 470 | INTERCEPTOR(ret_type, func, const char_type *nptr, char_type **endptr, \ |
| 471 | void *loc) { \ |
| 472 | INTERCEPTOR_STRTO_BODY(ret_type, func, nptr, endptr, loc); \ |
| 473 | } |
| 474 | |
| 475 | #define INTERCEPTOR_STRTO_SRET_LOC(ret_type, func, char_type) \ |
| 476 | INTERCEPTOR(void, func, ret_type *sret, const char_type *nptr, \ |
| 477 | char_type **endptr, void *loc) { \ |
| 478 | INTERCEPTOR_STRTO_SRET_BODY(func, sret, nptr, endptr, loc); \ |
| 479 | } |
| 480 | |
| 481 | #define INTERCEPTOR_STRTO_BASE_LOC(ret_type, func, char_type) \ |
| 482 | INTERCEPTOR(ret_type, func, const char_type * |
|---|