/proc/self/cwd/pw_protobuf/decoder_fuzzer.cc
Line | Count | Source |
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.58k | uint32_t depth = 0) { |
36 | 5.58k | constexpr size_t kMaxRepeatedRead = 256; |
37 | 5.58k | constexpr size_t kMaxDepth = 3; |
38 | | |
39 | 5.58k | if (depth > kMaxDepth) { |
40 | 781 | return; |
41 | 781 | } |
42 | 15.9k | while (provider.remaining_bytes() != 0 && decoder.Next().ok()) { |
43 | 12.7k | FieldType field_type = provider.ConsumeEnum<FieldType>(); |
44 | 12.7k | switch (field_type) { |
45 | 419 | case kUint32: |
46 | 419 | if (!decoder.ReadUint32().status().ok()) { |
47 | 148 | return; |
48 | 148 | } |
49 | 271 | break; |
50 | 409 | case kPackedUint32: { |
51 | 409 | uint32_t packed[kMaxRepeatedRead] = {0}; |
52 | 409 | if (!decoder.ReadPackedUint32(packed).status().ok()) { |
53 | 113 | return; |
54 | 113 | } |
55 | 409 | } break; |
56 | 296 | case kUint64: |
57 | 228 | if (!decoder.ReadUint64().status().ok()) { |
58 | 29 | return; |
59 | 29 | } |
60 | 199 | break; |
61 | 370 | case kPackedUint64: { |
62 | 370 | uint64_t packed[kMaxRepeatedRead] = {0}; |
63 | 370 | if (!decoder.ReadPackedUint64(packed).status().ok()) { |
64 | 68 | return; |
65 | 68 | } |
66 | 370 | } break; |
67 | 477 | case kInt32: |
68 | 477 | if (!decoder.ReadInt32().status().ok()) { |
69 | 211 | return; |
70 | 211 | } |
71 | 266 | break; |
72 | 324 | case kPackedInt32: { |
73 | 324 | int32_t packed[kMaxRepeatedRead] = {0}; |
74 | 324 | if (!decoder.ReadPackedInt32(packed).status().ok()) { |
75 | 42 | return; |
76 | 42 | } |
77 | 324 | } break; |
78 | 282 | case kInt64: |
79 | 221 | if (!decoder.ReadInt64().status().ok()) { |
80 | 26 | return; |
81 | 26 | } |
82 | 195 | break; |
83 | 335 | case kPackedInt64: { |
84 | 335 | int64_t packed[kMaxRepeatedRead] = {0}; |
85 | 335 | if (!decoder.ReadPackedInt64(packed).status().ok()) { |
86 | 54 | return; |
87 | 54 | } |
88 | 335 | } break; |
89 | 419 | case kSint32: |
90 | 419 | if (!decoder.ReadSint32().status().ok()) { |
91 | 138 | return; |
92 | 138 | } |
93 | 281 | break; |
94 | 505 | case kPackedSint32: { |
95 | 505 | int32_t packed[kMaxRepeatedRead] = {0}; |
96 | 505 | if (!decoder.ReadPackedSint32(packed).status().ok()) { |
97 | 107 | return; |
98 | 107 | } |
99 | 505 | } break; |
100 | 398 | case kSint64: |
101 | 271 | if (!decoder.ReadSint64().status().ok()) { |
102 | 73 | return; |
103 | 73 | } |
104 | 198 | break; |
105 | 321 | case kPackedSint64: { |
106 | 321 | int64_t packed[kMaxRepeatedRead] = {0}; |
107 | 321 | if (!decoder.ReadPackedSint64(packed).status().ok()) { |
108 | 38 | return; |
109 | 38 | } |
110 | 321 | } break; |
111 | 365 | case kBool: |
112 | 365 | if (!decoder.ReadBool().status().ok()) { |
113 | 169 | return; |
114 | 169 | } |
115 | 196 | break; |
116 | 455 | case kFixed32: |
117 | 455 | if (!decoder.ReadFixed32().status().ok()) { |
118 | 11 | return; |
119 | 11 | } |
120 | 444 | break; |
121 | 444 | case kPackedFixed32: { |
122 | 297 | uint32_t packed[kMaxRepeatedRead] = {0}; |
123 | 297 | if (!decoder.ReadPackedFixed32(packed).status().ok()) { |
124 | 38 | return; |
125 | 38 | } |
126 | 297 | } break; |
127 | 259 | case kFixed64: |
128 | 207 | if (!decoder.ReadFixed64().status().ok()) { |
129 | 8 | return; |
130 | 8 | } |
131 | 199 | break; |
132 | 578 | case kPackedFixed64: { |
133 | 578 | uint64_t packed[kMaxRepeatedRead] = {0}; |
134 | 578 | if (!decoder.ReadPackedFixed64(packed).status().ok()) { |
135 | 42 | return; |
136 | 42 | } |
137 | 578 | } break; |
138 | 536 | case kSfixed32: |
139 | 223 | if (!decoder.ReadSfixed32().status().ok()) { |
140 | 9 | return; |
141 | 9 | } |
142 | 214 | break; |
143 | 328 | case kPackedSfixed32: { |
144 | 328 | int32_t packed[kMaxRepeatedRead] = {0}; |
145 | 328 | if (!decoder.ReadPackedSfixed32(packed).status().ok()) { |
146 | 42 | return; |
147 | 42 | } |
148 | 328 | } break; |
149 | 286 | case kSfixed64: |
150 | 203 | if (!decoder.ReadSfixed64().status().ok()) { |
151 | 7 | return; |
152 | 7 | } |
153 | 196 | break; |
154 | 303 | case kPackedSfixed64: { |
155 | 303 | int64_t packed[kMaxRepeatedRead] = {0}; |
156 | 303 | if (!decoder.ReadPackedSfixed64(packed).status().ok()) { |
157 | 45 | return; |
158 | 45 | } |
159 | 303 | } break; |
160 | 350 | case kFloat: |
161 | 350 | if (!decoder.ReadFloat().status().ok()) { |
162 | 11 | return; |
163 | 11 | } |
164 | 339 | break; |
165 | 339 | case kPackedFloat: { |
166 | 317 | float packed[kMaxRepeatedRead] = {0}; |
167 | 317 | if (!decoder.ReadPackedFloat(packed).status().ok()) { |
168 | 35 | return; |
169 | 35 | } |
170 | 317 | } break; |
171 | 282 | case kDouble: |
172 | 210 | if (!decoder.ReadDouble().status().ok()) { |
173 | 3 | return; |
174 | 3 | } |
175 | 207 | break; |
176 | 329 | case kPackedDouble: { |
177 | 329 | double packed[kMaxRepeatedRead] = {0}; |
178 | 329 | if (!decoder.ReadPackedDouble(packed).status().ok()) { |
179 | 53 | return; |
180 | 53 | } |
181 | 329 | } break; |
182 | 603 | case kBytes: { |
183 | 603 | std::byte bytes[kMaxRepeatedRead] = {std::byte{0}}; |
184 | 603 | if (!decoder.ReadBytes(bytes).status().ok()) { |
185 | 55 | return; |
186 | 55 | } |
187 | 603 | } break; |
188 | 548 | case kString: { |
189 | 408 | char str[kMaxRepeatedRead] = {0}; |
190 | 408 | if (!decoder.ReadString(str).status().ok()) { |
191 | 57 | return; |
192 | 57 | } |
193 | 408 | } 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 | 12.7k | } |
204 | 12.7k | } |
205 | 4.80k | } |
206 | | |
207 | 2.31k | void TestOneInput(FuzzedDataProvider& provider) { |
208 | 2.31k | constexpr size_t kMaxFuzzedProtoSize = 4096; |
209 | 2.31k | std::vector<std::byte> proto_message_data = provider.ConsumeBytes<std::byte>( |
210 | 2.31k | provider.ConsumeIntegralInRange<size_t>(0, kMaxFuzzedProtoSize)); |
211 | 2.31k | stream::MemoryReader memory_reader(proto_message_data); |
212 | 2.31k | StreamDecoder decoder(memory_reader); |
213 | 2.31k | RecursiveFuzzedDecode(provider, decoder); |
214 | 2.31k | } |
215 | | |
216 | | } // namespace |
217 | | } // namespace pw::protobuf::fuzz |
218 | | |
219 | 17.0k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
220 | 17.0k | FuzzedDataProvider provider(data, size); |
221 | 17.0k | pw::protobuf::fuzz::TestOneInput(provider); |
222 | 17.0k | return 0; |
223 | 17.0k | } |