• 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/js_handle.h"
18 #include "ecmascript/mem/mem_common.h"
19 #include "ecmascript/mem/space.h"
20 #include "ecmascript/object_factory.h"
21 #include "ecmascript/tagged_array-inl.h"
22 #include "ecmascript/tests/test_helper.h"
23 
24 using namespace panda::ecmascript;
25 
26 namespace panda::test {
27 class ThrowOOMErrorTest : public testing::Test {
28 public:
SetUpTestCase()29     static void SetUpTestCase()
30     {
31         GTEST_LOG_(INFO) << "SetUpTestCase";
32     }
33 
TearDownTestCase()34     static void TearDownTestCase()
35     {
36         GTEST_LOG_(INFO) << "TearDownCase";
37     }
38 
SetUp()39     void SetUp() override
40     {
41         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
42         thread->GetEcmaVM()->SetEnableForceGC(false);
43         const_cast<Heap *>(thread->GetEcmaVM()->GetHeap())->SetMarkType(MarkType::MARK_FULL);
44     }
45 
TearDown()46     void TearDown() override
47     {
48         TestHelper::DestroyEcmaVMWithScope(instance, scope);
49     }
50 
51     JSThread *thread {nullptr};
52     EcmaVM *instance {nullptr};
53     ecmascript::EcmaHandleScope *scope {nullptr};
54 };
55 
HWTEST_F_L0(ThrowOOMErrorTest,ThrowNonMovableOOMError)56 HWTEST_F_L0(ThrowOOMErrorTest, ThrowNonMovableOOMError)
57 {
58     static constexpr size_t SIZE = 100_KB / 8;
59     [[maybe_unused]] ecmascript::EcmaHandleScope scope(thread);
60     for (int i = 0; i < 130; i++) {
61         [[maybe_unused]] JSHandle<TaggedArray> array =
62             thread->GetEcmaVM()->GetFactory()->NewTaggedArray(SIZE, JSTaggedValue::Hole(), MemSpaceType::NON_MOVABLE);
63     }
64 
65     EXPECT_TRUE(thread->HasPendingException());
66     JSType errorType = thread->GetException().GetTaggedObject()->GetClass()->GetObjectType();
67     EXPECT_EQ(errorType, JSType::JS_OOM_ERROR);
68 }
69 
HWTEST_F_L0(ThrowOOMErrorTest,ThrowOldSpaceMergeOOMError)70 HWTEST_F_L0(ThrowOOMErrorTest, ThrowOldSpaceMergeOOMError)
71 {
72     auto ecmaVm = thread->GetEcmaVM();
73     auto heap = const_cast<Heap *>(ecmaVm->GetHeap());
74     auto oldSpace = heap->GetOldSpace();
75     oldSpace->SetMaximumCapacity(6_MB);
76 
77     static constexpr size_t SIZE = 100_KB / 8;
78     [[maybe_unused]] ecmascript::EcmaHandleScope scope(thread);
79 
80     for (int i = 0; i < 46; i++) {
81         [[maybe_unused]] JSHandle<TaggedArray> array =
82             thread->GetEcmaVM()->GetFactory()->NewTaggedArray(SIZE, JSTaggedValue::Hole(), MemSpaceType::OLD_SPACE);
83     }
84 
85     EXPECT_TRUE(!thread->HasPendingException());
86 
87     for (int i = 0; i < 4; i++) {
88         [[maybe_unused]] JSHandle<TaggedArray> array =
89             thread->GetEcmaVM()->GetFactory()->NewTaggedArray(SIZE, JSTaggedValue::Hole(), MemSpaceType::SEMI_SPACE);
90     }
91 
92     heap->CollectGarbage(TriggerGCType::YOUNG_GC);
93     heap->Prepare();
94     heap->CollectGarbage(TriggerGCType::YOUNG_GC);
95     heap->Prepare();
96 
97     EXPECT_TRUE(thread->HasPendingException());
98     JSType errorType = thread->GetException().GetTaggedObject()->GetClass()->GetObjectType();
99     EXPECT_EQ(errorType, JSType::JS_OOM_ERROR);
100 }
101 }  // namespace panda::test
102