blob: 40ee86ce79a10b8fb7d2a58e47fb31e1777bfd1f [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"
Andreas Gampe5e36c2f2017-04-21 19:11:15 -070021#include "handle_scope-inl.h"
22#include "mirror/object-inl.h"
23#include "mirror/object_array-inl.h"
Mathieu Chartier1ca68902017-04-18 11:26:22 -070024#include "mirror/string.h"
Andreas Gampe5e36c2f2017-04-21 19:11:15 -070025#include "runtime.h"
Mathieu Chartier1ca68902017-04-18 11:26:22 -070026#include "scoped_thread_state_change-inl.h"
Andreas Gampe5e36c2f2017-04-21 19:11:15 -070027#include "verification.h"
Mathieu Chartier1ca68902017-04-18 11:26:22 -070028
29namespace art {
30namespace gc {
31
32class VerificationTest : public CommonRuntimeTest {
33 protected:
34 VerificationTest() {}
35
36 template <class T>
37 mirror::ObjectArray<T>* AllocObjectArray(Thread* self, size_t length)
38 REQUIRES_SHARED(Locks::mutator_lock_) {
39 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
40 return mirror::ObjectArray<T>::Alloc(
41 self,
42 class_linker->GetClassRoot(ClassLinker::ClassRoot::kObjectArrayClass),
43 length);
44 }
45};
46
47TEST_F(VerificationTest, IsValidHeapObjectAddress) {
48 ScopedObjectAccess soa(Thread::Current());
49 const Verification* const v = Runtime::Current()->GetHeap()->GetVerification();
50 EXPECT_FALSE(v->IsValidHeapObjectAddress(reinterpret_cast<const void*>(1)));
51 EXPECT_FALSE(v->IsValidHeapObjectAddress(reinterpret_cast<const void*>(4)));
52 EXPECT_FALSE(v->IsValidHeapObjectAddress(nullptr));
53 VariableSizedHandleScope hs(soa.Self());
54 Handle<mirror::String> string(
55 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "test")));
56 EXPECT_TRUE(v->IsValidHeapObjectAddress(string.Get()));
Mathieu Chartierb814ef52017-06-27 12:56:00 -070057 // Address in the heap that isn't aligned.
58 const void* unaligned_address =
59 reinterpret_cast<const void*>(reinterpret_cast<uintptr_t>(string.Get()) + 1);
60 EXPECT_TRUE(v->IsAddressInHeapSpace(unaligned_address));
61 EXPECT_FALSE(v->IsValidHeapObjectAddress(unaligned_address));
Mathieu Chartier1ca68902017-04-18 11:26:22 -070062 EXPECT_TRUE(v->IsValidHeapObjectAddress(string->GetClass()));
63 const uintptr_t uint_klass = reinterpret_cast<uintptr_t>(string->GetClass());
64 // Not actually a valid object but the verification can't know that. Guaranteed to be inside a
65 // heap space.
66 EXPECT_TRUE(v->IsValidHeapObjectAddress(
67 reinterpret_cast<const void*>(uint_klass + kObjectAlignment)));
68 EXPECT_FALSE(v->IsValidHeapObjectAddress(
69 reinterpret_cast<const void*>(&uint_klass)));
70}
71
Mathieu Chartier68dda8f2017-04-24 10:06:15 -070072TEST_F(VerificationTest, IsValidClassOrNotInHeap) {
Mathieu Chartier1ca68902017-04-18 11:26:22 -070073 ScopedObjectAccess soa(Thread::Current());
74 VariableSizedHandleScope hs(soa.Self());
75 Handle<mirror::String> string(
76 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "test")));
77 const Verification* const v = Runtime::Current()->GetHeap()->GetVerification();
78 EXPECT_FALSE(v->IsValidClass(reinterpret_cast<const void*>(1)));
79 EXPECT_FALSE(v->IsValidClass(reinterpret_cast<const void*>(4)));
80 EXPECT_FALSE(v->IsValidClass(nullptr));
Mathieu Chartier1ca68902017-04-18 11:26:22 -070081 EXPECT_TRUE(v->IsValidClass(string->GetClass()));
Mathieu Chartier68dda8f2017-04-24 10:06:15 -070082 EXPECT_FALSE(v->IsValidClass(string.Get()));
83}
84
85TEST_F(VerificationTest, IsValidClassInHeap) {
86 TEST_DISABLED_FOR_MEMORY_TOOL();
87 ScopedObjectAccess soa(Thread::Current());
88 VariableSizedHandleScope hs(soa.Self());
89 Handle<mirror::String> string(
90 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "test")));
91 const Verification* const v = Runtime::Current()->GetHeap()->GetVerification();
Mathieu Chartier1ca68902017-04-18 11:26:22 -070092 const uintptr_t uint_klass = reinterpret_cast<uintptr_t>(string->GetClass());
93 EXPECT_FALSE(v->IsValidClass(reinterpret_cast<const void*>(uint_klass - kObjectAlignment)));
94 EXPECT_FALSE(v->IsValidClass(reinterpret_cast<const void*>(&uint_klass)));
95}
96
Mathieu Chartier68dda8f2017-04-24 10:06:15 -070097TEST_F(VerificationTest, DumpInvalidObjectInfo) {
98 ScopedLogSeverity sls(LogSeverity::INFO);
99 ScopedObjectAccess soa(Thread::Current());
100 Runtime* const runtime = Runtime::Current();
101 VariableSizedHandleScope hs(soa.Self());
102 const Verification* const v = runtime->GetHeap()->GetVerification();
103 LOG(INFO) << v->DumpObjectInfo(reinterpret_cast<const void*>(1), "obj");
104 LOG(INFO) << v->DumpObjectInfo(reinterpret_cast<const void*>(4), "obj");
105 LOG(INFO) << v->DumpObjectInfo(nullptr, "obj");
106}
107
108TEST_F(VerificationTest, DumpValidObjectInfo) {
109 TEST_DISABLED_FOR_MEMORY_TOOL();
Mathieu Chartier1ca68902017-04-18 11:26:22 -0700110 ScopedLogSeverity sls(LogSeverity::INFO);
111 ScopedObjectAccess soa(Thread::Current());
112 Runtime* const runtime = Runtime::Current();
113 VariableSizedHandleScope hs(soa.Self());
114 Handle<mirror::String> string(
115 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "obj")));
116 Handle<mirror::ObjectArray<mirror::Object>> arr(
117 hs.NewHandle(AllocObjectArray<mirror::Object>(soa.Self(), 256)));
118 const Verification* const v = runtime->GetHeap()->GetVerification();
Mathieu Chartier1ca68902017-04-18 11:26:22 -0700119 LOG(INFO) << v->DumpObjectInfo(string.Get(), "test");
120 LOG(INFO) << v->DumpObjectInfo(string->GetClass(), "obj");
121 const uintptr_t uint_klass = reinterpret_cast<uintptr_t>(string->GetClass());
122 LOG(INFO) << v->DumpObjectInfo(reinterpret_cast<const void*>(uint_klass - kObjectAlignment),
123 "obj");
124 LOG(INFO) << v->DumpObjectInfo(reinterpret_cast<const void*>(&uint_klass), "obj");
125 LOG(INFO) << v->DumpObjectInfo(arr.Get(), "arr");
126}
127
128TEST_F(VerificationTest, LogHeapCorruption) {
Mathieu Chartier68dda8f2017-04-24 10:06:15 -0700129 TEST_DISABLED_FOR_MEMORY_TOOL();
Mathieu Chartier1ca68902017-04-18 11:26:22 -0700130 ScopedLogSeverity sls(LogSeverity::INFO);
131 ScopedObjectAccess soa(Thread::Current());
132 Runtime* const runtime = Runtime::Current();
133 VariableSizedHandleScope hs(soa.Self());
134 Handle<mirror::String> string(
135 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "obj")));
136 using ObjArray = mirror::ObjectArray<mirror::Object>;
137 Handle<ObjArray> arr(
138 hs.NewHandle(AllocObjectArray<mirror::Object>(soa.Self(), 256)));
139 const Verification* const v = runtime->GetHeap()->GetVerification();
140 arr->Set(0, string.Get());
141 // Test normal cases.
142 v->LogHeapCorruption(arr.Get(), ObjArray::DataOffset(kHeapReferenceSize), string.Get(), false);
143 v->LogHeapCorruption(string.Get(), mirror::Object::ClassOffset(), string->GetClass(), false);
144 // Test null holder cases.
145 v->LogHeapCorruption(nullptr, MemberOffset(0), string.Get(), false);
146 v->LogHeapCorruption(nullptr, MemberOffset(0), arr.Get(), false);
147}
148
Mathieu Chartier4f5e3cb2017-06-12 13:10:01 -0700149TEST_F(VerificationTest, FindPathFromRootSet) {
150 TEST_DISABLED_FOR_MEMORY_TOOL();
151 ScopedLogSeverity sls(LogSeverity::INFO);
152 ScopedObjectAccess soa(Thread::Current());
153 Runtime* const runtime = Runtime::Current();
154 VariableSizedHandleScope hs(soa.Self());
155 Handle<mirror::ObjectArray<mirror::Object>> arr(
156 hs.NewHandle(AllocObjectArray<mirror::Object>(soa.Self(), 256)));
157 ObjPtr<mirror::String> str = mirror::String::AllocFromModifiedUtf8(soa.Self(), "obj");
158 arr->Set(0, str);
159 const Verification* const v = runtime->GetHeap()->GetVerification();
160 std::string path = v->FirstPathFromRootSet(str);
161 EXPECT_GT(path.length(), 0u);
162 std::ostringstream oss;
163 oss << arr.Get();
164 EXPECT_NE(path.find(oss.str()), std::string::npos);
165 LOG(INFO) << path;
166}
167
Mathieu Chartier1ca68902017-04-18 11:26:22 -0700168} // namespace gc
169} // namespace art