blob: d72dbe69986cfece2dc184835b113ab5be0f914f [file] [log] [blame]
Dmitry Petrochenko135016a2014-04-03 14:35:54 +07001/*
2 * Copyright (C) 2014 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
Andreas Gampe38cea842016-11-03 13:06:25 -070017#include <type_traits>
18
Andreas Gampe542451c2016-07-26 09:02:02 -070019#include "base/enums.h"
Andreas Gampe90b936d2017-01-31 08:58:55 -080020#include "class_linker-inl.h"
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070021#include "common_runtime_test.h"
Dmitry Petrochenko135016a2014-04-03 14:35:54 +070022#include "gtest/gtest.h"
Andreas Gampe38cea842016-11-03 13:06:25 -070023#include "handle.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070024#include "handle_scope-inl.h"
Andreas Gampe70f5fd02018-10-24 19:58:37 -070025#include "mirror/class-alloc-inl.h"
Andreas Gampe90b936d2017-01-31 08:58:55 -080026#include "mirror/class-inl.h"
Andreas Gampe38cea842016-11-03 13:06:25 -070027#include "mirror/object.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070028#include "scoped_thread_state_change-inl.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070029#include "thread.h"
Dmitry Petrochenko135016a2014-04-03 14:35:54 +070030
31namespace art {
32
Andreas Gampe38cea842016-11-03 13:06:25 -070033// Handles are value objects and should be trivially copyable.
34static_assert(std::is_trivially_copyable<Handle<mirror::Object>>::value,
35 "Handle should be trivially copyable");
36static_assert(std::is_trivially_copyable<MutableHandle<mirror::Object>>::value,
37 "MutableHandle should be trivially copyable");
38static_assert(std::is_trivially_copyable<ScopedNullHandle<mirror::Object>>::value,
39 "ScopedNullHandle should be trivially copyable");
40
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070041class HandleScopeTest : public CommonRuntimeTest {};
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070042
43// Test the offsets computed for members of HandleScope. Because of cross-compiling
Dmitry Petrochenko135016a2014-04-03 14:35:54 +070044// it is impossible the use OFFSETOF_MEMBER, so we do some reasonable computations ourselves. This
45// test checks whether we do the right thing.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070046TEST_F(HandleScopeTest, Offsets) {
47 ScopedObjectAccess soa(Thread::Current());
48 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070049 // As the members of HandleScope are private, we cannot use OFFSETOF_MEMBER
Dmitry Petrochenko135016a2014-04-03 14:35:54 +070050 // here. So do the inverse: set some data, and access it through pointers created from the offsets.
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070051 StackHandleScope<0x1> hs0(soa.Self());
52 static const size_t kNumReferences = 0x9ABC;
53 StackHandleScope<kNumReferences> test_table(soa.Self());
54 ObjPtr<mirror::Class> c = class_linker->FindSystemClass(soa.Self(), "Ljava/lang/Object;");
55 test_table.SetReference(0, c.Ptr());
Dmitry Petrochenko135016a2014-04-03 14:35:54 +070056
Ian Rogers13735952014-10-08 12:43:28 -070057 uint8_t* table_base_ptr = reinterpret_cast<uint8_t*>(&test_table);
Dmitry Petrochenko135016a2014-04-03 14:35:54 +070058
59 {
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070060 BaseHandleScope** link_ptr = reinterpret_cast<BaseHandleScope**>(table_base_ptr +
Andreas Gampe542451c2016-07-26 09:02:02 -070061 HandleScope::LinkOffset(kRuntimePointerSize));
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070062 EXPECT_EQ(*link_ptr, &hs0);
Dmitry Petrochenko135016a2014-04-03 14:35:54 +070063 }
64
65 {
66 uint32_t* num_ptr = reinterpret_cast<uint32_t*>(table_base_ptr +
Andreas Gampe542451c2016-07-26 09:02:02 -070067 HandleScope::NumberOfReferencesOffset(kRuntimePointerSize));
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070068 EXPECT_EQ(*num_ptr, static_cast<size_t>(kNumReferences));
Dmitry Petrochenko135016a2014-04-03 14:35:54 +070069 }
70
71 {
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070072 auto* ref_ptr = reinterpret_cast<StackReference<mirror::Object>*>(table_base_ptr +
Andreas Gampe542451c2016-07-26 09:02:02 -070073 HandleScope::ReferencesOffset(kRuntimePointerSize));
Mathieu Chartiere8a3c572016-10-11 16:52:17 -070074 EXPECT_OBJ_PTR_EQ(ref_ptr->AsMirrorPtr(), c);
75 }
76}
77
78class CollectVisitor {
79 public:
80 void VisitRootIfNonNull(StackReference<mirror::Object>* ref) {
81 if (!ref->IsNull()) {
82 visited.insert(ref);
83 }
84 ++total_visited;
85 }
86
87 std::set<StackReference<mirror::Object>*> visited;
88 size_t total_visited = 0; // including null.
89};
90
91// Test functionality of variable sized handle scopes.
92TEST_F(HandleScopeTest, VariableSized) {
93 ScopedObjectAccess soa(Thread::Current());
94 VariableSizedHandleScope hs(soa.Self());
95 ClassLinker* const class_linker = Runtime::Current()->GetClassLinker();
96 Handle<mirror::Class> c =
97 hs.NewHandle(class_linker->FindSystemClass(soa.Self(), "Ljava/lang/Object;"));
98 // Test nested scopes.
99 StackHandleScope<1> inner(soa.Self());
100 inner.NewHandle(c->AllocObject(soa.Self()));
101 // Add a bunch of handles and make sure callbacks work.
102 static const size_t kNumHandles = 100;
103 std::vector<Handle<mirror::Object>> handles;
104 for (size_t i = 0; i < kNumHandles; ++i) {
105 BaseHandleScope* base = &hs;
106 ObjPtr<mirror::Object> o = c->AllocObject(soa.Self());
107 handles.push_back(hs.NewHandle(o));
108 EXPECT_OBJ_PTR_EQ(o, handles.back().Get());
109 EXPECT_TRUE(hs.Contains(handles.back().GetReference()));
110 EXPECT_TRUE(base->Contains(handles.back().GetReference()));
111 EXPECT_EQ(hs.NumberOfReferences(), base->NumberOfReferences());
112 }
113 CollectVisitor visitor;
114 BaseHandleScope* base = &hs;
115 base->VisitRoots(visitor);
116 EXPECT_LE(visitor.visited.size(), base->NumberOfReferences());
117 EXPECT_EQ(visitor.total_visited, base->NumberOfReferences());
118 for (StackReference<mirror::Object>* ref : visitor.visited) {
119 EXPECT_TRUE(base->Contains(ref));
Dmitry Petrochenko135016a2014-04-03 14:35:54 +0700120 }
121}
122
123} // namespace art