• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "perfrequest_fuzzer.h"
17 #include "socperf_client.h"
18 
19 #include <securec.h>
20 #include <cstddef>
21 #include <cstdint>
22 #include <cstdlib>
23 
24 #ifndef errno_t
25 typedef int errno_t;
26 #endif
27 
28 #ifndef EOK
29 #define EOK 0
30 #endif
31 
32 namespace OHOS {
33 namespace SOCPERF {
34     const uint8_t* g_data = nullptr;
35     size_t g_size = 0;
36     size_t g_pos;
37 
38     /*
39     * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
40     * tips: only support basic type
41     */
42     template<class T>
GetData()43     T GetData()
44     {
45         T object {};
46         size_t objectSize = sizeof(object);
47         if (g_data == nullptr || objectSize > g_size - g_pos) {
48             return object;
49         }
50         errno_t ret = memcpy_s(&object, objectSize, g_data + g_pos, objectSize);
51         if (ret != EOK) {
52             return {};
53         }
54         g_pos += objectSize;
55         return object;
56     }
57 
58     /*
59     * get a string from g_data
60     */
GetStringFromData(int strlen)61     std::string GetStringFromData(int strlen)
62     {
63         if (strlen <= 0) {
64             return "";
65         }
66         char cstr[strlen];
67         cstr[strlen - 1] = '\0';
68         for (int i = 0; i < strlen - 1; i++) {
69             char tmp = GetData<char>();
70             if (tmp == '\0') {
71                 tmp = '1';
72             }
73             cstr[i] = tmp;
74         }
75         std::string str(cstr);
76         return str;
77     }
78 
PerfRequestFuzzTest(const uint8_t * data,size_t size)79     bool PerfRequestFuzzTest(const uint8_t* data, size_t size)
80     {
81         if (data == nullptr) {
82             return false;
83         }
84 
85         // initialize
86         g_data = data;
87         g_size = size;
88         g_pos = 0;
89 
90         // getdata
91         std::string msg;
92         int32_t cmdId = GetData<int32_t>();
93         msg = GetStringFromData(int(size) - sizeof(int32_t));
94         OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequest(cmdId, msg);
95 
96         return true;
97     }
98 
PerfRequestExFuzzTest(const uint8_t * data,size_t size)99     bool PerfRequestExFuzzTest(const uint8_t* data, size_t size)
100     {
101         if (data == nullptr) {
102             return false;
103         }
104 
105         // initialize
106         g_data = data;
107         g_size = size;
108         g_pos = 0;
109 
110         // getdata
111         std::string msg;
112         int32_t cmdId = GetData<int32_t>();
113         bool onOffTag = GetData<bool>();
114         msg = GetStringFromData(int(size) - sizeof(int32_t) - sizeof(bool));
115         OHOS::SOCPERF::SocPerfClient::GetInstance().PerfRequestEx(cmdId, onOffTag, msg);
116 
117         return true;
118     }
119 
PowerLimitBoostFuzzTest(const uint8_t * data,size_t size)120     bool PowerLimitBoostFuzzTest(const uint8_t* data, size_t size)
121     {
122         if (data == nullptr) {
123             return false;
124         }
125 
126         // initialize
127         g_data = data;
128         g_size = size;
129         g_pos = 0;
130 
131         // getdata
132         std::string msg;
133         bool onOffTag = GetData<bool>();
134         msg = GetStringFromData(int(size) - sizeof(bool));
135         OHOS::SOCPERF::SocPerfClient::GetInstance().PowerLimitBoost(onOffTag, msg);
136 
137         return true;
138     }
139 
ThermalLimitBoostFuzzTest(const uint8_t * data,size_t size)140     bool ThermalLimitBoostFuzzTest(const uint8_t* data, size_t size)
141     {
142         if (data == nullptr) {
143             return false;
144         }
145 
146         // initialize
147         g_data = data;
148         g_size = size;
149         g_pos = 0;
150 
151         // getdata
152         std::string msg;
153         bool onOffTag = GetData<bool>();
154         msg = GetStringFromData(int(size) - sizeof(bool));
155         OHOS::SOCPERF::SocPerfClient::GetInstance().ThermalLimitBoost(onOffTag, msg);
156 
157         return true;
158     }
159 
LimitRequestFuzzTest(const uint8_t * data,size_t size)160     bool LimitRequestFuzzTest(const uint8_t* data, size_t size)
161     {
162         if (data == nullptr) {
163             return false;
164         }
165 
166         // initialize
167         g_data = data;
168         g_size = size;
169         g_pos = 0;
170 
171         // getdata
172         std::vector<int32_t> tags;
173         std::vector<int64_t> configs;
174         std::string msg;
175         int32_t tagsNumber = GetData<int32_t>();
176         int64_t configsNumber = GetData<int64_t>();
177         int32_t clientId = GetData<int32_t>();
178         tags.push_back(tagsNumber);
179         configs.push_back(configsNumber);
180         msg = GetStringFromData(int(size) - sizeof(int32_t) - sizeof(int32_t) - sizeof(int64_t));
181         OHOS::SOCPERF::SocPerfClient::GetInstance().LimitRequest(clientId, tags, configs, msg);
182 
183         return true;
184     }
185 } // namespace SOCPERF
186 } // namespace OHOS
187 
188 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)189 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
190 {
191     /* Run your code on data */
192     OHOS::SOCPERF::PerfRequestFuzzTest(data, size);
193     OHOS::SOCPERF::PerfRequestExFuzzTest(data, size);
194     OHOS::SOCPERF::PowerLimitBoostFuzzTest(data, size);
195     OHOS::SOCPERF::ThermalLimitBoostFuzzTest(data, size);
196     OHOS::SOCPERF::LimitRequestFuzzTest(data, size);
197     return 0;
198 }