1 /*
2 * Copyright (c) 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 "ecmascript/builtins/builtins_ark_tools.h"
17 #include "ecmascript/ecma_vm.h"
18 #include "ecmascript/object_factory-inl.h"
19 #include "ecmascript/mem/concurrent_marker.h"
20 #include "ecmascript/tests/ecma_test_common.h"
21 #include "ecmascript/mem/verification.h"
22 #include "ecmascript/mem/partial_gc.h"
23 #include "ecmascript/mem/full_gc.h"
24
25 using namespace panda;
26
27 using namespace panda::ecmascript;
28
29 //In this test case objects cannot be created frequently, because of the HEAP_VERIFY switch is turned on,
30 //which is easy to cause the test cases to time out.
31 namespace panda::test {
32 class GCTest : public BaseTestWithScope<false> {
33 public:
SetUp()34 void SetUp() override
35 {
36 JSRuntimeOptions options;
37 options.SetArkProperties(options.GetArkProperties() | ArkProperties::ENABLE_HEAP_VERIFY);
38 instance = JSNApi::CreateEcmaVM(options);
39 ASSERT_TRUE(instance != nullptr) << "Cannot create EcmaVM";
40 thread = instance->GetJSThread();
41 thread->ManagedCodeBegin();
42 scope = new EcmaHandleScope(thread);
43 auto heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
44 heap->GetConcurrentMarker()->EnableConcurrentMarking(EnableConcurrentMarkType::ENABLE);
45 heap->GetSweeper()->EnableConcurrentSweep(EnableConcurrentSweepType::ENABLE);
46 }
47 };
48
HWTEST_F_L0(GCTest,VerificationTest2)49 HWTEST_F_L0(GCTest, VerificationTest2)
50 {
51 Heap *heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
52 heap->SetMarkType(MarkType::MARK_YOUNG);
53 auto partialGc = heap->GetPartialGC();
54 partialGc->RunPhases();
55 }
56
HWTEST_F_L0(GCTest,VerificationTest3)57 HWTEST_F_L0(GCTest, VerificationTest3)
58 {
59 Heap *heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
60 heap->SetMarkType(MarkType::MARK_FULL);
61 auto partialGc = heap->GetPartialGC();
62 partialGc->RunPhases();
63 }
64
HWTEST_F_L0(GCTest,VerificationTest4)65 HWTEST_F_L0(GCTest, VerificationTest4)
66 {
67 Heap *heap = const_cast<Heap *>(thread->GetEcmaVM()->GetHeap());
68 auto fullGc = heap->GetFullGC();
69 fullGc->RunPhases();
70 }
71
HWTEST_F_L0(GCTest,SharedHeapVerificationTest)72 HWTEST_F_L0(GCTest, SharedHeapVerificationTest)
73 {
74 SharedHeap *sHeap = SharedHeap::GetInstance();
75 JSRuntimeOptions options;
76 options.SetArkProperties(options.GetArkProperties() | ArkProperties::ENABLE_HEAP_VERIFY);
77 auto nativeAreaAllocator_ = std::make_unique<NativeAreaAllocator>();
78 auto heapRegionAllocator_ = std::make_unique<HeapRegionAllocator>();
79 sHeap->Initialize(nativeAreaAllocator_.get(), heapRegionAllocator_.get(), options, DaemonThread::GetInstance());
80 ObjectFactory *factory = thread->GetEcmaVM()->GetFactory();
81 sHeap->CollectGarbage<TriggerGCType::SHARED_GC, GCReason::OTHER>(thread);
82 auto oldSizebase = sHeap->GetOldSpace()->GetHeapObjectSize();
83 {
84 [[maybe_unused]] ecmascript::EcmaHandleScope baseScope(thread);
85 factory->NewSOldSpaceTaggedArray(1024, JSTaggedValue::Undefined());
86 }
87 size_t oldSizeBefore = sHeap->GetOldSpace()->GetHeapObjectSize();
88 EXPECT_TRUE(oldSizeBefore > oldSizebase);
89 sHeap->CollectGarbage<TriggerGCType::SHARED_GC, GCReason::OTHER>(thread);
90 auto oldSizeAfter = sHeap->GetOldSpace()->GetHeapObjectSize();
91 EXPECT_TRUE(oldSizeBefore > oldSizeAfter);
92 }
93 } // namespace panda::test
94