Vitaly Buka | b5c12da | 2016-10-19 13:12:41 -0700 | [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 15:46:58 -0800 | [diff] [blame] | 15 | #ifndef SRC_MUTATOR_H_ |
| 16 | #define SRC_MUTATOR_H_ |
Vitaly Buka | b5c12da | 2016-10-19 13:12:41 -0700 | [diff] [blame] | 17 | |
Vitaly Buka | 781853c | 2016-11-21 23:09:35 -0800 | [diff] [blame] | 18 | #include <stddef.h> |
| 19 | #include <stdint.h> |
| 20 | |
Allen-Webb | c4fa591 | 2018-09-07 15:53:30 -0700 | [diff] [blame] | 21 | #include <functional> |
Vitaly Buka | 781853c | 2016-11-21 23:09:35 -0800 | [diff] [blame] | 22 | #include <memory> |
Vitaly Buka | 0e17fd7 | 2016-11-18 10:02:46 -0800 | [diff] [blame] | 23 | #include <random> |
Vitaly Buka | f047a00 | 2017-01-12 23:57:00 -0800 | [diff] [blame] | 24 | #include <string> |
Allen-Webb | c4fa591 | 2018-09-07 15:53:30 -0700 | [diff] [blame] | 25 | #include <unordered_map> |
| 26 | #include <vector> |
Vitaly Buka | 00b6107 | 2016-10-19 16:22:51 -0700 | [diff] [blame] | 27 | |
Vitaly Buka | 35df2e4 | 2017-02-25 01:17:25 -0800 | [diff] [blame] | 28 | #include "port/protobuf.h" |
Vitaly Buka | f62fe47 | 2017-03-01 23:05:15 -0800 | [diff] [blame] | 29 | #include "src/random.h" |
Vitaly Buka | b5c12da | 2016-10-19 13:12:41 -0700 | [diff] [blame] | 30 | |
Vitaly Buka | 432b545 | 2016-12-09 14:42:09 -0800 | [diff] [blame] | 31 | namespace protobuf_mutator { |
| 32 | |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 33 | // Randomly makes incremental change in the given protobuf. |
| 34 | // Usage example: |
Vitaly Buka | e79e018 | 2017-03-01 16:02:14 -0800 | [diff] [blame] | 35 | // protobuf_mutator::Mutator mutator(1); |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 36 | // MyMessage message; |
| 37 | // message.ParseFromString(encoded_message); |
Vitaly Buka | 4a6d6fc | 2016-12-16 14:21:37 -0800 | [diff] [blame] | 38 | // mutator.Mutate(&message, 10000); |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 39 | // |
Vitaly Buka | 432b545 | 2016-12-09 14:42:09 -0800 | [diff] [blame] | 40 | // Class implements very basic mutations of fields. E.g. it just flips bits for |
| 41 | // integers, floats and strings. Also it increases, decreases size of |
| 42 | // strings only by one. For better results users should override |
Vitaly Buka | e79e018 | 2017-03-01 16:02:14 -0800 | [diff] [blame] | 43 | // protobuf_mutator::Mutator::Mutate* methods with more useful logic, e.g. using |
| 44 | // library like libFuzzer. |
| 45 | class Mutator { |
Vitaly Buka | b5c12da | 2016-10-19 13:12:41 -0700 | [diff] [blame] | 46 | public: |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 47 | // seed: value to initialize random number generator. |
Vitaly Buka | f62fe47 | 2017-03-01 23:05:15 -0800 | [diff] [blame] | 48 | explicit Mutator(RandomEngine* random); |
Vitaly Buka | e79e018 | 2017-03-01 16:02:14 -0800 | [diff] [blame] | 49 | virtual ~Mutator() = default; |
Vitaly Buka | 781853c | 2016-11-21 23:09:35 -0800 | [diff] [blame] | 50 | |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 51 | // message: message to mutate. |
Vitaly Buka | 72019dc | 2016-12-14 19:17:24 -0800 | [diff] [blame] | 52 | // size_increase_hint: approximate number of bytes which can be added to the |
| 53 | // message. Method does not guarantee that real result size increase will be |
| 54 | // less than the value. It only changes probabilities of mutations which can |
| 55 | // cause size increase. Caller could repeat mutation if result was larger than |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 56 | // requested. |
Vitaly Buka | 6c6dbbe | 2017-02-22 13:58:24 -0800 | [diff] [blame] | 57 | void Mutate(protobuf::Message* message, size_t size_increase_hint); |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 58 | |
Vitaly Buka | adfc27c | 2017-02-26 22:36:36 -0800 | [diff] [blame] | 59 | void CrossOver(const protobuf::Message& message1, |
| 60 | protobuf::Message* message2); |
Vitaly Buka | b5c12da | 2016-10-19 13:12:41 -0700 | [diff] [blame] | 61 | |
Allen-Webb | c4fa591 | 2018-09-07 15:53:30 -0700 | [diff] [blame] | 62 | // field: Descriptor of the field to apply the mutation to. |
| 63 | // mutation: callback function that applies the mutation. |
| 64 | static void RegisterCustomMutation( |
| 65 | const protobuf::FieldDescriptor* field, |
| 66 | std::function<void(protobuf::Message* message)> mutation); |
| 67 | |
Vitaly Buka | 432b545 | 2016-12-09 14:42:09 -0800 | [diff] [blame] | 68 | protected: |
Vitaly Buka | b93a146 | 2017-01-06 17:52:58 -0800 | [diff] [blame] | 69 | // TODO(vitalybuka): Consider to replace with single mutate (uint8_t*, size). |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 70 | virtual int32_t MutateInt32(int32_t value); |
| 71 | virtual int64_t MutateInt64(int64_t value); |
| 72 | virtual uint32_t MutateUInt32(uint32_t value); |
| 73 | virtual uint64_t MutateUInt64(uint64_t value); |
| 74 | virtual float MutateFloat(float value); |
| 75 | virtual double MutateDouble(double value); |
| 76 | virtual bool MutateBool(bool value); |
| 77 | virtual size_t MutateEnum(size_t index, size_t item_count); |
| 78 | virtual std::string MutateString(const std::string& value, |
Vitaly Buka | 5d01320 | 2017-02-24 16:50:11 -0800 | [diff] [blame] | 79 | size_t size_increase_hint); |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 80 | |
Vitaly Buka | 432b545 | 2016-12-09 14:42:09 -0800 | [diff] [blame] | 81 | // TODO(vitalybuka): Allow user to control proto level mutations: |
Vitaly Buka | 4af611d | 2016-12-03 18:57:32 -0800 | [diff] [blame] | 82 | // * Callbacks to recursive traversal. |
| 83 | // * Callbacks for particular proto level mutations. |
Vitaly Buka | 2cfe02b | 2016-11-18 16:34:09 -0800 | [diff] [blame] | 84 | |
Vitaly Buka | f62fe47 | 2017-03-01 23:05:15 -0800 | [diff] [blame] | 85 | RandomEngine* random() { return random_; } |
Vitaly Buka | 68e49ad | 2017-02-24 14:24:31 -0800 | [diff] [blame] | 86 | |
Allen-Webb | c4fa591 | 2018-09-07 15:53:30 -0700 | [diff] [blame] | 87 | static std::unordered_map< |
| 88 | const protobuf::FieldDescriptor*, |
| 89 | std::vector<std::function<void(protobuf::Message* message)>>> |
| 90 | custom_mutations_; |
| 91 | |
Vitaly Buka | b5c12da | 2016-10-19 13:12:41 -0700 | [diff] [blame] | 92 | private: |
Vitaly Buka | 5d01320 | 2017-02-24 16:50:11 -0800 | [diff] [blame] | 93 | friend class FieldMutator; |
Vitaly Buka | e79e018 | 2017-03-01 16:02:14 -0800 | [diff] [blame] | 94 | friend class TestMutator; |
Vitaly Buka | e4eae60 | 2017-09-28 17:28:12 -0700 | [diff] [blame] | 95 | void InitializeAndTrim(protobuf::Message* message, int max_depth); |
Vitaly Buka | adfc27c | 2017-02-26 22:36:36 -0800 | [diff] [blame] | 96 | void CrossOverImpl(const protobuf::Message& message1, |
| 97 | protobuf::Message* message2); |
Vitaly Buka | af8136f | 2017-06-09 16:40:12 -0700 | [diff] [blame] | 98 | std::string MutateUtf8String(const std::string& value, |
| 99 | size_t size_increase_hint); |
Allen-Webb | c4fa591 | 2018-09-07 15:53:30 -0700 | [diff] [blame] | 100 | bool ApplyCustomMutations(protobuf::Message* message, |
| 101 | const protobuf::FieldDescriptor* field); |
Vitaly Buka | ba12972 | 2016-12-14 17:29:15 -0800 | [diff] [blame] | 102 | bool keep_initialized_ = true; |
Vitaly Buka | f62fe47 | 2017-03-01 23:05:15 -0800 | [diff] [blame] | 103 | RandomEngine* random_; |
Vitaly Buka | b5c12da | 2016-10-19 13:12:41 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
Vitaly Buka | 432b545 | 2016-12-09 14:42:09 -0800 | [diff] [blame] | 106 | } // namespace protobuf_mutator |
| 107 | |
Vitaly Buka | f90698f | 2017-03-01 15:46:58 -0800 | [diff] [blame] | 108 | #endif // SRC_MUTATOR_H_ |