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 | 5868ada | 2020-05-12 11:50:34 +0100 | [diff] [blame] | 21 | #include "class_root-inl.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" |
Andreas Gampe | 52ecb65 | 2018-10-24 15:18:21 -0700 | [diff] [blame] | 24 | #include "mirror/object_array-alloc-inl.h" |
Andreas Gampe | 5e36c2f | 2017-04-21 19:11:15 -0700 | [diff] [blame] | 25 | #include "mirror/object_array-inl.h" |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 26 | #include "mirror/string.h" |
Andreas Gampe | 5e36c2f | 2017-04-21 19:11:15 -0700 | [diff] [blame] | 27 | #include "runtime.h" |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 28 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | 5e36c2f | 2017-04-21 19:11:15 -0700 | [diff] [blame] | 29 | #include "verification.h" |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 30 | |
| 31 | namespace art { |
| 32 | namespace gc { |
| 33 | |
| 34 | class VerificationTest : public CommonRuntimeTest { |
| 35 | protected: |
| 36 | VerificationTest() {} |
| 37 | |
| 38 | template <class T> |
Vladimir Marko | bcf1752 | 2018-06-01 13:14:32 +0100 | [diff] [blame] | 39 | ObjPtr<mirror::ObjectArray<T>> AllocObjectArray(Thread* self, size_t length) |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 40 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 41 | return mirror::ObjectArray<T>::Alloc( |
| 42 | self, |
Vladimir Marko | b4eb1b1 | 2018-05-24 11:09:38 +0100 | [diff] [blame] | 43 | GetClassRoot<mirror::ObjectArray<mirror::Object>>(), |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 44 | length); |
| 45 | } |
| 46 | }; |
| 47 | |
| 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())); |
Mathieu Chartier | b814ef5 | 2017-06-27 12:56:00 -0700 | [diff] [blame] | 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)); |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 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 | |
Mathieu Chartier | 68dda8f | 2017-04-24 10:06:15 -0700 | [diff] [blame] | 73 | TEST_F(VerificationTest, IsValidClassOrNotInHeap) { |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 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)); |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 82 | EXPECT_TRUE(v->IsValidClass(string->GetClass())); |
Mathieu Chartier | 68dda8f | 2017-04-24 10:06:15 -0700 | [diff] [blame] | 83 | EXPECT_FALSE(v->IsValidClass(string.Get())); |
| 84 | } |
| 85 | |
| 86 | TEST_F(VerificationTest, IsValidClassInHeap) { |
Roland Levillain | 5fe2f34 | 2018-09-07 18:41:49 +0100 | [diff] [blame] | 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(); |
Mathieu Chartier | 68dda8f | 2017-04-24 10:06:15 -0700 | [diff] [blame] | 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(); |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 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 | |
Mathieu Chartier | 68dda8f | 2017-04-24 10:06:15 -0700 | [diff] [blame] | 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 | |
| 114 | TEST_F(VerificationTest, DumpValidObjectInfo) { |
Roland Levillain | 5fe2f34 | 2018-09-07 18:41:49 +0100 | [diff] [blame] | 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(); |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 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(); |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 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 | |
| 140 | TEST_F(VerificationTest, LogHeapCorruption) { |
Roland Levillain | 5fe2f34 | 2018-09-07 18:41:49 +0100 | [diff] [blame] | 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(); |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 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 | |
Mathieu Chartier | 4f5e3cb | 2017-06-12 13:10:01 -0700 | [diff] [blame] | 167 | TEST_F(VerificationTest, FindPathFromRootSet) { |
Mathieu Chartier | 4f5e3cb | 2017-06-12 13:10:01 -0700 | [diff] [blame] | 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 | |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 185 | } // namespace gc |
| 186 | } // namespace art |