• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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/dfx/hprof/heap_profiler.h"
17 #include "ecmascript/tests/test_helper.h"
18 
19 using namespace panda::ecmascript;
20 
21 namespace panda::test {
22 class LocalHandleLeakDetectTest : public testing::Test {
23 public:
SetUpTestCase()24     static void SetUpTestCase()
25     {
26         GTEST_LOG_(INFO) << "SetUpTestCase";
27     }
28 
TearDownTestCase()29     static void TearDownTestCase()
30     {
31         GTEST_LOG_(INFO) << "TearDownCase";
32     }
33 
SetUp()34     void SetUp() override
35     {
36         JSRuntimeOptions options;
37         ecmaVm_ = JSNApi::CreateEcmaVM(options);
38         ASSERT_TRUE(ecmaVm_ != nullptr) << "Cannot create EcmaVM";
39         thread_ = ecmaVm_->GetJSThread();
40         thread_->ManagedCodeBegin();
41         ecmaVm_->SetEnableForceGC(false);
42         heapProfiler_ = reinterpret_cast<HeapProfiler *>(HeapProfilerInterface::GetInstance(ecmaVm_));
43     }
44 
TearDown()45     void TearDown() override
46     {
47         ecmaVm_->GetJSThread()->ManagedCodeEnd();
48         JSNApi::DestroyJSVM(ecmaVm_);
49     }
50 
DumpHeapSnapshot(const std::string & detectResultFilePath)51     void DumpHeapSnapshot(const std::string &detectResultFilePath)
52     {
53         DumpSnapShotOption dumpOption;
54         dumpOption.dumpFormat = DumpFormat::JSON;
55         FileDescriptorStream stream(-1);    // No snapshot file name is specified
56 
57         heapProfiler_->SwitchStartLocalHandleLeakDetect();
58         if (heapProfiler_->IsStartLocalHandleLeakDetect()) {
59             int fd = open(detectResultFilePath.c_str(), O_RDWR | O_CREAT | O_TRUNC, 0666);
60             ASSERT_NE(fd, -1) << strerror(errno);
61             heapProfiler_->SetLeakStackTraceFd(fd);
62         }
63 
64         bool result = heapProfiler_->DumpHeapSnapshot(&stream, dumpOption, nullptr, nullptr);
65         ASSERT_TRUE(result) << "DumpHeapSnapshot failed!";
66     }
67 
68     EcmaVM *ecmaVm_ {nullptr};
69     JSThread *thread_ {nullptr};
70     HeapProfiler *heapProfiler_ {nullptr};
71 };
72 
HWTEST_F_L0(LocalHandleLeakDetectTest,TestDetectionResult)73 HWTEST_F_L0(LocalHandleLeakDetectTest, TestDetectionResult)
74 {
75     std::string detectResultFilePath{"local_handle_leak_detect_result.txt"};
76     DumpHeapSnapshot(detectResultFilePath);
77     ObjectFactory *factory = ecmaVm_->GetFactory();
78     // Create a handle without a HandleScope
79     factory->NewTaggedArray(10);
80     DumpHeapSnapshot(detectResultFilePath);
81 
82     std::ifstream file {detectResultFilePath};
83     std::string line;
84     bool hasLeakHeader {false};
85     bool hasNodeId {false};
86     bool hasLeakFooter {false};
87     while (std::getline(file, line)) {
88         if (!hasLeakHeader && line.find("Local Handle Leak Detection Result") != std::string::npos) {
89             hasLeakHeader = true;
90             continue;
91         }
92         if (!hasNodeId && line.find("NodeId") != std::string::npos) {
93             hasNodeId = true;
94             continue;
95         }
96         if (!hasLeakFooter && line.find("End of Local Handle Leak Detection Result") != std::string::npos) {
97             hasLeakFooter = true;
98             continue;
99         }
100     }
101     ASSERT_TRUE(hasLeakHeader);
102     ASSERT_TRUE(hasNodeId);
103     ASSERT_TRUE(hasLeakFooter);
104 }
105 }  // namespace panda::test