Vitaly Buka | 00b6107 | 2016-10-19 23:22:51 | [diff] [blame] | 1 | // Copyright 2016 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Vitaly Buka | 9dd2f8e | 2017-01-13 08:48:31 | [diff] [blame] | 15 | #include "src/protobuf_mutator.h" |
Vitaly Buka | 00b6107 | 2016-10-19 23:22:51 | [diff] [blame] | 16 | |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 17 | #include <algorithm> |
Vitaly Buka | 0e17fd7 | 2016-11-18 18:02:46 | [diff] [blame] | 18 | #include <iostream> |
| 19 | #include <map> |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 20 | #include <random> |
Vitaly Buka | 0e17fd7 | 2016-11-18 18:02:46 | [diff] [blame] | 21 | #include <string> |
Vitaly Buka | 00b6107 | 2016-10-19 23:22:51 | [diff] [blame] | 22 | |
Vitaly Buka | 9dd2f8e | 2017-01-13 08:48:31 | [diff] [blame] | 23 | #include "src/field_instance.h" |
| 24 | #include "src/weighted_reservoir_sampler.h" |
Vitaly Buka | 00b6107 | 2016-10-19 23:22:51 | [diff] [blame] | 25 | |
Vitaly Buka | f86815c | 2017-02-27 22:19:19 | [diff] [blame] | 26 | namespace protobuf_mutator { |
| 27 | |
Vitaly Buka | 6c6dbbe | 2017-02-22 21:58:24 | [diff] [blame] | 28 | using protobuf::Descriptor; |
| 29 | using protobuf::EnumDescriptor; |
| 30 | using protobuf::EnumValueDescriptor; |
| 31 | using protobuf::FieldDescriptor; |
| 32 | using protobuf::Message; |
| 33 | using protobuf::OneofDescriptor; |
| 34 | using protobuf::Reflection; |
Vitaly Buka | 0e17fd7 | 2016-11-18 18:02:46 | [diff] [blame] | 35 | |
| 36 | namespace { |
| 37 | |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 38 | const size_t kMaxInitializeDepth = 32; |
| 39 | const size_t kDeletionThreshold = 128; |
| 40 | const uint64_t kMutateWeight = 1000000; |
Vitaly Buka | 0e17fd7 | 2016-11-18 18:02:46 | [diff] [blame] | 41 | |
| 42 | enum class Mutation { |
Vitaly Buka | 30de309 | 2016-11-18 19:39:07 | [diff] [blame] | 43 | None, |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 44 | Add, // Adds new field with default value. |
| 45 | Mutate, // Mutates field contents. |
| 46 | Delete, // Deletes field. |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 47 | Copy, // Copy values copied from another field. |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 48 | |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 49 | // TODO(vitalybuka): |
| 50 | // Clone, // Adds new field with value copied from another field. |
Vitaly Buka | 0e17fd7 | 2016-11-18 18:02:46 | [diff] [blame] | 51 | }; |
| 52 | |
Vitaly Buka | b592ff0 | 2017-03-03 22:35:52 | [diff] [blame^] | 53 | // Return random integer from [0, count) |
| 54 | size_t GetRandomIndex(ProtobufMutator::RandomEngine* random, size_t count) { |
| 55 | assert(count > 0); |
| 56 | if (count == 1) return 0; |
| 57 | return std::uniform_int_distribution<size_t>(0, count - 1)(*random); |
| 58 | } |
| 59 | |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 60 | // Flips random bit in the buffer. |
| 61 | void FlipBit(size_t size, uint8_t* bytes, |
| 62 | ProtobufMutator::RandomEngine* random) { |
Vitaly Buka | b592ff0 | 2017-03-03 22:35:52 | [diff] [blame^] | 63 | size_t bit = GetRandomIndex(random, size * 8); |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 64 | bytes[bit / 8] ^= (1u << (bit % 8)); |
| 65 | } |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 66 | |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 67 | // Flips random bit in the value. |
| 68 | template <class T> |
| 69 | T FlipBit(T value, ProtobufMutator::RandomEngine* random) { |
| 70 | FlipBit(sizeof(value), reinterpret_cast<uint8_t*>(&value), random); |
| 71 | return value; |
| 72 | } |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 73 | |
Vitaly Buka | beb9080 | 2017-02-28 23:28:10 | [diff] [blame] | 74 | // Return true with probability about 1-of-n. |
| 75 | bool GetRandomBool(ProtobufMutator::RandomEngine* random, size_t n = 2) { |
| 76 | return GetRandomIndex(random, n) == 0; |
| 77 | } |
| 78 | |
Vitaly Buka | 91ad7b0 | 2016-12-12 23:41:41 | [diff] [blame] | 79 | struct CreateDefaultFieldTransformation { |
| 80 | template <class T> |
| 81 | void Apply(const FieldInstance& field) const { |
| 82 | T value; |
| 83 | field.GetDefault(&value); |
| 84 | field.Create(value); |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 85 | } |
Vitaly Buka | 91ad7b0 | 2016-12-12 23:41:41 | [diff] [blame] | 86 | }; |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 87 | |
Vitaly Buka | 91ad7b0 | 2016-12-12 23:41:41 | [diff] [blame] | 88 | struct DeleteFieldTransformation { |
| 89 | template <class T> |
| 90 | void Apply(const FieldInstance& field) const { |
| 91 | field.Delete(); |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 92 | } |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 93 | }; |
| 94 | |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 95 | struct CopyFieldTransformation { |
Vitaly Buka | 8871286 | 2017-02-27 06:21:30 | [diff] [blame] | 96 | explicit CopyFieldTransformation(const ConstFieldInstance& field) |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 97 | : source(field) {} |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 98 | |
| 99 | template <class T> |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 100 | void Apply(const FieldInstance& field) const { |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 101 | T value; |
| 102 | source.Load(&value); |
| 103 | field.Store(value); |
| 104 | } |
| 105 | |
Vitaly Buka | 8871286 | 2017-02-27 06:21:30 | [diff] [blame] | 106 | ConstFieldInstance source; |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 107 | }; |
| 108 | |
Vitaly Buka | adfc27c | 2017-02-27 06:36:36 | [diff] [blame] | 109 | struct AppendFieldTransformation { |
| 110 | explicit AppendFieldTransformation(const ConstFieldInstance& field) |
| 111 | : source(field) {} |
| 112 | |
| 113 | template <class T> |
| 114 | void Apply(const FieldInstance& field) const { |
| 115 | T value; |
| 116 | source.Load(&value); |
| 117 | field.Create(value); |
| 118 | } |
| 119 | |
| 120 | ConstFieldInstance source; |
| 121 | }; |
| 122 | |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 123 | // Selects random field and mutation from the given proto message. |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 124 | class MutationSampler { |
Vitaly Buka | c9d2248 | 2016-11-21 21:29:17 | [diff] [blame] | 125 | public: |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 126 | MutationSampler(bool keep_initialized, size_t size_increase_hint, |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 127 | ProtobufMutator::RandomEngine* random, Message* message) |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 128 | : keep_initialized_(keep_initialized), random_(random), sampler_(random) { |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 129 | if (size_increase_hint < kDeletionThreshold) { |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 130 | // Avoid adding new field and prefer deleting fields if we getting close |
| 131 | // to the limit. |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 132 | float adjustment = 0.5 * size_increase_hint / kDeletionThreshold; |
| 133 | add_weight_ *= adjustment; |
| 134 | delete_weight_ *= 1 - adjustment; |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 135 | } |
| 136 | Sample(message); |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 137 | assert(mutation() != Mutation::None); |
Vitaly Buka | c9d2248 | 2016-11-21 21:29:17 | [diff] [blame] | 138 | } |
| 139 | |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 140 | // Returns selected field. |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 141 | const FieldInstance& field() const { return sampler_.selected().field; } |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 142 | |
| 143 | // Returns selected mutation. |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 144 | Mutation mutation() const { return sampler_.selected().mutation; } |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 145 | |
| 146 | private: |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 147 | void Sample(Message* message) { |
| 148 | const Descriptor* descriptor = message->GetDescriptor(); |
| 149 | const Reflection* reflection = message->GetReflection(); |
| 150 | |
| 151 | int field_count = descriptor->field_count(); |
| 152 | for (int i = 0; i < field_count; ++i) { |
| 153 | const FieldDescriptor* field = descriptor->field(i); |
| 154 | if (const OneofDescriptor* oneof = field->containing_oneof()) { |
| 155 | // Handle entire oneof group on the first field. |
| 156 | if (field->index_in_oneof() == 0) { |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 157 | sampler_.Try( |
| 158 | add_weight_, |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 159 | {{message, |
| 160 | oneof->field(GetRandomIndex(random_, oneof->field_count()))}, |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 161 | Mutation::Add}); |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 162 | if (const FieldDescriptor* field = |
| 163 | reflection->GetOneofFieldDescriptor(*message, oneof)) { |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 164 | if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 165 | sampler_.Try(kMutateWeight, {{message, field}, Mutation::Mutate}); |
| 166 | sampler_.Try(delete_weight_, {{message, field}, Mutation::Delete}); |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 167 | sampler_.Try(GetCopyWeight(field), |
| 168 | {{message, field}, Mutation::Copy}); |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | } else { |
| 172 | if (field->is_repeated()) { |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 173 | int field_size = reflection->FieldSize(*message, field); |
| 174 | sampler_.Try(add_weight_, {{message, field, |
| 175 | GetRandomIndex(random_, field_size + 1)}, |
| 176 | Mutation::Add}); |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 177 | |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 178 | if (field_size) { |
| 179 | size_t random_index = GetRandomIndex(random_, field_size); |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 180 | if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 181 | sampler_.Try(kMutateWeight, |
| 182 | {{message, field, random_index}, Mutation::Mutate}); |
| 183 | sampler_.Try(delete_weight_, |
| 184 | {{message, field, random_index}, Mutation::Delete}); |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 185 | sampler_.Try(GetCopyWeight(field), |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 186 | {{message, field, random_index}, Mutation::Copy}); |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 187 | } |
| 188 | } else { |
| 189 | if (reflection->HasField(*message, field)) { |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 190 | if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 191 | sampler_.Try(kMutateWeight, {{message, field}, Mutation::Mutate}); |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 192 | if ((!field->is_required() || !keep_initialized_)) |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 193 | sampler_.Try(delete_weight_, |
| 194 | {{message, field}, Mutation::Delete}); |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 195 | sampler_.Try(GetCopyWeight(field), |
| 196 | {{message, field}, Mutation::Copy}); |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 197 | } else { |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 198 | sampler_.Try(add_weight_, {{message, field}, Mutation::Add}); |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
| 204 | if (field->is_repeated()) { |
| 205 | const int field_size = reflection->FieldSize(*message, field); |
| 206 | for (int j = 0; j < field_size; ++j) |
| 207 | Sample(reflection->MutableRepeatedMessage(message, field, j)); |
| 208 | } else if (reflection->HasField(*message, field)) { |
| 209 | Sample(reflection->MutableMessage(message, field)); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 215 | uint64_t GetCopyWeight(const FieldDescriptor* field) const { |
| 216 | // Coping sub-messages can increase size significantly. |
| 217 | return field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE |
| 218 | ? add_weight_ |
| 219 | : kMutateWeight; |
| 220 | } |
| 221 | |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 222 | bool keep_initialized_ = false; |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 223 | |
| 224 | // Adding and deleting are intrusive and expensive mutations, we'd like to do |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 225 | // them less often than field mutations. |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 226 | uint64_t add_weight_ = kMutateWeight / 10; |
| 227 | uint64_t delete_weight_ = kMutateWeight / 10; |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 228 | |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 229 | ProtobufMutator::RandomEngine* random_; |
| 230 | |
| 231 | struct Result { |
Vitaly Buka | 91ad7b0 | 2016-12-12 23:41:41 | [diff] [blame] | 232 | Result() = default; |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 233 | Result(const FieldInstance& f, Mutation m) : field(f), mutation(m) {} |
Vitaly Buka | 91ad7b0 | 2016-12-12 23:41:41 | [diff] [blame] | 234 | |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 235 | FieldInstance field; |
| 236 | Mutation mutation = Mutation::None; |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 237 | }; |
| 238 | WeightedReservoirSampler<Result, ProtobufMutator::RandomEngine> sampler_; |
Vitaly Buka | c9d2248 | 2016-11-21 21:29:17 | [diff] [blame] | 239 | }; |
| 240 | |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 241 | // Selects random field of compatible type to use for clone mutations. |
| 242 | class DataSourceSampler { |
| 243 | public: |
Vitaly Buka | 8871286 | 2017-02-27 06:21:30 | [diff] [blame] | 244 | DataSourceSampler(const ConstFieldInstance& match, |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 245 | ProtobufMutator::RandomEngine* random, Message* message) |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 246 | : match_(match), random_(random), sampler_(random) { |
| 247 | Sample(message); |
| 248 | } |
| 249 | |
| 250 | // Returns selected field. |
Vitaly Buka | 8871286 | 2017-02-27 06:21:30 | [diff] [blame] | 251 | const ConstFieldInstance& field() const { |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 252 | assert(!IsEmpty()); |
| 253 | return sampler_.selected(); |
| 254 | } |
| 255 | |
| 256 | bool IsEmpty() const { return sampler_.IsEmpty(); } |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 257 | |
| 258 | private: |
| 259 | void Sample(Message* message) { |
| 260 | const Descriptor* descriptor = message->GetDescriptor(); |
| 261 | const Reflection* reflection = message->GetReflection(); |
| 262 | |
| 263 | int field_count = descriptor->field_count(); |
| 264 | for (int i = 0; i < field_count; ++i) { |
| 265 | const FieldDescriptor* field = descriptor->field(i); |
| 266 | if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
| 267 | if (field->is_repeated()) { |
| 268 | const int field_size = reflection->FieldSize(*message, field); |
| 269 | for (int j = 0; j < field_size; ++j) { |
| 270 | Sample(reflection->MutableRepeatedMessage(message, field, j)); |
| 271 | } |
| 272 | } else if (reflection->HasField(*message, field)) { |
| 273 | Sample(reflection->MutableMessage(message, field)); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | if (field->cpp_type() != match_.cpp_type()) continue; |
| 278 | if (match_.cpp_type() == FieldDescriptor::CPPTYPE_ENUM) { |
| 279 | if (field->enum_type() != match_.enum_type()) continue; |
| 280 | } else if (match_.cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
| 281 | if (field->message_type() != match_.message_type()) continue; |
| 282 | } |
| 283 | |
| 284 | // TODO(vitalybuka) : make sure that values are different |
| 285 | if (field->is_repeated()) { |
| 286 | if (int field_size = reflection->FieldSize(*message, field)) { |
| 287 | sampler_.Try(field_size, |
| 288 | {message, field, GetRandomIndex(random_, field_size)}); |
| 289 | } |
| 290 | } else { |
| 291 | if (reflection->HasField(*message, field)) { |
| 292 | sampler_.Try(1, {message, field}); |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
Vitaly Buka | 8871286 | 2017-02-27 06:21:30 | [diff] [blame] | 298 | ConstFieldInstance match_; |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 299 | ProtobufMutator::RandomEngine* random_; |
| 300 | |
Vitaly Buka | 8871286 | 2017-02-27 06:21:30 | [diff] [blame] | 301 | WeightedReservoirSampler<ConstFieldInstance, ProtobufMutator::RandomEngine> |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 302 | sampler_; |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 303 | }; |
| 304 | |
Vitaly Buka | 0e17fd7 | 2016-11-18 18:02:46 | [diff] [blame] | 305 | } // namespace |
| 306 | |
Vitaly Buka | 5d01320 | 2017-02-25 00:50:11 | [diff] [blame] | 307 | class FieldMutator { |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 308 | public: |
Vitaly Buka | 5d01320 | 2017-02-25 00:50:11 | [diff] [blame] | 309 | FieldMutator(size_t size_increase_hint, ProtobufMutator* mutator) |
| 310 | : size_increase_hint_(size_increase_hint), mutator_(mutator) {} |
Vitaly Buka | 91ad7b0 | 2016-12-12 23:41:41 | [diff] [blame] | 311 | |
Vitaly Buka | 91ad7b0 | 2016-12-12 23:41:41 | [diff] [blame] | 312 | void Mutate(int32_t* value) const { *value = mutator_->MutateInt32(*value); } |
| 313 | |
| 314 | void Mutate(int64_t* value) const { *value = mutator_->MutateInt64(*value); } |
| 315 | |
| 316 | void Mutate(uint32_t* value) const { |
| 317 | *value = mutator_->MutateUInt32(*value); |
| 318 | } |
| 319 | |
| 320 | void Mutate(uint64_t* value) const { |
| 321 | *value = mutator_->MutateUInt64(*value); |
| 322 | } |
| 323 | |
| 324 | void Mutate(float* value) const { *value = mutator_->MutateFloat(*value); } |
| 325 | |
| 326 | void Mutate(double* value) const { *value = mutator_->MutateDouble(*value); } |
| 327 | |
| 328 | void Mutate(bool* value) const { *value = mutator_->MutateBool(*value); } |
| 329 | |
| 330 | void Mutate(FieldInstance::Enum* value) const { |
| 331 | value->index = mutator_->MutateEnum(value->index, value->count); |
| 332 | assert(value->index < value->count); |
| 333 | } |
| 334 | |
| 335 | void Mutate(std::string* value) const { |
Vitaly Buka | 5d01320 | 2017-02-25 00:50:11 | [diff] [blame] | 336 | *value = mutator_->MutateString(*value, size_increase_hint_); |
Vitaly Buka | 91ad7b0 | 2016-12-12 23:41:41 | [diff] [blame] | 337 | } |
| 338 | |
Vitaly Buka | 6c6dbbe | 2017-02-22 21:58:24 | [diff] [blame] | 339 | void Mutate(std::unique_ptr<Message>*) const { |
Vitaly Buka | 6c6dbbe | 2017-02-22 21:58:24 | [diff] [blame] | 340 | } |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 341 | |
Vitaly Buka | 5d01320 | 2017-02-25 00:50:11 | [diff] [blame] | 342 | private: |
| 343 | size_t size_increase_hint_; |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 344 | ProtobufMutator* mutator_; |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 345 | }; |
| 346 | |
Vitaly Buka | 5d01320 | 2017-02-25 00:50:11 | [diff] [blame] | 347 | namespace { |
| 348 | |
| 349 | class MutateTransformation { |
| 350 | public: |
| 351 | MutateTransformation(size_t size_increase_hint, ProtobufMutator* mutator) |
| 352 | : mutator_(size_increase_hint, mutator) {} |
| 353 | |
| 354 | template <class T> |
| 355 | void Apply(const FieldInstance& field) const { |
| 356 | T value; |
| 357 | field.Load(&value); |
| 358 | mutator_.Mutate(&value); |
| 359 | field.Store(value); |
| 360 | } |
| 361 | |
| 362 | private: |
| 363 | FieldMutator mutator_; |
| 364 | }; |
| 365 | |
| 366 | class CreateFieldTransformation { |
| 367 | public: |
| 368 | CreateFieldTransformation(size_t size_increase_hint, ProtobufMutator* mutator) |
| 369 | : mutator_(size_increase_hint, mutator) {} |
| 370 | |
| 371 | template <class T> |
| 372 | void Apply(const FieldInstance& field) const { |
| 373 | T value; |
| 374 | field.GetDefault(&value); |
| 375 | mutator_.Mutate(&value); |
| 376 | field.Create(value); |
| 377 | } |
| 378 | |
| 379 | private: |
| 380 | FieldMutator mutator_; |
| 381 | }; |
| 382 | |
| 383 | } // namespace |
| 384 | |
Vitaly Buka | ba12972 | 2016-12-15 01:29:15 | [diff] [blame] | 385 | ProtobufMutator::ProtobufMutator(uint32_t seed) : random_(seed) {} |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 386 | |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 387 | void ProtobufMutator::Mutate(Message* message, size_t size_increase_hint) { |
| 388 | MutationSampler mutation(keep_initialized_, size_increase_hint, &random_, |
| 389 | message); |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 390 | switch (mutation.mutation()) { |
| 391 | case Mutation::None: |
| 392 | break; |
| 393 | case Mutation::Add: |
Vitaly Buka | beb9080 | 2017-02-28 23:28:10 | [diff] [blame] | 394 | if (GetRandomBool(&random_)) { |
Vitaly Buka | 5d01320 | 2017-02-25 00:50:11 | [diff] [blame] | 395 | mutation.field().Apply( |
Vitaly Buka | 35df2e4 | 2017-02-25 09:17:25 | [diff] [blame] | 396 | CreateFieldTransformation(size_increase_hint / 2, this)); |
Vitaly Buka | 5d01320 | 2017-02-25 00:50:11 | [diff] [blame] | 397 | } else { |
| 398 | mutation.field().Apply(CreateDefaultFieldTransformation()); |
| 399 | } |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 400 | break; |
| 401 | case Mutation::Mutate: |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 402 | mutation.field().Apply( |
Vitaly Buka | 35df2e4 | 2017-02-25 09:17:25 | [diff] [blame] | 403 | MutateTransformation(size_increase_hint / 2, this)); |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 404 | break; |
| 405 | case Mutation::Delete: |
Vitaly Buka | 91ad7b0 | 2016-12-12 23:41:41 | [diff] [blame] | 406 | mutation.field().Apply(DeleteFieldTransformation()); |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 407 | break; |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 408 | case Mutation::Copy: { |
| 409 | DataSourceSampler source(mutation.field(), &random_, message); |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 410 | if (source.IsEmpty()) { |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 411 | // Fallback to message deletion. |
| 412 | mutation.field().Apply(DeleteFieldTransformation()); |
| 413 | break; |
| 414 | } |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 415 | mutation.field().Apply(CopyFieldTransformation(source.field())); |
| 416 | break; |
| 417 | } |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 418 | default: |
Vitaly Buka | 6c6dbbe | 2017-02-22 21:58:24 | [diff] [blame] | 419 | assert(false && "unexpected mutation"); |
Vitaly Buka | 0e17fd7 | 2016-11-18 18:02:46 | [diff] [blame] | 420 | } |
| 421 | |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 422 | if (keep_initialized_ && !message->IsInitialized()) { |
| 423 | InitializeMessage(message, kMaxInitializeDepth); |
| 424 | assert(message->IsInitialized()); |
Vitaly Buka | 0e17fd7 | 2016-11-18 18:02:46 | [diff] [blame] | 425 | } |
Vitaly Buka | 00b6107 | 2016-10-19 23:22:51 | [diff] [blame] | 426 | } |
| 427 | |
Vitaly Buka | adfc27c | 2017-02-27 06:36:36 | [diff] [blame] | 428 | void ProtobufMutator::CrossOver(const protobuf::Message& message1, |
| 429 | protobuf::Message* message2) { |
| 430 | CrossOverImpl(message1, message2); |
| 431 | |
| 432 | if (keep_initialized_ && !message2->IsInitialized()) { |
| 433 | InitializeMessage(message2, kMaxInitializeDepth); |
| 434 | assert(message2->IsInitialized()); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | void ProtobufMutator::CrossOverImpl(const protobuf::Message& message1, |
| 439 | protobuf::Message* message2) { |
| 440 | const Descriptor* descriptor = message2->GetDescriptor(); |
| 441 | const Reflection* reflection = message2->GetReflection(); |
| 442 | assert(message1.GetDescriptor() == descriptor); |
| 443 | assert(message1.GetReflection() == reflection); |
| 444 | |
| 445 | for (int i = 0; i < descriptor->field_count(); ++i) { |
| 446 | const FieldDescriptor* field = descriptor->field(i); |
| 447 | |
| 448 | if (field->is_repeated()) { |
| 449 | const int field_size1 = reflection->FieldSize(message1, field); |
| 450 | int field_size2 = reflection->FieldSize(*message2, field); |
| 451 | for (int j = 0; j < field_size1; ++j) { |
| 452 | ConstFieldInstance source(&message1, field, j); |
| 453 | FieldInstance destination(message2, field, field_size2++); |
| 454 | destination.Apply(AppendFieldTransformation(source)); |
| 455 | } |
| 456 | |
| 457 | assert(field_size2 == reflection->FieldSize(*message2, field)); |
| 458 | |
| 459 | // Shuffle |
| 460 | for (int j = 0; j < field_size2; ++j) { |
| 461 | if (int k = GetRandomIndex(&random_, field_size2 - j)) { |
| 462 | reflection->SwapElements(message2, field, j, j + k); |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | int keep = GetRandomIndex(&random_, field_size2 + 1); |
| 467 | |
| 468 | if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
| 469 | int remove = field_size2 - keep; |
| 470 | // Cross some message to keep with messages to remove. |
| 471 | int cross = GetRandomIndex(&random_, std::min(keep, remove) + 1); |
| 472 | for (int j = 0; j < cross; ++j) { |
| 473 | int k = GetRandomIndex(&random_, keep); |
| 474 | int r = keep + GetRandomIndex(&random_, remove); |
| 475 | assert(k != r); |
| 476 | CrossOverImpl(reflection->GetRepeatedMessage(*message2, field, r), |
| 477 | reflection->MutableRepeatedMessage(message2, field, k)); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | for (int j = keep; j < field_size2; ++j) |
| 482 | reflection->RemoveLast(message2, field); |
| 483 | assert(keep == reflection->FieldSize(*message2, field)); |
| 484 | |
| 485 | } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
| 486 | if (!reflection->HasField(message1, field)) { |
Vitaly Buka | beb9080 | 2017-02-28 23:28:10 | [diff] [blame] | 487 | if (GetRandomBool(&random_)) |
Vitaly Buka | adfc27c | 2017-02-27 06:36:36 | [diff] [blame] | 488 | FieldInstance(message2, field).Apply(DeleteFieldTransformation()); |
| 489 | } else if (!reflection->HasField(*message2, field)) { |
Vitaly Buka | beb9080 | 2017-02-28 23:28:10 | [diff] [blame] | 490 | if (GetRandomBool(&random_)) { |
Vitaly Buka | adfc27c | 2017-02-27 06:36:36 | [diff] [blame] | 491 | ConstFieldInstance source(&message1, field); |
| 492 | FieldInstance(message2, field).Apply(CopyFieldTransformation(source)); |
| 493 | } |
| 494 | } else { |
| 495 | CrossOverImpl(reflection->GetMessage(message1, field), |
| 496 | reflection->MutableMessage(message2, field)); |
| 497 | } |
| 498 | } else { |
Vitaly Buka | beb9080 | 2017-02-28 23:28:10 | [diff] [blame] | 499 | if (GetRandomBool(&random_)) { |
Vitaly Buka | adfc27c | 2017-02-27 06:36:36 | [diff] [blame] | 500 | if (reflection->HasField(message1, field)) { |
| 501 | ConstFieldInstance source(&message1, field); |
| 502 | FieldInstance(message2, field).Apply(CopyFieldTransformation(source)); |
| 503 | } else { |
| 504 | FieldInstance(message2, field).Apply(DeleteFieldTransformation()); |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 511 | void ProtobufMutator::InitializeMessage(Message* message, size_t max_depth) { |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 512 | assert(keep_initialized_); |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 513 | // It's pointless but possible to have infinite recursion of required |
| 514 | // messages. |
| 515 | assert(max_depth); |
Vitaly Buka | 13245af | 2016-11-18 21:20:12 | [diff] [blame] | 516 | const Descriptor* descriptor = message->GetDescriptor(); |
| 517 | const Reflection* reflection = message->GetReflection(); |
Vitaly Buka | 13245af | 2016-11-18 21:20:12 | [diff] [blame] | 518 | for (int i = 0; i < descriptor->field_count(); ++i) { |
| 519 | const FieldDescriptor* field = descriptor->field(i); |
Vitaly Buka | 91ad7b0 | 2016-12-12 23:41:41 | [diff] [blame] | 520 | if (field->is_required() && !reflection->HasField(*message, field)) |
| 521 | FieldInstance(message, field).Apply(CreateDefaultFieldTransformation()); |
Vitaly Buka | 13245af | 2016-11-18 21:20:12 | [diff] [blame] | 522 | |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 523 | if (max_depth > 0 && |
| 524 | field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
Vitaly Buka | 2cfe02b | 2016-11-19 00:34:09 | [diff] [blame] | 525 | if (field->is_repeated()) { |
| 526 | const int field_size = reflection->FieldSize(*message, field); |
| 527 | for (int j = 0; j < field_size; ++j) { |
| 528 | Message* nested_message = |
| 529 | reflection->MutableRepeatedMessage(message, field, j); |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 530 | if (!nested_message->IsInitialized()) |
| 531 | InitializeMessage(nested_message, max_depth - 1); |
Vitaly Buka | 2cfe02b | 2016-11-19 00:34:09 | [diff] [blame] | 532 | } |
| 533 | } else if (reflection->HasField(*message, field)) { |
| 534 | Message* nested_message = reflection->MutableMessage(message, field); |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 535 | if (!nested_message->IsInitialized()) |
| 536 | InitializeMessage(nested_message, max_depth - 1); |
Vitaly Buka | 13245af | 2016-11-18 21:20:12 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | } |
| 540 | } |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 541 | |
| 542 | int32_t ProtobufMutator::MutateInt32(int32_t value) { |
| 543 | return FlipBit(value, &random_); |
| 544 | } |
| 545 | |
| 546 | int64_t ProtobufMutator::MutateInt64(int64_t value) { |
| 547 | return FlipBit(value, &random_); |
| 548 | } |
| 549 | |
| 550 | uint32_t ProtobufMutator::MutateUInt32(uint32_t value) { |
| 551 | return FlipBit(value, &random_); |
| 552 | } |
| 553 | |
| 554 | uint64_t ProtobufMutator::MutateUInt64(uint64_t value) { |
| 555 | return FlipBit(value, &random_); |
| 556 | } |
| 557 | |
| 558 | float ProtobufMutator::MutateFloat(float value) { |
| 559 | return FlipBit(value, &random_); |
| 560 | } |
| 561 | |
| 562 | double ProtobufMutator::MutateDouble(double value) { |
| 563 | return FlipBit(value, &random_); |
| 564 | } |
| 565 | |
Vitaly Buka | beb9080 | 2017-02-28 23:28:10 | [diff] [blame] | 566 | bool ProtobufMutator::MutateBool(bool value) { return !value; } |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 567 | |
| 568 | size_t ProtobufMutator::MutateEnum(size_t index, size_t item_count) { |
Vitaly Buka | beb9080 | 2017-02-28 23:28:10 | [diff] [blame] | 569 | return (index + 1 + GetRandomIndex(&random_, item_count - 1)) % item_count; |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | std::string ProtobufMutator::MutateString(const std::string& value, |
Vitaly Buka | 5d01320 | 2017-02-25 00:50:11 | [diff] [blame] | 573 | size_t size_increase_hint) { |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 574 | std::string result = value; |
Vitaly Buka | 5d01320 | 2017-02-25 00:50:11 | [diff] [blame] | 575 | |
Vitaly Buka | beb9080 | 2017-02-28 23:28:10 | [diff] [blame] | 576 | while (!result.empty() && GetRandomBool(&random_)) { |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 577 | result.erase(GetRandomIndex(&random_, result.size()), 1); |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 578 | } |
| 579 | |
Vitaly Buka | beb9080 | 2017-02-28 23:28:10 | [diff] [blame] | 580 | while (result.size() < size_increase_hint && GetRandomBool(&random_)) { |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 581 | size_t index = GetRandomIndex(&random_, result.size() + 1); |
Vitaly Buka | 5d01320 | 2017-02-25 00:50:11 | [diff] [blame] | 582 | result.insert(result.begin() + index, GetRandomIndex(&random_, 1 << 8)); |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 583 | } |
| 584 | |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 585 | if (!result.empty()) |
| 586 | FlipBit(result.size(), reinterpret_cast<uint8_t*>(&result[0]), &random_); |
| 587 | return result; |
| 588 | } |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 589 | |
| 590 | } // namespace protobuf_mutator |