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 | |
| 19 | #include "class_linker.h" |
| 20 | #include "handle_scope-inl.h" |
| 21 | #include "mirror/object-inl.h" |
| 22 | #include "mirror/object_array-inl.h" |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 23 | #include "mirror/string.h" |
Andreas Gampe | 5e36c2f | 2017-04-21 19:11:15 -0700 | [diff] [blame^] | 24 | #include "runtime.h" |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 25 | #include "scoped_thread_state_change-inl.h" |
Andreas Gampe | 5e36c2f | 2017-04-21 19:11:15 -0700 | [diff] [blame^] | 26 | #include "verification.h" |
Mathieu Chartier | 1ca6890 | 2017-04-18 11:26:22 -0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | namespace gc { |
| 30 | |
| 31 | class VerificationTest : public CommonRuntimeTest { |
| 32 | protected: |
| 33 | VerificationTest() {} |
| 34 | |
| 35 | template <class T> |
| 36 | mirror::ObjectArray<T>* AllocObjectArray(Thread* self, size_t length) |
| 37 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 38 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
| 39 | return mirror::ObjectArray<T>::Alloc( |
| 40 | self, |
| 41 | class_linker->GetClassRoot(ClassLinker::ClassRoot::kObjectArrayClass), |
| 42 | length); |
| 43 | } |
| 44 | }; |
| 45 | |
| 46 | TEST_F(VerificationTest, IsValidHeapObjectAddress) { |
| 47 | ScopedObjectAccess soa(Thread::Current()); |
| 48 | const Verification* const v = Runtime::Current()->GetHeap()->GetVerification(); |
| 49 | EXPECT_FALSE(v->IsValidHeapObjectAddress(reinterpret_cast<const void*>(1))); |
| 50 | EXPECT_FALSE(v->IsValidHeapObjectAddress(reinterpret_cast<const void*>(4))); |
| 51 | EXPECT_FALSE(v->IsValidHeapObjectAddress(nullptr)); |
| 52 | VariableSizedHandleScope hs(soa.Self()); |
| 53 | Handle<mirror::String> string( |
| 54 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "test"))); |
| 55 | EXPECT_TRUE(v->IsValidHeapObjectAddress(string.Get())); |
| 56 | EXPECT_TRUE(v->IsValidHeapObjectAddress(string->GetClass())); |
| 57 | const uintptr_t uint_klass = reinterpret_cast<uintptr_t>(string->GetClass()); |
| 58 | // Not actually a valid object but the verification can't know that. Guaranteed to be inside a |
| 59 | // heap space. |
| 60 | EXPECT_TRUE(v->IsValidHeapObjectAddress( |
| 61 | reinterpret_cast<const void*>(uint_klass + kObjectAlignment))); |
| 62 | EXPECT_FALSE(v->IsValidHeapObjectAddress( |
| 63 | reinterpret_cast<const void*>(&uint_klass))); |
| 64 | } |
| 65 | |
| 66 | TEST_F(VerificationTest, IsValidClass) { |
| 67 | ScopedObjectAccess soa(Thread::Current()); |
| 68 | VariableSizedHandleScope hs(soa.Self()); |
| 69 | Handle<mirror::String> string( |
| 70 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "test"))); |
| 71 | const Verification* const v = Runtime::Current()->GetHeap()->GetVerification(); |
| 72 | EXPECT_FALSE(v->IsValidClass(reinterpret_cast<const void*>(1))); |
| 73 | EXPECT_FALSE(v->IsValidClass(reinterpret_cast<const void*>(4))); |
| 74 | EXPECT_FALSE(v->IsValidClass(nullptr)); |
| 75 | EXPECT_FALSE(v->IsValidClass(string.Get())); |
| 76 | EXPECT_TRUE(v->IsValidClass(string->GetClass())); |
| 77 | const uintptr_t uint_klass = reinterpret_cast<uintptr_t>(string->GetClass()); |
| 78 | EXPECT_FALSE(v->IsValidClass(reinterpret_cast<const void*>(uint_klass - kObjectAlignment))); |
| 79 | EXPECT_FALSE(v->IsValidClass(reinterpret_cast<const void*>(&uint_klass))); |
| 80 | } |
| 81 | |
| 82 | TEST_F(VerificationTest, DumpObjectInfo) { |
| 83 | ScopedLogSeverity sls(LogSeverity::INFO); |
| 84 | ScopedObjectAccess soa(Thread::Current()); |
| 85 | Runtime* const runtime = Runtime::Current(); |
| 86 | VariableSizedHandleScope hs(soa.Self()); |
| 87 | Handle<mirror::String> string( |
| 88 | hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), "obj"))); |
| 89 | Handle<mirror::ObjectArray<mirror::Object>> arr( |
| 90 | hs.NewHandle(AllocObjectArray<mirror::Object>(soa.Self(), 256))); |
| 91 | const Verification* const v = runtime->GetHeap()->GetVerification(); |
| 92 | LOG(INFO) << v->DumpObjectInfo(reinterpret_cast<const void*>(1), "obj"); |
| 93 | LOG(INFO) << v->DumpObjectInfo(reinterpret_cast<const void*>(4), "obj"); |
| 94 | LOG(INFO) << v->DumpObjectInfo(nullptr, "obj"); |
| 95 | LOG(INFO) << v->DumpObjectInfo(string.Get(), "test"); |
| 96 | LOG(INFO) << v->DumpObjectInfo(string->GetClass(), "obj"); |
| 97 | const uintptr_t uint_klass = reinterpret_cast<uintptr_t>(string->GetClass()); |
| 98 | LOG(INFO) << v->DumpObjectInfo(reinterpret_cast<const void*>(uint_klass - kObjectAlignment), |
| 99 | "obj"); |
| 100 | LOG(INFO) << v->DumpObjectInfo(reinterpret_cast<const void*>(&uint_klass), "obj"); |
| 101 | LOG(INFO) << v->DumpObjectInfo(arr.Get(), "arr"); |
| 102 | } |
| 103 | |
| 104 | TEST_F(VerificationTest, LogHeapCorruption) { |
| 105 | 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 | using ObjArray = mirror::ObjectArray<mirror::Object>; |
| 112 | Handle<ObjArray> arr( |
| 113 | hs.NewHandle(AllocObjectArray<mirror::Object>(soa.Self(), 256))); |
| 114 | const Verification* const v = runtime->GetHeap()->GetVerification(); |
| 115 | arr->Set(0, string.Get()); |
| 116 | // Test normal cases. |
| 117 | v->LogHeapCorruption(arr.Get(), ObjArray::DataOffset(kHeapReferenceSize), string.Get(), false); |
| 118 | v->LogHeapCorruption(string.Get(), mirror::Object::ClassOffset(), string->GetClass(), false); |
| 119 | // Test null holder cases. |
| 120 | v->LogHeapCorruption(nullptr, MemberOffset(0), string.Get(), false); |
| 121 | v->LogHeapCorruption(nullptr, MemberOffset(0), arr.Get(), false); |
| 122 | } |
| 123 | |
| 124 | } // namespace gc |
| 125 | } // namespace art |