115 lines · c
1//===-- asan_interceptors_memintrinsics.h -----------------------*- C++ -*-===//2//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-exception6//7//===---------------------------------------------------------------------===//8//9// This file is a part of AddressSanitizer, an address sanity checker.10//11// ASan-private header for asan_interceptors_memintrinsics.cpp12//===---------------------------------------------------------------------===//13#ifndef ASAN_MEMINTRIN_H14#define ASAN_MEMINTRIN_H15 16#include "asan_interface_internal.h"17#include "asan_internal.h"18#include "asan_mapping.h"19#include "interception/interception.h"20 21DECLARE_REAL(void *, memcpy, void *to, const void *from, SIZE_T size)22DECLARE_REAL(void *, memset, void *block, int c, SIZE_T size)23DECLARE_REAL(void *, memmove, void *to, const void *from, SIZE_T size)24 25namespace __asan {26 27// Return true if we can quickly decide that the region is unpoisoned.28// We assume that a redzone is at least 16 bytes.29static inline bool QuickCheckForUnpoisonedRegion(uptr beg, uptr size) {30 if (UNLIKELY(size == 0 || size > sizeof(uptr) * ASAN_SHADOW_GRANULARITY))31 return !size;32 33 uptr last = beg + size - 1;34 uptr shadow_first = MEM_TO_SHADOW(beg);35 uptr shadow_last = MEM_TO_SHADOW(last);36 uptr uptr_first = RoundDownTo(shadow_first, sizeof(uptr));37 uptr uptr_last = RoundDownTo(shadow_last, sizeof(uptr));38 if (LIKELY(((*reinterpret_cast<const uptr *>(uptr_first) |39 *reinterpret_cast<const uptr *>(uptr_last)) == 0)))40 return true;41 u8 shadow = AddressIsPoisoned(last);42 for (; shadow_first < shadow_last; ++shadow_first)43 shadow |= *((u8 *)shadow_first);44 return !shadow;45}46 47struct AsanInterceptorContext {48 const char *interceptor_name;49};50 51// We implement ACCESS_MEMORY_RANGE, ASAN_READ_RANGE,52// and ASAN_WRITE_RANGE as macro instead of function so53// that no extra frames are created, and stack trace contains54// relevant information only.55// We check all shadow bytes.56#define ACCESS_MEMORY_RANGE(ctx, offset, size, isWrite) \57 do { \58 uptr __offset = (uptr)(offset); \59 uptr __size = (uptr)(size); \60 uptr __bad = 0; \61 if (UNLIKELY(__offset > __offset + __size)) { \62 GET_STACK_TRACE_FATAL_HERE; \63 ReportStringFunctionSizeOverflow(__offset, __size, &stack); \64 } \65 if (UNLIKELY(!QuickCheckForUnpoisonedRegion(__offset, __size)) && \66 (__bad = __asan_region_is_poisoned(__offset, __size))) { \67 AsanInterceptorContext *_ctx = (AsanInterceptorContext *)ctx; \68 bool suppressed = false; \69 if (_ctx) { \70 suppressed = IsInterceptorSuppressed(_ctx->interceptor_name); \71 if (!suppressed && HaveStackTraceBasedSuppressions()) { \72 GET_STACK_TRACE_FATAL_HERE; \73 suppressed = IsStackTraceSuppressed(&stack); \74 } \75 } \76 if (!suppressed) { \77 GET_CURRENT_PC_BP_SP; \78 ReportGenericError(pc, bp, sp, __bad, isWrite, __size, 0, false); \79 } \80 } \81 } while (0)82 83#define ASAN_READ_RANGE(ctx, offset, size) \84 ACCESS_MEMORY_RANGE(ctx, offset, size, false)85#define ASAN_WRITE_RANGE(ctx, offset, size) \86 ACCESS_MEMORY_RANGE(ctx, offset, size, true)87 88// Behavior of functions like "memcpy" or "strcpy" is undefined89// if memory intervals overlap. We report error in this case.90// Macro is used to avoid creation of new frames.91static inline bool RangesOverlap(const char *offset1, uptr length1,92 const char *offset2, uptr length2) {93 return !((offset1 + length1 <= offset2) || (offset2 + length2 <= offset1));94}95#define CHECK_RANGES_OVERLAP(name, _offset1, length1, _offset2, length2) \96 do { \97 const char *offset1 = (const char *)_offset1; \98 const char *offset2 = (const char *)_offset2; \99 if (UNLIKELY(RangesOverlap(offset1, length1, offset2, length2))) { \100 GET_STACK_TRACE_FATAL_HERE; \101 bool suppressed = IsInterceptorSuppressed(name); \102 if (!suppressed && HaveStackTraceBasedSuppressions()) { \103 suppressed = IsStackTraceSuppressed(&stack); \104 } \105 if (!suppressed) { \106 ReportStringFunctionMemoryRangesOverlap(name, offset1, length1, \107 offset2, length2, &stack); \108 } \109 } \110 } while (0)111 112} // namespace __asan113 114#endif // ASAN_MEMINTRIN_H115