]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/asan/asan_premap_shadow.cc
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / asan / asan_premap_shadow.cc
1 //===-- asan_premap_shadow.cc ---------------------------------------------===//
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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of AddressSanitizer, an address sanity checker.
10 //
11 // Reserve shadow memory with an ifunc resolver.
12 //===----------------------------------------------------------------------===//
13
14 #include "asan_mapping.h"
15
16 #if ASAN_PREMAP_SHADOW
17
18 #include "asan_premap_shadow.h"
19 #include "sanitizer_common/sanitizer_posix.h"
20
21 namespace __asan {
22
23 // The code in this file needs to run in an unrelocated binary. It may not
24 // access any external symbol, including its own non-hidden globals.
25
26 // Conservative upper limit.
27 uptr PremapShadowSize() {
28   uptr granularity = GetMmapGranularity();
29   return RoundUpTo(GetMaxVirtualAddress() >> SHADOW_SCALE, granularity);
30 }
31
32 // Returns an address aligned to 8 pages, such that one page on the left and
33 // PremapShadowSize() bytes on the right of it are mapped r/o.
34 uptr PremapShadow() {
35   uptr granularity = GetMmapGranularity();
36   uptr alignment = granularity * 8;
37   uptr left_padding = granularity;
38   uptr shadow_size = PremapShadowSize();
39   uptr map_size = shadow_size + left_padding + alignment;
40
41   uptr map_start = (uptr)MmapNoAccess(map_size);
42   CHECK_NE(map_start, ~(uptr)0);
43
44   uptr shadow_start = RoundUpTo(map_start + left_padding, alignment);
45   uptr shadow_end = shadow_start + shadow_size;
46   internal_munmap(reinterpret_cast<void *>(map_start),
47                   shadow_start - left_padding - map_start);
48   internal_munmap(reinterpret_cast<void *>(shadow_end),
49                   map_start + map_size - shadow_end);
50   return shadow_start;
51 }
52
53 bool PremapShadowFailed() {
54   uptr shadow = reinterpret_cast<uptr>(&__asan_shadow);
55   uptr resolver = reinterpret_cast<uptr>(&__asan_premap_shadow);
56   // shadow == resolver is how Android KitKat and older handles ifunc.
57   // shadow == 0 just in case.
58   if (shadow == 0 || shadow == resolver)
59     return true;
60   return false;
61 }
62 } // namespace __asan
63
64 extern "C" {
65 decltype(__asan_shadow)* __asan_premap_shadow() {
66   // The resolver may be called multiple times. Map the shadow just once.
67   static uptr premapped_shadow = 0;
68   if (!premapped_shadow) premapped_shadow = __asan::PremapShadow();
69   return reinterpret_cast<decltype(__asan_shadow)*>(premapped_shadow);
70 }
71
72 // __asan_shadow is a "function" that has the same address as the first byte of
73 // the shadow mapping.
74 INTERFACE_ATTRIBUTE __attribute__((ifunc("__asan_premap_shadow"))) void
75 __asan_shadow();
76 }
77
78 #endif // ASAN_PREMAP_SHADOW