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 "hisyseventmanager_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <vector>
21
22 #include "hisysevent_manager.h"
23
24 namespace OHOS {
25 namespace HiviewDFX {
26 namespace {
27 class TestQueryCallback : public HiSysEventQueryCallback {
28 public:
TestQueryCallback()29 TestQueryCallback() {}
~TestQueryCallback()30 virtual ~TestQueryCallback() {}
31
OnQuery(std::shared_ptr<std::vector<HiSysEventRecord>> sysEvents)32 void OnQuery(std::shared_ptr<std::vector<HiSysEventRecord>> sysEvents) override
33 {}
34
OnComplete(int32_t reason,int32_t total)35 void OnComplete(int32_t reason, int32_t total) override
36 {}
37 };
38
HiSysEventRecordTest(const HiSysEventRecord & record,const std::string & data)39 void HiSysEventRecordTest(const HiSysEventRecord& record, const std::string& data)
40 {
41 (void)record.AsJson();
42 (void)record.GetDomain();
43 (void)record.GetEventName();
44 (void)record.GetLevel();
45 (void)record.GetTag();
46 (void)record.GetTimeZone();
47 (void)record.GetEventType();
48 (void)record.GetTraceFlag();
49 (void)record.GetPid();
50 (void)record.GetTid();
51 (void)record.GetUid();
52 (void)record.GetPspanId();
53 (void)record.GetSpanId();
54 (void)record.GetTime();
55 (void)record.GetTraceId();
56 std::vector<std::string> params;
57 record.GetParamNames(params);
58
59 int64_t intValue = 0;
60 (void)record.GetParamValue(data, intValue);
61 uint64_t uintValue = 0;
62 (void)record.GetParamValue(data, uintValue);
63 double dValue = 0;
64 (void)record.GetParamValue(data, dValue);
65 std::string strValue;
66 (void)record.GetParamValue(data, strValue);
67 std::vector<int64_t> intValues;
68 (void)record.GetParamValue(data, intValues);
69 std::vector<uint64_t> uintValues;
70 (void)record.GetParamValue(data, uintValues);
71 std::vector<double> dValues;
72 (void)record.GetParamValue(data, dValues);
73 std::vector<std::string> strValues;
74 (void)record.GetParamValue(data, strValues);
75 }
76
HiSysEventQueryTest(const std::string & strData,int64_t intData)77 void HiSysEventQueryTest(const std::string& strData, int64_t intData)
78 {
79 auto callback = std::make_shared<TestQueryCallback>();
80 QueryArg arg(0, intData, intData);
81 std::vector<QueryRule> rules = { QueryRule(strData, {strData}) };
82 (void)HiSysEventManager::Query(arg, rules, callback);
83 }
84 }
85
HiSysEventRecordFuzzTest(const uint8_t * data,size_t size)86 void HiSysEventRecordFuzzTest(const uint8_t* data, size_t size)
87 {
88 std::string strData((const char*) data, size);
89 const std::string jsonStr = R"~({"domain_":"test_domain","name_":"test_name"})~";
90 HiSysEventRecord record(jsonStr);
91 HiSysEventRecordTest(record, strData);
92 }
93
HiSysEventQueryFuzzTest(const uint8_t * data,size_t size)94 void HiSysEventQueryFuzzTest(const uint8_t* data, size_t size)
95 {
96 std::string strData((const char*) data, size);
97 int64_t intData = static_cast<int64_t>(*data);
98 HiSysEventQueryTest(strData, intData);
99 }
100 } // namespace HiviewDFX
101 } // namespace OHOS
102
103 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)104 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
105 {
106 /* Run your code on data */
107 OHOS::HiviewDFX::HiSysEventRecordFuzzTest(data, size);
108 OHOS::HiviewDFX::HiSysEventQueryFuzzTest(data, size);
109 return 0;
110 }
111
112