mirror of
https://github.com/tursodatabase/libsql.git
synced 2025-05-17 22:56:59 +00:00
* Remove unused elements from the json_tree() cursor. FossilOrigin-Name: 914a50117d477b2cd30d58388fb8d1b71ff7ff6842ba025f38efc6e9647d06d0 * Same results as the legacy JsonNode implementation on a small set of test cases. FossilOrigin-Name: c3da4b079a1a15a4c0b1a6e71f876648b1d9eb32eddc67b9946c2475c7b6d085 * Fix corner-case error conditions. FossilOrigin-Name: ec23d34ab75e1d7e9366e59c633e0d30def8759f6d4717583ebeb4c90aeccf0d * All tests passing. FossilOrigin-Name: b5a5660ca22437640c9bf32c44d92c76a7293dafcbaf4fa6a4c171128d64871d * Give the json_valid() function an optional second argument that determines what is meant by "valid". FossilOrigin-Name: a4e19ad43dac81e7655ec03ff69bb99d1d02b0c227034c90fb41415fd4793fe3 * Enhance the (SQLITE_DEBUG-only) json_parse() routine so that it shows a decoding of JSONB when given a BLOB argument. FossilOrigin-Name: af267868562e0799ad691dccad05f17afbc34d609eede8c55f57d209290246ef * In SQLITE_ENABLE_SETLK_TIMEOUT builds, use blocking locks in place of sleep() when opening a read-transaction. FossilOrigin-Name: a51ef39998e25e86bd0600e71d15011b12e05f4319608018293bdaecb09e8c97 * Have SQLITE_ENABLE_SETLK_TIMEOUT builds block when locking a read-lock slot. FossilOrigin-Name: f797baf47cf7859cfd8ce248f4f3087af4551a7040af990333426e5a7c269504 * Add untested (#ifdefed-out) code for the MergePatch algorithm against JSONB. Add (and test) the jsonBlobEdit() routine that is needed by the new MergePatch. FossilOrigin-Name: 4d353387fc10e1038cfdd86e66007bf728c231a928e588897bbee0fbfe76f225 * More aggressive use of jsonBlobEdit(). Improvements to the MergePatch implementation sketch. FossilOrigin-Name: fbca9570fd2e1465739e4d3a8d9bb40fad594fd78ab49b2cb34efa27ebdd8361 * The json_patch() code for JSONB compiles and works sometimes, but there are still issues. Incremental check-in. FossilOrigin-Name: e0099464a0045a04f4ccf29bc2b8325fc8c7f39ccf4847e74818f928c9153588 * All legacy tests are passing. FossilOrigin-Name: 2c436806b8d5f57de99c00f6154b038454fb9ae427d00d7b4a46ab9c7c69bcb9 * Handle an SQLITE_BUSY_TIMEOUT error if one occurs while attempting a shared lock on a read-lock slot. FossilOrigin-Name: 5fbf3906d272df3eb981f67455eb35f649ad2774cba9fc3f077b28d9bef3f0cb * The json_remove() function now uses only JSONB, never JsonNodes, internally. FossilOrigin-Name: b69786e746ae2b927b64d9871fd120b7f8f06cc53739fd46a4da51aa16cf8576 * Attempt to get json_extract() working with pure JSONB only, and without the use of JsonNode. Mostly working, but there are some differences from legacy in corner cases. FossilOrigin-Name: 8c324af1eca27e86adc45622af4f3b06a67a3f968596ac58aa7434b1f6f05f3c * Preserve flexibility in the format of the RHS of -> and ->> operators found in legacy. FossilOrigin-Name: 6231ec43adb7436195eb1497de39a6c13c6b4f1c5032e6ea52515d214e61fdbc * Do not set the J subtype when the output is JSONB. FossilOrigin-Name: 4f106b64fe8988435872806bd0a6c223b61f53af0dd1c47c847bb4eec4e03e27 * Convert the json_array_length() function to use JSONB instead of JsonNodes. FossilOrigin-Name: 5ab790736d943e08f097efcee5cfbf0d83c65b0a53f273060330ba719affa5e5 * The assertion change at check-in [7946c79567b0ccd3] is insufficient to fix the problem of a Table object being deleted out from under the OP_VCheck opcode. We need to reference count the Table, which is accomplished here. FossilOrigin-Name: cad269d5e274443c39203a56603b991accc0399135d436996fc039d1d28ec9db * In the recovery extension, if a payload size is unreasonably large, it is probably corrupt, so truncate it. FossilOrigin-Name: 988c3179e978a3a6d42541e9c7a2ab98150383671810926503376ed808f150ff * Fix signed integer overflow in fts5. FossilOrigin-Name: 60e46c7ec68fd8caaed960ca06d98fb06855b2d0bb860dd2fb7b5e89a5e9c7b4 * The json_patch() function now operates exclusively on JSONB. This patch also includes improvements to JSONB debug printing routines. FossilOrigin-Name: fee19d0098242110d2c44ec7b9620c1210ef3f87913305f66ec85d277dd96ab6 * Convert the json_error_position() routine to use only JSONB internally. FossilOrigin-Name: e7a8ba35bff6fde55827f978de5b343b6c134c7fa53827f5c63915a9dc2598ad * Convert json_insert(), json_replace(), json_set() to use JSONB internally. Mostly working, but some corner cases are still not quite right. FossilOrigin-Name: 99c8f6bd5c9a31b6d00f92e383bec8a8235ed553916ad59adbb1b7663f6ebff1 * Update some OPFS-related help text in WASM tests. Minor cleanups in speedtest1-worker.js. FossilOrigin-Name: 263f6d3a7784ef7d032dbf7a3265aca8dd70bf50797f28f6b2e8ddb6a301f83a * New test cases for insert/set/replace with paths that indicate substructure that does not yet exist. FossilOrigin-Name: 146c717c51940b2139befc45ac74e7a1c36ef3c32fd3cfe35b334488eebe6298 * New JSON test cases showing insert or set with missing substructure. FossilOrigin-Name: 6802b6459d0d16c961ff41d240a6c88287f197d8f609090f79308707490a49c2 * Simplification of the new JSON insert/set test cases. FossilOrigin-Name: 04c0d5644372446c924a2e31a26edf51ddc563a1990d170b0ed4739e3e8b239b * Enhance json_set() and json_insert() so that they create missing substructure. FossilOrigin-Name: cc7a641ab5ae739d31c24f0ad0caeb15a481a63fa8f13720718ea922c25862ff * Convert json_type() to use JSONB internally. FossilOrigin-Name: 83074835b900ce85cf67059e674ce959801505c37592671af25ca0af7ed483f1 * Add a basic batch-mode SQL runner for the SAH Pool VFS, for use in comparing it against WebSQL. Bring the WebSQL batch runner up to date, noting that it cannot run without addition of an "origin trial" activation key from Google because that's now the only way to enable WebSQL in Chrome (that part is not checked in because that key is private). Minor code-adjacent cleanups. FossilOrigin-Name: 883990e7938c1f63906300a6113f0fadce143913b7c384e8aeb5f886f0be7c62 * Convert json_valid() over to using only JSONB as its internal format. FossilOrigin-Name: 7b5756fa6d00b093bf083a8d7a5ef5485f7a09e4eac473785c8380688f861a1b * Remove all trace of JsonNode from the JSON implementation. The JSONB format is used as the internal binary encoding for searching and editing. FossilOrigin-Name: 11ebb5f712cc7a515e2e0f2be8c1d71de20c97fe5b74c4f4d72c84fd21182d35 * First attempt to get the JSON text-to-binary cache working. All test cases pass, but the cache seems not to help much. FossilOrigin-Name: 25ed295f300fea6185104a73721076bccd2b2a6e411c78564266fa6dca4ff70c * Cache is working better, but does not preserve the hasJson5 flag. FossilOrigin-Name: a12add7ab9f5aee5bb2ede0c4d22e599dd28f7a107dce72b2ea48ef92d233e8a * Fix up the JSON cache to work better. FossilOrigin-Name: 1fdbc39521f63aedc6f08ecaafa54ea467b8c6316a692a18ad01eecbf22a0977 * Different approach to querying a tokendata=1 table. Saves cpu and memory. FossilOrigin-Name: c523f40895866e6fc979a26483dbea8206126b4bbdf4b73b77263c09e13c855e * Remove old code for tokendata=1 queries. FossilOrigin-Name: b0a489e8e1bf0290c2117ab32d78b1cc7d67bcb226b55ec044c8367ebde3815b * Performance optimization in the JSON parser. FossilOrigin-Name: 68d191f40e708962ec88e0c245b4496bc4a671300484b1cc0f3fc7e6d199a6e6 * Fix harmless compiler warnings and enhance performance the parser. FossilOrigin-Name: 285633da6d188547e52f07779e209c9e5f3dc33ce0668e14858f3337889ef4b8 * Unroll a loop in the parser for a performance increase. FossilOrigin-Name: a6dc29e4d5e13949e0fcd9d5dde575c2670eb10a230ab9df3806fc8c3016c540 * Remove a NEVER that can be true if a virtual table column is declared to have a DEFAULT. See [forum:/forumpost/3d4de8917627d058|forum post 3d4de8917627d058]. FossilOrigin-Name: 8abc2ccaf8106f20243568cd7fa74174386eb85d7ea381201e97e2fd527033e0 * Simplification and optimization of the JSON parser. FossilOrigin-Name: f5ec9485119a2a6cb33eb864c7ca9b41d4a2ed08ab6ad9a6b0dd9358ab253576 * Performance optimization in jsonAppendString(). FossilOrigin-Name: fdf00e96239c73fb67e2acecc5b95f55a1fc51c3deed4512613c0d6070ce5805 * Minor fix to the header comment on jsonXlateTextToBlob(). FossilOrigin-Name: c3677ba410208c07b711f5f526eb5cf039a8eee49f632c7ae04fa55cdfbb9058 * Fix potential unsigned integer underflow in jsonAppendString(). FossilOrigin-Name: d2fba2cbdc3870d34228c1a9446eced884325acc183900d7dd0b96132570fb4a * Do not allow a JsonParse object to be considered "editable" after an OOM. FossilOrigin-Name: c6bacf57bd6fe0fee00c9d41163a270b60997c20659949971bbf5c6c62622bfe * Protect a memcpy() against OOM conditions. FossilOrigin-Name: 26144d1c25ae0435db568009ba05e485d23d146f2b1f29f3a426c87860316aed * Ensure that tokendata=1 queries avoid loading large doclists for queries like "common AND uncommon", just as tokendata=0 queries do. FossilOrigin-Name: 7bda09ab404a110d57449e149a3281fca8dc4cacf7bd9832ea2a1356ad20fe8e * Take extra care to ensure that JSONB values that are in cache are actually owned by the JSON subsystem, and that ownership of such values is not handed back to the bytecode engine. FossilOrigin-Name: 1304534001e9ef66c6b12752b69d790bfa3427cc803f87cc48ca22ae12df0fdf * When tokendata=1 queries require multiple segment-cursors, allow those cursors to share a single array of in-memory tombstone pages. FossilOrigin-Name: e0175d07e4094db5ea4b0378a5ff480dafb6ba9da86a113fa767c4c89c3c866f * Fix harmless compiler warnings. Refactor some identifier names for clearer presentation. FossilOrigin-Name: 7e3941502789c5afaf19b08112f464abf5e3cba7f92fc9290af2a0f96127ad9a * Code and comment cleanup. Everything should work the same. FossilOrigin-Name: c640754df0d3ffdad994745f0d0e10c8f19f424b87f6a6e6e269491a0350b950 * Fix various compiler warnings and other problems with the new code on this branch. FossilOrigin-Name: 3a623cfa173b4035c759cb84985d11d8727053beb383648503987d6ab15c0ef0 * Fix harmless compiler warnings reported by MSVC. FossilOrigin-Name: 419652c0c82980bd043584dcd2976f91dfff7b926b216d597698299850b855c0 * Implement strict JSONB checking in the json_valid() function. FossilOrigin-Name: 0f26d38880fcbc207abcc94dbc170a7428bab1b4f0b7731aaf5bee0224000994 * Minor code changes for consistency and to simplify testing. FossilOrigin-Name: df272bd837910ad9e03e222716a1201a601399664365f1dcf73d5932372518ed * Do not let bad hexadecimal digits in malformed JSONB cause an assertion fault. FossilOrigin-Name: 8dec1ba1e5076ff596756e00c1e2ada0245f168a503dd1cadadf848331acfac3 * Enable incorrect JSONB to be rendered into text without hitting an assertion for a bad whitespace escape in a string. FossilOrigin-Name: 4d6a9a217df6792b41766b774fb0c0553b45f9104c26a0955bf4a30862d7d7bf * Ensure that OOM conditions in the generation of the "bad JSON path" error message result in an SQLITE_NOMEM error. FossilOrigin-Name: aa0e02b5c26a2ef3d6216a0ed8bc01382be43173485f898cb63f2a8c559f2e74 * Avoid problems when the path argument to json_tree() contains embedded U+0000 characters. FossilOrigin-Name: 9f055091af01a5dddba1a7e9868ad030c8f206237e1569215cb161e53e54aa71 * Remove dead code. Improved reporting of errors in JSON inputs. FossilOrigin-Name: 2eaa738e6b5c1b67b3e57c868d9c3a30eea38a0b3b8b02482f06d57a45b10921 * Back off on the use of strlen() for situations where sqlite3_value_bytes() will work as well, for performance. FossilOrigin-Name: 79fb54fbb8b9c30f47cdbd437d24a21542716241e822749e5e28c9fbc449bfa8 * Better pre-scan size estimations for objects in the JSON parser resulting in fewer reallocations and memmove operations. FossilOrigin-Name: 526b27f90897f5e35dfff7257daf6c4ce4798d649b09b8aecfb02df0449e3c51 * Repair issues and inefficiencies found during testing. FossilOrigin-Name: ae973cb1515f9d76409c92a2ca2ffd6b71f32b0b490a4886770e7c1b90f12611 * Add tests for using tokendata=1 and contentless_delete=1 together. FossilOrigin-Name: a2506b8c9718054912270055638204753c4156bbc115e55194e6df9d7e76cb10 * Two new NEVER macros. FossilOrigin-Name: 52632c92cb06faf0e804654b3490fd6c199521107bd30c8fcbc3a2a5a488098f * Remove reachable ALWAYS and NEVER macros. FossilOrigin-Name: f601de3eeabd85993c1f5ee96b62de6fdabbeae2fe8950e00d08feb48d42c498 * Fix bug in xInstToken() causing the wrong token to be returned. FossilOrigin-Name: da78d07e77cbc783fbc725758911c230fd6a1c1885d9576125de955dcc2bd37f * Continuing simplifications and code cleanup. FossilOrigin-Name: ddf92b5059a9106753fd18b82ba8daa269a62af947561c460790107b83416f0b * Fix a problem with the xInstCount() API and "ORDER BY rank" queries. FossilOrigin-Name: 317a50563d9e8586fda136e513727241b414e7267d50a06571c8ebd0eae710bc * Fix memory leak in new code on this branch. FossilOrigin-Name: ebc160b9a05568df66f86e30804399ee29d34b44a60c57e062f98cb92826353f * Fixes for xInstToken() with tokendata=0 tables. And with prefix queries. FossilOrigin-Name: 78fbb71598b1ca756acc078253880a1d0f7983a5a26b9efc683e6488122505a1 * Fix errors in rendering JSON5 escape sequences embedded in JSONB. FossilOrigin-Name: f1a51ae3863557526a51c6e98e71fcdf4f1ed14a36212b3c90f7408f926345e4 * Do not make the input JSONB editable in json_remove() if there are no PATH argument. FossilOrigin-Name: 66594544f3ba9977475a3e3f74404eb2b2fb845053b28bd24c2b52c7df94e9d7 * Fixes to error handling in json_array_length(). FossilOrigin-Name: aa85df2d26b74c171c55bde19ef17c4f11f40b8af7181bbf7162f87cdea7e88b * Add further tests for xInstToken(). FossilOrigin-Name: 8582707f16133f003a6687f68cbea03d4eb6c2a0e2e07746b7cace0c44e84fa4 * Rename the internal routine jsonMergePatchBlob() to just jsonMergePatch(). FossilOrigin-Name: ebf667b616235bb64b83832008342ba5e7b10b2c170d7cebc431f040fef7ecfb * Fix OOM and corrupt JSONB handling in json_patch(). FossilOrigin-Name: 1910feb0b7d5cc2b810c3322f6cca281d8730182d30d162bd7bb56800979ea91 * Use an assert() to fix a harmless static analyzer warning. FossilOrigin-Name: a249ca657e624028bc6b3d2c2bcedd7162d118addb7d62ce519920cecebf1860 * Clean up the JSONB performance test script. FossilOrigin-Name: 905301075a7fc1010ee7e754867b1b698c9b8576d50e98125def32a5dfb7ee9d * Small performance gain by unwinding the string literal delimiter search loop in the JSON parser by one more level. FossilOrigin-Name: 4c587feac153e8ebe526559ec3d254f545f81e8d1ed3126f91a5ff25ec4aa72e * Use strspn() to accelerate whitespace bypass in the JSON parser. FossilOrigin-Name: 843197df08352bdff4b87be91d160e574572aded0d0c66142fd960000c0b4701 * Miscellaneous comment cleanup and typo fixes. FossilOrigin-Name: 59446dc0bd0091572122a3c8b4653d7a2dc867d16c4a5919f79b81bc3a673ce3 * Further tests for the new code on this branch. FossilOrigin-Name: 59d008b6c23ab900377bc696ee19381feb7614bac80546eae361e401c3620c4e * Use extra assert() statement to silence harmless static analyzer warnings. FossilOrigin-Name: 174c2b2eef5fecd96a5fc89b81032fe81f7801f12097cea10e7e7f0a02114813 * README.md typo fix reported in the forum and update all links from http: to https:. FossilOrigin-Name: 5c48acdbb44185b352b54911a57a6986d6c7e624bdeba2af48b985d29f0292bf * Increased rigor in comparisons between object labels in JSON. FossilOrigin-Name: 2bc86d145fccc07107b7753cb1a69122676d4096fe59c454497bd81a6142d45e * The rule for the RHS of the ->> and -> operators when the RHS does not begin with $ is that it must be (1) all digits, or (2) all alphanumerics, or (3) contained within [..] or else it will become a quoted label. FossilOrigin-Name: 0e059a546ec11fa5c6d007bd65c249ee2422f1facbdb2792c53e0bc0ccc97e14 * Test cases for object label matching with escape sequences. FossilOrigin-Name: c6f2aa38e95b7888650cfa7bb773b18a28e01d883033ac77be6d504ffe417d18 * In CLI, move -interactive flag handling back to arg-loop pass 2. FossilOrigin-Name: 63cb05a862532d2d56e9e81fe32ced09bf58f03146587a118f11c2a84e195e69 * Fix the routine that determines the json_tree.path value for the first row so that it correctly takes into account escape sequences in the path argument. FossilOrigin-Name: b9243ee8a37c62eb8848e765bd4af83bc1b3d3eb24fb4268a1357ad1f8b2e1fb * Correctly handle 8-byte sizes in the JSONB format. [forum:/forumpost/283daf08e91183fc|Forum post 283daf08e91183fc]. FossilOrigin-Name: 73d390f39c0bbbc017e01544e4d43c76761f2599bd57f900131c706270dfd202 * Update documentation comments in fts5.h. FossilOrigin-Name: 38c50e22c98607e6c1fd78d7615cda534773b6d4fd85c712b54749fcd7af0c83 * Work around LLVM's newfound hatred of function pointer casts. [forum:/forumpost/1a7d257346636292|Forum post 1a7d257346636292]. FossilOrigin-Name: ec0ae4030968c782af48d1c776351c14b2ada21d40aeb97915f33df30706e18f * Fix compiler warning about shadowed variable in fts5_index.c. FossilOrigin-Name: ee70e4c1c9c41617850228e48d8df44f105cf2fbbe789340ceca6f27ad6ce5eb * Improved detection of corrupt JSONB in the jsonReturnFromBlob() function. FossilOrigin-Name: b014736c1f80ccc46fb4b24ac04310a6ce5cb5b6653665efff366cb3bc742257 * Add ALWAYS() on branches added in [ec0ae4030968c782] that are always true. FossilOrigin-Name: 451cef8609e96dd9244818adc5c6f240544694bcb4ae620e88f90e403e59d70f * Rework the jsonEachPathLength() routine in json_tree() so that it is less susceptible to problems due to goofy object labels. FossilOrigin-Name: 858b76a00e8ff55215f7a2e6a4cd77fc4d4f98dea7224cd90488744f5ce246a4 * Different fix for the fts5 COMMIT-following-OOM problem first fixed by [fba3129d]. This one does not cause problems if an fts5 table is renamed and then dropped within the same transaction. FossilOrigin-Name: d8c6b246944934a7a6e027b3f5b986fd64a19dd5c5c5175f4ea8586da59a6764 * Fix a problem with handling OOM and other errors in fts5 when querying tokendata=1 tables. FossilOrigin-Name: bc911ab5953532956510c199be72b1d3c556f2d0ddbd7fc0ae6f5f917b337b48 * Fix a null-pointer dereference in fts5 tokendata=1 code. FossilOrigin-Name: d69fa8f0504887f968d9a190ecb889ddb40bb1b56d0d4479f9819c106aec719b * Avoid an assert() failure when querying an fts5vocab table that accesses a tokendata=1 fts5 table with corrupt %_data records. FossilOrigin-Name: 386ba9e20423fb2f623d6adc9d3c310fb1b135f54a1dad15ef3b593d97886926 * Ensure an fts5vocab table never uses a special tokendata=1 merge cursor. FossilOrigin-Name: 1e26510e83b40c9bd2e8bfa2a0e81f2cb915e78fed773204ef537683e48b61dc * Avoid dropping an error code in new fts5 tokendata=1 code. FossilOrigin-Name: a66596e33dc9aa4bab2ec3ff45546e1321d0a11bdc764f8381b315292ca92423 * Fix a harmless compiler warning about "confusing indentation". FossilOrigin-Name: 34f9e9a8c4bea13f60f43062e25cd7d9422f2e7f5b371ed0ddadc9abeb3ca256 * Fix a potential problem RCStr access on a JsonString object that is not really and RCStr. Fuzzer/UBSAN find. FossilOrigin-Name: d2f2174ce2cc89606034e158149a2d05fc3627ec4d5cdb772add7a2250f29d78 * Fix a harmless UBSAN warning. FossilOrigin-Name: 1503cba6d17e9bade7a5c103ddd23241ff4741f9a2e3032ffe2987af243dae65 * Fix a potential use of uninitialized value in json_valid() with 2nd argument of 8. FossilOrigin-Name: fa102036fe46eeb71b7df3e265be1935ae5c78e0b939b08841bcfb8abadbc77a * Work toward enhanced functionality for json_valid() with deep checking of the JSONB (second argument has bit 0x08). FossilOrigin-Name: c370d573198b151767f04e91bf8baa4ae0076751ae468c5709742a0b0ed16770 * Add SQLITE_TESTCTRL_VALIDATE_JSONB, which if enabled under SQLITE_DEBUG causes cross-checking of generate JSONB. FossilOrigin-Name: b410a4db74a650003539ffaaea18519d5159b504daac47db6a4874b730f40ac8 * Rename the new test-control to SQLITE_TESTCTRL_JSON_SELFCHECK. Make it so that the current value of the setting can be interrogated. FossilOrigin-Name: 7aff1d9a4cb17ecd5abab21ab032f35a78741dd641ddd8cbcc85fc4a81a0707d * Activate JSON_SELFCHECK within fuzzcheck. FossilOrigin-Name: 4d14e733bb521aed65e98533969d2303738232ae87dab70fdf7962e6513195f5 * json_valid(*,8) allows minus-signs on hexadecimal literals. FossilOrigin-Name: c0d7f4520d839a268b3fd2474d0897a9832aa608bd6238b3e287fabecf07a350 * json_error_position() now uses jsonValidityCheck() to find the approximate position of an error in a JSONB blob. FossilOrigin-Name: c3d60cf7028a333b825d5b89516945a73e0c158ac81d8bcc117d21bfd98602c8 * The json_error_position() function now reports an approximate byte offset to the problem in a JSONB if there is a problem. FossilOrigin-Name: 80d5d94dff6a2d2557039be3d7d47c1a6003c4b98defe0bd411acfeb963ad5dd * Validity checking of text nodes in JSONB. FossilOrigin-Name: fa5160687c2f970d407e8af73c246f7cd806bb4ce35f29a79ac534a8646a6c8e * Improvements to JSONB validation - catch more cases where the input does not conform to spec. FossilOrigin-Name: be1864eac4eb75cc30bf98f73092c8608467f4bd956240df6a0cbea9f1e09e85 * Add NEVER to two unreachable branches in JSON. FossilOrigin-Name: c96ebb086feb89341565cc52b970ae7799ce1327fe1ad4fc790f1b0dcaa6e229 * Worker1 Promiser API: when multiple db connections are active then use the requested connection instead of always the first-opened connection. Bug reported in [forum:894c330e7f23b177|forum post 894c330e7f23b177]. FossilOrigin-Name: 194276e18e0268829061c09317e7f9f527a703eb45f1755ff1dd30bd99dc1b68 * Fix the JSON object label comparison object so that it works correctly even if the label ends with escaped whitespace. FossilOrigin-Name: 4d5353cadd7b7c5f105bc197f3ec739e2d041472d6b3e939654c9f9cfc2749ae * Improvements to UTF8 handling, and especially the handling of invalid UTF8, in the JSON routines. FossilOrigin-Name: 1b229c1101d6c384a30f343c5e47b471ab084b2d8e81170eb8f642afc1c67e3b * Bug fix in the JSONB validator. dbsqlfuzz ac6fa521a08609a642198e7decf64180e750b3c4 FossilOrigin-Name: 3e940a6a08b0a0434650cd3d8dd286e09ad8ab805b0a4d515e57bba5d3608577 * Avoid invoking sqlite3ExprColUsage() on an unresolve column reference. dbsqlfuzz fc34aa62df4de103705d11b807074687ffafbda5. FossilOrigin-Name: ac9314c0e335694b48c613145f5397247bb88c51806cd0dc3ed4ec306db4bbad * In CLI, fix .read inability to open 2GB+ files on WIN32. FossilOrigin-Name: 56c80a62d2e033d64ba5d545ae9cbe3ed7c9d046c0a3fafb6cfa2f0b562d1ef0 * Pass subtype information through the aggregate ORDER BY sorter for aggregate functions that use subtype information. FossilOrigin-Name: 3536f4030eab6d650b7ed729d2f71eb6cc3b5fbe16b4e96b99008d66522aaccb * Improve the error message returned by an fts5 'rebuild' command on an external content table if there is a problem with the content table or view. FossilOrigin-Name: 0fbf4b8a58fde1c187908934da6f59999b146f32e07ac255cc531c5c4d7007fd * Fix harmless compiler warnings in JSON and FTS5. FossilOrigin-Name: 90135efccfeb1046f002bfcbd8dfec9a1a3b40cbe1b5c714ae065b06368e354f * Add assert()s to FTS5 to fix static analyzer warnings. FossilOrigin-Name: 27d4a89a5ff96b7b7fc5dc9650e1269f7c7edf91de9b9aafce40be9ecc8b95e9 * Use SQLITE_STRICT_SUBTYPE=1 by default for the JNI and WASM builds unless they're explicitly built with SQLITE_STRICT_SUBTYPE=0. FossilOrigin-Name: 990211357badf0ab08bd34cf6d25b58849d0fd8503e289c1839fc837a74e1909 * Correct --enable-sab flag in ext/wasm/GNUmakefile to fix a silent alhttpd args-parsing error. FossilOrigin-Name: 7b9b757d872a31395b0f6454e2309a6a4664b8bdd8749f6a15371cbe72c05b60 * Avoid running the "no_mutex_try" tests with SQLITE_ENABLE_SETLK_TIMEOUT builds as part of the release test. FossilOrigin-Name: 6b4e1344a28c213cbe8fb97f7f3f6688de93fb73ed96bf460ff74c959da1a712 * Do not run test script fts5origintest4.test with either "memsubsys1" or "mmap" permutations. FossilOrigin-Name: 05a63d9603ef42cbee6dadff72d97583a9c78e549f70e9a808534d5c1ae7c28a * Fix a new JSON test case so that it works even if SQLITE_OMIT_VIRTUALTABLE is defined. FossilOrigin-Name: b995aae510888a9746b46545d176a0885d4738e1f1bc0b7ad7937ed023efd7d6 * Add mention of --buildonly and --dryrun to the testrunner.tcl usage screen. FossilOrigin-Name: 23b92d915c12ee768857e2c3c961832f390cad9b53b8bcfc2b97664baab25bb7 * Avoid expiring prepared statements in the middle of an integrity-check. FossilOrigin-Name: 88beb48472da4667c0727c8ebabe046ea526450ff837fe789d041ed3f1ff105e * In the count-of-view optimization, deferring freeing obsolete parts of the parse tree, on the off-chance that some other part of the code might be holding a pointer to those parts. FossilOrigin-Name: da442578856c87137eb1677d9b13b7c1cf15828cc41d4756572b278060f69bae * New test case based on Chromium bug report 1511689. FossilOrigin-Name: 2c7ef4b4d215f99f8d6787adb64e2037ae96e5dd6cb49c8b81634249f5e1b328 * Enable SQLITE_STRICT_SUBTYPE for default builds of the shell, fuzzcheck, and testfixture. FossilOrigin-Name: 5a0c517ed7e46c0f8a3db752cf5b9f8010c60f35084606abe9e7c1c4f993b4a7 * Enhancements to the "randomjson.c" extension. Automatically load that extension into fuzzcheck. FossilOrigin-Name: 70620405ab01d6a5d38bafa9ae175fd6e4eabaf2efb7854734278dafd7b05c99 * Enhancements to ext/misc/randomjson.c. FossilOrigin-Name: a4e6d1f86f3a502e4170f5a90031e269e48363e95114a66b84d373e3ce0b2704 * Bug fix in the randomjson.c extension. FossilOrigin-Name: 1f3a33df530dbe330ea8b14a69369b807b413b25a167d1a3938f8f0faf97cc91 * Ensure that all object labels for individual objects generated by randomjson.c are unique. FossilOrigin-Name: 29c46aca231b3f1e997ef306a5a651408185bf3ad09ab9fc1fe21ed18caa4d02 * Add randomjson.c to testfixture. Use it for a new set of invariant tests against JSON functions. FossilOrigin-Name: f1c040606bfe784804134d8f3ca130908fad5212b47e3c32792baab977470943 * Ensure that the insert/delete size delta on JSONB objects in the JSON cache are always set to zero. FossilOrigin-Name: 4b4581668a908473dbf1322a3e98bc7cca122998c44518ea183af7f0d1ba9f95 * Fix JSON to JSONB translation so that it deals correctly with Infinity and NaN. FossilOrigin-Name: 178cb84f36bdb45ba17511900d6d8ea8dfa14912fc5bf7094a20348174a36c95 * Add NEVER() to an unfalsifiable branch. FossilOrigin-Name: 9a0c67db366d38a0b0741f6a1ae333cf27cfe6f6b7c6eed94bdec9686f9f9f8a * New JSON invariant test cases. FossilOrigin-Name: a6a1367b0bf364b1a2e20e153c5f4a578624b8846f9ec0b7c9c3cba0ea2ec346 * Remove a stray comment in the JSON code. FossilOrigin-Name: 6618bdf0679405b43911ea8cd94050b12a5dc469f3dfe4759ee3ff850a55229e * Extra ALWAYS() macros to verify state in the sqlite3ExprCanBeNull() routine. FossilOrigin-Name: be19b84c9f3fe127165809908add148dbe9a827a55608b0490de7e69b7f7f191 * Always make the sqlite_dbdata virtual table available in the CLI. FossilOrigin-Name: e5fd3b32ad87586a7413570e568c9c1859a37a4f836cca074126471b125fb682 * When unable to resolve an identifier, change the Expr node into TK_NULL rather than TK_COLUMN, to prevent any downstream misuse of the non-existent column. dbsqlfuzz 71869261db80a95e4733afa10ff5724bf3c78592. FossilOrigin-Name: d2e6117e4f97ab98b01deb5fcad5520f8181d00bed8d904d34963c01d73df857 * Test case for the previous check-in. FossilOrigin-Name: df5a07e1a5122e08c2fa6076ac08adb2820f997ee11dd88b84863666899dfb57 * Ignore COLLATE operators when determining whether the result of a subexpression should be shallow-copied or deep-copied. FossilOrigin-Name: 34ae36a45e814bed7c8340412c7ef3fc849b82357656d0eb5f0f805e59d846d0 * Add ALWAYS() and NEVER() on branches made unreachable by recent changes. FossilOrigin-Name: c50e6c2ace49d0928b05cbfd877c621e9a0f77dc4e056ccb1dbe5cf118a00d00 * More precise computation of the size of data structures in the query planner. Response to [forum:/forumpost/7d8685d49d|Forum post 7d8685d49d]. FossilOrigin-Name: 0c8d88e41167ea92341dd1129be01b596a73f46bdcd5b0dd931441a979c013d0 * Fix harmless compiler warning in the randomjson.c extension. FossilOrigin-Name: debe7060b16669ada7304ffb9bf7616c8fa30bd286d8be871ed17fd6d64a3d4c * On second thought, we don't really need sqlite_dbdata accessible to the CLI. FossilOrigin-Name: 36fe6a61ef8fb393281a5e15119d716521219c7b971fbfd63bdea07d27a78ac9 * Remove redundant conditional from sqlite3ExprCanBeNull(). FossilOrigin-Name: 257f96a2d22c605885fa66220c28cf7dc5941c330bccee3f132b9e7b70d89d30 * In JSON - minor code cleanup and refactoring with a small size reduction and performance increase. FossilOrigin-Name: 215fabda38daecdbd38b1eca5a6aafbc61b6a36a8303f1d7164d5a1138e63134 * Avoid harmless integer overflow in pager status statistics gathering. Response to [forum:/forumpost/7f4cdf23f9|forum post 7f4cdf23f9]. FossilOrigin-Name: 206d8c650d937bc700946c40a82a62ea6bc4a80e5f3fb42d0ae2968de25f0644 * Fix SQLITE_ENABLE_SETLK_TIMEOUT assert() statements in os_unix.c to avoid reading past the end of the unixShmNode.aMutex[] array. FossilOrigin-Name: 029a05cd2928d43d81e4549cce5388c432e2c9e75e3fa0b2fe6e91021b2fb9ac * Add internal core-developer-only documentation of the JSONB format. FossilOrigin-Name: 4d30478863b2a60512010de9ec6e3099bfaf75d4afee20acec536713fe94334d * Add a new comment to debugging output routine sqlite3WhereLoopPrint() to remind us of what the various fields of the debug output mean. No changes to code. FossilOrigin-Name: da5f34fd4052432b1ae27bb12e56b358cdc5c1282653d60ed0f0fe62f727e4ee * Fix a usan complaint about signed integer overflow. FossilOrigin-Name: e65907e0279f4814ec957f0790777d8b94a86926cd27c52442b311b27efc0185 * Update #ifdef checks in pager.c and util.c to account for [0462a2612d1fc1d0] to resolve the build problem reported in [forum:9819032aac|forum post 9819032aac]. FossilOrigin-Name: 0f22d809a1c6c80e381f6bcd931fe4ec36dca0e28d07ab4f4f7f83c813424f60 * Add the -fno-sanitize-recover=undefined to the sanitizer builds used for sdevtest and release testing. To ensure that any test that provokes undefined behaviour fails. FossilOrigin-Name: 89563311adb0ab7c7a3eadb11c2e27fbca50c56fce8ca616628facbc00d72b88 * Change parameters on a debugging function to include "const". FossilOrigin-Name: 94c3e1110c6590261bd30ba317fba4dd94023d69b81a94f4b216cce748fe7489 * Add debugging output routines sqlite3ShowWhereLoop(X) and sqlite3ShowWhereLoopList(X) that can be invoked from a debugger to show a summary of the content of a single WhereLoop object or a list of WhereLoop objects. No change in release builds. FossilOrigin-Name: 5db30bcc338aac1cf081de2deec7e60749ae012e2b6f95ccf745623adb4a31dc * Improvements to the query planner to address the inefficiency described by [forum/forumpost/2568d1f6e6|forum post 2568d1f6e6]. FossilOrigin-Name: 72fcc12cda910a0e3f7875eb3d117b2a5608705c97703985427a02960f1ab5c5 * Avoid signed integer overflow during integrity_check of FTS5. FossilOrigin-Name: 5937df3b25799eceaadfb04d7226c9995d44c8d8edb5ac3ad02af9d7e3570726 * Fix harmless compiler warnings associated with [5db30bcc338aac1c] FossilOrigin-Name: e55d1c2333f35fc20615aa83a7843d08cae7945710a2156d44eee0cc37d90ade * Remove an ALWAYS() added in [c50e6c2ace49d092] because it is sometimes false. dbsqlfuzz c393a4f783d42efd9552772110aff7e5d937f15e. FossilOrigin-Name: b9daf37e57cde12c4de271a2b1995e8e91b6411f8c2e8882e536241929609b3a * Improved handling of malformed unicode within JSON strings. FossilOrigin-Name: e252bdf5f5de26ba8e2bcc6b0ad94121ed6fc4d86c02fe4a2a058ada93747beb * Ensure that the xColumnText(), xQueryPhrase() and xPhraseFirstColumn() APIs all return SQLITE_RANGE if they are passed a bad column or phrase number. FossilOrigin-Name: 1a8a9b1c89519d265869251e8b6d3c5db733f0d3a7dea6c7962811a8f1157dff * Fix a problem in the shell tool (not library) causing an out-of-bounds write if an ".open" command failed, then the user pressed ctrl-c to interrupt a query running on the substitute in-memory database. FossilOrigin-Name: 026618b9e321576f616a32e41329066ba629814170c6cfeef35430343f5003f3 * Enhance the (undocumented, debug-only) json_parse() SQL function so that it returns the text rendering of the JSONB parse of the input, rather than printing the rendering on stdout. FossilOrigin-Name: 056de8d551dcbdf1d162e2db15ed418fa9c786f900cd3972ef8a1dea3f4f3aa1 * Fix harmless compiler warnings in FTS5. FossilOrigin-Name: 3cd5ef44e40570c357f913a9483fa1cd72e7f2827a5ed5826bff99febae213b1 * Performance improvement by unwinding a loop in jsonAppendString(). FossilOrigin-Name: 190ab3c08431a0ba24d76392eab251f5c1792add05e4ec780998b299208eca95 * Update fts5origintext4.test to work with SQLITE_DIRECT_OVERFLOW_READ. FossilOrigin-Name: 15ed002aed12556aeb9bbe537c4ba839f0c95bac65a69d03401b37cc3fd11b92 * Enable SQLITE_DIRECT_OVERFLOW_READ unless it is specifically disabled using the -DSQLITE_DIRECT_OVERFLOW_READ=0 compile-time option. FossilOrigin-Name: 630604a4e604bfb36c31602917bfa8d42c10c82966d0819932bf8f827b9158b8 * Minor doc touchup in the JS bits. FossilOrigin-Name: 8d2120c35425081e2158d6a8a6b083c4adf8d694046b2d98f5fd235520920432 * Use SQLITE_ENABLE_STAT4 in both the WASM and JNI builds. FossilOrigin-Name: 99d11e6d0ae687ff6bac5119027f7b04d5e7185214e79cf8c56289cfa809b0f9 * WASM: various build cleanups and add initial infrastructure for a build which elides the oo1 API and its dependents (worker1 and promiser). Sidebar: an attempt was made to move generation of the build rules to an external script, but the mixed-mode make/script was even less legible than the $(eval) indirection going on in the makefile. FossilOrigin-Name: 563d313163c02b398ae85b7c2ed231019a14e006726f09a7c1f294a58bf4363f * JNI: move the ByteBuffer-using APIs from public to package visibility for the time being because they have UB-inducing possibilities which need to be worked out. Update test code to account for a change in custom FTS5 columntext() impls. FossilOrigin-Name: dc501275fcfab3ad9b6ebbadf7588b225a9dd07a0abac5be83d96f15bfba99e9 * Extra steps taken to avoid using low-quality indexes in a query plan. This branch accomplishes the same end as the nearby enhanced-stat1 branch, but with much less change and hence less risk. FossilOrigin-Name: c030e646262fee43a59b45fdc1630d972f8bf88ac3c142b6bdaf4cbb36695a4f * Remove some unnecessary computations from ANALYZE so that ANALYZE runs with fewer CPU cycles. These changes were spotted while working on the nearby enhanced-stat1 branch. So even if enhanced-stat1 is abandoned, that effort put into it will not have been in vain. FossilOrigin-Name: 5527e8c4abb904b1a438ec1c353d4a960bf82faaf3a2c742af1df7c613850441 * Back out [99d11e6d0ae6] (enabling of STAT4 in WASM/JNI), per /chat discussion. FossilOrigin-Name: cd7929ee2e2c305475fa5a4dff2edaccf90067126ef04a1c2714cf464925453f * Update and clean up the in-makefile docs for ext/wasm. FossilOrigin-Name: 7a7b295e6d7e95ee4a46cc42761895d11700ab295870c5a4380072bb4a5b7099 * Elaborate on the various build flavors used by ext/wasm/. Doc changes only. FossilOrigin-Name: d489232aa492618d4c8e5817addb2323d0ca067742d7140216914239a66fb221 * Increase the default "max_page_count" to its theoretical maximum of 4294967294. FossilOrigin-Name: ffb35f1784a4305b979a850485f57f56938104a3a03f4a7aececde92864c4879 * Fix a problem in fts5 caused by a COMMIT involving fts5 data that immediately follows a ROLLBACK TO that does not. FossilOrigin-Name: 55c61f6a8d6a1bc79497b05669beac5c5397b06382bf24b6bec54845962d219b * Adjust the sqlite3PagerDirectReadOk() routine (part of the SQLITE_DIRECT_OVERFLOW_READ optimization) to use less code and to be more easily testable. FossilOrigin-Name: eed670ea2a9424f7df4eeb01c152fc38f7190a5e39aa891651b28dc91fcdc019 * Back out [b517a52fa36df0a0] which is no longer reachable due to early error detection enhancements in [166e82dd20efbfd3]. FossilOrigin-Name: 704943e96f2620b99260667ac9922c2f72bc3e92e2dfe1d9c2a91c7b704564d9 * Update the sqldiff.exe utility program so that it uses the sqlite3_str string interface, and so that it does console output using the ext/consio extension. FossilOrigin-Name: 4443b7e592da97d1cb1b3b79ed0559452d8057a33aba4d184c2fffbf200e05f5 * Enhance sqlite3_analyzer.exe so that it uses the ext/consio extension. FossilOrigin-Name: 769de0b98e136e4a0945b80216d0c9583c1ccd9de69cb0494875c2300e172646 * Change a constant from decimal to hex to avoid a compiler warning on Mac. FossilOrigin-Name: e3acb8a43ad544fd5b5341058276bd3b61b6bdb6b719790476a90e0de4320f90 * Convert the JSON functions to use lookaside memory allocation whenever feasible, to avoid hitting the global memory allocator mutex. FossilOrigin-Name: a79a244954f728596da3c0e28fa3b887258d1bd831f53881970f418f3fba84c7 * Fix a #ifdef in sqlite3_test_control() that was preventing builds with SQLITE_OMIT_WSD. FossilOrigin-Name: d546a9c94caf7408cc6e4530ec190d3a13fae09dc15b71b03d6369e02ee62abd * Restructure some code to fix what appears to be a false-positive UBSAN warning. FossilOrigin-Name: fe952c12903ea2150880c8bb57cda2efc00ce9fa801568a68c619e0745f30567 * Avoid errors with SQLITE_OMIT_VIRTUALTABLE builds in json106.test and unionall.test. FossilOrigin-Name: 90e8a233549a2d31e6959ce3fec927693b772ab3c0abce65e81d7350d2ca5cc6 * Update extension ext/misc/totext.c to avoid both ubsan warnings and dubious real->integer conversions. FossilOrigin-Name: c626aa108a7a30cef54af8d93ac9e45749568ed38e4e06623a6bad6b4bf6e8ec * Update JSON performance testing procedures for clarity and to describe how to do performance testing of JSONB. FossilOrigin-Name: b115b4f75bc7c4e6d9bab5edf13297f27a36f30083c80d2c502b01208da5dfc0 * Ensure that SQLITE_PROTOCOL is not returned too early when a SQLITE_ENABLE_SETLK_TIMEOUT build fails to open a transaction on a wal mode database in cases where blocking locks are not being used. FossilOrigin-Name: b934a33671d8a0190082ad7e5e68c78fe0c558d102404eafc1de26e4e7d65b92 * Updates to RTREE to facility testing. FossilOrigin-Name: 7a5b42ff74882c58493dc8b710fde73d4ff251f5d42271d84be73ceaabc01698 * Remove an ALWAYS() from RTREE. Dbsqlfuzz found a way to make it false. FossilOrigin-Name: 40f0a29e6dd90fcb969d7c0e49728ba0ee8f31d9e8f502b9a21469620a8ad283 * Minor change to os_unix.c to facilitate 100% MC/DC testing. FossilOrigin-Name: 0dfa7b4da134db281c3c4eddb4569c53a450f955f0af2f410e13db801aff4ea2 * Automatically turn off DEFENSIVE mode in the shell tool when executing scripts generated by the ".dump" command against an empty database. Add a warning to the top of generated ".dump" scripts that populate virtual tables. FossilOrigin-Name: 6e9e96b7e7afb9420110f4b93d10b945c9eadfde5e9c81e59ae9ee8167e75707 * Fix date on new file shell9.test. FossilOrigin-Name: c82da712113d5dcd63b764dbc68842026989627abc840acb4a33f3a4972b832a * Improved resolution of unqualified names in the REINDEX command. [forum:/info/74cd0ceabd|Forum thread 74cd0ceabd]. FossilOrigin-Name: 97709ce2a1f5ae05495e412ca27108048e5b8a63a1e3bca4be13933f7527da7b * Put an SQLITE_ENABLE_SETLK_TIMEOUT branch inside the appropriate ifdef with an assert on the else since the condition is always false if SETLK_TIMEOUT is not available. FossilOrigin-Name: d81e7a036ac5d70b6a6ee6ab7d81e041c1f5fc04b70bcee47e203d521caf7e93 * In fts5, flush the contents of the in-memory hash table whenever the secure-delete option is toggled. This prevents spurious corruption reports under some circumstances. FossilOrigin-Name: ccf552319a62bfb329820a3bc1f490bacbaa6e90694a257fc65a568a605542c3 * Fix a comment in sessions. No functional changes. [forum:/forumpost/8c20dc935b|Forum post 8c20dc935b]. FossilOrigin-Name: b0eb6d3628c1f70399a22d9fd3b79a796bc343adfeba50515440db609565961a * Have the shell tool automatically enable SQLITE_CONFIG_DQS_DDL when executing a ".dump" script against an empty db. FossilOrigin-Name: f47a5f4e0ce078e6cc1183e6cbb3c4013af379b496efae94863a42e5c39928ed * Version 3.45.0 FossilOrigin-Name: 1066602b2b1976fe58b5150777cced894af17c803e068f5918390d6915b46e1d * wasm build: reformulate an awk invocation to account for awks which do not support the -e flag. Problem reported on the forum via a docker-hosted build. FossilOrigin-Name: 90dd51153fd0a6197e2ee49b5492ad120f0bfc324b60651f3d4f47c286887b46 * When backing out a character in a constructed string in JSON, first make sure the string has not been reset by on OOM. FossilOrigin-Name: 950bf9fe7829864e0abe6d71ca0495f346feb5d7943d76c95e55a6b86ea855da * Ensure that the xIntegrity methods of fts3 and fts5 work on read-only databases. FossilOrigin-Name: e79b97369fa740f62f695057d4a2cf8dae48a683982ec879f04a19039c9cb418 * When a JSON input is a blob, but it looks like valid JSON when cast to text, then accept it as valid JSON. This replicates a long-standing bug in the behavior of JSON routines, and thus avoids breaking legacy apps. FossilOrigin-Name: 4c2c1b97dce46a279846380c937ac6de5c367927c6843516641eead7ea6db472 * Bump the version number to 3.45.1 FossilOrigin-Name: 54d34edb89430b266221b7e6eea0afbd2c9dafbe774344469473abc8ad1e13fd * Fix harmless "unused parameter" compiler warning in the new fts3IntegrityMethod implementation. FossilOrigin-Name: 9d459f6b50fb6f995e6284a0815c5e211cacac44aad0b96bf01ba68af97f51fc * In os_unix.c and os_win.c, do not allow xFetch() to return a pointer to a page buffer that is right at the end of the mapped region - if the database is corrupted in a specific way such a page buffer might be overread by several bytes. FossilOrigin-Name: d131cab652ac11795322af13d0b330e7e44ab91587a1a3e73fe7b9a14b2dd531 * Slight adjustment to test results for Windows in mmap1.test due to the previous check-in. FossilOrigin-Name: a8043eaed899285b5cf4aab0c23c3dabb8975910c353cb579fd1f1655db390f6 * Apply the same fix found in [99057383acc8f920] to descending scans. FossilOrigin-Name: 593d6a1c2e9256d797f160e867278414e882a3d04d7fea269bea86965eaa7576 * Automatically disable the DISTINCT optimization during query planning if the ORDER BY clause exceeds 63 terms. FossilOrigin-Name: 6edbdcc02d18727f68f0236e15dde4ecfc77e6f452b522eb4e1e895929b1fb63 * When rendering JSONB back into text JSON, report an error if a zero-length integer or floating-point node is encountered. Otherwise, if the node occurs at the very end of the JSONB, the rendering logic might read one byte past the end of the initialized part of the BLOB byte array. OSSFuzz 66284. FossilOrigin-Name: 3ab08ac75d97ffd9920f5c924362a4819560b40faa8a4f9100068057f5fa420a * Avoid a potential buffer overread when handling corrupt json blobs. FossilOrigin-Name: ac402cc551b2cbe3f8fbbc9c711a04942eab5eeb9d2f4a394e9370d2380427b5 * Detect malformed nested JSONB earlier and stop rendering to avoid long delays. FossilOrigin-Name: ab40e282465c989bf249453d7c6f60072a38b691f579411cdf9aad234b20f0f7 * Version 3.45.1 FossilOrigin-Name: e876e51a0ed5c5b3126f52e532044363a014bc594cfefa87ffb5b82257cc467a --------- Co-authored-by: drh <> Co-authored-by: dan <Dan Kennedy> Co-authored-by: stephan <stephan@noemail.net> Co-authored-by: larrybr <larrybr@noemail.net>
2558 lines
80 KiB
C
2558 lines
80 KiB
C
/*
|
|
** 2015-05-25
|
|
**
|
|
** The author disclaims copyright to this source code. In place of
|
|
** a legal notice, here is a blessing:
|
|
**
|
|
** May you do good and not evil.
|
|
** May you find forgiveness for yourself and forgive others.
|
|
** May you share freely, never taking more than you give.
|
|
**
|
|
*************************************************************************
|
|
**
|
|
** This is a utility program designed to aid running regressions tests on
|
|
** the SQLite library using data from external fuzzers.
|
|
**
|
|
** This program reads content from an SQLite database file with the following
|
|
** schema:
|
|
**
|
|
** CREATE TABLE db(
|
|
** dbid INTEGER PRIMARY KEY, -- database id
|
|
** dbcontent BLOB -- database disk file image
|
|
** );
|
|
** CREATE TABLE xsql(
|
|
** sqlid INTEGER PRIMARY KEY, -- SQL script id
|
|
** sqltext TEXT -- Text of SQL statements to run
|
|
** );
|
|
** CREATE TABLE IF NOT EXISTS readme(
|
|
** msg TEXT -- Human-readable description of this test collection
|
|
** );
|
|
**
|
|
** For each database file in the DB table, the SQL text in the XSQL table
|
|
** is run against that database. All README.MSG values are printed prior
|
|
** to the start of the test (unless the --quiet option is used). If the
|
|
** DB table is empty, then all entries in XSQL are run against an empty
|
|
** in-memory database.
|
|
**
|
|
** This program is looking for crashes, assertion faults, and/or memory leaks.
|
|
** No attempt is made to verify the output. The assumption is that either all
|
|
** of the database files or all of the SQL statements are malformed inputs,
|
|
** generated by a fuzzer, that need to be checked to make sure they do not
|
|
** present a security risk.
|
|
**
|
|
** This program also includes some command-line options to help with
|
|
** creation and maintenance of the source content database. The command
|
|
**
|
|
** ./fuzzcheck database.db --load-sql FILE...
|
|
**
|
|
** Loads all FILE... arguments into the XSQL table. The --load-db option
|
|
** works the same but loads the files into the DB table. The -m option can
|
|
** be used to initialize the README table. The "database.db" file is created
|
|
** if it does not previously exist. Example:
|
|
**
|
|
** ./fuzzcheck new.db --load-sql *.sql
|
|
** ./fuzzcheck new.db --load-db *.db
|
|
** ./fuzzcheck new.db -m 'New test cases'
|
|
**
|
|
** The three commands above will create the "new.db" file and initialize all
|
|
** tables. Then do "./fuzzcheck new.db" to run the tests.
|
|
**
|
|
** DEBUGGING HINTS:
|
|
**
|
|
** If fuzzcheck does crash, it can be run in the debugger and the content
|
|
** of the global variable g.zTextName[] will identify the specific XSQL and
|
|
** DB values that were running when the crash occurred.
|
|
**
|
|
** DBSQLFUZZ: (Added 2020-02-25)
|
|
**
|
|
** The dbsqlfuzz fuzzer includes both a database file and SQL to run against
|
|
** that database in its input. This utility can now process dbsqlfuzz
|
|
** input files. Load such files using the "--load-dbsql FILE ..." command-line
|
|
** option.
|
|
**
|
|
** Dbsqlfuzz inputs are ordinary text. The first part of the file is text
|
|
** that describes the content of the database (using a lot of hexadecimal),
|
|
** then there is a divider line followed by the SQL to run against the
|
|
** database. Because they are ordinary text, dbsqlfuzz inputs are stored
|
|
** in the XSQL table, as if they were ordinary SQL inputs. The isDbSql()
|
|
** function can look at a text string and determine whether or not it is
|
|
** a valid dbsqlfuzz input.
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <stdarg.h>
|
|
#include <ctype.h>
|
|
#include <assert.h>
|
|
#include "sqlite3.h"
|
|
#include "sqlite3recover.h"
|
|
#define ISSPACE(X) isspace((unsigned char)(X))
|
|
#define ISDIGIT(X) isdigit((unsigned char)(X))
|
|
|
|
|
|
#ifdef __unix__
|
|
# include <signal.h>
|
|
# include <unistd.h>
|
|
#endif
|
|
|
|
#include <stddef.h>
|
|
#if !defined(_MSC_VER)
|
|
# include <stdint.h>
|
|
#endif
|
|
|
|
#if defined(_MSC_VER)
|
|
typedef unsigned char uint8_t;
|
|
#endif
|
|
|
|
/*
|
|
** Files in the virtual file system.
|
|
*/
|
|
typedef struct VFile VFile;
|
|
struct VFile {
|
|
char *zFilename; /* Filename. NULL for delete-on-close. From malloc() */
|
|
int sz; /* Size of the file in bytes */
|
|
int nRef; /* Number of references to this file */
|
|
unsigned char *a; /* Content of the file. From malloc() */
|
|
};
|
|
typedef struct VHandle VHandle;
|
|
struct VHandle {
|
|
sqlite3_file base; /* Base class. Must be first */
|
|
VFile *pVFile; /* The underlying file */
|
|
};
|
|
|
|
/*
|
|
** The value of a database file template, or of an SQL script
|
|
*/
|
|
typedef struct Blob Blob;
|
|
struct Blob {
|
|
Blob *pNext; /* Next in a list */
|
|
int id; /* Id of this Blob */
|
|
int seq; /* Sequence number */
|
|
int sz; /* Size of this Blob in bytes */
|
|
unsigned char a[1]; /* Blob content. Extra space allocated as needed. */
|
|
};
|
|
|
|
/*
|
|
** Maximum number of files in the in-memory virtual filesystem.
|
|
*/
|
|
#define MX_FILE 10
|
|
|
|
/*
|
|
** Maximum allowed file size
|
|
*/
|
|
#define MX_FILE_SZ 10000000
|
|
|
|
/*
|
|
** All global variables are gathered into the "g" singleton.
|
|
*/
|
|
static struct GlobalVars {
|
|
const char *zArgv0; /* Name of program */
|
|
const char *zDbFile; /* Name of database file */
|
|
VFile aFile[MX_FILE]; /* The virtual filesystem */
|
|
int nDb; /* Number of template databases */
|
|
Blob *pFirstDb; /* Content of first template database */
|
|
int nSql; /* Number of SQL scripts */
|
|
Blob *pFirstSql; /* First SQL script */
|
|
unsigned int uRandom; /* Seed for the SQLite PRNG */
|
|
unsigned int nInvariant; /* Number of invariant checks run */
|
|
char zTestName[100]; /* Name of current test */
|
|
} g;
|
|
|
|
/*
|
|
** Include the external vt02.c and randomjson.c modules.
|
|
*/
|
|
extern int sqlite3_vt02_init(sqlite3*,char***,void*);
|
|
extern int sqlite3_randomjson_init(sqlite3*,char***,void*);
|
|
|
|
|
|
/*
|
|
** Print an error message and quit.
|
|
*/
|
|
static void fatalError(const char *zFormat, ...){
|
|
va_list ap;
|
|
fprintf(stderr, "%s", g.zArgv0);
|
|
if( g.zDbFile ) fprintf(stderr, " %s", g.zDbFile);
|
|
if( g.zTestName[0] ) fprintf(stderr, " (%s)", g.zTestName);
|
|
fprintf(stderr, ": ");
|
|
va_start(ap, zFormat);
|
|
vfprintf(stderr, zFormat, ap);
|
|
va_end(ap);
|
|
fprintf(stderr, "\n");
|
|
exit(1);
|
|
}
|
|
|
|
/*
|
|
** signal handler
|
|
*/
|
|
#ifdef __unix__
|
|
static void signalHandler(int signum){
|
|
const char *zSig;
|
|
if( signum==SIGABRT ){
|
|
zSig = "abort";
|
|
}else if( signum==SIGALRM ){
|
|
zSig = "timeout";
|
|
}else if( signum==SIGSEGV ){
|
|
zSig = "segfault";
|
|
}else{
|
|
zSig = "signal";
|
|
}
|
|
fatalError(zSig);
|
|
}
|
|
#endif
|
|
|
|
/*
|
|
** Set the an alarm to go off after N seconds. Disable the alarm
|
|
** if N==0
|
|
*/
|
|
static void setAlarm(int N){
|
|
#ifdef __unix__
|
|
alarm(N);
|
|
#else
|
|
(void)N;
|
|
#endif
|
|
}
|
|
|
|
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
|
|
/*
|
|
** This an SQL progress handler. After an SQL statement has run for
|
|
** many steps, we want to interrupt it. This guards against infinite
|
|
** loops from recursive common table expressions.
|
|
**
|
|
** *pVdbeLimitFlag is true if the --limit-vdbe command-line option is used.
|
|
** In that case, hitting the progress handler is a fatal error.
|
|
*/
|
|
static int progressHandler(void *pVdbeLimitFlag){
|
|
if( *(int*)pVdbeLimitFlag ) fatalError("too many VDBE cycles");
|
|
return 1;
|
|
}
|
|
#endif
|
|
|
|
/*
|
|
** Reallocate memory. Show an error and quit if unable.
|
|
*/
|
|
static void *safe_realloc(void *pOld, int szNew){
|
|
void *pNew = realloc(pOld, szNew<=0 ? 1 : szNew);
|
|
if( pNew==0 ) fatalError("unable to realloc for %d bytes", szNew);
|
|
return pNew;
|
|
}
|
|
|
|
/*
|
|
** Initialize the virtual file system.
|
|
*/
|
|
static void formatVfs(void){
|
|
int i;
|
|
for(i=0; i<MX_FILE; i++){
|
|
g.aFile[i].sz = -1;
|
|
g.aFile[i].zFilename = 0;
|
|
g.aFile[i].a = 0;
|
|
g.aFile[i].nRef = 0;
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
** Erase all information in the virtual file system.
|
|
*/
|
|
static void reformatVfs(void){
|
|
int i;
|
|
for(i=0; i<MX_FILE; i++){
|
|
if( g.aFile[i].sz<0 ) continue;
|
|
if( g.aFile[i].zFilename ){
|
|
free(g.aFile[i].zFilename);
|
|
g.aFile[i].zFilename = 0;
|
|
}
|
|
if( g.aFile[i].nRef>0 ){
|
|
fatalError("file %d still open. nRef=%d", i, g.aFile[i].nRef);
|
|
}
|
|
g.aFile[i].sz = -1;
|
|
free(g.aFile[i].a);
|
|
g.aFile[i].a = 0;
|
|
g.aFile[i].nRef = 0;
|
|
}
|
|
}
|
|
|
|
/*
|
|
** Find a VFile by name
|
|
*/
|
|
static VFile *findVFile(const char *zName){
|
|
int i;
|
|
if( zName==0 ) return 0;
|
|
for(i=0; i<MX_FILE; i++){
|
|
if( g.aFile[i].zFilename==0 ) continue;
|
|
if( strcmp(g.aFile[i].zFilename, zName)==0 ) return &g.aFile[i];
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
** Find a VFile by name. Create it if it does not already exist and
|
|
** initialize it to the size and content given.
|
|
**
|
|
** Return NULL only if the filesystem is full.
|
|
*/
|
|
static VFile *createVFile(const char *zName, int sz, unsigned char *pData){
|
|
VFile *pNew = findVFile(zName);
|
|
int i;
|
|
if( pNew ) return pNew;
|
|
for(i=0; i<MX_FILE && g.aFile[i].sz>=0; i++){}
|
|
if( i>=MX_FILE ) return 0;
|
|
pNew = &g.aFile[i];
|
|
if( zName ){
|
|
int nName = (int)strlen(zName)+1;
|
|
pNew->zFilename = safe_realloc(0, nName);
|
|
memcpy(pNew->zFilename, zName, nName);
|
|
}else{
|
|
pNew->zFilename = 0;
|
|
}
|
|
pNew->nRef = 0;
|
|
pNew->sz = sz;
|
|
pNew->a = safe_realloc(0, sz);
|
|
if( sz>0 ) memcpy(pNew->a, pData, sz);
|
|
return pNew;
|
|
}
|
|
|
|
/* Return true if the line is all zeros */
|
|
static int allZero(unsigned char *aLine){
|
|
int i;
|
|
for(i=0; i<16 && aLine[i]==0; i++){}
|
|
return i==16;
|
|
}
|
|
|
|
/*
|
|
** Render a database and query as text that can be input into
|
|
** the CLI.
|
|
*/
|
|
static void renderDbSqlForCLI(
|
|
FILE *out, /* Write to this file */
|
|
const char *zFile, /* Name of the database file */
|
|
unsigned char *aDb, /* Database content */
|
|
int nDb, /* Number of bytes in aDb[] */
|
|
unsigned char *zSql, /* SQL content */
|
|
int nSql /* Bytes of SQL */
|
|
){
|
|
fprintf(out, ".print ******* %s *******\n", zFile);
|
|
if( nDb>100 ){
|
|
int i, j; /* Loop counters */
|
|
int pgsz; /* Size of each page */
|
|
int lastPage = 0; /* Last page number shown */
|
|
int iPage; /* Current page number */
|
|
unsigned char *aLine; /* Single line to display */
|
|
unsigned char buf[16]; /* Fake line */
|
|
unsigned char bShow[256]; /* Characters ok to display */
|
|
|
|
memset(bShow, '.', sizeof(bShow));
|
|
for(i=' '; i<='~'; i++){
|
|
if( i!='{' && i!='}' && i!='"' && i!='\\' ) bShow[i] = i;
|
|
}
|
|
pgsz = (aDb[16]<<8) | aDb[17];
|
|
if( pgsz==0 ) pgsz = 65536;
|
|
if( pgsz<512 || (pgsz&(pgsz-1))!=0 ) pgsz = 4096;
|
|
fprintf(out,".open --hexdb\n");
|
|
fprintf(out,"| size %d pagesize %d filename %s\n",nDb,pgsz,zFile);
|
|
for(i=0; i<nDb; i += 16){
|
|
if( i+16>nDb ){
|
|
memset(buf, 0, sizeof(buf));
|
|
memcpy(buf, aDb+i, nDb-i);
|
|
aLine = buf;
|
|
}else{
|
|
aLine = aDb + i;
|
|
}
|
|
if( allZero(aLine) ) continue;
|
|
iPage = i/pgsz + 1;
|
|
if( lastPage!=iPage ){
|
|
fprintf(out,"| page %d offset %d\n", iPage, (iPage-1)*pgsz);
|
|
lastPage = iPage;
|
|
}
|
|
fprintf(out,"| %5d:", i-(iPage-1)*pgsz);
|
|
for(j=0; j<16; j++) fprintf(out," %02x", aLine[j]);
|
|
fprintf(out," ");
|
|
for(j=0; j<16; j++){
|
|
unsigned char c = (unsigned char)aLine[j];
|
|
fputc( bShow[c], stdout);
|
|
}
|
|
fputc('\n', stdout);
|
|
}
|
|
fprintf(out,"| end %s\n", zFile);
|
|
}else{
|
|
fprintf(out,".open :memory:\n");
|
|
}
|
|
fprintf(out,".testctrl prng_seed 1 db\n");
|
|
fprintf(out,".testctrl internal_functions\n");
|
|
fprintf(out,"%.*s", nSql, zSql);
|
|
if( nSql>0 && zSql[nSql-1]!='\n' ) fprintf(out, "\n");
|
|
}
|
|
|
|
/*
|
|
** Read the complete content of a file into memory. Add a 0x00 terminator
|
|
** and return a pointer to the result.
|
|
**
|
|
** The file content is held in memory obtained from sqlite_malloc64() which
|
|
** should be freed by the caller.
|
|
*/
|
|
static char *readFile(const char *zFilename, long *sz){
|
|
FILE *in;
|
|
long nIn;
|
|
unsigned char *pBuf;
|
|
|
|
*sz = 0;
|
|
if( zFilename==0 ) return 0;
|
|
in = fopen(zFilename, "rb");
|
|
if( in==0 ) return 0;
|
|
fseek(in, 0, SEEK_END);
|
|
*sz = nIn = ftell(in);
|
|
rewind(in);
|
|
pBuf = sqlite3_malloc64( nIn+1 );
|
|
if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
|
|
pBuf[nIn] = 0;
|
|
fclose(in);
|
|
return (char*)pBuf;
|
|
}
|
|
sqlite3_free(pBuf);
|
|
*sz = 0;
|
|
fclose(in);
|
|
return 0;
|
|
}
|
|
|
|
|
|
/*
|
|
** Implementation of the "readfile(X)" SQL function. The entire content
|
|
** of the file named X is read and returned as a BLOB. NULL is returned
|
|
** if the file does not exist or is unreadable.
|
|
*/
|
|
static void readfileFunc(
|
|
sqlite3_context *context,
|
|
int argc,
|
|
sqlite3_value **argv
|
|
){
|
|
long nIn;
|
|
void *pBuf;
|
|
const char *zName = (const char*)sqlite3_value_text(argv[0]);
|
|
|
|
if( zName==0 ) return;
|
|
pBuf = readFile(zName, &nIn);
|
|
if( pBuf ){
|
|
sqlite3_result_blob(context, pBuf, nIn, sqlite3_free);
|
|
}
|
|
}
|
|
|
|
/*
|
|
** Implementation of the "readtextfile(X)" SQL function. The text content
|
|
** of the file named X through the end of the file or to the first \000
|
|
** character, whichever comes first, is read and returned as TEXT. NULL
|
|
** is returned if the file does not exist or is unreadable.
|
|
*/
|
|
static void readtextfileFunc(
|
|
sqlite3_context *context,
|
|
int argc,
|
|
sqlite3_value **argv
|
|
){
|
|
const char *zName;
|
|
FILE *in;
|
|
long nIn;
|
|
char *pBuf;
|
|
|
|
zName = (const char*)sqlite3_value_text(argv[0]);
|
|
if( zName==0 ) return;
|
|
in = fopen(zName, "rb");
|
|
if( in==0 ) return;
|
|
fseek(in, 0, SEEK_END);
|
|
nIn = ftell(in);
|
|
rewind(in);
|
|
pBuf = sqlite3_malloc64( nIn+1 );
|
|
if( pBuf && 1==fread(pBuf, nIn, 1, in) ){
|
|
pBuf[nIn] = 0;
|
|
sqlite3_result_text(context, pBuf, -1, sqlite3_free);
|
|
}else{
|
|
sqlite3_free(pBuf);
|
|
}
|
|
fclose(in);
|
|
}
|
|
|
|
/*
|
|
** Implementation of the "writefile(X,Y)" SQL function. The argument Y
|
|
** is written into file X. The number of bytes written is returned. Or
|
|
** NULL is returned if something goes wrong, such as being unable to open
|
|
** file X for writing.
|
|
*/
|
|
static void writefileFunc(
|
|
sqlite3_context *context,
|
|
int argc,
|
|
sqlite3_value **argv
|
|
){
|
|
FILE *out;
|
|
const char *z;
|
|
sqlite3_int64 rc;
|
|
const char *zFile;
|
|
|
|
(void)argc;
|
|
zFile = (const char*)sqlite3_value_text(argv[0]);
|
|
if( zFile==0 ) return;
|
|
out = fopen(zFile, "wb");
|
|
if( out==0 ) return;
|
|
z = (const char*)sqlite3_value_blob(argv[1]);
|
|
if( z==0 ){
|
|
rc = 0;
|
|
}else{
|
|
rc = fwrite(z, 1, sqlite3_value_bytes(argv[1]), out);
|
|
}
|
|
fclose(out);
|
|
sqlite3_result_int64(context, rc);
|
|
}
|
|
|
|
|
|
/*
|
|
** Load a list of Blob objects from the database
|
|
*/
|
|
static void blobListLoadFromDb(
|
|
sqlite3 *db, /* Read from this database */
|
|
const char *zSql, /* Query used to extract the blobs */
|
|
int onlyId, /* Only load where id is this value */
|
|
int *pN, /* OUT: Write number of blobs loaded here */
|
|
Blob **ppList /* OUT: Write the head of the blob list here */
|
|
){
|
|
Blob head;
|
|
Blob *p;
|
|
sqlite3_stmt *pStmt;
|
|
int n = 0;
|
|
int rc;
|
|
char *z2;
|
|
|
|
if( onlyId>0 ){
|
|
z2 = sqlite3_mprintf("%s WHERE rowid=%d", zSql, onlyId);
|
|
}else{
|
|
z2 = sqlite3_mprintf("%s", zSql);
|
|
}
|
|
rc = sqlite3_prepare_v2(db, z2, -1, &pStmt, 0);
|
|
sqlite3_free(z2);
|
|
if( rc ) fatalError("%s", sqlite3_errmsg(db));
|
|
head.pNext = 0;
|
|
p = &head;
|
|
while( SQLITE_ROW==sqlite3_step(pStmt) ){
|
|
int sz = sqlite3_column_bytes(pStmt, 1);
|
|
Blob *pNew = safe_realloc(0, sizeof(*pNew)+sz );
|
|
pNew->id = sqlite3_column_int(pStmt, 0);
|
|
pNew->sz = sz;
|
|
pNew->seq = n++;
|
|
pNew->pNext = 0;
|
|
memcpy(pNew->a, sqlite3_column_blob(pStmt,1), sz);
|
|
pNew->a[sz] = 0;
|
|
p->pNext = pNew;
|
|
p = pNew;
|
|
}
|
|
sqlite3_finalize(pStmt);
|
|
*pN = n;
|
|
*ppList = head.pNext;
|
|
}
|
|
|
|
/*
|
|
** Free a list of Blob objects
|
|
*/
|
|
static void blobListFree(Blob *p){
|
|
Blob *pNext;
|
|
while( p ){
|
|
pNext = p->pNext;
|
|
free(p);
|
|
p = pNext;
|
|
}
|
|
}
|
|
|
|
/* Return the current wall-clock time
|
|
**
|
|
** The number of milliseconds since the julian epoch.
|
|
** 1907-01-01 00:00:00 -> 210866716800000
|
|
** 2021-01-01 00:00:00 -> 212476176000000
|
|
*/
|
|
static sqlite3_int64 timeOfDay(void){
|
|
static sqlite3_vfs *clockVfs = 0;
|
|
sqlite3_int64 t;
|
|
if( clockVfs==0 ){
|
|
clockVfs = sqlite3_vfs_find(0);
|
|
if( clockVfs==0 ) return 0;
|
|
}
|
|
if( clockVfs->iVersion>=1 && clockVfs->xCurrentTimeInt64!=0 ){
|
|
clockVfs->xCurrentTimeInt64(clockVfs, &t);
|
|
}else{
|
|
double r;
|
|
clockVfs->xCurrentTime(clockVfs, &r);
|
|
t = (sqlite3_int64)(r*86400000.0);
|
|
}
|
|
return t;
|
|
}
|
|
|
|
/***************************************************************************
|
|
** Code to process combined database+SQL scripts generated by the
|
|
** dbsqlfuzz fuzzer.
|
|
*/
|
|
|
|
/* An instance of the following object is passed by pointer as the
|
|
** client data to various callbacks.
|
|
*/
|
|
typedef struct FuzzCtx {
|
|
sqlite3 *db; /* The database connection */
|
|
sqlite3_int64 iCutoffTime; /* Stop processing at this time. */
|
|
sqlite3_int64 iLastCb; /* Time recorded for previous progress callback */
|
|
sqlite3_int64 mxInterval; /* Longest interval between two progress calls */
|
|
unsigned nCb; /* Number of progress callbacks */
|
|
unsigned mxCb; /* Maximum number of progress callbacks allowed */
|
|
unsigned execCnt; /* Number of calls to the sqlite3_exec callback */
|
|
int timeoutHit; /* True when reaching a timeout */
|
|
} FuzzCtx;
|
|
|
|
/* Verbosity level for the dbsqlfuzz test runner */
|
|
static int eVerbosity = 0;
|
|
|
|
/* True to activate PRAGMA vdbe_debug=on */
|
|
static int bVdbeDebug = 0;
|
|
|
|
/* Timeout for each fuzzing attempt, in milliseconds */
|
|
static int giTimeout = 10000; /* Defaults to 10 seconds */
|
|
|
|
/* Maximum number of progress handler callbacks */
|
|
static unsigned int mxProgressCb = 2000;
|
|
|
|
/* Maximum string length in SQLite */
|
|
static int lengthLimit = 1000000;
|
|
|
|
/* Maximum expression depth */
|
|
static int depthLimit = 500;
|
|
|
|
/* Limit on the amount of heap memory that can be used */
|
|
static sqlite3_int64 heapLimit = 100000000;
|
|
|
|
/* Maximum byte-code program length in SQLite */
|
|
static int vdbeOpLimit = 25000;
|
|
|
|
/* Maximum size of the in-memory database */
|
|
static sqlite3_int64 maxDbSize = 104857600;
|
|
/* OOM simulation parameters */
|
|
static unsigned int oomCounter = 0; /* Simulate OOM when equals 1 */
|
|
static unsigned int oomRepeat = 0; /* Number of OOMs in a row */
|
|
static void*(*defaultMalloc)(int) = 0; /* The low-level malloc routine */
|
|
|
|
/* Enable recovery */
|
|
static int bNoRecover = 0;
|
|
|
|
/* This routine is called when a simulated OOM occurs. It is broken
|
|
** out as a separate routine to make it easy to set a breakpoint on
|
|
** the OOM
|
|
*/
|
|
void oomFault(void){
|
|
if( eVerbosity ){
|
|
printf("Simulated OOM fault\n");
|
|
}
|
|
if( oomRepeat>0 ){
|
|
oomRepeat--;
|
|
}else{
|
|
oomCounter--;
|
|
}
|
|
}
|
|
|
|
/* This routine is a replacement malloc() that is used to simulate
|
|
** Out-Of-Memory (OOM) errors for testing purposes.
|
|
*/
|
|
static void *oomMalloc(int nByte){
|
|
if( oomCounter ){
|
|
if( oomCounter==1 ){
|
|
oomFault();
|
|
return 0;
|
|
}else{
|
|
oomCounter--;
|
|
}
|
|
}
|
|
return defaultMalloc(nByte);
|
|
}
|
|
|
|
/* Register the OOM simulator. This must occur before any memory
|
|
** allocations */
|
|
static void registerOomSimulator(void){
|
|
sqlite3_mem_methods mem;
|
|
sqlite3_shutdown();
|
|
sqlite3_config(SQLITE_CONFIG_GETMALLOC, &mem);
|
|
defaultMalloc = mem.xMalloc;
|
|
mem.xMalloc = oomMalloc;
|
|
sqlite3_config(SQLITE_CONFIG_MALLOC, &mem);
|
|
}
|
|
|
|
/* Turn off any pending OOM simulation */
|
|
static void disableOom(void){
|
|
oomCounter = 0;
|
|
oomRepeat = 0;
|
|
}
|
|
|
|
/*
|
|
** Translate a single byte of Hex into an integer.
|
|
** This routine only works if h really is a valid hexadecimal
|
|
** character: 0..9a..fA..F
|
|
*/
|
|
static unsigned char hexToInt(unsigned int h){
|
|
#ifdef SQLITE_EBCDIC
|
|
h += 9*(1&~(h>>4)); /* EBCDIC */
|
|
#else
|
|
h += 9*(1&(h>>6)); /* ASCII */
|
|
#endif
|
|
return h & 0xf;
|
|
}
|
|
|
|
/*
|
|
** The first character of buffer zIn[0..nIn-1] is a '['. This routine
|
|
** checked to see if the buffer holds "[NNNN]" or "[+NNNN]" and if it
|
|
** does it makes corresponding changes to the *pK value and *pI value
|
|
** and returns true. If the input buffer does not match the patterns,
|
|
** no changes are made to either *pK or *pI and this routine returns false.
|
|
*/
|
|
static int isOffset(
|
|
const unsigned char *zIn, /* Text input */
|
|
int nIn, /* Bytes of input */
|
|
unsigned int *pK, /* half-byte cursor to adjust */
|
|
unsigned int *pI /* Input index to adjust */
|
|
){
|
|
int i;
|
|
unsigned int k = 0;
|
|
unsigned char c;
|
|
for(i=1; i<nIn && (c = zIn[i])!=']'; i++){
|
|
if( !isxdigit(c) ) return 0;
|
|
k = k*16 + hexToInt(c);
|
|
}
|
|
if( i==nIn ) return 0;
|
|
*pK = 2*k;
|
|
*pI += i;
|
|
return 1;
|
|
}
|
|
|
|
/*
|
|
** Decode the text starting at zIn into a binary database file.
|
|
** The maximum length of zIn is nIn bytes. Store the binary database
|
|
** file in space obtained from sqlite3_malloc().
|
|
**
|
|
** Return the number of bytes of zIn consumed. Or return -1 if there
|
|
** is an error. One potential error is that the recipe specifies a
|
|
** database file larger than MX_FILE_SZ bytes.
|
|
**
|
|
** Abort on an OOM.
|
|
*/
|
|
static int decodeDatabase(
|
|
const unsigned char *zIn, /* Input text to be decoded */
|
|
int nIn, /* Bytes of input text */
|
|
unsigned char **paDecode, /* OUT: decoded database file */
|
|
int *pnDecode /* OUT: Size of decoded database */
|
|
){
|
|
unsigned char *a, *aNew; /* Database under construction */
|
|
int mx = 0; /* Current size of the database */
|
|
sqlite3_uint64 nAlloc = 4096; /* Space allocated in a[] */
|
|
unsigned int i; /* Next byte of zIn[] to read */
|
|
unsigned int j; /* Temporary integer */
|
|
unsigned int k; /* half-byte cursor index for output */
|
|
unsigned int n; /* Number of bytes of input */
|
|
unsigned char b = 0;
|
|
if( nIn<4 ) return -1;
|
|
n = (unsigned int)nIn;
|
|
a = sqlite3_malloc64( nAlloc );
|
|
if( a==0 ){
|
|
fprintf(stderr, "Out of memory!\n");
|
|
exit(1);
|
|
}
|
|
memset(a, 0, (size_t)nAlloc);
|
|
for(i=k=0; i<n; i++){
|
|
unsigned char c = (unsigned char)zIn[i];
|
|
if( isxdigit(c) ){
|
|
k++;
|
|
if( k & 1 ){
|
|
b = hexToInt(c)*16;
|
|
}else{
|
|
b += hexToInt(c);
|
|
j = k/2 - 1;
|
|
if( j>=nAlloc ){
|
|
sqlite3_uint64 newSize;
|
|
if( nAlloc==MX_FILE_SZ || j>=MX_FILE_SZ ){
|
|
if( eVerbosity ){
|
|
fprintf(stderr, "Input database too big: max %d bytes\n",
|
|
MX_FILE_SZ);
|
|
}
|
|
sqlite3_free(a);
|
|
return -1;
|
|
}
|
|
newSize = nAlloc*2;
|
|
if( newSize<=j ){
|
|
newSize = (j+4096)&~4095;
|
|
}
|
|
if( newSize>MX_FILE_SZ ){
|
|
if( j>=MX_FILE_SZ ){
|
|
sqlite3_free(a);
|
|
return -1;
|
|
}
|
|
newSize = MX_FILE_SZ;
|
|
}
|
|
aNew = sqlite3_realloc64( a, newSize );
|
|
if( aNew==0 ){
|
|
sqlite3_free(a);
|
|
return -1;
|
|
}
|
|
a = aNew;
|
|
assert( newSize > nAlloc );
|
|
memset(a+nAlloc, 0, (size_t)(newSize - nAlloc));
|
|
nAlloc = newSize;
|
|
}
|
|
if( j>=(unsigned)mx ){
|
|
mx = (j + 4095)&~4095;
|
|
if( mx>MX_FILE_SZ ) mx = MX_FILE_SZ;
|
|
}
|
|
assert( j<nAlloc );
|
|
a[j] = b;
|
|
}
|
|
}else if( zIn[i]=='[' && i<n-3 && isOffset(zIn+i, nIn-i, &k, &i) ){
|
|
continue;
|
|
}else if( zIn[i]=='\n' && i<n-4 && memcmp(zIn+i,"\n--\n",4)==0 ){
|
|
i += 4;
|
|
break;
|
|
}
|
|
}
|
|
*pnDecode = mx;
|
|
*paDecode = a;
|
|
return i;
|
|
}
|
|
|
|
/*
|
|
** Progress handler callback.
|
|
**
|
|
** The argument is the cutoff-time after which all processing should
|
|
** stop. So return non-zero if the cut-off time is exceeded.
|
|
*/
|
|
static int progress_handler(void *pClientData) {
|
|
FuzzCtx *p = (FuzzCtx*)pClientData;
|
|
sqlite3_int64 iNow = timeOfDay();
|
|
int rc = iNow>=p->iCutoffTime;
|
|
sqlite3_int64 iDiff = iNow - p->iLastCb;
|
|
/* printf("time-remaining: %lld\n", p->iCutoffTime - iNow); */
|
|
if( iDiff > p->mxInterval ) p->mxInterval = iDiff;
|
|
p->nCb++;
|
|
if( rc==0 && p->mxCb>0 && p->mxCb<=p->nCb ) rc = 1;
|
|
if( rc && !p->timeoutHit && eVerbosity>=2 ){
|
|
printf("Timeout on progress callback %d\n", p->nCb);
|
|
fflush(stdout);
|
|
p->timeoutHit = 1;
|
|
}
|
|
return rc;
|
|
}
|
|
|
|
/*
|
|
** Flag bits set by block_troublesome_sql()
|
|
*/
|
|
#define BTS_SELECT 0x000001
|
|
#define BTS_NONSELECT 0x000002
|
|
#define BTS_BADFUNC 0x000004
|
|
#define BTS_BADPRAGMA 0x000008 /* Sticky for rest of the script */
|
|
|
|
/*
|
|
** Disallow debugging pragmas such as "PRAGMA vdbe_debug" and
|
|
** "PRAGMA parser_trace" since they can dramatically increase the
|
|
** amount of output without actually testing anything useful.
|
|
**
|
|
** Also block ATTACH if attaching a file from the filesystem.
|
|
*/
|
|
static int block_troublesome_sql(
|
|
void *pClientData,
|
|
int eCode,
|
|
const char *zArg1,
|
|
const char *zArg2,
|
|
const char *zArg3,
|
|
const char *zArg4
|
|
){
|
|
unsigned int *pBtsFlags = (unsigned int*)pClientData;
|
|
|
|
(void)zArg3;
|
|
(void)zArg4;
|
|
switch( eCode ){
|
|
case SQLITE_PRAGMA: {
|
|
if( sqlite3_stricmp("busy_timeout",zArg1)==0
|
|
&& (zArg2==0 || strtoll(zArg2,0,0)>100 || strtoll(zArg2,0,10)>100)
|
|
){
|
|
return SQLITE_DENY;
|
|
}else if( sqlite3_stricmp("hard_heap_limit", zArg1)==0
|
|
|| sqlite3_stricmp("reverse_unordered_selects", zArg1)==0
|
|
){
|
|
/* BTS_BADPRAGMA is sticky. A hard_heap_limit or
|
|
** revert_unordered_selects should inhibit all future attempts
|
|
** at verifying query invariants */
|
|
*pBtsFlags |= BTS_BADPRAGMA;
|
|
}else if( eVerbosity==0 ){
|
|
if( sqlite3_strnicmp("vdbe_", zArg1, 5)==0
|
|
|| sqlite3_stricmp("parser_trace", zArg1)==0
|
|
|| sqlite3_stricmp("temp_store_directory", zArg1)==0
|
|
){
|
|
return SQLITE_DENY;
|
|
}
|
|
}else if( sqlite3_stricmp("oom",zArg1)==0
|
|
&& zArg2!=0 && zArg2[0]!=0 ){
|
|
oomCounter = atoi(zArg2);
|
|
}
|
|
*pBtsFlags |= BTS_NONSELECT;
|
|
break;
|
|
}
|
|
case SQLITE_ATTACH: {
|
|
/* Deny the ATTACH if it is attaching anything other than an in-memory
|
|
** database. */
|
|
*pBtsFlags |= BTS_NONSELECT;
|
|
if( zArg1==0 ) return SQLITE_DENY;
|
|
if( strcmp(zArg1,":memory:")==0 ) return SQLITE_OK;
|
|
if( sqlite3_strglob("file:*[?]vfs=memdb", zArg1)==0
|
|
&& sqlite3_strglob("file:*[^/a-zA-Z0-9_.]*[?]vfs=memdb", zArg1)!=0
|
|
){
|
|
return SQLITE_OK;
|
|
}
|
|
return SQLITE_DENY;
|
|
}
|
|
case SQLITE_SELECT: {
|
|
*pBtsFlags |= BTS_SELECT;
|
|
break;
|
|
}
|
|
case SQLITE_FUNCTION: {
|
|
static const char *azBadFuncs[] = {
|
|
"avg",
|
|
"count",
|
|
"cume_dist",
|
|
"current_date",
|
|
"current_time",
|
|
"current_timestamp",
|
|
"date",
|
|
"datetime",
|
|
"decimal_sum",
|
|
"dense_rank",
|
|
"first_value",
|
|
"geopoly_group_bbox",
|
|
"group_concat",
|
|
"implies_nonnull_row",
|
|
"json_group_array",
|
|
"json_group_object",
|
|
"julianday",
|
|
"lag",
|
|
"last_value",
|
|
"lead",
|
|
"max",
|
|
"min",
|
|
"nth_value",
|
|
"ntile",
|
|
"percent_rank",
|
|
"random",
|
|
"randomblob",
|
|
"rank",
|
|
"row_number",
|
|
"sqlite_offset",
|
|
"strftime",
|
|
"sum",
|
|
"time",
|
|
"total",
|
|
"unixepoch",
|
|
};
|
|
int first, last;
|
|
first = 0;
|
|
last = sizeof(azBadFuncs)/sizeof(azBadFuncs[0]) - 1;
|
|
do{
|
|
int mid = (first+last)/2;
|
|
int c = sqlite3_stricmp(azBadFuncs[mid], zArg2);
|
|
if( c<0 ){
|
|
first = mid+1;
|
|
}else if( c>0 ){
|
|
last = mid-1;
|
|
}else{
|
|
*pBtsFlags |= BTS_BADFUNC;
|
|
break;
|
|
}
|
|
}while( first<=last );
|
|
break;
|
|
}
|
|
case SQLITE_READ: {
|
|
/* Benign */
|
|
break;
|
|
}
|
|
default: {
|
|
*pBtsFlags |= BTS_NONSELECT;
|
|
}
|
|
}
|
|
return SQLITE_OK;
|
|
}
|
|
|
|
/* Implementation found in fuzzinvariant.c */
|
|
extern int fuzz_invariant(
|
|
sqlite3 *db, /* The database connection */
|
|
sqlite3_stmt *pStmt, /* Test statement stopped on an SQLITE_ROW */
|
|
int iCnt, /* Invariant sequence number, starting at 0 */
|
|
int iRow, /* The row number for pStmt */
|
|
int nRow, /* Total number of output rows */
|
|
int *pbCorrupt, /* IN/OUT: Flag indicating a corrupt database file */
|
|
int eVerbosity /* How much debugging output */
|
|
);
|
|
|
|
/* Implementation of sqlite_dbdata and sqlite_dbptr */
|
|
extern int sqlite3_dbdata_init(sqlite3*,const char**,void*);
|
|
|
|
|
|
/*
|
|
** This function is used as a callback by the recover extension. Simply
|
|
** print the supplied SQL statement to stdout.
|
|
*/
|
|
static int recoverSqlCb(void *pCtx, const char *zSql){
|
|
if( eVerbosity>=2 ){
|
|
printf("%s\n", zSql);
|
|
}
|
|
return SQLITE_OK;
|
|
}
|
|
|
|
/*
|
|
** This function is called to recover data from the database.
|
|
*/
|
|
static int recoverDatabase(sqlite3 *db){
|
|
int rc; /* Return code from this routine */
|
|
const char *zRecoveryDb = ""; /* Name of "recovery" database */
|
|
const char *zLAF = "lost_and_found"; /* Name of "lost_and_found" table */
|
|
int bFreelist = 1; /* True to scan the freelist */
|
|
int bRowids = 1; /* True to restore ROWID values */
|
|
sqlite3_recover *p = 0; /* The recovery object */
|
|
|
|
p = sqlite3_recover_init_sql(db, "main", recoverSqlCb, 0);
|
|
sqlite3_recover_config(p, 789, (void*)zRecoveryDb);
|
|
sqlite3_recover_config(p, SQLITE_RECOVER_LOST_AND_FOUND, (void*)zLAF);
|
|
sqlite3_recover_config(p, SQLITE_RECOVER_ROWIDS, (void*)&bRowids);
|
|
sqlite3_recover_config(p, SQLITE_RECOVER_FREELIST_CORRUPT,(void*)&bFreelist);
|
|
sqlite3_recover_run(p);
|
|
if( sqlite3_recover_errcode(p)!=SQLITE_OK ){
|
|
const char *zErr = sqlite3_recover_errmsg(p);
|
|
int errCode = sqlite3_recover_errcode(p);
|
|
if( eVerbosity>0 ){
|
|
printf("recovery error: %s (%d)\n", zErr, errCode);
|
|
}
|
|
}
|
|
rc = sqlite3_recover_finish(p);
|
|
if( eVerbosity>0 && rc ){
|
|
printf("recovery returns error code %d\n", rc);
|
|
}
|
|
return rc;
|
|
}
|
|
|
|
/*
|
|
** Run the SQL text
|
|
*/
|
|
static int runDbSql(sqlite3 *db, const char *zSql, unsigned int *pBtsFlags){
|
|
int rc;
|
|
sqlite3_stmt *pStmt;
|
|
int bCorrupt = 0;
|
|
while( isspace(zSql[0]&0x7f) ) zSql++;
|
|
if( zSql[0]==0 ) return SQLITE_OK;
|
|
if( eVerbosity>=4 ){
|
|
printf("RUNNING-SQL: [%s]\n", zSql);
|
|
fflush(stdout);
|
|
}
|
|
(*pBtsFlags) &= BTS_BADPRAGMA;
|
|
rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
|
|
if( rc==SQLITE_OK ){
|
|
int nRow = 0;
|
|
while( (rc = sqlite3_step(pStmt))==SQLITE_ROW ){
|
|
nRow++;
|
|
if( eVerbosity>=4 ){
|
|
int j;
|
|
for(j=0; j<sqlite3_column_count(pStmt); j++){
|
|
if( j ) printf(",");
|
|
switch( sqlite3_column_type(pStmt, j) ){
|
|
case SQLITE_NULL: {
|
|
printf("NULL");
|
|
break;
|
|
}
|
|
case SQLITE_INTEGER:
|
|
case SQLITE_FLOAT: {
|
|
printf("%s", sqlite3_column_text(pStmt, j));
|
|
break;
|
|
}
|
|
case SQLITE_BLOB: {
|
|
int n = sqlite3_column_bytes(pStmt, j);
|
|
int i;
|
|
const unsigned char *a;
|
|
a = (const unsigned char*)sqlite3_column_blob(pStmt, j);
|
|
printf("x'");
|
|
for(i=0; i<n; i++){
|
|
printf("%02x", a[i]);
|
|
}
|
|
printf("'");
|
|
break;
|
|
}
|
|
case SQLITE_TEXT: {
|
|
int n = sqlite3_column_bytes(pStmt, j);
|
|
int i;
|
|
const unsigned char *a;
|
|
a = (const unsigned char*)sqlite3_column_blob(pStmt, j);
|
|
printf("'");
|
|
for(i=0; i<n; i++){
|
|
if( a[i]=='\'' ){
|
|
printf("''");
|
|
}else{
|
|
putchar(a[i]);
|
|
}
|
|
}
|
|
printf("'");
|
|
break;
|
|
}
|
|
} /* End switch() */
|
|
} /* End for() */
|
|
printf("\n");
|
|
fflush(stdout);
|
|
} /* End if( eVerbosity>=5 ) */
|
|
} /* End while( SQLITE_ROW */
|
|
if( rc==SQLITE_DONE ){
|
|
if( (*pBtsFlags)==BTS_SELECT
|
|
&& !sqlite3_stmt_isexplain(pStmt)
|
|
&& nRow>0
|
|
){
|
|
int iRow = 0;
|
|
sqlite3_reset(pStmt);
|
|
while( sqlite3_step(pStmt)==SQLITE_ROW ){
|
|
int iCnt = 0;
|
|
iRow++;
|
|
for(iCnt=0; iCnt<99999; iCnt++){
|
|
rc = fuzz_invariant(db, pStmt, iCnt, iRow, nRow,
|
|
&bCorrupt, eVerbosity);
|
|
if( rc==SQLITE_DONE ) break;
|
|
if( rc!=SQLITE_ERROR ) g.nInvariant++;
|
|
if( eVerbosity>0 ){
|
|
if( rc==SQLITE_OK ){
|
|
printf("invariant-check: ok\n");
|
|
}else if( rc==SQLITE_CORRUPT ){
|
|
printf("invariant-check: failed due to database corruption\n");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}else if( eVerbosity>=4 ){
|
|
printf("SQL-ERROR: (%d) %s\n", rc, sqlite3_errmsg(db));
|
|
fflush(stdout);
|
|
}
|
|
}else if( eVerbosity>=4 ){
|
|
printf("SQL-ERROR (%d): %s\n", rc, sqlite3_errmsg(db));
|
|
fflush(stdout);
|
|
} /* End if( SQLITE_OK ) */
|
|
return sqlite3_finalize(pStmt);
|
|
}
|
|
|
|
/* Mappings into dbconfig settings for bits taken from bytes 72..75 of
|
|
** the input database.
|
|
**
|
|
** This should be the same as in dbsqlfuzz.c. Make sure those codes stay
|
|
** in sync.
|
|
*/
|
|
static const struct {
|
|
unsigned int mask;
|
|
int iSetting;
|
|
char *zName;
|
|
} aDbConfigSettings[] = {
|
|
{ 0x0001, SQLITE_DBCONFIG_ENABLE_FKEY, "enable_fkey" },
|
|
{ 0x0002, SQLITE_DBCONFIG_ENABLE_TRIGGER, "enable_trigger" },
|
|
{ 0x0004, SQLITE_DBCONFIG_ENABLE_VIEW, "enable_view" },
|
|
{ 0x0008, SQLITE_DBCONFIG_ENABLE_QPSG, "enable_qpsg" },
|
|
{ 0x0010, SQLITE_DBCONFIG_TRIGGER_EQP, "trigger_eqp" },
|
|
{ 0x0020, SQLITE_DBCONFIG_DEFENSIVE, "defensive" },
|
|
{ 0x0040, SQLITE_DBCONFIG_WRITABLE_SCHEMA, "writable_schema" },
|
|
{ 0x0080, SQLITE_DBCONFIG_LEGACY_ALTER_TABLE, "legacy_alter_table" },
|
|
{ 0x0100, SQLITE_DBCONFIG_STMT_SCANSTATUS, "stmt_scanstatus" },
|
|
{ 0x0200, SQLITE_DBCONFIG_REVERSE_SCANORDER, "reverse_scanorder" },
|
|
#ifdef SQLITE_DBCONFIG_STRICT_AGGREGATE
|
|
{ 0x0400, SQLITE_DBCONFIG_STRICT_AGGREGATE, "strict_aggregate" },
|
|
#endif
|
|
{ 0x0800, SQLITE_DBCONFIG_DQS_DML, "dqs_dml" },
|
|
{ 0x1000, SQLITE_DBCONFIG_DQS_DDL, "dqs_ddl" },
|
|
{ 0x2000, SQLITE_DBCONFIG_TRUSTED_SCHEMA, "trusted_schema" },
|
|
};
|
|
|
|
/* Toggle a dbconfig setting
|
|
*/
|
|
static void toggleDbConfig(sqlite3 *db, int iSetting){
|
|
int v = 0;
|
|
sqlite3_db_config(db, iSetting, -1, &v);
|
|
v = !v;
|
|
sqlite3_db_config(db, iSetting, v, 0);
|
|
}
|
|
|
|
/* Invoke this routine to run a single test case */
|
|
int runCombinedDbSqlInput(
|
|
const uint8_t *aData, /* Combined DB+SQL content */
|
|
size_t nByte, /* Size of aData in bytes */
|
|
int iTimeout, /* Use this timeout */
|
|
int bScript, /* If true, just render CLI output */
|
|
int iSqlId /* SQL identifier */
|
|
){
|
|
int rc; /* SQLite API return value */
|
|
int iSql; /* Index in aData[] of start of SQL */
|
|
unsigned char *aDb = 0; /* Decoded database content */
|
|
int nDb = 0; /* Size of the decoded database */
|
|
int i; /* Loop counter */
|
|
int j; /* Start of current SQL statement */
|
|
char *zSql = 0; /* SQL text to run */
|
|
int nSql; /* Bytes of SQL text */
|
|
FuzzCtx cx; /* Fuzzing context */
|
|
unsigned int btsFlags = 0; /* Parsing flags */
|
|
unsigned int dbFlags = 0; /* Flag values from db offset 72..75 */
|
|
unsigned int dbOpt = 0; /* Flag values from db offset 76..79 */
|
|
|
|
|
|
if( nByte<10 ) return 0;
|
|
if( sqlite3_initialize() ) return 0;
|
|
if( sqlite3_memory_used()!=0 ){
|
|
int nAlloc = 0;
|
|
int nNotUsed = 0;
|
|
sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &nAlloc, &nNotUsed, 0);
|
|
fprintf(stderr,"memory leak prior to test start:"
|
|
" %lld bytes in %d allocations\n",
|
|
sqlite3_memory_used(), nAlloc);
|
|
exit(1);
|
|
}
|
|
memset(&cx, 0, sizeof(cx));
|
|
iSql = decodeDatabase((unsigned char*)aData, (int)nByte, &aDb, &nDb);
|
|
if( iSql<0 ) return 0;
|
|
if( nDb>=75 ){
|
|
dbFlags = ((unsigned int)aDb[72]<<24) + ((unsigned int)aDb[73]<<16) +
|
|
((unsigned int)aDb[74]<<8) + (unsigned int)aDb[75];
|
|
}
|
|
if( nDb>=79 ){
|
|
dbOpt = ((unsigned int)aDb[76]<<24) + ((unsigned int)aDb[77]<<16) +
|
|
((unsigned int)aDb[78]<<8) + (unsigned int)aDb[79];
|
|
}
|
|
nSql = (int)(nByte - iSql);
|
|
if( bScript ){
|
|
char zName[100];
|
|
sqlite3_snprintf(sizeof(zName),zName,"dbsql%06d.db",iSqlId);
|
|
renderDbSqlForCLI(stdout, zName, aDb, nDb,
|
|
(unsigned char*)(aData+iSql), nSql);
|
|
sqlite3_free(aDb);
|
|
return 0;
|
|
}
|
|
if( eVerbosity>=3 ){
|
|
printf(
|
|
"****** %d-byte input, %d-byte database, %d-byte script "
|
|
"******\n", (int)nByte, nDb, nSql);
|
|
fflush(stdout);
|
|
}
|
|
rc = sqlite3_open(0, &cx.db);
|
|
if( rc ){
|
|
sqlite3_free(aDb);
|
|
return 1;
|
|
}
|
|
sqlite3_test_control(SQLITE_TESTCTRL_OPTIMIZATIONS, cx.db, dbOpt);
|
|
for(i=0; i<sizeof(aDbConfigSettings)/sizeof(aDbConfigSettings[0]); i++){
|
|
if( dbFlags & aDbConfigSettings[i].mask ){
|
|
toggleDbConfig(cx.db, aDbConfigSettings[i].iSetting);
|
|
}
|
|
}
|
|
if( bVdbeDebug ){
|
|
sqlite3_exec(cx.db, "PRAGMA vdbe_debug=ON", 0, 0, 0);
|
|
}
|
|
|
|
/* Invoke the progress handler frequently to check to see if we
|
|
** are taking too long. The progress handler will return true
|
|
** (which will block further processing) if more than giTimeout seconds have
|
|
** elapsed since the start of the test.
|
|
*/
|
|
cx.iLastCb = timeOfDay();
|
|
cx.iCutoffTime = cx.iLastCb + (iTimeout<giTimeout ? iTimeout : giTimeout);
|
|
cx.mxCb = mxProgressCb;
|
|
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
|
|
sqlite3_progress_handler(cx.db, 10, progress_handler, (void*)&cx);
|
|
#endif
|
|
|
|
/* Set a limit on the maximum size of a prepared statement, and the
|
|
** maximum length of a string or blob */
|
|
if( vdbeOpLimit>0 ){
|
|
sqlite3_limit(cx.db, SQLITE_LIMIT_VDBE_OP, vdbeOpLimit);
|
|
}
|
|
if( lengthLimit>0 ){
|
|
sqlite3_limit(cx.db, SQLITE_LIMIT_LENGTH, lengthLimit);
|
|
}
|
|
if( depthLimit>0 ){
|
|
sqlite3_limit(cx.db, SQLITE_LIMIT_EXPR_DEPTH, depthLimit);
|
|
}
|
|
sqlite3_limit(cx.db, SQLITE_LIMIT_LIKE_PATTERN_LENGTH, 100);
|
|
sqlite3_hard_heap_limit64(heapLimit);
|
|
rc = 1;
|
|
sqlite3_test_control(SQLITE_TESTCTRL_JSON_SELFCHECK, &rc);
|
|
|
|
if( nDb>=20 && aDb[18]==2 && aDb[19]==2 ){
|
|
aDb[18] = aDb[19] = 1;
|
|
}
|
|
rc = sqlite3_deserialize(cx.db, "main", aDb, nDb, nDb,
|
|
SQLITE_DESERIALIZE_RESIZEABLE |
|
|
SQLITE_DESERIALIZE_FREEONCLOSE);
|
|
if( rc ){
|
|
fprintf(stderr, "sqlite3_deserialize() failed with %d\n", rc);
|
|
goto testrun_finished;
|
|
}
|
|
if( maxDbSize>0 ){
|
|
sqlite3_int64 x = maxDbSize;
|
|
sqlite3_file_control(cx.db, "main", SQLITE_FCNTL_SIZE_LIMIT, &x);
|
|
}
|
|
|
|
/* For high debugging levels, turn on debug mode */
|
|
if( eVerbosity>=5 ){
|
|
sqlite3_exec(cx.db, "PRAGMA vdbe_debug=ON;", 0, 0, 0);
|
|
}
|
|
|
|
/* Block debug pragmas and ATTACH/DETACH. But wait until after
|
|
** deserialize to do this because deserialize depends on ATTACH */
|
|
sqlite3_set_authorizer(cx.db, block_troublesome_sql, &btsFlags);
|
|
|
|
/* Add the vt02 virtual table */
|
|
sqlite3_vt02_init(cx.db, 0, 0);
|
|
|
|
/* Add the random_json() and random_json5() functions */
|
|
sqlite3_randomjson_init(cx.db, 0, 0);
|
|
|
|
/* Add support for sqlite_dbdata and sqlite_dbptr virtual tables used
|
|
** by the recovery API */
|
|
sqlite3_dbdata_init(cx.db, 0, 0);
|
|
|
|
/* Consistent PRNG seed */
|
|
#ifdef SQLITE_TESTCTRL_PRNG_SEED
|
|
sqlite3_table_column_metadata(cx.db, 0, "x", 0, 0, 0, 0, 0, 0);
|
|
sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, 1, cx.db);
|
|
#else
|
|
sqlite3_randomness(0,0);
|
|
#endif
|
|
|
|
/* Run recovery on the initial database, just to make sure recovery
|
|
** works. */
|
|
if( !bNoRecover ){
|
|
recoverDatabase(cx.db);
|
|
}
|
|
|
|
zSql = sqlite3_malloc( nSql + 1 );
|
|
if( zSql==0 ){
|
|
fprintf(stderr, "Out of memory!\n");
|
|
}else{
|
|
memcpy(zSql, aData+iSql, nSql);
|
|
zSql[nSql] = 0;
|
|
for(i=j=0; zSql[i]; i++){
|
|
if( zSql[i]==';' ){
|
|
char cSaved = zSql[i+1];
|
|
zSql[i+1] = 0;
|
|
if( sqlite3_complete(zSql+j) ){
|
|
rc = runDbSql(cx.db, zSql+j, &btsFlags);
|
|
j = i+1;
|
|
}
|
|
zSql[i+1] = cSaved;
|
|
if( rc==SQLITE_INTERRUPT || progress_handler(&cx) ){
|
|
goto testrun_finished;
|
|
}
|
|
}
|
|
}
|
|
if( j<i ){
|
|
runDbSql(cx.db, zSql+j, &btsFlags);
|
|
}
|
|
}
|
|
testrun_finished:
|
|
sqlite3_free(zSql);
|
|
rc = sqlite3_close(cx.db);
|
|
if( rc!=SQLITE_OK ){
|
|
fprintf(stdout, "sqlite3_close() returns %d\n", rc);
|
|
}
|
|
if( eVerbosity>=2 && !bScript ){
|
|
fprintf(stdout, "Peak memory usages: %f MB\n",
|
|
sqlite3_memory_highwater(1) / 1000000.0);
|
|
}
|
|
if( sqlite3_memory_used()!=0 ){
|
|
int nAlloc = 0;
|
|
int nNotUsed = 0;
|
|
sqlite3_status(SQLITE_STATUS_MALLOC_COUNT, &nAlloc, &nNotUsed, 0);
|
|
fprintf(stderr,"Memory leak: %lld bytes in %d allocations\n",
|
|
sqlite3_memory_used(), nAlloc);
|
|
exit(1);
|
|
}
|
|
sqlite3_hard_heap_limit64(0);
|
|
sqlite3_soft_heap_limit64(0);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
** END of the dbsqlfuzz code
|
|
***************************************************************************/
|
|
|
|
/* Look at a SQL text and try to determine if it begins with a database
|
|
** description, such as would be found in a dbsqlfuzz test case. Return
|
|
** true if this does appear to be a dbsqlfuzz test case and false otherwise.
|
|
*/
|
|
static int isDbSql(unsigned char *a, int n){
|
|
unsigned char buf[12];
|
|
int i;
|
|
if( n>4 && memcmp(a,"\n--\n",4)==0 ) return 1;
|
|
while( n>0 && isspace(a[0]) ){ a++; n--; }
|
|
for(i=0; n>0 && i<8; n--, a++){
|
|
if( isxdigit(a[0]) ) buf[i++] = a[0];
|
|
}
|
|
if( i==8 && memcmp(buf,"53514c69",8)==0 ) return 1;
|
|
return 0;
|
|
}
|
|
|
|
/* Implementation of the isdbsql(TEXT) SQL function.
|
|
*/
|
|
static void isDbSqlFunc(
|
|
sqlite3_context *context,
|
|
int argc,
|
|
sqlite3_value **argv
|
|
){
|
|
int n = sqlite3_value_bytes(argv[0]);
|
|
unsigned char *a = (unsigned char*)sqlite3_value_blob(argv[0]);
|
|
sqlite3_result_int(context, a!=0 && n>0 && isDbSql(a,n));
|
|
}
|
|
|
|
/* Methods for the VHandle object
|
|
*/
|
|
static int inmemClose(sqlite3_file *pFile){
|
|
VHandle *p = (VHandle*)pFile;
|
|
VFile *pVFile = p->pVFile;
|
|
pVFile->nRef--;
|
|
if( pVFile->nRef==0 && pVFile->zFilename==0 ){
|
|
pVFile->sz = -1;
|
|
free(pVFile->a);
|
|
pVFile->a = 0;
|
|
}
|
|
return SQLITE_OK;
|
|
}
|
|
static int inmemRead(
|
|
sqlite3_file *pFile, /* Read from this open file */
|
|
void *pData, /* Store content in this buffer */
|
|
int iAmt, /* Bytes of content */
|
|
sqlite3_int64 iOfst /* Start reading here */
|
|
){
|
|
VHandle *pHandle = (VHandle*)pFile;
|
|
VFile *pVFile = pHandle->pVFile;
|
|
if( iOfst<0 || iOfst>=pVFile->sz ){
|
|
memset(pData, 0, iAmt);
|
|
return SQLITE_IOERR_SHORT_READ;
|
|
}
|
|
if( iOfst+iAmt>pVFile->sz ){
|
|
memset(pData, 0, iAmt);
|
|
iAmt = (int)(pVFile->sz - iOfst);
|
|
memcpy(pData, pVFile->a + iOfst, iAmt);
|
|
return SQLITE_IOERR_SHORT_READ;
|
|
}
|
|
memcpy(pData, pVFile->a + iOfst, iAmt);
|
|
return SQLITE_OK;
|
|
}
|
|
static int inmemWrite(
|
|
sqlite3_file *pFile, /* Write to this file */
|
|
const void *pData, /* Content to write */
|
|
int iAmt, /* bytes to write */
|
|
sqlite3_int64 iOfst /* Start writing here */
|
|
){
|
|
VHandle *pHandle = (VHandle*)pFile;
|
|
VFile *pVFile = pHandle->pVFile;
|
|
if( iOfst+iAmt > pVFile->sz ){
|
|
if( iOfst+iAmt >= MX_FILE_SZ ){
|
|
return SQLITE_FULL;
|
|
}
|
|
pVFile->a = safe_realloc(pVFile->a, (int)(iOfst+iAmt));
|
|
if( iOfst > pVFile->sz ){
|
|
memset(pVFile->a + pVFile->sz, 0, (int)(iOfst - pVFile->sz));
|
|
}
|
|
pVFile->sz = (int)(iOfst + iAmt);
|
|
}
|
|
memcpy(pVFile->a + iOfst, pData, iAmt);
|
|
return SQLITE_OK;
|
|
}
|
|
static int inmemTruncate(sqlite3_file *pFile, sqlite3_int64 iSize){
|
|
VHandle *pHandle = (VHandle*)pFile;
|
|
VFile *pVFile = pHandle->pVFile;
|
|
if( pVFile->sz>iSize && iSize>=0 ) pVFile->sz = (int)iSize;
|
|
return SQLITE_OK;
|
|
}
|
|
static int inmemSync(sqlite3_file *pFile, int flags){
|
|
return SQLITE_OK;
|
|
}
|
|
static int inmemFileSize(sqlite3_file *pFile, sqlite3_int64 *pSize){
|
|
*pSize = ((VHandle*)pFile)->pVFile->sz;
|
|
return SQLITE_OK;
|
|
}
|
|
static int inmemLock(sqlite3_file *pFile, int type){
|
|
return SQLITE_OK;
|
|
}
|
|
static int inmemUnlock(sqlite3_file *pFile, int type){
|
|
return SQLITE_OK;
|
|
}
|
|
static int inmemCheckReservedLock(sqlite3_file *pFile, int *pOut){
|
|
*pOut = 0;
|
|
return SQLITE_OK;
|
|
}
|
|
static int inmemFileControl(sqlite3_file *pFile, int op, void *pArg){
|
|
return SQLITE_NOTFOUND;
|
|
}
|
|
static int inmemSectorSize(sqlite3_file *pFile){
|
|
return 512;
|
|
}
|
|
static int inmemDeviceCharacteristics(sqlite3_file *pFile){
|
|
return
|
|
SQLITE_IOCAP_SAFE_APPEND |
|
|
SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN |
|
|
SQLITE_IOCAP_POWERSAFE_OVERWRITE;
|
|
}
|
|
|
|
|
|
/* Method table for VHandle
|
|
*/
|
|
static sqlite3_io_methods VHandleMethods = {
|
|
/* iVersion */ 1,
|
|
/* xClose */ inmemClose,
|
|
/* xRead */ inmemRead,
|
|
/* xWrite */ inmemWrite,
|
|
/* xTruncate */ inmemTruncate,
|
|
/* xSync */ inmemSync,
|
|
/* xFileSize */ inmemFileSize,
|
|
/* xLock */ inmemLock,
|
|
/* xUnlock */ inmemUnlock,
|
|
/* xCheck... */ inmemCheckReservedLock,
|
|
/* xFileCtrl */ inmemFileControl,
|
|
/* xSectorSz */ inmemSectorSize,
|
|
/* xDevchar */ inmemDeviceCharacteristics,
|
|
/* xShmMap */ 0,
|
|
/* xShmLock */ 0,
|
|
/* xShmBarrier */ 0,
|
|
/* xShmUnmap */ 0,
|
|
/* xFetch */ 0,
|
|
/* xUnfetch */ 0
|
|
};
|
|
|
|
/*
|
|
** Open a new file in the inmem VFS. All files are anonymous and are
|
|
** delete-on-close.
|
|
*/
|
|
static int inmemOpen(
|
|
sqlite3_vfs *pVfs,
|
|
const char *zFilename,
|
|
sqlite3_file *pFile,
|
|
int openFlags,
|
|
int *pOutFlags
|
|
){
|
|
VFile *pVFile = createVFile(zFilename, 0, (unsigned char*)"");
|
|
VHandle *pHandle = (VHandle*)pFile;
|
|
if( pVFile==0 ){
|
|
return SQLITE_FULL;
|
|
}
|
|
pHandle->pVFile = pVFile;
|
|
pVFile->nRef++;
|
|
pFile->pMethods = &VHandleMethods;
|
|
if( pOutFlags ) *pOutFlags = openFlags;
|
|
return SQLITE_OK;
|
|
}
|
|
|
|
/*
|
|
** Delete a file by name
|
|
*/
|
|
static int inmemDelete(
|
|
sqlite3_vfs *pVfs,
|
|
const char *zFilename,
|
|
int syncdir
|
|
){
|
|
VFile *pVFile = findVFile(zFilename);
|
|
if( pVFile==0 ) return SQLITE_OK;
|
|
if( pVFile->nRef==0 ){
|
|
free(pVFile->zFilename);
|
|
pVFile->zFilename = 0;
|
|
pVFile->sz = -1;
|
|
free(pVFile->a);
|
|
pVFile->a = 0;
|
|
return SQLITE_OK;
|
|
}
|
|
return SQLITE_IOERR_DELETE;
|
|
}
|
|
|
|
/* Check for the existance of a file
|
|
*/
|
|
static int inmemAccess(
|
|
sqlite3_vfs *pVfs,
|
|
const char *zFilename,
|
|
int flags,
|
|
int *pResOut
|
|
){
|
|
VFile *pVFile = findVFile(zFilename);
|
|
*pResOut = pVFile!=0;
|
|
return SQLITE_OK;
|
|
}
|
|
|
|
/* Get the canonical pathname for a file
|
|
*/
|
|
static int inmemFullPathname(
|
|
sqlite3_vfs *pVfs,
|
|
const char *zFilename,
|
|
int nOut,
|
|
char *zOut
|
|
){
|
|
sqlite3_snprintf(nOut, zOut, "%s", zFilename);
|
|
return SQLITE_OK;
|
|
}
|
|
|
|
/* Always use the same random see, for repeatability.
|
|
*/
|
|
static int inmemRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){
|
|
memset(zBuf, 0, nBuf);
|
|
memcpy(zBuf, &g.uRandom, nBuf<sizeof(g.uRandom) ? nBuf : sizeof(g.uRandom));
|
|
return nBuf;
|
|
}
|
|
|
|
/*
|
|
** Register the VFS that reads from the g.aFile[] set of files.
|
|
*/
|
|
static void inmemVfsRegister(int makeDefault){
|
|
static sqlite3_vfs inmemVfs;
|
|
sqlite3_vfs *pDefault = sqlite3_vfs_find(0);
|
|
inmemVfs.iVersion = 3;
|
|
inmemVfs.szOsFile = sizeof(VHandle);
|
|
inmemVfs.mxPathname = 200;
|
|
inmemVfs.zName = "inmem";
|
|
inmemVfs.xOpen = inmemOpen;
|
|
inmemVfs.xDelete = inmemDelete;
|
|
inmemVfs.xAccess = inmemAccess;
|
|
inmemVfs.xFullPathname = inmemFullPathname;
|
|
inmemVfs.xRandomness = inmemRandomness;
|
|
inmemVfs.xSleep = pDefault->xSleep;
|
|
inmemVfs.xCurrentTimeInt64 = pDefault->xCurrentTimeInt64;
|
|
sqlite3_vfs_register(&inmemVfs, makeDefault);
|
|
};
|
|
|
|
/*
|
|
** Allowed values for the runFlags parameter to runSql()
|
|
*/
|
|
#define SQL_TRACE 0x0001 /* Print each SQL statement as it is prepared */
|
|
#define SQL_OUTPUT 0x0002 /* Show the SQL output */
|
|
|
|
/*
|
|
** Run multiple commands of SQL. Similar to sqlite3_exec(), but does not
|
|
** stop if an error is encountered.
|
|
*/
|
|
static void runSql(sqlite3 *db, const char *zSql, unsigned runFlags){
|
|
const char *zMore;
|
|
sqlite3_stmt *pStmt;
|
|
|
|
while( zSql && zSql[0] ){
|
|
zMore = 0;
|
|
pStmt = 0;
|
|
sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zMore);
|
|
if( zMore==zSql ) break;
|
|
if( runFlags & SQL_TRACE ){
|
|
const char *z = zSql;
|
|
int n;
|
|
while( z<zMore && ISSPACE(z[0]) ) z++;
|
|
n = (int)(zMore - z);
|
|
while( n>0 && ISSPACE(z[n-1]) ) n--;
|
|
if( n==0 ) break;
|
|
if( pStmt==0 ){
|
|
printf("TRACE: %.*s (error: %s)\n", n, z, sqlite3_errmsg(db));
|
|
}else{
|
|
printf("TRACE: %.*s\n", n, z);
|
|
}
|
|
}
|
|
zSql = zMore;
|
|
if( pStmt ){
|
|
if( (runFlags & SQL_OUTPUT)==0 ){
|
|
while( SQLITE_ROW==sqlite3_step(pStmt) ){}
|
|
}else{
|
|
int nCol = -1;
|
|
while( SQLITE_ROW==sqlite3_step(pStmt) ){
|
|
int i;
|
|
if( nCol<0 ){
|
|
nCol = sqlite3_column_count(pStmt);
|
|
}else if( nCol>0 ){
|
|
printf("--------------------------------------------\n");
|
|
}
|
|
for(i=0; i<nCol; i++){
|
|
int eType = sqlite3_column_type(pStmt,i);
|
|
printf("%s = ", sqlite3_column_name(pStmt,i));
|
|
switch( eType ){
|
|
case SQLITE_NULL: {
|
|
printf("NULL\n");
|
|
break;
|
|
}
|
|
case SQLITE_INTEGER: {
|
|
printf("INT %s\n", sqlite3_column_text(pStmt,i));
|
|
break;
|
|
}
|
|
case SQLITE_FLOAT: {
|
|
printf("FLOAT %s\n", sqlite3_column_text(pStmt,i));
|
|
break;
|
|
}
|
|
case SQLITE_TEXT: {
|
|
printf("TEXT [%s]\n", sqlite3_column_text(pStmt,i));
|
|
break;
|
|
}
|
|
case SQLITE_BLOB: {
|
|
printf("BLOB (%d bytes)\n", sqlite3_column_bytes(pStmt,i));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
sqlite3_finalize(pStmt);
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
** Rebuild the database file.
|
|
**
|
|
** (1) Remove duplicate entries
|
|
** (2) Put all entries in order
|
|
** (3) Vacuum
|
|
*/
|
|
static void rebuild_database(sqlite3 *db, int dbSqlOnly){
|
|
int rc;
|
|
char *zSql;
|
|
zSql = sqlite3_mprintf(
|
|
"BEGIN;\n"
|
|
"CREATE TEMP TABLE dbx AS SELECT DISTINCT dbcontent FROM db;\n"
|
|
"DELETE FROM db;\n"
|
|
"INSERT INTO db(dbid, dbcontent) "
|
|
" SELECT NULL, dbcontent FROM dbx ORDER BY 2;\n"
|
|
"DROP TABLE dbx;\n"
|
|
"CREATE TEMP TABLE sx AS SELECT DISTINCT sqltext FROM xsql %s;\n"
|
|
"DELETE FROM xsql;\n"
|
|
"INSERT INTO xsql(sqlid,sqltext) "
|
|
" SELECT NULL, sqltext FROM sx ORDER BY 2;\n"
|
|
"DROP TABLE sx;\n"
|
|
"COMMIT;\n"
|
|
"PRAGMA page_size=1024;\n"
|
|
"VACUUM;\n",
|
|
dbSqlOnly ? " WHERE isdbsql(sqltext)" : ""
|
|
);
|
|
rc = sqlite3_exec(db, zSql, 0, 0, 0);
|
|
sqlite3_free(zSql);
|
|
if( rc ) fatalError("cannot rebuild: %s", sqlite3_errmsg(db));
|
|
}
|
|
|
|
/*
|
|
** Return the value of a hexadecimal digit. Return -1 if the input
|
|
** is not a hex digit.
|
|
*/
|
|
static int hexDigitValue(char c){
|
|
if( c>='0' && c<='9' ) return c - '0';
|
|
if( c>='a' && c<='f' ) return c - 'a' + 10;
|
|
if( c>='A' && c<='F' ) return c - 'A' + 10;
|
|
return -1;
|
|
}
|
|
|
|
/*
|
|
** Interpret zArg as an integer value, possibly with suffixes.
|
|
*/
|
|
static int integerValue(const char *zArg){
|
|
sqlite3_int64 v = 0;
|
|
static const struct { char *zSuffix; int iMult; } aMult[] = {
|
|
{ "KiB", 1024 },
|
|
{ "MiB", 1024*1024 },
|
|
{ "GiB", 1024*1024*1024 },
|
|
{ "KB", 1000 },
|
|
{ "MB", 1000000 },
|
|
{ "GB", 1000000000 },
|
|
{ "K", 1000 },
|
|
{ "M", 1000000 },
|
|
{ "G", 1000000000 },
|
|
};
|
|
int i;
|
|
int isNeg = 0;
|
|
if( zArg[0]=='-' ){
|
|
isNeg = 1;
|
|
zArg++;
|
|
}else if( zArg[0]=='+' ){
|
|
zArg++;
|
|
}
|
|
if( zArg[0]=='0' && zArg[1]=='x' ){
|
|
int x;
|
|
zArg += 2;
|
|
while( (x = hexDigitValue(zArg[0]))>=0 ){
|
|
v = (v<<4) + x;
|
|
zArg++;
|
|
}
|
|
}else{
|
|
while( ISDIGIT(zArg[0]) ){
|
|
v = v*10 + zArg[0] - '0';
|
|
zArg++;
|
|
}
|
|
}
|
|
for(i=0; i<sizeof(aMult)/sizeof(aMult[0]); i++){
|
|
if( sqlite3_stricmp(aMult[i].zSuffix, zArg)==0 ){
|
|
v *= aMult[i].iMult;
|
|
break;
|
|
}
|
|
}
|
|
if( v>0x7fffffff ) fatalError("parameter too large - max 2147483648");
|
|
return (int)(isNeg? -v : v);
|
|
}
|
|
|
|
/*
|
|
** Return the number of "v" characters in a string. Return 0 if there
|
|
** are any characters in the string other than "v".
|
|
*/
|
|
static int numberOfVChar(const char *z){
|
|
int N = 0;
|
|
while( z[0] && z[0]=='v' ){
|
|
z++;
|
|
N++;
|
|
}
|
|
return z[0]==0 ? N : 0;
|
|
}
|
|
|
|
/*
|
|
** Print sketchy documentation for this utility program
|
|
*/
|
|
static void showHelp(void){
|
|
printf("Usage: %s [options] SOURCE-DB ?ARGS...?\n", g.zArgv0);
|
|
printf(
|
|
"Read databases and SQL scripts from SOURCE-DB and execute each script against\n"
|
|
"each database, checking for crashes and memory leaks.\n"
|
|
"Options:\n"
|
|
" --cell-size-check Set the PRAGMA cell_size_check=ON\n"
|
|
" --dbid N Use only the database where dbid=N\n"
|
|
" --export-db DIR Write databases to files(s) in DIR. Works with --dbid\n"
|
|
" --export-sql DIR Write SQL to file(s) in DIR. Also works with --sqlid\n"
|
|
" --help Show this help text\n"
|
|
" --info Show information about SOURCE-DB w/o running tests\n"
|
|
" --limit-depth N Limit expression depth to N. Default: 500\n"
|
|
" --limit-heap N Limit heap memory to N. Default: 100M\n"
|
|
" --limit-mem N Limit memory used by test SQLite instance to N bytes\n"
|
|
" --limit-vdbe Panic if any test runs for more than 100,000 cycles\n"
|
|
" --load-sql FILE.. Load SQL scripts fron files into SOURCE-DB\n"
|
|
" --load-db FILE.. Load template databases from files into SOURCE_DB\n"
|
|
" --load-dbsql FILE.. Load dbsqlfuzz outputs into the xsql table\n"
|
|
" ^^^^------ Use \"-\" for FILE to read filenames from stdin\n"
|
|
" -m TEXT Add a description to the database\n"
|
|
" --native-vfs Use the native VFS for initially empty database files\n"
|
|
" --native-malloc Turn off MEMSYS3/5 and Lookaside\n"
|
|
" --no-recover Do not run recovery on dbsqlfuzz databases\n"
|
|
" --oss-fuzz Enable OSS-FUZZ testing\n"
|
|
" --prng-seed N Seed value for the PRGN inside of SQLite\n"
|
|
" -q|--quiet Reduced output\n"
|
|
" --rebuild Rebuild and vacuum the database file\n"
|
|
" --result-trace Show the results of each SQL command\n"
|
|
" --script Output CLI script instead of running tests\n"
|
|
" --skip N Skip the first N test cases\n"
|
|
" --spinner Use a spinner to show progress\n"
|
|
" --sqlid N Use only SQL where sqlid=N\n"
|
|
" --timeout N Maximum time for any one test in N millseconds\n"
|
|
" -v|--verbose Increased output. Repeat for more output.\n"
|
|
" --vdbe-debug Activate VDBE debugging.\n"
|
|
" --wait N Wait N seconds before continuing - useful for\n"
|
|
" attaching an MSVC debugging.\n"
|
|
);
|
|
}
|
|
|
|
int main(int argc, char **argv){
|
|
sqlite3_int64 iBegin; /* Start time of this program */
|
|
int quietFlag = 0; /* True if --quiet or -q */
|
|
int verboseFlag = 0; /* True if --verbose or -v */
|
|
char *zInsSql = 0; /* SQL statement for --load-db or --load-sql */
|
|
int iFirstInsArg = 0; /* First argv[] for --load-db or --load-sql */
|
|
sqlite3 *db = 0; /* The open database connection */
|
|
sqlite3_stmt *pStmt; /* A prepared statement */
|
|
int rc; /* Result code from SQLite interface calls */
|
|
Blob *pSql; /* For looping over SQL scripts */
|
|
Blob *pDb; /* For looping over template databases */
|
|
int i; /* Loop index for the argv[] loop */
|
|
int dbSqlOnly = 0; /* Only use scripts that are dbsqlfuzz */
|
|
int onlySqlid = -1; /* --sqlid */
|
|
int onlyDbid = -1; /* --dbid */
|
|
int nativeFlag = 0; /* --native-vfs */
|
|
int rebuildFlag = 0; /* --rebuild */
|
|
int vdbeLimitFlag = 0; /* --limit-vdbe */
|
|
int infoFlag = 0; /* --info */
|
|
int nSkip = 0; /* --skip */
|
|
int bScript = 0; /* --script */
|
|
int bSpinner = 0; /* True for --spinner */
|
|
int timeoutTest = 0; /* undocumented --timeout-test flag */
|
|
int runFlags = 0; /* Flags sent to runSql() */
|
|
char *zMsg = 0; /* Add this message */
|
|
int nSrcDb = 0; /* Number of source databases */
|
|
char **azSrcDb = 0; /* Array of source database names */
|
|
int iSrcDb; /* Loop over all source databases */
|
|
int nTest = 0; /* Total number of tests performed */
|
|
char *zDbName = ""; /* Appreviated name of a source database */
|
|
const char *zFailCode = 0; /* Value of the TEST_FAILURE env variable */
|
|
int cellSzCkFlag = 0; /* --cell-size-check */
|
|
int sqlFuzz = 0; /* True for SQL fuzz. False for DB fuzz */
|
|
int iTimeout = 120000; /* Default 120-second timeout */
|
|
int nMem = 0; /* Memory limit override */
|
|
int nMemThisDb = 0; /* Memory limit set by the CONFIG table */
|
|
char *zExpDb = 0; /* Write Databases to files in this directory */
|
|
char *zExpSql = 0; /* Write SQL to files in this directory */
|
|
void *pHeap = 0; /* Heap for use by SQLite */
|
|
int ossFuzz = 0; /* enable OSS-FUZZ testing */
|
|
int ossFuzzThisDb = 0; /* ossFuzz value for this particular database */
|
|
int nativeMalloc = 0; /* Turn off MEMSYS3/5 and lookaside if true */
|
|
sqlite3_vfs *pDfltVfs; /* The default VFS */
|
|
int openFlags4Data; /* Flags for sqlite3_open_v2() */
|
|
int bTimer = 0; /* Show elapse time for each test */
|
|
int nV; /* How much to increase verbosity with -vvvv */
|
|
sqlite3_int64 tmStart; /* Start of each test */
|
|
|
|
sqlite3_config(SQLITE_CONFIG_URI,1);
|
|
registerOomSimulator();
|
|
sqlite3_initialize();
|
|
iBegin = timeOfDay();
|
|
#ifdef __unix__
|
|
signal(SIGALRM, signalHandler);
|
|
signal(SIGSEGV, signalHandler);
|
|
signal(SIGABRT, signalHandler);
|
|
#endif
|
|
g.zArgv0 = argv[0];
|
|
openFlags4Data = SQLITE_OPEN_READONLY;
|
|
zFailCode = getenv("TEST_FAILURE");
|
|
pDfltVfs = sqlite3_vfs_find(0);
|
|
inmemVfsRegister(1);
|
|
for(i=1; i<argc; i++){
|
|
const char *z = argv[i];
|
|
if( z[0]=='-' ){
|
|
z++;
|
|
if( z[0]=='-' ) z++;
|
|
if( strcmp(z,"cell-size-check")==0 ){
|
|
cellSzCkFlag = 1;
|
|
}else
|
|
if( strcmp(z,"dbid")==0 ){
|
|
if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
|
|
onlyDbid = integerValue(argv[++i]);
|
|
}else
|
|
if( strcmp(z,"export-db")==0 ){
|
|
if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
|
|
zExpDb = argv[++i];
|
|
}else
|
|
if( strcmp(z,"export-sql")==0 || strcmp(z,"export-dbsql")==0 ){
|
|
if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
|
|
zExpSql = argv[++i];
|
|
}else
|
|
if( strcmp(z,"help")==0 ){
|
|
showHelp();
|
|
return 0;
|
|
}else
|
|
if( strcmp(z,"info")==0 ){
|
|
infoFlag = 1;
|
|
}else
|
|
if( strcmp(z,"limit-depth")==0 ){
|
|
if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
|
|
depthLimit = integerValue(argv[++i]);
|
|
}else
|
|
if( strcmp(z,"limit-heap")==0 ){
|
|
if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
|
|
heapLimit = integerValue(argv[++i]);
|
|
}else
|
|
if( strcmp(z,"limit-mem")==0 ){
|
|
if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
|
|
nMem = integerValue(argv[++i]);
|
|
}else
|
|
if( strcmp(z,"limit-vdbe")==0 ){
|
|
vdbeLimitFlag = 1;
|
|
}else
|
|
if( strcmp(z,"load-sql")==0 ){
|
|
zInsSql = "INSERT INTO xsql(sqltext)"
|
|
"VALUES(CAST(readtextfile(?1) AS text))";
|
|
iFirstInsArg = i+1;
|
|
openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
|
|
break;
|
|
}else
|
|
if( strcmp(z,"load-db")==0 ){
|
|
zInsSql = "INSERT INTO db(dbcontent) VALUES(readfile(?1))";
|
|
iFirstInsArg = i+1;
|
|
openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
|
|
break;
|
|
}else
|
|
if( strcmp(z,"load-dbsql")==0 ){
|
|
zInsSql = "INSERT INTO xsql(sqltext)"
|
|
"VALUES(readfile(?1))";
|
|
iFirstInsArg = i+1;
|
|
openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
|
|
dbSqlOnly = 1;
|
|
break;
|
|
}else
|
|
if( strcmp(z,"m")==0 ){
|
|
if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
|
|
zMsg = argv[++i];
|
|
openFlags4Data = SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE;
|
|
}else
|
|
if( strcmp(z,"native-malloc")==0 ){
|
|
nativeMalloc = 1;
|
|
}else
|
|
if( strcmp(z,"native-vfs")==0 ){
|
|
nativeFlag = 1;
|
|
}else
|
|
if( strcmp(z,"no-recover")==0 ){
|
|
bNoRecover = 1;
|
|
}else
|
|
if( strcmp(z,"oss-fuzz")==0 ){
|
|
ossFuzz = 1;
|
|
}else
|
|
if( strcmp(z,"prng-seed")==0 ){
|
|
if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
|
|
g.uRandom = atoi(argv[++i]);
|
|
}else
|
|
if( strcmp(z,"quiet")==0 || strcmp(z,"q")==0 ){
|
|
quietFlag = 1;
|
|
verboseFlag = 0;
|
|
eVerbosity = 0;
|
|
}else
|
|
if( strcmp(z,"rebuild")==0 ){
|
|
rebuildFlag = 1;
|
|
openFlags4Data = SQLITE_OPEN_READWRITE;
|
|
}else
|
|
if( strcmp(z,"result-trace")==0 ){
|
|
runFlags |= SQL_OUTPUT;
|
|
}else
|
|
if( strcmp(z,"script")==0 ){
|
|
bScript = 1;
|
|
}else
|
|
if( strcmp(z,"skip")==0 ){
|
|
if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
|
|
nSkip = atoi(argv[++i]);
|
|
}else
|
|
if( strcmp(z,"spinner")==0 ){
|
|
bSpinner = 1;
|
|
}else
|
|
if( strcmp(z,"timer")==0 ){
|
|
bTimer = 1;
|
|
}else
|
|
if( strcmp(z,"sqlid")==0 ){
|
|
if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
|
|
onlySqlid = integerValue(argv[++i]);
|
|
}else
|
|
if( strcmp(z,"timeout")==0 ){
|
|
if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
|
|
iTimeout = integerValue(argv[++i]);
|
|
}else
|
|
if( strcmp(z,"timeout-test")==0 ){
|
|
timeoutTest = 1;
|
|
#ifndef __unix__
|
|
fatalError("timeout is not available on non-unix systems");
|
|
#endif
|
|
}else
|
|
if( strcmp(z,"vdbe-debug")==0 ){
|
|
bVdbeDebug = 1;
|
|
}else
|
|
if( strcmp(z,"verbose")==0 ){
|
|
quietFlag = 0;
|
|
verboseFlag++;
|
|
eVerbosity++;
|
|
if( verboseFlag>2 ) runFlags |= SQL_TRACE;
|
|
}else
|
|
if( (nV = numberOfVChar(z))>=1 ){
|
|
quietFlag = 0;
|
|
verboseFlag += nV;
|
|
eVerbosity += nV;
|
|
if( verboseFlag>2 ) runFlags |= SQL_TRACE;
|
|
}else
|
|
if( strcmp(z,"version")==0 ){
|
|
int ii;
|
|
const char *zz;
|
|
printf("SQLite %s %s (%d-bit)\n",
|
|
sqlite3_libversion(), sqlite3_sourceid(),
|
|
8*(int)sizeof(char*));
|
|
for(ii=0; (zz = sqlite3_compileoption_get(ii))!=0; ii++){
|
|
printf("%s\n", zz);
|
|
}
|
|
return 0;
|
|
}else
|
|
if( strcmp(z,"wait")==0 ){
|
|
int iDelay;
|
|
if( i>=argc-1 ) fatalError("missing arguments on %s", argv[i]);
|
|
iDelay = integerValue(argv[++i]);
|
|
printf("Waiting %d seconds:", iDelay);
|
|
fflush(stdout);
|
|
while( 1 /*exit-by-break*/ ){
|
|
sqlite3_sleep(1000);
|
|
iDelay--;
|
|
if( iDelay<=0 ) break;
|
|
printf(" %d", iDelay);
|
|
fflush(stdout);
|
|
}
|
|
printf("\n");
|
|
fflush(stdout);
|
|
}else
|
|
if( strcmp(z,"is-dbsql")==0 ){
|
|
i++;
|
|
for(i++; i<argc; i++){
|
|
long nData;
|
|
char *aData = readFile(argv[i], &nData);
|
|
printf("%d %s\n", isDbSql((unsigned char*)aData,nData), argv[i]);
|
|
sqlite3_free(aData);
|
|
}
|
|
exit(0);
|
|
}else
|
|
{
|
|
fatalError("unknown option: %s", argv[i]);
|
|
}
|
|
}else{
|
|
nSrcDb++;
|
|
azSrcDb = safe_realloc(azSrcDb, nSrcDb*sizeof(azSrcDb[0]));
|
|
azSrcDb[nSrcDb-1] = argv[i];
|
|
}
|
|
}
|
|
if( nSrcDb==0 ) fatalError("no source database specified");
|
|
if( nSrcDb>1 ){
|
|
if( zMsg ){
|
|
fatalError("cannot change the description of more than one database");
|
|
}
|
|
if( zInsSql ){
|
|
fatalError("cannot import into more than one database");
|
|
}
|
|
}
|
|
|
|
/* Process each source database separately */
|
|
for(iSrcDb=0; iSrcDb<nSrcDb; iSrcDb++){
|
|
char *zRawData = 0;
|
|
long nRawData = 0;
|
|
g.zDbFile = azSrcDb[iSrcDb];
|
|
rc = sqlite3_open_v2(azSrcDb[iSrcDb], &db,
|
|
openFlags4Data, pDfltVfs->zName);
|
|
if( rc==SQLITE_OK ){
|
|
rc = sqlite3_exec(db, "SELECT count(*) FROM sqlite_schema", 0, 0, 0);
|
|
}
|
|
if( rc ){
|
|
sqlite3_close(db);
|
|
zRawData = readFile(azSrcDb[iSrcDb], &nRawData);
|
|
if( zRawData==0 ){
|
|
fatalError("input file \"%s\" is not recognized\n", azSrcDb[iSrcDb]);
|
|
}
|
|
sqlite3_open(":memory:", &db);
|
|
}
|
|
|
|
/* Print the description, if there is one */
|
|
if( infoFlag ){
|
|
int n;
|
|
zDbName = azSrcDb[iSrcDb];
|
|
i = (int)strlen(zDbName) - 1;
|
|
while( i>0 && zDbName[i-1]!='/' && zDbName[i-1]!='\\' ){ i--; }
|
|
zDbName += i;
|
|
sqlite3_prepare_v2(db, "SELECT msg FROM readme", -1, &pStmt, 0);
|
|
if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
|
|
printf("%s: %s", zDbName, sqlite3_column_text(pStmt,0));
|
|
}else{
|
|
printf("%s: (empty \"readme\")", zDbName);
|
|
}
|
|
sqlite3_finalize(pStmt);
|
|
sqlite3_prepare_v2(db, "SELECT count(*) FROM db", -1, &pStmt, 0);
|
|
if( pStmt
|
|
&& sqlite3_step(pStmt)==SQLITE_ROW
|
|
&& (n = sqlite3_column_int(pStmt,0))>0
|
|
){
|
|
printf(" - %d DBs", n);
|
|
}
|
|
sqlite3_finalize(pStmt);
|
|
sqlite3_prepare_v2(db, "SELECT count(*) FROM xsql", -1, &pStmt, 0);
|
|
if( pStmt
|
|
&& sqlite3_step(pStmt)==SQLITE_ROW
|
|
&& (n = sqlite3_column_int(pStmt,0))>0
|
|
){
|
|
printf(" - %d scripts", n);
|
|
}
|
|
sqlite3_finalize(pStmt);
|
|
printf("\n");
|
|
sqlite3_close(db);
|
|
sqlite3_free(zRawData);
|
|
continue;
|
|
}
|
|
|
|
rc = sqlite3_exec(db,
|
|
"CREATE TABLE IF NOT EXISTS db(\n"
|
|
" dbid INTEGER PRIMARY KEY, -- database id\n"
|
|
" dbcontent BLOB -- database disk file image\n"
|
|
");\n"
|
|
"CREATE TABLE IF NOT EXISTS xsql(\n"
|
|
" sqlid INTEGER PRIMARY KEY, -- SQL script id\n"
|
|
" sqltext TEXT -- Text of SQL statements to run\n"
|
|
");"
|
|
"CREATE TABLE IF NOT EXISTS readme(\n"
|
|
" msg TEXT -- Human-readable description of this file\n"
|
|
");", 0, 0, 0);
|
|
if( rc ) fatalError("cannot create schema: %s", sqlite3_errmsg(db));
|
|
if( zMsg ){
|
|
char *zSql;
|
|
zSql = sqlite3_mprintf(
|
|
"DELETE FROM readme; INSERT INTO readme(msg) VALUES(%Q)", zMsg);
|
|
rc = sqlite3_exec(db, zSql, 0, 0, 0);
|
|
sqlite3_free(zSql);
|
|
if( rc ) fatalError("cannot change description: %s", sqlite3_errmsg(db));
|
|
}
|
|
if( zRawData ){
|
|
zInsSql = "INSERT INTO xsql(sqltext) VALUES(?1)";
|
|
rc = sqlite3_prepare_v2(db, zInsSql, -1, &pStmt, 0);
|
|
if( rc ) fatalError("cannot prepare statement [%s]: %s",
|
|
zInsSql, sqlite3_errmsg(db));
|
|
sqlite3_bind_text(pStmt, 1, zRawData, nRawData, SQLITE_STATIC);
|
|
sqlite3_step(pStmt);
|
|
rc = sqlite3_reset(pStmt);
|
|
if( rc ) fatalError("insert failed for %s", argv[i]);
|
|
sqlite3_finalize(pStmt);
|
|
rebuild_database(db, dbSqlOnly);
|
|
zInsSql = 0;
|
|
sqlite3_free(zRawData);
|
|
zRawData = 0;
|
|
}
|
|
ossFuzzThisDb = ossFuzz;
|
|
|
|
/* If the CONFIG(name,value) table exists, read db-specific settings
|
|
** from that table */
|
|
if( sqlite3_table_column_metadata(db,0,"config",0,0,0,0,0,0)==SQLITE_OK ){
|
|
rc = sqlite3_prepare_v2(db, "SELECT name, value FROM config",
|
|
-1, &pStmt, 0);
|
|
if( rc ) fatalError("cannot prepare query of CONFIG table: %s",
|
|
sqlite3_errmsg(db));
|
|
while( SQLITE_ROW==sqlite3_step(pStmt) ){
|
|
const char *zName = (const char *)sqlite3_column_text(pStmt,0);
|
|
if( zName==0 ) continue;
|
|
if( strcmp(zName, "oss-fuzz")==0 ){
|
|
ossFuzzThisDb = sqlite3_column_int(pStmt,1);
|
|
if( verboseFlag>1 ) printf("Config: oss-fuzz=%d\n", ossFuzzThisDb);
|
|
}
|
|
if( strcmp(zName, "limit-mem")==0 ){
|
|
nMemThisDb = sqlite3_column_int(pStmt,1);
|
|
if( verboseFlag>1 ) printf("Config: limit-mem=%d\n", nMemThisDb);
|
|
}
|
|
}
|
|
sqlite3_finalize(pStmt);
|
|
}
|
|
|
|
if( zInsSql ){
|
|
sqlite3_create_function(db, "readfile", 1, SQLITE_UTF8, 0,
|
|
readfileFunc, 0, 0);
|
|
sqlite3_create_function(db, "readtextfile", 1, SQLITE_UTF8, 0,
|
|
readtextfileFunc, 0, 0);
|
|
sqlite3_create_function(db, "isdbsql", 1, SQLITE_UTF8, 0,
|
|
isDbSqlFunc, 0, 0);
|
|
rc = sqlite3_prepare_v2(db, zInsSql, -1, &pStmt, 0);
|
|
if( rc ) fatalError("cannot prepare statement [%s]: %s",
|
|
zInsSql, sqlite3_errmsg(db));
|
|
rc = sqlite3_exec(db, "BEGIN", 0, 0, 0);
|
|
if( rc ) fatalError("cannot start a transaction");
|
|
for(i=iFirstInsArg; i<argc; i++){
|
|
if( strcmp(argv[i],"-")==0 ){
|
|
/* A filename of "-" means read multiple filenames from stdin */
|
|
char zLine[2000];
|
|
while( rc==0 && fgets(zLine,sizeof(zLine),stdin)!=0 ){
|
|
size_t kk = strlen(zLine);
|
|
while( kk>0 && zLine[kk-1]<=' ' ) kk--;
|
|
sqlite3_bind_text(pStmt, 1, zLine, (int)kk, SQLITE_STATIC);
|
|
if( verboseFlag>1 ) printf("loading %.*s\n", (int)kk, zLine);
|
|
sqlite3_step(pStmt);
|
|
rc = sqlite3_reset(pStmt);
|
|
if( rc ) fatalError("insert failed for %s", zLine);
|
|
}
|
|
}else{
|
|
sqlite3_bind_text(pStmt, 1, argv[i], -1, SQLITE_STATIC);
|
|
if( verboseFlag>1 ) printf("loading %s\n", argv[i]);
|
|
sqlite3_step(pStmt);
|
|
rc = sqlite3_reset(pStmt);
|
|
if( rc ) fatalError("insert failed for %s", argv[i]);
|
|
}
|
|
}
|
|
sqlite3_finalize(pStmt);
|
|
rc = sqlite3_exec(db, "COMMIT", 0, 0, 0);
|
|
if( rc ) fatalError("cannot commit the transaction: %s",
|
|
sqlite3_errmsg(db));
|
|
rebuild_database(db, dbSqlOnly);
|
|
sqlite3_close(db);
|
|
return 0;
|
|
}
|
|
rc = sqlite3_exec(db, "PRAGMA query_only=1;", 0, 0, 0);
|
|
if( rc ) fatalError("cannot set database to query-only");
|
|
if( zExpDb!=0 || zExpSql!=0 ){
|
|
sqlite3_create_function(db, "writefile", 2, SQLITE_UTF8, 0,
|
|
writefileFunc, 0, 0);
|
|
if( zExpDb!=0 ){
|
|
const char *zExDb =
|
|
"SELECT writefile(printf('%s/db%06d.db',?1,dbid),dbcontent),"
|
|
" dbid, printf('%s/db%06d.db',?1,dbid), length(dbcontent)"
|
|
" FROM db WHERE ?2<0 OR dbid=?2;";
|
|
rc = sqlite3_prepare_v2(db, zExDb, -1, &pStmt, 0);
|
|
if( rc ) fatalError("cannot prepare statement [%s]: %s",
|
|
zExDb, sqlite3_errmsg(db));
|
|
sqlite3_bind_text64(pStmt, 1, zExpDb, strlen(zExpDb),
|
|
SQLITE_STATIC, SQLITE_UTF8);
|
|
sqlite3_bind_int(pStmt, 2, onlyDbid);
|
|
while( sqlite3_step(pStmt)==SQLITE_ROW ){
|
|
printf("write db-%d (%d bytes) into %s\n",
|
|
sqlite3_column_int(pStmt,1),
|
|
sqlite3_column_int(pStmt,3),
|
|
sqlite3_column_text(pStmt,2));
|
|
}
|
|
sqlite3_finalize(pStmt);
|
|
}
|
|
if( zExpSql!=0 ){
|
|
const char *zExSql =
|
|
"SELECT writefile(printf('%s/sql%06d.txt',?1,sqlid),sqltext),"
|
|
" sqlid, printf('%s/sql%06d.txt',?1,sqlid), length(sqltext)"
|
|
" FROM xsql WHERE ?2<0 OR sqlid=?2;";
|
|
rc = sqlite3_prepare_v2(db, zExSql, -1, &pStmt, 0);
|
|
if( rc ) fatalError("cannot prepare statement [%s]: %s",
|
|
zExSql, sqlite3_errmsg(db));
|
|
sqlite3_bind_text64(pStmt, 1, zExpSql, strlen(zExpSql),
|
|
SQLITE_STATIC, SQLITE_UTF8);
|
|
sqlite3_bind_int(pStmt, 2, onlySqlid);
|
|
while( sqlite3_step(pStmt)==SQLITE_ROW ){
|
|
printf("write sql-%d (%d bytes) into %s\n",
|
|
sqlite3_column_int(pStmt,1),
|
|
sqlite3_column_int(pStmt,3),
|
|
sqlite3_column_text(pStmt,2));
|
|
}
|
|
sqlite3_finalize(pStmt);
|
|
}
|
|
sqlite3_close(db);
|
|
return 0;
|
|
}
|
|
|
|
/* Load all SQL script content and all initial database images from the
|
|
** source db
|
|
*/
|
|
blobListLoadFromDb(db, "SELECT sqlid, sqltext FROM xsql", onlySqlid,
|
|
&g.nSql, &g.pFirstSql);
|
|
if( g.nSql==0 ) fatalError("need at least one SQL script");
|
|
blobListLoadFromDb(db, "SELECT dbid, dbcontent FROM db", onlyDbid,
|
|
&g.nDb, &g.pFirstDb);
|
|
if( g.nDb==0 ){
|
|
g.pFirstDb = safe_realloc(0, sizeof(Blob));
|
|
memset(g.pFirstDb, 0, sizeof(Blob));
|
|
g.pFirstDb->id = 1;
|
|
g.pFirstDb->seq = 0;
|
|
g.nDb = 1;
|
|
sqlFuzz = 1;
|
|
}
|
|
|
|
/* Print the description, if there is one */
|
|
if( !quietFlag && !bScript ){
|
|
zDbName = azSrcDb[iSrcDb];
|
|
i = (int)strlen(zDbName) - 1;
|
|
while( i>0 && zDbName[i-1]!='/' && zDbName[i-1]!='\\' ){ i--; }
|
|
zDbName += i;
|
|
if( verboseFlag ){
|
|
sqlite3_prepare_v2(db, "SELECT msg FROM readme", -1, &pStmt, 0);
|
|
if( pStmt && sqlite3_step(pStmt)==SQLITE_ROW ){
|
|
printf("%s: %s\n", zDbName, sqlite3_column_text(pStmt,0));
|
|
}
|
|
sqlite3_finalize(pStmt);
|
|
}
|
|
}
|
|
|
|
/* Rebuild the database, if requested */
|
|
if( rebuildFlag ){
|
|
if( !quietFlag ){
|
|
printf("%s: rebuilding... ", zDbName);
|
|
fflush(stdout);
|
|
}
|
|
rebuild_database(db, 0);
|
|
if( !quietFlag ) printf("done\n");
|
|
}
|
|
|
|
/* Close the source database. Verify that no SQLite memory allocations are
|
|
** outstanding.
|
|
*/
|
|
sqlite3_close(db);
|
|
if( sqlite3_memory_used()>0 ){
|
|
fatalError("SQLite has memory in use before the start of testing");
|
|
}
|
|
|
|
/* Limit available memory, if requested */
|
|
sqlite3_shutdown();
|
|
|
|
if( nMemThisDb>0 && nMem==0 ){
|
|
if( !nativeMalloc ){
|
|
pHeap = realloc(pHeap, nMemThisDb);
|
|
if( pHeap==0 ){
|
|
fatalError("failed to allocate %d bytes of heap memory", nMem);
|
|
}
|
|
sqlite3_config(SQLITE_CONFIG_HEAP, pHeap, nMemThisDb, 128);
|
|
}else{
|
|
sqlite3_hard_heap_limit64((sqlite3_int64)nMemThisDb);
|
|
}
|
|
}else{
|
|
sqlite3_hard_heap_limit64(0);
|
|
}
|
|
|
|
/* Disable lookaside with the --native-malloc option */
|
|
if( nativeMalloc ){
|
|
sqlite3_config(SQLITE_CONFIG_LOOKASIDE, 0, 0);
|
|
}
|
|
|
|
/* Reset the in-memory virtual filesystem */
|
|
formatVfs();
|
|
|
|
/* Run a test using each SQL script against each database.
|
|
*/
|
|
if( verboseFlag<2 && !quietFlag && !bSpinner && !bScript ){
|
|
printf("%s:", zDbName);
|
|
}
|
|
for(pSql=g.pFirstSql; pSql; pSql=pSql->pNext){
|
|
tmStart = timeOfDay();
|
|
if( isDbSql(pSql->a, pSql->sz) ){
|
|
sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "sqlid=%d",pSql->id);
|
|
if( bScript ){
|
|
/* No progress output */
|
|
}else if( bSpinner ){
|
|
int nTotal =g.nSql;
|
|
int idx = pSql->seq;
|
|
printf("\r%s: %d/%d ", zDbName, idx, nTotal);
|
|
fflush(stdout);
|
|
}else if( verboseFlag>1 ){
|
|
printf("%s\n", g.zTestName);
|
|
fflush(stdout);
|
|
}else if( !quietFlag ){
|
|
static int prevAmt = -1;
|
|
int idx = pSql->seq;
|
|
int amt = idx*10/(g.nSql);
|
|
if( amt!=prevAmt ){
|
|
printf(" %d%%", amt*10);
|
|
fflush(stdout);
|
|
prevAmt = amt;
|
|
}
|
|
}
|
|
if( nSkip>0 ){
|
|
nSkip--;
|
|
}else{
|
|
runCombinedDbSqlInput(pSql->a, pSql->sz, iTimeout, bScript, pSql->id);
|
|
}
|
|
nTest++;
|
|
if( bTimer && !bScript ){
|
|
sqlite3_int64 tmEnd = timeOfDay();
|
|
printf("%lld %s\n", tmEnd - tmStart, g.zTestName);
|
|
}
|
|
g.zTestName[0] = 0;
|
|
disableOom();
|
|
continue;
|
|
}
|
|
for(pDb=g.pFirstDb; pDb; pDb=pDb->pNext){
|
|
int openFlags;
|
|
const char *zVfs = "inmem";
|
|
sqlite3_snprintf(sizeof(g.zTestName), g.zTestName, "sqlid=%d,dbid=%d",
|
|
pSql->id, pDb->id);
|
|
if( bScript ){
|
|
/* No progress output */
|
|
}else if( bSpinner ){
|
|
int nTotal = g.nDb*g.nSql;
|
|
int idx = pSql->seq*g.nDb + pDb->id - 1;
|
|
printf("\r%s: %d/%d ", zDbName, idx, nTotal);
|
|
fflush(stdout);
|
|
}else if( verboseFlag>1 ){
|
|
printf("%s\n", g.zTestName);
|
|
fflush(stdout);
|
|
}else if( !quietFlag ){
|
|
static int prevAmt = -1;
|
|
int idx = pSql->seq*g.nDb + pDb->id - 1;
|
|
int amt = idx*10/(g.nDb*g.nSql);
|
|
if( amt!=prevAmt ){
|
|
printf(" %d%%", amt*10);
|
|
fflush(stdout);
|
|
prevAmt = amt;
|
|
}
|
|
}
|
|
if( nSkip>0 ){
|
|
nSkip--;
|
|
continue;
|
|
}
|
|
if( bScript ){
|
|
char zName[100];
|
|
sqlite3_snprintf(sizeof(zName), zName, "db%06d.db",
|
|
pDb->id>1 ? pDb->id : pSql->id);
|
|
renderDbSqlForCLI(stdout, zName,
|
|
pDb->a, pDb->sz, pSql->a, pSql->sz);
|
|
continue;
|
|
}
|
|
createVFile("main.db", pDb->sz, pDb->a);
|
|
sqlite3_randomness(0,0);
|
|
if( ossFuzzThisDb ){
|
|
#ifndef SQLITE_OSS_FUZZ
|
|
fatalError("--oss-fuzz not supported: recompile"
|
|
" with -DSQLITE_OSS_FUZZ");
|
|
#else
|
|
extern int LLVMFuzzerTestOneInput(const uint8_t*, size_t);
|
|
LLVMFuzzerTestOneInput((const uint8_t*)pSql->a, (size_t)pSql->sz);
|
|
#endif
|
|
}else{
|
|
openFlags = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE;
|
|
if( nativeFlag && pDb->sz==0 ){
|
|
openFlags |= SQLITE_OPEN_MEMORY;
|
|
zVfs = 0;
|
|
}
|
|
rc = sqlite3_open_v2("main.db", &db, openFlags, zVfs);
|
|
if( rc ) fatalError("cannot open inmem database");
|
|
sqlite3_limit(db, SQLITE_LIMIT_LENGTH, 100000000);
|
|
sqlite3_limit(db, SQLITE_LIMIT_LIKE_PATTERN_LENGTH, 50);
|
|
if( cellSzCkFlag ) runSql(db, "PRAGMA cell_size_check=ON", runFlags);
|
|
setAlarm((iTimeout+999)/1000);
|
|
/* Enable test functions */
|
|
sqlite3_test_control(SQLITE_TESTCTRL_INTERNAL_FUNCTIONS, db);
|
|
#ifndef SQLITE_OMIT_PROGRESS_CALLBACK
|
|
if( sqlFuzz || vdbeLimitFlag ){
|
|
sqlite3_progress_handler(db, 100000, progressHandler,
|
|
&vdbeLimitFlag);
|
|
}
|
|
#endif
|
|
#ifdef SQLITE_TESTCTRL_PRNG_SEED
|
|
sqlite3_test_control(SQLITE_TESTCTRL_PRNG_SEED, 1, db);
|
|
#endif
|
|
if( bVdbeDebug ){
|
|
sqlite3_exec(db, "PRAGMA vdbe_debug=ON", 0, 0, 0);
|
|
}
|
|
do{
|
|
runSql(db, (char*)pSql->a, runFlags);
|
|
}while( timeoutTest );
|
|
setAlarm(0);
|
|
sqlite3_exec(db, "PRAGMA temp_store_directory=''", 0, 0, 0);
|
|
sqlite3_close(db);
|
|
}
|
|
if( sqlite3_memory_used()>0 ){
|
|
fatalError("memory leak: %lld bytes outstanding",
|
|
sqlite3_memory_used());
|
|
}
|
|
reformatVfs();
|
|
nTest++;
|
|
if( bTimer ){
|
|
sqlite3_int64 tmEnd = timeOfDay();
|
|
printf("%lld %s\n", tmEnd - tmStart, g.zTestName);
|
|
}
|
|
g.zTestName[0] = 0;
|
|
|
|
/* Simulate an error if the TEST_FAILURE environment variable is "5".
|
|
** This is used to verify that automated test script really do spot
|
|
** errors that occur in this test program.
|
|
*/
|
|
if( zFailCode ){
|
|
if( zFailCode[0]=='5' && zFailCode[1]==0 ){
|
|
fatalError("simulated failure");
|
|
}else if( zFailCode[0]!=0 ){
|
|
/* If TEST_FAILURE is something other than 5, just exit the test
|
|
** early */
|
|
printf("\nExit early due to TEST_FAILURE being set\n");
|
|
iSrcDb = nSrcDb-1;
|
|
goto sourcedb_cleanup;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if( bScript ){
|
|
/* No progress output */
|
|
}else if( bSpinner ){
|
|
int nTotal = g.nDb*g.nSql;
|
|
printf("\r%s: %d/%d \n", zDbName, nTotal, nTotal);
|
|
}else if( !quietFlag && verboseFlag<2 ){
|
|
printf(" 100%% - %d tests\n", g.nDb*g.nSql);
|
|
}
|
|
|
|
/* Clean up at the end of processing a single source database
|
|
*/
|
|
sourcedb_cleanup:
|
|
blobListFree(g.pFirstSql);
|
|
blobListFree(g.pFirstDb);
|
|
reformatVfs();
|
|
|
|
} /* End loop over all source databases */
|
|
|
|
if( !quietFlag && !bScript ){
|
|
sqlite3_int64 iElapse = timeOfDay() - iBegin;
|
|
if( g.nInvariant ){
|
|
printf("fuzzcheck: %u query invariants checked\n", g.nInvariant);
|
|
}
|
|
printf("fuzzcheck: 0 errors out of %d tests in %d.%03d seconds\n"
|
|
"SQLite %s %s (%d-bit)\n",
|
|
nTest, (int)(iElapse/1000), (int)(iElapse%1000),
|
|
sqlite3_libversion(), sqlite3_sourceid(),
|
|
8*(int)sizeof(char*));
|
|
}
|
|
free(azSrcDb);
|
|
free(pHeap);
|
|
return 0;
|
|
}
|