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 | 6c6dbbe | 2017-02-22 21:58:24 | [diff] [blame^] | 26 | using protobuf::Descriptor; |
| 27 | using protobuf::EnumDescriptor; |
| 28 | using protobuf::EnumValueDescriptor; |
| 29 | using protobuf::FieldDescriptor; |
| 30 | using protobuf::Message; |
| 31 | using protobuf::OneofDescriptor; |
| 32 | using protobuf::Reflection; |
Vitaly Buka | 0e17fd7 | 2016-11-18 18:02:46 | [diff] [blame] | 33 | |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 34 | namespace protobuf_mutator { |
| 35 | |
Vitaly Buka | 0e17fd7 | 2016-11-18 18:02:46 | [diff] [blame] | 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 | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 53 | // Flips random bit in the buffer. |
| 54 | void FlipBit(size_t size, uint8_t* bytes, |
| 55 | ProtobufMutator::RandomEngine* random) { |
| 56 | size_t bit = std::uniform_int_distribution<size_t>(0, size * 8 - 1)(*random); |
| 57 | bytes[bit / 8] ^= (1u << (bit % 8)); |
| 58 | } |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 59 | |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 60 | // Flips random bit in the value. |
| 61 | template <class T> |
| 62 | T FlipBit(T value, ProtobufMutator::RandomEngine* random) { |
| 63 | FlipBit(sizeof(value), reinterpret_cast<uint8_t*>(&value), random); |
| 64 | return value; |
| 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 | // Return random integer from [0, count) |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 68 | size_t GetRandomIndex(ProtobufMutator::RandomEngine* random, size_t count) { |
| 69 | assert(count > 0); |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 70 | if (count == 1) return 0; |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 71 | return std::uniform_int_distribution<size_t>(0, count - 1)(*random); |
| 72 | } |
Vitaly Buka | 0e17fd7 | 2016-11-18 18:02:46 | [diff] [blame] | 73 | |
Vitaly Buka | 91ad7b0 | 2016-12-12 23:41:41 | [diff] [blame] | 74 | struct CreateDefaultFieldTransformation { |
| 75 | template <class T> |
| 76 | void Apply(const FieldInstance& field) const { |
| 77 | T value; |
| 78 | field.GetDefault(&value); |
| 79 | field.Create(value); |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 80 | } |
Vitaly Buka | 91ad7b0 | 2016-12-12 23:41:41 | [diff] [blame] | 81 | }; |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 82 | |
Vitaly Buka | 91ad7b0 | 2016-12-12 23:41:41 | [diff] [blame] | 83 | struct DeleteFieldTransformation { |
| 84 | template <class T> |
| 85 | void Apply(const FieldInstance& field) const { |
| 86 | field.Delete(); |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 87 | } |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 88 | }; |
| 89 | |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 90 | struct CopyFieldTransformation { |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 91 | explicit CopyFieldTransformation(const FieldInstance& field) |
| 92 | : source(field) {} |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 93 | |
| 94 | template <class T> |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 95 | void Apply(const FieldInstance& field) const { |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 96 | T value; |
| 97 | source.Load(&value); |
| 98 | field.Store(value); |
| 99 | } |
| 100 | |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 101 | FieldInstance source; |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 102 | }; |
| 103 | |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 104 | // Selects random field and mutation from the given proto message. |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 105 | class MutationSampler { |
Vitaly Buka | c9d2248 | 2016-11-21 21:29:17 | [diff] [blame] | 106 | public: |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 107 | MutationSampler(bool keep_initialized, size_t size_increase_hint, |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 108 | ProtobufMutator::RandomEngine* random, Message* message) |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 109 | : keep_initialized_(keep_initialized), random_(random), sampler_(random) { |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 110 | if (size_increase_hint < kDeletionThreshold) { |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 111 | // Avoid adding new field and prefer deleting fields if we getting close |
| 112 | // to the limit. |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 113 | float adjustment = 0.5 * size_increase_hint / kDeletionThreshold; |
| 114 | add_weight_ *= adjustment; |
| 115 | delete_weight_ *= 1 - adjustment; |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 116 | } |
| 117 | Sample(message); |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 118 | assert(mutation() != Mutation::None); |
Vitaly Buka | c9d2248 | 2016-11-21 21:29:17 | [diff] [blame] | 119 | } |
| 120 | |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 121 | // Returns selected field. |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 122 | const FieldInstance& field() const { return sampler_.selected().field; } |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 123 | |
| 124 | // Returns selected mutation. |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 125 | Mutation mutation() const { return sampler_.selected().mutation; } |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 126 | |
| 127 | private: |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 128 | void Sample(Message* message) { |
| 129 | const Descriptor* descriptor = message->GetDescriptor(); |
| 130 | const Reflection* reflection = message->GetReflection(); |
| 131 | |
| 132 | int field_count = descriptor->field_count(); |
| 133 | for (int i = 0; i < field_count; ++i) { |
| 134 | const FieldDescriptor* field = descriptor->field(i); |
| 135 | if (const OneofDescriptor* oneof = field->containing_oneof()) { |
| 136 | // Handle entire oneof group on the first field. |
| 137 | if (field->index_in_oneof() == 0) { |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 138 | sampler_.Try( |
| 139 | add_weight_, |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 140 | {{message, |
| 141 | oneof->field(GetRandomIndex(random_, oneof->field_count()))}, |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 142 | Mutation::Add}); |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 143 | if (const FieldDescriptor* field = |
| 144 | reflection->GetOneofFieldDescriptor(*message, oneof)) { |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 145 | if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 146 | sampler_.Try(kMutateWeight, {{message, field}, Mutation::Mutate}); |
| 147 | sampler_.Try(delete_weight_, {{message, field}, Mutation::Delete}); |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 148 | sampler_.Try(GetCopyWeight(field), |
| 149 | {{message, field}, Mutation::Copy}); |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | } else { |
| 153 | if (field->is_repeated()) { |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 154 | int field_size = reflection->FieldSize(*message, field); |
| 155 | sampler_.Try(add_weight_, {{message, field, |
| 156 | GetRandomIndex(random_, field_size + 1)}, |
| 157 | Mutation::Add}); |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 158 | |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 159 | if (field_size) { |
| 160 | size_t random_index = GetRandomIndex(random_, field_size); |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 161 | if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 162 | sampler_.Try(kMutateWeight, |
| 163 | {{message, field, random_index}, Mutation::Mutate}); |
| 164 | sampler_.Try(delete_weight_, |
| 165 | {{message, field, random_index}, Mutation::Delete}); |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 166 | sampler_.Try(GetCopyWeight(field), |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 167 | {{message, field, random_index}, Mutation::Copy}); |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 168 | } |
| 169 | } else { |
| 170 | if (reflection->HasField(*message, field)) { |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 171 | if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 172 | sampler_.Try(kMutateWeight, {{message, field}, Mutation::Mutate}); |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 173 | if ((!field->is_required() || !keep_initialized_)) |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 174 | sampler_.Try(delete_weight_, |
| 175 | {{message, field}, Mutation::Delete}); |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 176 | sampler_.Try(GetCopyWeight(field), |
| 177 | {{message, field}, Mutation::Copy}); |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 178 | } else { |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 179 | sampler_.Try(add_weight_, {{message, field}, Mutation::Add}); |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
| 185 | if (field->is_repeated()) { |
| 186 | const int field_size = reflection->FieldSize(*message, field); |
| 187 | for (int j = 0; j < field_size; ++j) |
| 188 | Sample(reflection->MutableRepeatedMessage(message, field, j)); |
| 189 | } else if (reflection->HasField(*message, field)) { |
| 190 | Sample(reflection->MutableMessage(message, field)); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 196 | uint64_t GetCopyWeight(const FieldDescriptor* field) const { |
| 197 | // Coping sub-messages can increase size significantly. |
| 198 | return field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE |
| 199 | ? add_weight_ |
| 200 | : kMutateWeight; |
| 201 | } |
| 202 | |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 203 | bool keep_initialized_ = false; |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 204 | |
| 205 | // Adding and deleting are intrusive and expensive mutations, we'd like to do |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 206 | // them less often than field mutations. |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 207 | uint64_t add_weight_ = kMutateWeight / 10; |
| 208 | uint64_t delete_weight_ = kMutateWeight / 10; |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 209 | |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 210 | ProtobufMutator::RandomEngine* random_; |
| 211 | |
| 212 | struct Result { |
Vitaly Buka | 91ad7b0 | 2016-12-12 23:41:41 | [diff] [blame] | 213 | Result() = default; |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 214 | Result(const FieldInstance& f, Mutation m) : field(f), mutation(m) {} |
Vitaly Buka | 91ad7b0 | 2016-12-12 23:41:41 | [diff] [blame] | 215 | |
Vitaly Buka | bec5222 | 2016-12-09 22:29:32 | [diff] [blame] | 216 | FieldInstance field; |
| 217 | Mutation mutation = Mutation::None; |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 218 | }; |
| 219 | WeightedReservoirSampler<Result, ProtobufMutator::RandomEngine> sampler_; |
Vitaly Buka | c9d2248 | 2016-11-21 21:29:17 | [diff] [blame] | 220 | }; |
| 221 | |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 222 | // Selects random field of compatible type to use for clone mutations. |
| 223 | class DataSourceSampler { |
| 224 | public: |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 225 | DataSourceSampler(const FieldInstance& match, |
| 226 | ProtobufMutator::RandomEngine* random, Message* message) |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 227 | : match_(match), random_(random), sampler_(random) { |
| 228 | Sample(message); |
| 229 | } |
| 230 | |
| 231 | // Returns selected field. |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 232 | const FieldInstance& field() const { |
| 233 | assert(!IsEmpty()); |
| 234 | return sampler_.selected(); |
| 235 | } |
| 236 | |
| 237 | bool IsEmpty() const { return sampler_.IsEmpty(); } |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 238 | |
| 239 | private: |
| 240 | void Sample(Message* message) { |
| 241 | const Descriptor* descriptor = message->GetDescriptor(); |
| 242 | const Reflection* reflection = message->GetReflection(); |
| 243 | |
| 244 | int field_count = descriptor->field_count(); |
| 245 | for (int i = 0; i < field_count; ++i) { |
| 246 | const FieldDescriptor* field = descriptor->field(i); |
| 247 | if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
| 248 | if (field->is_repeated()) { |
| 249 | const int field_size = reflection->FieldSize(*message, field); |
| 250 | for (int j = 0; j < field_size; ++j) { |
| 251 | Sample(reflection->MutableRepeatedMessage(message, field, j)); |
| 252 | } |
| 253 | } else if (reflection->HasField(*message, field)) { |
| 254 | Sample(reflection->MutableMessage(message, field)); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | if (field->cpp_type() != match_.cpp_type()) continue; |
| 259 | if (match_.cpp_type() == FieldDescriptor::CPPTYPE_ENUM) { |
| 260 | if (field->enum_type() != match_.enum_type()) continue; |
| 261 | } else if (match_.cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
| 262 | if (field->message_type() != match_.message_type()) continue; |
| 263 | } |
| 264 | |
| 265 | // TODO(vitalybuka) : make sure that values are different |
| 266 | if (field->is_repeated()) { |
| 267 | if (int field_size = reflection->FieldSize(*message, field)) { |
| 268 | sampler_.Try(field_size, |
| 269 | {message, field, GetRandomIndex(random_, field_size)}); |
| 270 | } |
| 271 | } else { |
| 272 | if (reflection->HasField(*message, field)) { |
| 273 | sampler_.Try(1, {message, field}); |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 279 | FieldInstance match_; |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 280 | ProtobufMutator::RandomEngine* random_; |
| 281 | |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 282 | WeightedReservoirSampler<FieldInstance, ProtobufMutator::RandomEngine> |
| 283 | sampler_; |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 284 | }; |
| 285 | |
Vitaly Buka | 0e17fd7 | 2016-11-18 18:02:46 | [diff] [blame] | 286 | } // namespace |
| 287 | |
Vitaly Buka | 91ad7b0 | 2016-12-12 23:41:41 | [diff] [blame] | 288 | class MutateTransformation { |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 289 | public: |
Vitaly Buka | 91ad7b0 | 2016-12-12 23:41:41 | [diff] [blame] | 290 | MutateTransformation(size_t allowed_growth, ProtobufMutator* mutator) |
| 291 | : allowed_growth_(allowed_growth), mutator_(mutator) {} |
| 292 | |
| 293 | template <class T> |
| 294 | void Apply(const FieldInstance& field) const { |
| 295 | T value; |
| 296 | field.Load(&value); |
| 297 | Mutate(&value); |
| 298 | field.Store(value); |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 299 | } |
Vitaly Buka | 00b6107 | 2016-10-19 23:22:51 | [diff] [blame] | 300 | |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 301 | private: |
Vitaly Buka | 91ad7b0 | 2016-12-12 23:41:41 | [diff] [blame] | 302 | void Mutate(int32_t* value) const { *value = mutator_->MutateInt32(*value); } |
| 303 | |
| 304 | void Mutate(int64_t* value) const { *value = mutator_->MutateInt64(*value); } |
| 305 | |
| 306 | void Mutate(uint32_t* value) const { |
| 307 | *value = mutator_->MutateUInt32(*value); |
| 308 | } |
| 309 | |
| 310 | void Mutate(uint64_t* value) const { |
| 311 | *value = mutator_->MutateUInt64(*value); |
| 312 | } |
| 313 | |
| 314 | void Mutate(float* value) const { *value = mutator_->MutateFloat(*value); } |
| 315 | |
| 316 | void Mutate(double* value) const { *value = mutator_->MutateDouble(*value); } |
| 317 | |
| 318 | void Mutate(bool* value) const { *value = mutator_->MutateBool(*value); } |
| 319 | |
| 320 | void Mutate(FieldInstance::Enum* value) const { |
| 321 | value->index = mutator_->MutateEnum(value->index, value->count); |
| 322 | assert(value->index < value->count); |
| 323 | } |
| 324 | |
| 325 | void Mutate(std::string* value) const { |
| 326 | *value = mutator_->MutateString(*value, allowed_growth_); |
| 327 | } |
| 328 | |
Vitaly Buka | 6c6dbbe | 2017-02-22 21:58:24 | [diff] [blame^] | 329 | void Mutate(std::unique_ptr<Message>*) const { |
| 330 | assert(false && "Unexpected"); |
| 331 | } |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 332 | |
| 333 | size_t allowed_growth_; |
| 334 | ProtobufMutator* mutator_; |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 335 | }; |
| 336 | |
Vitaly Buka | ba12972 | 2016-12-15 01:29:15 | [diff] [blame] | 337 | ProtobufMutator::ProtobufMutator(uint32_t seed) : random_(seed) {} |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 338 | |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 339 | void ProtobufMutator::Mutate(Message* message, size_t size_increase_hint) { |
| 340 | MutationSampler mutation(keep_initialized_, size_increase_hint, &random_, |
| 341 | message); |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 342 | switch (mutation.mutation()) { |
| 343 | case Mutation::None: |
| 344 | break; |
| 345 | case Mutation::Add: |
Vitaly Buka | 91ad7b0 | 2016-12-12 23:41:41 | [diff] [blame] | 346 | mutation.field().Apply(CreateDefaultFieldTransformation()); |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 347 | break; |
| 348 | case Mutation::Mutate: |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 349 | mutation.field().Apply( |
| 350 | MutateTransformation(size_increase_hint / 4, this)); |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 351 | break; |
| 352 | case Mutation::Delete: |
Vitaly Buka | 91ad7b0 | 2016-12-12 23:41:41 | [diff] [blame] | 353 | mutation.field().Apply(DeleteFieldTransformation()); |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 354 | break; |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 355 | case Mutation::Copy: { |
| 356 | DataSourceSampler source(mutation.field(), &random_, message); |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 357 | if (source.IsEmpty()) { |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 358 | // Fallback to message deletion. |
| 359 | mutation.field().Apply(DeleteFieldTransformation()); |
| 360 | break; |
| 361 | } |
Vitaly Buka | a3e59c7 | 2016-12-07 00:53:56 | [diff] [blame] | 362 | mutation.field().Apply(CopyFieldTransformation(source.field())); |
| 363 | break; |
| 364 | } |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 365 | default: |
Vitaly Buka | 6c6dbbe | 2017-02-22 21:58:24 | [diff] [blame^] | 366 | assert(false && "unexpected mutation"); |
Vitaly Buka | 0e17fd7 | 2016-11-18 18:02:46 | [diff] [blame] | 367 | } |
| 368 | |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 369 | if (keep_initialized_ && !message->IsInitialized()) { |
| 370 | InitializeMessage(message, kMaxInitializeDepth); |
| 371 | assert(message->IsInitialized()); |
Vitaly Buka | 0e17fd7 | 2016-11-18 18:02:46 | [diff] [blame] | 372 | } |
Vitaly Buka | 00b6107 | 2016-10-19 23:22:51 | [diff] [blame] | 373 | } |
| 374 | |
Vitaly Buka | 72019dc | 2016-12-15 03:17:24 | [diff] [blame] | 375 | void ProtobufMutator::InitializeMessage(Message* message, size_t max_depth) { |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 376 | assert(keep_initialized_); |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 377 | // It's pointless but possible to have infinite recursion of required |
| 378 | // messages. |
| 379 | assert(max_depth); |
Vitaly Buka | 13245af | 2016-11-18 21:20:12 | [diff] [blame] | 380 | const Descriptor* descriptor = message->GetDescriptor(); |
| 381 | const Reflection* reflection = message->GetReflection(); |
Vitaly Buka | 13245af | 2016-11-18 21:20:12 | [diff] [blame] | 382 | for (int i = 0; i < descriptor->field_count(); ++i) { |
| 383 | const FieldDescriptor* field = descriptor->field(i); |
Vitaly Buka | 91ad7b0 | 2016-12-12 23:41:41 | [diff] [blame] | 384 | if (field->is_required() && !reflection->HasField(*message, field)) |
| 385 | FieldInstance(message, field).Apply(CreateDefaultFieldTransformation()); |
Vitaly Buka | 13245af | 2016-11-18 21:20:12 | [diff] [blame] | 386 | |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 387 | if (max_depth > 0 && |
| 388 | field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
Vitaly Buka | 2cfe02b | 2016-11-19 00:34:09 | [diff] [blame] | 389 | if (field->is_repeated()) { |
| 390 | const int field_size = reflection->FieldSize(*message, field); |
| 391 | for (int j = 0; j < field_size; ++j) { |
| 392 | Message* nested_message = |
| 393 | reflection->MutableRepeatedMessage(message, field, j); |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 394 | if (!nested_message->IsInitialized()) |
| 395 | InitializeMessage(nested_message, max_depth - 1); |
Vitaly Buka | 2cfe02b | 2016-11-19 00:34:09 | [diff] [blame] | 396 | } |
| 397 | } else if (reflection->HasField(*message, field)) { |
| 398 | Message* nested_message = reflection->MutableMessage(message, field); |
Vitaly Buka | 781853c | 2016-11-22 07:09:35 | [diff] [blame] | 399 | if (!nested_message->IsInitialized()) |
| 400 | InitializeMessage(nested_message, max_depth - 1); |
Vitaly Buka | 13245af | 2016-11-18 21:20:12 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | } |
| 404 | } |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 405 | |
| 406 | int32_t ProtobufMutator::MutateInt32(int32_t value) { |
| 407 | return FlipBit(value, &random_); |
| 408 | } |
| 409 | |
| 410 | int64_t ProtobufMutator::MutateInt64(int64_t value) { |
| 411 | return FlipBit(value, &random_); |
| 412 | } |
| 413 | |
| 414 | uint32_t ProtobufMutator::MutateUInt32(uint32_t value) { |
| 415 | return FlipBit(value, &random_); |
| 416 | } |
| 417 | |
| 418 | uint64_t ProtobufMutator::MutateUInt64(uint64_t value) { |
| 419 | return FlipBit(value, &random_); |
| 420 | } |
| 421 | |
| 422 | float ProtobufMutator::MutateFloat(float value) { |
| 423 | return FlipBit(value, &random_); |
| 424 | } |
| 425 | |
| 426 | double ProtobufMutator::MutateDouble(double value) { |
| 427 | return FlipBit(value, &random_); |
| 428 | } |
| 429 | |
| 430 | bool ProtobufMutator::MutateBool(bool value) { |
| 431 | return std::uniform_int_distribution<uint8_t>(0, 1)(random_); |
| 432 | } |
| 433 | |
| 434 | size_t ProtobufMutator::MutateEnum(size_t index, size_t item_count) { |
| 435 | return (index + |
| 436 | std::uniform_int_distribution<uint8_t>(1, item_count - 1)(random_)) % |
| 437 | item_count; |
| 438 | } |
| 439 | |
| 440 | std::string ProtobufMutator::MutateString(const std::string& value, |
| 441 | size_t allowed_growth) { |
| 442 | std::string result = value; |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 443 | int min_diff = result.empty() ? 0 : -1; |
| 444 | int max_diff = allowed_growth ? 1 : 0; |
| 445 | int diff = std::uniform_int_distribution<int>(min_diff, max_diff)(random_); |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 446 | if (diff == -1) { |
| 447 | result.erase(GetRandomIndex(&random_, result.size()), 1); |
| 448 | return result; |
| 449 | } |
| 450 | |
| 451 | if (diff == 1) { |
| 452 | size_t index = GetRandomIndex(&random_, result.size() + 1); |
| 453 | result.insert(result.begin() + index, '\0'); |
| 454 | FlipBit(1, reinterpret_cast<uint8_t*>(&result[index]), &random_); |
| 455 | return result; |
| 456 | } |
| 457 | |
Vitaly Buka | 4af611d | 2016-12-04 02:57:32 | [diff] [blame] | 458 | if (!result.empty()) |
| 459 | FlipBit(result.size(), reinterpret_cast<uint8_t*>(&result[0]), &random_); |
| 460 | return result; |
| 461 | } |
Vitaly Buka | 432b545 | 2016-12-09 22:42:09 | [diff] [blame] | 462 | |
| 463 | } // namespace protobuf_mutator |