1 /*
2 * Copyright (c) 2024 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 "faultloggerddumpcatcher_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include "dfx_dump_catcher.h"
21 #include "faultloggerd_client.h"
22 #include "fault_logger_daemon.h"
23 #include "faultloggerd_fuzzertest_common.h"
24
25 namespace OHOS {
26 namespace HiviewDFX {
27 const int FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH = 50;
28
DumpStackTraceTest(const uint8_t * data,size_t size)29 void DumpStackTraceTest(const uint8_t* data, size_t size)
30 {
31 int pid;
32 int tid;
33 int offsetTotalLength = sizeof(pid) + sizeof(tid) +
34 (2 * FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH); // 2 : Offset by 2 string length
35 if (offsetTotalLength > size) {
36 return;
37 }
38
39 STREAM_TO_VALUEINFO(data, pid);
40 STREAM_TO_VALUEINFO(data, tid);
41
42 std::string msg(reinterpret_cast<const char*>(data), FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH);
43 data += FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH;
44 std::string invalidOption(reinterpret_cast<const char*>(data), FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH);
45 data += FAULTLOGGER_FUZZTEST_MAX_STRING_LENGTH;
46
47 std::shared_ptr<DfxDumpCatcher> catcher = std::make_shared<DfxDumpCatcher>();
48 catcher->DumpCatch(pid, tid, msg, DEFAULT_MAX_FRAME_NUM, false);
49
50 std::string processdumpCmd = "dumpcatcher -p " + std::to_string(pid) + " -t " + std::to_string(tid);
51 system(processdumpCmd.c_str());
52
53 std::string processdumpInvalidCmd = "dumpcatcher -" + invalidOption + " -p " +
54 std::to_string(pid) + " -t " + std::to_string(tid);
55 system(processdumpInvalidCmd.c_str());
56 }
57 } // namespace HiviewDFX
58 } // namespace OHOS
59
60 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)61 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
62 {
63 if (data == nullptr || size == 0) {
64 return 0;
65 }
66
67 /* Run your code on data */
68 OHOS::HiviewDFX::DumpStackTraceTest(data, size);
69 return 0;
70 }
71