• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "batterystats_fuzzer.h"
17 
18 #include <iostream>
19 #include <cstddef>
20 #include <cstdint>
21 #include <cstdlib>
22 #include <random>
23 #include <ctime>
24 #include "securec.h"
25 #include "battery_stats_client.h"
26 
27 using namespace std;
28 using namespace OHOS;
29 using namespace OHOS::PowerMgr;
30 
31 namespace {
32 auto& g_batterystatsClient = BatteryStatsClient::GetInstance();
33 constexpr size_t DATANUM = 4;
34 constexpr int32_t INDEX_0 = 0;
35 }
36 
GetBatteryStats(const uint8_t * data)37 static void GetBatteryStats(const uint8_t* data)
38 {
39     int32_t type[1];
40     int32_t idSize = 4;
41     if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) {
42         return;
43     }
44 
45     g_batterystatsClient.GetBatteryStats();
46 }
47 
SetOnBattery(const uint8_t * data)48 static void SetOnBattery(const uint8_t* data)
49 {
50     int32_t type[1];
51     int32_t idSize = 4;
52     if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) {
53         return;
54     }
55 
56     g_batterystatsClient.SetOnBattery(type[0]);
57 }
58 
GetAppStatsMah(const uint8_t * data)59 static void GetAppStatsMah(const uint8_t* data)
60 {
61     int32_t type[1];
62     int32_t idSize = 4;
63     if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) {
64         return;
65     }
66 
67     g_batterystatsClient.GetAppStatsMah(type[0]);
68 }
69 
GetAppStatsPercent(const uint8_t * data)70 static void GetAppStatsPercent(const uint8_t* data)
71 {
72     int32_t type[1];
73     int32_t idSize = 4;
74     if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) {
75         return;
76     }
77 
78     g_batterystatsClient.GetAppStatsPercent(type[0]);
79 }
80 
GetPartStatsMah(const uint8_t * data)81 static void GetPartStatsMah(const uint8_t* data)
82 {
83     int32_t type[1];
84     int32_t idSize = 4;
85     if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) {
86         return;
87     }
88 
89     g_batterystatsClient.GetPartStatsMah(static_cast <BatteryStatsInfo::ConsumptionType>(type[0]));
90 }
91 
GetPartStatsPercent(const uint8_t * data)92 static void GetPartStatsPercent(const uint8_t* data)
93 {
94     int32_t type[1];
95     int32_t idSize = 4;
96     if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) {
97         return;
98     }
99 
100     g_batterystatsClient.GetPartStatsPercent(static_cast <BatteryStatsInfo::ConsumptionType>(type[0]));
101 }
102 
GetTotalTimeSecond(const uint8_t * data,size_t size)103 static void GetTotalTimeSecond(const uint8_t* data, size_t size)
104 {
105     int32_t type[1];
106     size_t idSize = 4;
107     int32_t uid[1];
108     if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) {
109         return;
110     }
111     if (size <= (idSize + DATANUM) || (memcpy_s(uid, sizeof(uid), (data + DATANUM), idSize) != EOK)) {
112         uid[INDEX_0] = type[INDEX_0];
113     }
114 
115     g_batterystatsClient.GetTotalTimeSecond(static_cast <OHOS::PowerMgr::StatsUtils::StatsType>(type[0]), uid[0]);
116 }
117 
GetTotalDataBytes(const uint8_t * data,size_t size)118 static void GetTotalDataBytes(const uint8_t* data, size_t size)
119 {
120     int32_t type[1];
121     size_t idSize = 4;
122     int32_t uid[1];
123     if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) {
124         return;
125     }
126     if (size <= (idSize + DATANUM) || (memcpy_s(uid, sizeof(uid), (data + DATANUM), idSize) != EOK)) {
127         uid[INDEX_0] = type[INDEX_0];
128     }
129 
130     g_batterystatsClient.GetTotalDataBytes(static_cast <OHOS::PowerMgr::StatsUtils::StatsType>(type[0]), uid[0]);
131 }
132 
Reset(const uint8_t * data)133 static void Reset(const uint8_t* data)
134 {
135     int32_t type[1];
136     int32_t idSize = 4;
137     if ((memcpy_s(type, sizeof(type), data, idSize)) != EOK) {
138         return;
139     }
140 
141     g_batterystatsClient.Reset();
142 }
143 
144 namespace OHOS {
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)145 bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
146 {
147     int32_t idSize = 4;
148     int32_t cond[1];
149     if (static_cast<int32_t>(size) > idSize) {
150         if ((memcpy_s(cond, sizeof(cond), data, idSize)) != EOK) {
151             return false;
152         }
153         std::random_device rd;
154         std::default_random_engine engine(rd());
155         std::uniform_int_distribution<int32_t> randomNum(static_cast<int32_t>(ApiNumber::NUM_ZERO),
156             static_cast<int32_t>(ApiNumber::NUM_END) - 1);
157         int32_t number = randomNum(engine);
158 
159         switch (static_cast<ApiNumber>(number)) {
160             case ApiNumber::NUM_ZERO:
161                 GetBatteryStats(data);
162                 break;
163             case ApiNumber::NUM_ONE:
164                 SetOnBattery(data);
165                 break;
166             case ApiNumber::NUM_TWO:
167                 GetAppStatsMah(data);
168                 break;
169             case ApiNumber::NUM_THREE:
170                 GetAppStatsPercent(data);
171                 break;
172             case ApiNumber::NUM_FOUR:
173                 GetPartStatsMah(data);
174                 break;
175             case ApiNumber::NUM_FIVE:
176                 GetPartStatsPercent(data);
177                 break;
178             case ApiNumber::NUM_SIX:
179                 GetTotalTimeSecond(data, size);
180                 break;
181             case ApiNumber::NUM_SEVEN:
182                 GetTotalDataBytes(data, size);
183                 break;
184             case ApiNumber::NUM_EIGHT:
185                 Reset(data);
186                 break;
187             case ApiNumber::NUM_NINE:
188                 g_batterystatsClient.GetLastError();
189                 break;
190             default:
191                 break;
192         }
193     }
194 
195     return true;
196 }
197 }
198 
199 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)200 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
201 {
202     /* Run your code on data */
203     OHOS::DoSomethingInterestingWithMyAPI(data, size);
204     return 0;
205 }
206 
207