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