Path: blob/main/contrib/llvm-project/compiler-rt/lib/asan/asan_interceptors_memintrinsics.h
35233 views
//===-- asan_interceptors_memintrinsics.h -----------------------*- C++ -*-===//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-private header for asan_interceptors_memintrinsics.cpp11//===---------------------------------------------------------------------===//12#ifndef ASAN_MEMINTRIN_H13#define ASAN_MEMINTRIN_H1415#include "asan_interface_internal.h"16#include "asan_internal.h"17#include "asan_mapping.h"18#include "interception/interception.h"1920DECLARE_REAL(void *, memcpy, void *to, const void *from, uptr size)21DECLARE_REAL(void *, memset, void *block, int c, uptr size)2223namespace __asan {2425// Return true if we can quickly decide that the region is unpoisoned.26// We assume that a redzone is at least 16 bytes.27static inline bool QuickCheckForUnpoisonedRegion(uptr beg, uptr size) {28if (UNLIKELY(size == 0 || size > sizeof(uptr) * ASAN_SHADOW_GRANULARITY))29return !size;3031uptr last = beg + size - 1;32uptr shadow_first = MEM_TO_SHADOW(beg);33uptr shadow_last = MEM_TO_SHADOW(last);34uptr uptr_first = RoundDownTo(shadow_first, sizeof(uptr));35uptr uptr_last = RoundDownTo(shadow_last, sizeof(uptr));36if (LIKELY(((*reinterpret_cast<const uptr *>(uptr_first) |37*reinterpret_cast<const uptr *>(uptr_last)) == 0)))38return true;39u8 shadow = AddressIsPoisoned(last);40for (; shadow_first < shadow_last; ++shadow_first)41shadow |= *((u8 *)shadow_first);42return !shadow;43}4445struct AsanInterceptorContext {46const char *interceptor_name;47};4849// We implement ACCESS_MEMORY_RANGE, ASAN_READ_RANGE,50// and ASAN_WRITE_RANGE as macro instead of function so51// that no extra frames are created, and stack trace contains52// relevant information only.53// We check all shadow bytes.54#define ACCESS_MEMORY_RANGE(ctx, offset, size, isWrite) \55do { \56uptr __offset = (uptr)(offset); \57uptr __size = (uptr)(size); \58uptr __bad = 0; \59if (UNLIKELY(__offset > __offset + __size)) { \60GET_STACK_TRACE_FATAL_HERE; \61ReportStringFunctionSizeOverflow(__offset, __size, &stack); \62} \63if (UNLIKELY(!QuickCheckForUnpoisonedRegion(__offset, __size)) && \64(__bad = __asan_region_is_poisoned(__offset, __size))) { \65AsanInterceptorContext *_ctx = (AsanInterceptorContext *)ctx; \66bool suppressed = false; \67if (_ctx) { \68suppressed = IsInterceptorSuppressed(_ctx->interceptor_name); \69if (!suppressed && HaveStackTraceBasedSuppressions()) { \70GET_STACK_TRACE_FATAL_HERE; \71suppressed = IsStackTraceSuppressed(&stack); \72} \73} \74if (!suppressed) { \75GET_CURRENT_PC_BP_SP; \76ReportGenericError(pc, bp, sp, __bad, isWrite, __size, 0, false); \77} \78} \79} while (0)8081#define ASAN_READ_RANGE(ctx, offset, size) \82ACCESS_MEMORY_RANGE(ctx, offset, size, false)83#define ASAN_WRITE_RANGE(ctx, offset, size) \84ACCESS_MEMORY_RANGE(ctx, offset, size, true)8586// Behavior of functions like "memcpy" or "strcpy" is undefined87// if memory intervals overlap. We report error in this case.88// Macro is used to avoid creation of new frames.89static inline bool RangesOverlap(const char *offset1, uptr length1,90const char *offset2, uptr length2) {91return !((offset1 + length1 <= offset2) || (offset2 + length2 <= offset1));92}93#define CHECK_RANGES_OVERLAP(name, _offset1, length1, _offset2, length2) \94do { \95const char *offset1 = (const char *)_offset1; \96const char *offset2 = (const char *)_offset2; \97if (UNLIKELY(RangesOverlap(offset1, length1, offset2, length2))) { \98GET_STACK_TRACE_FATAL_HERE; \99bool suppressed = IsInterceptorSuppressed(name); \100if (!suppressed && HaveStackTraceBasedSuppressions()) { \101suppressed = IsStackTraceSuppressed(&stack); \102} \103if (!suppressed) { \104ReportStringFunctionMemoryRangesOverlap(name, offset1, length1, \105offset2, length2, &stack); \106} \107} \108} while (0)109110} // namespace __asan111112#endif // ASAN_MEMINTRIN_H113114115