1 /*
2 * Copyright 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #define LOG_TAG "BatteryNotifierFuzzer"
17 #include <batterystats/IBatteryStats.h>
18 #include <binder/IServiceManager.h>
19 #include <utils/String16.h>
20 #include <android/log.h>
21 #include <mediautils/SchedulingPolicyService.h>
22 #include <mediautils/TidWrapper.h>
23 #include "fuzzer/FuzzedDataProvider.h"
24 using android::IBatteryStats;
25 using android::IBinder;
26 using android::IInterface;
27 using android::IServiceManager;
28 using android::sp;
29 using android::String16;
30 using android::defaultServiceManager;
31 using android::requestCpusetBoost;
32 using android::requestPriority;
getBatteryService()33 sp<IBatteryStats> getBatteryService() {
34 sp<IBatteryStats> batteryStatService;
35 const sp<IServiceManager> sm(defaultServiceManager());
36 if (sm != nullptr) {
37 const String16 name("batterystats");
38 sp<IBinder> obj = sm->checkService(name);
39 if (!obj) {
40 ALOGW("batterystats service unavailable!");
41 return nullptr;
42 }
43 batteryStatService = checked_interface_cast<IBatteryStats>(obj);
44 if (batteryStatService == nullptr) {
45 ALOGW("batterystats service interface is invalid");
46 return nullptr;
47 }
48 }
49 return batteryStatService;
50 }
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)51 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
52 FuzzedDataProvider data_provider(data, size);
53 sp<IBatteryStats> batteryStatService = getBatteryService();
54 // There is some state here, but it's mostly focused around thread-safety, so
55 // we won't worry about order.
56 int32_t priority = data_provider.ConsumeIntegral<int32_t>();
57 bool is_for_app = data_provider.ConsumeBool();
58 bool async = data_provider.ConsumeBool();
59 requestPriority(getpid(), android::mediautils::getThreadIdWrapper(), priority, is_for_app,
60 async);
61 // TODO: Verify and re-enable in AOSP (R).
62 // bool enable = data_provider.ConsumeBool();
63 // We are just using batterystats to avoid the need
64 // to register a new service.
65 // requestCpusetBoost(enable, IInterface::asBinder(batteryStatService));
66 return 0;
67 }
68
69