xref: /freebsd/contrib/libcbor/test/fuzz_test.c (revision 5d3e7166f6a0187fa3f8831b16a06bd9955c21ff)
110ff414cSEd Maste /*
210ff414cSEd Maste  * Copyright (c) 2014-2020 Pavel Kalvoda <me@pavelkalvoda.com>
310ff414cSEd Maste  *
410ff414cSEd Maste  * libcbor is free software; you can redistribute it and/or modify
510ff414cSEd Maste  * it under the terms of the MIT license. See LICENSE for details.
610ff414cSEd Maste  */
710ff414cSEd Maste 
810ff414cSEd Maste #include <time.h>
9*5d3e7166SEd Maste #include "assertions.h"
1010ff414cSEd Maste #include "cbor.h"
1110ff414cSEd Maste 
1210ff414cSEd Maste #ifdef HUGE_FUZZ
1310ff414cSEd Maste #define ROUNDS 65536ULL
1410ff414cSEd Maste #define MAXLEN 131072ULL
1510ff414cSEd Maste #else
1610ff414cSEd Maste #define ROUNDS 256ULL
1710ff414cSEd Maste #define MAXLEN 2048ULL
1810ff414cSEd Maste #endif
1910ff414cSEd Maste 
2010ff414cSEd Maste #ifdef PRINT_FUZZ
printmem(const unsigned char * ptr,size_t length)2110ff414cSEd Maste static void printmem(const unsigned char *ptr, size_t length) {
2210ff414cSEd Maste   for (size_t i = 0; i < length; i++) printf("%02X", ptr[i]);
2310ff414cSEd Maste   printf("\n");
2410ff414cSEd Maste }
2510ff414cSEd Maste #endif
2610ff414cSEd Maste 
2710ff414cSEd Maste unsigned seed;
2810ff414cSEd Maste 
mock_malloc(size_t size)2910ff414cSEd Maste void *mock_malloc(size_t size) {
3010ff414cSEd Maste   if (size > (1 << 19))
3110ff414cSEd Maste     return NULL;
3210ff414cSEd Maste   else
3310ff414cSEd Maste     return malloc(size);
3410ff414cSEd Maste }
3510ff414cSEd Maste 
run_round(void)36*5d3e7166SEd Maste static void run_round(void) {
3710ff414cSEd Maste   cbor_item_t *item;
3810ff414cSEd Maste   struct cbor_load_result res;
3910ff414cSEd Maste 
4010ff414cSEd Maste   size_t length = rand() % MAXLEN + 1;
4110ff414cSEd Maste   unsigned char *data = malloc(length);
4210ff414cSEd Maste   for (size_t i = 0; i < length; i++) {
4310ff414cSEd Maste     data[i] = rand() % 0xFF;
4410ff414cSEd Maste   }
4510ff414cSEd Maste 
4610ff414cSEd Maste #ifdef PRINT_FUZZ
4710ff414cSEd Maste   printmem(data, length);
4810ff414cSEd Maste #endif
4910ff414cSEd Maste 
5010ff414cSEd Maste   item = cbor_load(data, length, &res);
5110ff414cSEd Maste 
5210ff414cSEd Maste   if (res.error.code == CBOR_ERR_NONE) cbor_decref(&item);
5310ff414cSEd Maste   /* Otherwise there should be nothing left behind by the decoder */
5410ff414cSEd Maste 
5510ff414cSEd Maste   free(data);
5610ff414cSEd Maste }
5710ff414cSEd Maste 
fuzz(void ** _CBOR_UNUSED (_state))58*5d3e7166SEd Maste static void fuzz(void **_CBOR_UNUSED(_state)) {
5910ff414cSEd Maste   cbor_set_allocs(mock_malloc, realloc, free);
6010ff414cSEd Maste   printf("Fuzzing %llu rounds of up to %llu bytes with seed %u\n", ROUNDS,
6110ff414cSEd Maste          MAXLEN, seed);
6210ff414cSEd Maste   srand(seed);
6310ff414cSEd Maste 
6410ff414cSEd Maste   for (size_t i = 0; i < ROUNDS; i++) run_round();
6510ff414cSEd Maste 
6610ff414cSEd Maste   printf("Successfully fuzzed through %llu kB of data\n",
6710ff414cSEd Maste          (ROUNDS * MAXLEN) / 1024);
6810ff414cSEd Maste }
6910ff414cSEd Maste 
main(int argc,char * argv[])7010ff414cSEd Maste int main(int argc, char *argv[]) {
7110ff414cSEd Maste   if (argc > 1)
7210ff414cSEd Maste     seed = (unsigned)strtoul(argv[1], NULL, 10);
7310ff414cSEd Maste   else
7410ff414cSEd Maste     seed = (unsigned)time(NULL);
7510ff414cSEd Maste 
7610ff414cSEd Maste   const struct CMUnitTest tests[] = {cmocka_unit_test(fuzz)};
7710ff414cSEd Maste   return cmocka_run_group_tests(tests, NULL, NULL);
7810ff414cSEd Maste }
79