1*ff6d591cSmrg //===-- asan_premap_shadow.cpp --------------------------------------------===// 2*ff6d591cSmrg // 3*ff6d591cSmrg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*ff6d591cSmrg // See https://llvm.org/LICENSE.txt for license information. 5*ff6d591cSmrg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*ff6d591cSmrg // 7*ff6d591cSmrg //===----------------------------------------------------------------------===// 8*ff6d591cSmrg // 9*ff6d591cSmrg // This file is a part of AddressSanitizer, an address sanity checker. 10*ff6d591cSmrg // 11*ff6d591cSmrg // Reserve shadow memory with an ifunc resolver. 12*ff6d591cSmrg //===----------------------------------------------------------------------===// 13*ff6d591cSmrg 14*ff6d591cSmrg #include "asan_mapping.h" 15*ff6d591cSmrg 16*ff6d591cSmrg #if ASAN_PREMAP_SHADOW 17*ff6d591cSmrg 18*ff6d591cSmrg #include "asan_premap_shadow.h" 19*ff6d591cSmrg #include "sanitizer_common/sanitizer_posix.h" 20*ff6d591cSmrg 21*ff6d591cSmrg namespace __asan { 22*ff6d591cSmrg 23*ff6d591cSmrg // The code in this file needs to run in an unrelocated binary. It may not 24*ff6d591cSmrg // access any external symbol, including its own non-hidden globals. 25*ff6d591cSmrg 26*ff6d591cSmrg // Conservative upper limit. PremapShadowSize()27*ff6d591cSmrguptr PremapShadowSize() { 28*ff6d591cSmrg uptr granularity = GetMmapGranularity(); 29*ff6d591cSmrg return RoundUpTo(GetMaxVirtualAddress() >> SHADOW_SCALE, granularity); 30*ff6d591cSmrg } 31*ff6d591cSmrg 32*ff6d591cSmrg // Returns an address aligned to 8 pages, such that one page on the left and 33*ff6d591cSmrg // PremapShadowSize() bytes on the right of it are mapped r/o. PremapShadow()34*ff6d591cSmrguptr PremapShadow() { 35*ff6d591cSmrg return MapDynamicShadow(PremapShadowSize(), /*mmap_alignment_scale*/ 3, 36*ff6d591cSmrg /*min_shadow_base_alignment*/ 0, kHighMemEnd); 37*ff6d591cSmrg } 38*ff6d591cSmrg PremapShadowFailed()39*ff6d591cSmrgbool PremapShadowFailed() { 40*ff6d591cSmrg uptr shadow = reinterpret_cast<uptr>(&__asan_shadow); 41*ff6d591cSmrg uptr resolver = reinterpret_cast<uptr>(&__asan_premap_shadow); 42*ff6d591cSmrg // shadow == resolver is how Android KitKat and older handles ifunc. 43*ff6d591cSmrg // shadow == 0 just in case. 44*ff6d591cSmrg if (shadow == 0 || shadow == resolver) 45*ff6d591cSmrg return true; 46*ff6d591cSmrg return false; 47*ff6d591cSmrg } 48*ff6d591cSmrg } // namespace __asan 49*ff6d591cSmrg 50*ff6d591cSmrg extern "C" { __asan_premap_shadow()51*ff6d591cSmrgdecltype(__asan_shadow)* __asan_premap_shadow() { 52*ff6d591cSmrg // The resolver may be called multiple times. Map the shadow just once. 53*ff6d591cSmrg static uptr premapped_shadow = 0; 54*ff6d591cSmrg if (!premapped_shadow) premapped_shadow = __asan::PremapShadow(); 55*ff6d591cSmrg return reinterpret_cast<decltype(__asan_shadow)*>(premapped_shadow); 56*ff6d591cSmrg } 57*ff6d591cSmrg 58*ff6d591cSmrg // __asan_shadow is a "function" that has the same address as the first byte of 59*ff6d591cSmrg // the shadow mapping. 60*ff6d591cSmrg INTERFACE_ATTRIBUTE __attribute__((ifunc("__asan_premap_shadow"))) void 61*ff6d591cSmrg __asan_shadow(); 62*ff6d591cSmrg } 63*ff6d591cSmrg 64*ff6d591cSmrg #endif // ASAN_PREMAP_SHADOW 65