Dmitry Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 1 | /* |
| 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 Gampe | 38cea84 | 2016-11-03 13:06:25 -0700 | [diff] [blame] | 17 | #include <type_traits> |
| 18 | |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 19 | #include "base/enums.h" |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 20 | #include "common_runtime_test.h" |
Dmitry Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 21 | #include "gtest/gtest.h" |
Andreas Gampe | 38cea84 | 2016-11-03 13:06:25 -0700 | [diff] [blame] | 22 | #include "handle.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 23 | #include "handle_scope-inl.h" |
Andreas Gampe | 38cea84 | 2016-11-03 13:06:25 -0700 | [diff] [blame] | 24 | #include "mirror/object.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 25 | #include "scoped_thread_state_change-inl.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 26 | #include "thread.h" |
Dmitry Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
Andreas Gampe | 38cea84 | 2016-11-03 13:06:25 -0700 | [diff] [blame] | 30 | // Handles are value objects and should be trivially copyable. |
| 31 | static_assert(std::is_trivially_copyable<Handle<mirror::Object>>::value, |
| 32 | "Handle should be trivially copyable"); |
| 33 | static_assert(std::is_trivially_copyable<MutableHandle<mirror::Object>>::value, |
| 34 | "MutableHandle should be trivially copyable"); |
| 35 | static_assert(std::is_trivially_copyable<ScopedNullHandle<mirror::Object>>::value, |
| 36 | "ScopedNullHandle should be trivially copyable"); |
| 37 | |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 38 | class HandleScopeTest : public CommonRuntimeTest {}; |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 39 | |
| 40 | // Test the offsets computed for members of HandleScope. Because of cross-compiling |
Dmitry Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 41 | // it is impossible the use OFFSETOF_MEMBER, so we do some reasonable computations ourselves. This |
| 42 | // test checks whether we do the right thing. |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 43 | TEST_F(HandleScopeTest, Offsets) { |
| 44 | ScopedObjectAccess soa(Thread::Current()); |
| 45 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 46 | // As the members of HandleScope are private, we cannot use OFFSETOF_MEMBER |
Dmitry Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 47 | // here. So do the inverse: set some data, and access it through pointers created from the offsets. |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 48 | StackHandleScope<0x1> hs0(soa.Self()); |
| 49 | static const size_t kNumReferences = 0x9ABC; |
| 50 | StackHandleScope<kNumReferences> test_table(soa.Self()); |
| 51 | ObjPtr<mirror::Class> c = class_linker->FindSystemClass(soa.Self(), "Ljava/lang/Object;"); |
| 52 | test_table.SetReference(0, c.Ptr()); |
Dmitry Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 53 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 54 | uint8_t* table_base_ptr = reinterpret_cast<uint8_t*>(&test_table); |
Dmitry Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 55 | |
| 56 | { |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 57 | BaseHandleScope** link_ptr = reinterpret_cast<BaseHandleScope**>(table_base_ptr + |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 58 | HandleScope::LinkOffset(kRuntimePointerSize)); |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 59 | EXPECT_EQ(*link_ptr, &hs0); |
Dmitry Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | { |
| 63 | uint32_t* num_ptr = reinterpret_cast<uint32_t*>(table_base_ptr + |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 64 | HandleScope::NumberOfReferencesOffset(kRuntimePointerSize)); |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 65 | EXPECT_EQ(*num_ptr, static_cast<size_t>(kNumReferences)); |
Dmitry Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | { |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 69 | auto* ref_ptr = reinterpret_cast<StackReference<mirror::Object>*>(table_base_ptr + |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 70 | HandleScope::ReferencesOffset(kRuntimePointerSize)); |
Mathieu Chartier | e8a3c57 | 2016-10-11 16:52:17 -0700 | [diff] [blame] | 71 | EXPECT_OBJ_PTR_EQ(ref_ptr->AsMirrorPtr(), c); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | class CollectVisitor { |
| 76 | public: |
| 77 | void VisitRootIfNonNull(StackReference<mirror::Object>* ref) { |
| 78 | if (!ref->IsNull()) { |
| 79 | visited.insert(ref); |
| 80 | } |
| 81 | ++total_visited; |
| 82 | } |
| 83 | |
| 84 | std::set<StackReference<mirror::Object>*> visited; |
| 85 | size_t total_visited = 0; // including null. |
| 86 | }; |
| 87 | |
| 88 | // Test functionality of variable sized handle scopes. |
| 89 | TEST_F(HandleScopeTest, VariableSized) { |
| 90 | ScopedObjectAccess soa(Thread::Current()); |
| 91 | VariableSizedHandleScope hs(soa.Self()); |
| 92 | ClassLinker* const class_linker = Runtime::Current()->GetClassLinker(); |
| 93 | Handle<mirror::Class> c = |
| 94 | hs.NewHandle(class_linker->FindSystemClass(soa.Self(), "Ljava/lang/Object;")); |
| 95 | // Test nested scopes. |
| 96 | StackHandleScope<1> inner(soa.Self()); |
| 97 | inner.NewHandle(c->AllocObject(soa.Self())); |
| 98 | // Add a bunch of handles and make sure callbacks work. |
| 99 | static const size_t kNumHandles = 100; |
| 100 | std::vector<Handle<mirror::Object>> handles; |
| 101 | for (size_t i = 0; i < kNumHandles; ++i) { |
| 102 | BaseHandleScope* base = &hs; |
| 103 | ObjPtr<mirror::Object> o = c->AllocObject(soa.Self()); |
| 104 | handles.push_back(hs.NewHandle(o)); |
| 105 | EXPECT_OBJ_PTR_EQ(o, handles.back().Get()); |
| 106 | EXPECT_TRUE(hs.Contains(handles.back().GetReference())); |
| 107 | EXPECT_TRUE(base->Contains(handles.back().GetReference())); |
| 108 | EXPECT_EQ(hs.NumberOfReferences(), base->NumberOfReferences()); |
| 109 | } |
| 110 | CollectVisitor visitor; |
| 111 | BaseHandleScope* base = &hs; |
| 112 | base->VisitRoots(visitor); |
| 113 | EXPECT_LE(visitor.visited.size(), base->NumberOfReferences()); |
| 114 | EXPECT_EQ(visitor.total_visited, base->NumberOfReferences()); |
| 115 | for (StackReference<mirror::Object>* ref : visitor.visited) { |
| 116 | EXPECT_TRUE(base->Contains(ref)); |
Dmitry Petrochenko | 135016a | 2014-04-03 14:35:54 +0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
| 120 | } // namespace art |