/proc/self/cwd/pw_protobuf/decoder_fuzzer.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2022 The Pigweed Authors |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
4 | | // use this file except in compliance with the License. You may obtain a copy of |
5 | | // the License at |
6 | | // |
7 | | // https://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, WITHOUT |
11 | | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
12 | | // License for the specific language governing permissions and limitations under |
13 | | // the License. |
14 | | |
15 | | #include <algorithm> |
16 | | #include <cstddef> |
17 | | #include <cstdint> |
18 | | #include <cstring> |
19 | | #include <vector> |
20 | | |
21 | | #include "fuzz.h" |
22 | | #include "pw_fuzzer/fuzzed_data_provider.h" |
23 | | #include "pw_protobuf/stream_decoder.h" |
24 | | #include "pw_span/span.h" |
25 | | #include "pw_status/status.h" |
26 | | #include "pw_status/status_with_size.h" |
27 | | #include "pw_stream/memory_stream.h" |
28 | | #include "pw_stream/stream.h" |
29 | | |
30 | | namespace pw::protobuf::fuzz { |
31 | | namespace { |
32 | | |
33 | | void RecursiveFuzzedDecode(FuzzedDataProvider& provider, |
34 | | StreamDecoder& decoder, |
35 | 5.57k | uint32_t depth = 0) { |
36 | 5.57k | constexpr size_t kMaxRepeatedRead = 256; |
37 | 5.57k | constexpr size_t kMaxDepth = 3; |
38 | | |
39 | 5.57k | if (depth > kMaxDepth) { |
40 | 795 | return; |
41 | 795 | } |
42 | 16.3k | while (provider.remaining_bytes() != 0 && decoder.Next().ok()) { |
43 | 13.3k | FieldType field_type = provider.ConsumeEnum<FieldType>(); |
44 | 13.3k | switch (field_type) { |
45 | 429 | case kUint32: |
46 | 429 | if (!decoder.ReadUint32().status().ok()) { |
47 | 146 | return; |
48 | 146 | } |
49 | 283 | break; |
50 | 414 | case kPackedUint32: { |
51 | 414 | uint32_t packed[kMaxRepeatedRead] = {0}; |
52 | 414 | if (!decoder.ReadPackedUint32(packed).status().ok()) { |
53 | 125 | return; |
54 | 125 | } |
55 | 414 | } break; |
56 | 289 | case kUint64: |
57 | 225 | if (!decoder.ReadUint64().status().ok()) { |
58 | 22 | return; |
59 | 22 | } |
60 | 203 | break; |
61 | 535 | case kPackedUint64: { |
62 | 535 | uint64_t packed[kMaxRepeatedRead] = {0}; |
63 | 535 | if (!decoder.ReadPackedUint64(packed).status().ok()) { |
64 | 79 | return; |
65 | 79 | } |
66 | 535 | } break; |
67 | 470 | case kInt32: |
68 | 470 | if (!decoder.ReadInt32().status().ok()) { |
69 | 212 | return; |
70 | 212 | } |
71 | 258 | break; |
72 | 319 | case kPackedInt32: { |
73 | 319 | int32_t packed[kMaxRepeatedRead] = {0}; |
74 | 319 | if (!decoder.ReadPackedInt32(packed).status().ok()) { |
75 | 46 | return; |
76 | 46 | } |
77 | 319 | } break; |
78 | 273 | case kInt64: |
79 | 225 | if (!decoder.ReadInt64().status().ok()) { |
80 | 28 | return; |
81 | 28 | } |
82 | 197 | break; |
83 | 344 | case kPackedInt64: { |
84 | 344 | int64_t packed[kMaxRepeatedRead] = {0}; |
85 | 344 | if (!decoder.ReadPackedInt64(packed).status().ok()) { |
86 | 55 | return; |
87 | 55 | } |
88 | 344 | } break; |
89 | 442 | case kSint32: |
90 | 442 | if (!decoder.ReadSint32().status().ok()) { |
91 | 144 | return; |
92 | 144 | } |
93 | 298 | break; |
94 | 509 | case kPackedSint32: { |
95 | 509 | int32_t packed[kMaxRepeatedRead] = {0}; |
96 | 509 | if (!decoder.ReadPackedSint32(packed).status().ok()) { |
97 | 132 | return; |
98 | 132 | } |
99 | 509 | } break; |
100 | 377 | case kSint64: |
101 | 293 | if (!decoder.ReadSint64().status().ok()) { |
102 | 77 | return; |
103 | 77 | } |
104 | 216 | break; |
105 | 335 | case kPackedSint64: { |
106 | 335 | int64_t packed[kMaxRepeatedRead] = {0}; |
107 | 335 | if (!decoder.ReadPackedSint64(packed).status().ok()) { |
108 | 46 | return; |
109 | 46 | } |
110 | 335 | } break; |
111 | 365 | case kBool: |
112 | 365 | if (!decoder.ReadBool().status().ok()) { |
113 | 169 | return; |
114 | 169 | } |
115 | 196 | break; |
116 | 312 | case kFixed32: |
117 | 312 | if (!decoder.ReadFixed32().status().ok()) { |
118 | 9 | return; |
119 | 9 | } |
120 | 303 | break; |
121 | 303 | case kPackedFixed32: { |
122 | 300 | uint32_t packed[kMaxRepeatedRead] = {0}; |
123 | 300 | if (!decoder.ReadPackedFixed32(packed).status().ok()) { |
124 | 39 | return; |
125 | 39 | } |
126 | 300 | } break; |
127 | 261 | case kFixed64: |
128 | 210 | if (!decoder.ReadFixed64().status().ok()) { |
129 | 10 | return; |
130 | 10 | } |
131 | 200 | break; |
132 | 639 | case kPackedFixed64: { |
133 | 639 | uint64_t packed[kMaxRepeatedRead] = {0}; |
134 | 639 | if (!decoder.ReadPackedFixed64(packed).status().ok()) { |
135 | 44 | return; |
136 | 44 | } |
137 | 639 | } break; |
138 | 595 | case kSfixed32: |
139 | 404 | if (!decoder.ReadSfixed32().status().ok()) { |
140 | 9 | return; |
141 | 9 | } |
142 | 395 | break; |
143 | 395 | case kPackedSfixed32: { |
144 | 320 | int32_t packed[kMaxRepeatedRead] = {0}; |
145 | 320 | if (!decoder.ReadPackedSfixed32(packed).status().ok()) { |
146 | 41 | return; |
147 | 41 | } |
148 | 320 | } break; |
149 | 279 | case kSfixed64: |
150 | 210 | if (!decoder.ReadSfixed64().status().ok()) { |
151 | 11 | return; |
152 | 11 | } |
153 | 199 | break; |
154 | 326 | case kPackedSfixed64: { |
155 | 326 | int64_t packed[kMaxRepeatedRead] = {0}; |
156 | 326 | if (!decoder.ReadPackedSfixed64(packed).status().ok()) { |
157 | 41 | return; |
158 | 41 | } |
159 | 326 | } break; |
160 | 301 | case kFloat: |
161 | 301 | if (!decoder.ReadFloat().status().ok()) { |
162 | 8 | return; |
163 | 8 | } |
164 | 293 | break; |
165 | 371 | case kPackedFloat: { |
166 | 371 | float packed[kMaxRepeatedRead] = {0}; |
167 | 371 | if (!decoder.ReadPackedFloat(packed).status().ok()) { |
168 | 47 | return; |
169 | 47 | } |
170 | 371 | } break; |
171 | 324 | case kDouble: |
172 | 211 | if (!decoder.ReadDouble().status().ok()) { |
173 | 7 | return; |
174 | 7 | } |
175 | 204 | break; |
176 | 350 | case kPackedDouble: { |
177 | 350 | double packed[kMaxRepeatedRead] = {0}; |
178 | 350 | if (!decoder.ReadPackedDouble(packed).status().ok()) { |
179 | 61 | return; |
180 | 61 | } |
181 | 350 | } break; |
182 | 772 | case kBytes: { |
183 | 772 | std::byte bytes[kMaxRepeatedRead] = {std::byte{0}}; |
184 | 772 | if (!decoder.ReadBytes(bytes).status().ok()) { |
185 | 69 | return; |
186 | 69 | } |
187 | 772 | } break; |
188 | 703 | case kString: { |
189 | 420 | char str[kMaxRepeatedRead] = {0}; |
190 | 420 | if (!decoder.ReadString(str).status().ok()) { |
191 | 66 | return; |
192 | 66 | } |
193 | 420 | } break; |
194 | 3.25k | case kPush: { |
195 | 3.25k | StreamDecoder nested_decoder = decoder.GetNestedDecoder(); |
196 | 3.25k | RecursiveFuzzedDecode(provider, nested_decoder, depth + 1); |
197 | 3.25k | } break; |
198 | 0 | case kPop: |
199 | 0 | if (depth > 0) { |
200 | | // Special "field". The marks the end of a nested message. |
201 | 0 | return; |
202 | 0 | } |
203 | 13.3k | } |
204 | 13.3k | } |
205 | 4.77k | } |
206 | | |
207 | 2.32k | void TestOneInput(FuzzedDataProvider& provider) { |
208 | 2.32k | constexpr size_t kMaxFuzzedProtoSize = 4096; |
209 | 2.32k | std::vector<std::byte> proto_message_data = provider.ConsumeBytes<std::byte>( |
210 | 2.32k | provider.ConsumeIntegralInRange<size_t>(0, kMaxFuzzedProtoSize)); |
211 | 2.32k | stream::MemoryReader memory_reader(proto_message_data); |
212 | 2.32k | StreamDecoder decoder(memory_reader); |
213 | 2.32k | RecursiveFuzzedDecode(provider, decoder); |
214 | 2.32k | } |
215 | | |
216 | | } // namespace |
217 | | } // namespace pw::protobuf::fuzz |
218 | | |
219 | 17.1k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
220 | 17.1k | FuzzedDataProvider provider(data, size); |
221 | 17.1k | pw::protobuf::fuzz::TestOneInput(provider); |
222 | 17.1k | return 0; |
223 | 17.1k | } |