1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "common_runtime_test.h"
18 
19 #include "base/memory_tool.h"
20 #include "class_linker-inl.h"
21 #include "class_root-inl.h"
22 #include "handle_scope-inl.h"
23 #include "mirror/object-inl.h"
24 #include "mirror/object_array-alloc-inl.h"
25 #include "mirror/object_array-inl.h"
26 #include "mirror/string.h"
27 #include "runtime.h"
28 #include "scoped_thread_state_change-inl.h"
29 #include "verification.h"
30 
31 namespace art {
32 namespace gc {
33 
34 class VerificationTest : public CommonRuntimeTest {
35  protected:
VerificationTest()36   VerificationTest() {}
37 
38   template <class T>
AllocObjectArray(Thread * self,size_t length)39   ObjPtr<mirror::ObjectArray<T>> AllocObjectArray(Thread* self, size_t length)
40       REQUIRES_SHARED(Locks::mutator_lock_) {
41     return mirror::ObjectArray<T>::Alloc(
42         self,
43         GetClassRoot<mirror::ObjectArray<mirror::Object>>(),
44         length);
45   }
46 };
47 
TEST_F(VerificationTest,IsValidHeapObjectAddress)48 TEST_F(VerificationTest, IsValidHeapObjectAddress) {
49   ScopedObjectAccess soa(Thread::Current());
50   const Verification* const v = Runtime::Current()->GetHeap()->GetVerification();
51   EXPECT_FALSE(v->IsValidHeapObjectAddress(reinterpret_cast<const void*>(1)));
52   EXPECT_FALSE(v->IsValidHeapObjectAddress(reinterpret_cast<const void*>(4)));
53   EXPECT_FALSE(v->IsValidHeapObjectAddress(nullptr));
54   VariableSizedHandleScope hs(soa.Self());
55   Handle<mirror::String> string(
56       hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "test")));
57   EXPECT_TRUE(v->IsValidHeapObjectAddress(string.Get()));
58   // Address in the heap that isn't aligned.
59   const void* unaligned_address =
60       reinterpret_cast<const void*>(reinterpret_cast<uintptr_t>(string.Get()) + 1);
61   EXPECT_TRUE(v->IsAddressInHeapSpace(unaligned_address));
62   EXPECT_FALSE(v->IsValidHeapObjectAddress(unaligned_address));
63   EXPECT_TRUE(v->IsValidHeapObjectAddress(string->GetClass()));
64   const uintptr_t uint_klass = reinterpret_cast<uintptr_t>(string->GetClass());
65   // Not actually a valid object but the verification can't know that. Guaranteed to be inside a
66   // heap space.
67   EXPECT_TRUE(v->IsValidHeapObjectAddress(
68       reinterpret_cast<const void*>(uint_klass + kObjectAlignment)));
69   EXPECT_FALSE(v->IsValidHeapObjectAddress(
70       reinterpret_cast<const void*>(&uint_klass)));
71 }
72 
TEST_F(VerificationTest,IsValidClassOrNotInHeap)73 TEST_F(VerificationTest, IsValidClassOrNotInHeap) {
74   ScopedObjectAccess soa(Thread::Current());
75   VariableSizedHandleScope hs(soa.Self());
76   Handle<mirror::String> string(
77       hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "test")));
78   const Verification* const v = Runtime::Current()->GetHeap()->GetVerification();
79   EXPECT_FALSE(v->IsValidClass(reinterpret_cast<const void*>(1)));
80   EXPECT_FALSE(v->IsValidClass(reinterpret_cast<const void*>(4)));
81   EXPECT_FALSE(v->IsValidClass(nullptr));
82   EXPECT_TRUE(v->IsValidClass(string->GetClass()));
83   EXPECT_FALSE(v->IsValidClass(string.Get()));
84 }
85 
TEST_F(VerificationTest,IsValidClassInHeap)86 TEST_F(VerificationTest, IsValidClassInHeap) {
87   // Now that the String class is allocated in the non-moving space when the
88   // runtime is running without a boot image (which is the case in this gtest),
89   // and we run with AddressSanizer, it is possible that the (presumably
90   // invalid) memory location `uint_klass - kObjectAlignment` tested below is
91   // poisoned when running with AddressSanizer. Disable this test in that case.
92   TEST_DISABLED_FOR_MEMORY_TOOL();
93   ScopedObjectAccess soa(Thread::Current());
94   VariableSizedHandleScope hs(soa.Self());
95   Handle<mirror::String> string(
96       hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "test")));
97   const Verification* const v = Runtime::Current()->GetHeap()->GetVerification();
98   const uintptr_t uint_klass = reinterpret_cast<uintptr_t>(string->GetClass());
99   EXPECT_FALSE(v->IsValidClass(reinterpret_cast<const void*>(uint_klass - kObjectAlignment)));
100   EXPECT_FALSE(v->IsValidClass(reinterpret_cast<const void*>(&uint_klass)));
101 }
102 
TEST_F(VerificationTest,DumpInvalidObjectInfo)103 TEST_F(VerificationTest, DumpInvalidObjectInfo) {
104   ScopedLogSeverity sls(LogSeverity::INFO);
105   ScopedObjectAccess soa(Thread::Current());
106   Runtime* const runtime = Runtime::Current();
107   VariableSizedHandleScope hs(soa.Self());
108   const Verification* const v = runtime->GetHeap()->GetVerification();
109   LOG(INFO) << v->DumpObjectInfo(reinterpret_cast<const void*>(1), "obj");
110   LOG(INFO) << v->DumpObjectInfo(reinterpret_cast<const void*>(4), "obj");
111   LOG(INFO) << v->DumpObjectInfo(nullptr, "obj");
112 }
113 
TEST_F(VerificationTest,DumpValidObjectInfo)114 TEST_F(VerificationTest, DumpValidObjectInfo) {
115   // Now that the String class is allocated in the non-moving space when the
116   // runtime is running without a boot image (which is the case in this gtest),
117   // and we run with AddressSanizer, it is possible that the calls to
118   // Verification::DumpObjectInfo below involving the String class object
119   // (`string->GetClass()`, `uint_klass`, etc.) access poisoned memory when they
120   // call Verification::DumpRAMAroundAddress. Disable this test in that case.
121   TEST_DISABLED_FOR_MEMORY_TOOL();
122   ScopedLogSeverity sls(LogSeverity::INFO);
123   ScopedObjectAccess soa(Thread::Current());
124   Runtime* const runtime = Runtime::Current();
125   VariableSizedHandleScope hs(soa.Self());
126   Handle<mirror::String> string(
127       hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "obj")));
128   Handle<mirror::ObjectArray<mirror::Object>> arr(
129       hs.NewHandle(AllocObjectArray<mirror::Object>(soa.Self(), 256)));
130   const Verification* const v = runtime->GetHeap()->GetVerification();
131   LOG(INFO) << v->DumpObjectInfo(string.Get(), "test");
132   LOG(INFO) << v->DumpObjectInfo(string->GetClass(), "obj");
133   const uintptr_t uint_klass = reinterpret_cast<uintptr_t>(string->GetClass());
134   LOG(INFO) << v->DumpObjectInfo(reinterpret_cast<const void*>(uint_klass - kObjectAlignment),
135                                  "obj");
136   LOG(INFO) << v->DumpObjectInfo(reinterpret_cast<const void*>(&uint_klass), "obj");
137   LOG(INFO) << v->DumpObjectInfo(arr.Get(), "arr");
138 }
139 
TEST_F(VerificationTest,LogHeapCorruption)140 TEST_F(VerificationTest, LogHeapCorruption) {
141   // Now that the String class is allocated in the non-moving space when the
142   // runtime is running without a boot image (which is the case in this gtest),
143   // and we run with AddressSanizer, it is possible that the call to
144   // Verification::LogHeapCorruption below involving the String class object
145   // (`string->GetClass()`) accesses poisoned memory when it calls
146   // Verification::DumpRAMAroundAddress. Disable this test in that case.
147   TEST_DISABLED_FOR_MEMORY_TOOL();
148   ScopedLogSeverity sls(LogSeverity::INFO);
149   ScopedObjectAccess soa(Thread::Current());
150   Runtime* const runtime = Runtime::Current();
151   VariableSizedHandleScope hs(soa.Self());
152   Handle<mirror::String> string(
153       hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "obj")));
154   using ObjArray = mirror::ObjectArray<mirror::Object>;
155   Handle<ObjArray> arr(
156       hs.NewHandle(AllocObjectArray<mirror::Object>(soa.Self(), 256)));
157   const Verification* const v = runtime->GetHeap()->GetVerification();
158   arr->Set(0, string.Get());
159   // Test normal cases.
160   v->LogHeapCorruption(arr.Get(), ObjArray::DataOffset(kHeapReferenceSize), string.Get(), false);
161   v->LogHeapCorruption(string.Get(), mirror::Object::ClassOffset(), string->GetClass(), false);
162   // Test null holder cases.
163   v->LogHeapCorruption(nullptr, MemberOffset(0), string.Get(), false);
164   v->LogHeapCorruption(nullptr, MemberOffset(0), arr.Get(), false);
165 }
166 
TEST_F(VerificationTest,FindPathFromRootSet)167 TEST_F(VerificationTest, FindPathFromRootSet) {
168   ScopedLogSeverity sls(LogSeverity::INFO);
169   ScopedObjectAccess soa(Thread::Current());
170   Runtime* const runtime = Runtime::Current();
171   VariableSizedHandleScope hs(soa.Self());
172   Handle<mirror::ObjectArray<mirror::Object>> arr(
173       hs.NewHandle(AllocObjectArray<mirror::Object>(soa.Self(), 256)));
174   ObjPtr<mirror::String> str = mirror::String::AllocFromModifiedUtf8(soa.Self(), "obj");
175   arr->Set(0, str);
176   const Verification* const v = runtime->GetHeap()->GetVerification();
177   std::string path = v->FirstPathFromRootSet(str);
178   EXPECT_GT(path.length(), 0u);
179   std::ostringstream oss;
180   oss << arr.Get();
181   EXPECT_NE(path.find(oss.str()), std::string::npos);
182   LOG(INFO) << path;
183 }
184 
185 }  // namespace gc
186 }  // namespace art
187