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 <binder/IBatteryStats.h>
18 #include <binder/IServiceManager.h>
19 #include <utils/String16.h>
20 #include <android/log.h>
21 #include <mediautils/SchedulingPolicyService.h>
22 #include "fuzzer/FuzzedDataProvider.h"
23 using android::IBatteryStats;
24 using android::IBinder;
25 using android::IInterface;
26 using android::IServiceManager;
27 using android::sp;
28 using android::String16;
29 using android::defaultServiceManager;
30 using android::requestCpusetBoost;
31 using android::requestPriority;
getBatteryService()32 sp<IBatteryStats> getBatteryService() {
33     sp<IBatteryStats> batteryStatService;
34     const sp<IServiceManager> sm(defaultServiceManager());
35     if (sm != nullptr) {
36         const String16 name("batterystats");
37         batteryStatService = checked_interface_cast<IBatteryStats>(sm->checkService(name));
38         if (batteryStatService == nullptr) {
39             ALOGW("batterystats service unavailable!");
40             return nullptr;
41         }
42     }
43     return batteryStatService;
44 }
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)45 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
46     FuzzedDataProvider data_provider(data, size);
47     sp<IBatteryStats> batteryStatService = getBatteryService();
48     // There is some state here, but it's mostly focused around thread-safety, so
49     // we won't worry about order.
50     int32_t priority = data_provider.ConsumeIntegral<int32_t>();
51     bool is_for_app = data_provider.ConsumeBool();
52     bool async = data_provider.ConsumeBool();
53     requestPriority(getpid(), gettid(), priority, is_for_app, async);
54     // TODO: Verify and re-enable in AOSP (R).
55     // bool enable = data_provider.ConsumeBool();
56     // We are just using batterystats to avoid the need
57     // to register a new service.
58     // requestCpusetBoost(enable, IInterface::asBinder(batteryStatService));
59     return 0;
60 }
61 
62