• 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 /* This files contains faultlog fuzzer test modules. */
17 
18 #include "thermal_fuzzer_test.h"
19 
20 #include "thermal_service.h"
21 #include "thermal_common.h"
22 #include "message_parcel.h"
23 #include "securec.h"
24 
25 using namespace std;
26 using namespace OHOS;
27 using namespace OHOS::PowerMgr;
28 namespace {
29 auto& g_thermalMgrClient = ThermalMgrClient::GetInstance();
30 const int32_t REWIND_READ_DATA = 0;
31 sptr<ThermalService> g_service = nullptr;
32 } // namespace
33 
34 static sptr<IThermalActionCallback> actionCb_;
35 static sptr<IThermalActionCallback> testActionCb_;
36 static sptr<IThermalTempCallback> tempCb_ = new ThermalFuzzerTest::ThermalTempTestCallback();
37 static sptr<IThermalTempCallback> testTempCb_;
38 static sptr<IThermalLevelCallback> levelCb_ = new ThermalFuzzerTest::ThermalLevelTestCallback();
39 static sptr<IThermalLevelCallback> testLevelCb_;
40 
OnThermalActionChanged(ActionCallbackMap & actionCbMap)41 bool ThermalFuzzerTest::ThermalActionTestCallback::OnThermalActionChanged(ActionCallbackMap& actionCbMap)
42 {
43     THERMAL_HILOGI(COMP_SVC, "action callback");
44     return true;
45 }
46 
OnThermalTempChanged(TempCallbackMap & tempCbMap)47 bool ThermalFuzzerTest::ThermalTempTestCallback::OnThermalTempChanged(TempCallbackMap& tempCbMap)
48 {
49     THERMAL_HILOGI(COMP_SVC, "temp callback");
50     return true;
51 }
52 
GetThermalLevel(ThermalLevel level)53 bool ThermalFuzzerTest::ThermalLevelTestCallback::GetThermalLevel(ThermalLevel level)
54 {
55     THERMAL_HILOGI(COMP_SVC, "level is: %{public}d", static_cast<int32_t>(level));
56     return true;
57 }
58 
GetVector(const uint8_t * data,size_t size)59 static std::vector<std::string> GetVector(const uint8_t* data, size_t size)
60 {
61     int32_t inputData;
62     std::vector<std::string> list;
63     if (size < sizeof(inputData)) {
64         return list;
65     }
66     if (memcpy_s(&inputData, sizeof(inputData), data, sizeof(inputData)) != EOK) {
67         return list;
68     }
69     int32_t minVectorSize = 0;
70     int32_t maxVectorSize = 5000;
71     int32_t length = (inputData < minVectorSize) ? minVectorSize : inputData;
72     length = (length > maxVectorSize) ? maxVectorSize : length;
73     list.resize(length);
74     for (auto &item : list) {
75         item = std::string(reinterpret_cast<const char *>(data), size);
76     }
77     return list;
78 }
79 
TestUnSubscribeTemp()80 static void TestUnSubscribeTemp()
81 {
82     g_thermalMgrClient.UnSubscribeThermalTempCallback(tempCb_);
83     g_thermalMgrClient.UnSubscribeThermalTempCallback(testTempCb_);
84 }
85 
TestSubscribeTemp(const uint8_t * data,size_t size)86 static void TestSubscribeTemp(const uint8_t* data, size_t size)
87 {
88     std::vector<std::string> typeList = GetVector(data, size);
89     g_thermalMgrClient.SubscribeThermalTempCallback(typeList, tempCb_);
90     g_thermalMgrClient.SubscribeThermalTempCallback(typeList, testTempCb_);
91     TestUnSubscribeTemp();
92 }
93 
TestUnSubscribeAction()94 static void TestUnSubscribeAction()
95 {
96     g_thermalMgrClient.UnSubscribeThermalActionCallback(actionCb_);
97     g_thermalMgrClient.UnSubscribeThermalActionCallback(testActionCb_);
98 }
99 
TestSubscribeAction(const uint8_t * data,size_t size)100 static void TestSubscribeAction(const uint8_t* data, size_t size)
101 {
102     if (data == nullptr || size == 0) {
103         return;
104     }
105     std::string desc(reinterpret_cast<const char *>(data), size);
106     std::vector<std::string> actionList = GetVector(data, size);
107     g_thermalMgrClient.SubscribeThermalActionCallback(actionList, desc, actionCb_);
108     g_thermalMgrClient.SubscribeThermalActionCallback(actionList, desc, testActionCb_);
109     TestUnSubscribeAction();
110 }
111 
TestUnSubscribeLevel()112 static void TestUnSubscribeLevel()
113 {
114     g_thermalMgrClient.UnSubscribeThermalLevelCallback(levelCb_);
115 }
116 
TestSubscribeLevel(const uint8_t * data,size_t size)117 static void TestSubscribeLevel([[maybe_unused]] const uint8_t* data, [[maybe_unused]] size_t size)
118 {
119     g_thermalMgrClient.SubscribeThermalLevelCallback(levelCb_);
120     TestUnSubscribeLevel();
121 }
122 
TestGetLevel(const uint8_t * data,size_t size)123 static void TestGetLevel([[maybe_unused]] const uint8_t* data, [[maybe_unused]] size_t size)
124 {
125     g_thermalMgrClient.GetThermalLevel();
126 }
127 
TestSetScene(const uint8_t * data,size_t size)128 static void TestSetScene(const uint8_t* data, size_t size)
129 {
130     if (data == nullptr || size == 0) {
131         return;
132     }
133     std::string sceneName(reinterpret_cast<const char *>(data), size);
134     g_thermalMgrClient.SetScene(sceneName);
135 }
136 
TestGetSensorTemp(const uint8_t * data,size_t size)137 static void TestGetSensorTemp(const uint8_t* data, size_t size)
138 {
139     int32_t code = 0;
140     if (size < sizeof(code)) {
141         return;
142     }
143     if (memcpy_s(&code, sizeof(code), data, sizeof(code)) != EOK) {
144         return;
145     }
146 
147     SensorType sensorType = static_cast<SensorType>(code);
148     g_thermalMgrClient.GetThermalSensorTemp(sensorType);
149 }
150 
ThermalServiceStub(const uint8_t * data,size_t size)151 static void ThermalServiceStub(const uint8_t* data, size_t size)
152 {
153     uint32_t code;
154     if (size < sizeof(code)) {
155         return;
156     }
157     if (memcpy_s(&code, sizeof(code), data, sizeof(code)) != EOK) {
158         return;
159     }
160 
161     MessageParcel datas;
162     datas.WriteInterfaceToken(ThermalService::GetDescriptor());
163     datas.WriteBuffer(data, size);
164     datas.RewindRead(REWIND_READ_DATA);
165     MessageParcel reply;
166     MessageOption option;
167     if (g_service == nullptr) {
168         g_service = DelayedSpSingleton<ThermalService>::GetInstance();
169         g_service->OnStart();
170     }
171     g_service->OnRemoteRequest(code, datas, reply, option);
172 }
173 
174 static std::vector<std::function<void(const uint8_t*, size_t)>> fuzzFunc = {
175     &TestSubscribeTemp,
176     &TestSubscribeLevel,
177     &TestGetLevel,
178     &TestGetSensorTemp,
179     &TestSubscribeAction,
180     &TestSetScene
181 };
182 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)183 static bool DoSomethingInterestingWithMyAPI(const uint8_t* data, size_t size)
184 {
185     std::random_device rd;
186     std::default_random_engine engine(rd());
187     std::uniform_int_distribution<int32_t> randomNum(0, fuzzFunc.size() - 1);
188     int32_t number = randomNum(engine);
189     fuzzFunc[number](data, size);
190     ThermalServiceStub(data, size);
191     return true;
192 }
193 
194 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)195 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
196 {
197     /* Run your code on data */
198     DoSomethingInterestingWithMyAPI(data, size);
199     return 0;
200 }
201