Update doc revision and pdf cover.
[apps/low-level-can-service.git] / libs / nanopb / tests / fuzztest / fuzztest.c
1 /* Fuzz testing for the nanopb core.
2  * Attempts to verify all the properties defined in the security model document.
3  */
4
5 #include <pb_decode.h>
6 #include <pb_encode.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <assert.h>
11 #include <time.h>
12 #include <malloc_wrappers.h>
13 #include "alltypes_static.pb.h"
14 #include "alltypes_pointer.pb.h"
15
16 static uint64_t random_seed;
17
18 /* Uses xorshift64 here instead of rand() for both speed and
19  * reproducibility across platforms. */
20 static uint32_t rand_word()
21 {
22     random_seed ^= random_seed >> 12;
23     random_seed ^= random_seed << 25;
24     random_seed ^= random_seed >> 27;
25     return random_seed * 2685821657736338717ULL;
26 }
27
28 /* Get a random integer in range, with approximately flat distribution. */
29 static int rand_int(int min, int max)
30 {
31     return rand_word() % (max + 1 - min) + min;
32 }
33
34 static bool rand_bool()
35 {
36     return rand_word() & 1;
37 }
38
39 /* Get a random byte, with skewed distribution.
40  * Important corner cases like 0xFF, 0x00 and 0xFE occur more
41  * often than other values. */
42 static uint8_t rand_byte()
43 {
44     uint32_t w = rand_word();
45     uint8_t b = w & 0xFF;
46     if (w & 0x100000)
47         b >>= (w >> 8) & 7;
48     if (w & 0x200000)
49         b <<= (w >> 12) & 7;
50     if (w & 0x400000)
51         b ^= 0xFF;
52     return b;
53 }
54
55 /* Get a random length, with skewed distribution.
56  * Favors the shorter lengths, but always atleast 1. */
57 static size_t rand_len(size_t max)
58 {
59     uint32_t w = rand_word();
60     size_t s;
61     if (w & 0x800000)
62         w &= 3;
63     else if (w & 0x400000)
64         w &= 15;
65     else if (w & 0x200000)
66         w &= 255;
67
68     s = (w % max);
69     if (s == 0)
70         s = 1;
71     
72     return s;
73 }
74
75 /* Fills a buffer with random data with skewed distribution. */
76 static void rand_fill(uint8_t *buf, size_t count)
77 {
78     while (count--)
79         *buf++ = rand_byte();
80 }
81
82 /* Fill with random protobuf-like data */
83 static size_t rand_fill_protobuf(uint8_t *buf, size_t min_bytes, size_t max_bytes, int min_tag)
84 {
85     pb_ostream_t stream = pb_ostream_from_buffer(buf, max_bytes);
86
87     while(stream.bytes_written < min_bytes)
88     {
89         pb_wire_type_t wt = rand_int(0, 3);
90         if (wt == 3) wt = 5; /* Gap in values */
91         
92         if (!pb_encode_tag(&stream, wt, rand_int(min_tag, min_tag + 512)))
93             break;
94     
95         if (wt == PB_WT_VARINT)
96         {
97             uint64_t value;
98             rand_fill((uint8_t*)&value, sizeof(value));
99             pb_encode_varint(&stream, value);
100         }
101         else if (wt == PB_WT_64BIT)
102         {
103             uint64_t value;
104             rand_fill((uint8_t*)&value, sizeof(value));
105             pb_encode_fixed64(&stream, &value);
106         }
107         else if (wt == PB_WT_32BIT)
108         {
109             uint32_t value;
110             rand_fill((uint8_t*)&value, sizeof(value));
111             pb_encode_fixed32(&stream, &value);
112         }
113         else if (wt == PB_WT_STRING)
114         {
115             size_t len;
116             uint8_t *buf;
117             
118             if (min_bytes > stream.bytes_written)
119                 len = rand_len(min_bytes - stream.bytes_written);
120             else
121                 len = 0;
122             
123             buf = malloc(len);
124             pb_encode_varint(&stream, len);
125             rand_fill(buf, len);
126             pb_write(&stream, buf, len);
127             free(buf);
128         }
129     }
130     
131     return stream.bytes_written;
132 }
133
134 /* Given a buffer of data, mess it up a bit */
135 static void rand_mess(uint8_t *buf, size_t count)
136 {
137     int m = rand_int(0, 3);
138     
139     if (m == 0)
140     {
141         /* Replace random substring */
142         int s = rand_int(0, count - 1);
143         int l = rand_len(count - s);
144         rand_fill(buf + s, l);
145     }
146     else if (m == 1)
147     {
148         /* Swap random bytes */
149         int a = rand_int(0, count - 1);
150         int b = rand_int(0, count - 1);
151         int x = buf[a];
152         buf[a] = buf[b];
153         buf[b] = x;
154     }
155     else if (m == 2)
156     {
157         /* Duplicate substring */
158         int s = rand_int(0, count - 2);
159         int l = rand_len((count - s) / 2);
160         memcpy(buf + s + l, buf + s, l);
161     }
162     else if (m == 3)
163     {
164         /* Add random protobuf noise */
165         int s = rand_int(0, count - 1);
166         int l = rand_len(count - s);
167         rand_fill_protobuf(buf + s, l, count - s, 1);
168     }
169 }
170
171 /* Some default data to put in the message */
172 static const alltypes_static_AllTypes initval = alltypes_static_AllTypes_init_default;
173
174 #define BUFSIZE 4096
175
176 static bool do_static_encode(uint8_t *buffer, size_t *msglen)
177 {
178     pb_ostream_t stream;
179     bool status;
180
181     /* Allocate a message and fill it with defaults */
182     alltypes_static_AllTypes *msg = malloc_with_check(sizeof(alltypes_static_AllTypes));
183     memcpy(msg, &initval, sizeof(initval));
184
185     /* Apply randomness to the data before encoding */
186     while (rand_int(0, 7))
187         rand_mess((uint8_t*)msg, sizeof(alltypes_static_AllTypes));
188
189     stream = pb_ostream_from_buffer(buffer, BUFSIZE);
190     status = pb_encode(&stream, alltypes_static_AllTypes_fields, msg);
191     assert(stream.bytes_written <= BUFSIZE);
192     assert(stream.bytes_written <= alltypes_static_AllTypes_size);
193     
194     *msglen = stream.bytes_written;
195     pb_release(alltypes_static_AllTypes_fields, msg);
196     free_with_check(msg);
197     
198     return status;
199 }
200
201 /* Append or prepend protobuf noise */
202 static void do_protobuf_noise(uint8_t *buffer, size_t *msglen)
203 {
204     int m = rand_int(0, 2);
205     size_t max_size = BUFSIZE - 32 - *msglen;
206     if (m == 1)
207     {
208         /* Prepend */
209         uint8_t *tmp = malloc_with_check(BUFSIZE);
210         size_t s = rand_fill_protobuf(tmp, rand_len(max_size), BUFSIZE - *msglen, 512);
211         memmove(buffer + s, buffer, *msglen);
212         memcpy(buffer, tmp, s);
213         free_with_check(tmp);
214         *msglen += s;
215     }
216     else if (m == 2)
217     {
218         /* Append */
219         size_t s = rand_fill_protobuf(buffer + *msglen, rand_len(max_size), BUFSIZE - *msglen, 512);
220         *msglen += s;
221     }
222 }
223
224 static bool do_static_decode(uint8_t *buffer, size_t msglen, bool assert_success)
225 {
226     pb_istream_t stream;
227     bool status;
228     
229     alltypes_static_AllTypes *msg = malloc_with_check(sizeof(alltypes_static_AllTypes));
230     rand_fill((uint8_t*)msg, sizeof(alltypes_static_AllTypes));
231     stream = pb_istream_from_buffer(buffer, msglen);
232     status = pb_decode(&stream, alltypes_static_AllTypes_fields, msg);
233     
234     if (!status && assert_success)
235     {
236         /* Anything that was successfully encoded, should be decodeable.
237          * One exception: strings without null terminator are encoded up
238          * to end of buffer, but refused on decode because the terminator
239          * would not fit. */
240         if (strcmp(stream.errmsg, "string overflow") != 0)
241             assert(status);
242     }
243     
244     free_with_check(msg);
245     return status;
246 }
247
248 static bool do_pointer_decode(uint8_t *buffer, size_t msglen, bool assert_success)
249 {
250     pb_istream_t stream;
251     bool status;
252     alltypes_pointer_AllTypes *msg;
253     
254     msg = malloc_with_check(sizeof(alltypes_pointer_AllTypes));
255     memset(msg, 0, sizeof(alltypes_pointer_AllTypes));
256     stream = pb_istream_from_buffer(buffer, msglen);
257
258     assert(get_alloc_count() == 0);
259     status = pb_decode(&stream, alltypes_pointer_AllTypes_fields, msg);
260     
261     if (assert_success)
262         assert(status);
263     
264     pb_release(alltypes_pointer_AllTypes_fields, msg);    
265     assert(get_alloc_count() == 0);
266     
267     free_with_check(msg);
268
269     return status;
270 }
271
272 /* Do a decode -> encode -> decode -> encode roundtrip */
273 static void do_static_roundtrip(uint8_t *buffer, size_t msglen)
274 {
275     bool status;
276     uint8_t *buf2 = malloc_with_check(BUFSIZE);
277     uint8_t *buf3 = malloc_with_check(BUFSIZE);
278     size_t msglen2, msglen3;
279     alltypes_static_AllTypes *msg1 = malloc_with_check(sizeof(alltypes_static_AllTypes));
280     alltypes_static_AllTypes *msg2 = malloc_with_check(sizeof(alltypes_static_AllTypes));
281     memset(msg1, 0, sizeof(alltypes_static_AllTypes));
282     memset(msg2, 0, sizeof(alltypes_static_AllTypes));
283     
284     {
285         pb_istream_t stream = pb_istream_from_buffer(buffer, msglen);
286         status = pb_decode(&stream, alltypes_static_AllTypes_fields, msg1);
287         assert(status);
288     }
289     
290     {
291         pb_ostream_t stream = pb_ostream_from_buffer(buf2, BUFSIZE);
292         status = pb_encode(&stream, alltypes_static_AllTypes_fields, msg1);
293         assert(status);
294         msglen2 = stream.bytes_written;
295     }
296     
297     {
298         pb_istream_t stream = pb_istream_from_buffer(buf2, msglen2);
299         status = pb_decode(&stream, alltypes_static_AllTypes_fields, msg2);
300         assert(status);
301     }
302     
303     {
304         pb_ostream_t stream = pb_ostream_from_buffer(buf3, BUFSIZE);
305         status = pb_encode(&stream, alltypes_static_AllTypes_fields, msg2);
306         assert(status);
307         msglen3 = stream.bytes_written;
308     }
309     
310     assert(msglen2 == msglen3);
311     assert(memcmp(buf2, buf3, msglen2) == 0);
312     
313     free_with_check(msg1);
314     free_with_check(msg2);
315     free_with_check(buf2);
316     free_with_check(buf3);
317 }
318
319 /* Do decode -> encode -> decode -> encode roundtrip */
320 static void do_pointer_roundtrip(uint8_t *buffer, size_t msglen)
321 {
322     bool status;
323     uint8_t *buf2 = malloc_with_check(BUFSIZE);
324     uint8_t *buf3 = malloc_with_check(BUFSIZE);
325     size_t msglen2, msglen3;
326     alltypes_pointer_AllTypes *msg1 = malloc_with_check(sizeof(alltypes_pointer_AllTypes));
327     alltypes_pointer_AllTypes *msg2 = malloc_with_check(sizeof(alltypes_pointer_AllTypes));
328     memset(msg1, 0, sizeof(alltypes_pointer_AllTypes));
329     memset(msg2, 0, sizeof(alltypes_pointer_AllTypes));
330     
331     {
332         pb_istream_t stream = pb_istream_from_buffer(buffer, msglen);
333         status = pb_decode(&stream, alltypes_pointer_AllTypes_fields, msg1);
334         assert(status);
335     }
336     
337     {
338         pb_ostream_t stream = pb_ostream_from_buffer(buf2, BUFSIZE);
339         status = pb_encode(&stream, alltypes_pointer_AllTypes_fields, msg1);
340         assert(status);
341         msglen2 = stream.bytes_written;
342     }
343     
344     {
345         pb_istream_t stream = pb_istream_from_buffer(buf2, msglen2);
346         status = pb_decode(&stream, alltypes_pointer_AllTypes_fields, msg2);
347         assert(status);
348     }
349     
350     {
351         pb_ostream_t stream = pb_ostream_from_buffer(buf3, BUFSIZE);
352         status = pb_encode(&stream, alltypes_pointer_AllTypes_fields, msg2);
353         assert(status);
354         msglen3 = stream.bytes_written;
355     }
356     
357     assert(msglen2 == msglen3);
358     assert(memcmp(buf2, buf3, msglen2) == 0);
359     
360     pb_release(alltypes_pointer_AllTypes_fields, msg1);
361     pb_release(alltypes_pointer_AllTypes_fields, msg2);
362     free_with_check(msg1);
363     free_with_check(msg2);
364     free_with_check(buf2);
365     free_with_check(buf3);
366 }
367
368 static void run_iteration()
369 {
370     uint8_t *buffer = malloc_with_check(BUFSIZE);
371     size_t msglen;
372     bool status;
373     
374     rand_fill(buffer, BUFSIZE);
375
376     if (do_static_encode(buffer, &msglen))
377     {
378         do_protobuf_noise(buffer, &msglen);
379     
380         status = do_static_decode(buffer, msglen, true);
381         
382         if (status)
383             do_static_roundtrip(buffer, msglen);
384         
385         status = do_pointer_decode(buffer, msglen, true);
386         
387         if (status)
388             do_pointer_roundtrip(buffer, msglen);
389         
390         /* Apply randomness to the encoded data */
391         while (rand_bool())
392             rand_mess(buffer, BUFSIZE);
393         
394         /* Apply randomness to encoded data length */
395         if (rand_bool())
396             msglen = rand_int(0, BUFSIZE);
397         
398         status = do_static_decode(buffer, msglen, false);
399         do_pointer_decode(buffer, msglen, status);
400         
401         if (status)
402         {
403             do_static_roundtrip(buffer, msglen);
404             do_pointer_roundtrip(buffer, msglen);
405         }
406     }
407     
408     free_with_check(buffer);
409 }
410
411 int main(int argc, char **argv)
412 {
413     int i;
414     if (argc > 1)
415     {
416         random_seed = atol(argv[1]);
417     }
418     else
419     {
420         random_seed = time(NULL);
421     }
422     
423     fprintf(stderr, "Random seed: %llu\n", (long long unsigned)random_seed);
424     
425     for (i = 0; i < 10000; i++)
426     {
427         run_iteration();
428     }
429     
430     return 0;
431 }
432