]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Initial work on a fix for the SAHPool VFS's effectively-no-op digest calculation...
authorstephan <stephan@noemail.net>
Mon, 3 Feb 2025 16:26:30 +0000 (16:26 +0000)
committerstephan <stephan@noemail.net>
Mon, 3 Feb 2025 16:26:30 +0000 (16:26 +0000)
FossilOrigin-Name: 9234c33f92d92bfddc6211c9c587f1072e70837c0ffe1416ef7d84d59bacd364

ext/wasm/api/sqlite3-vfs-opfs-sahpool.c-pp.js
ext/wasm/tester1.c-pp.js
manifest
manifest.uuid

index 6551b5c89c0b679818ad6ac17d65204b333dafcf..38a1b91dde34f8a95d099d2fbb1547f32cf42f26 100644 (file)
@@ -79,6 +79,32 @@ globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
         capi.SQLITE_OPEN_MAIN_JOURNAL |
         capi.SQLITE_OPEN_SUPER_JOURNAL |
         capi.SQLITE_OPEN_WAL;
+  const FLAG_COMPUTE_DIGEST_V2 = 1 ? capi.SQLITE_OPEN_MEMORY : 0
+  /* Part of the fix for
+     https://github.com/sqlite/sqlite-wasm/issues/97
+
+     Summary: prior to versions 3.49.1 and 3.50 computeDigest() always
+     computes a value of [0,0], so it does not actually do anything
+     useful.  Fixing it invalidates old persistent files, so we
+     instead only fix it for files created or updated since the bug
+     was discovered and fixed.
+
+     This flag determines whether we use the broken legacy
+     computeDigest() or the v2 variant. We only use this flag for
+     newly-created/overwritten files. Pre-existing files have the
+     broken digest stored in them so need to continue to use that.
+
+     This flag is stored in the same space as the
+     SQLITE_OPEN_... flags and we must be careful here to not use an
+     flag bit which is otherwise relevant for the VFS.
+     SQLITE_OPEN_MEMORY is handled by sqlite3_open_v2() and friends,
+     not the VFS, so we'll repurpose that one.  If we take a
+     currently-unused bit and it ends up, at some later point, being
+     used, we would have to invalidate existing VFS files in order to
+     move to another bit.  Similarly, if the SQLITE_OPEN_MEMORY bit
+     were ever reassigned (which it won't be!), we'd invalidate all
+     VFS-side files.
+  */;
 
   /** Subdirectory of the VFS's space where "opaque" (randomly-named)
       files are stored. Changing this effectively invalidates the data
@@ -329,6 +355,7 @@ globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
     xOpen: function f(pVfs, zName, pFile, flags, pOutFlags){
       const pool = getPoolForVfs(pVfs);
       try{
+        flags &= ~FLAG_COMPUTE_DIGEST_V2;
         pool.log(`xOpen ${wasm.cstrToJs(zName)} ${flags}`);
         // First try to open a path that already exists in the file system.
         const path = (zName && wasm.peek8(zName))
@@ -631,7 +658,8 @@ globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
 
       const fileDigest = new Uint32Array(HEADER_DIGEST_SIZE / 4);
       sah.read(fileDigest, {at: HEADER_OFFSET_DIGEST});
-      const compDigest = this.computeDigest(this.#apBody);
+      const compDigest = this.computeDigest(this.#apBody, flags);
+      //console.warn("getAssociatedPath() compDigest",compDigest);
       if(fileDigest.every((v,i) => v===compDigest[i])){
         // Valid digest
         const pathBytes = this.#apBody.findIndex((v)=>0===v);
@@ -662,10 +690,17 @@ globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
       if(HEADER_MAX_PATH_SIZE <= enc.written + 1/*NUL byte*/){
         toss("Path too long:",path);
       }
+      if(path && flags){
+        /* When re-writing files, update their digest, if needed,
+           to v2. We continue to use v1 for the (!path) case
+           (empty files) because there's little reason not to
+           use a digest of 0 for empty entries. */
+        flags |= FLAG_COMPUTE_DIGEST_V2;
+      }
       this.#apBody.fill(0, enc.written, HEADER_MAX_PATH_SIZE);
       this.#dvBody.setUint32(HEADER_OFFSET_FLAGS, flags);
-
-      const digest = this.computeDigest(this.#apBody);
+      const digest = this.computeDigest(this.#apBody, flags);
+      //console.warn("setAssociatedPath(",path,") digest",digest);
       sah.write(this.#apBody, {at: 0});
       sah.write(digest, {at: HEADER_OFFSET_DIGEST});
       sah.flush();
@@ -686,13 +721,20 @@ globalThis.sqlite3ApiBootstrap.initializers.push(function(sqlite3){
        metadata for each file as a validation check. Changing this
        algorithm invalidates all existing databases for this VFS, so
        don't do that.
+
+       See the docs for FLAG_COMPUTE_DIGEST_V2 for more details.
     */
-    computeDigest(byteArray){
+    computeDigest(byteArray, fileFlags){
       let h1 = 0xdeadbeef;
       let h2 = 0x41c6ce57;
-      for(const v of byteArray){
-        h1 = 31 * h1 + (v * 307);
-        h2 = 31 * h2 + (v * 307);
+      if( fileFlags & FLAG_COMPUTE_DIGEST_V2 ){
+        for(const v of byteArray){
+          h1 = Math.imul(h1 ^ v, 2654435761);
+          h2 = Math.imul(h2 ^ v, 104729);
+        }
+      }else{
+        /* this is what the buggy legacy computation worked out to */
+        h1 = h2 = 0;
       }
       return new Uint32Array([h1>>>0, h2>>>0]);
     }
index 880edcec1dd10bb9d0022dec9672886b481f917b..6d603752becbf889517f720f8874051541d9778f 100644 (file)
@@ -3492,7 +3492,7 @@ globalThis.sqlite3InitModule = sqlite3InitModule;
           });
           db.exec([
             "create table t(a);",
-            "insert into t(a) values(1),(2),(3);",
+            "insert into t(a) values(1),(2),(1);",
             "select auxtest(a,a), auxtest(a,a) from t order by a"
           ]);
         }finally{
index 54fc8aa8ae4551f1164fa28bc87ede8fe4eca71b..05212cec00a0f4e5f1d38369cf0709e0382b10a4 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Add\sa\smore\scomplete\stest\sfor\s[76c8435a]\sand\sadd\ssome\scommentary\sabout\s(A)\sthe\sinability\sto\sautomatically\sclean\sup\sautomatically-generated\sWASM\sproxy\sfunctions\sfor\ssqlite3_set_auxdata()\sdestructors\sand\s(B)\show\sto\sdeal\swith\s(A)\sto\savoid\sleaking\sWASM\sproxy\sfunctions.
-D 2025-02-03T14:55:56.185
+C Initial\swork\son\sa\sfix\sfor\sthe\sSAHPool\sVFS's\seffectively-no-op\sdigest\scalculation,\sas\sreported\sin\s[https://github.com/sqlite/sqlite-wasm/issues/97|ticket\s#97\sof\sthe\sdownstream\snpm\ssubproject].\sThis\srequires\smore\stesting\salongside\sdatabases\screated\sbefore\sthis\sversion\sto\sensure\sthat\sit's\sbackwards-compatible.
+D 2025-02-03T16:26:30.488
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md e108e1e69ae8e8a59e93c455654b8ac9356a11720d3345df2a4743e9590fb20d
@@ -645,7 +645,7 @@ F ext/wasm/api/sqlite3-api-worker1.c-pp.js 5cc22a3c0d52828cb32aad8691488719f47d2
 F ext/wasm/api/sqlite3-license-version-header.js 0c807a421f0187e778dc1078f10d2994b915123c1223fe752b60afdcd1263f89
 F ext/wasm/api/sqlite3-opfs-async-proxy.js 3774befd97cd1a5e2895c8225a894aad946848c6d9b4028acc988b5d123475af
 F ext/wasm/api/sqlite3-vfs-helper.c-pp.js 3f828cc66758acb40e9c5b4dcfd87fd478a14c8fb7f0630264e6c7fa0e57515d
-F ext/wasm/api/sqlite3-vfs-opfs-sahpool.c-pp.js bb5e96cd0fd6e1e54538256433f1c60a4e3095063c4d1a79a8a022fc59be9571
+F ext/wasm/api/sqlite3-vfs-opfs-sahpool.c-pp.js 2635b4c7f63a8ab496bcff91cb33a890d060e979c34d34e5c782d970fdbce36d
 F ext/wasm/api/sqlite3-vfs-opfs.c-pp.js 9b86ca2d8276cf919fbc9ba2a10e9786033b64f92c2db844d951804dee6c4b4e
 F ext/wasm/api/sqlite3-vtab-helper.c-pp.js e809739d71e8b35dfe1b55d24d91f02d04239e6aef7ca1ea92a15a29e704f616
 F ext/wasm/api/sqlite3-wasm.c 6f9d8529072d072359cd22dc5dfb0572c524684686569cfbd0f9640d7619fc10
@@ -696,7 +696,7 @@ F ext/wasm/test-opfs-vfs.html 1f2d672f3f3fce810dfd48a8d56914aba22e45c6834e262555
 F ext/wasm/test-opfs-vfs.js 1618670e466f424aa289859fe0ec8ded223e42e9e69b5c851f809baaaca1a00c
 F ext/wasm/tester1-worker.html ebc4b820a128963afce328ecf63ab200bd923309eb939f4110510ab449e9814c
 F ext/wasm/tester1.c-pp.html 1c1bc78b858af2019e663b1a31e76657b73dc24bede28ca92fbe917c3a972af2
-F ext/wasm/tester1.c-pp.js fb8d0761daaa69bd40c8253cc2d6c8c37ada97e1751b7f07af7369842ba2aeae
+F ext/wasm/tester1.c-pp.js 005a94be87b888ca33aff130aeaef58045781ebfb92e559063d14e0fee68c9bf
 F ext/wasm/tests/opfs/concurrency/index.html 657578a6e9ce1e9b8be951549ed93a6a471f4520a99e5b545928668f4285fb5e
 F ext/wasm/tests/opfs/concurrency/test.js d08889a5bb6e61937d0b8cbb78c9efbefbf65ad09f510589c779b7cc6a803a88
 F ext/wasm/tests/opfs/concurrency/worker.js 0a8c1a3e6ebb38aabbee24f122693f1fb29d599948915c76906681bb7da1d3d2
@@ -2209,8 +2209,11 @@ F tool/version-info.c 3b36468a90faf1bbd59c65fd0eb66522d9f941eedd364fabccd7227350
 F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7
 F tool/warnings.sh 49a486c5069de041aedcbde4de178293e0463ae9918ecad7539eedf0ec77a139
 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
-P 91ef45fc2902e46813366ec6b8317209f39f10e4a23c3808e33aceedab9da6c7
-R f2c90877762eddda2efda339cfb2ee34
+P d693c2dddbd10a2e0b77893b04b11502e30b768f1b06814105f7f35172845fb9
+R f5d8b5467421514674c485e5d9d6f1f0
+T *branch * sahpool-digest
+T *sym-sahpool-digest *
+T -sym-trunk * Cancelled\sby\sbranch.
 U stephan
-Z 6cfffa02c18a4e52a298c977368cc8d7
+Z fa5f726034dfcd33aa64a420eace023f
 # Remove this line to create a well-formed Fossil manifest.
index c7225ebda152698289edd68ea49eea46c4d88567..0ea45d1c3732b0754c108a309ed2be411ec34e43 100644 (file)
@@ -1 +1 @@
-d693c2dddbd10a2e0b77893b04b11502e30b768f1b06814105f7f35172845fb9
+9234c33f92d92bfddc6211c9c587f1072e70837c0ffe1416ef7d84d59bacd364