/* * Copyright (C) 2023 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ #ifndef _LIBHIDLBASE_FUZZER_HELPER_H #define _LIBHIDLBASE_FUZZER_HELPER_H #include #include using namespace android; using namespace hardware; using namespace details; constexpr uint32_t kMaxBytes = 256; constexpr uint32_t kMin = 0; hidl_string createHidlString(FuzzedDataProvider& fdp) { auto invokeHidlString = fdp.PickValueInArray>({ [&]() { return hidl_string(fdp.ConsumeRandomLengthString(kMaxBytes)); }, [&]() { return hidl_string((fdp.ConsumeRandomLengthString(kMaxBytes)).c_str()); }, [&]() { std::string testString = fdp.ConsumeRandomLengthString(kMaxBytes); return hidl_string(testString.c_str(), fdp.ConsumeIntegralInRange(kMin, testString.length())); }, [&]() { return fdp.ConsumeRandomLengthString(kMaxBytes); }, }); return invokeHidlString(); } hidl_memory createHidlMemory(FuzzedDataProvider& fdp) { if (fdp.ConsumeBool()) { return hidl_memory(); } return hidl_memory(createHidlString(fdp), hidl_handle(), fdp.ConsumeIntegral() /* size */); } Status createStatus(FuzzedDataProvider& fdp) { auto invokeStatus = fdp.PickValueInArray>({ [&]() { return Status::fromExceptionCode(fdp.ConsumeIntegral()); }, [&]() { return Status::fromExceptionCode( fdp.ConsumeIntegral(), (fdp.ConsumeRandomLengthString(kMaxBytes)).c_str()); }, [&]() { return Status::fromStatusT(fdp.ConsumeIntegral()); }, [&]() { return Status(); }, }); return invokeStatus(); } #endif // _LIBHIDLBASE_FUZZER_HELPER_H