If the address range is not covered by shadow memory, make interceptors like mmap fail earlier. --------- Signed-off-by: Abhishek Varma <abhvarma@amd.com> Signed-off-by: Minsoo Choo <minsoochoo0122@proton.me> Signed-off-by: hanhanW <hanhan0912@gmail.com> Signed-off-by: Nikita B <n2h9z4@gmail.com> Signed-off-by: Nick Sarnie <nick.sarnie@intel.com> Signed-off-by: Ian Wood <ianwood@u.northwestern.edu> Co-authored-by: Min-Yih Hsu <min.hsu@sifive.com> Co-authored-by: Sersawy <65075626+Abdelrhmansersawy@users.noreply.github.com> Co-authored-by: Krzysztof Parzyszek <Krzysztof.Parzyszek@amd.com> Co-authored-by: Walter Lee <49250218+googlewalt@users.noreply.github.com> Co-authored-by: cpist (He / Him) <tinyfrog12@gmail.com> Co-authored-by: Jonathan Cohen <joncoh@apple.com> Co-authored-by: Charitha Saumya <136391709+charithaintc@users.noreply.github.com> Co-authored-by: Mircea Trofin <mtrofin@google.com> Co-authored-by: Mingming Liu <mingmingl@google.com> Co-authored-by: Atharv Mane <atharvamane19@gmail.com> Co-authored-by: Sam Clegg <sbc@chromium.org> Co-authored-by: vporpo <vasileios.porpodas@amd.com> Co-authored-by: Usama Hameed <u_hameed@apple.com> Co-authored-by: PiJoules <6019989+PiJoules@users.noreply.github.com> Co-authored-by: Muhammad Bassiouni <60100307+bassiounix@users.noreply.github.com> Co-authored-by: Roland McGrath <mcgrathr@google.com> Co-authored-by: Jakub Kuderski <jakub@nod-labs.com> Co-authored-by: LLVM GN Syncbot <llvmgnsyncbot@gmail.com> Co-authored-by: Krzysztof Drewniak <Krzysztof.Drewniak@amd.com> Co-authored-by: Aiden Grossman <aidengrossman@google.com> Co-authored-by: Kelvin Li <kli@ca.ibm.com> Co-authored-by: Andrei Elovikov <a.elovikov@gmail.com> Co-authored-by: Craig Topper <craig.topper@sifive.com> Co-authored-by: NAKAMURA Takumi <geek4civic@gmail.com> Co-authored-by: Marco Elver <elver@google.com> Co-authored-by: Greg Clayton <gclayton@fb.com> Co-authored-by: Charles Zablit <c_zablit@apple.com> Co-authored-by: Zachary Yedidia <zyedidia@gmail.com> Co-authored-by: Michael Spencer <bigcheesegs@gmail.com> Co-authored-by: Florian Hahn <flo@fhahn.com> Co-authored-by: Wenju He <wenju.he@intel.com> Co-authored-by: Andy Kaylor <akaylor@nvidia.com> Co-authored-by: Luohao Wang <luohaothu@live.com> Co-authored-by: John Harrison <harjohn@google.com> Co-authored-by: Ebuka Ezike <yerimyah1@gmail.com> Co-authored-by: Jonas Devlieghere <jonas@devlieghere.com> Co-authored-by: Alexey Bataev <a.bataev@outlook.com> Co-authored-by: Rahul Joshi <rjoshi@nvidia.com> Co-authored-by: Jessica Clarke <jrtc27@jrtc27.com> Co-authored-by: puneeth_aditya_5656 <myakampuneeth@gmail.com> Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com> Co-authored-by: Joseph Huber <huberjn@outlook.com> Co-authored-by: Saina Daneshmand <66389676+SainaDaneshmandjahromi@users.noreply.github.com> Co-authored-by: Jameson Nash <vtjnash@gmail.com> Co-authored-by: Fangrui Song <i@maskray.me> Co-authored-by: Tom Stellard <tstellar@redhat.com> Co-authored-by: Jie Fu <jiefu@tencent.com> Co-authored-by: Timm Baeder <tbaeder@redhat.com> Co-authored-by: Pranav Kant <prka@google.com> Co-authored-by: woruyu <1214539920@qq.com> Co-authored-by: Pradeep Kumar <pradeepku@nvidia.com> Co-authored-by: Petr Hosek <phosek@google.com> Co-authored-by: bala-bhargav <penugondabalabharghav@gmail.com> Co-authored-by: Jhalak Patel <jhalakp@nvidia.com> Co-authored-by: Utkarsh Saxena <usx@google.com> Co-authored-by: Carl Ritson <carl.ritson@amd.com> Co-authored-by: Nikolas Klauser <nikolasklauser@berlin.de> Co-authored-by: Jay Foad <jay.foad@amd.com> Co-authored-by: Sudharsan Veeravalli <svs@qti.qualcomm.com> Co-authored-by: David Spickett <david.spickett@arm.com> Co-authored-by: Ricardo Jesus <rjj@nvidia.com> Co-authored-by: David Sherwood <david.sherwood@arm.com> Co-authored-by: Oleksandr Tarasiuk <oleksandr.tarasiuk@outlook.com> Co-authored-by: Yingying Wang <3171290993@qq.com> Co-authored-by: Sergio Afonso <safonsof@amd.com> Co-authored-by: Miloš Poletanović <poletanovicmilos97@gmail.com> Co-authored-by: Milos Poletanovic <mpoletanovic@syrmia.com> Co-authored-by: Abhishek Varma <avarma094@gmail.com> Co-authored-by: Henry Linjamäki <henry.linjamaki@tuni.fi> Co-authored-by: Henry Linjamäki <henry.mikael.linjamaki@intel.com> Co-authored-by: Leandro Lupori <leandro.lupori@linaro.org> Co-authored-by: Kerry McLaughlin <kerry.mclaughlin@arm.com> Co-authored-by: Nikita Popov <npopov@redhat.com> Co-authored-by: Michael Kruse <llvm-project@meinersbur.de> Co-authored-by: Konrad Kleine <kkleine@redhat.com> Co-authored-by: EugeneZelenko <eugene.zelenko@gmail.com> Co-authored-by: Frederik Harwath <frederik.harwath@amd.com> Co-authored-by: Fateme Hosseini <quic_fhossein@quicinc.com> Co-authored-by: Simon Pilgrim <llvm-dev@redking.me.uk> Co-authored-by: Michael Buch <michaelbuch12@gmail.com> Co-authored-by: Minsoo Choo <minsoochoo0122@proton.me> Co-authored-by: Ryan Cowan <ryan.cowan@arm.com> Co-authored-by: Matt Arsenault <arsenm2@gmail.com> Co-authored-by: Akash Banerjee <akash.banerjee@amd.com> Co-authored-by: Dark Steve <Prasoon.Mishra@amd.com> Co-authored-by: Aviral Goel <aviralg@users.noreply.github.com> Co-authored-by: Osman Yasar <37121924+osmanyasar05@users.noreply.github.com> Co-authored-by: Igor Wodiany <igor.wodiany@imgtec.com> Co-authored-by: Florian Mayer <fmayer@google.com> Co-authored-by: AidinT <at.aidin@gmail.com> Co-authored-by: Justin Fargnoli <jfargnoli@nvidia.com> Co-authored-by: Christopher Di Bella <cjdb@google.com> Co-authored-by: Han-Chung Wang <hanhan0912@gmail.com> Co-authored-by: Wei Xiao <wei3.xiao@intel.com> Co-authored-by: vangthao95 <vang.thao@amd.com> Co-authored-by: joaosaffran <joaosaffranllvm@gmail.com> Co-authored-by: Razvan Lupusoru <razvan.lupusoru@gmail.com> Co-authored-by: Aman LaChapelle <aman.lachapelle@gmail.com> Co-authored-by: Jacob Lalonde <jalalonde@fb.com> Co-authored-by: n2h9 <13541181+n2h9@users.noreply.github.com> Co-authored-by: Joao Saffran <jderezende@microsoft.com> Co-authored-by: Joshua Batista <jbatista@microsoft.com> Co-authored-by: Balázs Benics <benicsbalazs@gmail.com> Co-authored-by: Priyanshu Kumar <10b.priyanshu@gmail.com> Co-authored-by: Osman Yasar <osmanyas05@gmail.com> Co-authored-by: Sarah Kuhn <sarahlinhkuhn@gmail.com> Co-authored-by: Luisa Cicolini <48860705+luisacicolini@users.noreply.github.com> Co-authored-by: Osama Abdelkader <osama.abdelkader@gmail.com> Co-authored-by: Andrew Haberlandt <ahaberlandt@apple.com> Co-authored-by: Rohan Jacob-Rao <rohanjr@google.com> Co-authored-by: Eugene Epshteyn <eepshteyn@nvidia.com> Co-authored-by: Ian Anderson <iana@apple.com> Co-authored-by: adams381 <adams@nvidia.com> Co-authored-by: Nick Sarnie <nick.sarnie@intel.com> Co-authored-by: Sadaf Ebrahimi <sadafebrahimi@google.com> Co-authored-by: RattataKing <46631728+RattataKing@users.noreply.github.com> Co-authored-by: Chibuoyim (Wilson) Ogbonna <ogbonnachibuoyim12@gmail.com> Co-authored-by: Ian Wood <ianwood2024@u.northwestern.edu> Co-authored-by: Andres-Salamanca <andrealebarbaritos@gmail.com> Co-authored-by: Jason Molenda <jmolenda@apple.com> Co-authored-by: A. Jiang <de34@live.cn> Co-authored-by: Maksim Panchenko <maks@fb.com> Co-authored-by: Peter Collingbourne <peter@pcc.me.uk> Co-authored-by: Prabhu Rajasekaran <prabhukr@google.com> Co-authored-by: Dmitry Sidorov <18708689+MrSidims@users.noreply.github.com> Co-authored-by: Lang Hames <lhames@gmail.com> Co-authored-by: Daniil Fukalov <dfukalov@gmail.com> Co-authored-by: paperchalice <liujunchang97@outlook.com> Co-authored-by: Viktoria Maximova <viktoria.maksimova@intel.com> Co-authored-by: wanglei <wanglei@loongson.cn> Co-authored-by: hev <wangrui@loongson.cn> Co-authored-by: Zhihui Yang <youngwisdm@gmail.com> Co-authored-by: Ash <97464181+tnuha@users.noreply.github.com> Co-authored-by: YunQiang Su <syq@debian.org> Co-authored-by: Victor Chernyakin <chernyakin.victor.j@outlook.com> Co-authored-by: Thurston Dang <thurston@google.com> Co-authored-by: Kewen Meng <Kewen.Meng@amd.com> Co-authored-by: sohail <150878950+sohail103@users.noreply.github.com> Co-authored-by: Louis Dionne <ldionne.2@gmail.com> Co-authored-by: azwolski <antoni.zwolski@intel.com> Co-authored-by: Peter Hawkins <phawkins@google.com> Co-authored-by: David Green <david.green@arm.com> Co-authored-by: Sander de Smalen <sander.desmalen@arm.com> Co-authored-by: Riyaz Ahmad <69200125+riyaz86a@users.noreply.github.com> Co-authored-by: Riyaz Ahmad <riyaz.ahmad@ibm.com> Co-authored-by: Honey Goyal <honey.goyal3@ibm.com>
1024 lines
33 KiB
C++
1024 lines
33 KiB
C++
//===-- 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 <unwind.h>
|
|
# 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 (static_cast<bool>(REAL(strnlen)))
|
|
return REAL(strnlen)(s, maxlen);
|
|
# endif
|
|
return internal_strnlen(s, maxlen);
|
|
}
|
|
|
|
static inline uptr MaybeRealWcsnlen(const wchar_t* s, uptr maxlen) {
|
|
# if SANITIZER_INTERCEPT_WCSNLEN
|
|
if (static_cast<bool>(REAL(wcsnlen)))
|
|
return REAL(wcsnlen)(s, maxlen);
|
|
# endif
|
|
return internal_wcsnlen(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;
|
|
}
|
|
|
|
# if SANITIZER_POSIX
|
|
static inline bool RangeOverlaps(uptr beg, uptr end_excl, uptr seg_beg,
|
|
uptr seg_end_incl) {
|
|
if (!seg_beg && !seg_end_incl)
|
|
return false;
|
|
uptr seg_end_excl = seg_end_incl + 1;
|
|
return beg < seg_end_excl && end_excl > seg_beg;
|
|
}
|
|
|
|
static inline bool IntersectsShadow(uptr beg, uptr end_excl) {
|
|
// Check shadow regions
|
|
if (RangeOverlaps(beg, end_excl, kLowShadowBeg, kLowShadowEnd))
|
|
return true;
|
|
if (kMidShadowBeg &&
|
|
RangeOverlaps(beg, end_excl, kMidShadowBeg, kMidShadowEnd))
|
|
return true;
|
|
if (RangeOverlaps(beg, end_excl, kHighShadowBeg, kHighShadowEnd))
|
|
return true;
|
|
return false;
|
|
}
|
|
# endif // SANITIZER_POSIX
|
|
|
|
} // namespace __asan
|
|
|
|
// ---------------------- Wrappers ---------------- {{{1
|
|
using namespace __asan;
|
|
|
|
DECLARE_REAL_AND_INTERCEPTOR(void*, malloc, usize)
|
|
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 constexpr (SANITIZER_APPLE) { \
|
|
if (UNLIKELY(!AsanInited())) \
|
|
return REAL(func)(__VA_ARGS__); \
|
|
} else { \
|
|
if (!TryAsanInitFromRtl()) \
|
|
return REAL(func)(__VA_ARGS__); \
|
|
} \
|
|
} 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 <class Mmap>
|
|
static void* mmap_interceptor(Mmap real_mmap, void* addr, SIZE_T length,
|
|
int prot, int flags, int fd, OFF64_T offset) {
|
|
# if SANITIZER_POSIX
|
|
if (length == 0)
|
|
return real_mmap(addr, length, prot, flags, fd, offset);
|
|
const uptr start = reinterpret_cast<uptr>(addr);
|
|
uptr end_excl;
|
|
if (UNLIKELY(__builtin_add_overflow(start, static_cast<uptr>(length),
|
|
&end_excl))) {
|
|
errno = errno_EINVAL;
|
|
return (void*)-1;
|
|
}
|
|
if (flags & map_fixed) {
|
|
// TODO: shadow gap may need to be checked
|
|
if (__asan::IntersectsShadow(start, end_excl)) {
|
|
errno = errno_EINVAL;
|
|
return (void*)-1;
|
|
}
|
|
}
|
|
# endif // SANITIZER_POSIX
|
|
|
|
void* res = real_mmap(addr, length, prot, flags, fd, offset);
|
|
if (length && res != (void*)-1) {
|
|
const uptr beg = reinterpret_cast<uptr>(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 <class Munmap>
|
|
static int munmap_interceptor(Munmap real_munmap, void* addr, SIZE_T length) {
|
|
const uptr start = reinterpret_cast<uptr>(addr);
|
|
|
|
# if SANITIZER_POSIX
|
|
if (length == 0)
|
|
return real_munmap(addr, length);
|
|
|
|
uptr end_excl;
|
|
if (UNLIKELY(__builtin_add_overflow(start, static_cast<uptr>(length),
|
|
&end_excl))) {
|
|
errno = errno_EINVAL;
|
|
return -1;
|
|
}
|
|
// TODO: shadow gap may need to be checked
|
|
if (__asan::IntersectsShadow(start, end_excl)) {
|
|
errno = errno_EINVAL;
|
|
return -1;
|
|
}
|
|
# endif // SANITIZER_POSIX
|
|
|
|
// 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.
|
|
if (length && IsAligned(start, GetPageSize())) {
|
|
SIZE_T rounded_length = RoundUpTo(length, GetPageSize());
|
|
// Protect from unmapping the shadow.
|
|
if (AddrIsInMem(start) && AddrIsInMem(start + rounded_length - 1))
|
|
PoisonShadow(start, 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, length, prot, flags, fd, \
|
|
offset); \
|
|
} while (false)
|
|
|
|
# define COMMON_INTERCEPTOR_MUNMAP_IMPL(ctx, addr, length) \
|
|
do { \
|
|
(void)(ctx); \
|
|
return munmap_interceptor(REAL(munmap), addr, length); \
|
|
} while (false)
|
|
|
|
# if CAN_SANITIZE_LEAKS
|
|
# define COMMON_INTERCEPTOR_STRERROR() \
|
|
__lsan::ScopedInterceptorDisabler disabler
|
|
# endif
|
|
|
|
# define SIGNAL_INTERCEPTOR_ENTER() \
|
|
do { \
|
|
AsanInitFromRtl(); \
|
|
} while (false)
|
|
|
|
# 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);
|
|
// AIX pthread_t is unsigned int.
|
|
# if SANITIZER_AIX
|
|
return result ? 0 : *(unsigned*)(thread);
|
|
# else
|
|
return result ? 0 : *(uptr*)(thread);
|
|
# endif
|
|
});
|
|
}
|
|
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_INTERNAL_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 necessary.
|
|
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
|
|
|
|
# if ASAN_INTERCEPT_LONGJMP
|
|
INTERCEPTOR(void, longjmp, void* env, int val) {
|
|
__asan_handle_no_return();
|
|
REAL(longjmp)(env, val);
|
|
}
|
|
# endif
|
|
|
|
# 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);
|
|
AsanInitFromRtl();
|
|
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, usize size) {
|
|
void* ctx;
|
|
ASAN_INTERCEPTOR_ENTER(ctx, strncat);
|
|
AsanInitFromRtl();
|
|
if (flags()->replace_str) {
|
|
uptr from_length = MaybeRealStrnlen(from, size);
|
|
uptr copy_length = Min<uptr>(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 constexpr (SANITIZER_APPLE) {
|
|
// strcpy is called from malloc_default_purgeable_zone()
|
|
// in __asan::ReplaceSystemAlloc() on Mac.
|
|
if (UNLIKELY(!AsanInited()))
|
|
return REAL(strcpy)(to, from);
|
|
} else {
|
|
if (!TryAsanInitFromRtl())
|
|
return REAL(strcpy)(to, from);
|
|
}
|
|
|
|
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(wchar_t*, wcscpy, wchar_t* to, const wchar_t* from) {
|
|
void* ctx;
|
|
ASAN_INTERCEPTOR_ENTER(ctx, wcscpy);
|
|
if (!TryAsanInitFromRtl())
|
|
return REAL(wcscpy)(to, from);
|
|
if (flags()->replace_str) {
|
|
uptr size = (internal_wcslen(from) + 1) * sizeof(wchar_t);
|
|
CHECK_RANGES_OVERLAP("wcscpy", to, size, from, size);
|
|
ASAN_READ_RANGE(ctx, from, size);
|
|
ASAN_WRITE_RANGE(ctx, to, size);
|
|
}
|
|
return REAL(wcscpy)(to, from);
|
|
}
|
|
|
|
// Windows doesn't always define the strdup identifier,
|
|
// and when it does it's a macro defined to either _strdup
|
|
// or _strdup_dbg, _strdup_dbg ends up calling _strdup, so
|
|
// we want to intercept that. push/pop_macro are used to avoid problems
|
|
// if this file ends up including <string.h> in the future.
|
|
# if SANITIZER_WINDOWS
|
|
# pragma push_macro("strdup")
|
|
# undef strdup
|
|
# define strdup _strdup
|
|
# endif
|
|
|
|
INTERCEPTOR(char*, strdup, const char* s) {
|
|
void* ctx;
|
|
ASAN_INTERCEPTOR_ENTER(ctx, strdup);
|
|
// Allowing null input is Windows-specific
|
|
if (SANITIZER_WINDOWS && UNLIKELY(!s))
|
|
return nullptr;
|
|
if (UNLIKELY(!TryAsanInitFromRtl()))
|
|
return internal_strdup(s);
|
|
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<char*>(new_mem);
|
|
}
|
|
|
|
# if ASAN_INTERCEPT___STRDUP
|
|
INTERCEPTOR(char*, __strdup, const char* s) {
|
|
void* ctx;
|
|
ASAN_INTERCEPTOR_ENTER(ctx, strdup);
|
|
if (UNLIKELY(!TryAsanInitFromRtl()))
|
|
return internal_strdup(s);
|
|
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<char*>(new_mem);
|
|
}
|
|
# endif // ASAN_INTERCEPT___STRDUP
|
|
|
|
INTERCEPTOR(char*, strncpy, char* to, const char* from, usize size) {
|
|
void* ctx;
|
|
ASAN_INTERCEPTOR_ENTER(ctx, strncpy);
|
|
AsanInitFromRtl();
|
|
if (flags()->replace_str) {
|
|
uptr from_size = Min<uptr>(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);
|
|
}
|
|
|
|
INTERCEPTOR(wchar_t*, wcsncpy, wchar_t* to, const wchar_t* from, uptr size) {
|
|
void* ctx;
|
|
ASAN_INTERCEPTOR_ENTER(ctx, wcsncpy);
|
|
AsanInitFromRtl();
|
|
if (flags()->replace_str) {
|
|
uptr from_size =
|
|
Min(size, MaybeRealWcsnlen(from, size) + 1) * sizeof(wchar_t);
|
|
CHECK_RANGES_OVERLAP("wcsncpy", to, from_size, from, from_size);
|
|
ASAN_READ_RANGE(ctx, from, from_size);
|
|
ASAN_WRITE_RANGE(ctx, to, size * sizeof(wchar_t));
|
|
}
|
|
return REAL(wcsncpy)(to, from, size);
|
|
}
|
|
|
|
template <typename Fn>
|
|
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); \
|
|
AsanInitFromRtl(); \
|
|
return StrtolImpl(ctx, REAL(func), nptr, endptr, base); \
|
|
}
|
|
|
|
INTERCEPTOR_STRTO_BASE(long long, strtoll)
|
|
|
|
# if SANITIZER_WINDOWS
|
|
INTERCEPTOR(long, strtol, const char* nptr, char** endptr, int base) {
|
|
// REAL(strtol) may be ntdll!strtol, which doesn't set errno. Instead,
|
|
// call REAL(strtoll) and do the range check ourselves.
|
|
COMPILER_CHECK(sizeof(long) == sizeof(u32));
|
|
|
|
void* ctx;
|
|
ASAN_INTERCEPTOR_ENTER(ctx, strtol);
|
|
AsanInitFromRtl();
|
|
|
|
long long result = StrtolImpl(ctx, REAL(strtoll), nptr, endptr, base);
|
|
|
|
if (result > INT32_MAX) {
|
|
errno = errno_ERANGE;
|
|
return INT32_MAX;
|
|
}
|
|
if (result < INT32_MIN) {
|
|
errno = errno_ERANGE;
|
|
return INT32_MIN;
|
|
}
|
|
return (long)result;
|
|
}
|
|
# else
|
|
INTERCEPTOR_STRTO_BASE(long, strtol)
|
|
# endif
|
|
|
|
# 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 && UNLIKELY(!AsanInited()))
|
|
return REAL(atoi)(nptr);
|
|
AsanInitFromRtl();
|
|
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 && UNLIKELY(!AsanInited()))
|
|
return REAL(atol)(nptr);
|
|
AsanInitFromRtl();
|
|
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);
|
|
AsanInitFromRtl();
|
|
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
|
|
INTERCEPTOR(int, __cxa_atexit, void (*func)(void*), void* arg,
|
|
void* dso_handle) {
|
|
if (SANITIZER_APPLE && UNLIKELY(!AsanInited()))
|
|
return REAL(__cxa_atexit)(func, arg, dso_handle);
|
|
AsanInitFromRtl();
|
|
# if CAN_SANITIZE_LEAKS
|
|
__lsan::ScopedInterceptorDisabler disabler;
|
|
# endif
|
|
int res = REAL(__cxa_atexit)(func, arg, dso_handle);
|
|
REAL(__cxa_atexit)(AtCxaAtexit, nullptr, nullptr);
|
|
return res;
|
|
}
|
|
# endif // ASAN_INTERCEPT___CXA_ATEXIT
|
|
|
|
# if ASAN_INTERCEPT_ATEXIT
|
|
INTERCEPTOR(int, atexit, void (*func)()) {
|
|
AsanInitFromRtl();
|
|
# if CAN_SANITIZE_LEAKS
|
|
__lsan::ScopedInterceptorDisabler disabler;
|
|
# endif
|
|
// 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);
|
|
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);
|
|
|
|
// Intercept wcs* functions.
|
|
ASAN_INTERCEPT_FUNC(wcscpy);
|
|
ASAN_INTERCEPT_FUNC(wcsncpy);
|
|
|
|
# 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
|
|
|
|
// Intercept jump-related functions.
|
|
# if ASAN_INTERCEPT_LONGJMP
|
|
ASAN_INTERCEPT_FUNC(longjmp);
|
|
# endif
|
|
|
|
# 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");
|
|
}
|
|
|
|
# if SANITIZER_WINDOWS
|
|
# pragma pop_macro("strdup")
|
|
# endif
|
|
|
|
} // namespace __asan
|
|
|
|
#endif // !SANITIZER_FUCHSIA
|