1/* 2 * Copyright (c) 2021 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#include "ecmascript/ecma_vm.h" 16#include "ecmascript/global_env.h" 17#include "ecmascript/js_handle.h" 18#include "ecmascript/mem/space.h" 19#include "ecmascript/mem/verification.h" 20#include "ecmascript/object_factory.h" 21#include "ecmascript/tagged_array-inl.h" 22#include "ecmascript/tests/ecma_test_common.h" 23 24using namespace panda::ecmascript; 25 26namespace panda::test { 27class HugeObjectTest : public BaseTestWithScope<false> { 28public: 29 void SetUp() override 30 { 31 TestHelper::CreateEcmaVMWithScope(instance, thread, scope); 32 thread->GetEcmaVM()->SetEnableForceGC(false); 33 const_cast<Heap *>(thread->GetEcmaVM()->GetHeap())->SetMarkType(MarkType::MARK_FULL); 34 } 35}; 36 37static TaggedArray *LargeArrayTestCreate(JSThread *thread) 38{ 39 [[maybe_unused]] ecmascript::EcmaHandleScope scope(thread); 40 static constexpr size_t SIZE = 1ULL << 20UL; 41 JSHandle<TaggedArray> array = thread->GetEcmaVM()->GetFactory()->NewTaggedArray(SIZE); 42 return *array; 43} 44 45HWTEST_F_L0(HugeObjectTest, LargeArrayKeep) 46{ 47 TaggedArray *array = LargeArrayTestCreate(thread); 48 EXPECT_TRUE(array != nullptr); 49 JSHandle<TaggedArray> arrayHandle(thread, array); 50 JSHandle<JSObject> newObj(thread, EcmaContainerCommon::JSObjectTestCreate(thread)); 51 arrayHandle->Set(thread, 0, newObj.GetTaggedValue()); 52 auto ecmaVm = thread->GetEcmaVM(); 53 EXPECT_EQ(*arrayHandle, reinterpret_cast<TaggedObject *>(array)); 54 ecmaVm->CollectGarbage(TriggerGCType::YOUNG_GC); // Trigger GC. 55 ecmaVm->CollectGarbage(TriggerGCType::OLD_GC); // Trigger GC. 56 EXPECT_EQ(*newObj, array->Get(0).GetTaggedObject()); 57 EXPECT_EQ(*arrayHandle, reinterpret_cast<TaggedObject *>(array)); 58} 59 60HWTEST_F_L0(HugeObjectTest, MultipleArrays) 61{ 62 auto ecmaVm = thread->GetEcmaVM(); 63 auto heap = ecmaVm->GetHeap(); 64 { 65 [[maybe_unused]] ecmascript::EcmaHandleScope scope(thread); 66 for (int i = 0; i <= 14; i++) { 67 JSHandle<TaggedArray> array1(thread, LargeArrayTestCreate(thread)); 68 } 69 } 70 71 { 72 [[maybe_unused]] ecmascript::EcmaHandleScope scope(thread); 73 for (int i = 0; i <= 14; i++) { 74 JSHandle<TaggedArray> array2(thread, LargeArrayTestCreate(thread)); 75 } 76 } 77 78 { 79 [[maybe_unused]] ecmascript::EcmaHandleScope scope(thread); 80 for (int i = 0; i <= 14; i++) { 81 JSHandle<TaggedArray> array2(thread, LargeArrayTestCreate(thread)); 82 } 83 } 84 size_t failCount = 0; 85 VerifyObjectVisitor objVerifier(heap, &failCount); 86 heap->GetHugeObjectSpace()->IterateOverObjects(objVerifier); // newspace reference the old space 87 EXPECT_EQ(failCount, 0U); 88} 89} // namespace panda::test 90