blob: a307c519cb34db03e24eda8172f1d36ad8cd76f5 [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()));
57 EXPECT_TRUE(v->IsValidHeapObjectAddress(string->GetClass()));
58 const uintptr_t uint_klass = reinterpret_cast<uintptr_t>(string->GetClass());
59 // Not actually a valid object but the verification can't know that. Guaranteed to be inside a
60 // heap space.
61 EXPECT_TRUE(v->IsValidHeapObjectAddress(
62 reinterpret_cast<const void*>(uint_klass + kObjectAlignment)));
63 EXPECT_FALSE(v->IsValidHeapObjectAddress(
64 reinterpret_cast<const void*>(&uint_klass)));
65}
66
Mathieu Chartier68dda8f2017-04-24 10:06:15 -070067TEST_F(VerificationTest, IsValidClassOrNotInHeap) {
Mathieu Chartier1ca68902017-04-18 11:26:22 -070068 ScopedObjectAccess soa(Thread::Current());
69 VariableSizedHandleScope hs(soa.Self());
70 Handle<mirror::String> string(
71 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "test")));
72 const Verification* const v = Runtime::Current()->GetHeap()->GetVerification();
73 EXPECT_FALSE(v->IsValidClass(reinterpret_cast<const void*>(1)));
74 EXPECT_FALSE(v->IsValidClass(reinterpret_cast<const void*>(4)));
75 EXPECT_FALSE(v->IsValidClass(nullptr));
Mathieu Chartier1ca68902017-04-18 11:26:22 -070076 EXPECT_TRUE(v->IsValidClass(string->GetClass()));
Mathieu Chartier68dda8f2017-04-24 10:06:15 -070077 EXPECT_FALSE(v->IsValidClass(string.Get()));
78}
79
80TEST_F(VerificationTest, IsValidClassInHeap) {
81 TEST_DISABLED_FOR_MEMORY_TOOL();
82 ScopedObjectAccess soa(Thread::Current());
83 VariableSizedHandleScope hs(soa.Self());
84 Handle<mirror::String> string(
85 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "test")));
86 const Verification* const v = Runtime::Current()->GetHeap()->GetVerification();
Mathieu Chartier1ca68902017-04-18 11:26:22 -070087 const uintptr_t uint_klass = reinterpret_cast<uintptr_t>(string->GetClass());
88 EXPECT_FALSE(v->IsValidClass(reinterpret_cast<const void*>(uint_klass - kObjectAlignment)));
89 EXPECT_FALSE(v->IsValidClass(reinterpret_cast<const void*>(&uint_klass)));
90}
91
Mathieu Chartier68dda8f2017-04-24 10:06:15 -070092TEST_F(VerificationTest, DumpInvalidObjectInfo) {
93 ScopedLogSeverity sls(LogSeverity::INFO);
94 ScopedObjectAccess soa(Thread::Current());
95 Runtime* const runtime = Runtime::Current();
96 VariableSizedHandleScope hs(soa.Self());
97 const Verification* const v = runtime->GetHeap()->GetVerification();
98 LOG(INFO) << v->DumpObjectInfo(reinterpret_cast<const void*>(1), "obj");
99 LOG(INFO) << v->DumpObjectInfo(reinterpret_cast<const void*>(4), "obj");
100 LOG(INFO) << v->DumpObjectInfo(nullptr, "obj");
101}
102
103TEST_F(VerificationTest, DumpValidObjectInfo) {
104 TEST_DISABLED_FOR_MEMORY_TOOL();
Mathieu Chartier1ca68902017-04-18 11:26:22 -0700105 ScopedLogSeverity sls(LogSeverity::INFO);
106 ScopedObjectAccess soa(Thread::Current());
107 Runtime* const runtime = Runtime::Current();
108 VariableSizedHandleScope hs(soa.Self());
109 Handle<mirror::String> string(
110 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "obj")));
111 Handle<mirror::ObjectArray<mirror::Object>> arr(
112 hs.NewHandle(AllocObjectArray<mirror::Object>(soa.Self(), 256)));
113 const Verification* const v = runtime->GetHeap()->GetVerification();
Mathieu Chartier1ca68902017-04-18 11:26:22 -0700114 LOG(INFO) << v->DumpObjectInfo(string.Get(), "test");
115 LOG(INFO) << v->DumpObjectInfo(string->GetClass(), "obj");
116 const uintptr_t uint_klass = reinterpret_cast<uintptr_t>(string->GetClass());
117 LOG(INFO) << v->DumpObjectInfo(reinterpret_cast<const void*>(uint_klass - kObjectAlignment),
118 "obj");
119 LOG(INFO) << v->DumpObjectInfo(reinterpret_cast<const void*>(&uint_klass), "obj");
120 LOG(INFO) << v->DumpObjectInfo(arr.Get(), "arr");
121}
122
123TEST_F(VerificationTest, LogHeapCorruption) {
Mathieu Chartier68dda8f2017-04-24 10:06:15 -0700124 TEST_DISABLED_FOR_MEMORY_TOOL();
Mathieu Chartier1ca68902017-04-18 11:26:22 -0700125 ScopedLogSeverity sls(LogSeverity::INFO);
126 ScopedObjectAccess soa(Thread::Current());
127 Runtime* const runtime = Runtime::Current();
128 VariableSizedHandleScope hs(soa.Self());
129 Handle<mirror::String> string(
130 hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "obj")));
131 using ObjArray = mirror::ObjectArray<mirror::Object>;
132 Handle<ObjArray> arr(
133 hs.NewHandle(AllocObjectArray<mirror::Object>(soa.Self(), 256)));
134 const Verification* const v = runtime->GetHeap()->GetVerification();
135 arr->Set(0, string.Get());
136 // Test normal cases.
137 v->LogHeapCorruption(arr.Get(), ObjArray::DataOffset(kHeapReferenceSize), string.Get(), false);
138 v->LogHeapCorruption(string.Get(), mirror::Object::ClassOffset(), string->GetClass(), false);
139 // Test null holder cases.
140 v->LogHeapCorruption(nullptr, MemberOffset(0), string.Get(), false);
141 v->LogHeapCorruption(nullptr, MemberOffset(0), arr.Get(), false);
142}
143
144} // namespace gc
145} // namespace art