• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 <csetjmp>
17 #include <csignal>
18 
19 #include "ecmascript/ecma_handle_scope.h"
20 #include "ecmascript/global_env.h"
21 #include "ecmascript/js_tagged_value.h"
22 #include "ecmascript/jspandafile/program_object.h"
23 #include "ecmascript/mem/barriers.h"
24 #include "ecmascript/mem/heap-inl.h"
25 #include "ecmascript/mem/verification.h"
26 #include "ecmascript/napi/include/jsnapi.h"
27 #include "ecmascript/tagged_array.h"
28 #include "ecmascript/tests/test_helper.h"
29 #include "gtest/gtest.h"
30 
31 using namespace panda;
32 
33 using namespace panda::ecmascript;
34 
35 namespace panda::test {
36 class HandleLeakTest : public BaseTestWithScope<false> {
37 public:
SetUp()38     void SetUp() override
39     {
40         JSRuntimeOptions options;
41         options.SetEnableForceGC(false);
42         options.SetLogLevel("info");
43         instance = JSNApi::CreateEcmaVM(options);
44         ASSERT_TRUE(instance != nullptr) << "Cannot create EcmaVM";
45         thread = instance->GetJSThread();
46         thread->ManagedCodeBegin();
47         scope = new EcmaHandleScope(thread);
48     }
49 };
50 
51 static sigjmp_buf env;
52 static bool segmentFaultFlag = false;
53 class HandleLeakTestManager {
54 public:
ProcessHandleLeakSegmentFault(int sig)55     static void ProcessHandleLeakSegmentFault(int sig)
56     {
57         segmentFaultFlag = true;
58         siglongjmp(env, sig);
59     }
60 
RegisterSignal()61     static int RegisterSignal()
62     {
63         segmentFaultFlag = false;
64         struct sigaction act;
65         act.sa_handler = ProcessHandleLeakSegmentFault;
66         sigemptyset(&act.sa_mask);
67         sigaddset(&act.sa_mask, SIGQUIT);
68         act.sa_flags = SA_RESETHAND;
69         return sigaction(SIGSEGV, &act, nullptr);
70     }
71 };
72 
HWTEST_F_L0(HandleLeakTest,HandleLeakCheck)73 HWTEST_F_L0(HandleLeakTest, HandleLeakCheck)
74 {
75     EcmaHandleScope scope(thread);
76     std::vector<Global<ArrayRef>> result;
77     for (int i = 0; i < 75000; i++) {
78         result.emplace_back(Global<ArrayRef>(instance, ArrayRef::New(instance, 100)));
79     }
80 }
81 
HWTEST_F_L0(HandleLeakTest,UnInitializeCheckOneProperty)82 HWTEST_F_L0(HandleLeakTest, UnInitializeCheckOneProperty)
83 {
84     EcmaHandleScope scope(thread);
85     JSHandle<TaggedObject> newProgram(thread, const_cast<Heap *>(instance->GetHeap())->AllocateYoungOrHugeObject(
86         JSHClass::Cast(thread->GlobalConstants()->GetProgramClass().GetTaggedObject())));
87 
88     if (HandleLeakTestManager::RegisterSignal() == -1) {
89         perror("sigaction error");
90         exit(1);
91     }
92     size_t failCount = 0;
93     auto ret = sigsetjmp(env, 1);
94     if (ret != SIGSEGV) {
95         VerifyObjectVisitor verifier(instance->GetHeap(), &failCount);
96         verifier(*newProgram);
97         ASSERT_TRUE(false);
98     } else {
99         // catch signal SIGSEGV caused by uninitialize
100         EXPECT_TRUE(segmentFaultFlag);
101         ASSERT_TRUE(failCount == 0);
102     }
103 }
104 
HWTEST_F_L0(HandleLeakTest,InitializeCheckOneProperty)105 HWTEST_F_L0(HandleLeakTest, InitializeCheckOneProperty)
106 {
107     EcmaHandleScope scope(thread);
108     JSHandle<Program> newProgram(thread, const_cast<Heap *>(instance->GetHeap())->AllocateYoungOrHugeObject(
109         JSHClass::Cast(thread->GlobalConstants()->GetProgramClass().GetTaggedObject())));
110 
111     newProgram->SetMainFunction(thread, JSTaggedValue::Undefined());
112 
113     size_t failCount = 0;
114     VerifyObjectVisitor verifier(instance->GetHeap(), &failCount);
115     verifier(*newProgram);
116     ASSERT_TRUE(newProgram.GetTaggedValue().IsProgram());
117     ASSERT_TRUE(failCount == 0);
118 }
119 
HeandleLeakTestCommon(const EcmaVM * instance,JSHandle<TaggedArray> & newArray)120 static void HeandleLeakTestCommon(const EcmaVM *instance, JSHandle<TaggedArray>& newArray)
121 {
122     size_t failCount = 0;
123     auto ret = sigsetjmp(env, 1);
124     if (ret != SIGSEGV) {
125         VerifyObjectVisitor verifier(instance->GetHeap(), &failCount);
126         verifier(*newArray);
127         ASSERT_TRUE(false);
128     } else {
129         // catch signal SIGSEGV caused by uninitialize
130         EXPECT_TRUE(segmentFaultFlag);
131         ASSERT_TRUE(failCount == 0);
132     }
133 }
134 
HWTEST_F_L0(HandleLeakTest,UnInitializeCheckMoreProperty)135 HWTEST_F_L0(HandleLeakTest, UnInitializeCheckMoreProperty)
136 {
137     EcmaHandleScope scope(thread);
138     JSHandle<JSHClass> arrayClass(thread->GlobalConstants()->GetHandledArrayClass());
139     static constexpr int SIZE = 100;
140     JSHandle<TaggedArray> newArray(thread, const_cast<Heap *>(instance->GetHeap())->AllocateNonMovableOrHugeObject(
141         *arrayClass, TaggedArray::ComputeSize(JSTaggedValue::TaggedTypeSize(), SIZE)));
142     newArray->SetLength(SIZE);
143 
144     if (HandleLeakTestManager::RegisterSignal() == -1) {
145         perror("sigaction error");
146         exit(1);
147     }
148     HeandleLeakTestCommon(instance, newArray);
149 }
150 
HWTEST_F_L0(HandleLeakTest,PartInitializeCheckMoreProperty)151 HWTEST_F_L0(HandleLeakTest, PartInitializeCheckMoreProperty)
152 {
153     EcmaHandleScope scope(thread);
154     JSHandle<JSHClass> arrayClass(thread->GlobalConstants()->GetHandledArrayClass());
155     static constexpr int SIZE = 100;
156     JSHandle<TaggedArray> newArray(thread, const_cast<Heap *>(instance->GetHeap())->AllocateNonMovableOrHugeObject(
157         *arrayClass, TaggedArray::ComputeSize(JSTaggedValue::TaggedTypeSize(), SIZE)));
158     newArray->SetLength(SIZE);
159     for (uint32_t i = 0; i < SIZE / 2; i++) {
160         size_t offset = JSTaggedValue::TaggedTypeSize() * i;
161         ecmascript::Barriers::SetPrimitive(newArray->GetData(), offset, JSTaggedValue::Undefined());
162     }
163 
164     if (HandleLeakTestManager::RegisterSignal() == -1) {
165         perror("sigaction error");
166         exit(1);
167     }
168     HeandleLeakTestCommon(instance, newArray);
169 }
170 
HWTEST_F_L0(HandleLeakTest,InitializeCheckMoreProperty)171 HWTEST_F_L0(HandleLeakTest, InitializeCheckMoreProperty)
172 {
173     EcmaHandleScope scope(thread);
174     JSHandle<JSHClass> arrayClass(thread->GlobalConstants()->GetHandledArrayClass());
175     static constexpr int SIZE = 100;
176     JSHandle<TaggedArray> newArray(thread, const_cast<Heap *>(instance->GetHeap())->AllocateNonMovableOrHugeObject(
177         *arrayClass, TaggedArray::ComputeSize(JSTaggedValue::TaggedTypeSize(), SIZE)));
178 
179     newArray->InitializeWithSpecialValue(JSTaggedValue::Hole(), SIZE);
180     size_t failCount = 0;
181     VerifyObjectVisitor verifier(instance->GetHeap(), &failCount);
182     verifier(*newArray);
183     ASSERT_TRUE(newArray.GetTaggedValue().IsTaggedArray());
184     ASSERT_TRUE(failCount == 0);
185 }
186 }  // namespace panda::test
187