Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/test/fuzzinvariants.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / test / fuzzinvariants.c` 584 lines ยท 19551 bytes Raw Blame History 1 /* 2 ** 2022-06-14 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 ** 13 ** This library is used by fuzzcheck to test query invariants. 14 ** 15 ** An sqlite3_stmt is passed in that has just returned SQLITE_ROW. This 16 ** routine does: 17 ** 18 ** * Record the output of the current row 19 ** * Construct an alternative query that should return the same row 20 ** * Run the alternative query and verify that it does in fact return 21 ** the same row 22 ** 23 */ 24 #include "sqlite3.h" 25 #include 26 #include 27 #include 28 #include 29 typedef sqlite3_uint64 u64; 30 31 /* Forward references */ 32 static char *fuzz_invariant_sql(sqlite3_stmt*, int); 33 static int sameValue(sqlite3_stmt*,int,sqlite3_stmt*,int,sqlite3_stmt*); 34 static void reportInvariantFailed( 35 sqlite3_stmt *pOrig, /* The original query */ 36 sqlite3_stmt *pTest, /* The alternative test query with a missing row */ 37 int iRow, /* Row number in pOrig */ 38 u64 dbOpt, /* Optimization flags on pOrig */ 39 int noOpt /* True if opt flags inverted for pTest */ 40 ); 41 42 /* 43 ** Special parameter binding, for testing and debugging purposes. 44 ** 45 ** $int_NNN -> integer value NNN 46 ** $text_TTTT -> floating point value TTT with destructor 47 ** $carray_clr -> First argument to carray() for color names 48 ** $carray_primes -> First argument to carray() for prime numbers 49 */ 50 static void bindDebugParameters(sqlite3_stmt *pStmt){ 51 int nVar = sqlite3_bind_parameter_count(pStmt); 52 int i; 53 for(i=1; i<=nVar; i++){ 54 const char *zVar = sqlite3_bind_parameter_name(pStmt, i); 55 if( zVar==0 ) continue; 56 #ifdef SQLITE_ENABLE_CARRAY 57 if( strcmp(zVar,"$carray_clr")==0 ){ 58 static char *azColorNames[] = { 59 "azure", "black", "blue", "brown", "cyan", "fuchsia", "gold", 60 "gray", "green", "indigo", "khaki", "lime", "magenta", "maroon", 61 "navy", "olive", "orange", "pink", "purple", "red", "silver", 62 "tan", "teal", "violet", "white", "yellow" 63 }; 64 sqlite3_carray_bind(pStmt,i,azColorNames,26,SQLITE_CARRAY_TEXT,0); 65 }else 66 if( strcmp(zVar,"$carray_primes")==0 ){ 67 static int aPrimes[] = { 68 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 69 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 70 }; 71 sqlite3_carray_bind(pStmt,i,aPrimes,26,SQLITE_CARRAY_INT32,0); 72 }else 73 #endif 74 if( strncmp(zVar, "$int_", 5)==0 ){ 75 sqlite3_bind_int(pStmt, i, atoi(&zVar[5])); 76 }else 77 if( strncmp(zVar, "$text_", 6)==0 ){ 78 size_t szVar = strlen(zVar); 79 char *zBuf = sqlite3_malloc64( szVar-5 ); 80 if( zBuf ){ 81 memcpy(zBuf, &zVar[6], szVar-5); 82 sqlite3_bind_text64(pStmt, i, zBuf, szVar-6, sqlite3_free, SQLITE_UTF8); 83 } 84 } 85 } 86 } 87 88 /* 89 ** Do an invariant check on pStmt. iCnt determines which invariant check to 90 ** perform. The first check is iCnt==0. 91 ** 92 ** *pbCorrupt is a flag that, if true, indicates that the database file 93 ** is known to be corrupt. A value of non-zero means "yes, the database 94 ** is corrupt". A zero value means "we do not know whether or not the 95 ** database is corrupt". The value might be set prior to entry, or this 96 ** routine might set the value. 97 ** 98 ** Return values: 99 ** 100 ** SQLITE_OK This check was successful. 101 ** 102 ** SQLITE_DONE iCnt is out of range. The caller typically sets 103 ** up a loop on iCnt starting with zero, and increments 104 ** iCnt until this code is returned. 105 ** 106 ** SQLITE_CORRUPT The invariant failed, but the underlying database 107 ** file is indicating that it is corrupt, which might 108 ** be the cause of the malfunction. The *pCorrupt 109 ** value will also be set. 110 ** 111 ** SQLITE_INTERNAL The invariant failed, and the database file is not 112 ** corrupt. (This never happens because this function 113 ** will call abort() following an invariant failure.) 114 ** 115 ** (other) Some other kind of error occurred. 116 */ 117 int fuzz_invariant( 118 sqlite3 *db, /* The database connection */ 119 sqlite3_stmt *pStmt, /* Test statement stopped on an SQLITE_ROW */ 120 int iCnt, /* Invariant sequence number, starting at 0 */ 121 int iRow, /* Current row number */ 122 int nRow, /* Number of output rows from pStmt */ 123 int *pbCorrupt, /* IN/OUT: Flag indicating a corrupt database file */ 124 int eVerbosity, /* How much debugging output */ 125 u64 dbOpt /* Default optimization flags */ 126 ){ 127 char *zTest; 128 sqlite3_stmt *pTestStmt = 0; 129 int rc; 130 int i; 131 int nCol; 132 int nParam; 133 int noOpt = (iCnt%3)==0; 134 135 if( *pbCorrupt ) return SQLITE_DONE; 136 nParam = sqlite3_bind_parameter_count(pStmt); 137 if( nParam>100 ) return SQLITE_DONE; 138 zTest = fuzz_invariant_sql(pStmt, iCnt); 139 if( zTest==0 ) return SQLITE_DONE; 140 if( noOpt ){ 141 sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, db, ~dbOpt); 142 } 143 rc = sqlite3_prepare_v2(db, zTest, -1, &pTestStmt, 0); 144 if( noOpt ){ 145 sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, db, dbOpt); 146 } 147 if( rc ){ 148 if( eVerbosity ){ 149 printf("invariant compile failed: %s\n%s\n", 150 sqlite3_errmsg(db), zTest); 151 } 152 sqlite3_free(zTest); 153 sqlite3_finalize(pTestStmt); 154 return rc; 155 } 156 sqlite3_free(zTest); 157 bindDebugParameters(pTestStmt); 158 nCol = sqlite3_column_count(pStmt); 159 for(i=0; i=2 ){ 167 char *zSql = sqlite3_expanded_sql(pTestStmt); 168 printf("invariant-sql row=%d #%d:\n%s\n", iRow, iCnt, zSql); 169 sqlite3_free(zSql); 170 } 171 while( (rc = sqlite3_step(pTestStmt))==SQLITE_ROW ){ 172 for(i=0; i=nCol ) break; 176 } 177 if( rc==SQLITE_DONE ){ 178 /* No matching output row found */ 179 sqlite3_stmt *pCk = 0; 180 int iOrigRSO; 181 182 183 /* This is not a fault if the database file is corrupt, because anything 184 ** can happen with a corrupt database file */ 185 rc = sqlite3_prepare_v2(db, "PRAGMA integrity_check", -1, &pCk, 0); 186 if( rc ){ 187 sqlite3_finalize(pCk); 188 sqlite3_finalize(pTestStmt); 189 return rc; 190 } 191 if( eVerbosity>=2 ){ 192 char *zSql = sqlite3_expanded_sql(pCk); 193 printf("invariant-validity-check #1:\n%s\n", zSql); 194 sqlite3_free(zSql); 195 } 196 197 rc = sqlite3_step(pCk); 198 if( rc!=SQLITE_ROW 199 || sqlite3_column_text(pCk, 0)==0 200 || strcmp((const char*)sqlite3_column_text(pCk,0),"ok")!=0 201 ){ 202 *pbCorrupt = 1; 203 sqlite3_finalize(pCk); 204 sqlite3_finalize(pTestStmt); 205 return SQLITE_CORRUPT; 206 } 207 sqlite3_finalize(pCk); 208 209 /* 210 ** If inverting the scan order also results in a miss, assume that the 211 ** query is ambiguous and do not report a fault. 212 */ 213 sqlite3_db_config(db, SQLITE_DBCONFIG_REVERSE_SCANORDER, -1, &iOrigRSO); 214 sqlite3_db_config(db, SQLITE_DBCONFIG_REVERSE_SCANORDER, !iOrigRSO, 0); 215 sqlite3_prepare_v2(db, sqlite3_sql(pStmt), -1, &pCk, 0); 216 sqlite3_db_config(db, SQLITE_DBCONFIG_REVERSE_SCANORDER, iOrigRSO, 0); 217 if( eVerbosity>=2 ){ 218 char *zSql = sqlite3_expanded_sql(pCk); 219 printf("invariant-validity-check #2:\n%s\n", zSql); 220 sqlite3_free(zSql); 221 } 222 bindDebugParameters(pCk); 223 while( (rc = sqlite3_step(pCk))==SQLITE_ROW ){ 224 for(i=0; i=nCol ) break; 228 } 229 sqlite3_finalize(pCk); 230 if( rc==SQLITE_DONE ){ 231 sqlite3_finalize(pTestStmt); 232 return SQLITE_DONE; 233 } 234 235 /* The original sameValue() comparison assumed a collating sequence 236 ** of "binary". It can sometimes get an incorrect result for different 237 ** collating sequences. So rerun the test with no assumptions about 238 ** collations. 239 */ 240 rc = sqlite3_prepare_v2(db, 241 "SELECT ?1=?2 OR ?1=?2 COLLATE nocase OR ?1=?2 COLLATE rtrim", 242 -1, &pCk, 0); 243 if( rc==SQLITE_OK ){ 244 if( eVerbosity>=2 ){ 245 char *zSql = sqlite3_expanded_sql(pCk); 246 printf("invariant-validity-check #3:\n%s\n", zSql); 247 sqlite3_free(zSql); 248 } 249 250 sqlite3_reset(pTestStmt); 251 bindDebugParameters(pCk); 252 while( (rc = sqlite3_step(pTestStmt))==SQLITE_ROW ){ 253 for(i=0; i=nCol ){ 257 sqlite3_finalize(pCk); 258 goto not_a_fault; 259 } 260 } 261 } 262 sqlite3_finalize(pCk); 263 264 /* Invariants do not necessarily work if there are virtual tables 265 ** or scalar subqueries involved in the query */ 266 rc = sqlite3_prepare_v2(db, 267 "SELECT 1 FROM bytecode(?1)" 268 " WHERE opcode='VOpen' OR" 269 " (opcode='Explain' AND p4 GLOB 'SCALAR SUBQUERY*')", 270 -1, &pCk, 0); 271 if( rc==SQLITE_OK ){ 272 if( eVerbosity>=2 ){ 273 char *zSql = sqlite3_expanded_sql(pCk); 274 printf("invariant-validity-check #4:\n%s\n", zSql); 275 sqlite3_free(zSql); 276 } 277 sqlite3_bind_pointer(pCk, 1, pStmt, "stmt-pointer", 0); 278 rc = sqlite3_step(pCk); 279 } 280 sqlite3_finalize(pCk); 281 if( rc==SQLITE_DONE ){ 282 reportInvariantFailed(pStmt, pTestStmt, iRow, dbOpt, noOpt); 283 return SQLITE_INTERNAL; 284 }else if( eVerbosity>0 ){ 285 printf("invariant-error ignored due to the use of virtual tables\n"); 286 } 287 } 288 not_a_fault: 289 sqlite3_finalize(pTestStmt); 290 return SQLITE_OK; 291 } 292 293 /* 294 ** Generate SQL used to test a statement invariant. 295 ** 296 ** Return 0 if the iCnt is out of range. 297 ** 298 ** iCnt meanings: 299 ** 300 ** 0 SELECT * FROM () 301 ** 1 SELECT DISTINCT * FROM () 302 ** 2 SELECT * FROM () WHERE ORDER BY 1 303 ** 3 SELECT DISTINCT * FROM () ORDER BY 1 304 ** 4 SELECT * FROM () WHERE = 305 ** 5 SELECT DISTINCT * FROM () WHERE ) WHERE = ORDER BY 1 307 ** 7 SELECT DISTINCT * FROM () WHERE = 308 ** ORDER BY 1 309 ** N+0 SELECT * FROM () WHERE = 310 ** N+1 SELECT DISTINCT * FROM () WHERE = 311 ** N+2 SELECT * FROM () WHERE = ORDER BY 1 312 ** N+3 SELECT DISTINCT * FROM () WHERE = 313 ** ORDER BY N 314 ** 315 */ 316 static char *fuzz_invariant_sql(sqlite3_stmt *pStmt, int iCnt){ 317 const char *zIn; 318 size_t nIn; 319 const char *zAnd = "WHERE"; 320 int i, j; 321 sqlite3_str *pTest; 322 sqlite3_stmt *pBase = 0; 323 sqlite3 *db = sqlite3_db_handle(pStmt); 324 int rc; 325 int nCol = sqlite3_column_count(pStmt); 326 int mxCnt; 327 int bDistinct = 0; 328 int bOrderBy = 0; 329 int nParam = sqlite3_bind_parameter_count(pStmt); 330 int hasGroupBy = 0; 331 332 switch( iCnt % 4 ){ 333 case 1: bDistinct = 1; break; 334 case 2: bOrderBy = 1; break; 335 case 3: bDistinct = bOrderBy = 1; break; 336 } 337 iCnt /= 4; 338 mxCnt = nCol; 339 if( iCnt<0 || iCnt>mxCnt ) return 0; 340 zIn = sqlite3_sql(pStmt); 341 if( zIn==0 ) return 0; 342 nIn = strlen(zIn); 343 while( nIn>0 && (isspace((unsigned char)zIn[nIn-1]) || zIn[nIn-1]==';') ) nIn--; 344 if( strchr(zIn, '?') ) return 0; 345 pTest = sqlite3_str_new(0); 346 sqlite3_str_appendf(pTest, "SELECT %s* FROM (", 347 bDistinct ? "DISTINCT " : ""); 348 sqlite3_str_append(pTest, zIn, (int)nIn); 349 sqlite3_str_append(pTest, ")", 1); 350 rc = sqlite3_prepare_v2(db, sqlite3_str_value(pTest), -1, &pBase, 0); 351 if( rc ){ 352 sqlite3_finalize(pBase); 353 pBase = pStmt; 354 } 355 hasGroupBy = sqlite3_strlike("%GROUP BY%",zIn,0)==0; 356 bindDebugParameters(pBase); 357 for(i=0; i'3' || isdigit((unsigned char)zSuffix[2])) 363 ){ 364 /* This is a randomized column name and so cannot be used in the 365 ** WHERE clause. */ 366 continue; 367 } 368 for(j=0; j1 && i+2!=iCnt ) continue; 378 if( zColName==0 ) continue; 379 if( sqlite3_column_type(pStmt, i)==SQLITE_NULL ){ 380 const char *zPlus = hasGroupBy ? "+" : ""; 381 sqlite3_str_appendf(pTest, " %s %s\"%w\" ISNULL", zAnd, zPlus, zColName); 382 }else{ 383 sqlite3_str_appendf(pTest, " %s \"%w\"=?%d", zAnd, zColName, 384 i+1+nParam); 385 } 386 zAnd = "AND"; 387 } 388 if( pBase!=pStmt ) sqlite3_finalize(pBase); 389 if( bOrderBy ){ 390 sqlite3_str_appendf(pTest, " ORDER BY %d", iCnt>2 ? iCnt-1 : 1); 391 } 392 return sqlite3_str_finish(pTest); 393 } 394 395 /* 396 ** Return true if and only if v1 and is the same as v2. 397 */ 398 static int sameValue( 399 sqlite3_stmt *pS1, int i1, /* Value to text on the left */ 400 sqlite3_stmt *pS2, int i2, /* Value to test on the right */ 401 sqlite3_stmt *pTestCompare /* COLLATE comparison statement or NULL */ 402 ){ 403 int x = 1; 404 int t1 = sqlite3_column_type(pS1,i1); 405 int t2 = sqlite3_column_type(pS2,i2); 406 if( t1!=t2 ){ 407 if( (t1==SQLITE_INTEGER && t2==SQLITE_FLOAT) 408 || (t1==SQLITE_FLOAT && t2==SQLITE_INTEGER) 409 ){ 410 /* Comparison of numerics is ok */ 411 }else{ 412 return 0; 413 } 414 } 415 switch( sqlite3_column_type(pS1,i1) ){ 416 case SQLITE_INTEGER: { 417 x = sqlite3_column_int64(pS1,i1)==sqlite3_column_int64(pS2,i2); 418 break; 419 } 420 case SQLITE_FLOAT: { 421 x = sqlite3_column_double(pS1,i1)==sqlite3_column_double(pS2,i2); 422 break; 423 } 424 case SQLITE_TEXT: { 425 int e1 = sqlite3_value_encoding(sqlite3_column_value(pS1,i1)); 426 int e2 = sqlite3_value_encoding(sqlite3_column_value(pS2,i2)); 427 if( e1!=e2 ){ 428 const char *z1 = (const char*)sqlite3_column_text(pS1,i1); 429 const char *z2 = (const char*)sqlite3_column_text(pS2,i2); 430 x = ((z1==0 && z2==0) || (z1!=0 && z2!=0 && strcmp(z1,z1)==0)); 431 printf("Encodings differ. %d on left and %d on right\n", e1, e2); 432 abort(); 433 } 434 if( pTestCompare ){ 435 sqlite3_bind_value(pTestCompare, 1, sqlite3_column_value(pS1,i1)); 436 sqlite3_bind_value(pTestCompare, 2, sqlite3_column_value(pS2,i2)); 437 x = sqlite3_step(pTestCompare)==SQLITE_ROW 438 && sqlite3_column_int(pTestCompare,0)!=0; 439 sqlite3_reset(pTestCompare); 440 break; 441 } 442 if( e1!=SQLITE_UTF8 ){ 443 int len1 = sqlite3_column_bytes16(pS1,i1); 444 const unsigned char *b1 = sqlite3_column_blob(pS1,i1); 445 int len2 = sqlite3_column_bytes16(pS2,i2); 446 const unsigned char *b2 = sqlite3_column_blob(pS2,i2); 447 if( len1!=len2 ){ 448 x = 0; 449 }else if( len1==0 ){ 450 x = 1; 451 }else{ 452 x = (b1!=0 && b2!=0 && memcmp(b1,b2,len1)==0); 453 } 454 break; 455 } 456 /* Fall through into the SQLITE_BLOB case */ 457 } 458 case SQLITE_BLOB: { 459 int len1 = sqlite3_column_bytes(pS1,i1); 460 const unsigned char *b1 = sqlite3_column_blob(pS1,i1); 461 int len2 = sqlite3_column_bytes(pS2,i2); 462 const unsigned char *b2 = sqlite3_column_blob(pS2,i2); 463 if( len1!=len2 ){ 464 x = 0; 465 }else if( len1==0 ){ 466 x = 1; 467 }else{ 468 x = (b1!=0 && b2!=0 && memcmp(b1,b2,len1)==0); 469 } 470 break; 471 } 472 } 473 return x; 474 } 475 476 /* 477 ** Print binary data as hex 478 */ 479 static void printHex(const unsigned char *a, int n, int mx){ 480 int j; 481 for(j=0; j