1 /* 2 * Copyright (C) 2022 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 17 #include "CarPowerPolicyServer.h" 18 19 #include <android-base/result.h> 20 #include <binder/IPCThreadState.h> 21 #include <binder/ProcessState.h> 22 #include <fuzzbinder/libbinder_ndk_driver.h> 23 #include <fuzzer/FuzzedDataProvider.h> 24 #include <log/log.h> 25 #include <utils/Looper.h> 26 #include <utils/StrongPointer.h> 27 28 using ::android::fuzzService; 29 using ::android::Looper; 30 using ::android::sp; 31 using ::android::frameworks::automotive::powerpolicy::CarPowerPolicyServer; 32 using ::ndk::SharedRefBase; 33 34 using ::android::IPCThreadState; 35 using ::android::Looper; 36 using ::android::ProcessState; 37 using ::android::sp; 38 using ::android::frameworks::automotive::powerpolicy::CarPowerPolicyServer; 39 LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)40extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { 41 // Set up the binder 42 sp<ProcessState> ps(ProcessState::self()); 43 ps->setThreadPoolMaxThreadCount(2); 44 ps->startThreadPool(); 45 ps->giveThreadPoolName(); 46 IPCThreadState::self()->disableBackgroundScheduling(true); 47 48 sp<Looper> looper(Looper::prepare(/*opts=*/0)); 49 auto result = CarPowerPolicyServer::startService(looper); 50 if (!result.ok()) { 51 printf("Failed to start service: %s", result.error().message().c_str()); 52 CarPowerPolicyServer::terminateService(); 53 exit(result.error().code()); 54 } 55 auto carpowerServer = *result; 56 fuzzService(carpowerServer->asBinder().get(), FuzzedDataProvider(data, size)); 57 CarPowerPolicyServer::terminateService(); 58 59 return 0; 60 } 61