/ Check-in [67fecbc7]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add the -memtrace option to dbfuzz2.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:67fecbc79d3e927a7e22f3589be3184114322635874e4b3170666e352d0bfe9d
User & Date: drh 2019-02-04 19:50:44
Context
2019-02-04
19:52
Mention the new -memtrace command-line option in the -help output of the CLI. check-in: ada91aef user: drh tags: trunk
19:50
Add the -memtrace option to dbfuzz2. check-in: 67fecbc7 user: drh tags: trunk
19:45
Enhance dbfuzz2 so that with the -v option it shows the return code and error message for any failing SQL statements. check-in: 3a127ef9 user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to test/dbfuzz2.c.

    68     68   
    69     69   /* True to activate PRAGMA vdbe_debug=on */
    70     70   static int bVdbeDebug = 0;
    71     71   
    72     72   /* Maximum size of the in-memory database file */
    73     73   static sqlite3_int64 szMax = 104857600;
    74     74   
           75  +/***** Copy/paste from ext/misc/memtrace.c ***************************/
           76  +/* The original memory allocation routines */
           77  +static sqlite3_mem_methods memtraceBase;
           78  +static FILE *memtraceOut;
           79  +
           80  +/* Methods that trace memory allocations */
           81  +static void *memtraceMalloc(int n){
           82  +  if( memtraceOut ){
           83  +    fprintf(memtraceOut, "MEMTRACE: allocate %d bytes\n", 
           84  +            memtraceBase.xRoundup(n));
           85  +  }
           86  +  return memtraceBase.xMalloc(n);
           87  +}
           88  +static void memtraceFree(void *p){
           89  +  if( p==0 ) return;
           90  +  if( memtraceOut ){
           91  +    fprintf(memtraceOut, "MEMTRACE: free %d bytes\n", memtraceBase.xSize(p));
           92  +  }
           93  +  memtraceBase.xFree(p);
           94  +}
           95  +static void *memtraceRealloc(void *p, int n){
           96  +  if( p==0 ) return memtraceMalloc(n);
           97  +  if( n==0 ){
           98  +    memtraceFree(p);
           99  +    return 0;
          100  +  }
          101  +  if( memtraceOut ){
          102  +    fprintf(memtraceOut, "MEMTRACE: resize %d -> %d bytes\n",
          103  +            memtraceBase.xSize(p), memtraceBase.xRoundup(n));
          104  +  }
          105  +  return memtraceBase.xRealloc(p, n);
          106  +}
          107  +static int memtraceSize(void *p){
          108  +  return memtraceBase.xSize(p);
          109  +}
          110  +static int memtraceRoundup(int n){
          111  +  return memtraceBase.xRoundup(n);
          112  +}
          113  +static int memtraceInit(void *p){
          114  +  return memtraceBase.xInit(p);
          115  +}
          116  +static void memtraceShutdown(void *p){
          117  +  memtraceBase.xShutdown(p);
          118  +}
          119  +
          120  +/* The substitute memory allocator */
          121  +static sqlite3_mem_methods ersaztMethods = {
          122  +  memtraceMalloc,
          123  +  memtraceFree,
          124  +  memtraceRealloc,
          125  +  memtraceSize,
          126  +  memtraceRoundup,
          127  +  memtraceInit,
          128  +  memtraceShutdown
          129  +};
          130  +
          131  +/* Begin tracing memory allocations to out. */
          132  +int sqlite3MemTraceActivate(FILE *out){
          133  +  int rc = SQLITE_OK;
          134  +  if( memtraceBase.xMalloc==0 ){
          135  +    rc = sqlite3_config(SQLITE_CONFIG_GETMALLOC, &memtraceBase);
          136  +    if( rc==SQLITE_OK ){
          137  +      rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &ersaztMethods);
          138  +    }
          139  +  }
          140  +  memtraceOut = out;
          141  +  return rc;
          142  +}
          143  +
          144  +/* Deactivate memory tracing */
          145  +int sqlite3MemTraceDeactivate(void){
          146  +  int rc = SQLITE_OK;
          147  +  if( memtraceBase.xMalloc!=0 ){
          148  +    rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &memtraceBase);
          149  +    if( rc==SQLITE_OK ){
          150  +      memset(&memtraceBase, 0, sizeof(memtraceBase));
          151  +    }
          152  +  }
          153  +  memtraceOut = 0;
          154  +  return rc;
          155  +}
          156  +/***** End copy/paste from ext/misc/memtrace.c ***************************/
          157  +
    75    158   /* libFuzzer invokes this routine with fuzzed database files (in aData).
    76    159   ** This routine run SQLite against the malformed database to see if it
    77    160   ** can provoke a failure or malfunction.
    78    161   */
    79    162   int LLVMFuzzerTestOneInput(const uint8_t *aData, size_t nByte){
    80    163     unsigned char *a;
    81    164     sqlite3 *db;
................................................................................
   157    240         if( z[0]=='v' && (n = numberOfVChar(z))>0 ){
   158    241           eVerbosity += n;
   159    242           continue;
   160    243         }
   161    244         if( strcmp(z,"vdbe-debug")==0 ){
   162    245           bVdbeDebug = 1;
   163    246           continue;
          247  +      }
          248  +      if( strcmp(z,"memtrace")==0 ){
          249  +        sqlite3MemTraceActivate(stdout);
          250  +        continue;
          251  +      }
          252  +      if( strcmp(z,"mem")==0 ){
          253  +        bVdbeDebug = 1;
          254  +        continue;
   164    255         }
   165    256         if( strcmp(z,"max-db-size")==0 ){
   166    257           if( i+1==argc ){
   167    258             fprintf(stderr, "missing argument to %s\n", argv[i]);
   168    259             exit(1);
   169    260           }
   170    261           szMax = strtol(argv[++i], 0, 0);