/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.65k | uint32_t depth = 0) { |
36 | 5.65k | constexpr size_t kMaxRepeatedRead = 256; |
37 | 5.65k | constexpr size_t kMaxDepth = 3; |
38 | | |
39 | 5.65k | if (depth > kMaxDepth) { |
40 | 789 | return; |
41 | 789 | } |
42 | 16.3k | while (provider.remaining_bytes() != 0 && decoder.Next().ok()) { |
43 | 13.2k | FieldType field_type = provider.ConsumeEnum<FieldType>(); |
44 | 13.2k | switch (field_type) { |
45 | 450 | case kUint32: |
46 | 450 | if (!decoder.ReadUint32().status().ok()) { |
47 | 167 | return; |
48 | 167 | } |
49 | 283 | break; |
50 | 420 | case kPackedUint32: { |
51 | 420 | uint32_t packed[kMaxRepeatedRead] = {0}; |
52 | 420 | if (!decoder.ReadPackedUint32(packed).status().ok()) { |
53 | 132 | return; |
54 | 132 | } |
55 | 420 | } break; |
56 | 288 | case kUint64: |
57 | 236 | if (!decoder.ReadUint64().status().ok()) { |
58 | 31 | return; |
59 | 31 | } |
60 | 205 | break; |
61 | 546 | case kPackedUint64: { |
62 | 546 | uint64_t packed[kMaxRepeatedRead] = {0}; |
63 | 546 | if (!decoder.ReadPackedUint64(packed).status().ok()) { |
64 | 82 | return; |
65 | 82 | } |
66 | 546 | } break; |
67 | 499 | case kInt32: |
68 | 499 | if (!decoder.ReadInt32().status().ok()) { |
69 | 217 | return; |
70 | 217 | } |
71 | 282 | break; |
72 | 330 | case kPackedInt32: { |
73 | 330 | int32_t packed[kMaxRepeatedRead] = {0}; |
74 | 330 | if (!decoder.ReadPackedInt32(packed).status().ok()) { |
75 | 54 | return; |
76 | 54 | } |
77 | 330 | } break; |
78 | 276 | case kInt64: |
79 | 218 | if (!decoder.ReadInt64().status().ok()) { |
80 | 23 | return; |
81 | 23 | } |
82 | 195 | break; |
83 | 353 | case kPackedInt64: { |
84 | 353 | int64_t packed[kMaxRepeatedRead] = {0}; |
85 | 353 | if (!decoder.ReadPackedInt64(packed).status().ok()) { |
86 | 55 | return; |
87 | 55 | } |
88 | 353 | } break; |
89 | 444 | case kSint32: |
90 | 444 | if (!decoder.ReadSint32().status().ok()) { |
91 | 140 | return; |
92 | 140 | } |
93 | 304 | break; |
94 | 510 | case kPackedSint32: { |
95 | 510 | int32_t packed[kMaxRepeatedRead] = {0}; |
96 | 510 | if (!decoder.ReadPackedSint32(packed).status().ok()) { |
97 | 127 | return; |
98 | 127 | } |
99 | 510 | } break; |
100 | 383 | case kSint64: |
101 | 274 | if (!decoder.ReadSint64().status().ok()) { |
102 | 76 | return; |
103 | 76 | } |
104 | 198 | break; |
105 | 330 | case kPackedSint64: { |
106 | 330 | int64_t packed[kMaxRepeatedRead] = {0}; |
107 | 330 | if (!decoder.ReadPackedSint64(packed).status().ok()) { |
108 | 44 | return; |
109 | 44 | } |
110 | 330 | } break; |
111 | 386 | case kBool: |
112 | 386 | if (!decoder.ReadBool().status().ok()) { |
113 | 180 | return; |
114 | 180 | } |
115 | 206 | break; |
116 | 336 | case kFixed32: |
117 | 336 | if (!decoder.ReadFixed32().status().ok()) { |
118 | 10 | return; |
119 | 10 | } |
120 | 326 | break; |
121 | 326 | case kPackedFixed32: { |
122 | 293 | uint32_t packed[kMaxRepeatedRead] = {0}; |
123 | 293 | if (!decoder.ReadPackedFixed32(packed).status().ok()) { |
124 | 41 | return; |
125 | 41 | } |
126 | 293 | } break; |
127 | 252 | case kFixed64: |
128 | 206 | if (!decoder.ReadFixed64().status().ok()) { |
129 | 9 | return; |
130 | 9 | } |
131 | 197 | break; |
132 | 638 | case kPackedFixed64: { |
133 | 638 | uint64_t packed[kMaxRepeatedRead] = {0}; |
134 | 638 | if (!decoder.ReadPackedFixed64(packed).status().ok()) { |
135 | 44 | return; |
136 | 44 | } |
137 | 638 | } break; |
138 | 594 | case kSfixed32: |
139 | 365 | if (!decoder.ReadSfixed32().status().ok()) { |
140 | 10 | return; |
141 | 10 | } |
142 | 355 | break; |
143 | 355 | case kPackedSfixed32: { |
144 | 318 | int32_t packed[kMaxRepeatedRead] = {0}; |
145 | 318 | if (!decoder.ReadPackedSfixed32(packed).status().ok()) { |
146 | 33 | return; |
147 | 33 | } |
148 | 318 | } break; |
149 | 285 | case kSfixed64: |
150 | 207 | if (!decoder.ReadSfixed64().status().ok()) { |
151 | 6 | return; |
152 | 6 | } |
153 | 201 | break; |
154 | 337 | case kPackedSfixed64: { |
155 | 337 | int64_t packed[kMaxRepeatedRead] = {0}; |
156 | 337 | if (!decoder.ReadPackedSfixed64(packed).status().ok()) { |
157 | 46 | return; |
158 | 46 | } |
159 | 337 | } break; |
160 | 320 | case kFloat: |
161 | 320 | if (!decoder.ReadFloat().status().ok()) { |
162 | 10 | return; |
163 | 10 | } |
164 | 310 | break; |
165 | 333 | case kPackedFloat: { |
166 | 333 | float packed[kMaxRepeatedRead] = {0}; |
167 | 333 | if (!decoder.ReadPackedFloat(packed).status().ok()) { |
168 | 45 | return; |
169 | 45 | } |
170 | 333 | } break; |
171 | 288 | case kDouble: |
172 | 211 | if (!decoder.ReadDouble().status().ok()) { |
173 | 5 | return; |
174 | 5 | } |
175 | 206 | break; |
176 | 373 | case kPackedDouble: { |
177 | 373 | double packed[kMaxRepeatedRead] = {0}; |
178 | 373 | if (!decoder.ReadPackedDouble(packed).status().ok()) { |
179 | 61 | return; |
180 | 61 | } |
181 | 373 | } break; |
182 | 635 | case kBytes: { |
183 | 635 | std::byte bytes[kMaxRepeatedRead] = {std::byte{0}}; |
184 | 635 | if (!decoder.ReadBytes(bytes).status().ok()) { |
185 | 78 | return; |
186 | 78 | } |
187 | 635 | } break; |
188 | 557 | case kString: { |
189 | 395 | char str[kMaxRepeatedRead] = {0}; |
190 | 395 | if (!decoder.ReadString(str).status().ok()) { |
191 | 60 | return; |
192 | 60 | } |
193 | 395 | } break; |
194 | 3.27k | case kPush: { |
195 | 3.27k | StreamDecoder nested_decoder = decoder.GetNestedDecoder(); |
196 | 3.27k | RecursiveFuzzedDecode(provider, nested_decoder, depth + 1); |
197 | 3.27k | } 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.2k | } |
204 | 13.2k | } |
205 | 4.86k | } |
206 | | |
207 | 2.38k | void TestOneInput(FuzzedDataProvider& provider) { |
208 | 2.38k | constexpr size_t kMaxFuzzedProtoSize = 4096; |
209 | 2.38k | std::vector<std::byte> proto_message_data = provider.ConsumeBytes<std::byte>( |
210 | 2.38k | provider.ConsumeIntegralInRange<size_t>(0, kMaxFuzzedProtoSize)); |
211 | 2.38k | stream::MemoryReader memory_reader(proto_message_data); |
212 | 2.38k | StreamDecoder decoder(memory_reader); |
213 | 2.38k | RecursiveFuzzedDecode(provider, decoder); |
214 | 2.38k | } |
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 | } |