Skip to content

Commit 5fc3d5f

Browse files
authored
[wasm64] Add casts when makeDynCall returns a pointer (#26879)
This fixes a `UTF8ToString expects a number (got bigint)` crash in our `beforeunload` handler that was occurring on wasm64 only. Fixes: #26876
1 parent 9bbd787 commit 5fc3d5f

5 files changed

Lines changed: 35 additions & 1 deletion

File tree

src/parseTools.mjs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ function makeDynCall(sig, funcPtr, promising = false) {
682682
}
683683
args = args.join(', ');
684684

685+
const needRtnConversion = MEMORY64 && sig[0] == 'p';
685686
const needArgConversion = MEMORY64 && sig.includes('p');
686687
let callArgs = args;
687688
if (needArgConversion) {
@@ -751,7 +752,11 @@ Please update to new syntax.`);
751752
}
752753

753754
if (needArgConversion) {
754-
return `((${args}) => ${getWasmTableEntry}.call(null, ${callArgs}))`;
755+
if (needRtnConversion) {
756+
return `((${args}) => Number(${getWasmTableEntry}.call(null, ${callArgs})))`;
757+
} else {
758+
return `((${args}) => ${getWasmTableEntry}.call(null, ${callArgs}))`;
759+
}
755760
}
756761
return getWasmTableEntry;
757762
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
char* get_string(char* input) {
5+
static char buffer[256];
6+
strcpy(buffer, "hello ");
7+
strcat(buffer, input);
8+
return buffer;
9+
}
10+
11+
void test_ptr_handling(char* (*fnptr)(char*), char* arg);
12+
13+
int main() {
14+
test_ptr_handling(&get_string, "world");
15+
return 0;
16+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
addToLibrary({
2+
test_ptr_handling: function(fnptr, arg) {
3+
// Both 'p' as return type and 'p' as argument type should be handled.
4+
var ptr = {{{ makeDynCall('pp', 'fnptr') }}}(arg);
5+
console.log('ptr type: ' + typeof ptr);
6+
console.log('ptr value: ' + UTF8ToString(ptr));
7+
}
8+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ptr type: number
2+
ptr value: hello world

test/test_core.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7118,6 +7118,9 @@ def test_dyncall_specific(self):
71187118
print(str(extra_args) + ' ' + which)
71197119
self.do_core_test('test_dyncall_specific.c', cflags=['-D' + which] + extra_args)
71207120

7121+
def test_dyncall_ptr_handling(self):
7122+
self.do_core_test('test_dyncall_ptr_handling.c', cflags=['--js-library', test_file('core/test_dyncall_ptr_handling.js')])
7123+
71217124
@parameterized({
71227125
'': ([],),
71237126
'legacy': (['-sDYNCALLS'],),

0 commit comments

Comments
 (0)