1 /*
2 * Copyright (c) 2023-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 "hiviewservice_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <fcntl.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 #include <vector>
25
26 #include "hiview_service.h"
27 #include "hiview_platform.h"
28
29 namespace OHOS {
30 namespace HiviewDFX {
31 namespace {
32 HiviewService g_hiviewService;
33 int g_fd = 0;
34
35 struct Initializer {
InitializerOHOS::HiviewDFX::__anonbed8e8cc0110::Initializer36 Initializer()
37 {
38 g_fd = open("/dev/null", O_RDWR | O_CREAT | O_TRUNC, 0600); //0600 for file mode
39 if (g_fd <= 0) {
40 printf("failed to open file, exit\n");
41 isInit = false;
42 return;
43 }
44 isInit = true;
45 }
46
~InitializerOHOS::HiviewDFX::__anonbed8e8cc0110::Initializer47 ~Initializer()
48 {
49 if (isInit) {
50 (void)close(g_fd);
51 }
52 }
53
54 bool isInit = false;
55 };
56 Initializer g_initializer;
57 }
58
HiviewServiceDumpFuzzTest(const uint8_t * data,size_t size)59 static void HiviewServiceDumpFuzzTest(const uint8_t* data, size_t size)
60 {
61 HiviewPlatform platform;
62 (void)platform.IsReady();
63 std::string strData = std::string(reinterpret_cast<const char*>(data), size);
64 g_hiviewService.DumpRequestDispatcher(g_fd, {});
65 g_hiviewService.DumpRequestDispatcher(g_fd, {strData});
66 }
67
HiViewServiceSnapshotTraceFuzzTest(const uint8_t * data,size_t size)68 static void HiViewServiceSnapshotTraceFuzzTest(const uint8_t* data, size_t size)
69 {
70 std::string tag = std::string(reinterpret_cast<const char*>(data), size);
71 std::vector<std::string> tagsGroup;
72 tagsGroup.emplace_back(tag);
73 g_hiviewService.OpenSnapshotTrace(tagsGroup);
74 }
75
HiViewServiceDumpSnapshotTraceFuzzTest(const uint8_t * data,size_t size)76 static void HiViewServiceDumpSnapshotTraceFuzzTest(const uint8_t* data, size_t size)
77 {
78 UCollect::TraceCaller caller = static_cast<UCollect::TraceCaller>(*data);
79 g_hiviewService.DumpSnapshotTrace(caller);
80 }
81
HiViewServiceRecordingTraceFuzzTest(const uint8_t * data,size_t size)82 static void HiViewServiceRecordingTraceFuzzTest(const uint8_t* data, size_t size)
83 {
84 std::string tags = std::string(reinterpret_cast<const char*>(data), size);
85 g_hiviewService.OpenRecordingTrace(tags);
86 }
87 } // namespace HiviewDFX
88 } // namespace OHOS
89
90 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)91 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
92 {
93 /* Run your code on data */
94 if (!OHOS::HiviewDFX::g_initializer.isInit) {
95 printf("failed to init environment, exit\n");
96 return 0;
97 }
98 OHOS::HiviewDFX::HiviewServiceDumpFuzzTest(data, size);
99 OHOS::HiviewDFX::HiViewServiceSnapshotTraceFuzzTest(data, size);
100 OHOS::HiviewDFX::HiViewServiceDumpSnapshotTraceFuzzTest(data, size);
101 OHOS::HiviewDFX::HiViewServiceRecordingTraceFuzzTest(data, size);
102 return 0;
103 }
104
105