1 /*
2 * Copyright (c) 2022-2023 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 "sysevent_source.h"
25 #include "hiview_platform.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::__anonc185a5d20111::Initializer34 Initializer()
35 {
36 HiviewContext context;
37 g_eventSource.SetHiviewContext(&context);
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::__anonc185a5d20111::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 HiviewPlatform platform;
65 (void)platform.IsReady();
66 std::string strData = std::string(reinterpret_cast<const char*>(data), size);
67 g_eventSource.Dump(g_fd, {strData});
68 g_eventSource.Dump(g_fd, {strData, strData});
69 }
70 } // namespace HiviewDFX
71 } // namespace OHOS
72
73 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)74 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
75 {
76 /* Run your code on data */
77 if (!OHOS::HiviewDFX::g_initializer.isInit) {
78 printf("failed to init environment, exit\n");
79 return 0;
80 }
81 OHOS::HiviewDFX::SysEventSourceDumpTest(data, size);
82 return 0;
83 }
84
85