• 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_string.h"
17 #include "ecmascript/ecma_vm.h"
18 #include "ecmascript/js_hclass.h"
19 #include "ecmascript/js_object-inl.h"
20 #include "ecmascript/js_thread.h"
21 
22 #include "ecmascript/mem/heap.h"
23 #include "ecmascript/mem/space.h"
24 #include "ecmascript/mem/verification.h"
25 #include "ecmascript/object_factory.h"
26 #include "ecmascript/tagged_array-inl.h"
27 #include "ecmascript/tests/test_helper.h"
28 
29 using namespace panda::ecmascript;
30 using namespace panda::ecmascript::base;
31 
32 namespace panda::test {
33 class JSVerificationTest : public testing::Test {
34 public:
SetUpTestCase()35     static void SetUpTestCase()
36     {
37         GTEST_LOG_(INFO) << "SetUpTestCase";
38     }
39 
TearDownTestCase()40     static void TearDownTestCase()
41     {
42         GTEST_LOG_(INFO) << "TearDownCase";
43     }
44 
SetUp()45     void SetUp() override
46     {
47         TestHelper::CreateEcmaVMWithScope(instance, thread, scope);
48     }
49 
TearDown()50     void TearDown() override
51     {
52         TestHelper::DestroyEcmaVMWithScope(instance, scope);
53     }
54 
55     EcmaVM *instance {nullptr};
56     EcmaHandleScope *scope {nullptr};
57     JSThread *thread {nullptr};
58 };
59 
HWTEST_F_L0(JSVerificationTest,ContainObject)60 HWTEST_F_L0(JSVerificationTest, ContainObject)
61 {
62     auto ecmaVm = thread->GetEcmaVM();
63     auto heap = const_cast<Heap *>(ecmaVm->GetHeap());
64     auto objectFactory = ecmaVm->GetFactory();
65     auto verifier = Verification(heap);
66 
67     auto funcVerify = [](TaggedObject *object, [[maybe_unused]] Verification &v, const Heap *heap) {
68         EXPECT_TRUE(heap->ContainObject(object));
69         EXPECT_TRUE(heap->IsAlive(object));
70     };
71 
72     // new space object
73     JSHandle<EcmaString> string = objectFactory->NewFromASCII("123");
74     funcVerify(*string, verifier, heap);
75 
76     // old space object
77     auto oldArray = objectFactory->NewTaggedArray(2, JSTaggedValue::Undefined(), MemSpaceType::OLD_SPACE);
78     funcVerify(*oldArray, verifier, heap);
79 
80     // no movable object
81     auto nonMovableArray = objectFactory->NewTaggedArray(2, JSTaggedValue::Undefined(), MemSpaceType::NON_MOVABLE);
82     funcVerify(*nonMovableArray, verifier, heap);
83 }
84 
HWTEST_F_L0(JSVerificationTest,VerifyHeapObjects)85 HWTEST_F_L0(JSVerificationTest, VerifyHeapObjects)
86 {
87     auto ecmaVm = thread->GetEcmaVM();
88     auto heap = const_cast<Heap *>(ecmaVm->GetHeap());
89     auto objectFactory = ecmaVm->GetFactory();
90     EXPECT_EQ(heap->VerifyHeapObjects(), 0U);  // failcount is 0
91 
92     JSTaggedValue oldArray;
93     auto verifier = Verification(heap);
94     {
95         EcmaHandleScope handleScope(thread);
96         auto newArray = objectFactory->NewTaggedArray(1, JSTaggedValue::Undefined(), MemSpaceType::SEMI_SPACE);
97 
98         oldArray =
99             (objectFactory->NewTaggedArray(1, JSTaggedValue::Undefined(), MemSpaceType::NON_MOVABLE)).GetTaggedValue();
100         newArray->Set(thread, 0, oldArray);
101     }
102     heap->CollectGarbage(panda::ecmascript::TriggerGCType::OLD_GC);
103     EXPECT_EQ(verifier.VerifyRoot(), 0U);
104     size_t failCount = 0;
105     VerifyObjectVisitor objVerifier(heap, &failCount);
106     const_cast<SemiSpace *>(heap->GetNewSpace())->IterateOverObjects(objVerifier);  // newspace reference the old space
107 }
108 }  // namespace panda::test
109