1 : // Copyright 2012 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // Implements an all-static class that manages shadow memory for ASAN.
16 : #ifndef SYZYGY_AGENT_ASAN_ASAN_SHADOW_H_
17 : #define SYZYGY_AGENT_ASAN_ASAN_SHADOW_H_
18 :
19 : #include <string>
20 :
21 : #include "base/basictypes.h"
22 :
23 m : namespace agent {
24 m : namespace asan {
25 :
26 : // An all-static class that manages the ASAN shadow memory.
27 m : class Shadow {
28 m : public:
29 : // The granularity of the shadow memory.
30 m : static const size_t kShadowGranularityLog = 3;
31 m : static const size_t kShadowGranularity = 1 << kShadowGranularityLog;
32 :
33 : // Set up the shadow memory.
34 m : static void SetUp();
35 :
36 : // Tear down the shadow memory.
37 m : static void TearDown();
38 :
39 : // The different markers we use to mark the shadow memory.
40 m : enum ShadowMarker {
41 m : kHeapAddressableByte = 0x00,
42 m : kHeapNonAccessibleByteMask = 0xf0,
43 m : kAsanMemoryByte = 0xf1,
44 m : kInvalidAddress = 0xf2,
45 m : kUserRedzone = 0xf3,
46 m : kHeapLeftRedzone = 0xfa,
47 m : kHeapRightRedzone = 0xfb,
48 m : kHeapFreedByte = 0xfd,
49 m : };
50 :
51 : // Poisons @p size bytes starting at @p addr with @p shadow_val value.
52 : // @pre addr + size mod 8 == 0.
53 : // @param address The starting address.
54 : // @param size The size of the memory to poison.
55 : // @param shadow_val The poison marker value.
56 m : static void Poison(const void* addr, size_t size, ShadowMarker shadow_val);
57 :
58 : // Un-poisons @p size bytes starting at @p addr.
59 : // @pre addr mod 8 == 0 && size mod 8 == 0.
60 : // @param addr The starting address.
61 : // @param size The size of the memory to unpoison.
62 m : static void Unpoison(const void* addr, size_t size);
63 :
64 : // Mark @p size bytes starting at @p addr as freed.
65 : // @param addr The starting address.
66 : // @param size The size of the memory to mark as freed.
67 m : static void MarkAsFreed(const void* addr, size_t size);
68 :
69 : // Returns true iff the byte at @p addr is not poisoned.
70 : // @param addr The address that we want to check.
71 : // @returns true if this address is accessible, false otherwise.
72 m : static bool IsAccessible(const void* addr);
73 :
74 : // Returns the ShadowMarker value for the byte at @p addr.
75 : // @param addr The address for which we want the ShadowMarker value.
76 : // @returns the ShadowMarker value for this address.
77 m : static ShadowMarker GetShadowMarkerForAddress(const void* addr);
78 :
79 : // Appends a textual description of the shadow memory for @p addr to
80 : // @p output, including the values of the shadow bytes and a legend.
81 : // @param addr The address for which we want to get the textual description.
82 : // @param output The string in which we want to store this information.
83 m : static void AppendShadowMemoryText(const void* addr, std::string* output);
84 :
85 : // Appends a textual description of the shadow memory for @p addr to
86 : // @p output. This only appends the values of the shadow bytes.
87 : // @param addr The address whose shadow memory is to be described.
88 : // @param output The string to be populated with the shadow memory
89 : // information.
90 m : static void AppendShadowArrayText(const void* addr, std::string* output);
91 :
92 : // Returns true iff the array starting at @p addr is null terminated within a
93 : // contiguous accessible region of memory. When returning true the length of
94 : // the null-terminated array (including the trailing zero) will be returned
95 : // via @p size. When returning false the offset of the invalid access will be
96 : // returned via @p size.
97 : // @param addr The starting address of the array that we want to check.
98 : // @param size Will receive the size of the null terminated array or the
99 : // offset of the invalid access.
100 : // @param max_size The maximum length to check. Ignored if set to zero.
101 : // @returns true iff the array starting at @p addr is null terminated within a
102 : // contiguous accessible region of memory, false otherwise.
103 m : static bool GetNullTerminatedArraySize(const void* addr,
104 m : size_t* size,
105 m : size_t max_size);
106 :
107 : // Clones a shadow memory range from one location to another.
108 : // @pre src_pointer mod 8 == 0.
109 : // @pre dst_pointer mod 8 == 0.
110 : // @pre size mod 8 == 0.
111 : // @param src_pointer The starting address of the range to copy.
112 : // @param dst_pointer The destination where the copy should be made.
113 : // @param size The size of the range to copy.
114 m : static void CloneShadowRange(const void* src_pointer,
115 m : void* dst_pointer,
116 m : size_t size);
117 :
118 m : protected:
119 : // Reset the shadow memory.
120 m : static void Reset();
121 :
122 : // Appends a line of shadow byte text for the bytes ranging from
123 : // shadow_[index] to shadow_[index + 7], prefixed by @p prefix. If the index
124 : // @p bug_index is present in this range then its value will be surrounded by
125 : // brackets.
126 m : static void AppendShadowByteText(const char *prefix,
127 m : uintptr_t index,
128 m : std::string* output,
129 m : size_t bug_index);
130 :
131 : // One shadow byte for every 8 bytes in a 2G address space. By default Chrome
132 : // is not large address aware, so we shouldn't be using the high memory.
133 m : static const size_t kShadowSize = 1 << (31 - kShadowGranularityLog);
134 m : static uint8 shadow_[kShadowSize];
135 m : };
136 :
137 m : } // namespace asan
138 m : } // namespace agent
139 :
140 : #endif // SYZYGY_AGENT_ASAN_ASAN_SHADOW_H_
|