1 //===-- hwasan_poisoning.cpp ------------------------------------*- C++ -*-===//
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 HWAddressSanitizer.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "hwasan_poisoning.h"
14
15 #include "hwasan_mapping.h"
16 #include "interception/interception.h"
17 #include "sanitizer_common/sanitizer_common.h"
18 #include "sanitizer_common/sanitizer_linux.h"
19
20 namespace __hwasan {
21
TagMemoryAligned(uptr p,usize size,tag_t tag)22 uptr TagMemoryAligned(uptr p, usize size, tag_t tag) {
23 CHECK(IsAligned(p, kShadowAlignment));
24 CHECK(IsAligned(size, kShadowAlignment));
25 uptr shadow_start = MemToShadow(p);
26 uptr shadow_size = MemToShadowSize(size);
27
28 uptr page_size = GetPageSizeCached();
29 uptr page_start = RoundUpTo(shadow_start, page_size);
30 uptr page_end = RoundDownTo(shadow_start + shadow_size, page_size);
31 uptr threshold = common_flags()->clear_shadow_mmap_threshold;
32 if (SANITIZER_LINUX &&
33 UNLIKELY(page_end >= page_start + threshold && tag == 0)) {
34 internal_memset((void *)shadow_start, tag, page_start - shadow_start);
35 internal_memset((void *)page_end, tag,
36 shadow_start + shadow_size - page_end);
37 // For an anonymous private mapping MADV_DONTNEED will return a zero page on
38 // Linux.
39 ReleaseMemoryPagesToOSAndZeroFill(page_start, page_end);
40 } else {
41 internal_memset((void *)shadow_start, tag, shadow_size);
42 }
43 return AddTagToPointer(p, tag);
44 }
45
TagMemory(uptr p,usize size,tag_t tag)46 uptr TagMemory(uptr p, usize size, tag_t tag) {
47 uptr start = RoundDownTo(p, kShadowAlignment);
48 uptr end = RoundUpTo(p + size, kShadowAlignment);
49 return TagMemoryAligned(start, end - start, tag);
50 }
51
52 } // namespace __hwasan
53