• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "ecmascript/ecma_vm.h"
17 #include "ecmascript/global_env.h"
18 #include "ecmascript/js_handle.h"
19 #include "ecmascript/mem/space.h"
20 #include "ecmascript/mem/verification.h"
21 #include "ecmascript/object_factory.h"
22 #include "ecmascript/tagged_array-inl.h"
23 #include "ecmascript/tests/test_helper.h"
24 
25 using namespace panda::ecmascript;
26 
27 namespace panda::test {
28 class HugeObjectTest : public testing::Test {
29 public:
SetUpTestCase()30     static void SetUpTestCase()
31     {
32         GTEST_LOG_(INFO) << "SetUpTestCase";
33     }
34 
TearDownTestCase()35     static void TearDownTestCase()
36     {
37         GTEST_LOG_(INFO) << "TearDownCase";
38     }
39 
SetUp()40     void SetUp() override
41     {
42         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
43         thread->GetEcmaVM()->SetEnableForceGC(false);
44         const_cast<Heap *>(thread->GetEcmaVM()->GetHeap())->SetMarkType(MarkType::FULL_MARK);
45     }
46 
TearDown()47     void TearDown() override
48     {
49         TestHelper::DestroyEcmaVMWithScope(instance, scope);
50     }
51 
52     JSThread *thread {nullptr};
53     PandaVM *instance {nullptr};
54     ecmascript::EcmaHandleScope *scope {nullptr};
55 };
56 
JSObjectTestCreate(JSThread * thread)57 static JSObject *JSObjectTestCreate(JSThread *thread)
58 {
59     [[maybe_unused]] ecmascript::EcmaHandleScope scope(thread);
60     EcmaVM *ecmaVM = thread->GetEcmaVM();
61     auto globalEnv = ecmaVM->GetGlobalEnv();
62     JSHandle<JSTaggedValue> jsFunc = globalEnv->GetObjectFunction();
63     JSHandle<JSObject> newObj =
64         ecmaVM->GetFactory()->NewJSObjectByConstructor(JSHandle<JSFunction>(jsFunc), jsFunc);
65     return *newObj;
66 }
67 
LargeArrayTestCreate(JSThread * thread)68 static TaggedArray *LargeArrayTestCreate(JSThread *thread)
69 {
70     [[maybe_unused]] ecmascript::EcmaHandleScope scope(thread);
71     static constexpr size_t SIZE = 1024 * 1024;
72     JSHandle<TaggedArray> array = thread->GetEcmaVM()->GetFactory()->NewTaggedArray(SIZE);
73     return *array;
74 }
75 
76 
HWTEST_F_L0(HugeObjectTest,LargeArrayKeep)77 HWTEST_F_L0(HugeObjectTest, LargeArrayKeep)
78 {
79     TaggedArray *array = LargeArrayTestCreate(thread);
80     EXPECT_TRUE(array != nullptr);
81     JSHandle<TaggedArray> arrayHandle(thread, array);
82     JSHandle<JSObject> newObj(thread, JSObjectTestCreate(thread));
83     arrayHandle->Set(thread, 0, newObj.GetTaggedValue());
84     auto ecmaVm = thread->GetEcmaVM();
85     EXPECT_EQ(*arrayHandle, reinterpret_cast<TaggedObject *>(array));
86     ecmaVm->CollectGarbage(TriggerGCType::SEMI_GC);   // Trigger GC.
87     ecmaVm->CollectGarbage(TriggerGCType::OLD_GC);  // Trigger GC.
88     EXPECT_EQ(*newObj, array->Get(0).GetTaggedObject());
89     EXPECT_EQ(*arrayHandle, reinterpret_cast<TaggedObject *>(array));
90 }
91 
HWTEST_F_L0(HugeObjectTest,MultipleArrays)92 HWTEST_F_L0(HugeObjectTest, MultipleArrays)
93 {
94     auto ecmaVm = thread->GetEcmaVM();
95     auto heap = ecmaVm->GetHeap();
96     {
97         [[maybe_unused]] ecmascript::EcmaHandleScope scope(thread);
98         for (int i = 0; i <= 20; i++) {
99             JSHandle<TaggedArray> array1(thread, LargeArrayTestCreate(thread));
100         }
101     }
102 
103     {
104         [[maybe_unused]] ecmascript::EcmaHandleScope scope(thread);
105         for (int i = 0; i <= 20; i++) {
106             JSHandle<TaggedArray> array2(thread, LargeArrayTestCreate(thread));
107         }
108     }
109 
110     {
111         [[maybe_unused]] ecmascript::EcmaHandleScope scope(thread);
112         for (int i = 0; i <= 20; i++) {
113             JSHandle<TaggedArray> array2(thread, LargeArrayTestCreate(thread));
114         }
115     }
116     size_t failCount = 0;
117     VerifyObjectVisitor objVerifier(heap, &failCount);
118     heap->GetHugeObjectSpace()->IterateOverObjects(objVerifier);  // newspace reference the old space
119     EXPECT_TRUE(failCount == 0);
120 }
121 }  // namespace panda::test
122