LCOV - code coverage report
Current view: top level - ballet/siphash13 - fuzz_siphash13.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 6 6 100.0 %
Date: 2024-11-13 11:58:15 Functions: 1 1 100.0 %

          Line data    Source code
       1             : #if !FD_HAS_HOSTED
       2             : #error "This target requires FD_HAS_HOSTED"
       3             : #endif
       4             : 
       5             : #include <assert.h>
       6             : #include <stdio.h>
       7             : #include <stdlib.h>
       8             : 
       9             : #include "../../util/fd_util.h"
      10             : #include "../../util/sanitize/fd_fuzz.h"
      11             : #include "fd_siphash13.h"
      12             : 
      13             : static fd_siphash13_t sip[1];
      14             : static fd_siphash13_t sip_fast[1];
      15             : 
      16             : int
      17             : LLVMFuzzerInitialize( int  *   pargc,
      18          18 :                       char *** pargv ) {
      19             :   /* Set up shell without signal handlers */
      20          18 :   putenv( "FD_LOG_BACKTRACE=0" );
      21          18 :   fd_boot( pargc, pargv );
      22          18 :   atexit( fd_halt );
      23          18 :   return 0;
      24          18 : }
      25             : 
      26             : #define FAST_HASH_CHUNK_SZ (32UL) /* MUST be a multiple of 8 */
      27             : 
      28             : struct fuzz_siphash13
      29             : {
      30             :   ulong k0;
      31             :   ulong k1;
      32             :   uchar flex [];
      33             : };
      34             : 
      35             : 
      36             : int
      37             : LLVMFuzzerTestOneInput( uchar const * fuzz_data,
      38             :                         ulong         fuzz_data_sz ) {
      39             : 
      40             :   if (fuzz_data_sz < sizeof(struct fuzz_siphash13)) {
      41             :     return -1;
      42             :   };
      43             : 
      44             :   // Clear buffers on the heap
      45             :   memset( sip, 0, sizeof(fd_siphash13_t) );
      46             :   memset( sip_fast, 0, sizeof(fd_siphash13_t) );
      47             : 
      48             : 
      49             :   struct fuzz_siphash13 * testcase = (struct fuzz_siphash13 *)(fuzz_data);
      50             :   ulong flex_sz = fuzz_data_sz - sizeof(struct fuzz_siphash13);
      51             : 
      52             :   assert( sip == fd_siphash13_init  ( sip, testcase->k0, testcase->k1 ) );
      53             :   assert( sip == fd_siphash13_append( sip, testcase->flex, flex_sz ) );
      54             : 
      55             :   ulong hash = fd_siphash13_fini( sip );
      56             :   assert( hash == fd_siphash13_hash( testcase->flex, flex_sz, testcase->k0, testcase->k1 ) );
      57             : 
      58             :   // fuzz fast hashing
      59             :   assert( sip_fast == fd_siphash13_init( sip_fast, testcase->k0, testcase->k1 ) );
      60             : 
      61             :   uchar const * data_chunk = testcase->flex;
      62             :   for( ulong rem = flex_sz/FAST_HASH_CHUNK_SZ; rem > 0; rem-- ) {
      63             :     assert( sip_fast == fd_siphash13_append_fast( sip_fast, data_chunk, FAST_HASH_CHUNK_SZ ) );
      64             :     data_chunk += FAST_HASH_CHUNK_SZ;
      65             :   }
      66             :   assert( sip_fast == fd_siphash13_append( sip_fast, data_chunk, flex_sz%FAST_HASH_CHUNK_SZ ) );
      67             :   ulong hash_fast = fd_siphash13_fini( sip_fast );
      68             :   assert( hash == hash_fast );
      69             : 
      70             :   FD_FUZZ_MUST_BE_COVERED;
      71             :   return 0;
      72             : }

Generated by: LCOV version 1.14