Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 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" |
Andreas Gampe | 5e36c2f | 2017-04-21 19:11:15 -0700 | [diff] [blame] | 18 | |
Mathieu Chartier | 68dda8f | 2017-04-24 10:06:15 -0700 | [diff] [blame] | 19 | #include "base/memory_tool.h" |
Andreas Gampe | 508fdf3 | 2017-06-05 16:42:13 -0700 | [diff] [blame] | 20 | #include "class_linker-inl.h" |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame^] | 21 | #include "class_root.h" |
Andreas Gampe | 5e36c2f | 2017-04-21 19:11:15 -0700 | [diff] [blame] | 22 | #include "handle_scope-inl.h" |
| 23 | #include "mirror/object-inl.h" |
| 24 | #include "mirror/object_array-inl.h" |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 25 | #include "mirror/string.h" |
Andreas Gampe | 5e36c2f | 2017-04-21 19:11:15 -0700 | [diff] [blame] | 26 | #include "runtime.h" |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 27 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | 5e36c2f | 2017-04-21 19:11:15 -0700 | [diff] [blame] | 28 | #include "verification.h" |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | namespace gc { |
| 32 | |
| 33 | class VerificationTest : public CommonRuntimeTest { |
| 34 | protected: |
| 35 | VerificationTest() {} |
| 36 | |
| 37 | template <class T> |
| 38 | mirror::ObjectArray<T>* AllocObjectArray(Thread* self, size_t length) |
| 39 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 40 | return mirror::ObjectArray<T>::Alloc( |
| 41 | self, |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame^] | 42 | GetClassRoot<mirror::ObjectArray<mirror::Object>>(), |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 43 | length); |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | TEST_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 Chartier | b814ef5 | 2017-06-27 12:56:00 -0700 | [diff] [blame] | 57 | // 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 Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 62 | 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 Chartier | 68dda8f | 2017-04-24 10:06:15 -0700 | [diff] [blame] | 72 | TEST_F(VerificationTest, IsValidClassOrNotInHeap) { |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 73 | 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 Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 81 | EXPECT_TRUE(v->IsValidClass(string->GetClass())); |
Mathieu Chartier | 68dda8f | 2017-04-24 10:06:15 -0700 | [diff] [blame] | 82 | EXPECT_FALSE(v->IsValidClass(string.Get())); |
| 83 | } |
| 84 | |
| 85 | TEST_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 Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 92 | 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 Chartier | 68dda8f | 2017-04-24 10:06:15 -0700 | [diff] [blame] | 97 | TEST_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 | |
| 108 | TEST_F(VerificationTest, DumpValidObjectInfo) { |
| 109 | TEST_DISABLED_FOR_MEMORY_TOOL(); |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 110 | 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 Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 119 | 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 | |
| 128 | TEST_F(VerificationTest, LogHeapCorruption) { |
Mathieu Chartier | 68dda8f | 2017-04-24 10:06:15 -0700 | [diff] [blame] | 129 | TEST_DISABLED_FOR_MEMORY_TOOL(); |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 130 | 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 Chartier | 4f5e3cb | 2017-06-12 13:10:01 -0700 | [diff] [blame] | 149 | TEST_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 Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 168 | } // namespace gc |
| 169 | } // namespace art |