4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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.
11 *************************************************************************
13 ** This library is used by fuzzcheck to test query invariants.
15 ** An sqlite3_stmt is passed in that has just returned SQLITE_ROW. This
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
30 /* Forward references */
31 static char *fuzz_invariant_sql(sqlite3_stmt
*, int);
32 static int sameValue(sqlite3_stmt
*,int,sqlite3_stmt
*,int,sqlite3_stmt
*);
33 static void reportInvariantFailed(
34 sqlite3_stmt
*pOrig
, /* The original query */
35 sqlite3_stmt
*pTest
, /* The alternative test query with a missing row */
36 int iRow
, /* Row number in pOrig */
37 unsigned int dbOpt
, /* Optimization flags on pOrig */
38 int noOpt
/* True if opt flags inverted for pTest */
42 ** Do an invariant check on pStmt. iCnt determines which invariant check to
43 ** perform. The first check is iCnt==0.
45 ** *pbCorrupt is a flag that, if true, indicates that the database file
46 ** is known to be corrupt. A value of non-zero means "yes, the database
47 ** is corrupt". A zero value means "we do not know whether or not the
48 ** database is corrupt". The value might be set prior to entry, or this
49 ** routine might set the value.
53 ** SQLITE_OK This check was successful.
55 ** SQLITE_DONE iCnt is out of range. The caller typically sets
56 ** up a loop on iCnt starting with zero, and increments
57 ** iCnt until this code is returned.
59 ** SQLITE_CORRUPT The invariant failed, but the underlying database
60 ** file is indicating that it is corrupt, which might
61 ** be the cause of the malfunction. The *pCorrupt
62 ** value will also be set.
64 ** SQLITE_INTERNAL The invariant failed, and the database file is not
65 ** corrupt. (This never happens because this function
66 ** will call abort() following an invariant failure.)
68 ** (other) Some other kind of error occurred.
71 sqlite3
*db
, /* The database connection */
72 sqlite3_stmt
*pStmt
, /* Test statement stopped on an SQLITE_ROW */
73 int iCnt
, /* Invariant sequence number, starting at 0 */
74 int iRow
, /* Current row number */
75 int nRow
, /* Number of output rows from pStmt */
76 int *pbCorrupt
, /* IN/OUT: Flag indicating a corrupt database file */
77 int eVerbosity
, /* How much debugging output */
78 unsigned int dbOpt
/* Default optimization flags */
81 sqlite3_stmt
*pTestStmt
= 0;
86 int noOpt
= (iCnt
%3)==0;
88 if( *pbCorrupt
) return SQLITE_DONE
;
89 nParam
= sqlite3_bind_parameter_count(pStmt
);
90 if( nParam
>100 ) return SQLITE_DONE
;
91 zTest
= fuzz_invariant_sql(pStmt
, iCnt
);
92 if( zTest
==0 ) return SQLITE_DONE
;
94 sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS
, db
, ~dbOpt
);
96 rc
= sqlite3_prepare_v2(db
, zTest
, -1, &pTestStmt
, 0);
98 sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS
, db
, dbOpt
);
102 printf("invariant compile failed: %s\n%s\n",
103 sqlite3_errmsg(db
), zTest
);
106 sqlite3_finalize(pTestStmt
);
110 nCol
= sqlite3_column_count(pStmt
);
111 for(i
=0; i
<nCol
; i
++){
112 rc
= sqlite3_bind_value(pTestStmt
,i
+1+nParam
,sqlite3_column_value(pStmt
,i
));
113 if( rc
!=SQLITE_OK
&& rc
!=SQLITE_RANGE
){
114 sqlite3_finalize(pTestStmt
);
119 char *zSql
= sqlite3_expanded_sql(pTestStmt
);
120 printf("invariant-sql row=%d #%d:\n%s\n", iRow
, iCnt
, zSql
);
123 while( (rc
= sqlite3_step(pTestStmt
))==SQLITE_ROW
){
124 for(i
=0; i
<nCol
; i
++){
125 if( !sameValue(pStmt
, i
, pTestStmt
, i
, 0) ) break;
129 if( rc
==SQLITE_DONE
){
130 /* No matching output row found */
131 sqlite3_stmt
*pCk
= 0;
135 /* This is not a fault if the database file is corrupt, because anything
136 ** can happen with a corrupt database file */
137 rc
= sqlite3_prepare_v2(db
, "PRAGMA integrity_check", -1, &pCk
, 0);
139 sqlite3_finalize(pCk
);
140 sqlite3_finalize(pTestStmt
);
144 char *zSql
= sqlite3_expanded_sql(pCk
);
145 printf("invariant-validity-check #1:\n%s\n", zSql
);
149 rc
= sqlite3_step(pCk
);
151 || sqlite3_column_text(pCk
, 0)==0
152 || strcmp((const char*)sqlite3_column_text(pCk
,0),"ok")!=0
155 sqlite3_finalize(pCk
);
156 sqlite3_finalize(pTestStmt
);
157 return SQLITE_CORRUPT
;
159 sqlite3_finalize(pCk
);
162 ** If inverting the scan order also results in a miss, assume that the
163 ** query is ambiguous and do not report a fault.
165 sqlite3_db_config(db
, SQLITE_DBCONFIG_REVERSE_SCANORDER
, -1, &iOrigRSO
);
166 sqlite3_db_config(db
, SQLITE_DBCONFIG_REVERSE_SCANORDER
, !iOrigRSO
, 0);
167 sqlite3_prepare_v2(db
, sqlite3_sql(pStmt
), -1, &pCk
, 0);
168 sqlite3_db_config(db
, SQLITE_DBCONFIG_REVERSE_SCANORDER
, iOrigRSO
, 0);
170 char *zSql
= sqlite3_expanded_sql(pCk
);
171 printf("invariant-validity-check #2:\n%s\n", zSql
);
174 while( (rc
= sqlite3_step(pCk
))==SQLITE_ROW
){
175 for(i
=0; i
<nCol
; i
++){
176 if( !sameValue(pStmt
, i
, pTestStmt
, i
, 0) ) break;
180 sqlite3_finalize(pCk
);
181 if( rc
==SQLITE_DONE
){
182 sqlite3_finalize(pTestStmt
);
186 /* The original sameValue() comparison assumed a collating sequence
187 ** of "binary". It can sometimes get an incorrect result for different
188 ** collating sequences. So rerun the test with no assumptions about
191 rc
= sqlite3_prepare_v2(db
,
192 "SELECT ?1=?2 OR ?1=?2 COLLATE nocase OR ?1=?2 COLLATE rtrim",
196 char *zSql
= sqlite3_expanded_sql(pCk
);
197 printf("invariant-validity-check #3:\n%s\n", zSql
);
201 sqlite3_reset(pTestStmt
);
202 while( (rc
= sqlite3_step(pTestStmt
))==SQLITE_ROW
){
203 for(i
=0; i
<nCol
; i
++){
204 if( !sameValue(pStmt
, i
, pTestStmt
, i
, pCk
) ) break;
207 sqlite3_finalize(pCk
);
212 sqlite3_finalize(pCk
);
214 /* Invariants do not necessarily work if there are virtual tables
215 ** involved in the query */
216 rc
= sqlite3_prepare_v2(db
,
217 "SELECT 1 FROM bytecode(?1) WHERE opcode='VOpen'", -1, &pCk
, 0);
220 char *zSql
= sqlite3_expanded_sql(pCk
);
221 printf("invariant-validity-check #4:\n%s\n", zSql
);
224 sqlite3_bind_pointer(pCk
, 1, pStmt
, "stmt-pointer", 0);
225 rc
= sqlite3_step(pCk
);
227 sqlite3_finalize(pCk
);
228 if( rc
==SQLITE_DONE
){
229 reportInvariantFailed(pStmt
, pTestStmt
, iRow
, dbOpt
, noOpt
);
230 return SQLITE_INTERNAL
;
231 }else if( eVerbosity
>0 ){
232 printf("invariant-error ignored due to the use of virtual tables\n");
236 sqlite3_finalize(pTestStmt
);
241 ** Generate SQL used to test a statement invariant.
243 ** Return 0 if the iCnt is out of range.
247 ** 0 SELECT * FROM (<query>)
248 ** 1 SELECT DISTINCT * FROM (<query>)
249 ** 2 SELECT * FROM (<query>) WHERE ORDER BY 1
250 ** 3 SELECT DISTINCT * FROM (<query>) ORDER BY 1
251 ** 4 SELECT * FROM (<query>) WHERE <all-columns>=<all-values>
252 ** 5 SELECT DISTINCT * FROM (<query>) WHERE <all-columns=<all-values
253 ** 6 SELECT * FROM (<query>) WHERE <all-column>=<all-value> ORDER BY 1
254 ** 7 SELECT DISTINCT * FROM (<query>) WHERE <all-column>=<all-value>
256 ** N+0 SELECT * FROM (<query>) WHERE <nth-column>=<value>
257 ** N+1 SELECT DISTINCT * FROM (<query>) WHERE <Nth-column>=<value>
258 ** N+2 SELECT * FROM (<query>) WHERE <Nth-column>=<value> ORDER BY 1
259 ** N+3 SELECT DISTINCT * FROM (<query>) WHERE <Nth-column>=<value>
263 static char *fuzz_invariant_sql(sqlite3_stmt
*pStmt
, int iCnt
){
266 const char *zAnd
= "WHERE";
269 sqlite3_stmt
*pBase
= 0;
270 sqlite3
*db
= sqlite3_db_handle(pStmt
);
272 int nCol
= sqlite3_column_count(pStmt
);
276 int nParam
= sqlite3_bind_parameter_count(pStmt
);
279 case 1: bDistinct
= 1; break;
280 case 2: bOrderBy
= 1; break;
281 case 3: bDistinct
= bOrderBy
= 1; break;
285 if( iCnt
<0 || iCnt
>mxCnt
) return 0;
286 zIn
= sqlite3_sql(pStmt
);
287 if( zIn
==0 ) return 0;
289 while( nIn
>0 && (isspace(zIn
[nIn
-1]) || zIn
[nIn
-1]==';') ) nIn
--;
290 if( strchr(zIn
, '?') ) return 0;
291 pTest
= sqlite3_str_new(0);
292 sqlite3_str_appendf(pTest
, "SELECT %s* FROM (",
293 bDistinct
? "DISTINCT " : "");
294 sqlite3_str_append(pTest
, zIn
, (int)nIn
);
295 sqlite3_str_append(pTest
, ")", 1);
296 rc
= sqlite3_prepare_v2(db
, sqlite3_str_value(pTest
), -1, &pBase
, 0);
298 sqlite3_finalize(pBase
);
301 for(i
=0; i
<sqlite3_column_count(pStmt
); i
++){
302 const char *zColName
= sqlite3_column_name(pBase
,i
);
303 const char *zSuffix
= zColName
? strrchr(zColName
, ':') : 0;
305 && isdigit(zSuffix
[1])
306 && (zSuffix
[1]>'3' || isdigit(zSuffix
[2]))
308 /* This is a randomized column name and so cannot be used in the
313 const char *zPrior
= sqlite3_column_name(pBase
, j
);
314 if( sqlite3_stricmp(zPrior
, zColName
)==0 ) break;
317 /* Duplicate column name */
320 if( iCnt
==0 ) continue;
321 if( iCnt
>1 && i
+2!=iCnt
) continue;
322 if( zColName
==0 ) continue;
323 if( sqlite3_column_type(pStmt
, i
)==SQLITE_NULL
){
324 sqlite3_str_appendf(pTest
, " %s \"%w\" ISNULL", zAnd
, zColName
);
326 sqlite3_str_appendf(pTest
, " %s \"%w\"=?%d", zAnd
, zColName
,
331 if( pBase
!=pStmt
) sqlite3_finalize(pBase
);
333 sqlite3_str_appendf(pTest
, " ORDER BY %d", iCnt
>2 ? iCnt
-1 : 1);
335 return sqlite3_str_finish(pTest
);
339 ** Return true if and only if v1 and is the same as v2.
341 static int sameValue(
342 sqlite3_stmt
*pS1
, int i1
, /* Value to text on the left */
343 sqlite3_stmt
*pS2
, int i2
, /* Value to test on the right */
344 sqlite3_stmt
*pTestCompare
/* COLLATE comparison statement or NULL */
347 int t1
= sqlite3_column_type(pS1
,i1
);
348 int t2
= sqlite3_column_type(pS2
,i2
);
350 if( (t1
==SQLITE_INTEGER
&& t2
==SQLITE_FLOAT
)
351 || (t1
==SQLITE_FLOAT
&& t2
==SQLITE_INTEGER
)
353 /* Comparison of numerics is ok */
358 switch( sqlite3_column_type(pS1
,i1
) ){
359 case SQLITE_INTEGER
: {
360 x
= sqlite3_column_int64(pS1
,i1
)==sqlite3_column_int64(pS2
,i2
);
364 x
= sqlite3_column_double(pS1
,i1
)==sqlite3_column_double(pS2
,i2
);
368 int e1
= sqlite3_value_encoding(sqlite3_column_value(pS1
,i1
));
369 int e2
= sqlite3_value_encoding(sqlite3_column_value(pS2
,i2
));
371 const char *z1
= (const char*)sqlite3_column_text(pS1
,i1
);
372 const char *z2
= (const char*)sqlite3_column_text(pS2
,i2
);
373 x
= ((z1
==0 && z2
==0) || (z1
!=0 && z2
!=0 && strcmp(z1
,z1
)==0));
374 printf("Encodings differ. %d on left and %d on right\n", e1
, e2
);
378 sqlite3_bind_value(pTestCompare
, 1, sqlite3_column_value(pS1
,i1
));
379 sqlite3_bind_value(pTestCompare
, 2, sqlite3_column_value(pS2
,i2
));
380 x
= sqlite3_step(pTestCompare
)==SQLITE_ROW
381 && sqlite3_column_int(pTestCompare
,0)!=0;
382 sqlite3_reset(pTestCompare
);
385 if( e1
!=SQLITE_UTF8
){
386 int len1
= sqlite3_column_bytes16(pS1
,i1
);
387 const unsigned char *b1
= sqlite3_column_blob(pS1
,i1
);
388 int len2
= sqlite3_column_bytes16(pS2
,i2
);
389 const unsigned char *b2
= sqlite3_column_blob(pS2
,i2
);
395 x
= (b1
!=0 && b2
!=0 && memcmp(b1
,b2
,len1
)==0);
399 /* Fall through into the SQLITE_BLOB case */
402 int len1
= sqlite3_column_bytes(pS1
,i1
);
403 const unsigned char *b1
= sqlite3_column_blob(pS1
,i1
);
404 int len2
= sqlite3_column_bytes(pS2
,i2
);
405 const unsigned char *b2
= sqlite3_column_blob(pS2
,i2
);
411 x
= (b1
!=0 && b2
!=0 && memcmp(b1
,b2
,len1
)==0);
420 ** Print binary data as hex
422 static void printHex(const unsigned char *a
, int n
, int mx
){
424 for(j
=0; j
<mx
&& j
<n
; j
++){
425 printf("%02x", a
[j
]);
427 if( j
<n
) printf("...");
431 ** Print a single row from the prepared statement
433 static void printRow(sqlite3_stmt
*pStmt
, int iRow
){
435 unsigned const char *data
;
436 nCol
= sqlite3_column_count(pStmt
);
437 for(i
=0; i
<nCol
; i
++){
438 printf("row%d.col%d = ", iRow
, i
);
439 switch( sqlite3_column_type(pStmt
, i
) ){
444 case SQLITE_INTEGER
: {
445 printf("(integer) %lld\n", sqlite3_column_int64(pStmt
, i
));
449 printf("(float) %f\n", sqlite3_column_double(pStmt
, i
));
453 switch( sqlite3_value_encoding(sqlite3_column_value(pStmt
,i
)) ){
456 n
= sqlite3_column_bytes(pStmt
, i
);
457 data
= sqlite3_column_blob(pStmt
, i
);
458 printHex(data
, n
, 35);
462 case SQLITE_UTF16BE
: {
463 printf("(utf16be) x'");
464 n
= sqlite3_column_bytes16(pStmt
, i
);
465 data
= sqlite3_column_blob(pStmt
, i
);
466 printHex(data
, n
, 35);
470 case SQLITE_UTF16LE
: {
471 printf("(utf16le) x'");
472 n
= sqlite3_column_bytes16(pStmt
, i
);
473 data
= sqlite3_column_blob(pStmt
, i
);
474 printHex(data
, n
, 35);
479 printf("Illegal return from sqlite3_value_encoding(): %d\n",
480 sqlite3_value_encoding(sqlite3_column_value(pStmt
,i
)));
487 n
= sqlite3_column_bytes(pStmt
, i
);
488 data
= sqlite3_column_blob(pStmt
, i
);
489 printf("(blob %d bytes) x'", n
);
490 printHex(data
, n
, 35);
499 ** Report a failure of the invariant: The current output row of pOrig
500 ** does not appear in any row of the output from pTest.
502 static void reportInvariantFailed(
503 sqlite3_stmt
*pOrig
, /* The original query */
504 sqlite3_stmt
*pTest
, /* The alternative test query with a missing row */
505 int iRow
, /* Row number in pOrig */
506 unsigned int dbOpt
, /* Optimization flags on pOrig */
507 int noOpt
/* True if opt flags inverted for pTest */
510 printf("Invariant check failed on row %d.\n", iRow
);
511 printf("Original query (opt-flags: 0x%08x) --------------------------\n",
513 printf("%s\n", sqlite3_expanded_sql(pOrig
));
514 printf("Alternative query (opt-flags: 0x%08x) -----------------------\n",
515 noOpt
? ~dbOpt
: dbOpt
);
516 printf("%s\n", sqlite3_expanded_sql(pTest
));
517 printf("Result row that is missing from the alternative -----------------\n");
518 printRow(pOrig
, iRow
);
519 printf("Complete results from the alternative query ---------------------\n");
520 sqlite3_reset(pTest
);
521 while( sqlite3_step(pTest
)==SQLITE_ROW
){
523 printRow(pTest
, iTestRow
);
525 sqlite3_finalize(pTest
);