Path: blob/main/contrib/llvm-project/compiler-rt/lib/asan/asan_interceptors_memintrinsics.cpp
35233 views
//===-- asan_interceptors_memintrinsics.cpp -------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===---------------------------------------------------------------------===//7//8// This file is a part of AddressSanitizer, an address sanity checker.9//10// ASan versions of memcpy, memmove, and memset.11//===---------------------------------------------------------------------===//1213#define SANITIZER_COMMON_NO_REDEFINE_BUILTINS1415#include "asan_interceptors_memintrinsics.h"1617#include "asan_interceptors.h"18#include "asan_report.h"19#include "asan_stack.h"20#include "asan_suppressions.h"2122using namespace __asan;2324// memcpy is called during __asan_init() from the internals of printf(...).25// We do not treat memcpy with to==from as a bug.26// See http://llvm.org/bugs/show_bug.cgi?id=11763.27#define ASAN_MEMCPY_IMPL(ctx, to, from, size) \28do { \29if (LIKELY(replace_intrin_cached)) { \30if (LIKELY(to != from)) { \31CHECK_RANGES_OVERLAP("memcpy", to, size, from, size); \32} \33ASAN_READ_RANGE(ctx, from, size); \34ASAN_WRITE_RANGE(ctx, to, size); \35} else if (UNLIKELY(!AsanInited())) { \36return internal_memcpy(to, from, size); \37} \38return REAL(memcpy)(to, from, size); \39} while (0)4041// memset is called inside Printf.42#define ASAN_MEMSET_IMPL(ctx, block, c, size) \43do { \44if (LIKELY(replace_intrin_cached)) { \45ASAN_WRITE_RANGE(ctx, block, size); \46} else if (UNLIKELY(!AsanInited())) { \47return internal_memset(block, c, size); \48} \49return REAL(memset)(block, c, size); \50} while (0)5152#define ASAN_MEMMOVE_IMPL(ctx, to, from, size) \53do { \54if (LIKELY(replace_intrin_cached)) { \55ASAN_READ_RANGE(ctx, from, size); \56ASAN_WRITE_RANGE(ctx, to, size); \57} \58return internal_memmove(to, from, size); \59} while (0)6061void *__asan_memcpy(void *to, const void *from, uptr size) {62ASAN_MEMCPY_IMPL(nullptr, to, from, size);63}6465void *__asan_memset(void *block, int c, uptr size) {66ASAN_MEMSET_IMPL(nullptr, block, c, size);67}6869void *__asan_memmove(void *to, const void *from, uptr size) {70ASAN_MEMMOVE_IMPL(nullptr, to, from, size);71}7273#if SANITIZER_FUCHSIA7475// Fuchsia doesn't use sanitizer_common_interceptors.inc, but76// the only things there it wants are these three. Just define them77// as aliases here rather than repeating the contents.7879extern "C" decltype(__asan_memcpy) memcpy[[gnu::alias("__asan_memcpy")]];80extern "C" decltype(__asan_memmove) memmove[[gnu::alias("__asan_memmove")]];81extern "C" decltype(__asan_memset) memset[[gnu::alias("__asan_memset")]];8283#else // SANITIZER_FUCHSIA8485#define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \86do { \87ASAN_INTERCEPTOR_ENTER(ctx, memmove); \88ASAN_MEMMOVE_IMPL(ctx, to, from, size); \89} while (false)9091#define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size) \92do { \93ASAN_INTERCEPTOR_ENTER(ctx, memcpy); \94ASAN_MEMCPY_IMPL(ctx, to, from, size); \95} while (false)9697#define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \98do { \99ASAN_INTERCEPTOR_ENTER(ctx, memset); \100ASAN_MEMSET_IMPL(ctx, block, c, size); \101} while (false)102103#include "sanitizer_common/sanitizer_common_interceptors_memintrinsics.inc"104105#endif // SANITIZER_FUCHSIA106107108