1 /* 2 * Copyright 2016 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 #ifndef __VTS_PROTO_FUZZER_UTILS_H__ 18 #define __VTS_PROTO_FUZZER_UTILS_H__ 19 20 #include <iostream> 21 #include <random> 22 #include <string> 23 #include <unordered_map> 24 #include <vector> 25 26 #include "fuzz_tester/FuzzerBase.h" 27 #include "test/vts-testcase/fuzz/iface_fuzzer/proto/ExecutionSpecificationMessage.pb.h" 28 #include "test/vts/proto/ComponentSpecificationMessage.pb.h" 29 30 namespace android { 31 namespace vts { 32 namespace fuzzer { 33 34 // Use shorter names for convenience. 35 using CompSpec = ComponentSpecificationMessage; 36 using ExecSpec = ExecutionSpecificationMessage; 37 using FuncSpec = FunctionSpecificationMessage; 38 using IfaceSpec = InterfaceSpecificationMessage; 39 40 // VariableSpecificationMessage can be used to describe 3 things: a type 41 // declaration, a variable declaration, or a runtime variable instance. These 42 // use cases correspond to TypeSpec, VarSpec, and VarInstance respectively. 43 using TypeSpec = VariableSpecificationMessage; 44 using VarInstance = TypeSpec; 45 using VarSpec = TypeSpec; 46 47 using EnumData = EnumDataValueMessage; 48 using ScalarData = ScalarDataValueMessage; 49 50 // 64-bit random number generator. 51 class Random { 52 public: Random(uint64_t seed)53 Random(uint64_t seed) : rand_(seed) {} ~Random()54 virtual ~Random() {} 55 56 // Generates a 64-bit random number. Rand()57 virtual uint64_t Rand() { return rand_(); } 58 // Generates a random number in range [0, n). operator()59 virtual uint64_t operator()(uint64_t n) { return n ? Rand() % n : 0; } 60 61 private: 62 // Used to generate a 64-bit Mersenne Twister pseudo-random number. 63 std::mt19937_64 rand_; 64 }; 65 66 // Additional non-libfuzzer parameters passed to the fuzzer. 67 struct ProtoFuzzerParams { 68 // Number of function calls per execution (fixed throughout fuzzer run). 69 size_t exec_size_; 70 // VTS specs supplied to the fuzzer. 71 std::vector<CompSpec> comp_specs_; 72 // Service name of target interface, e.g. "INfc". 73 std::string service_name_ = "default"; 74 // Name of target interface, e.g. "default". 75 std::string target_iface_; 76 // Controls whether HAL is opened in passthrough or binder mode. 77 // Passthrough mode is default. Used for testsing. 78 bool get_stub_ = true; 79 }; 80 81 // Parses command-line flags to create a ProtoFuzzerParams instance. 82 ProtoFuzzerParams ExtractProtoFuzzerParams(int, char **); 83 84 // Returns CompSpec corresponding to targeted interface. 85 CompSpec FindTargetCompSpec(const std::vector<CompSpec> &, const std::string &); 86 87 // Loads VTS HAL driver library. 88 FuzzerBase *InitHalDriver(const CompSpec &, std::string, bool); 89 90 // Creates a key, value look-up table with keys being names of predefined types, 91 // and values being their definitions. 92 std::unordered_map<std::string, TypeSpec> ExtractPredefinedTypes( 93 const std::vector<CompSpec> &); 94 95 // Call every API from call sequence specified by the 96 // ExecutionSpecificationMessage. 97 void Execute(FuzzerBase *, const ExecutionSpecificationMessage &); 98 99 } // namespace fuzzer 100 } // namespace vts 101 } // namespace android 102 103 #endif // __VTS_PROTO_FUZZER_UTILS_H__ 104