Daniel McArdle | 72692a1 | 2020-05-11 16:47:16 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include <stddef.h> |
| 6 | #include <stdint.h> |
| 7 | |
| 8 | #include <memory> |
| 9 | |
| 10 | #include "base/json/json_reader.h" |
Hans Wennborg | 725d043 | 2020-06-18 13:54:16 | [diff] [blame] | 11 | #include "base/logging.h" |
Daniel McArdle | 72692a1 | 2020-05-11 16:47:16 | [diff] [blame] | 12 | #include "base/strings/string_piece_forward.h" |
Daniel McArdle | bbfc145 | 2020-05-18 15:11:55 | [diff] [blame] | 13 | #include "base/time/tick_clock.h" |
Daniel McArdle | 72692a1 | 2020-05-11 16:47:16 | [diff] [blame] | 14 | #include "base/time/time.h" |
| 15 | #include "net/base/backoff_entry.h" |
| 16 | #include "net/base/backoff_entry_serializer.h" |
| 17 | #include "net/base/backoff_entry_serializer_fuzzer_input.pb.h" |
| 18 | #include "testing/libfuzzer/proto/json_proto_converter.h" |
| 19 | #include "testing/libfuzzer/proto/lpm_interface.h" |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 20 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Daniel McArdle | 72692a1 | 2020-05-11 16:47:16 | [diff] [blame] | 21 | |
| 22 | namespace net { |
| 23 | |
| 24 | namespace { |
| 25 | struct Environment { |
| 26 | Environment() { logging::SetMinLogLevel(logging::LOG_ERROR); } |
| 27 | }; |
| 28 | |
| 29 | class ProtoTranslator { |
| 30 | public: |
| 31 | explicit ProtoTranslator(const fuzz_proto::FuzzerInput& input) |
| 32 | : input_(input) {} |
| 33 | |
| 34 | BackoffEntry::Policy policy() const { |
| 35 | return PolicyFromProto(input_.policy()); |
| 36 | } |
Daniel McArdle | bbfc145 | 2020-05-18 15:11:55 | [diff] [blame] | 37 | base::Time parse_time() const { |
| 38 | return base::Time() + |
| 39 | base::TimeDelta::FromMicroseconds(input_.parse_time()); |
Daniel McArdle | 72692a1 | 2020-05-11 16:47:16 | [diff] [blame] | 40 | } |
Dan McArdle | 82d7097 | 2021-04-12 15:54:20 | [diff] [blame] | 41 | base::TimeTicks parse_time_ticks() const { |
| 42 | return base::TimeTicks() + |
| 43 | base::TimeDelta::FromMicroseconds(input_.parse_time()); |
| 44 | } |
Daniel McArdle | bbfc145 | 2020-05-18 15:11:55 | [diff] [blame] | 45 | base::Time serialize_time() const { |
| 46 | return base::Time() + |
| 47 | base::TimeDelta::FromMicroseconds(input_.serialize_time()); |
| 48 | } |
| 49 | base::TimeTicks now_ticks() const { |
| 50 | return base::TimeTicks() + |
| 51 | base::TimeDelta::FromMicroseconds(input_.now_ticks()); |
| 52 | } |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 53 | absl::optional<base::Value> serialized_entry() const { |
Daniel McArdle | 72692a1 | 2020-05-11 16:47:16 | [diff] [blame] | 54 | json_proto::JsonProtoConverter converter; |
| 55 | std::string json_array = converter.Convert(input_.serialized_entry()); |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 56 | absl::optional<base::Value> value = base::JSONReader::Read(json_array); |
Daniel McArdle | 72692a1 | 2020-05-11 16:47:16 | [diff] [blame] | 57 | return value; |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | const fuzz_proto::FuzzerInput& input_; |
| 62 | |
| 63 | static BackoffEntry::Policy PolicyFromProto( |
| 64 | const fuzz_proto::BackoffEntryPolicy& policy) { |
Daniel McArdle | bbfc145 | 2020-05-18 15:11:55 | [diff] [blame] | 65 | BackoffEntry::Policy new_policy; |
| 66 | new_policy.num_errors_to_ignore = policy.num_errors_to_ignore(); |
| 67 | new_policy.initial_delay_ms = policy.initial_delay_ms(); |
| 68 | new_policy.multiply_factor = policy.multiply_factor(); |
| 69 | new_policy.jitter_factor = policy.jitter_factor(); |
| 70 | new_policy.maximum_backoff_ms = policy.maximum_backoff_ms(); |
| 71 | new_policy.entry_lifetime_ms = policy.entry_lifetime_ms(); |
| 72 | new_policy.always_use_initial_delay = policy.always_use_initial_delay(); |
| 73 | return new_policy; |
Daniel McArdle | 72692a1 | 2020-05-11 16:47:16 | [diff] [blame] | 74 | } |
| 75 | }; |
| 76 | |
Daniel McArdle | bbfc145 | 2020-05-18 15:11:55 | [diff] [blame] | 77 | class MockClock : public base::TickClock { |
| 78 | public: |
| 79 | MockClock() = default; |
| 80 | ~MockClock() override = default; |
| 81 | |
| 82 | void SetNow(base::TimeTicks now) { now_ = now; } |
| 83 | base::TimeTicks NowTicks() const override { return now_; } |
| 84 | |
| 85 | private: |
| 86 | base::TimeTicks now_; |
| 87 | }; |
| 88 | |
Daniel McArdle | 72692a1 | 2020-05-11 16:47:16 | [diff] [blame] | 89 | // Tests the "deserialize-reserialize" property. Deserializes a BackoffEntry |
Daniel McArdle | bbfc145 | 2020-05-18 15:11:55 | [diff] [blame] | 90 | // from JSON, reserializes it, then deserializes again. Holding time constant, |
| 91 | // we check that the parsed BackoffEntry values are equivalent. |
Daniel McArdle | 72692a1 | 2020-05-11 16:47:16 | [diff] [blame] | 92 | void TestDeserialize(const ProtoTranslator& translator) { |
| 93 | // Attempt to convert the json_proto.ArrayValue to a base::Value. |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 94 | absl::optional<base::Value> value = translator.serialized_entry(); |
Daniel McArdle | 72692a1 | 2020-05-11 16:47:16 | [diff] [blame] | 95 | if (!value) |
| 96 | return; |
| 97 | DCHECK(value->is_list()); |
| 98 | |
| 99 | BackoffEntry::Policy policy = translator.policy(); |
| 100 | |
Daniel McArdle | bbfc145 | 2020-05-18 15:11:55 | [diff] [blame] | 101 | MockClock clock; |
Dan McArdle | 82d7097 | 2021-04-12 15:54:20 | [diff] [blame] | 102 | clock.SetNow(translator.parse_time_ticks()); |
Daniel McArdle | bbfc145 | 2020-05-18 15:11:55 | [diff] [blame] | 103 | |
Daniel McArdle | 72692a1 | 2020-05-11 16:47:16 | [diff] [blame] | 104 | // Attempt to deserialize a BackoffEntry. |
| 105 | std::unique_ptr<BackoffEntry> entry = |
Daniel McArdle | bbfc145 | 2020-05-18 15:11:55 | [diff] [blame] | 106 | BackoffEntrySerializer::DeserializeFromValue(*value, &policy, &clock, |
Daniel McArdle | 72692a1 | 2020-05-11 16:47:16 | [diff] [blame] | 107 | translator.parse_time()); |
| 108 | if (!entry) |
| 109 | return; |
| 110 | |
Maks Orlovich | 691496c | 2021-05-18 15:03:21 | [diff] [blame^] | 111 | base::Value reserialized = |
Daniel McArdle | bbfc145 | 2020-05-18 15:11:55 | [diff] [blame] | 112 | BackoffEntrySerializer::SerializeToValue(*entry, translator.parse_time()); |
Daniel McArdle | bbfc145 | 2020-05-18 15:11:55 | [diff] [blame] | 113 | |
| 114 | // Due to fuzzy interpretation in BackoffEntrySerializer:: |
| 115 | // DeserializeFromValue, we cannot assert that |*reserialized == *value|. |
Daniel McArdle | a8799f0 | 2020-08-04 15:44:16 | [diff] [blame] | 116 | // Rather, we can deserialize |reserialized| and check that some weaker |
| 117 | // properties are preserved. |
Daniel McArdle | bbfc145 | 2020-05-18 15:11:55 | [diff] [blame] | 118 | std::unique_ptr<BackoffEntry> entry_reparsed = |
| 119 | BackoffEntrySerializer::DeserializeFromValue( |
Maks Orlovich | 691496c | 2021-05-18 15:03:21 | [diff] [blame^] | 120 | reserialized, &policy, &clock, translator.parse_time()); |
Daniel McArdle | bbfc145 | 2020-05-18 15:11:55 | [diff] [blame] | 121 | CHECK(entry_reparsed); |
Daniel McArdle | a8799f0 | 2020-08-04 15:44:16 | [diff] [blame] | 122 | CHECK_EQ(entry_reparsed->failure_count(), entry->failure_count()); |
| 123 | CHECK_LE(entry_reparsed->GetReleaseTime(), entry->GetReleaseTime()); |
Daniel McArdle | 72692a1 | 2020-05-11 16:47:16 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | // Tests the "serialize-deserialize" property. Serializes an arbitrary |
| 127 | // BackoffEntry to JSON, deserializes to another BackoffEntry, and checks |
| 128 | // equality of the two entries. Our notion of equality is *very weak* and needs |
| 129 | // improvement. |
| 130 | void TestSerialize(const ProtoTranslator& translator) { |
| 131 | BackoffEntry::Policy policy = translator.policy(); |
| 132 | |
| 133 | // Serialize the BackoffEntry. |
| 134 | BackoffEntry native_entry(&policy); |
Maks Orlovich | 691496c | 2021-05-18 15:03:21 | [diff] [blame^] | 135 | base::Value serialized = BackoffEntrySerializer::SerializeToValue( |
| 136 | native_entry, translator.serialize_time()); |
| 137 | CHECK(serialized.is_list()); |
Daniel McArdle | 72692a1 | 2020-05-11 16:47:16 | [diff] [blame] | 138 | |
Daniel McArdle | bbfc145 | 2020-05-18 15:11:55 | [diff] [blame] | 139 | MockClock clock; |
| 140 | clock.SetNow(translator.now_ticks()); |
| 141 | |
Daniel McArdle | 72692a1 | 2020-05-11 16:47:16 | [diff] [blame] | 142 | // Deserialize it. |
| 143 | std::unique_ptr<BackoffEntry> deserialized_entry = |
Maks Orlovich | 691496c | 2021-05-18 15:03:21 | [diff] [blame^] | 144 | BackoffEntrySerializer::DeserializeFromValue(serialized, &policy, &clock, |
Daniel McArdle | bbfc145 | 2020-05-18 15:11:55 | [diff] [blame] | 145 | translator.parse_time()); |
Daniel McArdle | 72692a1 | 2020-05-11 16:47:16 | [diff] [blame] | 146 | // Even though SerializeToValue was successful, we're not guaranteed to have a |
| 147 | // |deserialized_entry|. One reason deserialization may fail is if the parsed |
| 148 | // |absolute_release_time_us| is below zero. |
| 149 | if (!deserialized_entry) |
| 150 | return; |
| 151 | |
| 152 | // TODO(dmcardle) Develop a stronger equality check for BackoffEntry. |
| 153 | |
| 154 | // Note that while |BackoffEntry::GetReleaseTime| looks like an accessor, it |
| 155 | // returns a |value that is computed based on a random double, so it's not |
| 156 | // suitable for CHECK_EQ here. See |BackoffEntry::CalculateReleaseTime|. |
| 157 | |
| 158 | CHECK_EQ(native_entry.failure_count(), deserialized_entry->failure_count()); |
| 159 | } |
| 160 | } // namespace |
| 161 | |
| 162 | DEFINE_PROTO_FUZZER(const fuzz_proto::FuzzerInput& input) { |
| 163 | static Environment env; |
| 164 | |
| 165 | // Print the entire |input| protobuf if asked. |
| 166 | if (getenv("LPM_DUMP_NATIVE_INPUT")) { |
| 167 | std::cout << "input: " << input.DebugString(); |
| 168 | } |
| 169 | |
| 170 | ProtoTranslator translator(input); |
Dan McArdle | 22c2f64 | 2021-03-18 23:40:10 | [diff] [blame] | 171 | // Skip this input if any of the time values are infinite. |
| 172 | if (translator.now_ticks().is_inf() || translator.parse_time().is_inf() || |
| 173 | translator.serialize_time().is_inf()) { |
| 174 | return; |
| 175 | } |
Daniel McArdle | 72692a1 | 2020-05-11 16:47:16 | [diff] [blame] | 176 | TestDeserialize(translator); |
Daniel McArdle | bbfc145 | 2020-05-18 15:11:55 | [diff] [blame] | 177 | TestSerialize(translator); |
Daniel McArdle | 72692a1 | 2020-05-11 16:47:16 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | } // namespace net |