• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <sys/mman.h>
17 
18 #include "libpandabase/mem/mem.h"
19 #include "libpandabase/os/mem.h"
20 #include "libpandabase/utils/asan_interface.h"
21 #include "libpandabase/utils/logger.h"
22 #include "libpandabase/utils/math_helpers.h"
23 #include "runtime/include/runtime.h"
24 #include "runtime/mem/alloc_config.h"
25 #include "runtime/mem/region_allocator-inl.h"
26 #include "runtime/mem/tlab.h"
27 #include "runtime/tests/allocator_test_base.h"
28 
29 #include "runtime/mem/rem_set-inl.h"
30 #include "runtime/mem/region_space.h"
31 #include "runtime/mem/gc/gc.h"
32 
33 namespace ark::mem::test {
34 
35 using NonObjectRegionAllocator = RegionAllocator<EmptyAllocConfigWithCrossingMap>;
36 using RemSetWithCommonLock = RemSet<RemSetLockConfig::CommonLock>;
37 
38 class RemSetTest : public testing::Test {
39 public:
RemSetTest()40     RemSetTest()
41     {
42         options_.SetShouldLoadBootPandaFiles(false);
43         options_.SetShouldInitializeIntrinsics(false);
44         Runtime::Create(options_);
45         // For tests we don't limit spaces
46         size_t spaceSize = options_.GetHeapSizeLimit();
47         spaces_.youngSpace_.Initialize(spaceSize, spaceSize);
48         spaces_.memSpace_.Initialize(spaceSize, spaceSize);
49         // NOLINTNEXTLINE(readability-magic-numbers)
50         spaces_.InitializePercentages(0, 100U);
51         spaces_.isInitialized_ = true;
52         thread_ = ark::MTManagedThread::GetCurrent();
53         thread_->ManagedCodeBegin();
54         Init();
55     }
56 
~RemSetTest()57     ~RemSetTest() override
58     {
59         thread_->ManagedCodeEnd();
60         cardTable_ = nullptr;
61         Runtime::Destroy();
62     }
63 
64     NO_COPY_SEMANTIC(RemSetTest);
65     NO_MOVE_SEMANTIC(RemSetTest);
66 
Init()67     void Init()
68     {
69         auto lang = Runtime::GetCurrent()->GetLanguageContext(panda_file::SourceLang::PANDA_ASSEMBLY);
70         ext_ = Runtime::GetCurrent()->GetClassLinker()->GetExtension(lang);
71         cardTable_ = MakePandaUnique<CardTable>(Runtime::GetCurrent()->GetInternalAllocator(),
72                                                 PoolManager::GetMmapMemPool()->GetMinObjectAddress(),
73                                                 PoolManager::GetMmapMemPool()->GetTotalObjectSize());
74         cardTable_->Initialize();
75     }
76 
77 private:
78     ark::MTManagedThread *thread_ {};
79     RuntimeOptions options_;
80     PandaUniquePtr<CardTable> cardTable_ {nullptr};
81 
82 protected:
83     // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
84     ClassLinkerExtension *ext_ {};
85     // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
86     GenerationalSpaces spaces_;
87 };
88 
TEST_F(RemSetTest,AddRefTest)89 TEST_F(RemSetTest, AddRefTest)
90 {
91     auto *memStats = new mem::MemStatsType();
92     NonObjectRegionAllocator allocator(memStats, &spaces_);
93     auto cls = ext_->CreateClass(nullptr, 0, 0, sizeof(ark::Class));
94     cls->SetObjectSize(allocator.GetMaxRegularObjectSize());
95 
96     auto obj1 = static_cast<ObjectHeader *>(allocator.Alloc(allocator.GetMaxRegularObjectSize()));
97     obj1->SetClass(cls);
98     auto region1 = ObjectToRegion(obj1);
99 
100     auto obj2 = static_cast<ObjectHeader *>(allocator.Alloc(allocator.GetMaxRegularObjectSize()));
101     obj2->SetClass(cls);
102     auto region2 = ObjectToRegion(obj2);
103 
104     // simulate gc process: mark obj2 and update live bitmap with mark bitmap
105     region2->GetMarkBitmap()->Set(obj2);
106     region2->CreateLiveBitmap();
107     region2->SwapMarkBitmap();
108 
109     auto remset1 = region1->GetRemSet();
110     remset1->AddRef(obj2, 0);
111 
112     PandaVector<void *> testList;
113     auto visitor = [&testList](ObjectHeader *obj) { testList.push_back(obj); };
114     remset1->IterateOverObjects(visitor);
115     ASSERT_EQ(testList.size(), 1);
116     auto first = testList.front();
117     ASSERT_EQ(first, obj2);
118 
119     ext_->FreeClass(cls);
120     delete memStats;
121 }
122 
TEST_F(RemSetTest,AddRefWithAddrTest)123 TEST_F(RemSetTest, AddRefWithAddrTest)
124 {
125     auto *memStats = new mem::MemStatsType();
126     NonObjectRegionAllocator allocator(memStats, &spaces_);
127     auto cls = ext_->CreateClass(nullptr, 0, 0, sizeof(ark::Class));
128     cls->SetObjectSize(allocator.GetMaxRegularObjectSize());
129 
130     auto obj1 = static_cast<ObjectHeader *>(allocator.Alloc(allocator.GetMaxRegularObjectSize()));
131     obj1->SetClass(cls);
132     auto region1 = ObjectToRegion(obj1);
133 
134     auto obj2 = static_cast<ObjectHeader *>(allocator.Alloc(allocator.GetMaxRegularObjectSize()));
135     obj2->SetClass(cls);
136     auto region2 = ObjectToRegion(obj2);
137 
138     region1->CreateLiveBitmap()->Set(obj1);
139 
140     RemSetWithCommonLock::AddRefWithAddr(obj1, 0, obj2);
141     auto remset2 = region2->GetRemSet();
142 
143     PandaVector<void *> testList;
144     auto visitor = [&testList](void *obj) { testList.push_back(obj); };
145     remset2->IterateOverObjects(visitor);
146     ASSERT_EQ(1, testList.size());
147 
148     auto first = testList.front();
149     ASSERT_EQ(first, obj1);
150 
151     ext_->FreeClass(cls);
152     delete memStats;
153 }
154 
TEST_F(RemSetTest,TravelObjectToAddRefTest)155 TEST_F(RemSetTest, TravelObjectToAddRefTest)
156 {
157     auto *memStats = new mem::MemStatsType();
158     NonObjectRegionAllocator allocator(memStats, &spaces_);
159     auto cls = ext_->CreateClass(nullptr, 0, 0, sizeof(ark::Class));
160     cls->SetObjectSize(allocator.GetMaxRegularObjectSize());
161     cls->SetRefFieldsNum(1, false);
162     auto offset = ObjectHeader::ObjectHeaderSize();
163     cls->SetRefFieldsOffset(offset, false);
164 
165     auto obj1 = static_cast<ObjectHeader *>(allocator.Alloc(allocator.GetMaxRegularObjectSize()));
166     obj1->SetClass(cls);
167     auto region1 = ObjectToRegion(obj1);
168 
169     auto obj2 = static_cast<ObjectHeader *>(allocator.Alloc(allocator.GetMaxRegularObjectSize()));
170     obj2->SetClass(cls);
171     auto region2 = ObjectToRegion(obj2);
172 
173     // simulate gc process: mark obj2 and update live bitmap with mark bitmap
174     region1->CreateLiveBitmap()->Set(obj1);
175 
176     static_cast<ObjectHeader *>(obj1)->SetFieldObject<false, false>(offset, static_cast<ObjectHeader *>(obj2));
177 
178     auto traverseObjectVisitor = [](ObjectHeader *fromObject, ObjectHeader *objectToTraverse) {
179         RemSetWithCommonLock::AddRefWithAddr(fromObject, 0, objectToTraverse);
180     };
181     GCStaticObjectHelpers::TraverseAllObjects(obj1, traverseObjectVisitor);
182     auto remset2 = region2->GetRemSet();
183 
184     PandaVector<void *> testList;
185     auto visitor = [&testList](void *obj) { testList.push_back(obj); };
186     remset2->IterateOverObjects(visitor);
187     ASSERT_EQ(1, testList.size());
188 
189     auto first = testList.front();
190     ASSERT_EQ(first, obj1);
191 
192     ext_->FreeClass(cls);
193     delete memStats;
194 }
195 
196 }  // namespace ark::mem::test
197