drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2015-05-25 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** |
drh | 0045219 | 2015-06-17 18:24:40 +0000 | [diff] [blame] | 13 | ** This is a utility program designed to aid running regressions tests on |
drh | a8781d9 | 2020-02-25 20:05:58 +0000 | [diff] [blame] | 14 | ** the SQLite library using data from external fuzzers. |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 15 | ** |
| 16 | ** This program reads content from an SQLite database file with the following |
| 17 | ** schema: |
| 18 | ** |
| 19 | ** CREATE TABLE db( |
| 20 | ** dbid INTEGER PRIMARY KEY, -- database id |
| 21 | ** dbcontent BLOB -- database disk file image |
| 22 | ** ); |
| 23 | ** CREATE TABLE xsql( |
| 24 | ** sqlid INTEGER PRIMARY KEY, -- SQL script id |
| 25 | ** sqltext TEXT -- Text of SQL statements to run |
| 26 | ** ); |
drh | 0045219 | 2015-06-17 18:24:40 +0000 | [diff] [blame] | 27 | ** CREATE TABLE IF NOT EXISTS readme( |
| 28 | ** msg TEXT -- Human-readable description of this test collection |
| 29 | ** ); |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 30 | ** |
| 31 | ** For each database file in the DB table, the SQL text in the XSQL table |
drh | 0045219 | 2015-06-17 18:24:40 +0000 | [diff] [blame] | 32 | ** is run against that database. All README.MSG values are printed prior |
| 33 | ** to the start of the test (unless the --quiet option is used). If the |
| 34 | ** DB table is empty, then all entries in XSQL are run against an empty |
| 35 | ** in-memory database. |
| 36 | ** |
| 37 | ** This program is looking for crashes, assertion faults, and/or memory leaks. |
| 38 | ** No attempt is made to verify the output. The assumption is that either all |
| 39 | ** of the database files or all of the SQL statements are malformed inputs, |
| 40 | ** generated by a fuzzer, that need to be checked to make sure they do not |
| 41 | ** present a security risk. |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 42 | ** |
| 43 | ** This program also includes some command-line options to help with |
drh | 0045219 | 2015-06-17 18:24:40 +0000 | [diff] [blame] | 44 | ** creation and maintenance of the source content database. The command |
| 45 | ** |
| 46 | ** ./fuzzcheck database.db --load-sql FILE... |
| 47 | ** |
| 48 | ** Loads all FILE... arguments into the XSQL table. The --load-db option |
| 49 | ** works the same but loads the files into the DB table. The -m option can |
| 50 | ** be used to initialize the README table. The "database.db" file is created |
| 51 | ** if it does not previously exist. Example: |
| 52 | ** |
| 53 | ** ./fuzzcheck new.db --load-sql *.sql |
| 54 | ** ./fuzzcheck new.db --load-db *.db |
| 55 | ** ./fuzzcheck new.db -m 'New test cases' |
| 56 | ** |
| 57 | ** The three commands above will create the "new.db" file and initialize all |
| 58 | ** tables. Then do "./fuzzcheck new.db" to run the tests. |
| 59 | ** |
| 60 | ** DEBUGGING HINTS: |
| 61 | ** |
| 62 | ** If fuzzcheck does crash, it can be run in the debugger and the content |
| 63 | ** of the global variable g.zTextName[] will identify the specific XSQL and |
| 64 | ** DB values that were running when the crash occurred. |
drh | a8781d9 | 2020-02-25 20:05:58 +0000 | [diff] [blame] | 65 | ** |
| 66 | ** DBSQLFUZZ: |
| 67 | ** |
| 68 | ** The dbsqlfuzz fuzzer includes both a database file and SQL to run against |
| 69 | ** that database in its input. This utility can now process dbsqlfuzz |
| 70 | ** input files. Load such files using the "--load-dbsql FILE ..." command-line |
| 71 | ** option. |
| 72 | ** |
| 73 | ** Dbsqlfuzz inputs are ordinary text. The first part of the file is text |
| 74 | ** that describes the content of the database (using a lot of hexadecimal), |
| 75 | ** then there is a divider line followed by the SQL to run against the |
| 76 | ** database. Because they are ordinary text, dbsqlfuzz inputs are stored |
| 77 | ** in the XSQL table, as if they were ordinary SQL inputs. The isDbSql() |
| 78 | ** function can look at a text string and determine whether or not it is |
| 79 | ** a valid dbsqlfuzz input. |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 80 | */ |
| 81 | #include <stdio.h> |
| 82 | #include <stdlib.h> |
| 83 | #include <string.h> |
| 84 | #include <stdarg.h> |
| 85 | #include <ctype.h> |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 86 | #include <assert.h> |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 87 | #include "sqlite3.h" |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 88 | #define ISSPACE(X) isspace((unsigned char)(X)) |
| 89 | #define ISDIGIT(X) isdigit((unsigned char)(X)) |
| 90 | |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 91 | |
drh | 94701b0 | 2015-06-24 13:25:34 +0000 | [diff] [blame] | 92 | #ifdef __unix__ |
| 93 | # include <signal.h> |
| 94 | # include <unistd.h> |
| 95 | #endif |
| 96 | |
drh | ea432ba | 2016-11-11 16:33:47 +0000 | [diff] [blame] | 97 | #ifdef SQLITE_OSS_FUZZ |
| 98 | # include <stddef.h> |
mistachkin | ac8ba26 | 2018-03-07 14:42:17 +0000 | [diff] [blame] | 99 | # if !defined(_MSC_VER) |
| 100 | # include <stdint.h> |
| 101 | # endif |
| 102 | #endif |
| 103 | |
| 104 | #if defined(_MSC_VER) |
| 105 | typedef unsigned char uint8_t; |
drh | ea432ba | 2016-11-11 16:33:47 +0000 | [diff] [blame] | 106 | #endif |
| 107 | |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 108 | /* |
| 109 | ** Files in the virtual file system. |
| 110 | */ |
| 111 | typedef struct VFile VFile; |
| 112 | struct VFile { |
| 113 | char *zFilename; /* Filename. NULL for delete-on-close. From malloc() */ |
| 114 | int sz; /* Size of the file in bytes */ |
| 115 | int nRef; /* Number of references to this file */ |
| 116 | unsigned char *a; /* Content of the file. From malloc() */ |
| 117 | }; |
| 118 | typedef struct VHandle VHandle; |
| 119 | struct VHandle { |
| 120 | sqlite3_file base; /* Base class. Must be first */ |
| 121 | VFile *pVFile; /* The underlying file */ |
| 122 | }; |
| 123 | |
| 124 | /* |
| 125 | ** The value of a database file template, or of an SQL script |
| 126 | */ |
| 127 | typedef struct Blob Blob; |
| 128 | struct Blob { |
| 129 | Blob *pNext; /* Next in a list */ |
| 130 | int id; /* Id of this Blob */ |
drh | e5c5f2c | 2015-05-26 00:28:08 +0000 | [diff] [blame] | 131 | int seq; /* Sequence number */ |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 132 | int sz; /* Size of this Blob in bytes */ |
| 133 | unsigned char a[1]; /* Blob content. Extra space allocated as needed. */ |
| 134 | }; |
| 135 | |
| 136 | /* |
| 137 | ** Maximum number of files in the in-memory virtual filesystem. |
| 138 | */ |
| 139 | #define MX_FILE 10 |
| 140 | |
| 141 | /* |
| 142 | ** Maximum allowed file size |
| 143 | */ |
| 144 | #define MX_FILE_SZ 10000000 |
| 145 | |
| 146 | /* |
| 147 | ** All global variables are gathered into the "g" singleton. |
| 148 | */ |
| 149 | static struct GlobalVars { |
| 150 | const char *zArgv0; /* Name of program */ |
drh | a7648f0 | 2019-12-18 13:02:18 +0000 | [diff] [blame] | 151 | const char *zDbFile; /* Name of database file */ |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 152 | VFile aFile[MX_FILE]; /* The virtual filesystem */ |
| 153 | int nDb; /* Number of template databases */ |
| 154 | Blob *pFirstDb; /* Content of first template database */ |
| 155 | int nSql; /* Number of SQL scripts */ |
| 156 | Blob *pFirstSql; /* First SQL script */ |
drh | beaf514 | 2016-12-26 00:15:56 +0000 | [diff] [blame] | 157 | unsigned int uRandom; /* Seed for the SQLite PRNG */ |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 158 | char zTestName[100]; /* Name of current test */ |
| 159 | } g; |
| 160 | |
| 161 | /* |
| 162 | ** Print an error message and quit. |
| 163 | */ |
| 164 | static void fatalError(const char *zFormat, ...){ |
| 165 | va_list ap; |
drh | a7648f0 | 2019-12-18 13:02:18 +0000 | [diff] [blame] | 166 | fprintf(stderr, "%s", g.zArgv0); |
| 167 | if( g.zDbFile ) fprintf(stderr, " %s", g.zDbFile); |
| 168 | if( g.zTestName[0] ) fprintf(stderr, " (%s)", g.zTestName); |
| 169 | fprintf(stderr, ": "); |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 170 | va_start(ap, zFormat); |
| 171 | vfprintf(stderr, zFormat, ap); |
| 172 | va_end(ap); |
| 173 | fprintf(stderr, "\n"); |
| 174 | exit(1); |
| 175 | } |
| 176 | |
| 177 | /* |
drh | a7648f0 | 2019-12-18 13:02:18 +0000 | [diff] [blame] | 178 | ** signal handler |
drh | 94701b0 | 2015-06-24 13:25:34 +0000 | [diff] [blame] | 179 | */ |
| 180 | #ifdef __unix__ |
drh | a7648f0 | 2019-12-18 13:02:18 +0000 | [diff] [blame] | 181 | static void signalHandler(int signum){ |
| 182 | const char *zSig; |
| 183 | if( signum==SIGABRT ){ |
| 184 | zSig = "abort"; |
| 185 | }else if( signum==SIGALRM ){ |
| 186 | zSig = "timeout"; |
| 187 | }else if( signum==SIGSEGV ){ |
| 188 | zSig = "segfault"; |
| 189 | }else{ |
| 190 | zSig = "signal"; |
| 191 | } |
| 192 | fatalError(zSig); |
drh | 94701b0 | 2015-06-24 13:25:34 +0000 | [diff] [blame] | 193 | } |
| 194 | #endif |
| 195 | |
| 196 | /* |
| 197 | ** Set the an alarm to go off after N seconds. Disable the alarm |
| 198 | ** if N==0 |
| 199 | */ |
| 200 | static void setAlarm(int N){ |
| 201 | #ifdef __unix__ |
| 202 | alarm(N); |
| 203 | #else |
| 204 | (void)N; |
| 205 | #endif |
| 206 | } |
| 207 | |
drh | 7805735 | 2015-06-24 23:17:35 +0000 | [diff] [blame] | 208 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
drh | 94701b0 | 2015-06-24 13:25:34 +0000 | [diff] [blame] | 209 | /* |
drh | d83e283 | 2015-06-24 14:45:44 +0000 | [diff] [blame] | 210 | ** This an SQL progress handler. After an SQL statement has run for |
| 211 | ** many steps, we want to interrupt it. This guards against infinite |
| 212 | ** loops from recursive common table expressions. |
| 213 | ** |
| 214 | ** *pVdbeLimitFlag is true if the --limit-vdbe command-line option is used. |
| 215 | ** In that case, hitting the progress handler is a fatal error. |
| 216 | */ |
| 217 | static int progressHandler(void *pVdbeLimitFlag){ |
| 218 | if( *(int*)pVdbeLimitFlag ) fatalError("too many VDBE cycles"); |
| 219 | return 1; |
| 220 | } |
drh | 7805735 | 2015-06-24 23:17:35 +0000 | [diff] [blame] | 221 | #endif |
drh | d83e283 | 2015-06-24 14:45:44 +0000 | [diff] [blame] | 222 | |
| 223 | /* |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 224 | ** Reallocate memory. Show and error and quit if unable. |
| 225 | */ |
| 226 | static void *safe_realloc(void *pOld, int szNew){ |
drh | c5412d5 | 2016-03-23 17:54:19 +0000 | [diff] [blame] | 227 | void *pNew = realloc(pOld, szNew<=0 ? 1 : szNew); |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 228 | if( pNew==0 ) fatalError("unable to realloc for %d bytes", szNew); |
| 229 | return pNew; |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | ** Initialize the virtual file system. |
| 234 | */ |
| 235 | static void formatVfs(void){ |
| 236 | int i; |
| 237 | for(i=0; i<MX_FILE; i++){ |
| 238 | g.aFile[i].sz = -1; |
| 239 | g.aFile[i].zFilename = 0; |
| 240 | g.aFile[i].a = 0; |
| 241 | g.aFile[i].nRef = 0; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | |
| 246 | /* |
| 247 | ** Erase all information in the virtual file system. |
| 248 | */ |
| 249 | static void reformatVfs(void){ |
| 250 | int i; |
| 251 | for(i=0; i<MX_FILE; i++){ |
| 252 | if( g.aFile[i].sz<0 ) continue; |
| 253 | if( g.aFile[i].zFilename ){ |
| 254 | free(g.aFile[i].zFilename); |
| 255 | g.aFile[i].zFilename = 0; |
| 256 | } |
| 257 | if( g.aFile[i].nRef>0 ){ |
| 258 | fatalError("file %d still open. nRef=%d", i, g.aFile[i].nRef); |
| 259 | } |
| 260 | g.aFile[i].sz = -1; |
| 261 | free(g.aFile[i].a); |
| 262 | g.aFile[i].a = 0; |
| 263 | g.aFile[i].nRef = 0; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | ** Find a VFile by name |
| 269 | */ |
| 270 | static VFile *findVFile(const char *zName){ |
| 271 | int i; |
drh | a9542b1 | 2015-05-25 19:35:42 +0000 | [diff] [blame] | 272 | if( zName==0 ) return 0; |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 273 | for(i=0; i<MX_FILE; i++){ |
| 274 | if( g.aFile[i].zFilename==0 ) continue; |
| 275 | if( strcmp(g.aFile[i].zFilename, zName)==0 ) return &g.aFile[i]; |
| 276 | } |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | ** Find a VFile by name. Create it if it does not already exist and |
| 282 | ** initialize it to the size and content given. |
| 283 | ** |
| 284 | ** Return NULL only if the filesystem is full. |
| 285 | */ |
| 286 | static VFile *createVFile(const char *zName, int sz, unsigned char *pData){ |
| 287 | VFile *pNew = findVFile(zName); |
| 288 | int i; |
| 289 | if( pNew ) return pNew; |
| 290 | for(i=0; i<MX_FILE && g.aFile[i].sz>=0; i++){} |
| 291 | if( i>=MX_FILE ) return 0; |
| 292 | pNew = &g.aFile[i]; |
drh | a9542b1 | 2015-05-25 19:35:42 +0000 | [diff] [blame] | 293 | if( zName ){ |
drh | e683b89 | 2016-02-15 18:47:26 +0000 | [diff] [blame] | 294 | int nName = (int)strlen(zName)+1; |
| 295 | pNew->zFilename = safe_realloc(0, nName); |
| 296 | memcpy(pNew->zFilename, zName, nName); |
drh | a9542b1 | 2015-05-25 19:35:42 +0000 | [diff] [blame] | 297 | }else{ |
| 298 | pNew->zFilename = 0; |
| 299 | } |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 300 | pNew->nRef = 0; |
| 301 | pNew->sz = sz; |
| 302 | pNew->a = safe_realloc(0, sz); |
| 303 | if( sz>0 ) memcpy(pNew->a, pData, sz); |
| 304 | return pNew; |
| 305 | } |
| 306 | |
| 307 | |
| 308 | /* |
| 309 | ** Implementation of the "readfile(X)" SQL function. The entire content |
| 310 | ** of the file named X is read and returned as a BLOB. NULL is returned |
| 311 | ** if the file does not exist or is unreadable. |
| 312 | */ |
| 313 | static void readfileFunc( |
| 314 | sqlite3_context *context, |
| 315 | int argc, |
| 316 | sqlite3_value **argv |
| 317 | ){ |
| 318 | const char *zName; |
| 319 | FILE *in; |
| 320 | long nIn; |
| 321 | void *pBuf; |
| 322 | |
| 323 | zName = (const char*)sqlite3_value_text(argv[0]); |
| 324 | if( zName==0 ) return; |
| 325 | in = fopen(zName, "rb"); |
| 326 | if( in==0 ) return; |
| 327 | fseek(in, 0, SEEK_END); |
| 328 | nIn = ftell(in); |
| 329 | rewind(in); |
| 330 | pBuf = sqlite3_malloc64( nIn ); |
| 331 | if( pBuf && 1==fread(pBuf, nIn, 1, in) ){ |
| 332 | sqlite3_result_blob(context, pBuf, nIn, sqlite3_free); |
| 333 | }else{ |
| 334 | sqlite3_free(pBuf); |
| 335 | } |
| 336 | fclose(in); |
| 337 | } |
| 338 | |
| 339 | /* |
drh | a8781d9 | 2020-02-25 20:05:58 +0000 | [diff] [blame] | 340 | ** Implementation of the "readtextfile(X)" SQL function. The text content |
| 341 | ** of the file named X through the end of the file or to the first \000 |
| 342 | ** character, whichever comes first, is read and returned as TEXT. NULL |
| 343 | ** is returned if the file does not exist or is unreadable. |
| 344 | */ |
| 345 | static void readtextfileFunc( |
| 346 | sqlite3_context *context, |
| 347 | int argc, |
| 348 | sqlite3_value **argv |
| 349 | ){ |
| 350 | const char *zName; |
| 351 | FILE *in; |
| 352 | long nIn; |
| 353 | char *pBuf; |
| 354 | |
| 355 | zName = (const char*)sqlite3_value_text(argv[0]); |
| 356 | if( zName==0 ) return; |
| 357 | in = fopen(zName, "rb"); |
| 358 | if( in==0 ) return; |
| 359 | fseek(in, 0, SEEK_END); |
| 360 | nIn = ftell(in); |
| 361 | rewind(in); |
| 362 | pBuf = sqlite3_malloc64( nIn+1 ); |
| 363 | if( pBuf && 1==fread(pBuf, nIn, 1, in) ){ |
| 364 | pBuf[nIn] = 0; |
| 365 | sqlite3_result_text(context, pBuf, -1, sqlite3_free); |
| 366 | }else{ |
| 367 | sqlite3_free(pBuf); |
| 368 | } |
| 369 | fclose(in); |
| 370 | } |
| 371 | |
| 372 | /* |
drh | 40e0e0d | 2015-09-22 18:51:17 +0000 | [diff] [blame] | 373 | ** Implementation of the "writefile(X,Y)" SQL function. The argument Y |
| 374 | ** is written into file X. The number of bytes written is returned. Or |
| 375 | ** NULL is returned if something goes wrong, such as being unable to open |
| 376 | ** file X for writing. |
| 377 | */ |
| 378 | static void writefileFunc( |
| 379 | sqlite3_context *context, |
| 380 | int argc, |
| 381 | sqlite3_value **argv |
| 382 | ){ |
| 383 | FILE *out; |
| 384 | const char *z; |
| 385 | sqlite3_int64 rc; |
| 386 | const char *zFile; |
| 387 | |
| 388 | (void)argc; |
| 389 | zFile = (const char*)sqlite3_value_text(argv[0]); |
| 390 | if( zFile==0 ) return; |
| 391 | out = fopen(zFile, "wb"); |
| 392 | if( out==0 ) return; |
| 393 | z = (const char*)sqlite3_value_blob(argv[1]); |
| 394 | if( z==0 ){ |
| 395 | rc = 0; |
| 396 | }else{ |
| 397 | rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out); |
| 398 | } |
| 399 | fclose(out); |
| 400 | sqlite3_result_int64(context, rc); |
| 401 | } |
| 402 | |
| 403 | |
| 404 | /* |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 405 | ** Load a list of Blob objects from the database |
| 406 | */ |
| 407 | static void blobListLoadFromDb( |
| 408 | sqlite3 *db, /* Read from this database */ |
| 409 | const char *zSql, /* Query used to extract the blobs */ |
drh | a9542b1 | 2015-05-25 19:35:42 +0000 | [diff] [blame] | 410 | int onlyId, /* Only load where id is this value */ |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 411 | int *pN, /* OUT: Write number of blobs loaded here */ |
| 412 | Blob **ppList /* OUT: Write the head of the blob list here */ |
| 413 | ){ |
| 414 | Blob head; |
| 415 | Blob *p; |
| 416 | sqlite3_stmt *pStmt; |
| 417 | int n = 0; |
| 418 | int rc; |
drh | a9542b1 | 2015-05-25 19:35:42 +0000 | [diff] [blame] | 419 | char *z2; |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 420 | |
drh | a9542b1 | 2015-05-25 19:35:42 +0000 | [diff] [blame] | 421 | if( onlyId>0 ){ |
| 422 | z2 = sqlite3_mprintf("%s WHERE rowid=%d", zSql, onlyId); |
| 423 | }else{ |
| 424 | z2 = sqlite3_mprintf("%s", zSql); |
| 425 | } |
| 426 | rc = sqlite3_prepare_v2(db, z2, -1, &pStmt, 0); |
| 427 | sqlite3_free(z2); |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 428 | if( rc ) fatalError("%s", sqlite3_errmsg(db)); |
| 429 | head.pNext = 0; |
| 430 | p = &head; |
| 431 | while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 432 | int sz = sqlite3_column_bytes(pStmt, 1); |
| 433 | Blob *pNew = safe_realloc(0, sizeof(*pNew)+sz ); |
| 434 | pNew->id = sqlite3_column_int(pStmt, 0); |
| 435 | pNew->sz = sz; |
drh | e5c5f2c | 2015-05-26 00:28:08 +0000 | [diff] [blame] | 436 | pNew->seq = n++; |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 437 | pNew->pNext = 0; |
| 438 | memcpy(pNew->a, sqlite3_column_blob(pStmt,1), sz); |
| 439 | pNew->a[sz] = 0; |
| 440 | p->pNext = pNew; |
| 441 | p = pNew; |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 442 | } |
| 443 | sqlite3_finalize(pStmt); |
| 444 | *pN = n; |
| 445 | *ppList = head.pNext; |
| 446 | } |
| 447 | |
| 448 | /* |
| 449 | ** Free a list of Blob objects |
| 450 | */ |
| 451 | static void blobListFree(Blob *p){ |
| 452 | Blob *pNext; |
| 453 | while( p ){ |
| 454 | pNext = p->pNext; |
| 455 | free(p); |
| 456 | p = pNext; |
| 457 | } |
| 458 | } |
| 459 | |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 460 | /* Return the current wall-clock time */ |
| 461 | static sqlite3_int64 timeOfDay(void){ |
| 462 | static sqlite3_vfs *clockVfs = 0; |
| 463 | sqlite3_int64 t; |
drh | 8055a3e | 2018-11-21 14:27:34 +0000 | [diff] [blame] | 464 | if( clockVfs==0 ){ |
| 465 | clockVfs = sqlite3_vfs_find(0); |
| 466 | if( clockVfs==0 ) return 0; |
| 467 | } |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 468 | if( clockVfs->iVersion>=1 && clockVfs->xCurrentTimeInt64!=0 ){ |
| 469 | clockVfs->xCurrentTimeInt64(clockVfs, &t); |
| 470 | }else{ |
| 471 | double r; |
| 472 | clockVfs->xCurrentTime(clockVfs, &r); |
| 473 | t = (sqlite3_int64)(r*86400000.0); |
| 474 | } |
| 475 | return t; |
| 476 | } |
| 477 | |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 478 | /*************************************************************************** |
| 479 | ** Code to process combined database+SQL scripts generated by the |
| 480 | ** dbsqlfuzz fuzzer. |
| 481 | */ |
| 482 | |
| 483 | /* An instance of the following object is passed by pointer as the |
| 484 | ** client data to various callbacks. |
| 485 | */ |
| 486 | typedef struct FuzzCtx { |
| 487 | sqlite3 *db; /* The database connection */ |
| 488 | sqlite3_int64 iCutoffTime; /* Stop processing at this time. */ |
| 489 | sqlite3_int64 iLastCb; /* Time recorded for previous progress callback */ |
| 490 | sqlite3_int64 mxInterval; /* Longest interval between two progress calls */ |
| 491 | unsigned nCb; /* Number of progress callbacks */ |
| 492 | unsigned mxCb; /* Maximum number of progress callbacks allowed */ |
| 493 | unsigned execCnt; /* Number of calls to the sqlite3_exec callback */ |
| 494 | int timeoutHit; /* True when reaching a timeout */ |
| 495 | } FuzzCtx; |
| 496 | |
| 497 | /* Verbosity level for the dbsqlfuzz test runner */ |
| 498 | static int eVerbosity = 0; |
| 499 | |
| 500 | /* True to activate PRAGMA vdbe_debug=on */ |
| 501 | static int bVdbeDebug = 0; |
| 502 | |
| 503 | /* Timeout for each fuzzing attempt, in milliseconds */ |
drh | ed45703 | 2019-01-25 17:51:06 +0000 | [diff] [blame] | 504 | static int giTimeout = 10000; /* Defaults to 10 seconds */ |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 505 | |
| 506 | /* Maximum number of progress handler callbacks */ |
| 507 | static unsigned int mxProgressCb = 2000; |
| 508 | |
| 509 | /* Maximum string length in SQLite */ |
| 510 | static int lengthLimit = 1000000; |
| 511 | |
drh | be03cc9 | 2020-01-20 14:42:09 +0000 | [diff] [blame] | 512 | /* Maximum expression depth */ |
| 513 | static int depthLimit = 500; |
| 514 | |
drh | 31999c5 | 2019-11-14 17:46:32 +0000 | [diff] [blame] | 515 | /* Limit on the amount of heap memory that can be used */ |
drh | a8781d9 | 2020-02-25 20:05:58 +0000 | [diff] [blame] | 516 | static sqlite3_int64 heapLimit = 100000000; |
drh | 31999c5 | 2019-11-14 17:46:32 +0000 | [diff] [blame] | 517 | |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 518 | /* Maximum byte-code program length in SQLite */ |
| 519 | static int vdbeOpLimit = 25000; |
| 520 | |
| 521 | /* Maximum size of the in-memory database */ |
| 522 | static sqlite3_int64 maxDbSize = 104857600; |
drh | 39b3bcf | 2020-03-02 16:31:21 +0000 | [diff] [blame] | 523 | /* OOM simulation parameters */ |
| 524 | static unsigned int oomCounter = 0; /* Simulate OOM when equals 1 */ |
| 525 | static unsigned int oomRepeat = 0; /* Number of OOMs in a row */ |
| 526 | static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */ |
| 527 | |
| 528 | /* This routine is called when a simulated OOM occurs. It is broken |
| 529 | ** out as a separate routine to make it easy to set a breakpoint on |
| 530 | ** the OOM |
| 531 | */ |
| 532 | void oomFault(void){ |
| 533 | if( eVerbosity ){ |
| 534 | printf("Simulated OOM fault\n"); |
| 535 | } |
| 536 | if( oomRepeat>0 ){ |
| 537 | oomRepeat--; |
| 538 | }else{ |
| 539 | oomCounter--; |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | /* This routine is a replacement malloc() that is used to simulate |
| 544 | ** Out-Of-Memory (OOM) errors for testing purposes. |
| 545 | */ |
| 546 | static void *oomMalloc(int nByte){ |
| 547 | if( oomCounter ){ |
| 548 | if( oomCounter==1 ){ |
| 549 | oomFault(); |
| 550 | return 0; |
| 551 | }else{ |
| 552 | oomCounter--; |
| 553 | } |
| 554 | } |
| 555 | return defaultMalloc(nByte); |
| 556 | } |
| 557 | |
| 558 | /* Register the OOM simulator. This must occur before any memory |
| 559 | ** allocations */ |
| 560 | static void registerOomSimulator(void){ |
| 561 | sqlite3_mem_methods mem; |
| 562 | sqlite3_shutdown(); |
| 563 | sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem); |
| 564 | defaultMalloc = mem.xMalloc; |
| 565 | mem.xMalloc = oomMalloc; |
| 566 | sqlite3_config(SQLITE_CONFIG_MALLOC, &mem); |
| 567 | } |
| 568 | |
| 569 | /* Turn off any pending OOM simulation */ |
| 570 | static void disableOom(void){ |
| 571 | oomCounter = 0; |
| 572 | oomRepeat = 0; |
| 573 | } |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 574 | |
| 575 | /* |
| 576 | ** Translate a single byte of Hex into an integer. |
| 577 | ** This routine only works if h really is a valid hexadecimal |
| 578 | ** character: 0..9a..fA..F |
| 579 | */ |
drh | ed45703 | 2019-01-25 17:51:06 +0000 | [diff] [blame] | 580 | static unsigned char hexToInt(unsigned int h){ |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 581 | #ifdef SQLITE_EBCDIC |
| 582 | h += 9*(1&~(h>>4)); /* EBCDIC */ |
| 583 | #else |
| 584 | h += 9*(1&(h>>6)); /* ASCII */ |
| 585 | #endif |
| 586 | return h & 0xf; |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | ** The first character of buffer zIn[0..nIn-1] is a '['. This routine |
| 591 | ** checked to see if the buffer holds "[NNNN]" or "[+NNNN]" and if it |
| 592 | ** does it makes corresponding changes to the *pK value and *pI value |
| 593 | ** and returns true. If the input buffer does not match the patterns, |
| 594 | ** no changes are made to either *pK or *pI and this routine returns false. |
| 595 | */ |
| 596 | static int isOffset( |
| 597 | const unsigned char *zIn, /* Text input */ |
| 598 | int nIn, /* Bytes of input */ |
| 599 | unsigned int *pK, /* half-byte cursor to adjust */ |
| 600 | unsigned int *pI /* Input index to adjust */ |
| 601 | ){ |
| 602 | int i; |
| 603 | unsigned int k = 0; |
| 604 | unsigned char c; |
| 605 | for(i=1; i<nIn && (c = zIn[i])!=']'; i++){ |
| 606 | if( !isxdigit(c) ) return 0; |
| 607 | k = k*16 + hexToInt(c); |
| 608 | } |
| 609 | if( i==nIn ) return 0; |
| 610 | *pK = 2*k; |
| 611 | *pI += i; |
| 612 | return 1; |
| 613 | } |
| 614 | |
| 615 | /* |
| 616 | ** Decode the text starting at zIn into a binary database file. |
| 617 | ** The maximum length of zIn is nIn bytes. Compute the binary database |
| 618 | ** file contain in space obtained from sqlite3_malloc(). |
| 619 | ** |
| 620 | ** Return the number of bytes of zIn consumed. Or return -1 if there |
| 621 | ** is an error. One potential error is that the recipe specifies a |
| 622 | ** database file larger than MX_FILE_SZ bytes. |
| 623 | ** |
| 624 | ** Abort on an OOM. |
| 625 | */ |
| 626 | static int decodeDatabase( |
| 627 | const unsigned char *zIn, /* Input text to be decoded */ |
| 628 | int nIn, /* Bytes of input text */ |
| 629 | unsigned char **paDecode, /* OUT: decoded database file */ |
| 630 | int *pnDecode /* OUT: Size of decoded database */ |
| 631 | ){ |
| 632 | unsigned char *a; /* Database under construction */ |
| 633 | int mx = 0; /* Current size of the database */ |
| 634 | sqlite3_uint64 nAlloc = 4096; /* Space allocated in a[] */ |
| 635 | unsigned int i; /* Next byte of zIn[] to read */ |
| 636 | unsigned int j; /* Temporary integer */ |
| 637 | unsigned int k; /* half-byte cursor index for output */ |
| 638 | unsigned int n; /* Number of bytes of input */ |
| 639 | unsigned char b = 0; |
| 640 | if( nIn<4 ) return -1; |
| 641 | n = (unsigned int)nIn; |
drh | ed45703 | 2019-01-25 17:51:06 +0000 | [diff] [blame] | 642 | a = sqlite3_malloc64( nAlloc ); |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 643 | if( a==0 ){ |
| 644 | fprintf(stderr, "Out of memory!\n"); |
| 645 | exit(1); |
| 646 | } |
mistachkin | 065f3bf | 2019-03-20 05:45:03 +0000 | [diff] [blame] | 647 | memset(a, 0, (size_t)nAlloc); |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 648 | for(i=k=0; i<n; i++){ |
drh | af63892 | 2019-02-07 00:17:36 +0000 | [diff] [blame] | 649 | unsigned char c = (unsigned char)zIn[i]; |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 650 | if( isxdigit(c) ){ |
| 651 | k++; |
| 652 | if( k & 1 ){ |
| 653 | b = hexToInt(c)*16; |
| 654 | }else{ |
| 655 | b += hexToInt(c); |
| 656 | j = k/2 - 1; |
| 657 | if( j>=nAlloc ){ |
| 658 | sqlite3_uint64 newSize; |
| 659 | if( nAlloc==MX_FILE_SZ || j>=MX_FILE_SZ ){ |
| 660 | if( eVerbosity ){ |
| 661 | fprintf(stderr, "Input database too big: max %d bytes\n", |
| 662 | MX_FILE_SZ); |
| 663 | } |
| 664 | sqlite3_free(a); |
| 665 | return -1; |
| 666 | } |
| 667 | newSize = nAlloc*2; |
| 668 | if( newSize<=j ){ |
| 669 | newSize = (j+4096)&~4095; |
| 670 | } |
| 671 | if( newSize>MX_FILE_SZ ){ |
| 672 | if( j>=MX_FILE_SZ ){ |
| 673 | sqlite3_free(a); |
| 674 | return -1; |
| 675 | } |
| 676 | newSize = MX_FILE_SZ; |
| 677 | } |
drh | ed45703 | 2019-01-25 17:51:06 +0000 | [diff] [blame] | 678 | a = sqlite3_realloc64( a, newSize ); |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 679 | if( a==0 ){ |
| 680 | fprintf(stderr, "Out of memory!\n"); |
| 681 | exit(1); |
| 682 | } |
| 683 | assert( newSize > nAlloc ); |
mistachkin | 065f3bf | 2019-03-20 05:45:03 +0000 | [diff] [blame] | 684 | memset(a+nAlloc, 0, (size_t)(newSize - nAlloc)); |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 685 | nAlloc = newSize; |
| 686 | } |
| 687 | if( j>=(unsigned)mx ){ |
| 688 | mx = (j + 4095)&~4095; |
| 689 | if( mx>MX_FILE_SZ ) mx = MX_FILE_SZ; |
| 690 | } |
| 691 | assert( j<nAlloc ); |
| 692 | a[j] = b; |
| 693 | } |
| 694 | }else if( zIn[i]=='[' && i<n-3 && isOffset(zIn+i, nIn-i, &k, &i) ){ |
| 695 | continue; |
| 696 | }else if( zIn[i]=='\n' && i<n-4 && memcmp(zIn+i,"\n--\n",4)==0 ){ |
| 697 | i += 4; |
| 698 | break; |
| 699 | } |
| 700 | } |
| 701 | *pnDecode = mx; |
| 702 | *paDecode = a; |
| 703 | return i; |
| 704 | } |
| 705 | |
| 706 | /* |
| 707 | ** Progress handler callback. |
| 708 | ** |
| 709 | ** The argument is the cutoff-time after which all processing should |
| 710 | ** stop. So return non-zero if the cut-off time is exceeded. |
| 711 | */ |
| 712 | static int progress_handler(void *pClientData) { |
| 713 | FuzzCtx *p = (FuzzCtx*)pClientData; |
| 714 | sqlite3_int64 iNow = timeOfDay(); |
| 715 | int rc = iNow>=p->iCutoffTime; |
| 716 | sqlite3_int64 iDiff = iNow - p->iLastCb; |
| 717 | if( iDiff > p->mxInterval ) p->mxInterval = iDiff; |
| 718 | p->nCb++; |
| 719 | if( rc==0 && p->mxCb>0 && p->mxCb<=p->nCb ) rc = 1; |
drh | df21659 | 2019-01-25 04:43:26 +0000 | [diff] [blame] | 720 | if( rc && !p->timeoutHit && eVerbosity>=2 ){ |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 721 | printf("Timeout on progress callback %d\n", p->nCb); |
| 722 | fflush(stdout); |
| 723 | p->timeoutHit = 1; |
| 724 | } |
| 725 | return rc; |
| 726 | } |
| 727 | |
| 728 | /* |
| 729 | ** Disallow debugging pragmas such as "PRAGMA vdbe_debug" and |
| 730 | ** "PRAGMA parser_trace" since they can dramatically increase the |
| 731 | ** amount of output without actually testing anything useful. |
| 732 | ** |
| 733 | ** Also block ATTACH and DETACH |
| 734 | */ |
| 735 | static int block_troublesome_sql( |
| 736 | void *Notused, |
| 737 | int eCode, |
| 738 | const char *zArg1, |
| 739 | const char *zArg2, |
| 740 | const char *zArg3, |
| 741 | const char *zArg4 |
| 742 | ){ |
| 743 | (void)Notused; |
| 744 | (void)zArg2; |
| 745 | (void)zArg3; |
| 746 | (void)zArg4; |
| 747 | if( eCode==SQLITE_PRAGMA ){ |
| 748 | if( sqlite3_strnicmp("vdbe_", zArg1, 5)==0 |
| 749 | || sqlite3_stricmp("parser_trace", zArg1)==0 |
| 750 | || sqlite3_stricmp("temp_store_directory", zArg1)==0 |
| 751 | ){ |
| 752 | return SQLITE_DENY; |
| 753 | } |
drh | 39b3bcf | 2020-03-02 16:31:21 +0000 | [diff] [blame] | 754 | if( sqlite3_stricmp("oom",zArg1)==0 && zArg2!=0 && zArg2[0]!=0 ){ |
| 755 | oomCounter = atoi(zArg2); |
| 756 | } |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 757 | }else if( (eCode==SQLITE_ATTACH || eCode==SQLITE_DETACH) |
| 758 | && zArg1 && zArg1[0] ){ |
| 759 | return SQLITE_DENY; |
| 760 | } |
| 761 | return SQLITE_OK; |
| 762 | } |
| 763 | |
| 764 | /* |
| 765 | ** Run the SQL text |
| 766 | */ |
| 767 | static int runDbSql(sqlite3 *db, const char *zSql){ |
| 768 | int rc; |
| 769 | sqlite3_stmt *pStmt; |
drh | af63892 | 2019-02-07 00:17:36 +0000 | [diff] [blame] | 770 | while( isspace(zSql[0]&0x7f) ) zSql++; |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 771 | if( zSql[0]==0 ) return SQLITE_OK; |
drh | df21659 | 2019-01-25 04:43:26 +0000 | [diff] [blame] | 772 | if( eVerbosity>=4 ){ |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 773 | printf("RUNNING-SQL: [%s]\n", zSql); |
| 774 | fflush(stdout); |
| 775 | } |
| 776 | rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); |
| 777 | if( rc==SQLITE_OK ){ |
| 778 | while( (rc = sqlite3_step(pStmt))==SQLITE_ROW ){ |
drh | df21659 | 2019-01-25 04:43:26 +0000 | [diff] [blame] | 779 | if( eVerbosity>=5 ){ |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 780 | int j; |
| 781 | for(j=0; j<sqlite3_column_count(pStmt); j++){ |
| 782 | if( j ) printf(","); |
| 783 | switch( sqlite3_column_type(pStmt, j) ){ |
| 784 | case SQLITE_NULL: { |
| 785 | printf("NULL"); |
| 786 | break; |
| 787 | } |
| 788 | case SQLITE_INTEGER: |
| 789 | case SQLITE_FLOAT: { |
| 790 | printf("%s", sqlite3_column_text(pStmt, j)); |
| 791 | break; |
| 792 | } |
| 793 | case SQLITE_BLOB: { |
| 794 | int n = sqlite3_column_bytes(pStmt, j); |
| 795 | int i; |
| 796 | const unsigned char *a; |
| 797 | a = (const unsigned char*)sqlite3_column_blob(pStmt, j); |
| 798 | printf("x'"); |
| 799 | for(i=0; i<n; i++){ |
| 800 | printf("%02x", a[i]); |
| 801 | } |
| 802 | printf("'"); |
| 803 | break; |
| 804 | } |
| 805 | case SQLITE_TEXT: { |
| 806 | int n = sqlite3_column_bytes(pStmt, j); |
| 807 | int i; |
| 808 | const unsigned char *a; |
| 809 | a = (const unsigned char*)sqlite3_column_blob(pStmt, j); |
| 810 | printf("'"); |
| 811 | for(i=0; i<n; i++){ |
| 812 | if( a[i]=='\'' ){ |
| 813 | printf("''"); |
| 814 | }else{ |
| 815 | putchar(a[i]); |
| 816 | } |
| 817 | } |
| 818 | printf("'"); |
| 819 | break; |
| 820 | } |
| 821 | } /* End switch() */ |
| 822 | } /* End for() */ |
| 823 | printf("\n"); |
| 824 | fflush(stdout); |
drh | df21659 | 2019-01-25 04:43:26 +0000 | [diff] [blame] | 825 | } /* End if( eVerbosity>=5 ) */ |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 826 | } /* End while( SQLITE_ROW */ |
drh | df21659 | 2019-01-25 04:43:26 +0000 | [diff] [blame] | 827 | if( rc!=SQLITE_DONE && eVerbosity>=4 ){ |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 828 | printf("SQL-ERROR: (%d) %s\n", rc, sqlite3_errmsg(db)); |
| 829 | fflush(stdout); |
| 830 | } |
drh | df21659 | 2019-01-25 04:43:26 +0000 | [diff] [blame] | 831 | }else if( eVerbosity>=4 ){ |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 832 | printf("SQL-ERROR (%d): %s\n", rc, sqlite3_errmsg(db)); |
| 833 | fflush(stdout); |
| 834 | } /* End if( SQLITE_OK ) */ |
| 835 | return sqlite3_finalize(pStmt); |
| 836 | } |
| 837 | |
| 838 | /* Invoke this routine to run a single test case */ |
| 839 | int runCombinedDbSqlInput(const uint8_t *aData, size_t nByte){ |
| 840 | int rc; /* SQLite API return value */ |
| 841 | int iSql; /* Index in aData[] of start of SQL */ |
| 842 | unsigned char *aDb = 0; /* Decoded database content */ |
| 843 | int nDb = 0; /* Size of the decoded database */ |
| 844 | int i; /* Loop counter */ |
| 845 | int j; /* Start of current SQL statement */ |
| 846 | char *zSql = 0; /* SQL text to run */ |
| 847 | int nSql; /* Bytes of SQL text */ |
| 848 | FuzzCtx cx; /* Fuzzing context */ |
| 849 | |
| 850 | if( nByte<10 ) return 0; |
| 851 | if( sqlite3_initialize() ) return 0; |
| 852 | if( sqlite3_memory_used()!=0 ){ |
| 853 | int nAlloc = 0; |
| 854 | int nNotUsed = 0; |
| 855 | sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &nAlloc, &nNotUsed, 0); |
| 856 | fprintf(stderr,"Memory leak in mutator: %lld bytes in %d allocations\n", |
| 857 | sqlite3_memory_used(), nAlloc); |
| 858 | exit(1); |
| 859 | } |
| 860 | memset(&cx, 0, sizeof(cx)); |
| 861 | iSql = decodeDatabase((unsigned char*)aData, (int)nByte, &aDb, &nDb); |
| 862 | if( iSql<0 ) return 0; |
drh | ed45703 | 2019-01-25 17:51:06 +0000 | [diff] [blame] | 863 | nSql = (int)(nByte - iSql); |
drh | df21659 | 2019-01-25 04:43:26 +0000 | [diff] [blame] | 864 | if( eVerbosity>=3 ){ |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 865 | printf( |
| 866 | "****** %d-byte input, %d-byte database, %d-byte script " |
| 867 | "******\n", (int)nByte, nDb, nSql); |
| 868 | fflush(stdout); |
| 869 | } |
| 870 | rc = sqlite3_open(0, &cx.db); |
| 871 | if( rc ) return 1; |
| 872 | if( bVdbeDebug ){ |
| 873 | sqlite3_exec(cx.db, "PRAGMA vdbe_debug=ON", 0, 0, 0); |
| 874 | } |
| 875 | |
| 876 | /* Invoke the progress handler frequently to check to see if we |
| 877 | ** are taking too long. The progress handler will return true |
drh | ed45703 | 2019-01-25 17:51:06 +0000 | [diff] [blame] | 878 | ** (which will block further processing) if more than giTimeout seconds have |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 879 | ** elapsed since the start of the test. |
| 880 | */ |
| 881 | cx.iLastCb = timeOfDay(); |
drh | ed45703 | 2019-01-25 17:51:06 +0000 | [diff] [blame] | 882 | cx.iCutoffTime = cx.iLastCb + giTimeout; /* Now + giTimeout seconds */ |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 883 | cx.mxCb = mxProgressCb; |
| 884 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 885 | sqlite3_progress_handler(cx.db, 10, progress_handler, (void*)&cx); |
| 886 | #endif |
| 887 | |
| 888 | /* Set a limit on the maximum size of a prepared statement, and the |
| 889 | ** maximum length of a string or blob */ |
| 890 | if( vdbeOpLimit>0 ){ |
| 891 | sqlite3_limit(cx.db, SQLITE_LIMIT_VDBE_OP, vdbeOpLimit); |
| 892 | } |
| 893 | if( lengthLimit>0 ){ |
| 894 | sqlite3_limit(cx.db, SQLITE_LIMIT_LENGTH, lengthLimit); |
| 895 | } |
drh | be03cc9 | 2020-01-20 14:42:09 +0000 | [diff] [blame] | 896 | if( depthLimit>0 ){ |
| 897 | sqlite3_limit(cx.db, SQLITE_LIMIT_EXPR_DEPTH, depthLimit); |
| 898 | } |
drh | 31999c5 | 2019-11-14 17:46:32 +0000 | [diff] [blame] | 899 | sqlite3_hard_heap_limit64(heapLimit); |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 900 | |
| 901 | if( nDb>=20 && aDb[18]==2 && aDb[19]==2 ){ |
| 902 | aDb[18] = aDb[19] = 1; |
| 903 | } |
| 904 | rc = sqlite3_deserialize(cx.db, "main", aDb, nDb, nDb, |
| 905 | SQLITE_DESERIALIZE_RESIZEABLE | |
| 906 | SQLITE_DESERIALIZE_FREEONCLOSE); |
| 907 | if( rc ){ |
| 908 | fprintf(stderr, "sqlite3_deserialize() failed with %d\n", rc); |
| 909 | goto testrun_finished; |
| 910 | } |
| 911 | if( maxDbSize>0 ){ |
| 912 | sqlite3_int64 x = maxDbSize; |
| 913 | sqlite3_file_control(cx.db, "main", SQLITE_FCNTL_SIZE_LIMIT, &x); |
| 914 | } |
| 915 | |
drh | 725a9c7 | 2019-01-25 13:03:38 +0000 | [diff] [blame] | 916 | /* For high debugging levels, turn on debug mode */ |
| 917 | if( eVerbosity>=5 ){ |
| 918 | sqlite3_exec(cx.db, "PRAGMA vdbe_debug=ON;", 0, 0, 0); |
| 919 | } |
| 920 | |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 921 | /* Block debug pragmas and ATTACH/DETACH. But wait until after |
| 922 | ** deserialize to do this because deserialize depends on ATTACH */ |
| 923 | sqlite3_set_authorizer(cx.db, block_troublesome_sql, 0); |
| 924 | |
| 925 | /* Consistent PRNG seed */ |
| 926 | sqlite3_randomness(0,0); |
| 927 | |
| 928 | zSql = sqlite3_malloc( nSql + 1 ); |
| 929 | if( zSql==0 ){ |
| 930 | fprintf(stderr, "Out of memory!\n"); |
| 931 | }else{ |
| 932 | memcpy(zSql, aData+iSql, nSql); |
| 933 | zSql[nSql] = 0; |
| 934 | for(i=j=0; zSql[i]; i++){ |
| 935 | if( zSql[i]==';' ){ |
| 936 | char cSaved = zSql[i+1]; |
| 937 | zSql[i+1] = 0; |
| 938 | if( sqlite3_complete(zSql+j) ){ |
| 939 | rc = runDbSql(cx.db, zSql+j); |
| 940 | j = i+1; |
| 941 | } |
| 942 | zSql[i+1] = cSaved; |
| 943 | if( rc==SQLITE_INTERRUPT || progress_handler(&cx) ){ |
| 944 | goto testrun_finished; |
| 945 | } |
| 946 | } |
| 947 | } |
| 948 | if( j<i ){ |
| 949 | runDbSql(cx.db, zSql+j); |
| 950 | } |
| 951 | } |
| 952 | testrun_finished: |
| 953 | sqlite3_free(zSql); |
| 954 | rc = sqlite3_close(cx.db); |
| 955 | if( rc!=SQLITE_OK ){ |
| 956 | fprintf(stdout, "sqlite3_close() returns %d\n", rc); |
| 957 | } |
drh | df21659 | 2019-01-25 04:43:26 +0000 | [diff] [blame] | 958 | if( eVerbosity>=2 ){ |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 959 | fprintf(stdout, "Peak memory usages: %f MB\n", |
| 960 | sqlite3_memory_highwater(1) / 1000000.0); |
| 961 | } |
| 962 | if( sqlite3_memory_used()!=0 ){ |
| 963 | int nAlloc = 0; |
| 964 | int nNotUsed = 0; |
| 965 | sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &nAlloc, &nNotUsed, 0); |
| 966 | fprintf(stderr,"Memory leak: %lld bytes in %d allocations\n", |
| 967 | sqlite3_memory_used(), nAlloc); |
| 968 | exit(1); |
| 969 | } |
| 970 | return 0; |
| 971 | } |
| 972 | |
| 973 | /* |
| 974 | ** END of the dbsqlfuzz code |
| 975 | ***************************************************************************/ |
| 976 | |
| 977 | /* Look at a SQL text and try to determine if it begins with a database |
| 978 | ** description, such as would be found in a dbsqlfuzz test case. Return |
| 979 | ** true if this does appear to be a dbsqlfuzz test case and false otherwise. |
| 980 | */ |
| 981 | static int isDbSql(unsigned char *a, int n){ |
drh | df21659 | 2019-01-25 04:43:26 +0000 | [diff] [blame] | 982 | unsigned char buf[12]; |
| 983 | int i; |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 984 | if( n>4 && memcmp(a,"\n--\n",4)==0 ) return 1; |
| 985 | while( n>0 && isspace(a[0]) ){ a++; n--; } |
drh | df21659 | 2019-01-25 04:43:26 +0000 | [diff] [blame] | 986 | for(i=0; n>0 && i<8; n--, a++){ |
| 987 | if( isxdigit(a[0]) ) buf[i++] = a[0]; |
| 988 | } |
| 989 | if( i==8 && memcmp(buf,"53514c69",8)==0 ) return 1; |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 990 | return 0; |
| 991 | } |
| 992 | |
drh | e5da935 | 2019-01-27 01:11:40 +0000 | [diff] [blame] | 993 | /* Implementation of the isdbsql(TEXT) SQL function. |
| 994 | */ |
| 995 | static void isDbSqlFunc( |
| 996 | sqlite3_context *context, |
| 997 | int argc, |
| 998 | sqlite3_value **argv |
| 999 | ){ |
| 1000 | int n = sqlite3_value_bytes(argv[0]); |
| 1001 | unsigned char *a = (unsigned char*)sqlite3_value_blob(argv[0]); |
| 1002 | sqlite3_result_int(context, a!=0 && n>0 && isDbSql(a,n)); |
| 1003 | } |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 1004 | |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1005 | /* Methods for the VHandle object |
| 1006 | */ |
| 1007 | static int inmemClose(sqlite3_file *pFile){ |
| 1008 | VHandle *p = (VHandle*)pFile; |
| 1009 | VFile *pVFile = p->pVFile; |
| 1010 | pVFile->nRef--; |
| 1011 | if( pVFile->nRef==0 && pVFile->zFilename==0 ){ |
| 1012 | pVFile->sz = -1; |
| 1013 | free(pVFile->a); |
| 1014 | pVFile->a = 0; |
| 1015 | } |
| 1016 | return SQLITE_OK; |
| 1017 | } |
| 1018 | static int inmemRead( |
| 1019 | sqlite3_file *pFile, /* Read from this open file */ |
| 1020 | void *pData, /* Store content in this buffer */ |
| 1021 | int iAmt, /* Bytes of content */ |
| 1022 | sqlite3_int64 iOfst /* Start reading here */ |
| 1023 | ){ |
| 1024 | VHandle *pHandle = (VHandle*)pFile; |
| 1025 | VFile *pVFile = pHandle->pVFile; |
| 1026 | if( iOfst<0 || iOfst>=pVFile->sz ){ |
| 1027 | memset(pData, 0, iAmt); |
| 1028 | return SQLITE_IOERR_SHORT_READ; |
| 1029 | } |
| 1030 | if( iOfst+iAmt>pVFile->sz ){ |
| 1031 | memset(pData, 0, iAmt); |
drh | 1573dc3 | 2015-05-25 22:29:26 +0000 | [diff] [blame] | 1032 | iAmt = (int)(pVFile->sz - iOfst); |
drh | e45985b | 2018-12-14 02:29:56 +0000 | [diff] [blame] | 1033 | memcpy(pData, pVFile->a + iOfst, iAmt); |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1034 | return SQLITE_IOERR_SHORT_READ; |
| 1035 | } |
drh | aca7ea1 | 2015-05-25 23:14:37 +0000 | [diff] [blame] | 1036 | memcpy(pData, pVFile->a + iOfst, iAmt); |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1037 | return SQLITE_OK; |
| 1038 | } |
| 1039 | static int inmemWrite( |
| 1040 | sqlite3_file *pFile, /* Write to this file */ |
| 1041 | const void *pData, /* Content to write */ |
| 1042 | int iAmt, /* bytes to write */ |
| 1043 | sqlite3_int64 iOfst /* Start writing here */ |
| 1044 | ){ |
| 1045 | VHandle *pHandle = (VHandle*)pFile; |
| 1046 | VFile *pVFile = pHandle->pVFile; |
| 1047 | if( iOfst+iAmt > pVFile->sz ){ |
drh | a9542b1 | 2015-05-25 19:35:42 +0000 | [diff] [blame] | 1048 | if( iOfst+iAmt >= MX_FILE_SZ ){ |
| 1049 | return SQLITE_FULL; |
| 1050 | } |
drh | 1573dc3 | 2015-05-25 22:29:26 +0000 | [diff] [blame] | 1051 | pVFile->a = safe_realloc(pVFile->a, (int)(iOfst+iAmt)); |
drh | 908aced | 2015-05-26 16:12:45 +0000 | [diff] [blame] | 1052 | if( iOfst > pVFile->sz ){ |
| 1053 | memset(pVFile->a + pVFile->sz, 0, (int)(iOfst - pVFile->sz)); |
| 1054 | } |
drh | 1573dc3 | 2015-05-25 22:29:26 +0000 | [diff] [blame] | 1055 | pVFile->sz = (int)(iOfst + iAmt); |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1056 | } |
| 1057 | memcpy(pVFile->a + iOfst, pData, iAmt); |
| 1058 | return SQLITE_OK; |
| 1059 | } |
| 1060 | static int inmemTruncate(sqlite3_file *pFile, sqlite3_int64 iSize){ |
| 1061 | VHandle *pHandle = (VHandle*)pFile; |
| 1062 | VFile *pVFile = pHandle->pVFile; |
drh | 1573dc3 | 2015-05-25 22:29:26 +0000 | [diff] [blame] | 1063 | if( pVFile->sz>iSize && iSize>=0 ) pVFile->sz = (int)iSize; |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1064 | return SQLITE_OK; |
| 1065 | } |
| 1066 | static int inmemSync(sqlite3_file *pFile, int flags){ |
| 1067 | return SQLITE_OK; |
| 1068 | } |
| 1069 | static int inmemFileSize(sqlite3_file *pFile, sqlite3_int64 *pSize){ |
| 1070 | *pSize = ((VHandle*)pFile)->pVFile->sz; |
| 1071 | return SQLITE_OK; |
| 1072 | } |
| 1073 | static int inmemLock(sqlite3_file *pFile, int type){ |
| 1074 | return SQLITE_OK; |
| 1075 | } |
| 1076 | static int inmemUnlock(sqlite3_file *pFile, int type){ |
| 1077 | return SQLITE_OK; |
| 1078 | } |
| 1079 | static int inmemCheckReservedLock(sqlite3_file *pFile, int *pOut){ |
| 1080 | *pOut = 0; |
| 1081 | return SQLITE_OK; |
| 1082 | } |
| 1083 | static int inmemFileControl(sqlite3_file *pFile, int op, void *pArg){ |
| 1084 | return SQLITE_NOTFOUND; |
| 1085 | } |
| 1086 | static int inmemSectorSize(sqlite3_file *pFile){ |
| 1087 | return 512; |
| 1088 | } |
| 1089 | static int inmemDeviceCharacteristics(sqlite3_file *pFile){ |
| 1090 | return |
| 1091 | SQLITE_IOCAP_SAFE_APPEND | |
| 1092 | SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN | |
| 1093 | SQLITE_IOCAP_POWERSAFE_OVERWRITE; |
| 1094 | } |
| 1095 | |
| 1096 | |
| 1097 | /* Method table for VHandle |
| 1098 | */ |
| 1099 | static sqlite3_io_methods VHandleMethods = { |
| 1100 | /* iVersion */ 1, |
| 1101 | /* xClose */ inmemClose, |
| 1102 | /* xRead */ inmemRead, |
| 1103 | /* xWrite */ inmemWrite, |
| 1104 | /* xTruncate */ inmemTruncate, |
| 1105 | /* xSync */ inmemSync, |
| 1106 | /* xFileSize */ inmemFileSize, |
| 1107 | /* xLock */ inmemLock, |
| 1108 | /* xUnlock */ inmemUnlock, |
| 1109 | /* xCheck... */ inmemCheckReservedLock, |
| 1110 | /* xFileCtrl */ inmemFileControl, |
| 1111 | /* xSectorSz */ inmemSectorSize, |
| 1112 | /* xDevchar */ inmemDeviceCharacteristics, |
| 1113 | /* xShmMap */ 0, |
| 1114 | /* xShmLock */ 0, |
| 1115 | /* xShmBarrier */ 0, |
| 1116 | /* xShmUnmap */ 0, |
| 1117 | /* xFetch */ 0, |
| 1118 | /* xUnfetch */ 0 |
| 1119 | }; |
| 1120 | |
| 1121 | /* |
| 1122 | ** Open a new file in the inmem VFS. All files are anonymous and are |
| 1123 | ** delete-on-close. |
| 1124 | */ |
| 1125 | static int inmemOpen( |
| 1126 | sqlite3_vfs *pVfs, |
| 1127 | const char *zFilename, |
| 1128 | sqlite3_file *pFile, |
| 1129 | int openFlags, |
| 1130 | int *pOutFlags |
| 1131 | ){ |
| 1132 | VFile *pVFile = createVFile(zFilename, 0, (unsigned char*)""); |
| 1133 | VHandle *pHandle = (VHandle*)pFile; |
drh | a9542b1 | 2015-05-25 19:35:42 +0000 | [diff] [blame] | 1134 | if( pVFile==0 ){ |
| 1135 | return SQLITE_FULL; |
| 1136 | } |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1137 | pHandle->pVFile = pVFile; |
| 1138 | pVFile->nRef++; |
| 1139 | pFile->pMethods = &VHandleMethods; |
| 1140 | if( pOutFlags ) *pOutFlags = openFlags; |
| 1141 | return SQLITE_OK; |
| 1142 | } |
| 1143 | |
| 1144 | /* |
| 1145 | ** Delete a file by name |
| 1146 | */ |
| 1147 | static int inmemDelete( |
| 1148 | sqlite3_vfs *pVfs, |
| 1149 | const char *zFilename, |
| 1150 | int syncdir |
| 1151 | ){ |
| 1152 | VFile *pVFile = findVFile(zFilename); |
| 1153 | if( pVFile==0 ) return SQLITE_OK; |
| 1154 | if( pVFile->nRef==0 ){ |
| 1155 | free(pVFile->zFilename); |
| 1156 | pVFile->zFilename = 0; |
| 1157 | pVFile->sz = -1; |
| 1158 | free(pVFile->a); |
| 1159 | pVFile->a = 0; |
| 1160 | return SQLITE_OK; |
| 1161 | } |
| 1162 | return SQLITE_IOERR_DELETE; |
| 1163 | } |
| 1164 | |
| 1165 | /* Check for the existance of a file |
| 1166 | */ |
| 1167 | static int inmemAccess( |
| 1168 | sqlite3_vfs *pVfs, |
| 1169 | const char *zFilename, |
| 1170 | int flags, |
| 1171 | int *pResOut |
| 1172 | ){ |
| 1173 | VFile *pVFile = findVFile(zFilename); |
| 1174 | *pResOut = pVFile!=0; |
| 1175 | return SQLITE_OK; |
| 1176 | } |
| 1177 | |
| 1178 | /* Get the canonical pathname for a file |
| 1179 | */ |
| 1180 | static int inmemFullPathname( |
| 1181 | sqlite3_vfs *pVfs, |
| 1182 | const char *zFilename, |
| 1183 | int nOut, |
| 1184 | char *zOut |
| 1185 | ){ |
| 1186 | sqlite3_snprintf(nOut, zOut, "%s", zFilename); |
| 1187 | return SQLITE_OK; |
| 1188 | } |
| 1189 | |
drh | beaf514 | 2016-12-26 00:15:56 +0000 | [diff] [blame] | 1190 | /* Always use the same random see, for repeatability. |
| 1191 | */ |
| 1192 | static int inmemRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ |
| 1193 | memset(zBuf, 0, nBuf); |
| 1194 | memcpy(zBuf, &g.uRandom, nBuf<sizeof(g.uRandom) ? nBuf : sizeof(g.uRandom)); |
| 1195 | return nBuf; |
| 1196 | } |
| 1197 | |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1198 | /* |
| 1199 | ** Register the VFS that reads from the g.aFile[] set of files. |
| 1200 | */ |
drh | beaf514 | 2016-12-26 00:15:56 +0000 | [diff] [blame] | 1201 | static void inmemVfsRegister(int makeDefault){ |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1202 | static sqlite3_vfs inmemVfs; |
| 1203 | sqlite3_vfs *pDefault = sqlite3_vfs_find(0); |
drh | 5337dac | 2015-11-25 15:15:03 +0000 | [diff] [blame] | 1204 | inmemVfs.iVersion = 3; |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1205 | inmemVfs.szOsFile = sizeof(VHandle); |
| 1206 | inmemVfs.mxPathname = 200; |
| 1207 | inmemVfs.zName = "inmem"; |
| 1208 | inmemVfs.xOpen = inmemOpen; |
| 1209 | inmemVfs.xDelete = inmemDelete; |
| 1210 | inmemVfs.xAccess = inmemAccess; |
| 1211 | inmemVfs.xFullPathname = inmemFullPathname; |
drh | beaf514 | 2016-12-26 00:15:56 +0000 | [diff] [blame] | 1212 | inmemVfs.xRandomness = inmemRandomness; |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1213 | inmemVfs.xSleep = pDefault->xSleep; |
drh | 5337dac | 2015-11-25 15:15:03 +0000 | [diff] [blame] | 1214 | inmemVfs.xCurrentTimeInt64 = pDefault->xCurrentTimeInt64; |
drh | beaf514 | 2016-12-26 00:15:56 +0000 | [diff] [blame] | 1215 | sqlite3_vfs_register(&inmemVfs, makeDefault); |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1216 | }; |
| 1217 | |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1218 | /* |
drh | e5c5f2c | 2015-05-26 00:28:08 +0000 | [diff] [blame] | 1219 | ** Allowed values for the runFlags parameter to runSql() |
| 1220 | */ |
| 1221 | #define SQL_TRACE 0x0001 /* Print each SQL statement as it is prepared */ |
| 1222 | #define SQL_OUTPUT 0x0002 /* Show the SQL output */ |
| 1223 | |
| 1224 | /* |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1225 | ** Run multiple commands of SQL. Similar to sqlite3_exec(), but does not |
| 1226 | ** stop if an error is encountered. |
| 1227 | */ |
drh | e5c5f2c | 2015-05-26 00:28:08 +0000 | [diff] [blame] | 1228 | static void runSql(sqlite3 *db, const char *zSql, unsigned runFlags){ |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1229 | const char *zMore; |
| 1230 | sqlite3_stmt *pStmt; |
| 1231 | |
| 1232 | while( zSql && zSql[0] ){ |
| 1233 | zMore = 0; |
| 1234 | pStmt = 0; |
| 1235 | sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zMore); |
drh | 4ab3147 | 2015-05-25 22:17:06 +0000 | [diff] [blame] | 1236 | if( zMore==zSql ) break; |
drh | e5c5f2c | 2015-05-26 00:28:08 +0000 | [diff] [blame] | 1237 | if( runFlags & SQL_TRACE ){ |
drh | 4ab3147 | 2015-05-25 22:17:06 +0000 | [diff] [blame] | 1238 | const char *z = zSql; |
| 1239 | int n; |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 1240 | while( z<zMore && ISSPACE(z[0]) ) z++; |
drh | 4ab3147 | 2015-05-25 22:17:06 +0000 | [diff] [blame] | 1241 | n = (int)(zMore - z); |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 1242 | while( n>0 && ISSPACE(z[n-1]) ) n--; |
drh | 4ab3147 | 2015-05-25 22:17:06 +0000 | [diff] [blame] | 1243 | if( n==0 ) break; |
| 1244 | if( pStmt==0 ){ |
| 1245 | printf("TRACE: %.*s (error: %s)\n", n, z, sqlite3_errmsg(db)); |
| 1246 | }else{ |
| 1247 | printf("TRACE: %.*s\n", n, z); |
| 1248 | } |
| 1249 | } |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1250 | zSql = zMore; |
| 1251 | if( pStmt ){ |
drh | e5c5f2c | 2015-05-26 00:28:08 +0000 | [diff] [blame] | 1252 | if( (runFlags & SQL_OUTPUT)==0 ){ |
| 1253 | while( SQLITE_ROW==sqlite3_step(pStmt) ){} |
| 1254 | }else{ |
| 1255 | int nCol = -1; |
| 1256 | while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 1257 | int i; |
| 1258 | if( nCol<0 ){ |
| 1259 | nCol = sqlite3_column_count(pStmt); |
| 1260 | }else if( nCol>0 ){ |
| 1261 | printf("--------------------------------------------\n"); |
| 1262 | } |
| 1263 | for(i=0; i<nCol; i++){ |
| 1264 | int eType = sqlite3_column_type(pStmt,i); |
| 1265 | printf("%s = ", sqlite3_column_name(pStmt,i)); |
| 1266 | switch( eType ){ |
| 1267 | case SQLITE_NULL: { |
| 1268 | printf("NULL\n"); |
| 1269 | break; |
| 1270 | } |
| 1271 | case SQLITE_INTEGER: { |
| 1272 | printf("INT %s\n", sqlite3_column_text(pStmt,i)); |
| 1273 | break; |
| 1274 | } |
| 1275 | case SQLITE_FLOAT: { |
| 1276 | printf("FLOAT %s\n", sqlite3_column_text(pStmt,i)); |
| 1277 | break; |
| 1278 | } |
| 1279 | case SQLITE_TEXT: { |
| 1280 | printf("TEXT [%s]\n", sqlite3_column_text(pStmt,i)); |
| 1281 | break; |
| 1282 | } |
| 1283 | case SQLITE_BLOB: { |
| 1284 | printf("BLOB (%d bytes)\n", sqlite3_column_bytes(pStmt,i)); |
| 1285 | break; |
| 1286 | } |
| 1287 | } |
| 1288 | } |
| 1289 | } |
| 1290 | } |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1291 | sqlite3_finalize(pStmt); |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1292 | } |
| 1293 | } |
| 1294 | } |
| 1295 | |
drh | a9542b1 | 2015-05-25 19:35:42 +0000 | [diff] [blame] | 1296 | /* |
drh | 9a64586 | 2015-06-24 12:44:42 +0000 | [diff] [blame] | 1297 | ** Rebuild the database file. |
| 1298 | ** |
| 1299 | ** (1) Remove duplicate entries |
| 1300 | ** (2) Put all entries in order |
| 1301 | ** (3) Vacuum |
| 1302 | */ |
drh | e5da935 | 2019-01-27 01:11:40 +0000 | [diff] [blame] | 1303 | static void rebuild_database(sqlite3 *db, int dbSqlOnly){ |
drh | 9a64586 | 2015-06-24 12:44:42 +0000 | [diff] [blame] | 1304 | int rc; |
drh | e5da935 | 2019-01-27 01:11:40 +0000 | [diff] [blame] | 1305 | char *zSql; |
| 1306 | zSql = sqlite3_mprintf( |
drh | 9a64586 | 2015-06-24 12:44:42 +0000 | [diff] [blame] | 1307 | "BEGIN;\n" |
| 1308 | "CREATE TEMP TABLE dbx AS SELECT DISTINCT dbcontent FROM db;\n" |
| 1309 | "DELETE FROM db;\n" |
drh | 5ecf903 | 2018-05-08 12:49:53 +0000 | [diff] [blame] | 1310 | "INSERT INTO db(dbid, dbcontent) " |
| 1311 | " SELECT NULL, dbcontent FROM dbx ORDER BY 2;\n" |
drh | 9a64586 | 2015-06-24 12:44:42 +0000 | [diff] [blame] | 1312 | "DROP TABLE dbx;\n" |
drh | e5da935 | 2019-01-27 01:11:40 +0000 | [diff] [blame] | 1313 | "CREATE TEMP TABLE sx AS SELECT DISTINCT sqltext FROM xsql %s;\n" |
drh | 9a64586 | 2015-06-24 12:44:42 +0000 | [diff] [blame] | 1314 | "DELETE FROM xsql;\n" |
drh | 5ecf903 | 2018-05-08 12:49:53 +0000 | [diff] [blame] | 1315 | "INSERT INTO xsql(sqlid,sqltext) " |
| 1316 | " SELECT NULL, sqltext FROM sx ORDER BY 2;\n" |
drh | 9a64586 | 2015-06-24 12:44:42 +0000 | [diff] [blame] | 1317 | "DROP TABLE sx;\n" |
| 1318 | "COMMIT;\n" |
| 1319 | "PRAGMA page_size=1024;\n" |
drh | e5da935 | 2019-01-27 01:11:40 +0000 | [diff] [blame] | 1320 | "VACUUM;\n", |
| 1321 | dbSqlOnly ? " WHERE isdbsql(sqltext)" : "" |
| 1322 | ); |
| 1323 | rc = sqlite3_exec(db, zSql, 0, 0, 0); |
| 1324 | sqlite3_free(zSql); |
drh | 9a64586 | 2015-06-24 12:44:42 +0000 | [diff] [blame] | 1325 | if( rc ) fatalError("cannot rebuild: %s", sqlite3_errmsg(db)); |
| 1326 | } |
| 1327 | |
| 1328 | /* |
drh | 53e66c3 | 2015-07-24 15:49:23 +0000 | [diff] [blame] | 1329 | ** Return the value of a hexadecimal digit. Return -1 if the input |
| 1330 | ** is not a hex digit. |
| 1331 | */ |
| 1332 | static int hexDigitValue(char c){ |
| 1333 | if( c>='0' && c<='9' ) return c - '0'; |
| 1334 | if( c>='a' && c<='f' ) return c - 'a' + 10; |
| 1335 | if( c>='A' && c<='F' ) return c - 'A' + 10; |
| 1336 | return -1; |
| 1337 | } |
| 1338 | |
| 1339 | /* |
| 1340 | ** Interpret zArg as an integer value, possibly with suffixes. |
| 1341 | */ |
| 1342 | static int integerValue(const char *zArg){ |
| 1343 | sqlite3_int64 v = 0; |
| 1344 | static const struct { char *zSuffix; int iMult; } aMult[] = { |
| 1345 | { "KiB", 1024 }, |
| 1346 | { "MiB", 1024*1024 }, |
| 1347 | { "GiB", 1024*1024*1024 }, |
| 1348 | { "KB", 1000 }, |
| 1349 | { "MB", 1000000 }, |
| 1350 | { "GB", 1000000000 }, |
| 1351 | { "K", 1000 }, |
| 1352 | { "M", 1000000 }, |
| 1353 | { "G", 1000000000 }, |
| 1354 | }; |
| 1355 | int i; |
| 1356 | int isNeg = 0; |
| 1357 | if( zArg[0]=='-' ){ |
| 1358 | isNeg = 1; |
| 1359 | zArg++; |
| 1360 | }else if( zArg[0]=='+' ){ |
| 1361 | zArg++; |
| 1362 | } |
| 1363 | if( zArg[0]=='0' && zArg[1]=='x' ){ |
| 1364 | int x; |
| 1365 | zArg += 2; |
| 1366 | while( (x = hexDigitValue(zArg[0]))>=0 ){ |
| 1367 | v = (v<<4) + x; |
| 1368 | zArg++; |
| 1369 | } |
| 1370 | }else{ |
drh | c56fac7 | 2015-10-29 13:48:15 +0000 | [diff] [blame] | 1371 | while( ISDIGIT(zArg[0]) ){ |
drh | 53e66c3 | 2015-07-24 15:49:23 +0000 | [diff] [blame] | 1372 | v = v*10 + zArg[0] - '0'; |
| 1373 | zArg++; |
| 1374 | } |
| 1375 | } |
| 1376 | for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){ |
| 1377 | if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){ |
| 1378 | v *= aMult[i].iMult; |
| 1379 | break; |
| 1380 | } |
| 1381 | } |
| 1382 | if( v>0x7fffffff ) fatalError("parameter too large - max 2147483648"); |
| 1383 | return (int)(isNeg? -v : v); |
| 1384 | } |
| 1385 | |
| 1386 | /* |
drh | 725a9c7 | 2019-01-25 13:03:38 +0000 | [diff] [blame] | 1387 | ** Return the number of "v" characters in a string. Return 0 if there |
| 1388 | ** are any characters in the string other than "v". |
| 1389 | */ |
| 1390 | static int numberOfVChar(const char *z){ |
| 1391 | int N = 0; |
| 1392 | while( z[0] && z[0]=='v' ){ |
| 1393 | z++; |
| 1394 | N++; |
| 1395 | } |
| 1396 | return z[0]==0 ? N : 0; |
| 1397 | } |
| 1398 | |
| 1399 | /* |
drh | a9542b1 | 2015-05-25 19:35:42 +0000 | [diff] [blame] | 1400 | ** Print sketchy documentation for this utility program |
| 1401 | */ |
| 1402 | static void showHelp(void){ |
| 1403 | printf("Usage: %s [options] SOURCE-DB ?ARGS...?\n", g.zArgv0); |
| 1404 | printf( |
| 1405 | "Read databases and SQL scripts from SOURCE-DB and execute each script against\n" |
| 1406 | "each database, checking for crashes and memory leaks.\n" |
| 1407 | "Options:\n" |
drh | a36e01a | 2016-08-03 13:40:54 +0000 | [diff] [blame] | 1408 | " --cell-size-check Set the PRAGMA cell_size_check=ON\n" |
| 1409 | " --dbid N Use only the database where dbid=N\n" |
| 1410 | " --export-db DIR Write databases to files(s) in DIR. Works with --dbid\n" |
| 1411 | " --export-sql DIR Write SQL to file(s) in DIR. Also works with --sqlid\n" |
| 1412 | " --help Show this help text\n" |
drh | 5180d68 | 2018-08-06 01:39:31 +0000 | [diff] [blame] | 1413 | " --info Show information about SOURCE-DB w/o running tests\n" |
drh | be03cc9 | 2020-01-20 14:42:09 +0000 | [diff] [blame] | 1414 | " --limit-depth N Limit expression depth to N\n" |
drh | a36e01a | 2016-08-03 13:40:54 +0000 | [diff] [blame] | 1415 | " --limit-mem N Limit memory used by test SQLite instance to N bytes\n" |
| 1416 | " --limit-vdbe Panic if any test runs for more than 100,000 cycles\n" |
drh | 5ecf903 | 2018-05-08 12:49:53 +0000 | [diff] [blame] | 1417 | " --load-sql ARGS... Load SQL scripts fron files into SOURCE-DB\n" |
drh | a36e01a | 2016-08-03 13:40:54 +0000 | [diff] [blame] | 1418 | " --load-db ARGS... Load template databases from files into SOURCE_DB\n" |
drh | e5da935 | 2019-01-27 01:11:40 +0000 | [diff] [blame] | 1419 | " --load-dbsql ARGS.. Load dbsqlfuzz outputs into the xsql table\n" |
drh | a36e01a | 2016-08-03 13:40:54 +0000 | [diff] [blame] | 1420 | " -m TEXT Add a description to the database\n" |
| 1421 | " --native-vfs Use the native VFS for initially empty database files\n" |
drh | 174f855 | 2017-03-20 22:58:27 +0000 | [diff] [blame] | 1422 | " --native-malloc Turn off MEMSYS3/5 and Lookaside\n" |
drh | ea432ba | 2016-11-11 16:33:47 +0000 | [diff] [blame] | 1423 | " --oss-fuzz Enable OSS-FUZZ testing\n" |
drh | beaf514 | 2016-12-26 00:15:56 +0000 | [diff] [blame] | 1424 | " --prng-seed N Seed value for the PRGN inside of SQLite\n" |
drh | 5180d68 | 2018-08-06 01:39:31 +0000 | [diff] [blame] | 1425 | " -q|--quiet Reduced output\n" |
drh | a36e01a | 2016-08-03 13:40:54 +0000 | [diff] [blame] | 1426 | " --rebuild Rebuild and vacuum the database file\n" |
| 1427 | " --result-trace Show the results of each SQL command\n" |
| 1428 | " --sqlid N Use only SQL where sqlid=N\n" |
| 1429 | " --timeout N Abort if any single test needs more than N seconds\n" |
| 1430 | " -v|--verbose Increased output. Repeat for more output.\n" |
drh | 6e1c45e | 2019-12-18 13:42:04 +0000 | [diff] [blame] | 1431 | " --vdbe-debug Activate VDBE debugging.\n" |
drh | a9542b1 | 2015-05-25 19:35:42 +0000 | [diff] [blame] | 1432 | ); |
| 1433 | } |
| 1434 | |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1435 | int main(int argc, char **argv){ |
| 1436 | sqlite3_int64 iBegin; /* Start time of this program */ |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1437 | int quietFlag = 0; /* True if --quiet or -q */ |
| 1438 | int verboseFlag = 0; /* True if --verbose or -v */ |
| 1439 | char *zInsSql = 0; /* SQL statement for --load-db or --load-sql */ |
drh | 5ecf903 | 2018-05-08 12:49:53 +0000 | [diff] [blame] | 1440 | int iFirstInsArg = 0; /* First argv[] for --load-db or --load-sql */ |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1441 | sqlite3 *db = 0; /* The open database connection */ |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1442 | sqlite3_stmt *pStmt; /* A prepared statement */ |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1443 | int rc; /* Result code from SQLite interface calls */ |
| 1444 | Blob *pSql; /* For looping over SQL scripts */ |
| 1445 | Blob *pDb; /* For looping over template databases */ |
| 1446 | int i; /* Loop index for the argv[] loop */ |
drh | e5da935 | 2019-01-27 01:11:40 +0000 | [diff] [blame] | 1447 | int dbSqlOnly = 0; /* Only use scripts that are dbsqlfuzz */ |
drh | a9542b1 | 2015-05-25 19:35:42 +0000 | [diff] [blame] | 1448 | int onlySqlid = -1; /* --sqlid */ |
| 1449 | int onlyDbid = -1; /* --dbid */ |
drh | 15b3128 | 2015-05-25 21:59:05 +0000 | [diff] [blame] | 1450 | int nativeFlag = 0; /* --native-vfs */ |
drh | 9a64586 | 2015-06-24 12:44:42 +0000 | [diff] [blame] | 1451 | int rebuildFlag = 0; /* --rebuild */ |
drh | d83e283 | 2015-06-24 14:45:44 +0000 | [diff] [blame] | 1452 | int vdbeLimitFlag = 0; /* --limit-vdbe */ |
drh | 5180d68 | 2018-08-06 01:39:31 +0000 | [diff] [blame] | 1453 | int infoFlag = 0; /* --info */ |
drh | 94701b0 | 2015-06-24 13:25:34 +0000 | [diff] [blame] | 1454 | int timeoutTest = 0; /* undocumented --timeout-test flag */ |
drh | e5c5f2c | 2015-05-26 00:28:08 +0000 | [diff] [blame] | 1455 | int runFlags = 0; /* Flags sent to runSql() */ |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1456 | char *zMsg = 0; /* Add this message */ |
| 1457 | int nSrcDb = 0; /* Number of source databases */ |
| 1458 | char **azSrcDb = 0; /* Array of source database names */ |
| 1459 | int iSrcDb; /* Loop over all source databases */ |
| 1460 | int nTest = 0; /* Total number of tests performed */ |
| 1461 | char *zDbName = ""; /* Appreviated name of a source database */ |
drh | 5ecf903 | 2018-05-08 12:49:53 +0000 | [diff] [blame] | 1462 | const char *zFailCode = 0; /* Value of the TEST_FAILURE env variable */ |
drh | 1421d98 | 2015-05-27 03:46:18 +0000 | [diff] [blame] | 1463 | int cellSzCkFlag = 0; /* --cell-size-check */ |
drh | 5ecf903 | 2018-05-08 12:49:53 +0000 | [diff] [blame] | 1464 | int sqlFuzz = 0; /* True for SQL fuzz. False for DB fuzz */ |
drh | d4ddcbc | 2015-06-25 02:25:28 +0000 | [diff] [blame] | 1465 | int iTimeout = 120; /* Default 120-second timeout */ |
drh | 31999c5 | 2019-11-14 17:46:32 +0000 | [diff] [blame] | 1466 | int nMem = 0; /* Memory limit override */ |
drh | 362b66f | 2016-11-14 18:27:41 +0000 | [diff] [blame] | 1467 | int nMemThisDb = 0; /* Memory limit set by the CONFIG table */ |
drh | 40e0e0d | 2015-09-22 18:51:17 +0000 | [diff] [blame] | 1468 | char *zExpDb = 0; /* Write Databases to files in this directory */ |
| 1469 | char *zExpSql = 0; /* Write SQL to files in this directory */ |
drh | 6653fbe | 2015-11-13 20:52:49 +0000 | [diff] [blame] | 1470 | void *pHeap = 0; /* Heap for use by SQLite */ |
drh | ea432ba | 2016-11-11 16:33:47 +0000 | [diff] [blame] | 1471 | int ossFuzz = 0; /* enable OSS-FUZZ testing */ |
drh | 362b66f | 2016-11-14 18:27:41 +0000 | [diff] [blame] | 1472 | int ossFuzzThisDb = 0; /* ossFuzz value for this particular database */ |
drh | 174f855 | 2017-03-20 22:58:27 +0000 | [diff] [blame] | 1473 | int nativeMalloc = 0; /* Turn off MEMSYS3/5 and lookaside if true */ |
drh | beaf514 | 2016-12-26 00:15:56 +0000 | [diff] [blame] | 1474 | sqlite3_vfs *pDfltVfs; /* The default VFS */ |
drh | f2cf412 | 2018-05-08 13:03:31 +0000 | [diff] [blame] | 1475 | int openFlags4Data; /* Flags for sqlite3_open_v2() */ |
drh | 725a9c7 | 2019-01-25 13:03:38 +0000 | [diff] [blame] | 1476 | int nV; /* How much to increase verbosity with -vvvv */ |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1477 | |
drh | 39b3bcf | 2020-03-02 16:31:21 +0000 | [diff] [blame] | 1478 | registerOomSimulator(); |
drh | 8055a3e | 2018-11-21 14:27:34 +0000 | [diff] [blame] | 1479 | sqlite3_initialize(); |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1480 | iBegin = timeOfDay(); |
drh | 94701b0 | 2015-06-24 13:25:34 +0000 | [diff] [blame] | 1481 | #ifdef __unix__ |
drh | a7648f0 | 2019-12-18 13:02:18 +0000 | [diff] [blame] | 1482 | signal(SIGALRM, signalHandler); |
| 1483 | signal(SIGSEGV, signalHandler); |
| 1484 | signal(SIGABRT, signalHandler); |
drh | 94701b0 | 2015-06-24 13:25:34 +0000 | [diff] [blame] | 1485 | #endif |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1486 | g.zArgv0 = argv[0]; |
drh | f2cf412 | 2018-05-08 13:03:31 +0000 | [diff] [blame] | 1487 | openFlags4Data = SQLITE_OPEN_READONLY; |
drh | 4d6fda7 | 2015-05-26 18:58:32 +0000 | [diff] [blame] | 1488 | zFailCode = getenv("TEST_FAILURE"); |
drh | beaf514 | 2016-12-26 00:15:56 +0000 | [diff] [blame] | 1489 | pDfltVfs = sqlite3_vfs_find(0); |
| 1490 | inmemVfsRegister(1); |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1491 | for(i=1; i<argc; i++){ |
| 1492 | const char *z = argv[i]; |
| 1493 | if( z[0]=='-' ){ |
| 1494 | z++; |
| 1495 | if( z[0]=='-' ) z++; |
drh | 1421d98 | 2015-05-27 03:46:18 +0000 | [diff] [blame] | 1496 | if( strcmp(z,"cell-size-check")==0 ){ |
| 1497 | cellSzCkFlag = 1; |
| 1498 | }else |
drh | a9542b1 | 2015-05-25 19:35:42 +0000 | [diff] [blame] | 1499 | if( strcmp(z,"dbid")==0 ){ |
| 1500 | if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); |
drh | 53e66c3 | 2015-07-24 15:49:23 +0000 | [diff] [blame] | 1501 | onlyDbid = integerValue(argv[++i]); |
drh | a9542b1 | 2015-05-25 19:35:42 +0000 | [diff] [blame] | 1502 | }else |
drh | 40e0e0d | 2015-09-22 18:51:17 +0000 | [diff] [blame] | 1503 | if( strcmp(z,"export-db")==0 ){ |
| 1504 | if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); |
| 1505 | zExpDb = argv[++i]; |
| 1506 | }else |
drh | e5da935 | 2019-01-27 01:11:40 +0000 | [diff] [blame] | 1507 | if( strcmp(z,"export-sql")==0 || strcmp(z,"export-dbsql")==0 ){ |
drh | 40e0e0d | 2015-09-22 18:51:17 +0000 | [diff] [blame] | 1508 | if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); |
| 1509 | zExpSql = argv[++i]; |
| 1510 | }else |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1511 | if( strcmp(z,"help")==0 ){ |
| 1512 | showHelp(); |
| 1513 | return 0; |
| 1514 | }else |
drh | 5180d68 | 2018-08-06 01:39:31 +0000 | [diff] [blame] | 1515 | if( strcmp(z,"info")==0 ){ |
| 1516 | infoFlag = 1; |
| 1517 | }else |
drh | be03cc9 | 2020-01-20 14:42:09 +0000 | [diff] [blame] | 1518 | if( strcmp(z,"limit-depth")==0 ){ |
| 1519 | if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); |
| 1520 | depthLimit = integerValue(argv[++i]); |
| 1521 | }else |
drh | 53e66c3 | 2015-07-24 15:49:23 +0000 | [diff] [blame] | 1522 | if( strcmp(z,"limit-mem")==0 ){ |
| 1523 | if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); |
| 1524 | nMem = integerValue(argv[++i]); |
| 1525 | }else |
drh | d83e283 | 2015-06-24 14:45:44 +0000 | [diff] [blame] | 1526 | if( strcmp(z,"limit-vdbe")==0 ){ |
| 1527 | vdbeLimitFlag = 1; |
| 1528 | }else |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1529 | if( strcmp(z,"load-sql")==0 ){ |
drh | a8781d9 | 2020-02-25 20:05:58 +0000 | [diff] [blame] | 1530 | zInsSql = "INSERT INTO xsql(sqltext)" |
| 1531 | "VALUES(CAST(readtextfile(?1) AS text))"; |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1532 | iFirstInsArg = i+1; |
drh | f2cf412 | 2018-05-08 13:03:31 +0000 | [diff] [blame] | 1533 | openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1534 | break; |
| 1535 | }else |
| 1536 | if( strcmp(z,"load-db")==0 ){ |
| 1537 | zInsSql = "INSERT INTO db(dbcontent) VALUES(readfile(?1))"; |
| 1538 | iFirstInsArg = i+1; |
drh | f2cf412 | 2018-05-08 13:03:31 +0000 | [diff] [blame] | 1539 | openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1540 | break; |
| 1541 | }else |
drh | e5da935 | 2019-01-27 01:11:40 +0000 | [diff] [blame] | 1542 | if( strcmp(z,"load-dbsql")==0 ){ |
drh | a8781d9 | 2020-02-25 20:05:58 +0000 | [diff] [blame] | 1543 | zInsSql = "INSERT INTO xsql(sqltext)" |
| 1544 | "VALUES(CAST(readtextfile(?1) AS text))"; |
drh | e5da935 | 2019-01-27 01:11:40 +0000 | [diff] [blame] | 1545 | iFirstInsArg = i+1; |
| 1546 | openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; |
| 1547 | dbSqlOnly = 1; |
| 1548 | break; |
| 1549 | }else |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1550 | if( strcmp(z,"m")==0 ){ |
| 1551 | if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); |
| 1552 | zMsg = argv[++i]; |
drh | f2cf412 | 2018-05-08 13:03:31 +0000 | [diff] [blame] | 1553 | openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE; |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1554 | }else |
drh | 174f855 | 2017-03-20 22:58:27 +0000 | [diff] [blame] | 1555 | if( strcmp(z,"native-malloc")==0 ){ |
| 1556 | nativeMalloc = 1; |
| 1557 | }else |
drh | 15b3128 | 2015-05-25 21:59:05 +0000 | [diff] [blame] | 1558 | if( strcmp(z,"native-vfs")==0 ){ |
| 1559 | nativeFlag = 1; |
| 1560 | }else |
drh | ea432ba | 2016-11-11 16:33:47 +0000 | [diff] [blame] | 1561 | if( strcmp(z,"oss-fuzz")==0 ){ |
| 1562 | ossFuzz = 1; |
| 1563 | }else |
drh | beaf514 | 2016-12-26 00:15:56 +0000 | [diff] [blame] | 1564 | if( strcmp(z,"prng-seed")==0 ){ |
| 1565 | if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); |
| 1566 | g.uRandom = atoi(argv[++i]); |
| 1567 | }else |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1568 | if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){ |
| 1569 | quietFlag = 1; |
| 1570 | verboseFlag = 0; |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 1571 | eVerbosity = 0; |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1572 | }else |
drh | 9a64586 | 2015-06-24 12:44:42 +0000 | [diff] [blame] | 1573 | if( strcmp(z,"rebuild")==0 ){ |
| 1574 | rebuildFlag = 1; |
drh | f2cf412 | 2018-05-08 13:03:31 +0000 | [diff] [blame] | 1575 | openFlags4Data = SQLITE_OPEN_READWRITE; |
drh | 9a64586 | 2015-06-24 12:44:42 +0000 | [diff] [blame] | 1576 | }else |
drh | e5c5f2c | 2015-05-26 00:28:08 +0000 | [diff] [blame] | 1577 | if( strcmp(z,"result-trace")==0 ){ |
| 1578 | runFlags |= SQL_OUTPUT; |
| 1579 | }else |
drh | a9542b1 | 2015-05-25 19:35:42 +0000 | [diff] [blame] | 1580 | if( strcmp(z,"sqlid")==0 ){ |
| 1581 | if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); |
drh | 53e66c3 | 2015-07-24 15:49:23 +0000 | [diff] [blame] | 1582 | onlySqlid = integerValue(argv[++i]); |
drh | a9542b1 | 2015-05-25 19:35:42 +0000 | [diff] [blame] | 1583 | }else |
drh | 9229863 | 2015-06-24 23:44:30 +0000 | [diff] [blame] | 1584 | if( strcmp(z,"timeout")==0 ){ |
| 1585 | if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); |
drh | 53e66c3 | 2015-07-24 15:49:23 +0000 | [diff] [blame] | 1586 | iTimeout = integerValue(argv[++i]); |
drh | 9229863 | 2015-06-24 23:44:30 +0000 | [diff] [blame] | 1587 | }else |
drh | 94701b0 | 2015-06-24 13:25:34 +0000 | [diff] [blame] | 1588 | if( strcmp(z,"timeout-test")==0 ){ |
| 1589 | timeoutTest = 1; |
| 1590 | #ifndef __unix__ |
| 1591 | fatalError("timeout is not available on non-unix systems"); |
| 1592 | #endif |
| 1593 | }else |
drh | 6e1c45e | 2019-12-18 13:42:04 +0000 | [diff] [blame] | 1594 | if( strcmp(z,"vdbe-debug")==0 ){ |
| 1595 | bVdbeDebug = 1; |
| 1596 | }else |
drh | 725a9c7 | 2019-01-25 13:03:38 +0000 | [diff] [blame] | 1597 | if( strcmp(z,"verbose")==0 ){ |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1598 | quietFlag = 0; |
drh | 4c9d228 | 2016-02-18 14:03:15 +0000 | [diff] [blame] | 1599 | verboseFlag++; |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 1600 | eVerbosity++; |
drh | 4c9d228 | 2016-02-18 14:03:15 +0000 | [diff] [blame] | 1601 | if( verboseFlag>1 ) runFlags |= SQL_TRACE; |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1602 | }else |
drh | 725a9c7 | 2019-01-25 13:03:38 +0000 | [diff] [blame] | 1603 | if( (nV = numberOfVChar(z))>=1 ){ |
| 1604 | quietFlag = 0; |
| 1605 | verboseFlag += nV; |
| 1606 | eVerbosity += nV; |
| 1607 | if( verboseFlag>1 ) runFlags |= SQL_TRACE; |
| 1608 | }else |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 1609 | if( strcmp(z,"version")==0 ){ |
| 1610 | int ii; |
drh | ed45703 | 2019-01-25 17:51:06 +0000 | [diff] [blame] | 1611 | const char *zz; |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 1612 | printf("SQLite %s %s\n", sqlite3_libversion(), sqlite3_sourceid()); |
drh | ed45703 | 2019-01-25 17:51:06 +0000 | [diff] [blame] | 1613 | for(ii=0; (zz = sqlite3_compileoption_get(ii))!=0; ii++){ |
| 1614 | printf("%s\n", zz); |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 1615 | } |
| 1616 | return 0; |
| 1617 | }else |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1618 | { |
| 1619 | fatalError("unknown option: %s", argv[i]); |
| 1620 | } |
| 1621 | }else{ |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1622 | nSrcDb++; |
| 1623 | azSrcDb = safe_realloc(azSrcDb, nSrcDb*sizeof(azSrcDb[0])); |
| 1624 | azSrcDb[nSrcDb-1] = argv[i]; |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1625 | } |
| 1626 | } |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1627 | if( nSrcDb==0 ) fatalError("no source database specified"); |
| 1628 | if( nSrcDb>1 ){ |
| 1629 | if( zMsg ){ |
| 1630 | fatalError("cannot change the description of more than one database"); |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1631 | } |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1632 | if( zInsSql ){ |
| 1633 | fatalError("cannot import into more than one database"); |
| 1634 | } |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1635 | } |
| 1636 | |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1637 | /* Process each source database separately */ |
| 1638 | for(iSrcDb=0; iSrcDb<nSrcDb; iSrcDb++){ |
drh | a7648f0 | 2019-12-18 13:02:18 +0000 | [diff] [blame] | 1639 | g.zDbFile = azSrcDb[iSrcDb]; |
drh | beaf514 | 2016-12-26 00:15:56 +0000 | [diff] [blame] | 1640 | rc = sqlite3_open_v2(azSrcDb[iSrcDb], &db, |
drh | f2cf412 | 2018-05-08 13:03:31 +0000 | [diff] [blame] | 1641 | openFlags4Data, pDfltVfs->zName); |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1642 | if( rc ){ |
| 1643 | fatalError("cannot open source database %s - %s", |
| 1644 | azSrcDb[iSrcDb], sqlite3_errmsg(db)); |
| 1645 | } |
drh | 5180d68 | 2018-08-06 01:39:31 +0000 | [diff] [blame] | 1646 | |
| 1647 | /* Print the description, if there is one */ |
| 1648 | if( infoFlag ){ |
| 1649 | int n; |
| 1650 | zDbName = azSrcDb[iSrcDb]; |
| 1651 | i = (int)strlen(zDbName) - 1; |
| 1652 | while( i>0 && zDbName[i-1]!='/' && zDbName[i-1]!='\\' ){ i--; } |
| 1653 | zDbName += i; |
| 1654 | sqlite3_prepare_v2(db, "SELECT msg FROM readme", -1, &pStmt, 0); |
| 1655 | if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 1656 | printf("%s: %s", zDbName, sqlite3_column_text(pStmt,0)); |
| 1657 | }else{ |
| 1658 | printf("%s: (empty \"readme\")", zDbName); |
| 1659 | } |
| 1660 | sqlite3_finalize(pStmt); |
| 1661 | sqlite3_prepare_v2(db, "SELECT count(*) FROM db", -1, &pStmt, 0); |
| 1662 | if( pStmt |
| 1663 | && sqlite3_step(pStmt)==SQLITE_ROW |
| 1664 | && (n = sqlite3_column_int(pStmt,0))>0 |
| 1665 | ){ |
| 1666 | printf(" - %d DBs", n); |
| 1667 | } |
| 1668 | sqlite3_finalize(pStmt); |
| 1669 | sqlite3_prepare_v2(db, "SELECT count(*) FROM xsql", -1, &pStmt, 0); |
| 1670 | if( pStmt |
| 1671 | && sqlite3_step(pStmt)==SQLITE_ROW |
| 1672 | && (n = sqlite3_column_int(pStmt,0))>0 |
| 1673 | ){ |
| 1674 | printf(" - %d scripts", n); |
| 1675 | } |
| 1676 | sqlite3_finalize(pStmt); |
| 1677 | printf("\n"); |
| 1678 | sqlite3_close(db); |
| 1679 | continue; |
| 1680 | } |
| 1681 | |
drh | 9a64586 | 2015-06-24 12:44:42 +0000 | [diff] [blame] | 1682 | rc = sqlite3_exec(db, |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1683 | "CREATE TABLE IF NOT EXISTS db(\n" |
| 1684 | " dbid INTEGER PRIMARY KEY, -- database id\n" |
| 1685 | " dbcontent BLOB -- database disk file image\n" |
| 1686 | ");\n" |
| 1687 | "CREATE TABLE IF NOT EXISTS xsql(\n" |
| 1688 | " sqlid INTEGER PRIMARY KEY, -- SQL script id\n" |
| 1689 | " sqltext TEXT -- Text of SQL statements to run\n" |
| 1690 | ");" |
| 1691 | "CREATE TABLE IF NOT EXISTS readme(\n" |
| 1692 | " msg TEXT -- Human-readable description of this file\n" |
| 1693 | ");", 0, 0, 0); |
| 1694 | if( rc ) fatalError("cannot create schema: %s", sqlite3_errmsg(db)); |
| 1695 | if( zMsg ){ |
| 1696 | char *zSql; |
| 1697 | zSql = sqlite3_mprintf( |
| 1698 | "DELETE FROM readme; INSERT INTO readme(msg) VALUES(%Q)", zMsg); |
| 1699 | rc = sqlite3_exec(db, zSql, 0, 0, 0); |
| 1700 | sqlite3_free(zSql); |
| 1701 | if( rc ) fatalError("cannot change description: %s", sqlite3_errmsg(db)); |
| 1702 | } |
drh | 362b66f | 2016-11-14 18:27:41 +0000 | [diff] [blame] | 1703 | ossFuzzThisDb = ossFuzz; |
| 1704 | |
| 1705 | /* If the CONFIG(name,value) table exists, read db-specific settings |
| 1706 | ** from that table */ |
| 1707 | if( sqlite3_table_column_metadata(db,0,"config",0,0,0,0,0,0)==SQLITE_OK ){ |
drh | 5ecf903 | 2018-05-08 12:49:53 +0000 | [diff] [blame] | 1708 | rc = sqlite3_prepare_v2(db, "SELECT name, value FROM config", |
| 1709 | -1, &pStmt, 0); |
drh | 362b66f | 2016-11-14 18:27:41 +0000 | [diff] [blame] | 1710 | if( rc ) fatalError("cannot prepare query of CONFIG table: %s", |
| 1711 | sqlite3_errmsg(db)); |
| 1712 | while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 1713 | const char *zName = (const char *)sqlite3_column_text(pStmt,0); |
| 1714 | if( zName==0 ) continue; |
| 1715 | if( strcmp(zName, "oss-fuzz")==0 ){ |
| 1716 | ossFuzzThisDb = sqlite3_column_int(pStmt,1); |
| 1717 | if( verboseFlag ) printf("Config: oss-fuzz=%d\n", ossFuzzThisDb); |
| 1718 | } |
drh | 31999c5 | 2019-11-14 17:46:32 +0000 | [diff] [blame] | 1719 | if( strcmp(zName, "limit-mem")==0 ){ |
drh | 362b66f | 2016-11-14 18:27:41 +0000 | [diff] [blame] | 1720 | nMemThisDb = sqlite3_column_int(pStmt,1); |
| 1721 | if( verboseFlag ) printf("Config: limit-mem=%d\n", nMemThisDb); |
drh | 362b66f | 2016-11-14 18:27:41 +0000 | [diff] [blame] | 1722 | } |
| 1723 | } |
| 1724 | sqlite3_finalize(pStmt); |
| 1725 | } |
| 1726 | |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1727 | if( zInsSql ){ |
| 1728 | sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0, |
| 1729 | readfileFunc, 0, 0); |
drh | a8781d9 | 2020-02-25 20:05:58 +0000 | [diff] [blame] | 1730 | sqlite3_create_function(db, "readtextfile", 1, SQLITE_UTF8, 0, |
| 1731 | readtextfileFunc, 0, 0); |
drh | e5da935 | 2019-01-27 01:11:40 +0000 | [diff] [blame] | 1732 | sqlite3_create_function(db, "isdbsql", 1, SQLITE_UTF8, 0, |
| 1733 | isDbSqlFunc, 0, 0); |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1734 | rc = sqlite3_prepare_v2(db, zInsSql, -1, &pStmt, 0); |
| 1735 | if( rc ) fatalError("cannot prepare statement [%s]: %s", |
| 1736 | zInsSql, sqlite3_errmsg(db)); |
| 1737 | rc = sqlite3_exec(db, "BEGIN", 0, 0, 0); |
| 1738 | if( rc ) fatalError("cannot start a transaction"); |
| 1739 | for(i=iFirstInsArg; i<argc; i++){ |
| 1740 | sqlite3_bind_text(pStmt, 1, argv[i], -1, SQLITE_STATIC); |
| 1741 | sqlite3_step(pStmt); |
| 1742 | rc = sqlite3_reset(pStmt); |
| 1743 | if( rc ) fatalError("insert failed for %s", argv[i]); |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1744 | } |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1745 | sqlite3_finalize(pStmt); |
| 1746 | rc = sqlite3_exec(db, "COMMIT", 0, 0, 0); |
drh | 5ecf903 | 2018-05-08 12:49:53 +0000 | [diff] [blame] | 1747 | if( rc ) fatalError("cannot commit the transaction: %s", |
| 1748 | sqlite3_errmsg(db)); |
drh | e5da935 | 2019-01-27 01:11:40 +0000 | [diff] [blame] | 1749 | rebuild_database(db, dbSqlOnly); |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1750 | sqlite3_close(db); |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1751 | return 0; |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1752 | } |
drh | 16f0582 | 2017-03-20 20:42:21 +0000 | [diff] [blame] | 1753 | rc = sqlite3_exec(db, "PRAGMA query_only=1;", 0, 0, 0); |
| 1754 | if( rc ) fatalError("cannot set database to query-only"); |
drh | 40e0e0d | 2015-09-22 18:51:17 +0000 | [diff] [blame] | 1755 | if( zExpDb!=0 || zExpSql!=0 ){ |
| 1756 | sqlite3_create_function(db, "writefile", 2, SQLITE_UTF8, 0, |
| 1757 | writefileFunc, 0, 0); |
| 1758 | if( zExpDb!=0 ){ |
| 1759 | const char *zExDb = |
| 1760 | "SELECT writefile(printf('%s/db%06d.db',?1,dbid),dbcontent)," |
| 1761 | " dbid, printf('%s/db%06d.db',?1,dbid), length(dbcontent)" |
| 1762 | " FROM db WHERE ?2<0 OR dbid=?2;"; |
| 1763 | rc = sqlite3_prepare_v2(db, zExDb, -1, &pStmt, 0); |
| 1764 | if( rc ) fatalError("cannot prepare statement [%s]: %s", |
| 1765 | zExDb, sqlite3_errmsg(db)); |
| 1766 | sqlite3_bind_text64(pStmt, 1, zExpDb, strlen(zExpDb), |
| 1767 | SQLITE_STATIC, SQLITE_UTF8); |
| 1768 | sqlite3_bind_int(pStmt, 2, onlyDbid); |
| 1769 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 1770 | printf("write db-%d (%d bytes) into %s\n", |
| 1771 | sqlite3_column_int(pStmt,1), |
| 1772 | sqlite3_column_int(pStmt,3), |
| 1773 | sqlite3_column_text(pStmt,2)); |
| 1774 | } |
| 1775 | sqlite3_finalize(pStmt); |
| 1776 | } |
| 1777 | if( zExpSql!=0 ){ |
| 1778 | const char *zExSql = |
| 1779 | "SELECT writefile(printf('%s/sql%06d.txt',?1,sqlid),sqltext)," |
| 1780 | " sqlid, printf('%s/sql%06d.txt',?1,sqlid), length(sqltext)" |
| 1781 | " FROM xsql WHERE ?2<0 OR sqlid=?2;"; |
| 1782 | rc = sqlite3_prepare_v2(db, zExSql, -1, &pStmt, 0); |
| 1783 | if( rc ) fatalError("cannot prepare statement [%s]: %s", |
| 1784 | zExSql, sqlite3_errmsg(db)); |
| 1785 | sqlite3_bind_text64(pStmt, 1, zExpSql, strlen(zExpSql), |
| 1786 | SQLITE_STATIC, SQLITE_UTF8); |
| 1787 | sqlite3_bind_int(pStmt, 2, onlySqlid); |
| 1788 | while( sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 1789 | printf("write sql-%d (%d bytes) into %s\n", |
| 1790 | sqlite3_column_int(pStmt,1), |
| 1791 | sqlite3_column_int(pStmt,3), |
| 1792 | sqlite3_column_text(pStmt,2)); |
| 1793 | } |
| 1794 | sqlite3_finalize(pStmt); |
| 1795 | } |
| 1796 | sqlite3_close(db); |
| 1797 | return 0; |
| 1798 | } |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1799 | |
| 1800 | /* Load all SQL script content and all initial database images from the |
| 1801 | ** source db |
| 1802 | */ |
| 1803 | blobListLoadFromDb(db, "SELECT sqlid, sqltext FROM xsql", onlySqlid, |
| 1804 | &g.nSql, &g.pFirstSql); |
| 1805 | if( g.nSql==0 ) fatalError("need at least one SQL script"); |
| 1806 | blobListLoadFromDb(db, "SELECT dbid, dbcontent FROM db", onlyDbid, |
| 1807 | &g.nDb, &g.pFirstDb); |
| 1808 | if( g.nDb==0 ){ |
| 1809 | g.pFirstDb = safe_realloc(0, sizeof(Blob)); |
| 1810 | memset(g.pFirstDb, 0, sizeof(Blob)); |
| 1811 | g.pFirstDb->id = 1; |
| 1812 | g.pFirstDb->seq = 0; |
| 1813 | g.nDb = 1; |
drh | d83e283 | 2015-06-24 14:45:44 +0000 | [diff] [blame] | 1814 | sqlFuzz = 1; |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | /* Print the description, if there is one */ |
| 1818 | if( !quietFlag ){ |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1819 | zDbName = azSrcDb[iSrcDb]; |
drh | e683b89 | 2016-02-15 18:47:26 +0000 | [diff] [blame] | 1820 | i = (int)strlen(zDbName) - 1; |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1821 | while( i>0 && zDbName[i-1]!='/' && zDbName[i-1]!='\\' ){ i--; } |
| 1822 | zDbName += i; |
| 1823 | sqlite3_prepare_v2(db, "SELECT msg FROM readme", -1, &pStmt, 0); |
| 1824 | if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 1825 | printf("%s: %s\n", zDbName, sqlite3_column_text(pStmt,0)); |
| 1826 | } |
| 1827 | sqlite3_finalize(pStmt); |
| 1828 | } |
drh | 9a64586 | 2015-06-24 12:44:42 +0000 | [diff] [blame] | 1829 | |
| 1830 | /* Rebuild the database, if requested */ |
| 1831 | if( rebuildFlag ){ |
| 1832 | if( !quietFlag ){ |
| 1833 | printf("%s: rebuilding... ", zDbName); |
| 1834 | fflush(stdout); |
| 1835 | } |
drh | e5da935 | 2019-01-27 01:11:40 +0000 | [diff] [blame] | 1836 | rebuild_database(db, 0); |
drh | 9a64586 | 2015-06-24 12:44:42 +0000 | [diff] [blame] | 1837 | if( !quietFlag ) printf("done\n"); |
| 1838 | } |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1839 | |
| 1840 | /* Close the source database. Verify that no SQLite memory allocations are |
| 1841 | ** outstanding. |
| 1842 | */ |
| 1843 | sqlite3_close(db); |
| 1844 | if( sqlite3_memory_used()>0 ){ |
| 1845 | fatalError("SQLite has memory in use before the start of testing"); |
| 1846 | } |
drh | 53e66c3 | 2015-07-24 15:49:23 +0000 | [diff] [blame] | 1847 | |
| 1848 | /* Limit available memory, if requested */ |
drh | 174f855 | 2017-03-20 22:58:27 +0000 | [diff] [blame] | 1849 | sqlite3_shutdown(); |
drh | 39b3bcf | 2020-03-02 16:31:21 +0000 | [diff] [blame] | 1850 | |
drh | 31999c5 | 2019-11-14 17:46:32 +0000 | [diff] [blame] | 1851 | if( nMemThisDb>0 && nMem==0 ){ |
| 1852 | if( !nativeMalloc ){ |
| 1853 | pHeap = realloc(pHeap, nMemThisDb); |
| 1854 | if( pHeap==0 ){ |
| 1855 | fatalError("failed to allocate %d bytes of heap memory", nMem); |
| 1856 | } |
| 1857 | sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nMemThisDb, 128); |
| 1858 | }else{ |
| 1859 | sqlite3_hard_heap_limit64((sqlite3_int64)nMemThisDb); |
drh | 53e66c3 | 2015-07-24 15:49:23 +0000 | [diff] [blame] | 1860 | } |
drh | 31999c5 | 2019-11-14 17:46:32 +0000 | [diff] [blame] | 1861 | }else{ |
| 1862 | sqlite3_hard_heap_limit64(0); |
drh | 53e66c3 | 2015-07-24 15:49:23 +0000 | [diff] [blame] | 1863 | } |
drh | 174f855 | 2017-03-20 22:58:27 +0000 | [diff] [blame] | 1864 | |
| 1865 | /* Disable lookaside with the --native-malloc option */ |
| 1866 | if( nativeMalloc ){ |
| 1867 | sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0); |
| 1868 | } |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1869 | |
drh | beaf514 | 2016-12-26 00:15:56 +0000 | [diff] [blame] | 1870 | /* Reset the in-memory virtual filesystem */ |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1871 | formatVfs(); |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1872 | |
| 1873 | /* Run a test using each SQL script against each database. |
| 1874 | */ |
| 1875 | if( !verboseFlag && !quietFlag ) printf("%s:", zDbName); |
| 1876 | for(pSql=g.pFirstSql; pSql; pSql=pSql->pNext){ |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 1877 | if( isDbSql(pSql->a, pSql->sz) ){ |
| 1878 | sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "sqlid=%d",pSql->id); |
| 1879 | if( verboseFlag ){ |
| 1880 | printf("%s\n", g.zTestName); |
| 1881 | fflush(stdout); |
| 1882 | }else if( !quietFlag ){ |
| 1883 | static int prevAmt = -1; |
| 1884 | int idx = pSql->seq; |
| 1885 | int amt = idx*10/(g.nSql); |
| 1886 | if( amt!=prevAmt ){ |
| 1887 | printf(" %d%%", amt*10); |
| 1888 | fflush(stdout); |
| 1889 | prevAmt = amt; |
| 1890 | } |
| 1891 | } |
| 1892 | runCombinedDbSqlInput(pSql->a, pSql->sz); |
| 1893 | nTest++; |
| 1894 | g.zTestName[0] = 0; |
drh | 39b3bcf | 2020-03-02 16:31:21 +0000 | [diff] [blame] | 1895 | disableOom(); |
drh | a47e709 | 2019-01-25 04:00:14 +0000 | [diff] [blame] | 1896 | continue; |
| 1897 | } |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1898 | for(pDb=g.pFirstDb; pDb; pDb=pDb->pNext){ |
| 1899 | int openFlags; |
| 1900 | const char *zVfs = "inmem"; |
| 1901 | sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "sqlid=%d,dbid=%d", |
| 1902 | pSql->id, pDb->id); |
| 1903 | if( verboseFlag ){ |
| 1904 | printf("%s\n", g.zTestName); |
| 1905 | fflush(stdout); |
| 1906 | }else if( !quietFlag ){ |
| 1907 | static int prevAmt = -1; |
| 1908 | int idx = pSql->seq*g.nDb + pDb->id - 1; |
| 1909 | int amt = idx*10/(g.nDb*g.nSql); |
| 1910 | if( amt!=prevAmt ){ |
| 1911 | printf(" %d%%", amt*10); |
| 1912 | fflush(stdout); |
| 1913 | prevAmt = amt; |
| 1914 | } |
| 1915 | } |
| 1916 | createVFile("main.db", pDb->sz, pDb->a); |
drh | beaf514 | 2016-12-26 00:15:56 +0000 | [diff] [blame] | 1917 | sqlite3_randomness(0,0); |
drh | 362b66f | 2016-11-14 18:27:41 +0000 | [diff] [blame] | 1918 | if( ossFuzzThisDb ){ |
drh | ea432ba | 2016-11-11 16:33:47 +0000 | [diff] [blame] | 1919 | #ifndef SQLITE_OSS_FUZZ |
drh | 5ecf903 | 2018-05-08 12:49:53 +0000 | [diff] [blame] | 1920 | fatalError("--oss-fuzz not supported: recompile" |
| 1921 | " with -DSQLITE_OSS_FUZZ"); |
drh | ea432ba | 2016-11-11 16:33:47 +0000 | [diff] [blame] | 1922 | #else |
| 1923 | extern int LLVMFuzzerTestOneInput(const uint8_t*, size_t); |
| 1924 | LLVMFuzzerTestOneInput((const uint8_t*)pSql->a, (size_t)pSql->sz); |
drh | 7805735 | 2015-06-24 23:17:35 +0000 | [diff] [blame] | 1925 | #endif |
drh | ea432ba | 2016-11-11 16:33:47 +0000 | [diff] [blame] | 1926 | }else{ |
| 1927 | openFlags = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE; |
| 1928 | if( nativeFlag && pDb->sz==0 ){ |
| 1929 | openFlags |= SQLITE_OPEN_MEMORY; |
| 1930 | zVfs = 0; |
| 1931 | } |
| 1932 | rc = sqlite3_open_v2("main.db", &db, openFlags, zVfs); |
| 1933 | if( rc ) fatalError("cannot open inmem database"); |
drh | dfcfff6 | 2016-12-26 12:25:19 +0000 | [diff] [blame] | 1934 | sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 100000000); |
| 1935 | sqlite3_limit(db, SQLITE_LIMIT_LIKE_PATTERN_LENGTH, 50); |
drh | ea432ba | 2016-11-11 16:33:47 +0000 | [diff] [blame] | 1936 | if( cellSzCkFlag ) runSql(db, "PRAGMA cell_size_check=ON", runFlags); |
| 1937 | setAlarm(iTimeout); |
| 1938 | #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 1939 | if( sqlFuzz || vdbeLimitFlag ){ |
drh | 5ecf903 | 2018-05-08 12:49:53 +0000 | [diff] [blame] | 1940 | sqlite3_progress_handler(db, 100000, progressHandler, |
| 1941 | &vdbeLimitFlag); |
drh | ea432ba | 2016-11-11 16:33:47 +0000 | [diff] [blame] | 1942 | } |
| 1943 | #endif |
drh | e6e96b1 | 2019-08-02 21:03:24 +0000 | [diff] [blame] | 1944 | #ifdef SQLITE_TESTCTRL_PRNG_SEED |
drh | 2e6d83b | 2019-08-03 01:39:20 +0000 | [diff] [blame] | 1945 | sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, 1, db); |
drh | e6e96b1 | 2019-08-02 21:03:24 +0000 | [diff] [blame] | 1946 | #endif |
drh | 6e1c45e | 2019-12-18 13:42:04 +0000 | [diff] [blame] | 1947 | if( bVdbeDebug ){ |
| 1948 | sqlite3_exec(db, "PRAGMA vdbe_debug=ON", 0, 0, 0); |
| 1949 | } |
drh | ea432ba | 2016-11-11 16:33:47 +0000 | [diff] [blame] | 1950 | do{ |
| 1951 | runSql(db, (char*)pSql->a, runFlags); |
| 1952 | }while( timeoutTest ); |
| 1953 | setAlarm(0); |
drh | 174f855 | 2017-03-20 22:58:27 +0000 | [diff] [blame] | 1954 | sqlite3_exec(db, "PRAGMA temp_store_directory=''", 0, 0, 0); |
drh | ea432ba | 2016-11-11 16:33:47 +0000 | [diff] [blame] | 1955 | sqlite3_close(db); |
| 1956 | } |
drh | 174f855 | 2017-03-20 22:58:27 +0000 | [diff] [blame] | 1957 | if( sqlite3_memory_used()>0 ){ |
| 1958 | fatalError("memory leak: %lld bytes outstanding", |
| 1959 | sqlite3_memory_used()); |
| 1960 | } |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1961 | reformatVfs(); |
| 1962 | nTest++; |
| 1963 | g.zTestName[0] = 0; |
drh | 4d6fda7 | 2015-05-26 18:58:32 +0000 | [diff] [blame] | 1964 | |
| 1965 | /* Simulate an error if the TEST_FAILURE environment variable is "5". |
| 1966 | ** This is used to verify that automated test script really do spot |
| 1967 | ** errors that occur in this test program. |
| 1968 | */ |
| 1969 | if( zFailCode ){ |
| 1970 | if( zFailCode[0]=='5' && zFailCode[1]==0 ){ |
| 1971 | fatalError("simulated failure"); |
| 1972 | }else if( zFailCode[0]!=0 ){ |
| 1973 | /* If TEST_FAILURE is something other than 5, just exit the test |
| 1974 | ** early */ |
| 1975 | printf("\nExit early due to TEST_FAILURE being set\n"); |
| 1976 | iSrcDb = nSrcDb-1; |
| 1977 | goto sourcedb_cleanup; |
| 1978 | } |
| 1979 | } |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1980 | } |
| 1981 | } |
| 1982 | if( !quietFlag && !verboseFlag ){ |
| 1983 | printf(" 100%% - %d tests\n", g.nDb*g.nSql); |
| 1984 | } |
| 1985 | |
| 1986 | /* Clean up at the end of processing a single source database |
| 1987 | */ |
drh | 4d6fda7 | 2015-05-26 18:58:32 +0000 | [diff] [blame] | 1988 | sourcedb_cleanup: |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1989 | blobListFree(g.pFirstSql); |
| 1990 | blobListFree(g.pFirstDb); |
| 1991 | reformatVfs(); |
| 1992 | |
| 1993 | } /* End loop over all source databases */ |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 1994 | |
| 1995 | if( !quietFlag ){ |
| 1996 | sqlite3_int64 iElapse = timeOfDay() - iBegin; |
drh | d9972ef | 2015-05-26 17:57:56 +0000 | [diff] [blame] | 1997 | printf("fuzzcheck: 0 errors out of %d tests in %d.%03d seconds\n" |
| 1998 | "SQLite %s %s\n", |
| 1999 | nTest, (int)(iElapse/1000), (int)(iElapse%1000), |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 2000 | sqlite3_libversion(), sqlite3_sourceid()); |
| 2001 | } |
drh | f74d35b | 2015-05-27 18:19:50 +0000 | [diff] [blame] | 2002 | free(azSrcDb); |
drh | 6653fbe | 2015-11-13 20:52:49 +0000 | [diff] [blame] | 2003 | free(pHeap); |
drh | 3b74d03 | 2015-05-25 18:48:19 +0000 | [diff] [blame] | 2004 | return 0; |
| 2005 | } |