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 <gtest/gtest.h>
17
18 #include "runtime/include/runtime.h"
19 #include "runtime/include/panda_vm.h"
20 #include "runtime/include/class_linker.h"
21 #include "runtime/include/thread_scopes.h"
22 #include "runtime/mem/vm_handle.h"
23 #include "runtime/handle_scope-inl.h"
24 #include "runtime/include/coretypes/array.h"
25 #include "runtime/include/coretypes/string.h"
26 #include "runtime/mem/gc/card_table.h"
27 #include "runtime/mem/gc/g1/g1-allocator.h"
28 #include "runtime/mem/rem_set-inl.h"
29 #include "runtime/mem/region_space.h"
30 #include "runtime/mem/object_helpers.h"
31
32 #include "test_utils.h"
33
34 namespace ark::mem {
35
36 class IntrusiveGCTestApiTest : public testing::TestWithParam<const char *> {
37 public:
SetUp()38 void SetUp() override
39 {
40 RuntimeOptions options;
41 options.SetLoadRuntimes({"core"});
42 options.SetGcType(GetParam());
43 options.SetRunGcInPlace(true);
44 options.SetCompilerEnableJit(false);
45 options.SetGcWorkersCount(0);
46 options.SetShouldLoadBootPandaFiles(false);
47 options.SetShouldInitializeIntrinsics(false);
48
49 Runtime::Create(options);
50 }
51
TearDown()52 void TearDown() override
53 {
54 Runtime::Destroy();
55 }
56 };
57
TEST_P(IntrusiveGCTestApiTest,TestMarkQueue)58 TEST_P(IntrusiveGCTestApiTest, TestMarkQueue)
59 {
60 ManagedThread *thread = ManagedThread::GetCurrent();
61 PandaVM *vm = Runtime::GetCurrent()->GetPandaVM();
62 GC *gc = vm->GetGC();
63 ScopedManagedCodeThread s(thread);
64 ObjectAllocator objectAllocator;
65 ObjectHeader *object = objectAllocator.AllocObjectInYoung();
66 vm->MarkObject(object);
67 size_t count = 0;
68 bool found = false;
69 vm->IterateOverMarkQueue([&count, &found, object](ObjectHeader *obj) {
70 ++count;
71 if (object == obj) {
72 found = true;
73 }
74 });
75 ASSERT_EQ(1U, count);
76 ASSERT_TRUE(found);
77
78 [[maybe_unused]] HandleScope<ObjectHeader *> scope(thread);
79 VMHandle<ObjectHeader> objHandle(thread, object);
80 {
81 ScopedNativeCodeThread sn(thread);
82 GCTask task(GCTaskCause::YOUNG_GC_CAUSE);
83 task.Run(*gc);
84 }
85
86 object = objHandle.GetPtr(); // GC may move the object
87 found = false;
88 vm->IterateOverMarkQueue([&found, object](ObjectHeader *obj) {
89 if (object == obj) {
90 found = true;
91 }
92 });
93 ASSERT_TRUE(found);
94
95 vm->ClearMarkQueue();
96 count = 0;
97 vm->IterateOverMarkQueue([&count]([[maybe_unused]] ObjectHeader *obj) { ++count; });
98 ASSERT_EQ(0, count);
99 }
100
101 INSTANTIATE_TEST_SUITE_P(IntrusiveGCTestAPITestSuite, IntrusiveGCTestApiTest,
102 testing::Values("stw", "gen-gc", "g1-gc"));
103
104 } // namespace ark::mem
105