/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.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 | 803 | return; |
41 | 803 | } |
42 | 16.0k | while (provider.remaining_bytes() != 0 && decoder.Next().ok()) { |
43 | 12.8k | FieldType field_type = provider.ConsumeEnum<FieldType>(); |
44 | 12.8k | switch (field_type) { |
45 | 423 | case kUint32: |
46 | 423 | if (!decoder.ReadUint32().status().ok()) { |
47 | 153 | return; |
48 | 153 | } |
49 | 270 | break; |
50 | 418 | case kPackedUint32: { |
51 | 418 | uint32_t packed[kMaxRepeatedRead] = {0}; |
52 | 418 | if (!decoder.ReadPackedUint32(packed).status().ok()) { |
53 | 116 | return; |
54 | 116 | } |
55 | 418 | } break; |
56 | 302 | case kUint64: |
57 | 221 | if (!decoder.ReadUint64().status().ok()) { |
58 | 23 | return; |
59 | 23 | } |
60 | 198 | break; |
61 | 376 | case kPackedUint64: { |
62 | 376 | uint64_t packed[kMaxRepeatedRead] = {0}; |
63 | 376 | if (!decoder.ReadPackedUint64(packed).status().ok()) { |
64 | 71 | return; |
65 | 71 | } |
66 | 376 | } break; |
67 | 474 | case kInt32: |
68 | 474 | if (!decoder.ReadInt32().status().ok()) { |
69 | 211 | return; |
70 | 211 | } |
71 | 263 | break; |
72 | 346 | case kPackedInt32: { |
73 | 346 | int32_t packed[kMaxRepeatedRead] = {0}; |
74 | 346 | if (!decoder.ReadPackedInt32(packed).status().ok()) { |
75 | 56 | return; |
76 | 56 | } |
77 | 346 | } break; |
78 | 290 | case kInt64: |
79 | 220 | if (!decoder.ReadInt64().status().ok()) { |
80 | 25 | return; |
81 | 25 | } |
82 | 195 | break; |
83 | 340 | case kPackedInt64: { |
84 | 340 | int64_t packed[kMaxRepeatedRead] = {0}; |
85 | 340 | if (!decoder.ReadPackedInt64(packed).status().ok()) { |
86 | 56 | return; |
87 | 56 | } |
88 | 340 | } break; |
89 | 425 | case kSint32: |
90 | 425 | if (!decoder.ReadSint32().status().ok()) { |
91 | 139 | return; |
92 | 139 | } |
93 | 286 | break; |
94 | 497 | case kPackedSint32: { |
95 | 497 | int32_t packed[kMaxRepeatedRead] = {0}; |
96 | 497 | if (!decoder.ReadPackedSint32(packed).status().ok()) { |
97 | 109 | return; |
98 | 109 | } |
99 | 497 | } break; |
100 | 388 | case kSint64: |
101 | 269 | if (!decoder.ReadSint64().status().ok()) { |
102 | 73 | return; |
103 | 73 | } |
104 | 196 | break; |
105 | 325 | case kPackedSint64: { |
106 | 325 | int64_t packed[kMaxRepeatedRead] = {0}; |
107 | 325 | if (!decoder.ReadPackedSint64(packed).status().ok()) { |
108 | 37 | return; |
109 | 37 | } |
110 | 325 | } break; |
111 | 364 | case kBool: |
112 | 364 | if (!decoder.ReadBool().status().ok()) { |
113 | 166 | return; |
114 | 166 | } |
115 | 198 | break; |
116 | 433 | case kFixed32: |
117 | 433 | if (!decoder.ReadFixed32().status().ok()) { |
118 | 5 | return; |
119 | 5 | } |
120 | 428 | break; |
121 | 428 | case kPackedFixed32: { |
122 | 295 | uint32_t packed[kMaxRepeatedRead] = {0}; |
123 | 295 | if (!decoder.ReadPackedFixed32(packed).status().ok()) { |
124 | 38 | return; |
125 | 38 | } |
126 | 295 | } break; |
127 | 257 | case kFixed64: |
128 | 210 | if (!decoder.ReadFixed64().status().ok()) { |
129 | 9 | return; |
130 | 9 | } |
131 | 201 | break; |
132 | 582 | case kPackedFixed64: { |
133 | 582 | uint64_t packed[kMaxRepeatedRead] = {0}; |
134 | 582 | if (!decoder.ReadPackedFixed64(packed).status().ok()) { |
135 | 40 | return; |
136 | 40 | } |
137 | 582 | } break; |
138 | 542 | case kSfixed32: |
139 | 217 | if (!decoder.ReadSfixed32().status().ok()) { |
140 | 5 | return; |
141 | 5 | } |
142 | 212 | break; |
143 | 322 | case kPackedSfixed32: { |
144 | 322 | int32_t packed[kMaxRepeatedRead] = {0}; |
145 | 322 | if (!decoder.ReadPackedSfixed32(packed).status().ok()) { |
146 | 40 | return; |
147 | 40 | } |
148 | 322 | } break; |
149 | 282 | case kSfixed64: |
150 | 204 | if (!decoder.ReadSfixed64().status().ok()) { |
151 | 9 | return; |
152 | 9 | } |
153 | 195 | break; |
154 | 309 | case kPackedSfixed64: { |
155 | 309 | int64_t packed[kMaxRepeatedRead] = {0}; |
156 | 309 | if (!decoder.ReadPackedSfixed64(packed).status().ok()) { |
157 | 41 | return; |
158 | 41 | } |
159 | 309 | } break; |
160 | 367 | case kFloat: |
161 | 367 | if (!decoder.ReadFloat().status().ok()) { |
162 | 13 | return; |
163 | 13 | } |
164 | 354 | break; |
165 | 354 | case kPackedFloat: { |
166 | 323 | float packed[kMaxRepeatedRead] = {0}; |
167 | 323 | if (!decoder.ReadPackedFloat(packed).status().ok()) { |
168 | 45 | return; |
169 | 45 | } |
170 | 323 | } break; |
171 | 278 | case kDouble: |
172 | 211 | if (!decoder.ReadDouble().status().ok()) { |
173 | 6 | return; |
174 | 6 | } |
175 | 205 | break; |
176 | 340 | case kPackedDouble: { |
177 | 340 | double packed[kMaxRepeatedRead] = {0}; |
178 | 340 | if (!decoder.ReadPackedDouble(packed).status().ok()) { |
179 | 61 | return; |
180 | 61 | } |
181 | 340 | } break; |
182 | 594 | case kBytes: { |
183 | 594 | std::byte bytes[kMaxRepeatedRead] = {std::byte{0}}; |
184 | 594 | if (!decoder.ReadBytes(bytes).status().ok()) { |
185 | 55 | return; |
186 | 55 | } |
187 | 594 | } break; |
188 | 539 | case kString: { |
189 | 428 | char str[kMaxRepeatedRead] = {0}; |
190 | 428 | if (!decoder.ReadString(str).status().ok()) { |
191 | 61 | return; |
192 | 61 | } |
193 | 428 | } break; |
194 | 3.31k | case kPush: { |
195 | 3.31k | StreamDecoder nested_decoder = decoder.GetNestedDecoder(); |
196 | 3.31k | RecursiveFuzzedDecode(provider, nested_decoder, depth + 1); |
197 | 3.31k | } 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.8k | } |
204 | 12.8k | } |
205 | 4.85k | } |
206 | | |
207 | 2.34k | void TestOneInput(FuzzedDataProvider& provider) { |
208 | 2.34k | constexpr size_t kMaxFuzzedProtoSize = 4096; |
209 | 2.34k | std::vector<std::byte> proto_message_data = provider.ConsumeBytes<std::byte>( |
210 | 2.34k | provider.ConsumeIntegralInRange<size_t>(0, kMaxFuzzedProtoSize)); |
211 | 2.34k | stream::MemoryReader memory_reader(proto_message_data); |
212 | 2.34k | StreamDecoder decoder(memory_reader); |
213 | 2.34k | RecursiveFuzzedDecode(provider, decoder); |
214 | 2.34k | } |
215 | | |
216 | | } // namespace |
217 | | } // namespace pw::protobuf::fuzz |
218 | | |
219 | 17.4k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
220 | 17.4k | FuzzedDataProvider provider(data, size); |
221 | 17.4k | pw::protobuf::fuzz::TestOneInput(provider); |
222 | 17.4k | return 0; |
223 | 17.4k | } |