Skip to content

Commit 1206ca2

Browse files
committed
Automatically convert i32 pointer args to unsigned when CAN_ADDRESS_2GB is set
This extends the use automatically-generated JS wrappers to handle conversion of incoming i32 pointer to u32 JS numbers. This is only needed when CAN_ADDRESS_2GB is set. See #19737
1 parent 818523a commit 1206ca2

File tree

9 files changed

+21
-53
lines changed

9 files changed

+21
-53
lines changed

emcc.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2864,12 +2864,16 @@ def get_full_import_name(name):
28642864
'emscripten_stack_get_base',
28652865
'emscripten_stack_get_end']
28662866

2867-
# check if we can address the 2GB mark and higher: either if we start at
2867+
# Check if we can address the 2GB mark and higher: either if we start at
28682868
# 2GB, or if we allow growth to either any amount or to 2GB or more.
2869-
if settings.INITIAL_MEMORY > 2 * 1024 * 1024 * 1024 or \
2869+
# Since `CAN_ADDRESS_2GB` controls how i32 pointers are interpreted it is
2870+
# not needed under wasm64 (where pointers come threw and i63, i52).
2871+
# Indeed, attempted to interpret a wasm64 pointer as unsigned using `>>> 0`
2872+
# will loose information.
2873+
if not settings.MEMORY64 and (settings.INITIAL_MEMORY > 2 * 1024 * 1024 * 1024 or
28702874
(settings.ALLOW_MEMORY_GROWTH and
28712875
(settings.MAXIMUM_MEMORY < 0 or
2872-
settings.MAXIMUM_MEMORY > 2 * 1024 * 1024 * 1024)):
2876+
settings.MAXIMUM_MEMORY > 2 * 1024 * 1024 * 1024))):
28732877
settings.CAN_ADDRESS_2GB = 1
28742878

28752879
settings.EMSCRIPTEN_VERSION = shared.EMSCRIPTEN_VERSION

src/jsifier.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ function runJSify() {
128128
for (let i = 1; i < sig.length; i++) {
129129
const name = argNames[i - 1];
130130
if (!name) {
131-
error(`convertPointerParams: missing name for argument ${i} in ${symbol}`);
131+
error(`handleI64Signatures: missing name for argument ${i} in ${symbol}`);
132132
return snippet;
133133
}
134134
if (WASM_BIGINT) {
@@ -139,6 +139,9 @@ function runJSify() {
139139
if (sig[i] == 'j' && i53abi) {
140140
argConvertions += ` ${receiveI64ParamAsI53(name, undefined, false)}\n`;
141141
newArgs.push(defineI64Param(name));
142+
} else if (sig[i] == 'p' && CAN_ADDRESS_2GB) {
143+
argConvertions += ` ${name} >>>= 0;\n`;
144+
newArgs.push(name);
142145
} else {
143146
newArgs.push(name);
144147
}
@@ -197,8 +200,8 @@ function(${args}) {
197200

198201
const sig = LibraryManager.library[symbol + '__sig'];
199202
const i53abi = LibraryManager.library[symbol + '__i53abi'];
200-
if (sig && ((i53abi && sig.includes('j')) ||
201-
(MEMORY64 && sig.includes('p')))) {
203+
if (sig &&
204+
((i53abi && sig.includes('j')) || ((MEMORY64 || CAN_ADDRESS_2GB) && sig.includes('p')))) {
202205
snippet = handleI64Signatures(symbol, snippet, sig, i53abi);
203206
i53ConversionDeps.forEach((d) => deps.push(d))
204207
}

src/library.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ mergeInto(LibraryManager.library, {
2424
$ptrToString: (ptr) => {
2525
#if ASSERTIONS
2626
assert(typeof ptr === 'number');
27+
#endif
28+
#if !CAN_ADDRESS_2GB && !MEMORY64
29+
// With CAN_ADDRESS_2GB or MEMORY64, pointers are already unsigned.
30+
ptr >>>= 0;
2731
#endif
2832
return '0x' + ptr.toString(16).padStart(8, '0');
2933
},
@@ -199,9 +203,6 @@ mergeInto(LibraryManager.library, {
199203
emscripten_resize_heap: 'ip',
200204
emscripten_resize_heap: (requestedSize) => {
201205
var oldSize = HEAPU8.length;
202-
#if MEMORY64 != 1
203-
requestedSize = requestedSize >>> 0;
204-
#endif
205206
#if ALLOW_MEMORY_GROWTH == 0
206207
#if ABORTING_MALLOC
207208
abortOnCannotGrowMemory(requestedSize);
@@ -3090,7 +3091,6 @@ mergeInto(LibraryManager.library, {
30903091
// Used by wasm-emscripten-finalize to implement STACK_OVERFLOW_CHECK
30913092
__handle_stack_overflow__deps: ['emscripten_stack_get_base', 'emscripten_stack_get_end', '$ptrToString'],
30923093
__handle_stack_overflow: (requested) => {
3093-
requested = requested >>> 0;
30943094
var base = _emscripten_stack_get_base();
30953095
var end = _emscripten_stack_get_end();
30963096
abort(`stack overflow (Attempt to set SP to ${ptrToString(requested)}` +
@@ -3548,6 +3548,9 @@ mergeInto(LibraryManager.library, {
35483548
#if hasExportedSymbol('emscripten_builtin_memalign')
35493549
size = alignMemory(size, {{{ WASM_PAGE_SIZE }}});
35503550
var ptr = _emscripten_builtin_memalign({{{ WASM_PAGE_SIZE }}}, size);
3551+
#if CAN_ADDRESS_2GB
3552+
ptr >>>= 0;
3553+
#endif
35513554
if (!ptr) return 0;
35523555
return zeroMemory(ptr, size);
35533556
#elif ASSERTIONS

src/library_fs.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,9 +1132,6 @@ FS.staticInit();` +
11321132
return stream.position;
11331133
},
11341134
read: (stream, buffer, offset, length, position) => {
1135-
#if CAN_ADDRESS_2GB
1136-
offset >>>= 0;
1137-
#endif
11381135
if (length < 0 || position < 0) {
11391136
throw new FS.ErrnoError({{{ cDefs.EINVAL }}});
11401137
}
@@ -1166,9 +1163,6 @@ FS.staticInit();` +
11661163
return bytesRead;
11671164
},
11681165
write: (stream, buffer, offset, length, position, canOwn) => {
1169-
#if CAN_ADDRESS_2GB
1170-
offset >>>= 0;
1171-
#endif
11721166
if (length < 0 || position < 0) {
11731167
throw new FS.ErrnoError({{{ cDefs.EINVAL }}});
11741168
}
@@ -1242,9 +1236,6 @@ FS.staticInit();` +
12421236
return stream.stream_ops.mmap(stream, length, position, prot, flags);
12431237
},
12441238
msync: (stream, buffer, offset, length, mmapFlags) => {
1245-
#if CAN_ADDRESS_2GB
1246-
offset >>>= 0;
1247-
#endif
12481239
if (!stream.stream_ops.msync) {
12491240
return 0;
12501241
}

src/library_memfs.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,6 @@ mergeInto(LibraryManager.library, {
105105
// May allocate more, to provide automatic geometric increase and amortized linear performance appending writes.
106106
// Never shrinks the storage.
107107
expandFileStorage: function(node, newCapacity) {
108-
#if CAN_ADDRESS_2GB
109-
newCapacity >>>= 0;
110-
#endif
111108
var prevCapacity = node.contents ? node.contents.length : 0;
112109
if (prevCapacity >= newCapacity) return; // No need to expand, the storage was already large enough.
113110
// Don't expand strictly to the given requested limit if it's only a very small increase, but instead geometrically grow capacity.
@@ -123,9 +120,6 @@ mergeInto(LibraryManager.library, {
123120

124121
// Performs an exact resize of the backing file storage to the given size, if the size is not exactly this, the storage is fully reallocated.
125122
resizeFileStorage: function(node, newSize) {
126-
#if CAN_ADDRESS_2GB
127-
newSize >>>= 0;
128-
#endif
129123
if (node.usedBytes == newSize) return;
130124
if (newSize == 0) {
131125
node.contents = null; // Fully decommit when requesting a resize to zero.
@@ -360,9 +354,6 @@ mergeInto(LibraryManager.library, {
360354
if (!ptr) {
361355
throw new FS.ErrnoError({{{ cDefs.ENOMEM }}});
362356
}
363-
#if CAN_ADDRESS_2GB
364-
ptr >>>= 0;
365-
#endif
366357
HEAP8.set(contents, ptr);
367358
}
368359
return { ptr, allocated };

src/library_strings.js

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ mergeInto(LibraryManager.library, {
2727
$UTF8ArrayToString__deps: ['$UTF8Decoder'],
2828
#endif
2929
$UTF8ArrayToString: (heapOrArray, idx, maxBytesToRead) => {
30-
#if CAN_ADDRESS_2GB
31-
idx >>>= 0;
32-
#endif
3330
var endIdx = idx + maxBytesToRead;
3431
#if TEXTDECODER
3532
var endPtr = idx;
@@ -118,9 +115,6 @@ mergeInto(LibraryManager.library, {
118115
#if ASSERTIONS
119116
assert(typeof ptr == 'number');
120117
#endif
121-
#if CAN_ADDRESS_2GB
122-
ptr >>>= 0;
123-
#endif
124118
#if TEXTDECODER == 2
125119
if (!ptr) return '';
126120
var maxPtr = ptr + maxBytesToRead;
@@ -154,9 +148,6 @@ mergeInto(LibraryManager.library, {
154148
* @return {number} The number of bytes written, EXCLUDING the null terminator.
155149
*/
156150
$stringToUTF8Array: (str, heap, outIdx, maxBytesToWrite) => {
157-
#if CAN_ADDRESS_2GB
158-
outIdx >>>= 0;
159-
#endif
160151
#if ASSERTIONS
161152
assert(typeof str === 'string');
162153
#endif
@@ -262,9 +253,6 @@ mergeInto(LibraryManager.library, {
262253
// emscripten HEAP, returns a copy of that string as a Javascript String
263254
// object.
264255
$AsciiToString: (ptr) => {
265-
#if CAN_ADDRESS_2GB
266-
ptr >>>= 0;
267-
#endif
268256
var str = '';
269257
while (1) {
270258
var ch = {{{ makeGetValue('ptr++', 0, 'u8') }}};
@@ -429,9 +417,6 @@ mergeInto(LibraryManager.library, {
429417
// output, not even the null terminator.
430418
// Returns the number of bytes written, EXCLUDING the null terminator.
431419
$stringToUTF32: (str, outPtr, maxBytesToWrite) => {
432-
#if CAN_ADDRESS_2GB
433-
outPtr >>>= 0;
434-
#endif
435420
#if ASSERTIONS
436421
assert(outPtr % 4 == 0, 'Pointer passed to stringToUTF32 must be aligned to four bytes!');
437422
#endif

src/library_syscall.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,6 @@ var SyscallsLibrary = {
8080
// MAP_PRIVATE calls need not to be synced back to underlying fs
8181
return 0;
8282
}
83-
#if CAN_ADDRESS_2GB
84-
addr >>>= 0;
85-
#endif
8683
var buffer = HEAPU8.slice(addr, addr + len);
8784
FS.msync(stream, buffer, offset, len, flags);
8885
},
@@ -142,9 +139,6 @@ var SyscallsLibrary = {
142139
var res = FS.mmap(stream, len, offset, prot, flags);
143140
var ptr = res.ptr;
144141
{{{ makeSetValue('allocated', 0, 'res.allocated', 'i32') }}};
145-
#if CAN_ADDRESS_2GB
146-
ptr >>>= 0;
147-
#endif
148142
{{{ makeSetValue('addr', 0, 'ptr', '*') }}};
149143
return 0;
150144
#else // no filesystem support; report lack of support

src/library_webgpu.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,7 +1841,6 @@ var LibraryWebGPU = {
18411841

18421842
if (size === 0) warnOnce('getMappedRange size=0 no longer means WGPU_WHOLE_MAP_SIZE');
18431843

1844-
size = size >>> 0;
18451844
if (size === {{{ gpu.WHOLE_MAP_SIZE }}}) size = undefined;
18461845

18471846
var mapped;
@@ -1875,7 +1874,6 @@ var LibraryWebGPU = {
18751874

18761875
if (size === 0) warnOnce('getMappedRange size=0 no longer means WGPU_WHOLE_MAP_SIZE');
18771876

1878-
size = size >>> 0;
18791877
if (size === {{{ gpu.WHOLE_MAP_SIZE }}}) size = undefined;
18801878

18811879
if (bufferWrapper.mapMode !== {{{ gpu.MapMode.Write }}}) {
@@ -1916,7 +1914,6 @@ var LibraryWebGPU = {
19161914
bufferWrapper.onUnmap = [];
19171915
var buffer = bufferWrapper.object;
19181916

1919-
size = size >>> 0;
19201917
if (size === {{{ gpu.WHOLE_MAP_SIZE }}}) size = undefined;
19211918

19221919
// `callback` takes (WGPUBufferMapAsyncStatus status, void * userdata)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6195
1+
6188

0 commit comments

Comments
 (0)