1 //===-- asan_interceptors_memintrinsics.cpp -------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===---------------------------------------------------------------------===//
9 // This file is a part of AddressSanitizer, an address sanity checker.
11 // ASan versions of memcpy, memmove, and memset.
12 //===---------------------------------------------------------------------===//
14 #define SANITIZER_COMMON_NO_REDEFINE_BUILTINS
16 #include "asan_interceptors_memintrinsics.h"
18 #include "asan_interceptors.h"
19 #include "asan_report.h"
20 #include "asan_stack.h"
21 #include "asan_suppressions.h"
23 using namespace __asan
;
25 // memcpy is called during __asan_init() from the internals of printf(...).
26 // We do not treat memcpy with to==from as a bug.
27 // See http://llvm.org/bugs/show_bug.cgi?id=11763.
28 #define ASAN_MEMCPY_IMPL(ctx, to, from, size) \
30 if (LIKELY(replace_intrin_cached)) { \
31 if (LIKELY(to != from)) { \
32 CHECK_RANGES_OVERLAP("memcpy", to, size, from, size); \
34 ASAN_READ_RANGE(ctx, from, size); \
35 ASAN_WRITE_RANGE(ctx, to, size); \
36 } else if (UNLIKELY(!AsanInited())) { \
37 return internal_memcpy(to, from, size); \
39 return REAL(memcpy)(to, from, size); \
42 // memset is called inside Printf.
43 #define ASAN_MEMSET_IMPL(ctx, block, c, size) \
45 if (LIKELY(replace_intrin_cached)) { \
46 ASAN_WRITE_RANGE(ctx, block, size); \
47 } else if (UNLIKELY(!AsanInited())) { \
48 return internal_memset(block, c, size); \
50 return REAL(memset)(block, c, size); \
53 #define ASAN_MEMMOVE_IMPL(ctx, to, from, size) \
55 if (LIKELY(replace_intrin_cached)) { \
56 ASAN_READ_RANGE(ctx, from, size); \
57 ASAN_WRITE_RANGE(ctx, to, size); \
59 return internal_memmove(to, from, size); \
62 void *__asan_memcpy(void *to
, const void *from
, uptr size
) {
63 ASAN_MEMCPY_IMPL(nullptr, to
, from
, size
);
66 void *__asan_memset(void *block
, int c
, uptr size
) {
67 ASAN_MEMSET_IMPL(nullptr, block
, c
, size
);
70 void *__asan_memmove(void *to
, const void *from
, uptr size
) {
71 ASAN_MEMMOVE_IMPL(nullptr, to
, from
, size
);
76 // Fuchsia doesn't use sanitizer_common_interceptors.inc, but
77 // the only things there it wants are these three. Just define them
78 // as aliases here rather than repeating the contents.
80 extern "C" decltype(__asan_memcpy
) memcpy
[[gnu::alias("__asan_memcpy")]];
81 extern "C" decltype(__asan_memmove
) memmove
[[gnu::alias("__asan_memmove")]];
82 extern "C" decltype(__asan_memset
) memset
[[gnu::alias("__asan_memset")]];
84 #else // SANITIZER_FUCHSIA
86 #define COMMON_INTERCEPTOR_MEMMOVE_IMPL(ctx, to, from, size) \
88 ASAN_INTERCEPTOR_ENTER(ctx, memmove); \
89 ASAN_MEMMOVE_IMPL(ctx, to, from, size); \
92 #define COMMON_INTERCEPTOR_MEMCPY_IMPL(ctx, to, from, size) \
94 ASAN_INTERCEPTOR_ENTER(ctx, memcpy); \
95 ASAN_MEMCPY_IMPL(ctx, to, from, size); \
98 #define COMMON_INTERCEPTOR_MEMSET_IMPL(ctx, block, c, size) \
100 ASAN_INTERCEPTOR_ENTER(ctx, memset); \
101 ASAN_MEMSET_IMPL(ctx, block, c, size); \
104 #include "sanitizer_common/sanitizer_common_interceptors_memintrinsics.inc"
106 #endif // SANITIZER_FUCHSIA