1//===-- copyprof_shadow.cpp ----------------------------------------------===//
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 implements the shadow memory management for CopyProf, tracking
10/// the copy status and modification state of application memory.
11///
12//===----------------------------------------------------------------------===//
13
14#include "copyprof_shadow.h"
15
16#include "sanitizer_common/sanitizer_internal_defs.h"
17
18namespace __copyprof {
19namespace {
20
21constexpr uptr kBitsPerShadowByte = 8;
22static_assert(
23 1 << kShadowScale == kBitsPerShadowByte,
24 "CopyProf tracks 1 bit per application byte (8 bits per shadow byte)");
25
26// Tracks whether application memory is marked as a copy.
27ShadowMemory g_copy_shadow;
28
29// Returns a mask with bits [`start_bit`, `end_bit`) set.
30unsigned char BitMask(uptr start_bit, uptr end_bit) {
31 CHECK_LE(start_bit, end_bit);
32 return static_cast<unsigned char>((1 << end_bit) - (1 << start_bit));
33}
34
35// Whether bits [`start_bit`, `end_bit`) of `shadow_byte` are all set.
36bool AllBitsSet(unsigned char shadow_byte, uptr start_bit, uptr end_bit) {
37 const unsigned char mask = BitMask(start_bit, end_bit);
38 return (shadow_byte & mask) == mask;
39}
40
41// Sets (or clears) bits [`start_bit`, `end_bit`) of `*shadow_byte`.
42void SetBits(unsigned char* shadow_byte, uptr start_bit, uptr end_bit,
43 bool is_copy) {
44 const unsigned char mask = BitMask(start_bit, end_bit);
45 if (is_copy)
46 *shadow_byte |= mask;
47 else
48 *shadow_byte &= static_cast<unsigned char>(~mask);
49}
50
51uptr BytesToShadowBits(uptr num_bytes) {
52 // Each application byte maps to one shadow bit.
53 return num_bytes;
54}
55
56} // namespace
57
58void InitializeShadowMemory() {
59 g_copy_shadow = ShadowMemory::Create(name: "copyprof");
60}
61
62void MarkApplicationMemory(const void* app_addr, uptr num_bytes, bool is_copy) {
63 if (UNLIKELY(num_bytes == 0))
64 return;
65 const uptr addr = reinterpret_cast<uptr>(app_addr);
66 unsigned char* shadow =
67 reinterpret_cast<unsigned char*>(g_copy_shadow.MemToShadow(p: addr));
68
69 // An application range need not start on a shadow byte boundary, so it is
70 // updated in three steps: the leading (possibly partial) shadow byte, the
71 // whole shadow bytes in the middle, and the trailing partial byte.
72 uptr num_shadow_bits = BytesToShadowBits(num_bytes);
73 if (uptr start_bit = addr % kBitsPerShadowByte; start_bit > 0) {
74 uptr end_bit =
75 start_bit + Min(a: kBitsPerShadowByte - start_bit, b: num_shadow_bits);
76 SetBits(shadow_byte: shadow++, start_bit, end_bit, is_copy);
77 num_shadow_bits -= end_bit - start_bit;
78 }
79 if (uptr full_bytes = num_shadow_bits / kBitsPerShadowByte; full_bytes > 0) {
80 internal_memset(s: shadow, c: is_copy ? 0xFF : 0, n: full_bytes);
81 shadow += full_bytes;
82 num_shadow_bits -= full_bytes * kBitsPerShadowByte;
83 }
84 if (num_shadow_bits > 0)
85 SetBits(shadow_byte: shadow, /*start_bit=*/0, end_bit: num_shadow_bits, is_copy);
86}
87
88bool IsMarkedAsCopy(const void* app_addr, uptr num_bytes) {
89 if (UNLIKELY(num_bytes == 0))
90 return true;
91 const uptr addr = reinterpret_cast<uptr>(app_addr);
92 const auto* shadow =
93 reinterpret_cast<const unsigned char*>(g_copy_shadow.MemToShadow(p: addr));
94
95 uptr num_shadow_bits = BytesToShadowBits(num_bytes);
96 if (uptr start_bit = addr % kBitsPerShadowByte; start_bit > 0) {
97 uptr end_bit =
98 start_bit + Min(a: kBitsPerShadowByte - start_bit, b: num_shadow_bits);
99 if (!AllBitsSet(shadow_byte: *shadow++, start_bit, end_bit))
100 return false;
101 num_shadow_bits -= end_bit - start_bit;
102 }
103 uptr full_bytes = num_shadow_bits / kBitsPerShadowByte;
104 for (uptr i = 0; i < full_bytes; ++i) {
105 if (shadow[i] != 0xFF)
106 return false;
107 }
108 shadow += full_bytes;
109 num_shadow_bits -= full_bytes * kBitsPerShadowByte;
110 return num_shadow_bits == 0 ||
111 AllBitsSet(shadow_byte: *shadow, /*start_bit=*/0, end_bit: num_shadow_bits);
112}
113
114} // namespace __copyprof
115