1 /*
2 * Copyright (c) 2022 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 "eventservice_fuzzer.h"
17
18 #include <cinttypes>
19 #include <cstddef>
20 #include <cstdint>
21
22 #include "file_util.h"
23 #include "hiview_platform.h"
24 #include "query_sys_event_callback_stub.h"
25 #include "sys_event_service_ohos.h"
26
27 namespace OHOS {
28 namespace HiviewDFX {
29 namespace {
30 const std::string CONFIG_FILE_PATH = "/data/test/hiview_platform_config";
31 }
32
33 class TestQueryCallback : public QuerySysEventCallbackStub {
34 public:
OnQuery(const std::vector<std::u16string> & sysEvent,const std::vector<int64_t> & seq)35 void OnQuery(const std::vector<std::u16string>& sysEvent, const std::vector<int64_t>& seq) override {}
36
OnComplete(int32_t reason,int32_t total,int64_t seq)37 void OnComplete(int32_t reason, int32_t total, int64_t seq) override {}
38
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)39 int32_t OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
40 MessageOption& option) override
41 {
42 return 0;
43 }
44 };
45
CreateConfigFile()46 bool CreateConfigFile()
47 {
48 std::string content = "DEFAULT_PLUGIN_CONFIG_NAME = \"plugin_config\"\n" \
49 "PLUGIN_CONFIG_FILE_DIR = \"/system/etc/hiview/\"\n" \
50 "DYNAMIC_LIB_SEARCH_DIR = \"/system/lib/\"\n" \
51 "DYNAMIC_LIB64_SEARCH_DIR = \"/system/lib64/\"\n" \
52 "WORK_DIR = \"/data/test/hiview_fuzz/\"\n" \
53 "COMMERCIAL_WORK_DIR = \"/log/LogService/\"\n" \
54 "PERSIST_DIR = \"/log/hiview/\"";
55 return FileUtil::SaveStringToFile(CONFIG_FILE_PATH, content);
56 }
57
InitEnvironment()58 bool InitEnvironment()
59 {
60 if (!CreateConfigFile()) {
61 printf("failed to create config file, exit\n");
62 return false;
63 }
64 HiviewPlatform &platform = HiviewPlatform::GetInstance();
65 return platform.InitEnvironment(CONFIG_FILE_PATH);
66 }
67
SysEventQueryTest(const uint8_t * data,size_t size)68 int32_t SysEventQueryTest(const uint8_t* data, size_t size)
69 {
70 static OHOS::sptr<OHOS::HiviewDFX::IQuerySysEventCallback> callback = new(std::nothrow) TestQueryCallback();
71 if (callback == nullptr) {
72 printf("callback is null, exit.\n");
73 return -1;
74 }
75 int64_t int64Data = static_cast<int64_t>(*data);
76 int32_t int32Data = static_cast<int32_t>(*data);
77 QueryArgument arg(int64Data, int64Data, int32Data);
78 std::string strData = std::string(reinterpret_cast<const char*>(data), size);
79 SysEventQueryRuleGroupOhos rules = { SysEventQueryRule(strData, { strData }) };
80 auto service = SysEventServiceOhos::GetInstance();
81 if (service == nullptr) {
82 printf("SysEventServiceOhos service is null.\n");
83 return -1;
84 }
85 return service->Query(arg, rules, callback);
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 static bool initialized = OHOS::HiviewDFX::InitEnvironment();
95 if (!initialized) {
96 printf("failed to init environment, exit\n");
97 return -1;
98 }
99 (void)OHOS::HiviewDFX::SysEventQueryTest(data, size);
100 return 0;
101 }
102
103