blob: d58d9b6d846a6aad01958e171ff8216e89c6bdda [file] [log] [blame]
Corbin Souffrant84f5c0f2020-06-26 00:42:43 -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
17#pragma once
18
19#include <fuzzer/FuzzedDataProvider.h>
20#include <vector>
21
22// Calls a function from the ops_vector
23template <class F, class T, class... Types>
24void callArbitraryFunction(F* fdp, T const& ops_vector, Types... args) {
25 // Choose which function we'll be calling
26 uint8_t function_id = fdp->template ConsumeIntegralInRange<uint8_t>(0, ops_vector.size() - 1);
27
28 // Call the function we've chosen
29 ops_vector[function_id](fdp, args...);
30}
31
32template <class T>
33T getArbitraryVectorElement(FuzzedDataProvider* fdp, std::vector<T> const& vect, bool allow_null) {
34 // If we're allowing null, give it a 50:50 shot at returning a nullptr
35 if (vect.empty() || (allow_null && fdp->ConsumeBool())) {
36 return nullptr;
37 }
38
39 // Otherwise, return an element from our vector
40 return vect.at(fdp->ConsumeIntegralInRange<size_t>(0, vect.size() - 1));
41}