1 //===-- asan_interceptors_memintrinsics.h -----------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===---------------------------------------------------------------------===//
8 // This file is a part of AddressSanitizer, an address sanity checker.
10 // ASan-private header for asan_memintrin.cc
11 //===---------------------------------------------------------------------===//
12 #ifndef ASAN_MEMINTRIN_H
13 #define ASAN_MEMINTRIN_H
15 #include "asan_interface_internal.h"
16 #include "asan_internal.h"
17 #include "asan_mapping.h"
18 #include "interception/interception.h"
20 DECLARE_REAL(void*, memcpy
, void *to
, const void *from
, uptr size
)
21 DECLARE_REAL(void*, memset
, void *block
, int c
, uptr size
)
25 // Return true if we can quickly decide that the region is unpoisoned.
26 // We assume that a redzone is at least 16 bytes.
27 static inline bool QuickCheckForUnpoisonedRegion(uptr beg
, uptr size
) {
28 if (size
== 0) return true;
30 return !AddressIsPoisoned(beg
) &&
31 !AddressIsPoisoned(beg
+ size
- 1) &&
32 !AddressIsPoisoned(beg
+ size
/ 2);
34 return !AddressIsPoisoned(beg
) &&
35 !AddressIsPoisoned(beg
+ size
/ 4) &&
36 !AddressIsPoisoned(beg
+ size
- 1) &&
37 !AddressIsPoisoned(beg
+ 3 * size
/ 4) &&
38 !AddressIsPoisoned(beg
+ size
/ 2);
42 struct AsanInterceptorContext
{
43 const char *interceptor_name
;
46 // We implement ACCESS_MEMORY_RANGE, ASAN_READ_RANGE,
47 // and ASAN_WRITE_RANGE as macro instead of function so
48 // that no extra frames are created, and stack trace contains
49 // relevant information only.
50 // We check all shadow bytes.
51 #define ACCESS_MEMORY_RANGE(ctx, offset, size, isWrite) do { \
52 uptr __offset = (uptr)(offset); \
53 uptr __size = (uptr)(size); \
55 if (__offset > __offset + __size) { \
56 GET_STACK_TRACE_FATAL_HERE; \
57 ReportStringFunctionSizeOverflow(__offset, __size, &stack); \
59 if (!QuickCheckForUnpoisonedRegion(__offset, __size) && \
60 (__bad = __asan_region_is_poisoned(__offset, __size))) { \
61 AsanInterceptorContext *_ctx = (AsanInterceptorContext *)ctx; \
62 bool suppressed = false; \
64 suppressed = IsInterceptorSuppressed(_ctx->interceptor_name); \
65 if (!suppressed && HaveStackTraceBasedSuppressions()) { \
66 GET_STACK_TRACE_FATAL_HERE; \
67 suppressed = IsStackTraceSuppressed(&stack); \
71 GET_CURRENT_PC_BP_SP; \
72 ReportGenericError(pc, bp, sp, __bad, isWrite, __size, 0, false);\
77 // memcpy is called during __asan_init() from the internals of printf(...).
78 // We do not treat memcpy with to==from as a bug.
79 // See http://llvm.org/bugs/show_bug.cgi?id=11763.
80 #define ASAN_MEMCPY_IMPL(ctx, to, from, size) \
82 if (UNLIKELY(!asan_inited)) return internal_memcpy(to, from, size); \
83 if (asan_init_is_running) { \
84 return REAL(memcpy)(to, from, size); \
86 ENSURE_ASAN_INITED(); \
87 if (flags()->replace_intrin) { \
89 CHECK_RANGES_OVERLAP("memcpy", to, size, from, size); \
91 ASAN_READ_RANGE(ctx, from, size); \
92 ASAN_WRITE_RANGE(ctx, to, size); \
94 return REAL(memcpy)(to, from, size); \
97 // memset is called inside Printf.
98 #define ASAN_MEMSET_IMPL(ctx, block, c, size) \
100 if (UNLIKELY(!asan_inited)) return internal_memset(block, c, size); \
101 if (asan_init_is_running) { \
102 return REAL(memset)(block, c, size); \
104 ENSURE_ASAN_INITED(); \
105 if (flags()->replace_intrin) { \
106 ASAN_WRITE_RANGE(ctx, block, size); \
108 return REAL(memset)(block, c, size); \
111 #define ASAN_MEMMOVE_IMPL(ctx, to, from, size) \
113 if (UNLIKELY(!asan_inited)) return internal_memmove(to, from, size); \
114 ENSURE_ASAN_INITED(); \
115 if (flags()->replace_intrin) { \
116 ASAN_READ_RANGE(ctx, from, size); \
117 ASAN_WRITE_RANGE(ctx, to, size); \
119 return internal_memmove(to, from, size); \
122 #define ASAN_READ_RANGE(ctx, offset, size) \
123 ACCESS_MEMORY_RANGE(ctx, offset, size, false)
124 #define ASAN_WRITE_RANGE(ctx, offset, size) \
125 ACCESS_MEMORY_RANGE(ctx, offset, size, true)
127 // Behavior of functions like "memcpy" or "strcpy" is undefined
128 // if memory intervals overlap. We report error in this case.
129 // Macro is used to avoid creation of new frames.
130 static inline bool RangesOverlap(const char *offset1
, uptr length1
,
131 const char *offset2
, uptr length2
) {
132 return !((offset1
+ length1
<= offset2
) || (offset2
+ length2
<= offset1
));
134 #define CHECK_RANGES_OVERLAP(name, _offset1, length1, _offset2, length2) do { \
135 const char *offset1 = (const char*)_offset1; \
136 const char *offset2 = (const char*)_offset2; \
137 if (RangesOverlap(offset1, length1, offset2, length2)) { \
138 GET_STACK_TRACE_FATAL_HERE; \
139 ReportStringFunctionMemoryRangesOverlap(name, offset1, length1, \
140 offset2, length2, &stack); \
144 } // namespace __asan
146 #endif // ASAN_MEMINTRIN_H