blob: 7835c2969a7498222420b6ce92a059ce3cc65c47 [file] [log] [blame]
Mathieu Chartier1ca68902017-04-18 11:26:22 -07001/*
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"
Andreas Gampe5e36c2f2017-04-21 19:11:15 -070018
Mathieu Chartier68dda8f2017-04-24 10:06:15 -070019#include "base/memory_tool.h"
Andreas Gampe508fdf32017-06-05 16:42:13 -070020#include "class_linker-inl.h"
Vladimir Markob4eb1b12018-05-24 11:09:38 +010021#include "class_root.h"
Andreas Gampe5e36c2f2017-04-21 19:11:15 -070022#include "handle_scope-inl.h"
23#include "mirror/object-inl.h"
Andreas Gampe52ecb652018-10-24 15:18:21 -070024#include "mirror/object_array-alloc-inl.h"
Andreas Gampe5e36c2f2017-04-21 19:11:15 -070025#include "mirror/object_array-inl.h"
Mathieu Chartier1ca68902017-04-18 11:26:22 -070026#include "mirror/string.h"
Andreas Gampe5e36c2f2017-04-21 19:11:15 -070027#include "runtime.h"
Mathieu Chartier1ca68902017-04-18 11:26:22 -070028#include "scoped_thread_state_change-inl.h"
Andreas Gampe5e36c2f2017-04-21 19:11:15 -070029#include "verification.h"
Mathieu Chartier1ca68902017-04-18 11:26:22 -070030
31namespace art {
32namespace gc {
33
34class VerificationTest : public CommonRuntimeTest {
35 protected:
36 VerificationTest() {}
37
38 template <class T>
Vladimir Markobcf17522018-06-01 13:14:32 +010039 ObjPtr<mirror::ObjectArray<T>> AllocObjectArray(Thread* self, size_t length)
Mathieu Chartier1ca68902017-04-18 11:26:22 -070040 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier1ca68902017-04-18 11:26:22 -070041 return mirror::ObjectArray<T>::Alloc(
42 self,
Vladimir Markob4eb1b12018-05-24 11:09:38 +010043 GetClassRoot<mirror::ObjectArray<mirror::Object>>(),
Mathieu Chartier1ca68902017-04-18 11:26:22 -070044 length);
45 }
46};
47
48TEST_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()));
Mathieu Chartierb814ef52017-06-27 12:56:00 -070058 // 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));
Mathieu Chartier1ca68902017-04-18 11:26:22 -070063 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
Mathieu Chartier68dda8f2017-04-24 10:06:15 -070073TEST_F(VerificationTest, IsValidClassOrNotInHeap) {
Mathieu Chartier1ca68902017-04-18 11:26:22 -070074 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));
Mathieu Chartier1ca68902017-04-18 11:26:22 -070082 EXPECT_TRUE(v->IsValidClass(string->GetClass()));
Mathieu Chartier68dda8f2017-04-24 10:06:15 -070083 EXPECT_FALSE(v->IsValidClass(string.Get()));
84}
85
86TEST_F(VerificationTest, IsValidClassInHeap) {
Roland Levillain5fe2f342018-09-07 18:41:49 +010087 // 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();
Mathieu Chartier68dda8f2017-04-24 10:06:15 -070093 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();
Mathieu Chartier1ca68902017-04-18 11:26:22 -070098 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
Mathieu Chartier68dda8f2017-04-24 10:06:15 -0700103TEST_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
114TEST_F(VerificationTest, DumpValidObjectInfo) {
Roland Levillain5fe2f342018-09-07 18:41:49 +0100115 // 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();
Mathieu Chartier1ca68902017-04-18 11:26:22 -0700122 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();
Mathieu Chartier1ca68902017-04-18 11:26:22 -0700131 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
140TEST_F(VerificationTest, LogHeapCorruption) {
Roland Levillain5fe2f342018-09-07 18:41:49 +0100141 // 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();
Mathieu Chartier1ca68902017-04-18 11:26:22 -0700148 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
Mathieu Chartier4f5e3cb2017-06-12 13:10:01 -0700167TEST_F(VerificationTest, FindPathFromRootSet) {
Mathieu Chartier4f5e3cb2017-06-12 13:10:01 -0700168 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
Mathieu Chartier1ca68902017-04-18 11:26:22 -0700185} // namespace gc
186} // namespace art