168d75effSDimitry Andric //===-- asan_premap_shadow.cpp --------------------------------------------===//
268d75effSDimitry Andric //
368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
668d75effSDimitry Andric //
768d75effSDimitry Andric //===----------------------------------------------------------------------===//
868d75effSDimitry Andric //
968d75effSDimitry Andric // This file is a part of AddressSanitizer, an address sanity checker.
1068d75effSDimitry Andric //
1168d75effSDimitry Andric // Reserve shadow memory with an ifunc resolver.
1268d75effSDimitry Andric //===----------------------------------------------------------------------===//
1368d75effSDimitry Andric 
1468d75effSDimitry Andric #include "asan_mapping.h"
1568d75effSDimitry Andric 
1668d75effSDimitry Andric #if ASAN_PREMAP_SHADOW
1768d75effSDimitry Andric 
1868d75effSDimitry Andric #include "asan_premap_shadow.h"
1968d75effSDimitry Andric #include "sanitizer_common/sanitizer_posix.h"
2068d75effSDimitry Andric 
2168d75effSDimitry Andric namespace __asan {
2268d75effSDimitry Andric 
2368d75effSDimitry Andric // The code in this file needs to run in an unrelocated binary. It may not
2468d75effSDimitry Andric // access any external symbol, including its own non-hidden globals.
2568d75effSDimitry Andric 
2668d75effSDimitry Andric // Conservative upper limit.
PremapShadowSize()2768d75effSDimitry Andric uptr PremapShadowSize() {
2868d75effSDimitry Andric   uptr granularity = GetMmapGranularity();
29*0eae32dcSDimitry Andric   return RoundUpTo(GetMaxVirtualAddress() >> ASAN_SHADOW_SCALE, granularity);
3068d75effSDimitry Andric }
3168d75effSDimitry Andric 
3268d75effSDimitry Andric // Returns an address aligned to 8 pages, such that one page on the left and
3368d75effSDimitry Andric // PremapShadowSize() bytes on the right of it are mapped r/o.
PremapShadow()3468d75effSDimitry Andric uptr PremapShadow() {
35e8d8bef9SDimitry Andric   return MapDynamicShadow(PremapShadowSize(), /*mmap_alignment_scale*/ 3,
36e8d8bef9SDimitry Andric                           /*min_shadow_base_alignment*/ 0, kHighMemEnd);
3768d75effSDimitry Andric }
3868d75effSDimitry Andric 
PremapShadowFailed()3968d75effSDimitry Andric bool PremapShadowFailed() {
4068d75effSDimitry Andric   uptr shadow = reinterpret_cast<uptr>(&__asan_shadow);
4168d75effSDimitry Andric   uptr resolver = reinterpret_cast<uptr>(&__asan_premap_shadow);
4268d75effSDimitry Andric   // shadow == resolver is how Android KitKat and older handles ifunc.
4368d75effSDimitry Andric   // shadow == 0 just in case.
4468d75effSDimitry Andric   if (shadow == 0 || shadow == resolver)
4568d75effSDimitry Andric     return true;
4668d75effSDimitry Andric   return false;
4768d75effSDimitry Andric }
4868d75effSDimitry Andric } // namespace __asan
4968d75effSDimitry Andric 
5068d75effSDimitry Andric extern "C" {
__asan_premap_shadow()5168d75effSDimitry Andric decltype(__asan_shadow)* __asan_premap_shadow() {
5268d75effSDimitry Andric   // The resolver may be called multiple times. Map the shadow just once.
5368d75effSDimitry Andric   static uptr premapped_shadow = 0;
5468d75effSDimitry Andric   if (!premapped_shadow) premapped_shadow = __asan::PremapShadow();
5568d75effSDimitry Andric   return reinterpret_cast<decltype(__asan_shadow)*>(premapped_shadow);
5668d75effSDimitry Andric }
5768d75effSDimitry Andric 
5868d75effSDimitry Andric // __asan_shadow is a "function" that has the same address as the first byte of
5968d75effSDimitry Andric // the shadow mapping.
6068d75effSDimitry Andric INTERFACE_ATTRIBUTE __attribute__((ifunc("__asan_premap_shadow"))) void
6168d75effSDimitry Andric __asan_shadow();
6268d75effSDimitry Andric }
6368d75effSDimitry Andric 
6468d75effSDimitry Andric #endif // ASAN_PREMAP_SHADOW
65