blob: b40fa1fa55cde66f8b0d4804bd953c03b4cf85c1 [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++){
drh36f904f2022-06-17 15:11:31 +000095 sqlite3_bind_value(pTestStmt, i+1+nParam, sqlite3_column_value(pStmt,i));
drha1f79da2022-06-14 19:12:25 +000096 }
drhe3bf2c82022-06-15 16:26:37 +000097 if( eVerbosity>=2 ){
98 char *zSql = sqlite3_expanded_sql(pTestStmt);
99 printf("invariant-sql #%d:\n%s\n", iCnt, zSql);
100 sqlite3_free(zSql);
101 }
drha1f79da2022-06-14 19:12:25 +0000102 while( (rc = sqlite3_step(pTestStmt))==SQLITE_ROW ){
103 for(i=0; i<nCol; i++){
drh6efabd62022-06-17 12:25:33 +0000104 if( !sameValue(pStmt, i, pTestStmt, i) ) break;
drha1f79da2022-06-14 19:12:25 +0000105 }
106 if( i>=nCol ) break;
107 }
drh36f904f2022-06-17 15:11:31 +0000108 if( rc!=SQLITE_ROW && rc!=SQLITE_NOMEM ){
drha1f79da2022-06-14 19:12:25 +0000109 /* No matching output row found */
110 sqlite3_stmt *pCk = 0;
111 rc = sqlite3_prepare_v2(db, "PRAGMA integrity_check", -1, &pCk, 0);
112 if( rc ){
113 sqlite3_finalize(pCk);
114 sqlite3_finalize(pTestStmt);
115 return rc;
116 }
117 rc = sqlite3_step(pCk);
118 if( rc!=SQLITE_ROW
119 || sqlite3_column_text(pCk, 0)==0
120 || strcmp((const char*)sqlite3_column_text(pCk,0),"ok")!=0
121 ){
122 *pbCorrupt = 1;
123 sqlite3_finalize(pCk);
124 sqlite3_finalize(pTestStmt);
125 return SQLITE_CORRUPT;
126 }
127 sqlite3_finalize(pCk);
drha913f9b2022-06-15 10:37:16 +0000128 rc = sqlite3_prepare_v2(db,
129 "SELECT 1 FROM bytecode(?1) WHERE opcode='VOpen'", -1, &pCk, 0);
drh36f904f2022-06-17 15:11:31 +0000130 if( rc==SQLITE_OK ){
131 sqlite3_bind_pointer(pCk, 1, pStmt, "stmt-pointer", 0);
132 rc = sqlite3_step(pCk);
133 }
drha913f9b2022-06-15 10:37:16 +0000134 sqlite3_finalize(pCk);
135 if( rc==SQLITE_DONE ){
136 reportInvariantFailed(pStmt, pTestStmt, iRow);
137 return SQLITE_INTERNAL;
drhe3bf2c82022-06-15 16:26:37 +0000138 }else if( eVerbosity>0 ){
139 printf("invariant-error ignored due to the use of virtual tables\n");
drha913f9b2022-06-15 10:37:16 +0000140 }
drha1f79da2022-06-14 19:12:25 +0000141 }
142 sqlite3_finalize(pTestStmt);
143 return SQLITE_OK;
144}
145
146
147/*
148** Generate SQL used to test a statement invariant.
149**
150** Return 0 if the iCnt is out of range.
151*/
152static char *fuzz_invariant_sql(sqlite3_stmt *pStmt, int iCnt){
153 const char *zIn;
154 size_t nIn;
155 const char *zAnd = "WHERE";
156 int i;
157 sqlite3_str *pTest;
158 sqlite3_stmt *pBase = 0;
159 sqlite3 *db = sqlite3_db_handle(pStmt);
160 int rc;
drhe3bf2c82022-06-15 16:26:37 +0000161 int nCol = sqlite3_column_count(pStmt);
162 int mxCnt;
drh8f9261a2022-06-15 20:18:44 +0000163 int bDistinct = 0;
164 int bOrderBy = 0;
drh36f904f2022-06-17 15:11:31 +0000165 int nParam = sqlite3_bind_parameter_count(pStmt);
drha1f79da2022-06-14 19:12:25 +0000166
drh63880362022-06-17 16:09:47 +0000167 iCnt++;
drh8f9261a2022-06-15 20:18:44 +0000168 switch( iCnt % 4 ){
169 case 1: bDistinct = 1; break;
170 case 2: bOrderBy = 1; break;
171 case 3: bDistinct = bOrderBy = 1; break;
172 }
173 iCnt /= 4;
drh63880362022-06-17 16:09:47 +0000174 mxCnt = nCol;
drhe3bf2c82022-06-15 16:26:37 +0000175 if( iCnt<0 || iCnt>mxCnt ) return 0;
drha1f79da2022-06-14 19:12:25 +0000176 zIn = sqlite3_sql(pStmt);
177 if( zIn==0 ) return 0;
178 nIn = strlen(zIn);
179 while( nIn>0 && (isspace(zIn[nIn-1]) || zIn[nIn-1]==';') ) nIn--;
180 if( strchr(zIn, '?') ) return 0;
181 pTest = sqlite3_str_new(0);
drh8f9261a2022-06-15 20:18:44 +0000182 sqlite3_str_appendf(pTest, "SELECT %s* FROM (%.*s)",
183 bDistinct ? "DISTINCT " : "", (int)nIn, zIn);
drha1f79da2022-06-14 19:12:25 +0000184 rc = sqlite3_prepare_v2(db, sqlite3_str_value(pTest), -1, &pBase, 0);
185 if( rc ){
186 sqlite3_finalize(pBase);
187 pBase = pStmt;
188 }
189 for(i=0; i<sqlite3_column_count(pStmt); i++){
drha913f9b2022-06-15 10:37:16 +0000190 const char *zColName = sqlite3_column_name(pBase,i);
drh3e245bc2022-06-16 20:29:36 +0000191 const char *zSuffix = zColName ? strchr(zColName, ':') : 0;
drha913f9b2022-06-15 10:37:16 +0000192 if( zSuffix
193 && isdigit(zSuffix[1])
194 && (zSuffix[1]>'3' || isdigit(zSuffix[2]))
195 ){
196 /* This is a randomized column name and so cannot be used in the
197 ** WHERE clause. */
198 continue;
199 }
drh63880362022-06-17 16:09:47 +0000200 if( i+1!=iCnt ) continue;
drha1f79da2022-06-14 19:12:25 +0000201 if( sqlite3_column_type(pStmt, i)==SQLITE_NULL ){
drha913f9b2022-06-15 10:37:16 +0000202 sqlite3_str_appendf(pTest, " %s \"%w\" ISNULL", zAnd, zColName);
drha1f79da2022-06-14 19:12:25 +0000203 }else{
drh36f904f2022-06-17 15:11:31 +0000204 sqlite3_str_appendf(pTest, " %s \"%w\"=?%d", zAnd, zColName,
205 i+1+nParam);
drha1f79da2022-06-14 19:12:25 +0000206 }
207 zAnd = "AND";
208 }
209 if( pBase!=pStmt ) sqlite3_finalize(pBase);
drh8f9261a2022-06-15 20:18:44 +0000210 if( bOrderBy ){
211 sqlite3_str_appendf(pTest, " ORDER BY 1");
212 }
drha1f79da2022-06-14 19:12:25 +0000213 return sqlite3_str_finish(pTest);
214}
215
216/*
217** Return true if and only if v1 and is the same as v2.
218*/
drh6efabd62022-06-17 12:25:33 +0000219static int sameValue(sqlite3_stmt *pS1, int i1, sqlite3_stmt *pS2, int i2){
drha1f79da2022-06-14 19:12:25 +0000220 int x = 1;
drhea64cb32022-06-17 16:32:21 +0000221 int t1 = sqlite3_column_type(pS1,i1);
222 int t2 = sqlite3_column_type(pS2,i2);
223 if( t1!=t2 ){
224 if( (t1==SQLITE_INTEGER && t2==SQLITE_FLOAT)
225 || (t1==SQLITE_FLOAT && t2==SQLITE_INTEGER)
226 ){
227 /* Comparison of numerics is ok */
228 }else{
229 return 0;
230 }
231 }
drh6efabd62022-06-17 12:25:33 +0000232 switch( sqlite3_column_type(pS1,i1) ){
drha1f79da2022-06-14 19:12:25 +0000233 case SQLITE_INTEGER: {
drh6efabd62022-06-17 12:25:33 +0000234 x = sqlite3_column_int64(pS1,i1)==sqlite3_column_int64(pS2,i2);
drha1f79da2022-06-14 19:12:25 +0000235 break;
236 }
237 case SQLITE_FLOAT: {
drh6efabd62022-06-17 12:25:33 +0000238 x = sqlite3_column_double(pS1,i1)==sqlite3_column_double(pS2,i2);
drha1f79da2022-06-14 19:12:25 +0000239 break;
240 }
241 case SQLITE_TEXT: {
drh6efabd62022-06-17 12:25:33 +0000242 const char *z1 = (const char*)sqlite3_column_text(pS1,i1);
243 const char *z2 = (const char*)sqlite3_column_text(pS2,i2);
drha1f79da2022-06-14 19:12:25 +0000244 x = ((z1==0 && z2==0) || (z1!=0 && z2!=0 && strcmp(z1,z1)==0));
245 break;
246 }
247 case SQLITE_BLOB: {
drh6efabd62022-06-17 12:25:33 +0000248 int len1 = sqlite3_column_bytes(pS1,i1);
249 const unsigned char *b1 = sqlite3_column_blob(pS1,i1);
250 int len2 = sqlite3_column_bytes(pS2,i2);
251 const unsigned char *b2 = sqlite3_column_blob(pS2,i2);
drha1f79da2022-06-14 19:12:25 +0000252 if( len1!=len2 ){
253 x = 0;
254 }else if( len1==0 ){
255 x = 1;
256 }else{
257 x = (b1!=0 && b2!=0 && memcmp(b1,b2,len1)==0);
258 }
259 break;
260 }
261 }
262 return x;
263}
264
265/*
266** Print a single row from the prepared statement
267*/
268static void printRow(sqlite3_stmt *pStmt, int iRow){
269 int i, nCol;
270 nCol = sqlite3_column_count(pStmt);
271 for(i=0; i<nCol; i++){
drh36f904f2022-06-17 15:11:31 +0000272 printf("row%d.col%d = ", iRow, i);
drha1f79da2022-06-14 19:12:25 +0000273 switch( sqlite3_column_type(pStmt, i) ){
274 case SQLITE_NULL: {
275 printf("NULL\n");
276 break;
277 }
278 case SQLITE_INTEGER: {
279 printf("(integer) %lld\n", sqlite3_column_int64(pStmt, i));
280 break;
281 }
282 case SQLITE_FLOAT: {
283 printf("(float) %f\n", sqlite3_column_double(pStmt, i));
284 break;
285 }
286 case SQLITE_TEXT: {
287 printf("(text) \"%s\"\n", sqlite3_column_text(pStmt, i));
288 break;
289 }
290 case SQLITE_BLOB: {
291 int n = sqlite3_column_bytes(pStmt, i);
292 int j;
293 unsigned const char *data = sqlite3_column_blob(pStmt, i);
294 printf("(blob %d bytes) x'", n);
295 for(j=0; j<20 && j<n; j++){
296 printf("%02x", data[j]);
297 }
298 if( j<n ) printf("...");
299 printf("'\n");
300 break;
301 }
302 }
303 }
304}
305
306/*
307** Report a failure of the invariant: The current output row of pOrig
308** does not appear in any row of the output from pTest.
309*/
310static void reportInvariantFailed(
311 sqlite3_stmt *pOrig, /* The original query */
312 sqlite3_stmt *pTest, /* The alternative test query with a missing row */
313 int iRow /* Row number in pOrig */
314){
315 int iTestRow = 0;
316 printf("Invariant check failed on row %d.\n", iRow);
317 printf("Original query --------------------------------------------------\n");
318 printf("%s\n", sqlite3_expanded_sql(pOrig));
319 printf("Alternative query -----------------------------------------------\n");
320 printf("%s\n", sqlite3_expanded_sql(pTest));
321 printf("Result row that is missing from the alternative -----------------\n");
322 printRow(pOrig, iRow);
323 printf("Complete results from the alternative query ---------------------\n");
324 sqlite3_reset(pTest);
325 while( sqlite3_step(pTest)==SQLITE_ROW ){
326 iTestRow++;
327 printRow(pTest, iTestRow);
328 }
329 sqlite3_finalize(pTest);
330 abort();
331}