head 1.5; access; symbols netbsd-11-0-RELEASE:1.3 netbsd-11-0-RC7:1.3 netbsd-11-0-RC6:1.3 netbsd-11-0-RC5:1.3 netbsd-11-0-RC4:1.3 netbsd-11-0-RC3:1.3 netbsd-11-0-RC2:1.3 netbsd-11-0-RC1:1.3 gcc-14-3-0:1.1.1.3 perseant-exfatfs-base-20250801:1.3 netbsd-11:1.3.0.4 netbsd-11-base:1.3 gcc-12-5-0:1.1.1.2 perseant-exfatfs-base-20240630:1.3 gcc-12-4-0:1.1.1.2 perseant-exfatfs:1.3.0.2 perseant-exfatfs-base:1.3 gcc-12-3-0:1.1.1.2 gcc-10-5-0:1.1.1.1 gcc-10-4-0:1.1.1.1 cjep_sun2x:1.2.0.4 cjep_sun2x-base:1.2 cjep_staticlib_x-base1:1.2 cjep_staticlib_x:1.2.0.2 cjep_staticlib_x-base:1.2 gcc-10-3-0:1.1.1.1 FSF:1.1.1; locks; strict; comment @// @; 1.5 date 2025.10.15.15.50.46; author christos; state Exp; branches; next 1.4; commitid kKUSMWEtXD2GiGeG; 1.4 date 2025.09.14.00.08.54; author mrg; state Exp; branches; next 1.3; commitid x9D5QEnvbeMI4CaG; 1.3 date 2023.07.31.01.44.54; author mrg; state Exp; branches; next 1.2; commitid q79F5Opf0FLsyTyE; 1.2 date 2021.04.11.23.54.25; author mrg; state dead; branches; next 1.1; commitid wJn7ggfUTEMOWVOC; 1.1 date 2021.04.10.22.09.22; author mrg; state Exp; branches 1.1.1.1; next ; commitid eC4g0MRpqTvEkNOC; 1.1.1.1 date 2021.04.10.22.09.22; author mrg; state Exp; branches; next 1.1.1.2; commitid eC4g0MRpqTvEkNOC; 1.1.1.2 date 2023.07.30.05.20.41; author mrg; state Exp; branches; next 1.1.1.3; commitid tk6nV4mbc9nVEMyE; 1.1.1.3 date 2025.09.13.23.45.04; author mrg; state Exp; branches; next ; commitid KwhwN4krNWa6XBaG; desc @@ 1.5 log @fix the remaining __cxa_atexit(f, 0, 0) -> atexit(f) to avoid libc assertion. @ text @//===-- asan_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 AddressSanitizer, an address sanity checker. // // Intercept various libc functions. //===----------------------------------------------------------------------===// #include "asan_interceptors.h" #include "asan_allocator.h" #include "asan_internal.h" #include "asan_mapping.h" #include "asan_poisoning.h" #include "asan_report.h" #include "asan_stack.h" #include "asan_stats.h" #include "asan_suppressions.h" #include "asan_thread.h" #include "lsan/lsan_common.h" #include "sanitizer_common/sanitizer_errno.h" #include "sanitizer_common/sanitizer_internal_defs.h" #include "sanitizer_common/sanitizer_libc.h" // There is no general interception at all on Fuchsia. // Only the functions in asan_interceptors_memintrinsics.cpp are // really defined to replace libc functions. #if !SANITIZER_FUCHSIA # if SANITIZER_POSIX # include "sanitizer_common/sanitizer_posix.h" # endif # if ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION || \ ASAN_INTERCEPT__SJLJ_UNWIND_RAISEEXCEPTION # include # endif # if defined(__i386) && SANITIZER_LINUX # define ASAN_PTHREAD_CREATE_VERSION "GLIBC_2.1" # elif defined(__mips__) && SANITIZER_LINUX # define ASAN_PTHREAD_CREATE_VERSION "GLIBC_2.2" # endif namespace __asan { #define ASAN_READ_STRING_OF_LEN(ctx, s, len, n) \ ASAN_READ_RANGE((ctx), (s), \ common_flags()->strict_string_checks ? (len) + 1 : (n)) # define ASAN_READ_STRING(ctx, s, n) \ ASAN_READ_STRING_OF_LEN((ctx), (s), internal_strlen(s), (n)) static inline uptr MaybeRealStrnlen(const char *s, uptr maxlen) { #if SANITIZER_INTERCEPT_STRNLEN if (REAL(strnlen)) { return REAL(strnlen)(s, maxlen); } #endif return internal_strnlen(s, maxlen); } void SetThreadName(const char *name) { AsanThread *t = GetCurrentThread(); if (t) asanThreadRegistry().SetThreadName(t->tid(), name); } int OnExit() { if (CAN_SANITIZE_LEAKS && common_flags()->detect_leaks && __lsan::HasReportedLeaks()) { return common_flags()->exitcode; } // FIXME: ask frontend whether we need to return failure. return 0; } } // namespace __asan // ---------------------- Wrappers ---------------- {{{1 using namespace __asan; DECLARE_REAL_AND_INTERCEPTOR(void *, malloc, uptr) DECLARE_REAL_AND_INTERCEPTOR(void, free, void *) #define COMMON_INTERCEPT_FUNCTION_VER(name, ver) \ ASAN_INTERCEPT_FUNC_VER(name, ver) #define COMMON_INTERCEPT_FUNCTION_VER_UNVERSIONED_FALLBACK(name, ver) \ ASAN_INTERCEPT_FUNC_VER_UNVERSIONED_FALLBACK(name, ver) #define COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ptr, size) \ ASAN_WRITE_RANGE(ctx, ptr, size) #define COMMON_INTERCEPTOR_READ_RANGE(ctx, ptr, size) \ ASAN_READ_RANGE(ctx, ptr, size) # define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \ ASAN_INTERCEPTOR_ENTER(ctx, func); \ do { \ if (AsanInitIsRunning()) \ return REAL(func)(__VA_ARGS__); \ if (SANITIZER_APPLE && UNLIKELY(!AsanInited())) \ return REAL(func)(__VA_ARGS__); \ ENSURE_ASAN_INITED(); \ } while (false) #define COMMON_INTERCEPTOR_DIR_ACQUIRE(ctx, path) \ do { \ } while (false) #define COMMON_INTERCEPTOR_FD_ACQUIRE(ctx, fd) \ do { \ } while (false) #define COMMON_INTERCEPTOR_FD_RELEASE(ctx, fd) \ do { \ } while (false) #define COMMON_INTERCEPTOR_FD_SOCKET_ACCEPT(ctx, fd, newfd) \ do { \ } while (false) #define COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, name) SetThreadName(name) // Should be asanThreadRegistry().SetThreadNameByUserId(thread, name) // But asan does not remember UserId's for threads (pthread_t); // and remembers all ever existed threads, so the linear search by UserId // can be slow. #define COMMON_INTERCEPTOR_SET_PTHREAD_NAME(ctx, thread, name) \ do { \ } while (false) #define COMMON_INTERCEPTOR_BLOCK_REAL(name) REAL(name) // Strict init-order checking is dlopen-hostile: // https://github.com/google/sanitizers/issues/178 # define COMMON_INTERCEPTOR_DLOPEN(filename, flag) \ ({ \ if (flags()->strict_init_order) \ StopInitOrderChecking(); \ CheckNoDeepBind(filename, flag); \ REAL(dlopen)(filename, flag); \ }) # define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit() # define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) # define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() # define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (!AsanInited()) # define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) \ if (AsanThread *t = GetCurrentThread()) { \ *begin = t->tls_begin(); \ *end = t->tls_end(); \ } else { \ *begin = *end = 0; \ } template static void* mmap_interceptor(Mmap real_mmap, void *addr, SIZE_T length, int prot, int flags, int fd, OFF64_T offset) { void *res = real_mmap(addr, length, prot, flags, fd, offset); if (length && res != (void *)-1) { const uptr beg = reinterpret_cast(res); DCHECK(IsAligned(beg, GetPageSize())); SIZE_T rounded_length = RoundUpTo(length, GetPageSize()); // Only unpoison shadow if it's an ASAN managed address. if (AddrIsInMem(beg) && AddrIsInMem(beg + rounded_length - 1)) PoisonShadow(beg, RoundUpTo(length, GetPageSize()), 0); } return res; } template static int munmap_interceptor(Munmap real_munmap, void *addr, SIZE_T length) { // We should not tag if munmap fail, but it's to late to tag after // real_munmap, as the pages could be mmaped by another thread. const uptr beg = reinterpret_cast(addr); if (length && IsAligned(beg, GetPageSize())) { SIZE_T rounded_length = RoundUpTo(length, GetPageSize()); // Protect from unmapping the shadow. if (AddrIsInMem(beg) && AddrIsInMem(beg + rounded_length - 1)) PoisonShadow(beg, rounded_length, 0); } return real_munmap(addr, length); } # define COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, length, prot, flags, \ fd, offset) \ do { \ (void)(ctx); \ return mmap_interceptor(REAL(mmap), addr, sz, prot, flags, fd, off); \ } while (false) # define COMMON_INTERCEPTOR_MUNMAP_IMPL(ctx, addr, length) \ do { \ (void)(ctx); \ return munmap_interceptor(REAL(munmap), addr, sz); \ } while (false) #if CAN_SANITIZE_LEAKS #define COMMON_INTERCEPTOR_STRERROR() \ __lsan::ScopedInterceptorDisabler disabler #endif #define SIGNAL_INTERCEPTOR_ENTER() ENSURE_ASAN_INITED() #include "sanitizer_common/sanitizer_common_interceptors.inc" #include "sanitizer_common/sanitizer_signal_interceptors.inc" // Syscall interceptors don't have contexts, we don't support suppressions // for them. #define COMMON_SYSCALL_PRE_READ_RANGE(p, s) ASAN_READ_RANGE(nullptr, p, s) #define COMMON_SYSCALL_PRE_WRITE_RANGE(p, s) ASAN_WRITE_RANGE(nullptr, p, s) #define COMMON_SYSCALL_POST_READ_RANGE(p, s) \ do { \ (void)(p); \ (void)(s); \ } while (false) #define COMMON_SYSCALL_POST_WRITE_RANGE(p, s) \ do { \ (void)(p); \ (void)(s); \ } while (false) #include "sanitizer_common/sanitizer_common_syscalls.inc" #include "sanitizer_common/sanitizer_syscalls_netbsd.inc" #if ASAN_INTERCEPT_PTHREAD_CREATE static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) { AsanThread *t = (AsanThread *)arg; SetCurrentThread(t); auto self = GetThreadSelf(); auto args = asanThreadArgRetval().GetArgs(self); t->ThreadStart(GetTid()); # if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \ SANITIZER_SOLARIS __sanitizer_sigset_t sigset; t->GetStartData(sigset); SetSigProcMask(&sigset, nullptr); # endif thread_return_t retval = (*args.routine)(args.arg_retval); asanThreadArgRetval().Finish(self, retval); return retval; } INTERCEPTOR(int, pthread_create, void *thread, void *attr, void *(*start_routine)(void *), void *arg) { EnsureMainThreadIDIsCorrect(); // Strict init-order checking is thread-hostile. if (flags()->strict_init_order) StopInitOrderChecking(); GET_STACK_TRACE_THREAD; bool detached = [attr]() { int d = 0; return attr && !REAL(pthread_attr_getdetachstate)(attr, &d) && IsStateDetached(d); }(); u32 current_tid = GetCurrentTidOrInvalid(); __sanitizer_sigset_t sigset = {}; # if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \ SANITIZER_SOLARIS ScopedBlockSignals block(&sigset); # endif AsanThread *t = AsanThread::Create(sigset, current_tid, &stack, detached); int result; { // Ignore all allocations made by pthread_create: thread stack/TLS may be // stored by pthread for future reuse even after thread destruction, and // the linked list it's stored in doesn't even hold valid pointers to the // objects, the latter are calculated by obscure pointer arithmetic. # if CAN_SANITIZE_LEAKS __lsan::ScopedInterceptorDisabler disabler; # endif asanThreadArgRetval().Create(detached, {start_routine, arg}, [&]() -> uptr { result = REAL(pthread_create)(thread, attr, asan_thread_start, t); return result ? 0 : *(uptr *)(thread); }); } if (result != 0) { // If the thread didn't start delete the AsanThread to avoid leaking it. // Note AsanThreadContexts never get destroyed so the AsanThreadContext // that was just created for the AsanThread is wasted. t->Destroy(); } return result; } INTERCEPTOR(int, pthread_join, void *thread, void **retval) { int result; asanThreadArgRetval().Join((uptr)thread, [&]() { result = REAL(pthread_join)(thread, retval); return !result; }); return result; } INTERCEPTOR(int, pthread_detach, void *thread) { int result; asanThreadArgRetval().Detach((uptr)thread, [&]() { result = REAL(pthread_detach)(thread); return !result; }); return result; } INTERCEPTOR(void, pthread_exit, void *retval) { asanThreadArgRetval().Finish(GetThreadSelf(), retval); REAL(pthread_exit)(retval); } # if ASAN_INTERCEPT_TRYJOIN INTERCEPTOR(int, pthread_tryjoin_np, void *thread, void **ret) { int result; asanThreadArgRetval().Join((uptr)thread, [&]() { result = REAL(pthread_tryjoin_np)(thread, ret); return !result; }); return result; } # endif # if ASAN_INTERCEPT_TIMEDJOIN INTERCEPTOR(int, pthread_timedjoin_np, void *thread, void **ret, const struct timespec *abstime) { int result; asanThreadArgRetval().Join((uptr)thread, [&]() { result = REAL(pthread_timedjoin_np)(thread, ret, abstime); return !result; }); return result; } # endif DEFINE_REAL_PTHREAD_FUNCTIONS #endif // ASAN_INTERCEPT_PTHREAD_CREATE #if ASAN_INTERCEPT_SWAPCONTEXT static void ClearShadowMemoryForContextStack(uptr stack, uptr ssize) { // Only clear if we know the stack. This should be true only for contexts // created with makecontext(). if (!ssize) return; // Align to page size. uptr PageSize = GetPageSizeCached(); uptr bottom = RoundDownTo(stack, PageSize); if (!AddrIsInMem(bottom)) return; ssize += stack - bottom; ssize = RoundUpTo(ssize, PageSize); PoisonShadow(bottom, ssize, 0); } // Since Solaris 10/SPARC, ucp->uc_stack.ss_sp refers to the stack base address // as on other targets. For binary compatibility, the new version uses a // different external name, so we intercept that. # if SANITIZER_SOLARIS && defined(__sparc__) INTERCEPTOR(void, __makecontext_v2, struct ucontext_t *ucp, void (*func)(), int argc, ...) { # else INTERCEPTOR(void, makecontext, struct ucontext_t *ucp, void (*func)(), int argc, ...) { # endif va_list ap; uptr args[64]; // We don't know a better way to forward ... into REAL function. We can // increase args size if neccecary. CHECK_LE(argc, ARRAY_SIZE(args)); internal_memset(args, 0, sizeof(args)); va_start(ap, argc); for (int i = 0; i < argc; ++i) args[i] = va_arg(ap, uptr); va_end(ap); # define ENUMERATE_ARRAY_4(start) \ args[start], args[start + 1], args[start + 2], args[start + 3] # define ENUMERATE_ARRAY_16(start) \ ENUMERATE_ARRAY_4(start), ENUMERATE_ARRAY_4(start + 4), \ ENUMERATE_ARRAY_4(start + 8), ENUMERATE_ARRAY_4(start + 12) # define ENUMERATE_ARRAY_64() \ ENUMERATE_ARRAY_16(0), ENUMERATE_ARRAY_16(16), ENUMERATE_ARRAY_16(32), \ ENUMERATE_ARRAY_16(48) # if SANITIZER_SOLARIS && defined(__sparc__) REAL(__makecontext_v2) # else REAL(makecontext) # endif ((struct ucontext_t *)ucp, func, argc, ENUMERATE_ARRAY_64()); # undef ENUMERATE_ARRAY_4 # undef ENUMERATE_ARRAY_16 # undef ENUMERATE_ARRAY_64 // Sign the stack so we can identify it for unpoisoning. SignContextStack(ucp); } INTERCEPTOR(int, swapcontext, struct ucontext_t *oucp, struct ucontext_t *ucp) { static bool reported_warning = false; if (!reported_warning) { Report("WARNING: ASan doesn't fully support makecontext/swapcontext " "functions and may produce false positives in some cases!\n"); reported_warning = true; } // Clear shadow memory for new context (it may share stack // with current context). uptr stack, ssize; ReadContextStack(ucp, &stack, &ssize); ClearShadowMemoryForContextStack(stack, ssize); # if __has_attribute(__indirect_return__) && \ (defined(__x86_64__) || defined(__i386__)) int (*real_swapcontext)(struct ucontext_t *, struct ucontext_t *) __attribute__((__indirect_return__)) = REAL(swapcontext); int res = real_swapcontext(oucp, ucp); # else int res = REAL(swapcontext)(oucp, ucp); # endif // swapcontext technically does not return, but program may swap context to // "oucp" later, that would look as if swapcontext() returned 0. // We need to clear shadow for ucp once again, as it may be in arbitrary // state. ClearShadowMemoryForContextStack(stack, ssize); return res; } #endif // ASAN_INTERCEPT_SWAPCONTEXT #if SANITIZER_NETBSD #define longjmp __longjmp14 #define siglongjmp __siglongjmp14 #endif INTERCEPTOR(void, longjmp, void *env, int val) { __asan_handle_no_return(); REAL(longjmp)(env, val); } #if ASAN_INTERCEPT__LONGJMP INTERCEPTOR(void, _longjmp, void *env, int val) { __asan_handle_no_return(); REAL(_longjmp)(env, val); } #endif #if ASAN_INTERCEPT___LONGJMP_CHK INTERCEPTOR(void, __longjmp_chk, void *env, int val) { __asan_handle_no_return(); REAL(__longjmp_chk)(env, val); } #endif #if ASAN_INTERCEPT_SIGLONGJMP INTERCEPTOR(void, siglongjmp, void *env, int val) { __asan_handle_no_return(); REAL(siglongjmp)(env, val); } #endif #if ASAN_INTERCEPT___CXA_THROW INTERCEPTOR(void, __cxa_throw, void *a, void *b, void *c) { CHECK(REAL(__cxa_throw)); __asan_handle_no_return(); REAL(__cxa_throw)(a, b, c); } #endif #if ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION INTERCEPTOR(void, __cxa_rethrow_primary_exception, void *a) { CHECK(REAL(__cxa_rethrow_primary_exception)); __asan_handle_no_return(); REAL(__cxa_rethrow_primary_exception)(a); } #endif #if ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION INTERCEPTOR(_Unwind_Reason_Code, _Unwind_RaiseException, _Unwind_Exception *object) { CHECK(REAL(_Unwind_RaiseException)); __asan_handle_no_return(); return REAL(_Unwind_RaiseException)(object); } #endif #if ASAN_INTERCEPT__SJLJ_UNWIND_RAISEEXCEPTION INTERCEPTOR(_Unwind_Reason_Code, _Unwind_SjLj_RaiseException, _Unwind_Exception *object) { CHECK(REAL(_Unwind_SjLj_RaiseException)); __asan_handle_no_return(); return REAL(_Unwind_SjLj_RaiseException)(object); } #endif #if ASAN_INTERCEPT_INDEX # if ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX INTERCEPTOR(char*, index, const char *string, int c) ALIAS(WRAP(strchr)); # else # if SANITIZER_APPLE DECLARE_REAL(char*, index, const char *string, int c) OVERRIDE_FUNCTION(index, strchr); # else DEFINE_REAL(char*, index, const char *string, int c) # endif # endif #endif // ASAN_INTERCEPT_INDEX // For both strcat() and strncat() we need to check the validity of |to| // argument irrespective of the |from| length. INTERCEPTOR(char *, strcat, char *to, const char *from) { void *ctx; ASAN_INTERCEPTOR_ENTER(ctx, strcat); ENSURE_ASAN_INITED(); if (flags()->replace_str) { uptr from_length = internal_strlen(from); ASAN_READ_RANGE(ctx, from, from_length + 1); uptr to_length = internal_strlen(to); ASAN_READ_STRING_OF_LEN(ctx, to, to_length, to_length); ASAN_WRITE_RANGE(ctx, to + to_length, from_length + 1); // If the copying actually happens, the |from| string should not overlap // with the resulting string starting at |to|, which has a length of // to_length + from_length + 1. if (from_length > 0) { CHECK_RANGES_OVERLAP("strcat", to, from_length + to_length + 1, from, from_length + 1); } } return REAL(strcat)(to, from); } INTERCEPTOR(char*, strncat, char *to, const char *from, uptr size) { void *ctx; ASAN_INTERCEPTOR_ENTER(ctx, strncat); ENSURE_ASAN_INITED(); if (flags()->replace_str) { uptr from_length = MaybeRealStrnlen(from, size); uptr copy_length = Min(size, from_length + 1); ASAN_READ_RANGE(ctx, from, copy_length); uptr to_length = internal_strlen(to); ASAN_READ_STRING_OF_LEN(ctx, to, to_length, to_length); ASAN_WRITE_RANGE(ctx, to + to_length, from_length + 1); if (from_length > 0) { CHECK_RANGES_OVERLAP("strncat", to, to_length + copy_length + 1, from, copy_length); } } return REAL(strncat)(to, from, size); } INTERCEPTOR(char *, strcpy, char *to, const char *from) { void *ctx; ASAN_INTERCEPTOR_ENTER(ctx, strcpy); #if SANITIZER_APPLE if (UNLIKELY(!AsanInited())) return REAL(strcpy)(to, from); #endif // strcpy is called from malloc_default_purgeable_zone() // in __asan::ReplaceSystemAlloc() on Mac. if (AsanInitIsRunning()) { return REAL(strcpy)(to, from); } ENSURE_ASAN_INITED(); if (flags()->replace_str) { uptr from_size = internal_strlen(from) + 1; CHECK_RANGES_OVERLAP("strcpy", to, from_size, from, from_size); ASAN_READ_RANGE(ctx, from, from_size); ASAN_WRITE_RANGE(ctx, to, from_size); } return REAL(strcpy)(to, from); } INTERCEPTOR(char*, strdup, const char *s) { void *ctx; ASAN_INTERCEPTOR_ENTER(ctx, strdup); if (UNLIKELY(!AsanInited())) return internal_strdup(s); ENSURE_ASAN_INITED(); uptr length = internal_strlen(s); if (flags()->replace_str) { ASAN_READ_RANGE(ctx, s, length + 1); } GET_STACK_TRACE_MALLOC; void *new_mem = asan_malloc(length + 1, &stack); if (new_mem) { REAL(memcpy)(new_mem, s, length + 1); } return reinterpret_cast(new_mem); } #if ASAN_INTERCEPT___STRDUP INTERCEPTOR(char*, __strdup, const char *s) { void *ctx; ASAN_INTERCEPTOR_ENTER(ctx, strdup); if (UNLIKELY(!AsanInited())) return internal_strdup(s); ENSURE_ASAN_INITED(); uptr length = internal_strlen(s); if (flags()->replace_str) { ASAN_READ_RANGE(ctx, s, length + 1); } GET_STACK_TRACE_MALLOC; void *new_mem = asan_malloc(length + 1, &stack); if (new_mem) { REAL(memcpy)(new_mem, s, length + 1); } return reinterpret_cast(new_mem); } #endif // ASAN_INTERCEPT___STRDUP INTERCEPTOR(char*, strncpy, char *to, const char *from, uptr size) { void *ctx; ASAN_INTERCEPTOR_ENTER(ctx, strncpy); ENSURE_ASAN_INITED(); if (flags()->replace_str) { uptr from_size = Min(size, MaybeRealStrnlen(from, size) + 1); CHECK_RANGES_OVERLAP("strncpy", to, from_size, from, from_size); ASAN_READ_RANGE(ctx, from, from_size); ASAN_WRITE_RANGE(ctx, to, size); } return REAL(strncpy)(to, from, size); } template static ALWAYS_INLINE auto StrtolImpl(void *ctx, Fn real, const char *nptr, char **endptr, int base) -> decltype(real(nullptr, nullptr, 0)) { if (!flags()->replace_str) return real(nptr, endptr, base); char *real_endptr; auto res = real(nptr, &real_endptr, base); StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base); return res; } # define INTERCEPTOR_STRTO_BASE(ret_type, func) \ INTERCEPTOR(ret_type, func, const char *nptr, char **endptr, int base) { \ void *ctx; \ ASAN_INTERCEPTOR_ENTER(ctx, func); \ ENSURE_ASAN_INITED(); \ return StrtolImpl(ctx, REAL(func), nptr, endptr, base); \ } INTERCEPTOR_STRTO_BASE(long, strtol) INTERCEPTOR_STRTO_BASE(long long, strtoll) # if SANITIZER_GLIBC INTERCEPTOR_STRTO_BASE(long, __isoc23_strtol) INTERCEPTOR_STRTO_BASE(long long, __isoc23_strtoll) # endif INTERCEPTOR(int, atoi, const char *nptr) { void *ctx; ASAN_INTERCEPTOR_ENTER(ctx, atoi); #if SANITIZER_APPLE if (UNLIKELY(!AsanInited())) return REAL(atoi)(nptr); # endif ENSURE_ASAN_INITED(); if (!flags()->replace_str) { return REAL(atoi)(nptr); } char *real_endptr; // "man atoi" tells that behavior of atoi(nptr) is the same as // strtol(nptr, 0, 10), i.e. it sets errno to ERANGE if the // parsed integer can't be stored in *long* type (even if it's // different from int). So, we just imitate this behavior. int result = REAL(strtol)(nptr, &real_endptr, 10); FixRealStrtolEndptr(nptr, &real_endptr); ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1); return result; } INTERCEPTOR(long, atol, const char *nptr) { void *ctx; ASAN_INTERCEPTOR_ENTER(ctx, atol); #if SANITIZER_APPLE if (UNLIKELY(!AsanInited())) return REAL(atol)(nptr); # endif ENSURE_ASAN_INITED(); if (!flags()->replace_str) { return REAL(atol)(nptr); } char *real_endptr; long result = REAL(strtol)(nptr, &real_endptr, 10); FixRealStrtolEndptr(nptr, &real_endptr); ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1); return result; } INTERCEPTOR(long long, atoll, const char *nptr) { void *ctx; ASAN_INTERCEPTOR_ENTER(ctx, atoll); ENSURE_ASAN_INITED(); if (!flags()->replace_str) { return REAL(atoll)(nptr); } char *real_endptr; long long result = REAL(strtoll)(nptr, &real_endptr, 10); FixRealStrtolEndptr(nptr, &real_endptr); ASAN_READ_STRING(ctx, nptr, (real_endptr - nptr) + 1); return result; } #if ASAN_INTERCEPT___CXA_ATEXIT || ASAN_INTERCEPT_ATEXIT static void AtCxaAtexit(void *unused) { (void)unused; StopInitOrderChecking(); } #endif #if ASAN_INTERCEPT___CXA_ATEXIT #ifdef SANITIZER_NETBSD DECLARE_REAL(int, atexit, void (*func)()); #endif INTERCEPTOR(int, __cxa_atexit, void (*func)(void *), void *arg, void *dso_handle) { #if SANITIZER_APPLE if (UNLIKELY(!AsanInited())) return REAL(__cxa_atexit)(func, arg, dso_handle); # endif ENSURE_ASAN_INITED(); #if CAN_SANITIZE_LEAKS __lsan::ScopedInterceptorDisabler disabler; #endif int res = REAL(__cxa_atexit)(func, arg, dso_handle); #ifdef SANITIZER_NETBSD REAL(atexit)((void (*)())AtCxaAtexit); #else REAL(__cxa_atexit)(AtCxaAtexit, nullptr, nullptr); #endif return res; } #endif // ASAN_INTERCEPT___CXA_ATEXIT #if ASAN_INTERCEPT_ATEXIT INTERCEPTOR(int, atexit, void (*func)()) { ENSURE_ASAN_INITED(); #if CAN_SANITIZE_LEAKS __lsan::ScopedInterceptorDisabler disabler; #endif #ifdef SANITIZER_NETBSD int res = REAL(atexit)(func); REAL(atexit)((void (*)())AtCxaAtexit); #else // Avoid calling real atexit as it is unreachable on at least on Linux. int res = REAL(__cxa_atexit)((void (*)(void *a))func, nullptr, nullptr); REAL(__cxa_atexit)(AtCxaAtexit, nullptr, nullptr); #endif return res; } #endif #if ASAN_INTERCEPT_PTHREAD_ATFORK extern "C" { extern int _pthread_atfork(void (*prepare)(), void (*parent)(), void (*child)()); }; INTERCEPTOR(int, pthread_atfork, void (*prepare)(), void (*parent)(), void (*child)()) { #if CAN_SANITIZE_LEAKS __lsan::ScopedInterceptorDisabler disabler; #endif // REAL(pthread_atfork) cannot be called due to symbol indirections at least // on NetBSD return _pthread_atfork(prepare, parent, child); } #endif #if ASAN_INTERCEPT_VFORK DEFINE_REAL(int, vfork) DECLARE_EXTERN_INTERCEPTOR_AND_WRAPPER(int, vfork) #endif // ---------------------- InitializeAsanInterceptors ---------------- {{{1 namespace __asan { void InitializeAsanInterceptors() { static bool was_called_once; CHECK(!was_called_once); was_called_once = true; InitializePlatformInterceptors(); InitializeCommonInterceptors(); InitializeSignalInterceptors(); // Intercept str* functions. ASAN_INTERCEPT_FUNC(strcat); ASAN_INTERCEPT_FUNC(strcpy); ASAN_INTERCEPT_FUNC(strncat); ASAN_INTERCEPT_FUNC(strncpy); ASAN_INTERCEPT_FUNC(strdup); #if ASAN_INTERCEPT___STRDUP ASAN_INTERCEPT_FUNC(__strdup); #endif #if ASAN_INTERCEPT_INDEX && ASAN_USE_ALIAS_ATTRIBUTE_FOR_INDEX ASAN_INTERCEPT_FUNC(index); #endif ASAN_INTERCEPT_FUNC(atoi); ASAN_INTERCEPT_FUNC(atol); ASAN_INTERCEPT_FUNC(atoll); ASAN_INTERCEPT_FUNC(strtol); ASAN_INTERCEPT_FUNC(strtoll); # if SANITIZER_GLIBC ASAN_INTERCEPT_FUNC(__isoc23_strtol); ASAN_INTERCEPT_FUNC(__isoc23_strtoll); # endif // Intecept jump-related functions. ASAN_INTERCEPT_FUNC(longjmp); # if ASAN_INTERCEPT_SWAPCONTEXT ASAN_INTERCEPT_FUNC(swapcontext); // See the makecontext interceptor above for an explanation. # if SANITIZER_SOLARIS && defined(__sparc__) ASAN_INTERCEPT_FUNC(__makecontext_v2); # else ASAN_INTERCEPT_FUNC(makecontext); # endif # endif # if ASAN_INTERCEPT__LONGJMP ASAN_INTERCEPT_FUNC(_longjmp); #endif #if ASAN_INTERCEPT___LONGJMP_CHK ASAN_INTERCEPT_FUNC(__longjmp_chk); #endif #if ASAN_INTERCEPT_SIGLONGJMP ASAN_INTERCEPT_FUNC(siglongjmp); #endif // Intercept exception handling functions. #if ASAN_INTERCEPT___CXA_THROW ASAN_INTERCEPT_FUNC(__cxa_throw); #endif #if ASAN_INTERCEPT___CXA_RETHROW_PRIMARY_EXCEPTION ASAN_INTERCEPT_FUNC(__cxa_rethrow_primary_exception); #endif // Indirectly intercept std::rethrow_exception. #if ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION ASAN_INTERCEPT_FUNC(_Unwind_RaiseException); #endif // Indirectly intercept std::rethrow_exception. #if ASAN_INTERCEPT__UNWIND_SJLJ_RAISEEXCEPTION ASAN_INTERCEPT_FUNC(_Unwind_SjLj_RaiseException); #endif // Intercept threading-related functions #if ASAN_INTERCEPT_PTHREAD_CREATE // TODO: this should probably have an unversioned fallback for newer arches? #if defined(ASAN_PTHREAD_CREATE_VERSION) ASAN_INTERCEPT_FUNC_VER(pthread_create, ASAN_PTHREAD_CREATE_VERSION); #else ASAN_INTERCEPT_FUNC(pthread_create); #endif ASAN_INTERCEPT_FUNC(pthread_join); ASAN_INTERCEPT_FUNC(pthread_detach); ASAN_INTERCEPT_FUNC(pthread_exit); # endif # if ASAN_INTERCEPT_TIMEDJOIN ASAN_INTERCEPT_FUNC(pthread_timedjoin_np); #endif #if ASAN_INTERCEPT_TRYJOIN ASAN_INTERCEPT_FUNC(pthread_tryjoin_np); #endif // Intercept atexit function. #if ASAN_INTERCEPT___CXA_ATEXIT ASAN_INTERCEPT_FUNC(__cxa_atexit); #endif #if ASAN_INTERCEPT_ATEXIT ASAN_INTERCEPT_FUNC(atexit); #endif #if ASAN_INTERCEPT_PTHREAD_ATFORK ASAN_INTERCEPT_FUNC(pthread_atfork); #endif #if ASAN_INTERCEPT_VFORK ASAN_INTERCEPT_FUNC(vfork); #endif VReport(1, "AddressSanitizer: libc interceptors initialized\n"); } } // namespace __asan #endif // !SANITIZER_FUCHSIA @ 1.4 log @merge GCC 14.3.0. @ text @d709 3 d723 3 d727 1 d738 4 d745 1 @ 1.3 log @make this actually be GCC 12.3.0's libsanitizer. the libsanitizer we used with GCC 9 and GCC 10 was significantly ahead of the GCC 9 and GCC 10 provided versions. @ text @d15 1 d24 1 d26 2 a90 6 #define ASAN_INTERCEPTOR_ENTER(ctx, func) \ AsanInterceptorContext _ctx = {#func}; \ ctx = (void *)&_ctx; \ (void) ctx; \ #define COMMON_INTERCEPT_FUNCTION(name) ASAN_INTERCEPT_FUNC(name) d99 9 a107 9 #define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \ ASAN_INTERCEPTOR_ENTER(ctx, func); \ do { \ if (asan_init_is_running) \ return REAL(func)(__VA_ARGS__); \ if (SANITIZER_MAC && UNLIKELY(!asan_inited)) \ return REAL(func)(__VA_ARGS__); \ ENSURE_ASAN_INITED(); \ } while (false) d131 50 a180 1 #define COMMON_INTERCEPTOR_ON_DLOPEN(filename, flag) \ d182 2 a183 20 if (flags()->strict_init_order) \ StopInitOrderChecking(); \ CheckNoDeepBind(filename, flag); \ } while (false) #define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit() #define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) #define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() #define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (!asan_inited) #define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) \ if (AsanThread *t = GetCurrentThread()) { \ *begin = t->tls_begin(); \ *end = t->tls_end(); \ } else { \ *begin = *end = 0; \ } #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \ do { \ ASAN_INTERCEPTOR_ENTER(ctx, memmove); \ ASAN_MEMMOVE_IMPL(ctx, to, from, size); \ d186 4 a189 10 #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size) \ do { \ ASAN_INTERCEPTOR_ENTER(ctx, memcpy); \ ASAN_MEMCPY_IMPL(ctx, to, from, size); \ } while (false) #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \ do { \ ASAN_INTERCEPTOR_ENTER(ctx, memset); \ ASAN_MEMSET_IMPL(ctx, block, c, size); \ d197 2 d223 14 a236 1 return t->ThreadStart(GetTid()); d239 2 a240 2 INTERCEPTOR(int, pthread_create, void *thread, void *attr, void *(*start_routine)(void*), void *arg) { d246 5 a250 3 int detached = 0; if (attr) REAL(pthread_attr_getdetachstate)(attr, &detached); d253 8 a260 2 AsanThread *t = AsanThread::Create(start_routine, arg, current_tid, &stack, detached); d268 1 a268 1 #if CAN_SANITIZE_LEAKS d270 5 a274 2 #endif result = REAL(pthread_create)(thread, attr, asan_thread_start, t); d285 21 a305 2 INTERCEPTOR(int, pthread_join, void *t, void **arg) { return real_pthread_join(t, arg); d308 23 d336 4 d342 3 a344 1 uptr bottom = stack & ~(PageSize - 1); d347 45 a391 4 static const uptr kMaxSaneContextStackSize = 1 << 22; // 4 Mb if (AddrIsInMem(bottom) && ssize && ssize <= kMaxSaneContextStackSize) { PoisonShadow(bottom, ssize, 0); } d407 3 a409 2 #if __has_attribute(__indirect_return__) && \ (defined(__x86_64__) || defined(__i386__)) d411 1 a411 2 __attribute__((__indirect_return__)) = REAL(swapcontext); d413 1 a413 1 #else d415 1 a415 1 #endif d493 1 a493 1 ALIAS(WRAPPER_NAME(strchr)); d495 1 a495 1 # if SANITIZER_MAC d549 2 a550 2 #if SANITIZER_MAC if (UNLIKELY(!asan_inited)) d555 1 a555 1 if (asan_init_is_running) { d571 2 a572 1 if (UNLIKELY(!asan_inited)) return internal_strdup(s); d580 3 a582 1 REAL(memcpy)(new_mem, s, length + 1); d590 2 a591 1 if (UNLIKELY(!asan_inited)) return internal_strdup(s); d599 3 a601 1 REAL(memcpy)(new_mem, s, length + 1); d619 6 a624 7 INTERCEPTOR(long, strtol, const char *nptr, char **endptr, int base) { void *ctx; ASAN_INTERCEPTOR_ENTER(ctx, strtol); ENSURE_ASAN_INITED(); if (!flags()->replace_str) { return REAL(strtol)(nptr, endptr, base); } d626 1 a626 1 long result = REAL(strtol)(nptr, &real_endptr, base); d628 1 a628 1 return result; d631 16 d650 4 a653 3 #if SANITIZER_MAC if (UNLIKELY(!asan_inited)) return REAL(atoi)(nptr); #endif d672 4 a675 3 #if SANITIZER_MAC if (UNLIKELY(!asan_inited)) return REAL(atol)(nptr); #endif a686 14 #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL INTERCEPTOR(long long, strtoll, const char *nptr, char **endptr, int base) { void *ctx; ASAN_INTERCEPTOR_ENTER(ctx, strtoll); ENSURE_ASAN_INITED(); if (!flags()->replace_str) { return REAL(strtoll)(nptr, endptr, base); } char *real_endptr; long long result = REAL(strtoll)(nptr, &real_endptr, base); StrtolFixAndCheck(ctx, nptr, endptr, real_endptr, base); return result; } a699 1 #endif // ASAN_INTERCEPT_ATOLL_AND_STRTOLL d711 4 a714 3 #if SANITIZER_MAC if (UNLIKELY(!asan_inited)) return REAL(__cxa_atexit)(func, arg, dso_handle); #endif d766 1 d785 1 a786 2 #if ASAN_INTERCEPT_ATOLL_AND_STRTOLL ASAN_INTERCEPT_FUNC(atoll); d788 4 a791 1 #endif d796 1 a796 1 #if ASAN_INTERCEPT_SWAPCONTEXT d798 8 a805 2 #endif #if ASAN_INTERCEPT__LONGJMP d824 1 a824 1 INTERCEPT_FUNCTION(_Unwind_RaiseException); d828 1 a828 1 INTERCEPT_FUNCTION(_Unwind_SjLj_RaiseException); d840 10 a868 2 InitializePlatformInterceptors(); @ 1.2 log @revert sanitizer back to the version we were using with GCC 9, since that one was already newer than the GCC 10 version. @ text @d26 1 a26 1 // There is no general interception at all on Fuchsia and RTEMS. d29 1 a29 1 #if !SANITIZER_FUCHSIA && !SANITIZER_RTEMS d31 3 a33 3 #if SANITIZER_POSIX #include "sanitizer_common/sanitizer_posix.h" #endif d35 4 a38 4 #if ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION || \ ASAN_INTERCEPT__SJLJ_UNWIND_RAISEEXCEPTION #include #endif d40 5 a44 5 #if defined(__i386) && SANITIZER_LINUX #define ASAN_PTHREAD_CREATE_VERSION "GLIBC_2.1" #elif defined(__mips__) && SANITIZER_LINUX #define ASAN_PTHREAD_CREATE_VERSION "GLIBC_2.2" #endif d52 2 a53 2 #define ASAN_READ_STRING(ctx, s, n) \ ASAN_READ_STRING_OF_LEN((ctx), (s), REAL(strlen)(s), (n)) d93 1 a93 1 #define COMMON_INTERCEPT_FUNCTION_VER(name, ver) \ d95 2 a193 5 struct ThreadStartParam { atomic_uintptr_t t; atomic_uintptr_t is_registered; }; d196 1 a196 5 ThreadStartParam *param = reinterpret_cast(arg); AsanThread *t = nullptr; while ((t = reinterpret_cast( atomic_load(¶m->t, memory_order_acquire))) == nullptr) internal_sched_yield(); d198 1 a198 1 return t->ThreadStart(GetTid(), ¶m->is_registered); d211 5 a215 3 ThreadStartParam param; atomic_store(¶m.t, 0, memory_order_relaxed); atomic_store(¶m.is_registered, 0, memory_order_relaxed); d225 1 a225 1 result = REAL(pthread_create)(thread, attr, asan_thread_start, ¶m); d227 5 a231 13 if (result == 0) { u32 current_tid = GetCurrentTidOrInvalid(); AsanThread *t = AsanThread::Create(start_routine, arg, current_tid, &stack, detached); atomic_store(¶m.t, reinterpret_cast(t), memory_order_release); // Wait until the AsanThread object is initialized and the ThreadRegistry // entry is in "started" state. One reason for this is that after this // interceptor exits, the child thread's stack may be the only thing holding // the |arg| pointer. This may cause LSan to report a leak if leak checking // happens at a point when the interceptor has already exited, but the stack // range for the child thread is not yet known. while (atomic_load(¶m.is_registered, memory_order_acquire) == 0) internal_sched_yield(); d373 1 a373 1 uptr from_length = REAL(strlen)(from); d375 1 a375 1 uptr to_length = REAL(strlen)(to); d397 1 a397 1 uptr to_length = REAL(strlen)(to); d422 1 a422 1 uptr from_size = REAL(strlen)(from) + 1; d435 1 a435 1 uptr length = REAL(strlen)(s); d451 1 a451 1 uptr length = REAL(strlen)(s); d584 1 a584 1 // Avoid calling real atexit as it is unrechable on at least on Linux. d677 1 @ 1.1 log @Initial revision @ text @@ 1.1.1.1 log @initial import of GCC 10.3.0. main changes include: caveats: - ABI issue between c++14 and c++17 fixed - profile mode is removed from libstdc++ - -fno-common is now the default new features: - new flags -fallocation-dce, -fprofile-partial-training, -fprofile-reproducible, -fprofile-prefix-path, and -fanalyzer - many new compile and link time optimisations - enhanced drive optimisations - openacc 2.6 support - openmp 5.0 features - new warnings: -Wstring-compare and -Wzero-length-bounds - extended warnings: -Warray-bounds, -Wformat-overflow, -Wrestrict, -Wreturn-local-addr, -Wstringop-overflow, -Warith-conversion, -Wmismatched-tags, and -Wredundant-tags - some likely C2X features implemented - more C++20 implemented - many new arm & intel CPUs known hundreds of reported bugs are fixed. full list of changes can be found at: https://gcc.gnu.org/gcc-10/changes.html @ text @@ 1.1.1.2 log @initial import of GCC 12.3.0. major changes in GCC 11 included: - The default mode for C++ is now -std=gnu++17 instead of -std=gnu++14. - When building GCC itself, the host compiler must now support C++11, rather than C++98. - Some short options of the gcov tool have been renamed: -i to -j and -j to -H. - ThreadSanitizer improvements. - Introduce Hardware-assisted AddressSanitizer support. - For targets that produce DWARF debugging information GCC now defaults to DWARF version 5. This can produce up to 25% more compact debug information compared to earlier versions. - Many optimisations. - The existing malloc attribute has been extended so that it can be used to identify allocator/deallocator API pairs. A pair of new -Wmismatched-dealloc and -Wmismatched-new-delete warnings are added. - Other new warnings: -Wsizeof-array-div, enabled by -Wall, warns about divisions of two sizeof operators when the first one is applied to an array and the divisor does not equal the size of the array element. -Wstringop-overread, enabled by default, warns about calls to string functions reading past the end of the arrays passed to them as arguments. -Wtsan, enabled by default, warns about unsupported features in ThreadSanitizer (currently std::atomic_thread_fence). - Enchanced warnings: -Wfree-nonheap-object detects many more instances of calls to deallocation functions with pointers not returned from a dynamic memory allocation function. -Wmaybe-uninitialized diagnoses passing pointers or references to uninitialized memory to functions taking const-qualified arguments. -Wuninitialized detects reads from uninitialized dynamically allocated memory. -Warray-parameter warns about functions with inconsistent array forms. -Wvla-parameter warns about functions with inconsistent VLA forms. - Several new features from the upcoming C2X revision of the ISO C standard are supported with -std=c2x and -std=gnu2x. - Several C++20 features have been implemented. - The C++ front end has experimental support for some of the upcoming C++23 draft. - Several new C++ warnings. - Enhanced Arm, AArch64, x86, and RISC-V CPU support. - The implementation of how program state is tracked within -fanalyzer has been completely rewritten with many enhancements. see https://gcc.gnu.org/gcc-11/changes.html for a full list. major changes in GCC 12 include: - An ABI incompatibility between C and C++ when passing or returning by value certain aggregates containing zero width bit-fields has been discovered on various targets. x86-64, ARM and AArch64 will always ignore them (so there is a C ABI incompatibility between GCC 11 and earlier with GCC 12 or later), PowerPC64 ELFv2 always take them into account (so there is a C++ ABI incompatibility, GCC 4.4 and earlier compatible with GCC 12 or later, incompatible with GCC 4.5 through GCC 11). RISC-V has changed the handling of these already starting with GCC 10. As the ABI requires, MIPS takes them into account handling function return values so there is a C++ ABI incompatibility with GCC 4.5 through 11. - STABS: Support for emitting the STABS debugging format is deprecated and will be removed in the next release. All ports now default to emit DWARF (version 2 or later) debugging info or are obsoleted. - Vectorization is enabled at -O2 which is now equivalent to the original -O2 -ftree-vectorize -fvect-cost-model=very-cheap. - GCC now supports the ShadowCallStack sanitizer. - Support for __builtin_shufflevector compatible with the clang language extension was added. - Support for attribute unavailable was added. - Support for __builtin_dynamic_object_size compatible with the clang language extension was added. - New warnings: -Wbidi-chars warns about potentially misleading UTF-8 bidirectional control characters. -Warray-compare warns about comparisons between two operands of array type. - Some new features from the upcoming C2X revision of the ISO C standard are supported with -std=c2x and -std=gnu2x. - Several C++23 features have been implemented. - Many C++ enhancements across warnings and -f options. see https://gcc.gnu.org/gcc-12/changes.html for a full list. @ text @d26 1 a26 1 // There is no general interception at all on Fuchsia. d29 1 a29 1 #if !SANITIZER_FUCHSIA d31 3 a33 3 # if SANITIZER_POSIX # include "sanitizer_common/sanitizer_posix.h" # endif d35 4 a38 4 # if ASAN_INTERCEPT__UNWIND_RAISEEXCEPTION || \ ASAN_INTERCEPT__SJLJ_UNWIND_RAISEEXCEPTION # include # endif d40 5 a44 5 # if defined(__i386) && SANITIZER_LINUX # define ASAN_PTHREAD_CREATE_VERSION "GLIBC_2.1" # elif defined(__mips__) && SANITIZER_LINUX # define ASAN_PTHREAD_CREATE_VERSION "GLIBC_2.2" # endif d52 2 a53 2 # define ASAN_READ_STRING(ctx, s, n) \ ASAN_READ_STRING_OF_LEN((ctx), (s), internal_strlen(s), (n)) d93 1 a93 1 #define COMMON_INTERCEPT_FUNCTION_VER(name, ver) \ a94 2 #define COMMON_INTERCEPT_FUNCTION_VER_UNVERSIONED_FALLBACK(name, ver) \ ASAN_INTERCEPT_FUNC_VER_UNVERSIONED_FALLBACK(name, ver) d192 5 d199 5 a203 1 AsanThread *t = (AsanThread *)arg; d205 1 a205 1 return t->ThreadStart(GetTid()); d218 3 a220 5 u32 current_tid = GetCurrentTidOrInvalid(); AsanThread *t = AsanThread::Create(start_routine, arg, current_tid, &stack, detached); d230 1 a230 1 result = REAL(pthread_create)(thread, attr, asan_thread_start, t); d232 13 a244 5 if (result != 0) { // If the thread didn't start delete the AsanThread to avoid leaking it. // Note AsanThreadContexts never get destroyed so the AsanThreadContext // that was just created for the AsanThread is wasted. t->Destroy(); d386 1 a386 1 uptr from_length = internal_strlen(from); d388 1 a388 1 uptr to_length = internal_strlen(to); d410 1 a410 1 uptr to_length = internal_strlen(to); d435 1 a435 1 uptr from_size = internal_strlen(from) + 1; d448 1 a448 1 uptr length = internal_strlen(s); d464 1 a464 1 uptr length = internal_strlen(s); d597 1 a597 1 // Avoid calling real atexit as it is unreachable on at least on Linux. a689 1 // TODO: this should probably have an unversioned fallback for newer arches? @ 1.1.1.3 log @initial import of GCC 14.3.0. major changes in GCC 13: - improved sanitizer - zstd debug info compression - LTO improvements - SARIF based diagnostic support - new warnings: -Wxor-used-as-pow, -Wenum-int-mismatch, -Wself-move, -Wdangling-reference - many new -Wanalyzer* specific warnings - enhanced warnings: -Wpessimizing-move, -Wredundant-move - new attributes to mark file descriptors, c++23 "assume" - several C23 features added - several C++23 features added - many new features for Arm, x86, RISC-V major changes in GCC 14: - more strict C99 or newer support - ia64* marked deprecated (but seemingly still in GCC 15.) - several new hardening features - support for "hardbool", which can have user supplied values of true/false - explicit support for stack scrubbing upon function exit - better auto-vectorisation support - added clang-compatible __has_feature and __has_extension - more C23, including -std=c23 - several C++26 features added - better diagnostics in C++ templates - new warnings: -Wnrvo, Welaborated-enum-base - many new features for Arm, x86, RISC-V - possible ABI breaking change for SPARC64 and small structures with arrays of floats. @ text @a14 1 a22 1 #include "asan_thread.h" a23 2 #include "sanitizer_common/sanitizer_errno.h" #include "sanitizer_common/sanitizer_internal_defs.h" d87 6 d101 9 a109 9 # define COMMON_INTERCEPTOR_ENTER(ctx, func, ...) \ ASAN_INTERCEPTOR_ENTER(ctx, func); \ do { \ if (AsanInitIsRunning()) \ return REAL(func)(__VA_ARGS__); \ if (SANITIZER_APPLE && UNLIKELY(!AsanInited())) \ return REAL(func)(__VA_ARGS__); \ ENSURE_ASAN_INITED(); \ } while (false) d133 23 a155 18 # define COMMON_INTERCEPTOR_DLOPEN(filename, flag) \ ({ \ if (flags()->strict_init_order) \ StopInitOrderChecking(); \ CheckNoDeepBind(filename, flag); \ REAL(dlopen)(filename, flag); \ }) # define COMMON_INTERCEPTOR_ON_EXIT(ctx) OnExit() # define COMMON_INTERCEPTOR_LIBRARY_LOADED(filename, handle) # define COMMON_INTERCEPTOR_LIBRARY_UNLOADED() # define COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED (!AsanInited()) # define COMMON_INTERCEPTOR_GET_TLS_RANGE(begin, end) \ if (AsanThread *t = GetCurrentThread()) { \ *begin = t->tls_begin(); \ *end = t->tls_end(); \ } else { \ *begin = *end = 0; \ } d157 4 a160 34 template static void* mmap_interceptor(Mmap real_mmap, void *addr, SIZE_T length, int prot, int flags, int fd, OFF64_T offset) { void *res = real_mmap(addr, length, prot, flags, fd, offset); if (length && res != (void *)-1) { const uptr beg = reinterpret_cast(res); DCHECK(IsAligned(beg, GetPageSize())); SIZE_T rounded_length = RoundUpTo(length, GetPageSize()); // Only unpoison shadow if it's an ASAN managed address. if (AddrIsInMem(beg) && AddrIsInMem(beg + rounded_length - 1)) PoisonShadow(beg, RoundUpTo(length, GetPageSize()), 0); } return res; } template static int munmap_interceptor(Munmap real_munmap, void *addr, SIZE_T length) { // We should not tag if munmap fail, but it's to late to tag after // real_munmap, as the pages could be mmaped by another thread. const uptr beg = reinterpret_cast(addr); if (length && IsAligned(beg, GetPageSize())) { SIZE_T rounded_length = RoundUpTo(length, GetPageSize()); // Protect from unmapping the shadow. if (AddrIsInMem(beg) && AddrIsInMem(beg + rounded_length - 1)) PoisonShadow(beg, rounded_length, 0); } return real_munmap(addr, length); } # define COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, length, prot, flags, \ fd, offset) \ do { \ (void)(ctx); \ return mmap_interceptor(REAL(mmap), addr, sz, prot, flags, fd, off); \ d163 4 a166 4 # define COMMON_INTERCEPTOR_MUNMAP_IMPL(ctx, addr, length) \ do { \ (void)(ctx); \ return munmap_interceptor(REAL(munmap), addr, sz); \ a173 2 #define SIGNAL_INTERCEPTOR_ENTER() ENSURE_ASAN_INITED() d198 1 a198 14 auto self = GetThreadSelf(); auto args = asanThreadArgRetval().GetArgs(self); t->ThreadStart(GetTid()); # if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \ SANITIZER_SOLARIS __sanitizer_sigset_t sigset; t->GetStartData(sigset); SetSigProcMask(&sigset, nullptr); # endif thread_return_t retval = (*args.routine)(args.arg_retval); asanThreadArgRetval().Finish(self, retval); return retval; d201 2 a202 2 INTERCEPTOR(int, pthread_create, void *thread, void *attr, void *(*start_routine)(void *), void *arg) { d208 3 a210 5 bool detached = [attr]() { int d = 0; return attr && !REAL(pthread_attr_getdetachstate)(attr, &d) && IsStateDetached(d); }(); d213 2 a214 8 __sanitizer_sigset_t sigset = {}; # if SANITIZER_FREEBSD || SANITIZER_LINUX || SANITIZER_NETBSD || \ SANITIZER_SOLARIS ScopedBlockSignals block(&sigset); # endif AsanThread *t = AsanThread::Create(sigset, current_tid, &stack, detached); d222 1 a222 1 # if CAN_SANITIZE_LEAKS d224 2 a225 5 # endif asanThreadArgRetval().Create(detached, {start_routine, arg}, [&]() -> uptr { result = REAL(pthread_create)(thread, attr, asan_thread_start, t); return result ? 0 : *(uptr *)(thread); }); d236 2 a237 21 INTERCEPTOR(int, pthread_join, void *thread, void **retval) { int result; asanThreadArgRetval().Join((uptr)thread, [&]() { result = REAL(pthread_join)(thread, retval); return !result; }); return result; } INTERCEPTOR(int, pthread_detach, void *thread) { int result; asanThreadArgRetval().Detach((uptr)thread, [&]() { result = REAL(pthread_detach)(thread); return !result; }); return result; } INTERCEPTOR(void, pthread_exit, void *retval) { asanThreadArgRetval().Finish(GetThreadSelf(), retval); REAL(pthread_exit)(retval); a239 23 # if ASAN_INTERCEPT_TRYJOIN INTERCEPTOR(int, pthread_tryjoin_np, void *thread, void **ret) { int result; asanThreadArgRetval().Join((uptr)thread, [&]() { result = REAL(pthread_tryjoin_np)(thread, ret); return !result; }); return result; } # endif # if ASAN_INTERCEPT_TIMEDJOIN INTERCEPTOR(int, pthread_timedjoin_np, void *thread, void **ret, const struct timespec *abstime) { int result; asanThreadArgRetval().Join((uptr)thread, [&]() { result = REAL(pthread_timedjoin_np)(thread, ret, abstime); return !result; }); return result; } # endif a244 4 // Only clear if we know the stack. This should be true only for contexts // created with makecontext(). if (!ssize) return; d247 1 a247 3 uptr bottom = RoundDownTo(stack, PageSize); if (!AddrIsInMem(bottom)) return; d250 4 a253 45 PoisonShadow(bottom, ssize, 0); } // Since Solaris 10/SPARC, ucp->uc_stack.ss_sp refers to the stack base address // as on other targets. For binary compatibility, the new version uses a // different external name, so we intercept that. # if SANITIZER_SOLARIS && defined(__sparc__) INTERCEPTOR(void, __makecontext_v2, struct ucontext_t *ucp, void (*func)(), int argc, ...) { # else INTERCEPTOR(void, makecontext, struct ucontext_t *ucp, void (*func)(), int argc, ...) { # endif va_list ap; uptr args[64]; // We don't know a better way to forward ... into REAL function. We can // increase args size if neccecary. CHECK_LE(argc, ARRAY_SIZE(args)); internal_memset(args, 0, sizeof(args)); va_start(ap, argc); for (int i = 0; i < argc; ++i) args[i] = va_arg(ap, uptr); va_end(ap); # define ENUMERATE_ARRAY_4(start) \ args[start], args[start + 1], args[start + 2], args[start + 3] # define ENUMERATE_ARRAY_16(start) \ ENUMERATE_ARRAY_4(start), ENUMERATE_ARRAY_4(start + 4), \ ENUMERATE_ARRAY_4(start + 8), ENUMERATE_ARRAY_4(start + 12) # define ENUMERATE_ARRAY_64() \ ENUMERATE_ARRAY_16(0), ENUMERATE_ARRAY_16(16), ENUMERATE_ARRAY_16(32), \ ENUMERATE_ARRAY_16(48) # if SANITIZER_SOLARIS && defined(__sparc__) REAL(__makecontext_v2) # else REAL(makecontext) # endif ((struct ucontext_t *)ucp, func, argc, ENUMERATE_ARRAY_64()); # undef ENUMERATE_ARRAY_4 # undef ENUMERATE_ARRAY_16 # undef ENUMERATE_ARRAY_64 // Sign the stack so we can identify it for unpoisoning. SignContextStack(ucp); d269 2 a270 3 # if __has_attribute(__indirect_return__) && \ (defined(__x86_64__) || defined(__i386__)) d272 2 a273 1 __attribute__((__indirect_return__)) = REAL(swapcontext); d275 1 a275 1 # else d277 1 a277 1 # endif d355 1 a355 1 ALIAS(WRAP(strchr)); d357 1 a357 1 # if SANITIZER_APPLE d411 2 a412 2 #if SANITIZER_APPLE if (UNLIKELY(!AsanInited())) d417 1 a417 1 if (AsanInitIsRunning()) { d433 1 a433 2 if (UNLIKELY(!AsanInited())) return internal_strdup(s); d441 1 a441 3 if (new_mem) { REAL(memcpy)(new_mem, s, length + 1); } d449 1 a449 2 if (UNLIKELY(!AsanInited())) return internal_strdup(s); d457 1 a457 3 if (new_mem) { REAL(memcpy)(new_mem, s, length + 1); } d475 7 a481 6 template static ALWAYS_INLINE auto StrtolImpl(void *ctx, Fn real, const char *nptr, char **endptr, int base) -> decltype(real(nullptr, nullptr, 0)) { if (!flags()->replace_str) return real(nptr, endptr, base); d483 1 a483 1 auto res = real(nptr, &real_endptr, base); d485 1 a485 1 return res; a487 16 # define INTERCEPTOR_STRTO_BASE(ret_type, func) \ INTERCEPTOR(ret_type, func, const char *nptr, char **endptr, int base) { \ void *ctx; \ ASAN_INTERCEPTOR_ENTER(ctx, func); \ ENSURE_ASAN_INITED(); \ return StrtolImpl(ctx, REAL(func), nptr, endptr, base); \ } INTERCEPTOR_STRTO_BASE(long, strtol) INTERCEPTOR_STRTO_BASE(long long, strtoll) # if SANITIZER_GLIBC INTERCEPTOR_STRTO_BASE(long, __isoc23_strtol) INTERCEPTOR_STRTO_BASE(long long, __isoc23_strtoll) # endif d491 3 a493 4 #if SANITIZER_APPLE if (UNLIKELY(!AsanInited())) return REAL(atoi)(nptr); # endif d512 3 a514 4 #if SANITIZER_APPLE if (UNLIKELY(!AsanInited())) return REAL(atol)(nptr); # endif d526 14 d553 1 d565 3 a567 4 #if SANITIZER_APPLE if (UNLIKELY(!AsanInited())) return REAL(__cxa_atexit)(func, arg, dso_handle); # endif a618 1 InitializePlatformInterceptors(); d637 2 a639 1 ASAN_INTERCEPT_FUNC(strtol); d641 1 a641 4 # if SANITIZER_GLIBC ASAN_INTERCEPT_FUNC(__isoc23_strtol); ASAN_INTERCEPT_FUNC(__isoc23_strtoll); # endif d646 1 a646 1 # if ASAN_INTERCEPT_SWAPCONTEXT d648 2 a649 8 // See the makecontext interceptor above for an explanation. # if SANITIZER_SOLARIS && defined(__sparc__) ASAN_INTERCEPT_FUNC(__makecontext_v2); # else ASAN_INTERCEPT_FUNC(makecontext); # endif # endif # if ASAN_INTERCEPT__LONGJMP d668 1 a668 1 ASAN_INTERCEPT_FUNC(_Unwind_RaiseException); d672 1 a672 1 ASAN_INTERCEPT_FUNC(_Unwind_SjLj_RaiseException); a683 10 ASAN_INTERCEPT_FUNC(pthread_detach); ASAN_INTERCEPT_FUNC(pthread_exit); # endif # if ASAN_INTERCEPT_TIMEDJOIN ASAN_INTERCEPT_FUNC(pthread_timedjoin_np); #endif #if ASAN_INTERCEPT_TRYJOIN ASAN_INTERCEPT_FUNC(pthread_tryjoin_np); d703 2 @