LCOV - code coverage report
Current view: top level - util/net - fuzz_pcapng.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 8 8 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 <stdio.h>
       6             : #include <stdlib.h>
       7             : #include <unistd.h>
       8             : 
       9             : #include "../fd_util.h"
      10             : #include "../../util/sanitize/fd_fuzz.h"
      11             : #include "./fd_pcapng_private.h"
      12             : 
      13             : int
      14             : LLVMFuzzerInitialize( int  *   argc,
      15          18 :                       char *** argv ) {
      16             :   /* Set up shell without signal handlers */
      17          18 :   putenv( "FD_LOG_BACKTRACE=0" );
      18          18 :   putenv( "FD_LOG_PATH=" );
      19          18 :   fd_boot( argc, argv );
      20          18 :   atexit( fd_halt );
      21             : 
      22             :   /* Disable parsing error logging */
      23          18 :   fd_log_level_stderr_set(4);
      24          18 :   return 0;
      25          18 : }
      26             : 
      27             : int
      28             : LLVMFuzzerTestOneInput( uchar const * data,
      29             :                         ulong         data_sz ) {
      30             : 
      31             :   FILE * file;
      32             :   if( FD_UNLIKELY( data_sz==0UL ) )
      33             :     file = fopen( "/dev/null", "rb" );
      34             :   else
      35             :     file = fmemopen( (void *)data, data_sz, "rb" );
      36             :   FD_TEST( file );
      37             : 
      38             :   /* Construct fake pcapng state */
      39             :   fd_pcapng_iter_t iter = {
      40             :     .stream    = file,
      41             :     .iface_cnt = 1UL,
      42             :     .iface     = {
      43             :       { .link_type = FD_PCAPNG_LINKTYPE_ETHERNET,
      44             :         .opts = {
      45             :           .name     = "eth0",
      46             :           .ip4_addr = {(uchar)127, (uchar)0, (uchar)0, (uchar)1},
      47             :           .mac_addr = "\x06\x00\xde\xad\xbe\xef",
      48             :           .tsresol  = FD_PCAPNG_TSRESOL_NS,
      49             :           .hardware = "fake interface"
      50             :         } }
      51             :     }
      52             :   };
      53             : 
      54             :   for(;;) {
      55             :     FD_FUZZ_MUST_BE_COVERED;
      56             :     fd_pcapng_frame_t const * frame = fd_pcapng_iter_next( &iter );
      57             :     if( !frame ) break;
      58             : 
      59             :     /* Read all fields */
      60             :     ulong value;
      61             :     value = (ulong)frame->type;    FD_COMPILER_FORGET( value );
      62             :     value = (ulong)frame->ts;      FD_COMPILER_FORGET( value );
      63             :     value = (ulong)frame->orig_sz; FD_COMPILER_FORGET( value );
      64             :     value = (ulong)frame->if_idx;  FD_COMPILER_FORGET( value );
      65             :     FD_TEST( frame->data_sz <= FD_PCAPNG_FRAME_SZ );
      66             : 
      67             :     uchar x=0;
      68             :     for( uint i=0; i<frame->data_sz; i++ ) x ^= frame->data[ i ];
      69             :     FD_COMPILER_FORGET( x );
      70             :   }
      71             : 
      72             :   FD_TEST( 0==fclose( file ) );
      73             : 
      74             :   FD_FUZZ_MUST_BE_COVERED;
      75             :   return 0;
      76             : }
      77             : 

Generated by: LCOV version 1.14