• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "config_manager_fuzzer.h"
17 
18 #include <string>
19 #include <fuzzer/FuzzedDataProvider.h>
20 #include "securec.h"
21 
22 #define private public
23 #define protected public
24 #include "base_config.h"
25 #include "config_data_manager.h"
26 #include "i_model_info.h"
27 #include "config_manager.h"
28 #include "config_operator.h"
29 #include "config_subscriber.h"
30 #include "event_config.h"
31 #include "json_cfg.h"
32 #include "security_guard_utils.h"
33 #include "model_analysis_define.h"
34 #include "model_cfg_marshalling.h"
35 #include "model_config.h"
36 #include "security_guard_log.h"
37 #undef private
38 #undef protected
39 
40 using namespace OHOS::Security::SecurityGuard;
41 namespace {
42     constexpr int MAX_STRING_SIZE = 1024;
43 }
44 namespace OHOS {
ConfigDataManagerFuzzTest(const uint8_t * data,size_t size)45 bool ConfigDataManagerFuzzTest(const uint8_t* data, size_t size)
46 {
47     FuzzedDataProvider fdp(data, size);
48     int64_t eventId = fdp.ConsumeIntegral<int64_t>();
49     uint32_t modelId = fdp.ConsumeIntegral<uint32_t>();
50     std::string table = fdp.ConsumeRandomLengthString(MAX_STRING_SIZE);
51     std::set<int64_t> eventIds{eventId};
52     ModelCfg modelCfg = {};
53     EventCfg eventCfg = {};
54     ConfigDataManager::GetInstance().InsertModelMap(modelId, modelCfg);
55     ConfigDataManager::GetInstance().InsertEventMap(eventId, eventCfg);
56     ConfigDataManager::GetInstance().InsertModelToEventMap(modelId, eventIds);
57     ConfigDataManager::GetInstance().InsertEventToTableMap(eventId, table);
58     ConfigDataManager::GetInstance().GetEventIds(modelId);
59     ConfigDataManager::GetInstance().GetAllEventIds();
60     ConfigDataManager::GetInstance().GetAllModelIds();
61     ConfigDataManager::GetInstance().GetModelConfig(modelId, modelCfg);
62     ConfigDataManager::GetInstance().GetEventConfig(eventId, eventCfg);
63     ConfigDataManager::GetInstance().GetTableFromEventId(eventId);
64     ConfigDataManager::GetInstance().ResetModelMap();
65     ConfigDataManager::GetInstance().ResetEventMap();
66     ConfigDataManager::GetInstance().ResetModelToEventMap();
67     ConfigDataManager::GetInstance().ResetEventToTableMap();
68     return true;
69 }
70 
ConfigManagerFuzzTest(const uint8_t * data,size_t size)71 bool ConfigManagerFuzzTest(const uint8_t* data, size_t size)
72 {
73     ConfigManager::GetInstance().StartUpdate();
74     return true;
75 }
76 
EventConfigFuzzTest(const uint8_t * data,size_t size)77 bool EventConfigFuzzTest(const uint8_t* data, size_t size)
78 {
79     FuzzedDataProvider fdp(data, size);
80     int mode = fdp.ConsumeIntegral<int32_t>();
81     EventConfig config{};
82     nlohmann::json jsonObj;
83     EventCfg cfg{};
84     std::vector<EventCfg> cfgs{cfg};
85     config.Load(mode);
86     config.Parse();
87     config.Update();
88     config.ParseEventConfig(cfgs, jsonObj);
89     config.CacheEventConfig(cfgs);
90     config.CacheEventToTable(cfgs);
91     return true;
92 }
93 
ModelConfigFuzzTest(const uint8_t * data,size_t size)94 bool ModelConfigFuzzTest(const uint8_t* data, size_t size)
95 {
96     FuzzedDataProvider fdp(data, size);
97     int mode = fdp.ConsumeIntegral<int32_t>();
98     ModelConfig config{};
99     nlohmann::json jsonObj;
100     ModelCfg cfg = {};
101     std::vector<ModelCfg> cfgs{cfg};
102     config.Load(mode);
103     config.Parse();
104     config.Update();
105     config.ParseModelConfig(cfgs, jsonObj);
106     config.CacheModelConfig(cfgs);
107     config.CacheModelToEvent(cfgs);
108     return true;
109 }
110 
JsonConfigFuzzTest(const uint8_t * data,size_t size)111 void JsonConfigFuzzTest(const uint8_t* data, size_t size)
112 {
113     FuzzedDataProvider fdp(data, size);
114     uint64_t uint64 = fdp.ConsumeIntegral<uint64_t>();
115     int64_t int64 = fdp.ConsumeIntegral<int64_t>();
116     int32_t int32 = fdp.ConsumeIntegral<int32_t>();
117     std::string string = fdp.ConsumeRandomLengthString(MAX_STRING_SIZE);
118     std::vector<int32_t> vec32;
119     std::vector<int64_t> vec64;
120     nlohmann::json jsonObj {
121         { string, uint64 }
122     };
123     JsonCfg::Unmarshal(uint64, jsonObj, string);
124     nlohmann::json jsonObj1 {
125         { string, int64 }
126     };
127     JsonCfg::Unmarshal(int64, jsonObj1, string);
128     nlohmann::json jsonObj2 {
129         { string, {int32} }
130     };
131     JsonCfg::Unmarshal(vec32, jsonObj2, string);
132     nlohmann::json jsonObj3 {
133         { string, {int64} }
134     };
135     JsonCfg::Unmarshal(vec64, jsonObj3, string);
136 }
137 
SecurityGuardUtilsFuzzTest(const uint8_t * data,size_t size)138 void SecurityGuardUtilsFuzzTest(const uint8_t* data, size_t size)
139 {
140     FuzzedDataProvider fdp(data, size);
141     int64_t int64 = fdp.ConsumeIntegral<int64_t>();
142     uint32_t uint32 = fdp.ConsumeIntegral<uint32_t>();
143     std::string string = fdp.ConsumeRandomLengthString(MAX_STRING_SIZE);
144     unsigned long long value = 0;
145     SecurityGuardUtils::StrToU32(string, uint32);
146     SecurityGuardUtils::StrToI64(string, int64);
147     SecurityGuardUtils::StrToULL(string, value);
148     SecurityGuardUtils::CopyFile(string, string);
149 }
150 }  // namespace OHOS
151 
152 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)153 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
154 {
155     /* Run your code on date */
156     OHOS::ConfigDataManagerFuzzTest(data, size);
157     OHOS::ConfigManagerFuzzTest(data, size);
158     OHOS::EventConfigFuzzTest(data, size);
159     OHOS::ModelConfigFuzzTest(data, size);
160     OHOS::JsonConfigFuzzTest(data, size);
161     OHOS::SecurityGuardUtilsFuzzTest(data, size);
162     return 0;
163 }
164