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 <unistd.h> 21 #include <iostream> 22 #include <random> 23 #include <string> 24 #include <unordered_map> 25 #include <vector> 26 27 #include <hidl-util/FQName.h> 28 29 #include "driver_base/DriverBase.h" 30 #include "test/vts/proto/ExecutionSpecificationMessage.pb.h" 31 32 namespace android { 33 namespace vts { 34 namespace fuzzer { 35 36 // Use shorter names for convenience. 37 using CompSpec = ComponentSpecificationMessage; 38 using ExecSpec = ExecutionSpecificationMessage; 39 using FuncCall = FunctionCallMessage; 40 using FuncSpec = FunctionSpecificationMessage; 41 using IfaceSpec = InterfaceSpecificationMessage; 42 43 // VariableSpecificationMessage can be used to describe 3 things: a type 44 // declaration, a variable declaration, or a runtime variable instance. These 45 // use cases correspond to TypeSpec, VarSpec, and VarInstance respectively. 46 using TypeSpec = VariableSpecificationMessage; 47 using VarInstance = TypeSpec; 48 using VarSpec = TypeSpec; 49 50 using EnumData = EnumDataValueMessage; 51 using ScalarData = ScalarDataValueMessage; 52 53 // 64-bit random number generator. 54 class Random { 55 public: Random(uint64_t seed)56 explicit Random(uint64_t seed) : rand_(seed) {} ~Random()57 virtual ~Random() {} 58 59 // Generates a 64-bit random number. Rand()60 virtual uint64_t Rand() { return rand_(); } 61 // Generates a random number in range [0, n). operator()62 virtual uint64_t operator()(uint64_t n) { return n ? Rand() % n : 0; } 63 64 private: 65 // Used to generate a 64-bit Mersenne Twister pseudo-random number. 66 std::mt19937_64 rand_; 67 }; 68 69 // Additional non-libfuzzer parameters passed to the fuzzer. 70 class ProtoFuzzerParams { 71 public: 72 // Number of function calls per execution (fixed throughout fuzzer run). 73 size_t exec_size_; 74 // VTS specs supplied to the fuzzer. 75 std::vector<CompSpec> comp_specs_; 76 // Fully-qualified name of the target, e.g. 77 // "android.hardware.light@2.0::ILight" 78 android::FQName target_fq_name_; 79 // Controls whether HAL is opened in passthrough or binder mode. 80 // Binder mode is default. Used for testing. 81 bool binder_mode_ = true; 82 // Seed used to initialize the random number generator. 83 uint64_t seed_ = static_cast<uint64_t>(time(0)); 84 // Returns a string summarizing content of this object. 85 string DebugString() const; 86 }; 87 88 // Loads/parses component specs from a list of directories. 89 vector<CompSpec> ExtractCompSpecs(const vector<string> &dirs); 90 91 // Parses command-line flags to create a ProtoFuzzerParams instance. 92 ProtoFuzzerParams ExtractProtoFuzzerParams(int, char **); 93 94 // Returns CompSpec corresponding to given interface name. 95 const CompSpec &FindCompSpec(const std::vector<CompSpec> &, 96 const std::string &); 97 98 // Creates a key, value look-up table with keys being names of predefined types, 99 // and values being their definitions. 100 std::unordered_map<std::string, TypeSpec> ExtractPredefinedTypes( 101 const std::vector<CompSpec> &); 102 103 // Serializes ExecSpec into byte form and writes it to buffer. Returns number of 104 // written bytes. 105 size_t ToArray(uint8_t *, size_t, ExecSpec *); 106 107 // Deserializes given buffer to an ExecSpec. Returns true on success. 108 bool FromArray(const uint8_t *, size_t, ExecSpec *); 109 110 } // namespace fuzzer 111 } // namespace vts 112 } // namespace android 113 114 #endif // __VTS_PROTO_FUZZER_UTILS_H__ 115