Path: blob/main/contrib/llvm-project/compiler-rt/lib/asan/asan_premap_shadow.cpp
35233 views
//===-- asan_premap_shadow.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// Reserve shadow memory with an ifunc resolver.11//===----------------------------------------------------------------------===//1213#include "asan_mapping.h"1415#if ASAN_PREMAP_SHADOW1617#include "asan_premap_shadow.h"18#include "sanitizer_common/sanitizer_posix.h"1920namespace __asan {2122// The code in this file needs to run in an unrelocated binary. It may not23// access any external symbol, including its own non-hidden globals.2425// Conservative upper limit.26uptr PremapShadowSize() {27uptr granularity = GetMmapGranularity();28return RoundUpTo(GetMaxVirtualAddress() >> ASAN_SHADOW_SCALE, granularity);29}3031// Returns an address aligned to 8 pages, such that one page on the left and32// PremapShadowSize() bytes on the right of it are mapped r/o.33uptr PremapShadow() {34return MapDynamicShadow(PremapShadowSize(), /*mmap_alignment_scale*/ 3,35/*min_shadow_base_alignment*/ 0, kHighMemEnd,36GetMmapGranularity());37}3839bool PremapShadowFailed() {40uptr shadow = reinterpret_cast<uptr>(&__asan_shadow);41uptr resolver = reinterpret_cast<uptr>(&__asan_premap_shadow);42// shadow == resolver is how Android KitKat and older handles ifunc.43// shadow == 0 just in case.44if (shadow == 0 || shadow == resolver)45return true;46return false;47}48} // namespace __asan4950extern "C" {51decltype(__asan_shadow)* __asan_premap_shadow() {52// The resolver may be called multiple times. Map the shadow just once.53static uptr premapped_shadow = 0;54if (!premapped_shadow) premapped_shadow = __asan::PremapShadow();55return reinterpret_cast<decltype(__asan_shadow)*>(premapped_shadow);56}5758// __asan_shadow is a "function" that has the same address as the first byte of59// the shadow mapping.60INTERFACE_ATTRIBUTE __attribute__((ifunc("__asan_premap_shadow"))) void61__asan_shadow();62}6364#endif // ASAN_PREMAP_SHADOW656667