1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci
16800b99b8Sopenharmony_ci#include <gtest/gtest.h>
17800b99b8Sopenharmony_ci#include <csignal>
18800b99b8Sopenharmony_ci#include <map>
19800b99b8Sopenharmony_ci#include <securec.h>
20800b99b8Sopenharmony_ci#include <string>
21800b99b8Sopenharmony_ci#include <thread>
22800b99b8Sopenharmony_ci#include <unistd.h>
23800b99b8Sopenharmony_ci#include <vector>
24800b99b8Sopenharmony_ci#include <sys/prctl.h>
25800b99b8Sopenharmony_ci
26800b99b8Sopenharmony_ci#include "dfx_define.h"
27800b99b8Sopenharmony_ci#include "dfx_signal_local_handler.h"
28800b99b8Sopenharmony_ci#include "dfx_socket_request.h"
29800b99b8Sopenharmony_ci#include "dfx_test_util.h"
30800b99b8Sopenharmony_ci#include "dfx_allocator.h"
31800b99b8Sopenharmony_ci#include "faultloggerd_client.h"
32800b99b8Sopenharmony_ci
33800b99b8Sopenharmony_ci#define MALLOC_TEST_TIMES  1000
34800b99b8Sopenharmony_ci#define MALLOC_TEST_SMALL_SIZE 16
35800b99b8Sopenharmony_ci#define MALLOC_TEST_BIG_SIZE 2000
36800b99b8Sopenharmony_ci
37800b99b8Sopenharmony_ciusing namespace testing;
38800b99b8Sopenharmony_ciusing namespace testing::ext;
39800b99b8Sopenharmony_ciusing namespace std;
40800b99b8Sopenharmony_ci
41800b99b8Sopenharmony_cinamespace OHOS {
42800b99b8Sopenharmony_cinamespace HiviewDFX {
43800b99b8Sopenharmony_ciclass LocalHandlerTest : public testing::Test {
44800b99b8Sopenharmony_cipublic:
45800b99b8Sopenharmony_ci    static void SetUpTestCase();
46800b99b8Sopenharmony_ci    static void TearDownTestCase();
47800b99b8Sopenharmony_ci    void SetUp();
48800b99b8Sopenharmony_ci    void TearDown();
49800b99b8Sopenharmony_ci};
50800b99b8Sopenharmony_ci
51800b99b8Sopenharmony_civoid LocalHandlerTest::SetUpTestCase()
52800b99b8Sopenharmony_ci{}
53800b99b8Sopenharmony_ci
54800b99b8Sopenharmony_civoid LocalHandlerTest::TearDownTestCase()
55800b99b8Sopenharmony_ci{}
56800b99b8Sopenharmony_ci
57800b99b8Sopenharmony_civoid LocalHandlerTest::SetUp()
58800b99b8Sopenharmony_ci{}
59800b99b8Sopenharmony_ci
60800b99b8Sopenharmony_civoid LocalHandlerTest::TearDown()
61800b99b8Sopenharmony_ci{}
62800b99b8Sopenharmony_ci
63800b99b8Sopenharmony_cistatic bool CheckLocalCrashKeyWords(const string& filePath, pid_t pid, int sig)
64800b99b8Sopenharmony_ci{
65800b99b8Sopenharmony_ci    if (filePath.empty() || pid <= 0) {
66800b99b8Sopenharmony_ci        return false;
67800b99b8Sopenharmony_ci    }
68800b99b8Sopenharmony_ci    map<int, string> sigKey = {
69800b99b8Sopenharmony_ci        { SIGILL, string("Signal(4)") },
70800b99b8Sopenharmony_ci        { SIGABRT, string("Signal(6)") },
71800b99b8Sopenharmony_ci        { SIGBUS, string("Signal(7)") },
72800b99b8Sopenharmony_ci        { SIGSEGV, string("Signal(11)") },
73800b99b8Sopenharmony_ci    };
74800b99b8Sopenharmony_ci    string sigKeyword = "";
75800b99b8Sopenharmony_ci    map<int, string>::iterator iter = sigKey.find(sig);
76800b99b8Sopenharmony_ci    if (iter != sigKey.end()) {
77800b99b8Sopenharmony_ci        sigKeyword = iter->second;
78800b99b8Sopenharmony_ci    }
79800b99b8Sopenharmony_ci#ifdef __aarch64__
80800b99b8Sopenharmony_ci    string keywords[] = {
81800b99b8Sopenharmony_ci        "Pid:" + to_string(pid), "Uid:", "name:./test_localhandler",
82800b99b8Sopenharmony_ci        sigKeyword, "Tid:", "#00", "x0:", "test_localhandler"
83800b99b8Sopenharmony_ci    };
84800b99b8Sopenharmony_ci#else
85800b99b8Sopenharmony_ci    string keywords[] = {
86800b99b8Sopenharmony_ci        "Pid:" + to_string(pid), "Uid:", "name:./test_localhandler",
87800b99b8Sopenharmony_ci        sigKeyword, "Tid:", "#00", "test_localhandler"
88800b99b8Sopenharmony_ci    };
89800b99b8Sopenharmony_ci#endif
90800b99b8Sopenharmony_ci
91800b99b8Sopenharmony_ci    int length = sizeof(keywords) / sizeof(keywords[0]);
92800b99b8Sopenharmony_ci    int minRegIdx = -1;
93800b99b8Sopenharmony_ci    return CheckKeyWords(filePath, keywords, length, minRegIdx) == length;
94800b99b8Sopenharmony_ci}
95800b99b8Sopenharmony_ci
96800b99b8Sopenharmony_ci/**
97800b99b8Sopenharmony_ci * @tc.name: LocalHandlerTest001
98800b99b8Sopenharmony_ci * @tc.desc: test crashlocalhandler signo(SIGILL)
99800b99b8Sopenharmony_ci * @tc.type: FUNC
100800b99b8Sopenharmony_ci */
101800b99b8Sopenharmony_ciHWTEST_F(LocalHandlerTest, LocalHandlerTest001, TestSize.Level2)
102800b99b8Sopenharmony_ci{
103800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "LocalHandlerTest001: start.";
104800b99b8Sopenharmony_ci    pid_t pid = fork();
105800b99b8Sopenharmony_ci    if (pid < 0) {
106800b99b8Sopenharmony_ci        GTEST_LOG_(ERROR) << "Failed to fork new test process.";
107800b99b8Sopenharmony_ci    } else if (pid == 0) {
108800b99b8Sopenharmony_ci        DFX_InstallLocalSignalHandler();
109800b99b8Sopenharmony_ci        sleep(1);
110800b99b8Sopenharmony_ci    } else {
111800b99b8Sopenharmony_ci        usleep(10000); // 10000 : sleep 10ms
112800b99b8Sopenharmony_ci        GTEST_LOG_(INFO) << "process(" << getpid() << ") is ready to kill process(" << pid << ")";
113800b99b8Sopenharmony_ci        kill(pid, SIGILL);
114800b99b8Sopenharmony_ci        sleep(2); // 2 : wait for cppcrash generating
115800b99b8Sopenharmony_ci        bool ret = CheckLocalCrashKeyWords(GetCppCrashFileName(pid), pid, SIGILL);
116800b99b8Sopenharmony_ci        ASSERT_TRUE(ret);
117800b99b8Sopenharmony_ci    }
118800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "LocalHandlerTest001: end.";
119800b99b8Sopenharmony_ci}
120800b99b8Sopenharmony_ci
121800b99b8Sopenharmony_ci/**
122800b99b8Sopenharmony_ci * @tc.name: LocalHandlerTest002
123800b99b8Sopenharmony_ci * @tc.desc: test crashlocalhandler signo(SIGABRT)
124800b99b8Sopenharmony_ci * @tc.type: FUNC
125800b99b8Sopenharmony_ci */
126800b99b8Sopenharmony_ciHWTEST_F(LocalHandlerTest, LocalHandlerTest002, TestSize.Level2)
127800b99b8Sopenharmony_ci{
128800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "LocalHandlerTest002: start.";
129800b99b8Sopenharmony_ci    pid_t pid = fork();
130800b99b8Sopenharmony_ci    if (pid < 0) {
131800b99b8Sopenharmony_ci        GTEST_LOG_(ERROR) << "Failed to fork new test process.";
132800b99b8Sopenharmony_ci    } else if (pid == 0) {
133800b99b8Sopenharmony_ci        DFX_InstallLocalSignalHandler();
134800b99b8Sopenharmony_ci        sleep(1);
135800b99b8Sopenharmony_ci    } else {
136800b99b8Sopenharmony_ci        usleep(10000); // 10000 : sleep 10ms
137800b99b8Sopenharmony_ci        GTEST_LOG_(INFO) << "process(" << getpid() << ") is ready to kill process(" << pid << ")";
138800b99b8Sopenharmony_ci        kill(pid, SIGABRT);
139800b99b8Sopenharmony_ci        sleep(2); // 2 : wait for cppcrash generating
140800b99b8Sopenharmony_ci        bool ret = CheckLocalCrashKeyWords(GetCppCrashFileName(pid), pid, SIGABRT);
141800b99b8Sopenharmony_ci        ASSERT_TRUE(ret);
142800b99b8Sopenharmony_ci    }
143800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "LocalHandlerTest002: end.";
144800b99b8Sopenharmony_ci}
145800b99b8Sopenharmony_ci
146800b99b8Sopenharmony_ci/**
147800b99b8Sopenharmony_ci * @tc.name: LocalHandlerTest003
148800b99b8Sopenharmony_ci * @tc.desc: test crashlocalhandler signo(SIGBUS)
149800b99b8Sopenharmony_ci * @tc.type: FUNC
150800b99b8Sopenharmony_ci */
151800b99b8Sopenharmony_ciHWTEST_F(LocalHandlerTest, LocalHandlerTest003, TestSize.Level2)
152800b99b8Sopenharmony_ci{
153800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "LocalHandlerTest003: start.";
154800b99b8Sopenharmony_ci    pid_t pid = fork();
155800b99b8Sopenharmony_ci    if (pid < 0) {
156800b99b8Sopenharmony_ci        GTEST_LOG_(ERROR) << "Failed to fork new test process.";
157800b99b8Sopenharmony_ci    } else if (pid == 0) {
158800b99b8Sopenharmony_ci        DFX_InstallLocalSignalHandler();
159800b99b8Sopenharmony_ci        sleep(1);
160800b99b8Sopenharmony_ci    } else {
161800b99b8Sopenharmony_ci        usleep(10000); // 10000 : sleep 10ms
162800b99b8Sopenharmony_ci        GTEST_LOG_(INFO) << "process(" << getpid() << ") is ready to kill process(" << pid << ")";
163800b99b8Sopenharmony_ci        kill(pid, SIGBUS);
164800b99b8Sopenharmony_ci        sleep(2); // 2 : wait for cppcrash generating
165800b99b8Sopenharmony_ci        bool ret = CheckLocalCrashKeyWords(GetCppCrashFileName(pid), pid, SIGBUS);
166800b99b8Sopenharmony_ci        ASSERT_TRUE(ret);
167800b99b8Sopenharmony_ci    }
168800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "LocalHandlerTest003: end.";
169800b99b8Sopenharmony_ci}
170800b99b8Sopenharmony_ci
171800b99b8Sopenharmony_ci/**
172800b99b8Sopenharmony_ci * @tc.name: LocalHandlerTest004
173800b99b8Sopenharmony_ci * @tc.desc: test crashlocalhandler signo(SIGSEGV)
174800b99b8Sopenharmony_ci * @tc.type: FUNC
175800b99b8Sopenharmony_ci */
176800b99b8Sopenharmony_ciHWTEST_F(LocalHandlerTest, LocalHandlerTest004, TestSize.Level2)
177800b99b8Sopenharmony_ci{
178800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "LocalHandlerTest004: start.";
179800b99b8Sopenharmony_ci    pid_t pid = fork();
180800b99b8Sopenharmony_ci    if (pid < 0) {
181800b99b8Sopenharmony_ci        GTEST_LOG_(ERROR) << "Failed to fork new test process.";
182800b99b8Sopenharmony_ci    } else if (pid == 0) {
183800b99b8Sopenharmony_ci        DFX_InstallLocalSignalHandler();
184800b99b8Sopenharmony_ci        sleep(1);
185800b99b8Sopenharmony_ci    } else {
186800b99b8Sopenharmony_ci        usleep(10000); // 10000 : sleep 10ms
187800b99b8Sopenharmony_ci        GTEST_LOG_(INFO) << "process(" << getpid() << ") is ready to kill process(" << pid << ")";
188800b99b8Sopenharmony_ci        kill(pid, SIGSEGV);
189800b99b8Sopenharmony_ci        sleep(2); // 2 : wait for cppcrash generating
190800b99b8Sopenharmony_ci        bool ret = CheckLocalCrashKeyWords(GetCppCrashFileName(pid), pid, SIGSEGV);
191800b99b8Sopenharmony_ci        ASSERT_TRUE(ret);
192800b99b8Sopenharmony_ci    }
193800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "LocalHandlerTest004: end.";
194800b99b8Sopenharmony_ci}
195800b99b8Sopenharmony_ci
196800b99b8Sopenharmony_ci/**
197800b99b8Sopenharmony_ci * @tc.name: LocalHandlerTest005
198800b99b8Sopenharmony_ci * @tc.desc: test crashlocalhandler signo(SIGSEGV) by execl
199800b99b8Sopenharmony_ci * @tc.type: FUNC
200800b99b8Sopenharmony_ci */
201800b99b8Sopenharmony_ciHWTEST_F(LocalHandlerTest, LocalHandlerTest005, TestSize.Level2)
202800b99b8Sopenharmony_ci{
203800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "LocalHandlerTest005: start.";
204800b99b8Sopenharmony_ci    pid_t pid = fork();
205800b99b8Sopenharmony_ci    if (pid < 0) {
206800b99b8Sopenharmony_ci        GTEST_LOG_(ERROR) << "Failed to fork new test process.";
207800b99b8Sopenharmony_ci    } else if (pid == 0) {
208800b99b8Sopenharmony_ci        DFX_InstallLocalSignalHandler();
209800b99b8Sopenharmony_ci        sleep(1);
210800b99b8Sopenharmony_ci        raise(SIGSEGV);
211800b99b8Sopenharmony_ci    } else {
212800b99b8Sopenharmony_ci        usleep(10000); // 10000 : sleep 10ms
213800b99b8Sopenharmony_ci        GTEST_LOG_(INFO) << "process(" << getpid() << ") is ready to wait process(" << pid << ")";
214800b99b8Sopenharmony_ci        sleep(2); // 2 : wait for cppcrash generating
215800b99b8Sopenharmony_ci        bool ret = CheckLocalCrashKeyWords(GetCppCrashFileName(pid), pid, SIGSEGV);
216800b99b8Sopenharmony_ci        ASSERT_TRUE(ret);
217800b99b8Sopenharmony_ci    }
218800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "LocalHandlerTest005: end.";
219800b99b8Sopenharmony_ci}
220800b99b8Sopenharmony_ci
221800b99b8Sopenharmony_ci/**
222800b99b8Sopenharmony_ci * @tc.name: LocalHandlerTest006
223800b99b8Sopenharmony_ci * @tc.desc: test crashlocalhandler signo(SIGSEGV) by execl
224800b99b8Sopenharmony_ci * @tc.type: FUNC
225800b99b8Sopenharmony_ci */
226800b99b8Sopenharmony_ciHWTEST_F(LocalHandlerTest, LocalHandlerTest006, TestSize.Level2)
227800b99b8Sopenharmony_ci{
228800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "LocalHandlerTest006: start.";
229800b99b8Sopenharmony_ci    pid_t pid = fork();
230800b99b8Sopenharmony_ci    if (pid < 0) {
231800b99b8Sopenharmony_ci        GTEST_LOG_(ERROR) << "Failed to fork new test process.";
232800b99b8Sopenharmony_ci    } else if (pid == 0) {
233800b99b8Sopenharmony_ci        siginfo_t siginfo {
234800b99b8Sopenharmony_ci            .si_signo = SIGSEGV
235800b99b8Sopenharmony_ci        };
236800b99b8Sopenharmony_ci        DFX_SignalLocalHandler(SIGSEGV, &siginfo, nullptr);
237800b99b8Sopenharmony_ci    } else {
238800b99b8Sopenharmony_ci        GTEST_LOG_(INFO) << "process(" << getpid() << ") is ready to wait process(" << pid << ")";
239800b99b8Sopenharmony_ci        sleep(2); // 2 : wait for cppcrash generating
240800b99b8Sopenharmony_ci        ASSERT_TRUE(CheckLocalCrashKeyWords(GetCppCrashFileName(pid), pid, SIGSEGV));
241800b99b8Sopenharmony_ci    }
242800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "LocalHandlerTest006: end.";
243800b99b8Sopenharmony_ci}
244800b99b8Sopenharmony_ci
245800b99b8Sopenharmony_ci/**
246800b99b8Sopenharmony_ci * @tc.name: LocalHandlerTest007
247800b99b8Sopenharmony_ci * @tc.desc: test crashlocalhandler signo(SIGSEGV) by execl
248800b99b8Sopenharmony_ci * @tc.type: FUNC
249800b99b8Sopenharmony_ci */
250800b99b8Sopenharmony_ciHWTEST_F(LocalHandlerTest, LocalHandlerTest007, TestSize.Level2)
251800b99b8Sopenharmony_ci{
252800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "LocalHandlerTest005: start.";
253800b99b8Sopenharmony_ci    pid_t pid = fork();
254800b99b8Sopenharmony_ci    if (pid < 0) {
255800b99b8Sopenharmony_ci        GTEST_LOG_(ERROR) << "Failed to fork new test process.";
256800b99b8Sopenharmony_ci    } else if (pid == 0) {
257800b99b8Sopenharmony_ci        siginfo_t siginfo {
258800b99b8Sopenharmony_ci            .si_signo = SIGSEGV
259800b99b8Sopenharmony_ci        };
260800b99b8Sopenharmony_ci        DFX_GetCrashFdFunc([]{return RequestFileDescriptor((int)FaultLoggerType::CPP_CRASH);});
261800b99b8Sopenharmony_ci        DFX_SignalLocalHandler(SIGSEGV, &siginfo, nullptr);
262800b99b8Sopenharmony_ci    } else {
263800b99b8Sopenharmony_ci        GTEST_LOG_(INFO) << "process(" << getpid() << ") is ready to wait process(" << pid << ")";
264800b99b8Sopenharmony_ci        sleep(2); // 2 : wait for cppcrash generating
265800b99b8Sopenharmony_ci        bool ret = CheckLocalCrashKeyWords(GetCppCrashFileName(pid), pid, SIGSEGV);
266800b99b8Sopenharmony_ci        ASSERT_TRUE(ret);
267800b99b8Sopenharmony_ci    }
268800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "LocalHandlerTest007: end.";
269800b99b8Sopenharmony_ci}
270800b99b8Sopenharmony_ci
271800b99b8Sopenharmony_ci/**
272800b99b8Sopenharmony_ci * @tc.name: DfxAllocatorTest001
273800b99b8Sopenharmony_ci * @tc.desc: test dfxAllocator isDfxAllocatorMem
274800b99b8Sopenharmony_ci * @tc.type: FUNC
275800b99b8Sopenharmony_ci */
276800b99b8Sopenharmony_ciHWTEST_F(LocalHandlerTest, DfxAllocatorTest001, TestSize.Level2)
277800b99b8Sopenharmony_ci{
278800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "DfxAllocatorTest001: start.";
279800b99b8Sopenharmony_ci    void* p = malloc(MALLOC_TEST_SMALL_SIZE);
280800b99b8Sopenharmony_ci    int ret = 0;
281800b99b8Sopenharmony_ci    if (p) {
282800b99b8Sopenharmony_ci        ret = IsDfxAllocatorMem(p);
283800b99b8Sopenharmony_ci        free(p);
284800b99b8Sopenharmony_ci    }
285800b99b8Sopenharmony_ci    ASSERT_TRUE(ret == 0);
286800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "DfxAllocatorTest001: end.";
287800b99b8Sopenharmony_ci}
288800b99b8Sopenharmony_ci
289800b99b8Sopenharmony_ci/**
290800b99b8Sopenharmony_ci * @tc.name: DfxAllocatorTest002
291800b99b8Sopenharmony_ci * @tc.desc: test dfxAllocator malloc and free
292800b99b8Sopenharmony_ci * @tc.type: FUNC
293800b99b8Sopenharmony_ci */
294800b99b8Sopenharmony_ciHWTEST_F(LocalHandlerTest, DfxAllocatorTest002, TestSize.Level2)
295800b99b8Sopenharmony_ci{
296800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "DfxAllocatorTest002: start.";
297800b99b8Sopenharmony_ci    void* p = nullptr;
298800b99b8Sopenharmony_ci    void* parr[MALLOC_TEST_TIMES] = {nullptr};
299800b99b8Sopenharmony_ci    uint32_t size = MALLOC_TEST_SMALL_SIZE;
300800b99b8Sopenharmony_ci    int res = 0;
301800b99b8Sopenharmony_ci    DfxAllocator* allocator = GetDfxAllocator();
302800b99b8Sopenharmony_ci    RegisterAllocator();
303800b99b8Sopenharmony_ci    for (int i = 0; i < DFX_MEMPOOLS_NUM; i++) {
304800b99b8Sopenharmony_ci        // malloc and free 1000 times
305800b99b8Sopenharmony_ci        for (int time = 0; time < MALLOC_TEST_TIMES; time++) {
306800b99b8Sopenharmony_ci            p = malloc(size);
307800b99b8Sopenharmony_ci            if (p == nullptr || !IsDfxAllocatorMem(p)) {
308800b99b8Sopenharmony_ci                res = 1;
309800b99b8Sopenharmony_ci            }
310800b99b8Sopenharmony_ci            free(p);
311800b99b8Sopenharmony_ci            if (allocator->dfxMempoolBuf[i].pageList != nullptr) {
312800b99b8Sopenharmony_ci                res = 1;
313800b99b8Sopenharmony_ci            }
314800b99b8Sopenharmony_ci        }
315800b99b8Sopenharmony_ci        // malloc 1000 times and free 1000 times
316800b99b8Sopenharmony_ci        for (int time = 0; time < MALLOC_TEST_TIMES; time++) {
317800b99b8Sopenharmony_ci            parr[time] = malloc(size);
318800b99b8Sopenharmony_ci            if (parr[time] == nullptr || !IsDfxAllocatorMem(parr[time])) {
319800b99b8Sopenharmony_ci                res = 1;
320800b99b8Sopenharmony_ci            }
321800b99b8Sopenharmony_ci        }
322800b99b8Sopenharmony_ci        for (int time = 0; time < MALLOC_TEST_TIMES; time++) {
323800b99b8Sopenharmony_ci            free(parr[time]);
324800b99b8Sopenharmony_ci        }
325800b99b8Sopenharmony_ci        if (allocator->dfxMempoolBuf[i].pageList != nullptr) {
326800b99b8Sopenharmony_ci            res = 1;
327800b99b8Sopenharmony_ci        }
328800b99b8Sopenharmony_ci        size = size << 1;
329800b99b8Sopenharmony_ci    }
330800b99b8Sopenharmony_ci    UnregisterAllocator();
331800b99b8Sopenharmony_ci    ASSERT_TRUE(res == 0);
332800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "DfxAllocatorTest002: end.";
333800b99b8Sopenharmony_ci}
334800b99b8Sopenharmony_ci
335800b99b8Sopenharmony_ci/**
336800b99b8Sopenharmony_ci * @tc.name: DfxAllocatorTest003
337800b99b8Sopenharmony_ci * @tc.desc: test dfxAllocator mmap
338800b99b8Sopenharmony_ci * @tc.type: FUNC
339800b99b8Sopenharmony_ci */
340800b99b8Sopenharmony_ciHWTEST_F(LocalHandlerTest, DfxAllocatorTest003, TestSize.Level2)
341800b99b8Sopenharmony_ci{
342800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "DfxAllocatorTest003: start.";
343800b99b8Sopenharmony_ci    void* p = nullptr;
344800b99b8Sopenharmony_ci    void* parr[MALLOC_TEST_TIMES] = {nullptr};
345800b99b8Sopenharmony_ci    uint32_t size = MALLOC_TEST_BIG_SIZE;
346800b99b8Sopenharmony_ci    int res = 0;
347800b99b8Sopenharmony_ci    DfxAllocator* allocator = GetDfxAllocator();
348800b99b8Sopenharmony_ci    RegisterAllocator();
349800b99b8Sopenharmony_ci    // malloc and free 1000 times
350800b99b8Sopenharmony_ci    for (int time = 0; time < MALLOC_TEST_TIMES; time++) {
351800b99b8Sopenharmony_ci        p = malloc(size);
352800b99b8Sopenharmony_ci        if (p == nullptr || !IsDfxAllocatorMem(p)) {
353800b99b8Sopenharmony_ci            res = 1;
354800b99b8Sopenharmony_ci        }
355800b99b8Sopenharmony_ci        free(p);
356800b99b8Sopenharmony_ci        if (allocator->pageList != nullptr) {
357800b99b8Sopenharmony_ci            res = 1;
358800b99b8Sopenharmony_ci        }
359800b99b8Sopenharmony_ci    }
360800b99b8Sopenharmony_ci    // malloc 1000 times and free 1000 times
361800b99b8Sopenharmony_ci    for (int time = 0; time < MALLOC_TEST_TIMES; time++) {
362800b99b8Sopenharmony_ci        parr[time] = malloc(size);
363800b99b8Sopenharmony_ci        if (parr[time] == nullptr || !IsDfxAllocatorMem(parr[time])) {
364800b99b8Sopenharmony_ci            res = 1;
365800b99b8Sopenharmony_ci        }
366800b99b8Sopenharmony_ci    }
367800b99b8Sopenharmony_ci    for (int time = 0; time < MALLOC_TEST_TIMES; time++) {
368800b99b8Sopenharmony_ci        free(parr[time]);
369800b99b8Sopenharmony_ci    }
370800b99b8Sopenharmony_ci    if (allocator->pageList != nullptr) {
371800b99b8Sopenharmony_ci        res = 1;
372800b99b8Sopenharmony_ci    }
373800b99b8Sopenharmony_ci    UnregisterAllocator();
374800b99b8Sopenharmony_ci    ASSERT_TRUE(res == 0);
375800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "DfxAllocatorTest003: end.";
376800b99b8Sopenharmony_ci}
377800b99b8Sopenharmony_ci
378800b99b8Sopenharmony_ci/**
379800b99b8Sopenharmony_ci * @tc.name: DfxAllocatorTest004
380800b99b8Sopenharmony_ci * @tc.desc: test dfxAllocator realloc
381800b99b8Sopenharmony_ci * @tc.type: FUNC
382800b99b8Sopenharmony_ci */
383800b99b8Sopenharmony_ciHWTEST_F(LocalHandlerTest, DfxAllocatorTest004, TestSize.Level2)
384800b99b8Sopenharmony_ci{
385800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "DfxAllocatorTest004: start.";
386800b99b8Sopenharmony_ci    void* p = nullptr;
387800b99b8Sopenharmony_ci    uint32_t size = MALLOC_TEST_SMALL_SIZE;
388800b99b8Sopenharmony_ci    int res = 0;
389800b99b8Sopenharmony_ci    DfxAllocator* allocator = GetDfxAllocator();
390800b99b8Sopenharmony_ci    RegisterAllocator();
391800b99b8Sopenharmony_ci    p = malloc(size);
392800b99b8Sopenharmony_ci    (void)memset_s(p, size, 0, size);
393800b99b8Sopenharmony_ci    size += MALLOC_TEST_SMALL_SIZE;
394800b99b8Sopenharmony_ci    p = realloc(p, size);
395800b99b8Sopenharmony_ci    if (p == nullptr || !IsDfxAllocatorMem(p)) {
396800b99b8Sopenharmony_ci        res = 1;
397800b99b8Sopenharmony_ci    }
398800b99b8Sopenharmony_ci    if (allocator->dfxMempoolBuf[1].pageList == nullptr) {
399800b99b8Sopenharmony_ci        res = 1;
400800b99b8Sopenharmony_ci    }
401800b99b8Sopenharmony_ci    free(p);
402800b99b8Sopenharmony_ci    UnregisterAllocator();
403800b99b8Sopenharmony_ci    ASSERT_TRUE(res == 0);
404800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "DfxAllocatorTest004: end.";
405800b99b8Sopenharmony_ci}
406800b99b8Sopenharmony_ci
407800b99b8Sopenharmony_ci/**
408800b99b8Sopenharmony_ci * @tc.name: DfxAllocatorTest005
409800b99b8Sopenharmony_ci * @tc.desc: test dfxAllocator localhandler crash log
410800b99b8Sopenharmony_ci * @tc.type: FUNC
411800b99b8Sopenharmony_ci */
412800b99b8Sopenharmony_ciHWTEST_F(LocalHandlerTest, DfxAllocatorTest005, TestSize.Level2)
413800b99b8Sopenharmony_ci{
414800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "DfxAllocatorTest005: start.";
415800b99b8Sopenharmony_ci    pid_t pid = fork();
416800b99b8Sopenharmony_ci    if (pid < 0) {
417800b99b8Sopenharmony_ci        GTEST_LOG_(ERROR) << "Failed to fork new test process.";
418800b99b8Sopenharmony_ci    } else if (pid == 0) {
419800b99b8Sopenharmony_ci        DFX_InstallLocalSignalHandler();
420800b99b8Sopenharmony_ci        sleep(1);
421800b99b8Sopenharmony_ci        // alloc a buffer
422800b99b8Sopenharmony_ci        int32_t initAllocSz = 10;
423800b99b8Sopenharmony_ci        int32_t reallocSz = 20;
424800b99b8Sopenharmony_ci        int8_t* addr = reinterpret_cast<int8_t*>(malloc(initAllocSz));
425800b99b8Sopenharmony_ci        // overwrite the control block
426800b99b8Sopenharmony_ci        int8_t* newAddr = addr - initAllocSz;
427800b99b8Sopenharmony_ci        (void)memset_s(newAddr, initAllocSz, 0, initAllocSz);
428800b99b8Sopenharmony_ci        addr = reinterpret_cast<int8_t*>(realloc(reinterpret_cast<void*>(addr), reallocSz));
429800b99b8Sopenharmony_ci        free(addr);
430800b99b8Sopenharmony_ci        // force crash if not crash in realloc
431800b99b8Sopenharmony_ci        abort();
432800b99b8Sopenharmony_ci    } else {
433800b99b8Sopenharmony_ci        usleep(10000); // 10000 : sleep 10ms
434800b99b8Sopenharmony_ci        GTEST_LOG_(INFO) << "process(" << getpid() << ") is ready to wait process(" << pid << ")";
435800b99b8Sopenharmony_ci        sleep(2); // 2 : wait for cppcrash generating
436800b99b8Sopenharmony_ci#ifdef __aarch64__
437800b99b8Sopenharmony_ci        bool ret = CheckLocalCrashKeyWords(GetCppCrashFileName(pid), pid, SIGABRT);
438800b99b8Sopenharmony_ci#else
439800b99b8Sopenharmony_ci        bool ret = CheckLocalCrashKeyWords(GetCppCrashFileName(pid), pid, SIGSEGV);
440800b99b8Sopenharmony_ci#endif
441800b99b8Sopenharmony_ci        ASSERT_TRUE(ret);
442800b99b8Sopenharmony_ci    }
443800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "DfxAllocatorTest005: end.";
444800b99b8Sopenharmony_ci}
445800b99b8Sopenharmony_ci
446800b99b8Sopenharmony_ci/**
447800b99b8Sopenharmony_ci * @tc.name: DfxAllocatorTest006
448800b99b8Sopenharmony_ci * @tc.desc: test dfxAllocator localhandler abnormal scenario
449800b99b8Sopenharmony_ci * @tc.type: FUNC
450800b99b8Sopenharmony_ci */
451800b99b8Sopenharmony_ciHWTEST_F(LocalHandlerTest, DfxAllocatorTest006, TestSize.Level2)
452800b99b8Sopenharmony_ci{
453800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "DfxAllocatorTest005: start.";
454800b99b8Sopenharmony_ci    int ret = IsDfxAllocatorMem(nullptr);
455800b99b8Sopenharmony_ci    ASSERT_EQ(ret, 0);
456800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "DfxAllocatorTest005: end.";
457800b99b8Sopenharmony_ci}
458800b99b8Sopenharmony_ci} // namespace HiviewDFX
459800b99b8Sopenharmony_ci} // namepsace OHOS
460