1 : // Copyright 2012 Google Inc.
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 : #include "syzygy/agent/asan/asan_shadow.h"
15 :
16 : #include "base/logging.h"
17 :
18 : namespace agent {
19 : namespace asan {
20 :
21 : uint8 Shadow::shadow_[kShadowSize];
22 :
23 E : void Shadow::Poison(const void* addr, size_t size) {
24 E : uintptr_t index = reinterpret_cast<uintptr_t>(addr);
25 E : uintptr_t start = index & 0x7;
26 E : DCHECK_EQ(0U, (index + size) & 0x7);
27 :
28 E : index >>= 3;
29 E : if (start)
30 E : shadow_[index++] = start;
31 :
32 E : size >>= 3;
33 E : DCHECK_GT(arraysize(shadow_), index + size);
34 E : memset(shadow_ + index, 0xFF, size);
35 E : }
36 :
37 E : void Shadow::Unpoison(const void* addr, size_t size) {
38 E : uintptr_t index = reinterpret_cast<uintptr_t>(addr);
39 E : DCHECK_EQ(0U, index & 0x7);
40 :
41 E : uint8 remainder = size & 0x7;
42 E : index >>= 3;
43 E : size >>= 3;
44 E : DCHECK_GT(arraysize(shadow_), index + size);
45 E : memset(shadow_ + index, 0, size);
46 :
47 E : if (remainder != 0)
48 E : shadow_[index + size] = remainder;
49 E : }
50 :
51 E : bool Shadow::IsAccessible(const void* addr) {
52 E : uintptr_t index = reinterpret_cast<uintptr_t>(addr);
53 E : uintptr_t start = index & 0x7;
54 :
55 E : index >>= 3;
56 :
57 E : DCHECK_GT(arraysize(shadow_), index);
58 E : uint8 shadow = shadow_[index];
59 E : if (shadow == 0)
60 E : return true;
61 :
62 E : if (shadow == 0xFF)
63 E : return false;
64 :
65 E : return start < shadow;
66 E : }
67 :
68 : } // namespace asan
69 : } // namespace agent
|