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