Skip to content

Commit ad2bf6f

Browse files
authored
Fix ccall() returning a pointer in CAN_ADDRESS_2GB mode. (#25765)
`ccall()` returned pointers as negative numbers in CAN_ADDRESS_2GB modes, breaking pointer `begin < end` comparisons.
1 parent 29d207a commit ad2bf6f

File tree

4 files changed

+25
-0
lines changed

4 files changed

+25
-0
lines changed

src/lib/libccall.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ addToLibrary({
5353
}
5454
#if MEMORY64
5555
if (returnType === 'pointer') return Number(ret);
56+
#elif CAN_ADDRESS_2GB
57+
if (returnType === 'pointer') return ret >>> 0;
5658
#endif
5759
if (returnType === 'boolean') return Boolean(ret);
5860
return ret;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <emscripten.h>
2+
#include <stdio.h>
3+
4+
int g_data;
5+
6+
EMSCRIPTEN_KEEPALIVE void* getDataPtr() {
7+
return &g_data;
8+
}
9+
10+
int main() {
11+
EM_ASM({
12+
var ptr = ccall("getDataPtr", 'pointer');
13+
assert(ptr > 0);
14+
});
15+
printf("ok\n");
16+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ok

test/test_core.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7173,6 +7173,12 @@ def test_ccall_cwrap_fast_path(self):
71737173
self.set_setting('EXPORTED_FUNCTIONS', ['_print_bool'])
71747174
self.do_runf('core/test_ccall.cpp', 'true')
71757175

7176+
@no_modularize_instance('ccall is not yet compatible with MODULARIZE=instance')
7177+
def test_ccall_return_pointer(self):
7178+
if self.get_setting('MINIMAL_RUNTIME'):
7179+
self.skipTest('ccall is not available in MINIMAL_RUNTIME')
7180+
self.do_core_test('test_ccall_return_pointer.c', cflags=['-sDEFAULT_LIBRARY_FUNCS_TO_INCLUDE=$ccall'])
7181+
71767182
@no_modularize_instance('uses Module object directly')
71777183
def test_EXPORTED_RUNTIME_METHODS(self):
71787184
self.set_setting('DEFAULT_LIBRARY_FUNCS_TO_INCLUDE', ['$dynCall', '$ASSERTIONS'])

0 commit comments

Comments
 (0)