blob: b2baa776b6b595ce6287252a07279cf2d80e45d6 [file] [log] [blame]
Tri Vo77e0ca02016-12-05 10:08:59 -08001/*
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#include "ProtoFuzzerMutator.h"
Tri Vo77e0ca02016-12-05 10:08:59 -080018#include <iostream>
19
Tri Vo77e0ca02016-12-05 10:08:59 -080020using std::cerr;
21using std::cout;
Tri Vof657d672017-07-26 16:13:02 -070022using std::endl;
Tri Vo77e0ca02016-12-05 10:08:59 -080023using std::make_unique;
Tri Vo70c1ab62017-03-15 09:19:10 -070024using std::unordered_map;
25using namespace std::placeholders;
Tri Vo77e0ca02016-12-05 10:08:59 -080026
27namespace android {
28namespace vts {
Tri Vo70c1ab62017-03-15 09:19:10 -070029namespace fuzzer {
Tri Vo77e0ca02016-12-05 10:08:59 -080030
31ProtoFuzzerMutator::ProtoFuzzerMutator(
Tri Vo70c1ab62017-03-15 09:19:10 -070032 Random &rand, unordered_map<string, TypeSpec> predefined_types,
Tri Vof4037d42017-03-31 17:28:25 -070033 ProtoFuzzerMutatorConfig mutator_config)
Tri Vo70c1ab62017-03-15 09:19:10 -070034 : rand_(rand),
35 predefined_types_(predefined_types),
Tri Vof4037d42017-03-31 17:28:25 -070036 mutator_config_(mutator_config) {
Tri Vo70c1ab62017-03-15 09:19:10 -070037 // Default function used for mutation/random generation. Used for types for
38 // which the notion of mutation/random generation is not defined, e.g.
39 // TYPE_HANDLE, TYPE_HIDL_CALLBACK.
40 VarTransformFn default_transform =
41 [](const VariableSpecificationMessage &var_spec) {
42 return VariableSpecificationMessage{var_spec};
43 };
44
45 // Initialize random_gen_fns_ and mutate_fns_ tables.
46 random_gen_fns_[TYPE_ARRAY] =
47 std::bind(&ProtoFuzzerMutator::ArrayRandomGen, this, _1);
48 mutate_fns_[TYPE_ARRAY] =
49 std::bind(&ProtoFuzzerMutator::ArrayMutate, this, _1);
50
51 random_gen_fns_[TYPE_ENUM] =
52 std::bind(&ProtoFuzzerMutator::EnumRandomGen, this, _1);
53 mutate_fns_[TYPE_ENUM] = std::bind(&ProtoFuzzerMutator::EnumMutate, this, _1);
54
55 random_gen_fns_[TYPE_HANDLE] = default_transform;
56 mutate_fns_[TYPE_HANDLE] = default_transform;
57
58 random_gen_fns_[TYPE_HIDL_CALLBACK] = default_transform;
59 mutate_fns_[TYPE_HIDL_CALLBACK] = default_transform;
60
Tri Vo165b1ed2017-04-03 14:44:45 -070061 random_gen_fns_[TYPE_HIDL_INTERFACE] = default_transform;
62 mutate_fns_[TYPE_HIDL_INTERFACE] = default_transform;
63
Tri Vo80d2a362017-07-20 16:13:40 -070064 random_gen_fns_[TYPE_HIDL_MEMORY] = default_transform;
65 mutate_fns_[TYPE_HIDL_MEMORY] = default_transform;
66
Tri Vo69d71b32017-04-12 18:24:50 -070067 // Interpret masks as enums.
68 random_gen_fns_[TYPE_MASK] =
69 std::bind(&ProtoFuzzerMutator::EnumRandomGen, this, _1);
70 mutate_fns_[TYPE_MASK] = std::bind(&ProtoFuzzerMutator::EnumMutate, this, _1);
71
Tri Vo78f89c52017-05-24 15:10:26 -070072 random_gen_fns_[TYPE_POINTER] = default_transform;
73 mutate_fns_[TYPE_POINTER] = default_transform;
74
Tri Vo70c1ab62017-03-15 09:19:10 -070075 random_gen_fns_[TYPE_SCALAR] =
76 std::bind(&ProtoFuzzerMutator::ScalarRandomGen, this, _1);
77 mutate_fns_[TYPE_SCALAR] =
78 std::bind(&ProtoFuzzerMutator::ScalarMutate, this, _1);
79
Tri Vo5ecdf5b2017-04-14 08:30:48 -070080 random_gen_fns_[TYPE_STRING] =
81 std::bind(&ProtoFuzzerMutator::StringRandomGen, this, _1);
82 mutate_fns_[TYPE_STRING] =
83 std::bind(&ProtoFuzzerMutator::StringMutate, this, _1);
84
Tri Vo70c1ab62017-03-15 09:19:10 -070085 random_gen_fns_[TYPE_STRUCT] =
86 std::bind(&ProtoFuzzerMutator::StructRandomGen, this, _1);
87 mutate_fns_[TYPE_STRUCT] =
88 std::bind(&ProtoFuzzerMutator::StructMutate, this, _1);
89
90 random_gen_fns_[TYPE_UNION] =
91 std::bind(&ProtoFuzzerMutator::UnionRandomGen, this, _1);
92 mutate_fns_[TYPE_UNION] =
93 std::bind(&ProtoFuzzerMutator::UnionMutate, this, _1);
94
95 random_gen_fns_[TYPE_VECTOR] =
96 std::bind(&ProtoFuzzerMutator::VectorRandomGen, this, _1);
97 mutate_fns_[TYPE_VECTOR] =
98 std::bind(&ProtoFuzzerMutator::VectorMutate, this, _1);
Tri Vo77e0ca02016-12-05 10:08:59 -080099}
100
Tri Vo9f5fd852017-05-20 12:32:26 -0700101// TODO(trong): add a mutator config option which controls how an interface is
102// selected.
103const CompSpec *ProtoFuzzerMutator::RandomSelectIface(const IfaceDescTbl &tbl) {
104 size_t rand_idx = rand_(tbl.size());
105 auto it = tbl.begin();
106 std::advance(it, rand_idx);
107 return it->second.comp_spec_;
108}
109
110ExecSpec ProtoFuzzerMutator::RandomGen(const IfaceDescTbl &tbl,
Tri Vo70c1ab62017-03-15 09:19:10 -0700111 size_t num_calls) {
112 ExecSpec result{};
Tri Vo77e0ca02016-12-05 10:08:59 -0800113 for (size_t i = 0; i < num_calls; ++i) {
Tri Vo9f5fd852017-05-20 12:32:26 -0700114 const CompSpec *comp_spec = RandomSelectIface(tbl);
115 string iface_name = comp_spec->component_name();
116 const IfaceSpec &iface_spec = comp_spec->interface();
117
118 // Generate a random interface function call.
119 FuncCall rand_call{};
120 rand_call.set_hidl_interface_name(iface_name);
Tri Vo77e0ca02016-12-05 10:08:59 -0800121 size_t num_apis = iface_spec.api_size();
122 size_t rand_api_idx = rand_(num_apis);
Tri Vo70c1ab62017-03-15 09:19:10 -0700123 FuncSpec rand_api = RandomGen(iface_spec.api(rand_api_idx));
Tri Vo9f5fd852017-05-20 12:32:26 -0700124 *rand_call.mutable_api() = rand_api;
125 *result.add_function_call() = rand_call;
Tri Vo77e0ca02016-12-05 10:08:59 -0800126 }
Tri Vo77e0ca02016-12-05 10:08:59 -0800127 return result;
128}
129
Tri Vo9f5fd852017-05-20 12:32:26 -0700130void ProtoFuzzerMutator::Mutate(const IfaceDescTbl &tbl, ExecSpec *exec_spec) {
Tri Vof4037d42017-03-31 17:28:25 -0700131 // Mutate a randomly chosen function call with probability
132 // odds_for/(odds_for + odds_against).
133 uint64_t odds_for = mutator_config_.func_mutated_.first;
134 uint64_t odds_against = mutator_config_.func_mutated_.second;
135 uint64_t rand_num = rand_(odds_for + odds_against);
Tri Vo77e0ca02016-12-05 10:08:59 -0800136
Tri Vof4037d42017-03-31 17:28:25 -0700137 if (rand_num < odds_for) {
Tri Voafcb0242017-01-17 16:11:01 -0800138 // Mutate a random function in execution.
Tri Vo9f5fd852017-05-20 12:32:26 -0700139 size_t idx = rand_(exec_spec->function_call_size());
140 const FuncSpec &rand_api = exec_spec->function_call(idx).api();
141 *exec_spec->mutable_function_call(idx)->mutable_api() = Mutate(rand_api);
Tri Vo77e0ca02016-12-05 10:08:59 -0800142 } else {
Tri Voafcb0242017-01-17 16:11:01 -0800143 // Generate a random function call in place of randomly chosen function in
144 // execution.
Tri Vo9f5fd852017-05-20 12:32:26 -0700145 const CompSpec *comp_spec = RandomSelectIface(tbl);
146 string iface_name = comp_spec->component_name();
147 const IfaceSpec &iface_spec = comp_spec->interface();
148
149 size_t func_idx = rand_(exec_spec->function_call_size());
Tri Voafcb0242017-01-17 16:11:01 -0800150 size_t blueprint_idx = rand_(iface_spec.api_size());
Tri Vo9f5fd852017-05-20 12:32:26 -0700151 FuncCall *func_call = exec_spec->mutable_function_call(func_idx);
152 func_call->set_hidl_interface_name(iface_name);
153 *func_call->mutable_api() = RandomGen(iface_spec.api(blueprint_idx));
Tri Vo77e0ca02016-12-05 10:08:59 -0800154 }
155}
156
Tri Vo70c1ab62017-03-15 09:19:10 -0700157FuncSpec ProtoFuzzerMutator::RandomGen(const FuncSpec &func_spec) {
158 FuncSpec result{func_spec};
Tri Vo77e0ca02016-12-05 10:08:59 -0800159 // We'll repopulate arg field.
160 result.clear_arg();
Tri Vof4037d42017-03-31 17:28:25 -0700161 result.clear_return_type_hidl();
Tri Vo77e0ca02016-12-05 10:08:59 -0800162 for (const auto &var_spec : func_spec.arg()) {
Tri Vo70c1ab62017-03-15 09:19:10 -0700163 VarInstance rand_var_spec = RandomGen(var_spec);
Tri Vo77e0ca02016-12-05 10:08:59 -0800164 auto *new_var = result.add_arg();
165 new_var->Swap(&rand_var_spec);
166 }
Tri Vo77e0ca02016-12-05 10:08:59 -0800167 return result;
168}
169
Tri Vo70c1ab62017-03-15 09:19:10 -0700170FuncSpec ProtoFuzzerMutator::Mutate(const FuncSpec &func_spec) {
171 FuncSpec result{func_spec};
Tri Vo77e0ca02016-12-05 10:08:59 -0800172 size_t num_args = result.arg_size();
173 if (num_args > 0) {
174 size_t rand_arg_idx = rand_(num_args);
Tri Vo70c1ab62017-03-15 09:19:10 -0700175 VarInstance rand_arg = Mutate(result.arg(rand_arg_idx));
Tri Vo77e0ca02016-12-05 10:08:59 -0800176 result.mutable_arg(rand_arg_idx)->Swap(&rand_arg);
177 }
178 return result;
179}
180
Tri Vo70c1ab62017-03-15 09:19:10 -0700181static VariableSpecificationMessage Transform(
182 const VariableSpecificationMessage &var_spec,
183 unordered_map<VariableType, VarTransformFn> &transform_fns) {
184 auto type = var_spec.type();
185 auto transform_fn = transform_fns.find(type);
186 if (transform_fn == transform_fns.end()) {
187 cerr << "Transformation function not found for type: " << type << endl;
Tri Vo4fb63ba2017-07-20 15:36:48 -0700188 std::abort();
Tri Voafcb0242017-01-17 16:11:01 -0800189 }
Tri Vo70c1ab62017-03-15 09:19:10 -0700190 return transform_fn->second(var_spec);
Tri Voafcb0242017-01-17 16:11:01 -0800191}
192
Tri Vo70c1ab62017-03-15 09:19:10 -0700193VarInstance ProtoFuzzerMutator::RandomGen(const VarSpec &var_spec) {
194 return Transform(var_spec, random_gen_fns_);
195}
196
197VarInstance ProtoFuzzerMutator::Mutate(const VarInstance &var_instance) {
198 return Transform(var_instance, mutate_fns_);
199}
200
201const TypeSpec &ProtoFuzzerMutator::FindPredefinedType(string name) {
202 auto type_spec = predefined_types_.find(name);
203 if (type_spec == predefined_types_.end()) {
204 cerr << "Predefined type not found: " << name << endl;
Tri Vo4fb63ba2017-07-20 15:36:48 -0700205 std::abort();
Tri Vo70c1ab62017-03-15 09:19:10 -0700206 }
207 return type_spec->second;
208}
209
210} // namespace fuzzer
Tri Vo77e0ca02016-12-05 10:08:59 -0800211} // namespace vts
212} // namespace android