1 //===-- hwasan_dynamic_shadow.cpp -------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file is a part of HWAddressSanitizer. It reserves dynamic shadow memory
11 /// region and handles ifunc resolver case, when necessary.
13 //===----------------------------------------------------------------------===//
15 #include "hwasan_dynamic_shadow.h"
21 #include "hwasan_mapping.h"
22 #include "hwasan_thread_list.h"
23 #include "sanitizer_common/sanitizer_common.h"
24 #include "sanitizer_common/sanitizer_posix.h"
26 // The code in this file needs to run in an unrelocated binary. It should not
27 // access any external symbol, including its own non-hidden globals.
32 INTERFACE_ATTRIBUTE
void __hwasan_shadow();
33 decltype(__hwasan_shadow
)* __hwasan_premap_shadow();
39 // Conservative upper limit.
40 static uptr
PremapShadowSize() {
41 return RoundUpTo(GetMaxVirtualAddress() >> kShadowScale
,
42 GetMmapGranularity());
45 static uptr
PremapShadow() {
46 return MapDynamicShadow(PremapShadowSize(), kShadowScale
,
47 kShadowBaseAlignment
, kHighMemEnd
);
50 static bool IsPremapShadowAvailable() {
51 const uptr shadow
= reinterpret_cast<uptr
>(&__hwasan_shadow
);
52 const uptr resolver
= reinterpret_cast<uptr
>(&__hwasan_premap_shadow
);
53 // shadow == resolver is how Android KitKat and older handles ifunc.
54 // shadow == 0 just in case.
55 return shadow
!= 0 && shadow
!= resolver
;
58 static uptr
FindPremappedShadowStart(uptr shadow_size_bytes
) {
59 const uptr granularity
= GetMmapGranularity();
60 const uptr shadow_start
= reinterpret_cast<uptr
>(&__hwasan_shadow
);
61 const uptr premap_shadow_size
= PremapShadowSize();
62 const uptr shadow_size
= RoundUpTo(shadow_size_bytes
, granularity
);
64 // We may have mapped too much. Release extra memory.
65 UnmapFromTo(shadow_start
+ shadow_size
, shadow_start
+ premap_shadow_size
);
69 } // namespace __hwasan
73 decltype(__hwasan_shadow
)* __hwasan_premap_shadow() {
74 // The resolver might be called multiple times. Map the shadow just once.
75 static __sanitizer::uptr shadow
= 0;
77 shadow
= __hwasan::PremapShadow();
78 return reinterpret_cast<decltype(__hwasan_shadow
)*>(shadow
);
81 // __hwasan_shadow is a "function" that has the same address as the first byte
82 // of the shadow mapping.
83 INTERFACE_ATTRIBUTE
__attribute__((ifunc("__hwasan_premap_shadow")))
84 void __hwasan_shadow();
86 extern __attribute((weak
, visibility("hidden"))) ElfW(Rela
) __rela_iplt_start
[],
93 void InitShadowGOT() {
94 // Call the ifunc resolver for __hwasan_shadow and fill in its GOT entry. This
95 // needs to be done before other ifunc resolvers (which are handled by libc)
96 // because a resolver might read __hwasan_shadow.
97 typedef ElfW(Addr
) (*ifunc_resolver_t
)(void);
98 for (ElfW(Rela
) *r
= __rela_iplt_start
; r
!= __rela_iplt_end
; ++r
) {
99 ElfW(Addr
)* offset
= reinterpret_cast<ElfW(Addr
)*>(r
->r_offset
);
100 ElfW(Addr
) resolver
= r
->r_addend
;
101 if (resolver
== reinterpret_cast<ElfW(Addr
)>(&__hwasan_premap_shadow
)) {
102 *offset
= reinterpret_cast<ifunc_resolver_t
>(resolver
)();
108 uptr
FindDynamicShadowStart(uptr shadow_size_bytes
) {
109 if (IsPremapShadowAvailable())
110 return FindPremappedShadowStart(shadow_size_bytes
);
111 return MapDynamicShadow(shadow_size_bytes
, kShadowScale
, kShadowBaseAlignment
,
115 } // namespace __hwasan
117 #elif SANITIZER_FUCHSIA
121 void InitShadowGOT() {}
123 } // namespace __hwasan
128 void InitShadowGOT() {}
130 uptr
FindDynamicShadowStart(uptr shadow_size_bytes
) {
131 # if defined(HWASAN_ALIASING_MODE)
132 constexpr uptr kAliasSize
= 1ULL << kAddressTagShift
;
133 constexpr uptr kNumAliases
= 1ULL << kTagBits
;
134 return MapDynamicShadowAndAliases(shadow_size_bytes
, kAliasSize
, kNumAliases
,
137 return MapDynamicShadow(shadow_size_bytes
, kShadowScale
, kShadowBaseAlignment
,
141 } // namespace __hwasan
143 #endif // SANITIZER_ANDROID