1 : // Copyright 2014 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_notifiers/shadow_memory_notifier.h"
16 :
17 : #include "base/memory/scoped_ptr.h"
18 : #include "gtest/gtest.h"
19 : #include "syzygy/agent/asan/shadow.h"
20 :
21 : namespace agent {
22 : namespace asan {
23 : namespace memory_notifiers {
24 :
25 E : TEST(ShadowMemoryNotifierTest, ShadowStateTransitionsWithNotification) {
26 : // A buffer to use. This is allocated dynamically to ensure it has 8 byte
27 : // alignment.
28 E : const size_t kBufferSize = 1024;
29 E : scoped_ptr<uint8> buffer(new uint8[kBufferSize]);
30 :
31 : // Unpoison the memory so that the test starts from clean data.
32 E : Shadow::Unpoison(buffer.get(), kBufferSize);
33 :
34 E : ShadowMemoryNotifier n;
35 E : n.NotifyInternalUse(buffer.get(), kBufferSize);
36 E : EXPECT_FALSE(Shadow::IsAccessible(buffer.get()));
37 E : EXPECT_FALSE(Shadow::IsAccessible(buffer.get() + 10));
38 E : EXPECT_TRUE(Shadow::IsAccessible(buffer.get() + kBufferSize));
39 E : for (size_t i = 0; i < kBufferSize; ++i) {
40 : EXPECT_EQ(kAsanMemoryMarker,
41 E : Shadow::GetShadowMarkerForAddress(buffer.get() + i));
42 E : }
43 :
44 E : n.NotifyFutureHeapUse(buffer.get(), kBufferSize);
45 E : EXPECT_FALSE(Shadow::IsAccessible(buffer.get()));
46 E : EXPECT_FALSE(Shadow::IsAccessible(buffer.get() + 10));
47 E : EXPECT_TRUE(Shadow::IsAccessible(buffer.get() + kBufferSize));
48 E : for (size_t i = 0; i < kBufferSize; ++i) {
49 : EXPECT_EQ(kAsanReservedMarker,
50 E : Shadow::GetShadowMarkerForAddress(buffer.get() + i));
51 E : }
52 :
53 E : n.NotifyReturnedToOS(buffer.get(), kBufferSize);
54 E : EXPECT_TRUE(Shadow::IsAccessible(buffer.get()));
55 E : EXPECT_TRUE(Shadow::IsAccessible(buffer.get() + 10));
56 E : EXPECT_TRUE(Shadow::IsAccessible(buffer.get() + kBufferSize));
57 E : for (size_t i = 0; i < kBufferSize; ++i) {
58 : EXPECT_EQ(kHeapAddressableMarker,
59 E : Shadow::GetShadowMarkerForAddress(buffer.get() + i));
60 E : }
61 :
62 : // Clean up behind ourselves.
63 E : Shadow::Unpoison(buffer.get(), kBufferSize);
64 E : }
65 :
66 : } // namespace memory_notifiers
67 : } // namespace asan
68 : } // namespace agent
|