1 : // Copyright 2015 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 : #include "syzygy/agent/asan/memory_interceptors_patcher.h"
16 :
17 : #include "gtest/gtest.h"
18 : #include "syzygy/agent/asan/memory_interceptors.h"
19 : #include "syzygy/agent/asan/shadow.h"
20 :
21 : // The linker satisfies this symbol. This gets us a pointer to our own module
22 : // when we're loaded.
23 : extern "C" IMAGE_DOS_HEADER __ImageBase;
24 :
25 : namespace agent {
26 : namespace asan {
27 :
28 : namespace {
29 :
30 E : void ExpectShadowReferencesTo(const uint8_t* shadow_memory) {
31 E : for (const void** cursor = asan_shadow_references; *cursor != 0; ++cursor) {
32 E : EXPECT_EQ(shadow_memory, *reinterpret_cast<uint8_t const* const*>(*cursor));
33 E : }
34 E : }
35 :
36 : } // namespace
37 :
38 E : TEST(MemoryInterceptorPatcherTest, PatchMemoryInterceptorShadowReferences) {
39 : // The references should initially be to the static shadow memory.
40 : EXPECT_NO_FATAL_FAILURE(
41 E : ExpectShadowReferencesTo(asan_memory_interceptors_shadow_memory));
42 :
43 : // Patch the references to point to a new shadow memory.
44 E : uint8_t dummy_shadow[1] = {};
45 : EXPECT_TRUE(PatchMemoryInterceptorShadowReferences(
46 E : asan_memory_interceptors_shadow_memory, dummy_shadow));
47 E : EXPECT_NO_FATAL_FAILURE(ExpectShadowReferencesTo(dummy_shadow));
48 :
49 : // Try patching again. The 'current' shadow memory matching will fail
50 : // and the functions should still point to the new shadow.
51 : EXPECT_FALSE(PatchMemoryInterceptorShadowReferences(
52 E : asan_memory_interceptors_shadow_memory, dummy_shadow));
53 E : EXPECT_NO_FATAL_FAILURE(ExpectShadowReferencesTo(dummy_shadow));
54 :
55 : // Patch this back to the original shadow memory so the unittest leaves no
56 : // side effects.
57 : EXPECT_TRUE(PatchMemoryInterceptorShadowReferences(
58 E : dummy_shadow, asan_memory_interceptors_shadow_memory));
59 : EXPECT_NO_FATAL_FAILURE(
60 E : ExpectShadowReferencesTo(asan_memory_interceptors_shadow_memory));
61 E : }
62 :
63 : } // namespace asan
64 : } // namespace agent
|