blob: a2a5028d1bcef614943d989946b404b94a0a1668 [file] [log] [blame]
drha1f79da2022-06-14 19:12:25 +00001/*
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 <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <ctype.h>
29
30/* Forward references */
31static char *fuzz_invariant_sql(sqlite3_stmt*, int);
drh6efabd62022-06-17 12:25:33 +000032static int sameValue(sqlite3_stmt*,int,sqlite3_stmt*,int);
drha1f79da2022-06-14 19:12:25 +000033static void reportInvariantFailed(sqlite3_stmt*,sqlite3_stmt*,int);
34
35/*
36** Do an invariant check on pStmt. iCnt determines which invariant check to
37** perform. The first check is iCnt==0.
38**
39** *pbCorrupt is a flag that, if true, indicates that the database file
40** is known to be corrupt. A value of non-zero means "yes, the database
41** is corrupt". A zero value means "we do not know whether or not the
42** database is corrupt". The value might be set prior to entry, or this
43** routine might set the value.
44**
45** Return values:
46**
47** SQLITE_OK This check was successful.
48**
49** SQLITE_DONE iCnt is out of range.
50**
51** SQLITE_CORRUPT The invariant failed, but the underlying database
52** file is indicating that it is corrupt, which might
53** be the cause of the malfunction.
54**
55** SQLITE_INTERNAL The invariant failed, and the database file is not
56** corrupt. (This never happens because this function
57** will call abort() following an invariant failure.)
58**
59** (other) Some other kind of error occurred.
60*/
61int fuzz_invariant(
62 sqlite3 *db, /* The database connection */
63 sqlite3_stmt *pStmt, /* Test statement stopped on an SQLITE_ROW */
64 int iCnt, /* Invariant sequence number, starting at 0 */
drh63880362022-06-17 16:09:47 +000065 int iRow, /* Current row number */
66 int nRow, /* Number of output rows from pStmt */
drhe3bf2c82022-06-15 16:26:37 +000067 int *pbCorrupt, /* IN/OUT: Flag indicating a corrupt database file */
68 int eVerbosity /* How much debugging output */
drha1f79da2022-06-14 19:12:25 +000069){
70 char *zTest;
71 sqlite3_stmt *pTestStmt = 0;
72 int rc;
73 int i;
74 int nCol;
drh36f904f2022-06-17 15:11:31 +000075 int nParam;
drha1f79da2022-06-14 19:12:25 +000076
77 if( *pbCorrupt ) return SQLITE_DONE;
drh36f904f2022-06-17 15:11:31 +000078 nParam = sqlite3_bind_parameter_count(pStmt);
79 if( nParam>100 ) return SQLITE_DONE;
drha1f79da2022-06-14 19:12:25 +000080 zTest = fuzz_invariant_sql(pStmt, iCnt);
81 if( zTest==0 ) return SQLITE_DONE;
82 rc = sqlite3_prepare_v2(db, zTest, -1, &pTestStmt, 0);
drha1f79da2022-06-14 19:12:25 +000083 if( rc ){
drhe3bf2c82022-06-15 16:26:37 +000084 if( eVerbosity ){
85 printf("invariant compile failed: %s\n%s\n",
86 sqlite3_errmsg(db), zTest);
87 }
88 sqlite3_free(zTest);
drha1f79da2022-06-14 19:12:25 +000089 sqlite3_finalize(pTestStmt);
90 return rc;
91 }
drhe3bf2c82022-06-15 16:26:37 +000092 sqlite3_free(zTest);
drha1f79da2022-06-14 19:12:25 +000093 nCol = sqlite3_column_count(pStmt);
94 for(i=0; i<nCol; i++){
drhd0d21f52022-06-18 14:50:43 +000095 rc = sqlite3_bind_value(pTestStmt,i+1+nParam,sqlite3_column_value(pStmt,i));
96 if( rc!=SQLITE_OK && rc!=SQLITE_RANGE ){
97 sqlite3_finalize(pTestStmt);
98 return rc;
99 }
drha1f79da2022-06-14 19:12:25 +0000100 }
drhe3bf2c82022-06-15 16:26:37 +0000101 if( eVerbosity>=2 ){
102 char *zSql = sqlite3_expanded_sql(pTestStmt);
103 printf("invariant-sql #%d:\n%s\n", iCnt, zSql);
104 sqlite3_free(zSql);
105 }
drha1f79da2022-06-14 19:12:25 +0000106 while( (rc = sqlite3_step(pTestStmt))==SQLITE_ROW ){
107 for(i=0; i<nCol; i++){
drh6efabd62022-06-17 12:25:33 +0000108 if( !sameValue(pStmt, i, pTestStmt, i) ) break;
drha1f79da2022-06-14 19:12:25 +0000109 }
110 if( i>=nCol ) break;
111 }
drh13736992022-06-18 20:20:30 +0000112 if( rc==SQLITE_DONE ){
drha1f79da2022-06-14 19:12:25 +0000113 /* No matching output row found */
114 sqlite3_stmt *pCk = 0;
115 rc = sqlite3_prepare_v2(db, "PRAGMA integrity_check", -1, &pCk, 0);
116 if( rc ){
117 sqlite3_finalize(pCk);
118 sqlite3_finalize(pTestStmt);
119 return rc;
120 }
121 rc = sqlite3_step(pCk);
122 if( rc!=SQLITE_ROW
123 || sqlite3_column_text(pCk, 0)==0
124 || strcmp((const char*)sqlite3_column_text(pCk,0),"ok")!=0
125 ){
126 *pbCorrupt = 1;
127 sqlite3_finalize(pCk);
128 sqlite3_finalize(pTestStmt);
129 return SQLITE_CORRUPT;
130 }
131 sqlite3_finalize(pCk);
drha913f9b2022-06-15 10:37:16 +0000132 rc = sqlite3_prepare_v2(db,
133 "SELECT 1 FROM bytecode(?1) WHERE opcode='VOpen'", -1, &pCk, 0);
drh36f904f2022-06-17 15:11:31 +0000134 if( rc==SQLITE_OK ){
135 sqlite3_bind_pointer(pCk, 1, pStmt, "stmt-pointer", 0);
136 rc = sqlite3_step(pCk);
137 }
drha913f9b2022-06-15 10:37:16 +0000138 sqlite3_finalize(pCk);
139 if( rc==SQLITE_DONE ){
140 reportInvariantFailed(pStmt, pTestStmt, iRow);
141 return SQLITE_INTERNAL;
drhe3bf2c82022-06-15 16:26:37 +0000142 }else if( eVerbosity>0 ){
143 printf("invariant-error ignored due to the use of virtual tables\n");
drha913f9b2022-06-15 10:37:16 +0000144 }
drha1f79da2022-06-14 19:12:25 +0000145 }
146 sqlite3_finalize(pTestStmt);
147 return SQLITE_OK;
148}
149
150
151/*
152** Generate SQL used to test a statement invariant.
153**
154** Return 0 if the iCnt is out of range.
155*/
156static char *fuzz_invariant_sql(sqlite3_stmt *pStmt, int iCnt){
157 const char *zIn;
158 size_t nIn;
159 const char *zAnd = "WHERE";
160 int i;
161 sqlite3_str *pTest;
162 sqlite3_stmt *pBase = 0;
163 sqlite3 *db = sqlite3_db_handle(pStmt);
164 int rc;
drhe3bf2c82022-06-15 16:26:37 +0000165 int nCol = sqlite3_column_count(pStmt);
166 int mxCnt;
drh8f9261a2022-06-15 20:18:44 +0000167 int bDistinct = 0;
168 int bOrderBy = 0;
drh36f904f2022-06-17 15:11:31 +0000169 int nParam = sqlite3_bind_parameter_count(pStmt);
drha1f79da2022-06-14 19:12:25 +0000170
drh63880362022-06-17 16:09:47 +0000171 iCnt++;
drh8f9261a2022-06-15 20:18:44 +0000172 switch( iCnt % 4 ){
173 case 1: bDistinct = 1; break;
174 case 2: bOrderBy = 1; break;
175 case 3: bDistinct = bOrderBy = 1; break;
176 }
177 iCnt /= 4;
drh63880362022-06-17 16:09:47 +0000178 mxCnt = nCol;
drhe3bf2c82022-06-15 16:26:37 +0000179 if( iCnt<0 || iCnt>mxCnt ) return 0;
drha1f79da2022-06-14 19:12:25 +0000180 zIn = sqlite3_sql(pStmt);
181 if( zIn==0 ) return 0;
182 nIn = strlen(zIn);
183 while( nIn>0 && (isspace(zIn[nIn-1]) || zIn[nIn-1]==';') ) nIn--;
184 if( strchr(zIn, '?') ) return 0;
185 pTest = sqlite3_str_new(0);
drh13736992022-06-18 20:20:30 +0000186 sqlite3_str_appendf(pTest, "SELECT %s* FROM (%s",
187 bDistinct ? "DISTINCT " : "", zIn);
188 sqlite3_str_appendf(pTest, ")");
drha1f79da2022-06-14 19:12:25 +0000189 rc = sqlite3_prepare_v2(db, sqlite3_str_value(pTest), -1, &pBase, 0);
190 if( rc ){
191 sqlite3_finalize(pBase);
192 pBase = pStmt;
193 }
194 for(i=0; i<sqlite3_column_count(pStmt); i++){
drha913f9b2022-06-15 10:37:16 +0000195 const char *zColName = sqlite3_column_name(pBase,i);
drh3e245bc2022-06-16 20:29:36 +0000196 const char *zSuffix = zColName ? strchr(zColName, ':') : 0;
drha913f9b2022-06-15 10:37:16 +0000197 if( zSuffix
198 && isdigit(zSuffix[1])
199 && (zSuffix[1]>'3' || isdigit(zSuffix[2]))
200 ){
201 /* This is a randomized column name and so cannot be used in the
202 ** WHERE clause. */
203 continue;
204 }
drh63880362022-06-17 16:09:47 +0000205 if( i+1!=iCnt ) continue;
drh8fa62062022-06-18 10:26:12 +0000206 if( zColName==0 ) continue;
drha1f79da2022-06-14 19:12:25 +0000207 if( sqlite3_column_type(pStmt, i)==SQLITE_NULL ){
drha913f9b2022-06-15 10:37:16 +0000208 sqlite3_str_appendf(pTest, " %s \"%w\" ISNULL", zAnd, zColName);
drha1f79da2022-06-14 19:12:25 +0000209 }else{
drh36f904f2022-06-17 15:11:31 +0000210 sqlite3_str_appendf(pTest, " %s \"%w\"=?%d", zAnd, zColName,
211 i+1+nParam);
drha1f79da2022-06-14 19:12:25 +0000212 }
213 zAnd = "AND";
214 }
215 if( pBase!=pStmt ) sqlite3_finalize(pBase);
drh8f9261a2022-06-15 20:18:44 +0000216 if( bOrderBy ){
217 sqlite3_str_appendf(pTest, " ORDER BY 1");
218 }
drha1f79da2022-06-14 19:12:25 +0000219 return sqlite3_str_finish(pTest);
220}
221
222/*
223** Return true if and only if v1 and is the same as v2.
224*/
drh6efabd62022-06-17 12:25:33 +0000225static int sameValue(sqlite3_stmt *pS1, int i1, sqlite3_stmt *pS2, int i2){
drha1f79da2022-06-14 19:12:25 +0000226 int x = 1;
drhea64cb32022-06-17 16:32:21 +0000227 int t1 = sqlite3_column_type(pS1,i1);
228 int t2 = sqlite3_column_type(pS2,i2);
229 if( t1!=t2 ){
230 if( (t1==SQLITE_INTEGER && t2==SQLITE_FLOAT)
231 || (t1==SQLITE_FLOAT && t2==SQLITE_INTEGER)
232 ){
233 /* Comparison of numerics is ok */
234 }else{
235 return 0;
236 }
237 }
drh6efabd62022-06-17 12:25:33 +0000238 switch( sqlite3_column_type(pS1,i1) ){
drha1f79da2022-06-14 19:12:25 +0000239 case SQLITE_INTEGER: {
drh6efabd62022-06-17 12:25:33 +0000240 x = sqlite3_column_int64(pS1,i1)==sqlite3_column_int64(pS2,i2);
drha1f79da2022-06-14 19:12:25 +0000241 break;
242 }
243 case SQLITE_FLOAT: {
drh6efabd62022-06-17 12:25:33 +0000244 x = sqlite3_column_double(pS1,i1)==sqlite3_column_double(pS2,i2);
drha1f79da2022-06-14 19:12:25 +0000245 break;
246 }
247 case SQLITE_TEXT: {
drh6efabd62022-06-17 12:25:33 +0000248 const char *z1 = (const char*)sqlite3_column_text(pS1,i1);
249 const char *z2 = (const char*)sqlite3_column_text(pS2,i2);
drha1f79da2022-06-14 19:12:25 +0000250 x = ((z1==0 && z2==0) || (z1!=0 && z2!=0 && strcmp(z1,z1)==0));
251 break;
252 }
253 case SQLITE_BLOB: {
drh6efabd62022-06-17 12:25:33 +0000254 int len1 = sqlite3_column_bytes(pS1,i1);
255 const unsigned char *b1 = sqlite3_column_blob(pS1,i1);
256 int len2 = sqlite3_column_bytes(pS2,i2);
257 const unsigned char *b2 = sqlite3_column_blob(pS2,i2);
drha1f79da2022-06-14 19:12:25 +0000258 if( len1!=len2 ){
259 x = 0;
260 }else if( len1==0 ){
261 x = 1;
262 }else{
263 x = (b1!=0 && b2!=0 && memcmp(b1,b2,len1)==0);
264 }
265 break;
266 }
267 }
268 return x;
269}
270
271/*
272** Print a single row from the prepared statement
273*/
274static void printRow(sqlite3_stmt *pStmt, int iRow){
275 int i, nCol;
276 nCol = sqlite3_column_count(pStmt);
277 for(i=0; i<nCol; i++){
drh36f904f2022-06-17 15:11:31 +0000278 printf("row%d.col%d = ", iRow, i);
drha1f79da2022-06-14 19:12:25 +0000279 switch( sqlite3_column_type(pStmt, i) ){
280 case SQLITE_NULL: {
281 printf("NULL\n");
282 break;
283 }
284 case SQLITE_INTEGER: {
285 printf("(integer) %lld\n", sqlite3_column_int64(pStmt, i));
286 break;
287 }
288 case SQLITE_FLOAT: {
289 printf("(float) %f\n", sqlite3_column_double(pStmt, i));
290 break;
291 }
292 case SQLITE_TEXT: {
293 printf("(text) \"%s\"\n", sqlite3_column_text(pStmt, i));
294 break;
295 }
296 case SQLITE_BLOB: {
297 int n = sqlite3_column_bytes(pStmt, i);
298 int j;
299 unsigned const char *data = sqlite3_column_blob(pStmt, i);
300 printf("(blob %d bytes) x'", n);
301 for(j=0; j<20 && j<n; j++){
302 printf("%02x", data[j]);
303 }
304 if( j<n ) printf("...");
305 printf("'\n");
306 break;
307 }
308 }
309 }
310}
311
312/*
313** Report a failure of the invariant: The current output row of pOrig
314** does not appear in any row of the output from pTest.
315*/
316static void reportInvariantFailed(
317 sqlite3_stmt *pOrig, /* The original query */
318 sqlite3_stmt *pTest, /* The alternative test query with a missing row */
319 int iRow /* Row number in pOrig */
320){
321 int iTestRow = 0;
322 printf("Invariant check failed on row %d.\n", iRow);
323 printf("Original query --------------------------------------------------\n");
324 printf("%s\n", sqlite3_expanded_sql(pOrig));
325 printf("Alternative query -----------------------------------------------\n");
326 printf("%s\n", sqlite3_expanded_sql(pTest));
327 printf("Result row that is missing from the alternative -----------------\n");
328 printRow(pOrig, iRow);
329 printf("Complete results from the alternative query ---------------------\n");
330 sqlite3_reset(pTest);
331 while( sqlite3_step(pTest)==SQLITE_ROW ){
332 iTestRow++;
333 printRow(pTest, iTestRow);
334 }
335 sqlite3_finalize(pTest);
336 abort();
337}