1 /*
2 * Copyright (c) 2022-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 "eventsource_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <fcntl.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23
24 #include "hiview_platform.h"
25 #include "sysevent_source.h"
26
27 namespace OHOS {
28 namespace HiviewDFX {
29 namespace {
30 SysEventSource g_eventSource;
31 int g_fd = 0;
32
33 struct Initializer {
InitializerOHOS::HiviewDFX::__anonceaec1090110::Initializer34 Initializer()
35 {
36 HiviewPlatform platform;
37 g_eventSource.SetHiviewContext(&platform);
38 g_eventSource.OnLoad();
39
40 g_fd = open("/dev/null", O_RDWR | O_CREAT | O_TRUNC, 0644); //0644 for file mode
41 if (g_fd <= 0) {
42 printf("failed to open file, exit\n");
43 isInit = false;
44 return;
45 }
46 isInit = true;
47 }
48
~InitializerOHOS::HiviewDFX::__anonceaec1090110::Initializer49 ~Initializer()
50 {
51 if (isInit) {
52 g_eventSource.OnUnload();
53 (void)close(g_fd);
54 }
55 }
56
57 bool isInit = false;
58 };
59 Initializer g_initializer;
60 }
61
SysEventSourceDumpTest(const uint8_t * data,size_t size)62 static void SysEventSourceDumpTest(const uint8_t* data, size_t size)
63 {
64 std::string strData = std::string(reinterpret_cast<const char*>(data), size);
65 g_eventSource.Dump(g_fd, {strData});
66 g_eventSource.Dump(g_fd, {strData, strData});
67 }
68 } // namespace HiviewDFX
69 } // namespace OHOS
70
71 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)72 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
73 {
74 /* Run your code on data */
75 if (!OHOS::HiviewDFX::g_initializer.isInit) {
76 printf("failed to init environment, exit\n");
77 return 0;
78 }
79 OHOS::HiviewDFX::SysEventSourceDumpTest(data, size);
80 return 0;
81 }
82