LCOV - code coverage report
Current view: top level - ballet/sha256 - fuzz_sha256.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 8 8 100.0 %
Date: 2026-03-18 06:04:16 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_sha256.h"
      12             : 
      13             : #define BATCH_CNT 32UL /* must be at least 1UL */
      14             : 
      15             : static fd_sha256_batch_t batch_sha[1];
      16             : static uchar             hash1    [ FD_SHA256_HASH_SZ ];
      17             : static uchar             hash2    [ FD_SHA256_HASH_SZ ];
      18             : static uchar             ref_hash [ FD_SHA256_HASH_SZ ];
      19             : static uchar             hash_mem [ FD_SHA256_HASH_SZ * BATCH_CNT ];
      20             : static uchar *           hashes   [ BATCH_CNT ];
      21             : static char const *      messages [ BATCH_CNT ];
      22             : static ulong             msg_sizes[ BATCH_CNT ];
      23             : 
      24             : int
      25             : LLVMFuzzerInitialize( int  *   argc,
      26          12 :                       char *** argv ) {
      27             :   /* Set up shell without signal handlers */
      28          12 :   putenv( "FD_LOG_BACKTRACE=0" );
      29          12 :   setenv( "FD_LOG_PATH", "", 0 );
      30          12 :   fd_boot( argc, argv );
      31          12 :   atexit( fd_halt );
      32          12 :   fd_log_level_core_set(3); /* crash on warning log */
      33          12 :   return 0;
      34          12 : }
      35             : 
      36             : int
      37             : LLVMFuzzerTestOneInput( uchar const * fuzz_data,
      38             :                         ulong         fuzz_sz ) {
      39             :   // hash single message
      40             :   char const * msg = ( char const * ) fuzz_data;
      41             : 
      42             :   fd_sha256_t sha[1];
      43             :   assert( fd_sha256_init( sha ) == sha );
      44             :   assert( fd_sha256_append( sha, msg, fuzz_sz ) == sha );
      45             :   assert( fd_sha256_fini( sha, hash1 ) == hash1 );
      46             :   assert( fd_sha256_hash( fuzz_data, fuzz_sz, hash2 ) == hash2 );
      47             :   assert( !memcmp( hash1, hash2, FD_SHA256_HASH_SZ ) );
      48             : 
      49             :   // batch hashing
      50             :   if( fuzz_sz>=BATCH_CNT ) {
      51             :     FD_FUZZ_MUST_BE_COVERED;
      52             : 
      53             :     assert( fd_sha256_batch_init( batch_sha ) == batch_sha );
      54             : 
      55             :     ulong entry_sz = fuzz_sz/BATCH_CNT;
      56             :     for( ulong batch_idx=0UL; batch_idx<BATCH_CNT; batch_idx++ ) {
      57             :       FD_FUZZ_MUST_BE_COVERED;
      58             :       hashes   [ batch_idx ] = hash_mem + FD_SHA256_HASH_SZ*batch_idx;
      59             :       messages [ batch_idx ] = (char const *) fuzz_data + entry_sz*batch_idx;
      60             :       msg_sizes[ batch_idx ] = batch_idx<BATCH_CNT-1UL ? entry_sz : entry_sz+fuzz_sz%BATCH_CNT;
      61             :       assert( fd_sha256_batch_add( batch_sha, messages[ batch_idx ], msg_sizes[ batch_idx ], hashes[ batch_idx ] ) == batch_sha );
      62             :     }
      63             : 
      64             :     assert( fd_sha256_batch_fini( batch_sha ) == batch_sha );
      65             : 
      66             :     for( ulong batch_idx=0UL; batch_idx<BATCH_CNT; batch_idx++ ) {
      67             :       FD_FUZZ_MUST_BE_COVERED;
      68             :       assert( !memcmp( fd_sha256_hash( messages[ batch_idx ], msg_sizes[ batch_idx ], ref_hash ), hashes[ batch_idx ], FD_SHA256_HASH_SZ ) );
      69             :     }
      70             :   } else {
      71             :     FD_FUZZ_MUST_BE_COVERED;
      72             :   }
      73             : 
      74             :   return 0;
      75             : }

Generated by: LCOV version 1.14