aboutsummaryrefslogtreecommitdiff
path: root/ext/wasm/api
diff options
context:
space:
mode:
authorstephan <stephan@noemail.net>2022-12-03 13:10:58 +0000
committerstephan <stephan@noemail.net>2022-12-03 13:10:58 +0000
commit09c27a59db621e621e32e4e90aa2b564dc84c574 (patch)
treeaa7e165330c25a02ce29a5e108d788ff3c77f8fc /ext/wasm/api
parent54ac04c8315f8ffc66991cea36d6daa580e8ad8b (diff)
downloadsqlite-09c27a59db621e621e32e4e90aa2b564dc84c574.tar.gz
sqlite-09c27a59db621e621e32e4e90aa2b564dc84c574.zip
Rename wasm.xWrap.resultAdapter() X:free entries to X:dealloc for consistency with wasm.dealloc(). Add an undocumented feature to replace wasm.alloc/dealloc/realloc() with the C-standard allocators (after an allocator misuse led down a several-hour rabbit hole trying to discover a mis-free() violation). Related test updates.
FossilOrigin-Name: d9807656f8a7c2a893d3f68ee5592f44826b8e999ae66f7d9000674b5c1b0207
Diffstat (limited to 'ext/wasm/api')
-rw-r--r--ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api1
-rw-r--r--ext/wasm/api/sqlite3-api-prologue.js26
-rw-r--r--ext/wasm/api/sqlite3-wasm.c2
3 files changed, 19 insertions, 10 deletions
diff --git a/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api b/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api
index 1f7908e3b..bbf16215a 100644
--- a/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api
+++ b/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api
@@ -93,3 +93,4 @@ _sqlite3_vfs_register
_sqlite3_vfs_unregister
_malloc
_free
+_realloc
diff --git a/ext/wasm/api/sqlite3-api-prologue.js b/ext/wasm/api/sqlite3-api-prologue.js
index b6ff94dfe..a0e978c95 100644
--- a/ext/wasm/api/sqlite3-api-prologue.js
+++ b/ext/wasm/api/sqlite3-api-prologue.js
@@ -112,12 +112,23 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap(
}
return !!self.BigInt64Array;
})(),
- allocExportName: 'sqlite3_malloc',
- deallocExportName: 'sqlite3_free',
- reallocExportName: 'sqlite3_realloc',
- wasmfsOpfsDir: '/opfs'
+ wasmfsOpfsDir: '/opfs',
+ /**
+ useStdAlloc is just for testing an allocator discrepancy. The
+ docs guarantee that this is false in the canonical builds. For
+ 99% of purposes it doesn't matter which allocators we use, but
+ it becomes significant with, e.g., sqlite3_deserialize()
+ and certain wasm.xWrap.resultAdapter()s.
+ */
+ useStdAlloc: false
}, apiConfig || {});
+ Object.assign(config, {
+ allocExportName: config.useStdAlloc ? 'malloc' : 'sqlite3_malloc',
+ deallocExportName: config.useStdAlloc ? 'free' : 'sqlite3_free',
+ reallocExportName: config.useStdAlloc ? 'realloc' : 'sqlite3_realloc'
+ }, config);
+
[
// If any of these config options are functions, replace them with
// the result of calling that function...
@@ -768,15 +779,12 @@ self.sqlite3ApiBootstrap = function sqlite3ApiBootstrap(
}
wasm.alloc = function f(n){
- const m = f.impl(n);
- if(!m) throw new WasmAllocError("Failed to allocate",n," bytes.");
- return m;
+ return f.impl(n) || WasmAllocError.toss("Failed to allocate",n," bytes.");
};
wasm.alloc.impl = wasm.exports[keyAlloc];
wasm.realloc = function f(m,n){
const m2 = f.impl(m,n);
- if(n && !m2) throw new WasmAllocError("Failed to reallocate",n," bytes.");
- return n ? m2 : 0;
+ return n ? (m2 || WasmAllocError.toss("Failed to reallocate",n," bytes.")) : 0;
};
wasm.realloc.impl = wasm.exports[keyRealloc];
wasm.dealloc = wasm.exports[keyDealloc];
diff --git a/ext/wasm/api/sqlite3-wasm.c b/ext/wasm/api/sqlite3-wasm.c
index cbc449976..f6499243a 100644
--- a/ext/wasm/api/sqlite3-wasm.c
+++ b/ext/wasm/api/sqlite3-wasm.c
@@ -1204,7 +1204,7 @@ void sqlite3_wasm_test_stack_overflow(int recurse){
/* For testing the 'string-free' whwasmutil.xWrap() conversion. */
SQLITE_WASM_KEEP
char * sqlite3_wasm_test_str_hello(int fail){
- char * s = fail ? 0 : (char *)malloc(6);
+ char * s = fail ? 0 : (char *)sqlite3_malloc(6);
if(s){
memcpy(s, "hello", 5);
s[5] = 0;