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