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