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 BaseTestWithScope<false> {
34 };
35
HWTEST_F_L0(JSVerificationTest,ContainObject)36 HWTEST_F_L0(JSVerificationTest, ContainObject)
37 {
38 auto ecmaVm = thread->GetEcmaVM();
39 auto heap = const_cast<Heap *>(ecmaVm->GetHeap());
40 auto objectFactory = ecmaVm->GetFactory();
41 auto verifier = Verification(heap);
42
43 auto funcVerify = [](TaggedObject *object, [[maybe_unused]] Verification &v, const Heap *heap) {
44 EXPECT_TRUE(heap->ContainObject(object));
45 EXPECT_TRUE(heap->IsAlive(object));
46 };
47
48 // new space object
49 JSHandle<EcmaString> string = objectFactory->NewFromASCII("123");
50 funcVerify(*string, verifier, heap);
51
52 // old space object
53 auto oldArray = objectFactory->NewTaggedArray(2, JSTaggedValue::Undefined(), MemSpaceType::OLD_SPACE);
54 funcVerify(*oldArray, verifier, heap);
55
56 // no movable object
57 auto nonMovableArray = objectFactory->NewTaggedArray(2, JSTaggedValue::Undefined(), MemSpaceType::NON_MOVABLE);
58 funcVerify(*nonMovableArray, verifier, heap);
59 }
60
HWTEST_F_L0(JSVerificationTest,VerifyHeapObjects)61 HWTEST_F_L0(JSVerificationTest, VerifyHeapObjects)
62 {
63 auto ecmaVm = thread->GetEcmaVM();
64 auto heap = const_cast<Heap *>(ecmaVm->GetHeap());
65 auto objectFactory = ecmaVm->GetFactory();
66 EXPECT_EQ(heap->VerifyHeapObjects(), 0U); // failcount is 0
67
68 JSTaggedValue oldArray;
69 auto verifier = Verification(heap);
70 {
71 EcmaHandleScope handleScope(thread);
72 auto newArray = objectFactory->NewTaggedArray(1, JSTaggedValue::Undefined(), MemSpaceType::SEMI_SPACE);
73
74 oldArray =
75 (objectFactory->NewTaggedArray(1, JSTaggedValue::Undefined(), MemSpaceType::NON_MOVABLE)).GetTaggedValue();
76 newArray->Set(thread, 0, oldArray);
77 }
78 heap->CollectGarbage(panda::ecmascript::TriggerGCType::OLD_GC);
79 EXPECT_EQ(verifier.VerifyRoot(), 0U);
80 size_t failCount = 0;
81 VerifyObjectVisitor objVerifier(heap, &failCount);
82 const_cast<SemiSpace *>(heap->GetNewSpace())->IterateOverObjects(objVerifier); // newspace reference the old space
83 }
84
HWTEST_F_L0(JSVerificationTest,NoBarrierInternalAccessor)85 HWTEST_F_L0(JSVerificationTest, NoBarrierInternalAccessor)
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);
91 size_t failCount = 0;
92 {
93 EcmaHandleScope handleScope(thread);
94 auto newArray = objectFactory->NewTaggedArray(5, JSTaggedValue::Undefined(), MemSpaceType::SEMI_SPACE);
95 newArray->Set<false>(thread, 0, thread->GlobalConstants()->GetFunctionNameAccessor());
96 newArray->Set<false>(thread, 1, thread->GlobalConstants()->GetFunctionPrototypeAccessor());
97 newArray->Set<false>(thread, 2, thread->GlobalConstants()->GetFunctionLengthAccessor());
98 newArray->Set<false>(thread, 3, thread->GlobalConstants()->GetArrayLengthAccessor());
99 newArray->Set<false>(thread, 4, thread->GlobalConstants()->GetSharedArrayLengthAccessor());
100 VerifyObjectVisitor(heap, &failCount, VerifyKind::VERIFY_MARK_YOUNG)(
101 newArray.GetTaggedValue().GetTaggedObject());
102 }
103 EXPECT_EQ(failCount, 0U);
104 }
105 } // namespace panda::test
106