blob: d672fb0570d3bc343f5fbca63695266e79d759b0 [file] [log] [blame]
Dylan Katzc247e4a2020-06-10 16:21:39 -07001/*
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"
Steven Moreland36960002021-04-01 00:08:45 +000017#include <batterystats/IBatteryStats.h>
Dylan Katzc247e4a2020-06-10 16:21:39 -070018#include <binder/IServiceManager.h>
19#include <utils/String16.h>
20#include <android/log.h>
21#include <mediautils/SchedulingPolicyService.h>
Atneya Nairf5b68512022-05-23 20:02:49 -040022#include <mediautils/TidWrapper.h>
Dylan Katzc247e4a2020-06-10 16:21:39 -070023#include "fuzzer/FuzzedDataProvider.h"
24using android::IBatteryStats;
25using android::IBinder;
26using android::IInterface;
27using android::IServiceManager;
28using android::sp;
29using android::String16;
30using android::defaultServiceManager;
31using android::requestCpusetBoost;
32using android::requestPriority;
33sp<IBatteryStats> getBatteryService() {
34 sp<IBatteryStats> batteryStatService;
35 const sp<IServiceManager> sm(defaultServiceManager());
36 if (sm != nullptr) {
37 const String16 name("batterystats");
Ayushi Khopkar2a4e9592021-08-06 16:01:20 +053038 sp<IBinder> obj = sm->checkService(name);
39 if (!obj) {
Dylan Katzc247e4a2020-06-10 16:21:39 -070040 ALOGW("batterystats service unavailable!");
41 return nullptr;
42 }
Ayushi Khopkar2a4e9592021-08-06 16:01:20 +053043 batteryStatService = checked_interface_cast<IBatteryStats>(obj);
44 if (batteryStatService == nullptr) {
45 ALOGW("batterystats service interface is invalid");
46 return nullptr;
47 }
Dylan Katzc247e4a2020-06-10 16:21:39 -070048 }
49 return batteryStatService;
50}
51extern "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();
Atneya Nairf5b68512022-05-23 20:02:49 -040059 requestPriority(getpid(), android::mediautils::getThreadIdWrapper(), priority, is_for_app,
60 async);
Dylan Katzc247e4a2020-06-10 16:21:39 -070061 // 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