| OLD | NEW |
| 1 /* | 1 /* |
| 2 ** 2015-05-25 | 2 ** 2015-05-25 |
| 3 ** | 3 ** |
| 4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
| 5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
| 6 ** | 6 ** |
| 7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
| 8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
| 9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
| 10 ** | 10 ** |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 #include "sqlite3.h" | 72 #include "sqlite3.h" |
| 73 #define ISSPACE(X) isspace((unsigned char)(X)) | 73 #define ISSPACE(X) isspace((unsigned char)(X)) |
| 74 #define ISDIGIT(X) isdigit((unsigned char)(X)) | 74 #define ISDIGIT(X) isdigit((unsigned char)(X)) |
| 75 | 75 |
| 76 | 76 |
| 77 #ifdef __unix__ | 77 #ifdef __unix__ |
| 78 # include <signal.h> | 78 # include <signal.h> |
| 79 # include <unistd.h> | 79 # include <unistd.h> |
| 80 #endif | 80 #endif |
| 81 | 81 |
| 82 #ifdef SQLITE_OSS_FUZZ |
| 83 # include <stddef.h> |
| 84 # include <stdint.h> |
| 85 #endif |
| 86 |
| 82 /* | 87 /* |
| 83 ** Files in the virtual file system. | 88 ** Files in the virtual file system. |
| 84 */ | 89 */ |
| 85 typedef struct VFile VFile; | 90 typedef struct VFile VFile; |
| 86 struct VFile { | 91 struct VFile { |
| 87 char *zFilename; /* Filename. NULL for delete-on-close. From malloc()
*/ | 92 char *zFilename; /* Filename. NULL for delete-on-close. From malloc()
*/ |
| 88 int sz; /* Size of the file in bytes */ | 93 int sz; /* Size of the file in bytes */ |
| 89 int nRef; /* Number of references to this file */ | 94 int nRef; /* Number of references to this file */ |
| 90 unsigned char *a; /* Content of the file. From malloc() */ | 95 unsigned char *a; /* Content of the file. From malloc() */ |
| 91 }; | 96 }; |
| (...skipping 28 matching lines...) Expand all Loading... |
| 120 /* | 125 /* |
| 121 ** All global variables are gathered into the "g" singleton. | 126 ** All global variables are gathered into the "g" singleton. |
| 122 */ | 127 */ |
| 123 static struct GlobalVars { | 128 static struct GlobalVars { |
| 124 const char *zArgv0; /* Name of program */ | 129 const char *zArgv0; /* Name of program */ |
| 125 VFile aFile[MX_FILE]; /* The virtual filesystem */ | 130 VFile aFile[MX_FILE]; /* The virtual filesystem */ |
| 126 int nDb; /* Number of template databases */ | 131 int nDb; /* Number of template databases */ |
| 127 Blob *pFirstDb; /* Content of first template database */ | 132 Blob *pFirstDb; /* Content of first template database */ |
| 128 int nSql; /* Number of SQL scripts */ | 133 int nSql; /* Number of SQL scripts */ |
| 129 Blob *pFirstSql; /* First SQL script */ | 134 Blob *pFirstSql; /* First SQL script */ |
| 135 unsigned int uRandom; /* Seed for the SQLite PRNG */ |
| 130 char zTestName[100]; /* Name of current test */ | 136 char zTestName[100]; /* Name of current test */ |
| 131 } g; | 137 } g; |
| 132 | 138 |
| 133 /* | 139 /* |
| 134 ** Print an error message and quit. | 140 ** Print an error message and quit. |
| 135 */ | 141 */ |
| 136 static void fatalError(const char *zFormat, ...){ | 142 static void fatalError(const char *zFormat, ...){ |
| 137 va_list ap; | 143 va_list ap; |
| 138 if( g.zTestName[0] ){ | 144 if( g.zTestName[0] ){ |
| 139 fprintf(stderr, "%s (%s): ", g.zArgv0, g.zTestName); | 145 fprintf(stderr, "%s (%s): ", g.zArgv0, g.zTestName); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 static int progressHandler(void *pVdbeLimitFlag){ | 187 static int progressHandler(void *pVdbeLimitFlag){ |
| 182 if( *(int*)pVdbeLimitFlag ) fatalError("too many VDBE cycles"); | 188 if( *(int*)pVdbeLimitFlag ) fatalError("too many VDBE cycles"); |
| 183 return 1; | 189 return 1; |
| 184 } | 190 } |
| 185 #endif | 191 #endif |
| 186 | 192 |
| 187 /* | 193 /* |
| 188 ** Reallocate memory. Show and error and quit if unable. | 194 ** Reallocate memory. Show and error and quit if unable. |
| 189 */ | 195 */ |
| 190 static void *safe_realloc(void *pOld, int szNew){ | 196 static void *safe_realloc(void *pOld, int szNew){ |
| 191 void *pNew = realloc(pOld, szNew); | 197 void *pNew = realloc(pOld, szNew<=0 ? 1 : szNew); |
| 192 if( pNew==0 ) fatalError("unable to realloc for %d bytes", szNew); | 198 if( pNew==0 ) fatalError("unable to realloc for %d bytes", szNew); |
| 193 return pNew; | 199 return pNew; |
| 194 } | 200 } |
| 195 | 201 |
| 196 /* | 202 /* |
| 197 ** Initialize the virtual file system. | 203 ** Initialize the virtual file system. |
| 198 */ | 204 */ |
| 199 static void formatVfs(void){ | 205 static void formatVfs(void){ |
| 200 int i; | 206 int i; |
| 201 for(i=0; i<MX_FILE; i++){ | 207 for(i=0; i<MX_FILE; i++){ |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 ** Return NULL only if the filesystem is full. | 254 ** Return NULL only if the filesystem is full. |
| 249 */ | 255 */ |
| 250 static VFile *createVFile(const char *zName, int sz, unsigned char *pData){ | 256 static VFile *createVFile(const char *zName, int sz, unsigned char *pData){ |
| 251 VFile *pNew = findVFile(zName); | 257 VFile *pNew = findVFile(zName); |
| 252 int i; | 258 int i; |
| 253 if( pNew ) return pNew; | 259 if( pNew ) return pNew; |
| 254 for(i=0; i<MX_FILE && g.aFile[i].sz>=0; i++){} | 260 for(i=0; i<MX_FILE && g.aFile[i].sz>=0; i++){} |
| 255 if( i>=MX_FILE ) return 0; | 261 if( i>=MX_FILE ) return 0; |
| 256 pNew = &g.aFile[i]; | 262 pNew = &g.aFile[i]; |
| 257 if( zName ){ | 263 if( zName ){ |
| 258 pNew->zFilename = safe_realloc(0, strlen(zName)+1); | 264 int nName = (int)strlen(zName)+1; |
| 259 memcpy(pNew->zFilename, zName, strlen(zName)+1); | 265 pNew->zFilename = safe_realloc(0, nName); |
| 266 memcpy(pNew->zFilename, zName, nName); |
| 260 }else{ | 267 }else{ |
| 261 pNew->zFilename = 0; | 268 pNew->zFilename = 0; |
| 262 } | 269 } |
| 263 pNew->nRef = 0; | 270 pNew->nRef = 0; |
| 264 pNew->sz = sz; | 271 pNew->sz = sz; |
| 265 pNew->a = safe_realloc(0, sz); | 272 pNew->a = safe_realloc(0, sz); |
| 266 if( sz>0 ) memcpy(pNew->a, pData, sz); | 273 if( sz>0 ) memcpy(pNew->a, pData, sz); |
| 267 return pNew; | 274 return pNew; |
| 268 } | 275 } |
| 269 | 276 |
| (...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 static int inmemFullPathname( | 588 static int inmemFullPathname( |
| 582 sqlite3_vfs *pVfs, | 589 sqlite3_vfs *pVfs, |
| 583 const char *zFilename, | 590 const char *zFilename, |
| 584 int nOut, | 591 int nOut, |
| 585 char *zOut | 592 char *zOut |
| 586 ){ | 593 ){ |
| 587 sqlite3_snprintf(nOut, zOut, "%s", zFilename); | 594 sqlite3_snprintf(nOut, zOut, "%s", zFilename); |
| 588 return SQLITE_OK; | 595 return SQLITE_OK; |
| 589 } | 596 } |
| 590 | 597 |
| 598 /* Always use the same random see, for repeatability. |
| 599 */ |
| 600 static int inmemRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ |
| 601 memset(zBuf, 0, nBuf); |
| 602 memcpy(zBuf, &g.uRandom, nBuf<sizeof(g.uRandom) ? nBuf : sizeof(g.uRandom)); |
| 603 return nBuf; |
| 604 } |
| 605 |
| 591 /* | 606 /* |
| 592 ** Register the VFS that reads from the g.aFile[] set of files. | 607 ** Register the VFS that reads from the g.aFile[] set of files. |
| 593 */ | 608 */ |
| 594 static void inmemVfsRegister(void){ | 609 static void inmemVfsRegister(int makeDefault){ |
| 595 static sqlite3_vfs inmemVfs; | 610 static sqlite3_vfs inmemVfs; |
| 596 sqlite3_vfs *pDefault = sqlite3_vfs_find(0); | 611 sqlite3_vfs *pDefault = sqlite3_vfs_find(0); |
| 597 inmemVfs.iVersion = 3; | 612 inmemVfs.iVersion = 3; |
| 598 inmemVfs.szOsFile = sizeof(VHandle); | 613 inmemVfs.szOsFile = sizeof(VHandle); |
| 599 inmemVfs.mxPathname = 200; | 614 inmemVfs.mxPathname = 200; |
| 600 inmemVfs.zName = "inmem"; | 615 inmemVfs.zName = "inmem"; |
| 601 inmemVfs.xOpen = inmemOpen; | 616 inmemVfs.xOpen = inmemOpen; |
| 602 inmemVfs.xDelete = inmemDelete; | 617 inmemVfs.xDelete = inmemDelete; |
| 603 inmemVfs.xAccess = inmemAccess; | 618 inmemVfs.xAccess = inmemAccess; |
| 604 inmemVfs.xFullPathname = inmemFullPathname; | 619 inmemVfs.xFullPathname = inmemFullPathname; |
| 605 inmemVfs.xRandomness = pDefault->xRandomness; | 620 inmemVfs.xRandomness = inmemRandomness; |
| 606 inmemVfs.xSleep = pDefault->xSleep; | 621 inmemVfs.xSleep = pDefault->xSleep; |
| 607 inmemVfs.xCurrentTimeInt64 = pDefault->xCurrentTimeInt64; | 622 inmemVfs.xCurrentTimeInt64 = pDefault->xCurrentTimeInt64; |
| 608 sqlite3_vfs_register(&inmemVfs, 0); | 623 sqlite3_vfs_register(&inmemVfs, makeDefault); |
| 609 }; | 624 }; |
| 610 | 625 |
| 611 /* | 626 /* |
| 612 ** Allowed values for the runFlags parameter to runSql() | 627 ** Allowed values for the runFlags parameter to runSql() |
| 613 */ | 628 */ |
| 614 #define SQL_TRACE 0x0001 /* Print each SQL statement as it is prepared */ | 629 #define SQL_TRACE 0x0001 /* Print each SQL statement as it is prepared */ |
| 615 #define SQL_OUTPUT 0x0002 /* Show the SQL output */ | 630 #define SQL_OUTPUT 0x0002 /* Show the SQL output */ |
| 616 | 631 |
| 617 /* | 632 /* |
| 618 ** Run multiple commands of SQL. Similar to sqlite3_exec(), but does not | 633 ** Run multiple commands of SQL. Similar to sqlite3_exec(), but does not |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 771 | 786 |
| 772 /* | 787 /* |
| 773 ** Print sketchy documentation for this utility program | 788 ** Print sketchy documentation for this utility program |
| 774 */ | 789 */ |
| 775 static void showHelp(void){ | 790 static void showHelp(void){ |
| 776 printf("Usage: %s [options] SOURCE-DB ?ARGS...?\n", g.zArgv0); | 791 printf("Usage: %s [options] SOURCE-DB ?ARGS...?\n", g.zArgv0); |
| 777 printf( | 792 printf( |
| 778 "Read databases and SQL scripts from SOURCE-DB and execute each script against\n
" | 793 "Read databases and SQL scripts from SOURCE-DB and execute each script against\n
" |
| 779 "each database, checking for crashes and memory leaks.\n" | 794 "each database, checking for crashes and memory leaks.\n" |
| 780 "Options:\n" | 795 "Options:\n" |
| 781 " --cell-size-check Set the PRAGMA cell_size_check=ON\n" | 796 " --cell-size-check Set the PRAGMA cell_size_check=ON\n" |
| 782 " --dbid N Use only the database where dbid=N\n" | 797 " --dbid N Use only the database where dbid=N\n" |
| 783 " --export-db DIR Write databases to files(s) in DIR. Works with --dbid\n
" | 798 " --export-db DIR Write databases to files(s) in DIR. Works with --dbid\n" |
| 784 " --export-sql DIR Write SQL to file(s) in DIR. Also works with --sqlid\n" | 799 " --export-sql DIR Write SQL to file(s) in DIR. Also works with --sqlid\n" |
| 785 " --help Show this help text\n" | 800 " --help Show this help text\n" |
| 786 " -q Reduced output\n" | 801 " -q|--quiet Reduced output\n" |
| 787 " --quiet Reduced output\n" | 802 " --limit-mem N Limit memory used by test SQLite instance to N bytes\n" |
| 788 " --limit-mem N Limit memory used by test SQLite instance to N bytes\n" | 803 " --limit-vdbe Panic if any test runs for more than 100,000 cycles\n" |
| 789 " --limit-vdbe Panic if an sync SQL runs for more than 100,000 cycles\
n" | 804 " --load-sql ARGS... Load SQL scripts fro files into SOURCE-DB\n" |
| 790 " --load-sql ARGS... Load SQL scripts fro files into SOURCE-DB\n" | 805 " --load-db ARGS... Load template databases from files into SOURCE_DB\n" |
| 791 " --load-db ARGS... Load template databases from files into SOURCE_DB\n" | 806 " -m TEXT Add a description to the database\n" |
| 792 " -m TEXT Add a description to the database\n" | 807 " --native-vfs Use the native VFS for initially empty database files\n" |
| 793 " --native-vfs Use the native VFS for initially empty database files\n
" | 808 " --oss-fuzz Enable OSS-FUZZ testing\n" |
| 794 " --rebuild Rebuild and vacuum the database file\n" | 809 " --prng-seed N Seed value for the PRGN inside of SQLite\n" |
| 795 " --result-trace Show the results of each SQL command\n" | 810 " --rebuild Rebuild and vacuum the database file\n" |
| 796 " --sqlid N Use only SQL where sqlid=N\n" | 811 " --result-trace Show the results of each SQL command\n" |
| 797 " --timeout N Abort if any single test case needs more than N seconds
\n" | 812 " --sqlid N Use only SQL where sqlid=N\n" |
| 798 " -v Increased output\n" | 813 " --timeout N Abort if any single test needs more than N seconds\n" |
| 799 " --verbose Increased output\n" | 814 " -v|--verbose Increased output. Repeat for more output.\n" |
| 800 ); | 815 ); |
| 801 } | 816 } |
| 802 | 817 |
| 803 int main(int argc, char **argv){ | 818 int main(int argc, char **argv){ |
| 804 sqlite3_int64 iBegin; /* Start time of this program */ | 819 sqlite3_int64 iBegin; /* Start time of this program */ |
| 805 int quietFlag = 0; /* True if --quiet or -q */ | 820 int quietFlag = 0; /* True if --quiet or -q */ |
| 806 int verboseFlag = 0; /* True if --verbose or -v */ | 821 int verboseFlag = 0; /* True if --verbose or -v */ |
| 807 char *zInsSql = 0; /* SQL statement for --load-db or --load-sql */ | 822 char *zInsSql = 0; /* SQL statement for --load-db or --load-sql */ |
| 808 int iFirstInsArg = 0; /* First argv[] to use for --load-db or --load-sq
l */ | 823 int iFirstInsArg = 0; /* First argv[] to use for --load-db or --load-sq
l */ |
| 809 sqlite3 *db = 0; /* The open database connection */ | 824 sqlite3 *db = 0; /* The open database connection */ |
| (...skipping 13 matching lines...) Expand all Loading... |
| 823 int nSrcDb = 0; /* Number of source databases */ | 838 int nSrcDb = 0; /* Number of source databases */ |
| 824 char **azSrcDb = 0; /* Array of source database names */ | 839 char **azSrcDb = 0; /* Array of source database names */ |
| 825 int iSrcDb; /* Loop over all source databases */ | 840 int iSrcDb; /* Loop over all source databases */ |
| 826 int nTest = 0; /* Total number of tests performed */ | 841 int nTest = 0; /* Total number of tests performed */ |
| 827 char *zDbName = ""; /* Appreviated name of a source database */ | 842 char *zDbName = ""; /* Appreviated name of a source database */ |
| 828 const char *zFailCode = 0; /* Value of the TEST_FAILURE environment variable
*/ | 843 const char *zFailCode = 0; /* Value of the TEST_FAILURE environment variable
*/ |
| 829 int cellSzCkFlag = 0; /* --cell-size-check */ | 844 int cellSzCkFlag = 0; /* --cell-size-check */ |
| 830 int sqlFuzz = 0; /* True for SQL fuzz testing. False for DB fuzz *
/ | 845 int sqlFuzz = 0; /* True for SQL fuzz testing. False for DB fuzz *
/ |
| 831 int iTimeout = 120; /* Default 120-second timeout */ | 846 int iTimeout = 120; /* Default 120-second timeout */ |
| 832 int nMem = 0; /* Memory limit */ | 847 int nMem = 0; /* Memory limit */ |
| 848 int nMemThisDb = 0; /* Memory limit set by the CONFIG table */ |
| 833 char *zExpDb = 0; /* Write Databases to files in this directory */ | 849 char *zExpDb = 0; /* Write Databases to files in this directory */ |
| 834 char *zExpSql = 0; /* Write SQL to files in this directory */ | 850 char *zExpSql = 0; /* Write SQL to files in this directory */ |
| 835 void *pHeap = 0; /* Heap for use by SQLite */ | 851 void *pHeap = 0; /* Heap for use by SQLite */ |
| 852 int ossFuzz = 0; /* enable OSS-FUZZ testing */ |
| 853 int ossFuzzThisDb = 0; /* ossFuzz value for this particular database */ |
| 854 sqlite3_vfs *pDfltVfs; /* The default VFS */ |
| 836 | 855 |
| 837 iBegin = timeOfDay(); | 856 iBegin = timeOfDay(); |
| 838 #ifdef __unix__ | 857 #ifdef __unix__ |
| 839 signal(SIGALRM, timeoutHandler); | 858 signal(SIGALRM, timeoutHandler); |
| 840 #endif | 859 #endif |
| 841 g.zArgv0 = argv[0]; | 860 g.zArgv0 = argv[0]; |
| 842 zFailCode = getenv("TEST_FAILURE"); | 861 zFailCode = getenv("TEST_FAILURE"); |
| 862 pDfltVfs = sqlite3_vfs_find(0); |
| 863 inmemVfsRegister(1); |
| 843 for(i=1; i<argc; i++){ | 864 for(i=1; i<argc; i++){ |
| 844 const char *z = argv[i]; | 865 const char *z = argv[i]; |
| 845 if( z[0]=='-' ){ | 866 if( z[0]=='-' ){ |
| 846 z++; | 867 z++; |
| 847 if( z[0]=='-' ) z++; | 868 if( z[0]=='-' ) z++; |
| 848 if( strcmp(z,"cell-size-check")==0 ){ | 869 if( strcmp(z,"cell-size-check")==0 ){ |
| 849 cellSzCkFlag = 1; | 870 cellSzCkFlag = 1; |
| 850 }else | 871 }else |
| 851 if( strcmp(z,"dbid")==0 ){ | 872 if( strcmp(z,"dbid")==0 ){ |
| 852 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); | 873 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); |
| 853 onlyDbid = integerValue(argv[++i]); | 874 onlyDbid = integerValue(argv[++i]); |
| 854 }else | 875 }else |
| 855 if( strcmp(z,"export-db")==0 ){ | 876 if( strcmp(z,"export-db")==0 ){ |
| 856 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); | 877 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); |
| 857 zExpDb = argv[++i]; | 878 zExpDb = argv[++i]; |
| 858 }else | 879 }else |
| 859 if( strcmp(z,"export-sql")==0 ){ | 880 if( strcmp(z,"export-sql")==0 ){ |
| 860 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); | 881 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); |
| 861 zExpSql = argv[++i]; | 882 zExpSql = argv[++i]; |
| 862 }else | 883 }else |
| 863 if( strcmp(z,"help")==0 ){ | 884 if( strcmp(z,"help")==0 ){ |
| 864 showHelp(); | 885 showHelp(); |
| 865 return 0; | 886 return 0; |
| 866 }else | 887 }else |
| 867 if( strcmp(z,"limit-mem")==0 ){ | 888 if( strcmp(z,"limit-mem")==0 ){ |
| 889 #if !defined(SQLITE_ENABLE_MEMSYS3) && !defined(SQLITE_ENABLE_MEMSYS5) |
| 890 fatalError("the %s option requires -DSQLITE_ENABLE_MEMSYS5 or _MEMSYS3", |
| 891 argv[i]); |
| 892 #else |
| 868 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); | 893 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); |
| 869 nMem = integerValue(argv[++i]); | 894 nMem = integerValue(argv[++i]); |
| 895 #endif |
| 870 }else | 896 }else |
| 871 if( strcmp(z,"limit-vdbe")==0 ){ | 897 if( strcmp(z,"limit-vdbe")==0 ){ |
| 872 vdbeLimitFlag = 1; | 898 vdbeLimitFlag = 1; |
| 873 }else | 899 }else |
| 874 if( strcmp(z,"load-sql")==0 ){ | 900 if( strcmp(z,"load-sql")==0 ){ |
| 875 zInsSql = "INSERT INTO xsql(sqltext) VALUES(CAST(readfile(?1) AS text))"
; | 901 zInsSql = "INSERT INTO xsql(sqltext) VALUES(CAST(readfile(?1) AS text))"
; |
| 876 iFirstInsArg = i+1; | 902 iFirstInsArg = i+1; |
| 877 break; | 903 break; |
| 878 }else | 904 }else |
| 879 if( strcmp(z,"load-db")==0 ){ | 905 if( strcmp(z,"load-db")==0 ){ |
| 880 zInsSql = "INSERT INTO db(dbcontent) VALUES(readfile(?1))"; | 906 zInsSql = "INSERT INTO db(dbcontent) VALUES(readfile(?1))"; |
| 881 iFirstInsArg = i+1; | 907 iFirstInsArg = i+1; |
| 882 break; | 908 break; |
| 883 }else | 909 }else |
| 884 if( strcmp(z,"m")==0 ){ | 910 if( strcmp(z,"m")==0 ){ |
| 885 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); | 911 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); |
| 886 zMsg = argv[++i]; | 912 zMsg = argv[++i]; |
| 887 }else | 913 }else |
| 888 if( strcmp(z,"native-vfs")==0 ){ | 914 if( strcmp(z,"native-vfs")==0 ){ |
| 889 nativeFlag = 1; | 915 nativeFlag = 1; |
| 890 }else | 916 }else |
| 917 if( strcmp(z,"oss-fuzz")==0 ){ |
| 918 ossFuzz = 1; |
| 919 }else |
| 920 if( strcmp(z,"prng-seed")==0 ){ |
| 921 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); |
| 922 g.uRandom = atoi(argv[++i]); |
| 923 }else |
| 891 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){ | 924 if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){ |
| 892 quietFlag = 1; | 925 quietFlag = 1; |
| 893 verboseFlag = 0; | 926 verboseFlag = 0; |
| 894 }else | 927 }else |
| 895 if( strcmp(z,"rebuild")==0 ){ | 928 if( strcmp(z,"rebuild")==0 ){ |
| 896 rebuildFlag = 1; | 929 rebuildFlag = 1; |
| 897 }else | 930 }else |
| 898 if( strcmp(z,"result-trace")==0 ){ | 931 if( strcmp(z,"result-trace")==0 ){ |
| 899 runFlags |= SQL_OUTPUT; | 932 runFlags |= SQL_OUTPUT; |
| 900 }else | 933 }else |
| 901 if( strcmp(z,"sqlid")==0 ){ | 934 if( strcmp(z,"sqlid")==0 ){ |
| 902 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); | 935 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); |
| 903 onlySqlid = integerValue(argv[++i]); | 936 onlySqlid = integerValue(argv[++i]); |
| 904 }else | 937 }else |
| 905 if( strcmp(z,"timeout")==0 ){ | 938 if( strcmp(z,"timeout")==0 ){ |
| 906 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); | 939 if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]); |
| 907 iTimeout = integerValue(argv[++i]); | 940 iTimeout = integerValue(argv[++i]); |
| 908 }else | 941 }else |
| 909 if( strcmp(z,"timeout-test")==0 ){ | 942 if( strcmp(z,"timeout-test")==0 ){ |
| 910 timeoutTest = 1; | 943 timeoutTest = 1; |
| 911 #ifndef __unix__ | 944 #ifndef __unix__ |
| 912 fatalError("timeout is not available on non-unix systems"); | 945 fatalError("timeout is not available on non-unix systems"); |
| 913 #endif | 946 #endif |
| 914 }else | 947 }else |
| 915 if( strcmp(z,"verbose")==0 || strcmp(z,"v")==0 ){ | 948 if( strcmp(z,"verbose")==0 || strcmp(z,"v")==0 ){ |
| 916 quietFlag = 0; | 949 quietFlag = 0; |
| 917 verboseFlag = 1; | 950 verboseFlag++; |
| 918 runFlags |= SQL_TRACE; | 951 if( verboseFlag>1 ) runFlags |= SQL_TRACE; |
| 919 }else | 952 }else |
| 920 { | 953 { |
| 921 fatalError("unknown option: %s", argv[i]); | 954 fatalError("unknown option: %s", argv[i]); |
| 922 } | 955 } |
| 923 }else{ | 956 }else{ |
| 924 nSrcDb++; | 957 nSrcDb++; |
| 925 azSrcDb = safe_realloc(azSrcDb, nSrcDb*sizeof(azSrcDb[0])); | 958 azSrcDb = safe_realloc(azSrcDb, nSrcDb*sizeof(azSrcDb[0])); |
| 926 azSrcDb[nSrcDb-1] = argv[i]; | 959 azSrcDb[nSrcDb-1] = argv[i]; |
| 927 } | 960 } |
| 928 } | 961 } |
| 929 if( nSrcDb==0 ) fatalError("no source database specified"); | 962 if( nSrcDb==0 ) fatalError("no source database specified"); |
| 930 if( nSrcDb>1 ){ | 963 if( nSrcDb>1 ){ |
| 931 if( zMsg ){ | 964 if( zMsg ){ |
| 932 fatalError("cannot change the description of more than one database"); | 965 fatalError("cannot change the description of more than one database"); |
| 933 } | 966 } |
| 934 if( zInsSql ){ | 967 if( zInsSql ){ |
| 935 fatalError("cannot import into more than one database"); | 968 fatalError("cannot import into more than one database"); |
| 936 } | 969 } |
| 937 } | 970 } |
| 938 | 971 |
| 939 /* Process each source database separately */ | 972 /* Process each source database separately */ |
| 940 for(iSrcDb=0; iSrcDb<nSrcDb; iSrcDb++){ | 973 for(iSrcDb=0; iSrcDb<nSrcDb; iSrcDb++){ |
| 941 rc = sqlite3_open(azSrcDb[iSrcDb], &db); | 974 rc = sqlite3_open_v2(azSrcDb[iSrcDb], &db, |
| 975 SQLITE_OPEN_READONLY, pDfltVfs->zName); |
| 942 if( rc ){ | 976 if( rc ){ |
| 943 fatalError("cannot open source database %s - %s", | 977 fatalError("cannot open source database %s - %s", |
| 944 azSrcDb[iSrcDb], sqlite3_errmsg(db)); | 978 azSrcDb[iSrcDb], sqlite3_errmsg(db)); |
| 945 } | 979 } |
| 946 rc = sqlite3_exec(db, | 980 rc = sqlite3_exec(db, |
| 947 "CREATE TABLE IF NOT EXISTS db(\n" | 981 "CREATE TABLE IF NOT EXISTS db(\n" |
| 948 " dbid INTEGER PRIMARY KEY, -- database id\n" | 982 " dbid INTEGER PRIMARY KEY, -- database id\n" |
| 949 " dbcontent BLOB -- database disk file image\n" | 983 " dbcontent BLOB -- database disk file image\n" |
| 950 ");\n" | 984 ");\n" |
| 951 "CREATE TABLE IF NOT EXISTS xsql(\n" | 985 "CREATE TABLE IF NOT EXISTS xsql(\n" |
| 952 " sqlid INTEGER PRIMARY KEY, -- SQL script id\n" | 986 " sqlid INTEGER PRIMARY KEY, -- SQL script id\n" |
| 953 " sqltext TEXT -- Text of SQL statements to run\n" | 987 " sqltext TEXT -- Text of SQL statements to run\n" |
| 954 ");" | 988 ");" |
| 955 "CREATE TABLE IF NOT EXISTS readme(\n" | 989 "CREATE TABLE IF NOT EXISTS readme(\n" |
| 956 " msg TEXT -- Human-readable description of this file\n" | 990 " msg TEXT -- Human-readable description of this file\n" |
| 957 ");", 0, 0, 0); | 991 ");", 0, 0, 0); |
| 958 if( rc ) fatalError("cannot create schema: %s", sqlite3_errmsg(db)); | 992 if( rc ) fatalError("cannot create schema: %s", sqlite3_errmsg(db)); |
| 959 if( zMsg ){ | 993 if( zMsg ){ |
| 960 char *zSql; | 994 char *zSql; |
| 961 zSql = sqlite3_mprintf( | 995 zSql = sqlite3_mprintf( |
| 962 "DELETE FROM readme; INSERT INTO readme(msg) VALUES(%Q)", zMsg); | 996 "DELETE FROM readme; INSERT INTO readme(msg) VALUES(%Q)", zMsg); |
| 963 rc = sqlite3_exec(db, zSql, 0, 0, 0); | 997 rc = sqlite3_exec(db, zSql, 0, 0, 0); |
| 964 sqlite3_free(zSql); | 998 sqlite3_free(zSql); |
| 965 if( rc ) fatalError("cannot change description: %s", sqlite3_errmsg(db)); | 999 if( rc ) fatalError("cannot change description: %s", sqlite3_errmsg(db)); |
| 966 } | 1000 } |
| 1001 ossFuzzThisDb = ossFuzz; |
| 1002 |
| 1003 /* If the CONFIG(name,value) table exists, read db-specific settings |
| 1004 ** from that table */ |
| 1005 if( sqlite3_table_column_metadata(db,0,"config",0,0,0,0,0,0)==SQLITE_OK ){ |
| 1006 rc = sqlite3_prepare_v2(db, "SELECT name, value FROM config", -1, &pStmt,
0); |
| 1007 if( rc ) fatalError("cannot prepare query of CONFIG table: %s", |
| 1008 sqlite3_errmsg(db)); |
| 1009 while( SQLITE_ROW==sqlite3_step(pStmt) ){ |
| 1010 const char *zName = (const char *)sqlite3_column_text(pStmt,0); |
| 1011 if( zName==0 ) continue; |
| 1012 if( strcmp(zName, "oss-fuzz")==0 ){ |
| 1013 ossFuzzThisDb = sqlite3_column_int(pStmt,1); |
| 1014 if( verboseFlag ) printf("Config: oss-fuzz=%d\n", ossFuzzThisDb); |
| 1015 } |
| 1016 if( strcmp(zName, "limit-mem")==0 ){ |
| 1017 #if !defined(SQLITE_ENABLE_MEMSYS3) && !defined(SQLITE_ENABLE_MEMSYS5) |
| 1018 fatalError("the limit-mem option requires -DSQLITE_ENABLE_MEMSYS5" |
| 1019 " or _MEMSYS3"); |
| 1020 #else |
| 1021 nMemThisDb = sqlite3_column_int(pStmt,1); |
| 1022 if( verboseFlag ) printf("Config: limit-mem=%d\n", nMemThisDb); |
| 1023 #endif |
| 1024 } |
| 1025 } |
| 1026 sqlite3_finalize(pStmt); |
| 1027 } |
| 1028 |
| 967 if( zInsSql ){ | 1029 if( zInsSql ){ |
| 968 sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0, | 1030 sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0, |
| 969 readfileFunc, 0, 0); | 1031 readfileFunc, 0, 0); |
| 970 rc = sqlite3_prepare_v2(db, zInsSql, -1, &pStmt, 0); | 1032 rc = sqlite3_prepare_v2(db, zInsSql, -1, &pStmt, 0); |
| 971 if( rc ) fatalError("cannot prepare statement [%s]: %s", | 1033 if( rc ) fatalError("cannot prepare statement [%s]: %s", |
| 972 zInsSql, sqlite3_errmsg(db)); | 1034 zInsSql, sqlite3_errmsg(db)); |
| 973 rc = sqlite3_exec(db, "BEGIN", 0, 0, 0); | 1035 rc = sqlite3_exec(db, "BEGIN", 0, 0, 0); |
| 974 if( rc ) fatalError("cannot start a transaction"); | 1036 if( rc ) fatalError("cannot start a transaction"); |
| 975 for(i=iFirstInsArg; i<argc; i++){ | 1037 for(i=iFirstInsArg; i<argc; i++){ |
| 976 sqlite3_bind_text(pStmt, 1, argv[i], -1, SQLITE_STATIC); | 1038 sqlite3_bind_text(pStmt, 1, argv[i], -1, SQLITE_STATIC); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1043 memset(g.pFirstDb, 0, sizeof(Blob)); | 1105 memset(g.pFirstDb, 0, sizeof(Blob)); |
| 1044 g.pFirstDb->id = 1; | 1106 g.pFirstDb->id = 1; |
| 1045 g.pFirstDb->seq = 0; | 1107 g.pFirstDb->seq = 0; |
| 1046 g.nDb = 1; | 1108 g.nDb = 1; |
| 1047 sqlFuzz = 1; | 1109 sqlFuzz = 1; |
| 1048 } | 1110 } |
| 1049 | 1111 |
| 1050 /* Print the description, if there is one */ | 1112 /* Print the description, if there is one */ |
| 1051 if( !quietFlag ){ | 1113 if( !quietFlag ){ |
| 1052 zDbName = azSrcDb[iSrcDb]; | 1114 zDbName = azSrcDb[iSrcDb]; |
| 1053 i = strlen(zDbName) - 1; | 1115 i = (int)strlen(zDbName) - 1; |
| 1054 while( i>0 && zDbName[i-1]!='/' && zDbName[i-1]!='\\' ){ i--; } | 1116 while( i>0 && zDbName[i-1]!='/' && zDbName[i-1]!='\\' ){ i--; } |
| 1055 zDbName += i; | 1117 zDbName += i; |
| 1056 sqlite3_prepare_v2(db, "SELECT msg FROM readme", -1, &pStmt, 0); | 1118 sqlite3_prepare_v2(db, "SELECT msg FROM readme", -1, &pStmt, 0); |
| 1057 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ | 1119 if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){ |
| 1058 printf("%s: %s\n", zDbName, sqlite3_column_text(pStmt,0)); | 1120 printf("%s: %s\n", zDbName, sqlite3_column_text(pStmt,0)); |
| 1059 } | 1121 } |
| 1060 sqlite3_finalize(pStmt); | 1122 sqlite3_finalize(pStmt); |
| 1061 } | 1123 } |
| 1062 | 1124 |
| 1063 /* Rebuild the database, if requested */ | 1125 /* Rebuild the database, if requested */ |
| 1064 if( rebuildFlag ){ | 1126 if( rebuildFlag ){ |
| 1065 if( !quietFlag ){ | 1127 if( !quietFlag ){ |
| 1066 printf("%s: rebuilding... ", zDbName); | 1128 printf("%s: rebuilding... ", zDbName); |
| 1067 fflush(stdout); | 1129 fflush(stdout); |
| 1068 } | 1130 } |
| 1069 rebuild_database(db); | 1131 rebuild_database(db); |
| 1070 if( !quietFlag ) printf("done\n"); | 1132 if( !quietFlag ) printf("done\n"); |
| 1071 } | 1133 } |
| 1072 | 1134 |
| 1073 /* Close the source database. Verify that no SQLite memory allocations are | 1135 /* Close the source database. Verify that no SQLite memory allocations are |
| 1074 ** outstanding. | 1136 ** outstanding. |
| 1075 */ | 1137 */ |
| 1076 sqlite3_close(db); | 1138 sqlite3_close(db); |
| 1077 if( sqlite3_memory_used()>0 ){ | 1139 if( sqlite3_memory_used()>0 ){ |
| 1078 fatalError("SQLite has memory in use before the start of testing"); | 1140 fatalError("SQLite has memory in use before the start of testing"); |
| 1079 } | 1141 } |
| 1080 | 1142 |
| 1081 /* Limit available memory, if requested */ | 1143 /* Limit available memory, if requested */ |
| 1082 if( nMem>0 ){ | 1144 if( nMemThisDb>0 ){ |
| 1083 sqlite3_shutdown(); | 1145 sqlite3_shutdown(); |
| 1084 pHeap = malloc(nMem); | 1146 pHeap = realloc(pHeap, nMemThisDb); |
| 1085 if( pHeap==0 ){ | 1147 if( pHeap==0 ){ |
| 1086 fatalError("failed to allocate %d bytes of heap memory", nMem); | 1148 fatalError("failed to allocate %d bytes of heap memory", nMem); |
| 1087 } | 1149 } |
| 1088 sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nMem, 128); | 1150 sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nMemThisDb, 128); |
| 1089 } | 1151 } |
| 1090 | 1152 |
| 1091 /* Register the in-memory virtual filesystem | 1153 /* Reset the in-memory virtual filesystem */ |
| 1092 */ | |
| 1093 formatVfs(); | 1154 formatVfs(); |
| 1094 inmemVfsRegister(); | |
| 1095 | 1155 |
| 1096 /* Run a test using each SQL script against each database. | 1156 /* Run a test using each SQL script against each database. |
| 1097 */ | 1157 */ |
| 1098 if( !verboseFlag && !quietFlag ) printf("%s:", zDbName); | 1158 if( !verboseFlag && !quietFlag ) printf("%s:", zDbName); |
| 1099 for(pSql=g.pFirstSql; pSql; pSql=pSql->pNext){ | 1159 for(pSql=g.pFirstSql; pSql; pSql=pSql->pNext){ |
| 1100 for(pDb=g.pFirstDb; pDb; pDb=pDb->pNext){ | 1160 for(pDb=g.pFirstDb; pDb; pDb=pDb->pNext){ |
| 1101 int openFlags; | 1161 int openFlags; |
| 1102 const char *zVfs = "inmem"; | 1162 const char *zVfs = "inmem"; |
| 1103 sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "sqlid=%d,dbid=%d", | 1163 sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "sqlid=%d,dbid=%d", |
| 1104 pSql->id, pDb->id); | 1164 pSql->id, pDb->id); |
| 1105 if( verboseFlag ){ | 1165 if( verboseFlag ){ |
| 1106 printf("%s\n", g.zTestName); | 1166 printf("%s\n", g.zTestName); |
| 1107 fflush(stdout); | 1167 fflush(stdout); |
| 1108 }else if( !quietFlag ){ | 1168 }else if( !quietFlag ){ |
| 1109 static int prevAmt = -1; | 1169 static int prevAmt = -1; |
| 1110 int idx = pSql->seq*g.nDb + pDb->id - 1; | 1170 int idx = pSql->seq*g.nDb + pDb->id - 1; |
| 1111 int amt = idx*10/(g.nDb*g.nSql); | 1171 int amt = idx*10/(g.nDb*g.nSql); |
| 1112 if( amt!=prevAmt ){ | 1172 if( amt!=prevAmt ){ |
| 1113 printf(" %d%%", amt*10); | 1173 printf(" %d%%", amt*10); |
| 1114 fflush(stdout); | 1174 fflush(stdout); |
| 1115 prevAmt = amt; | 1175 prevAmt = amt; |
| 1116 } | 1176 } |
| 1117 } | 1177 } |
| 1118 createVFile("main.db", pDb->sz, pDb->a); | 1178 createVFile("main.db", pDb->sz, pDb->a); |
| 1119 openFlags = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE; | 1179 sqlite3_randomness(0,0); |
| 1120 if( nativeFlag && pDb->sz==0 ){ | 1180 if( ossFuzzThisDb ){ |
| 1121 openFlags |= SQLITE_OPEN_MEMORY; | 1181 #ifndef SQLITE_OSS_FUZZ |
| 1122 zVfs = 0; | 1182 fatalError("--oss-fuzz not supported: recompile with -DSQLITE_OSS_FUZZ
"); |
| 1183 #else |
| 1184 extern int LLVMFuzzerTestOneInput(const uint8_t*, size_t); |
| 1185 LLVMFuzzerTestOneInput((const uint8_t*)pSql->a, (size_t)pSql->sz); |
| 1186 #endif |
| 1187 }else{ |
| 1188 openFlags = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE; |
| 1189 if( nativeFlag && pDb->sz==0 ){ |
| 1190 openFlags |= SQLITE_OPEN_MEMORY; |
| 1191 zVfs = 0; |
| 1192 } |
| 1193 rc = sqlite3_open_v2("main.db", &db, openFlags, zVfs); |
| 1194 if( rc ) fatalError("cannot open inmem database"); |
| 1195 sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 100000000); |
| 1196 sqlite3_limit(db, SQLITE_LIMIT_LIKE_PATTERN_LENGTH, 50); |
| 1197 if( cellSzCkFlag ) runSql(db, "PRAGMA cell_size_check=ON", runFlags); |
| 1198 setAlarm(iTimeout); |
| 1199 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK |
| 1200 if( sqlFuzz || vdbeLimitFlag ){ |
| 1201 sqlite3_progress_handler(db, 100000, progressHandler, &vdbeLimitFlag
); |
| 1202 } |
| 1203 #endif |
| 1204 do{ |
| 1205 runSql(db, (char*)pSql->a, runFlags); |
| 1206 }while( timeoutTest ); |
| 1207 setAlarm(0); |
| 1208 sqlite3_close(db); |
| 1123 } | 1209 } |
| 1124 rc = sqlite3_open_v2("main.db", &db, openFlags, zVfs); | |
| 1125 if( rc ) fatalError("cannot open inmem database"); | |
| 1126 if( cellSzCkFlag ) runSql(db, "PRAGMA cell_size_check=ON", runFlags); | |
| 1127 setAlarm(iTimeout); | |
| 1128 #ifndef SQLITE_OMIT_PROGRESS_CALLBACK | |
| 1129 if( sqlFuzz || vdbeLimitFlag ){ | |
| 1130 sqlite3_progress_handler(db, 100000, progressHandler, &vdbeLimitFlag); | |
| 1131 } | |
| 1132 #endif | |
| 1133 do{ | |
| 1134 runSql(db, (char*)pSql->a, runFlags); | |
| 1135 }while( timeoutTest ); | |
| 1136 setAlarm(0); | |
| 1137 sqlite3_close(db); | |
| 1138 if( sqlite3_memory_used()>0 ) fatalError("memory leak"); | 1210 if( sqlite3_memory_used()>0 ) fatalError("memory leak"); |
| 1139 reformatVfs(); | 1211 reformatVfs(); |
| 1140 nTest++; | 1212 nTest++; |
| 1141 g.zTestName[0] = 0; | 1213 g.zTestName[0] = 0; |
| 1142 | 1214 |
| 1143 /* Simulate an error if the TEST_FAILURE environment variable is "5". | 1215 /* Simulate an error if the TEST_FAILURE environment variable is "5". |
| 1144 ** This is used to verify that automated test script really do spot | 1216 ** This is used to verify that automated test script really do spot |
| 1145 ** errors that occur in this test program. | 1217 ** errors that occur in this test program. |
| 1146 */ | 1218 */ |
| 1147 if( zFailCode ){ | 1219 if( zFailCode ){ |
| (...skipping 26 matching lines...) Expand all Loading... |
| 1174 sqlite3_int64 iElapse = timeOfDay() - iBegin; | 1246 sqlite3_int64 iElapse = timeOfDay() - iBegin; |
| 1175 printf("fuzzcheck: 0 errors out of %d tests in %d.%03d seconds\n" | 1247 printf("fuzzcheck: 0 errors out of %d tests in %d.%03d seconds\n" |
| 1176 "SQLite %s %s\n", | 1248 "SQLite %s %s\n", |
| 1177 nTest, (int)(iElapse/1000), (int)(iElapse%1000), | 1249 nTest, (int)(iElapse/1000), (int)(iElapse%1000), |
| 1178 sqlite3_libversion(), sqlite3_sourceid()); | 1250 sqlite3_libversion(), sqlite3_sourceid()); |
| 1179 } | 1251 } |
| 1180 free(azSrcDb); | 1252 free(azSrcDb); |
| 1181 free(pHeap); | 1253 free(pHeap); |
| 1182 return 0; | 1254 return 0; |
| 1183 } | 1255 } |
| OLD | NEW |