Skip to content

Commit 6ca2414

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 6ca2414

9 files changed

+12
-51
lines changed

emcc.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2866,10 +2866,10 @@ def get_full_import_name(name):
28662866

28672867
# 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+
if not settings.MEMORY64 and (settings.INITIAL_MEMORY > 2 * 1024 * 1024 * 1024 or \
28702870
(settings.ALLOW_MEMORY_GROWTH and
28712871
(settings.MAXIMUM_MEMORY < 0 or
2872-
settings.MAXIMUM_MEMORY > 2 * 1024 * 1024 * 1024)):
2872+
settings.MAXIMUM_MEMORY > 2 * 1024 * 1024 * 1024))):
28732873
settings.CAN_ADDRESS_2GB = 1
28742874

28752875
settings.EMSCRIPTEN_VERSION = shared.EMSCRIPTEN_VERSION

src/jsifier.js

+6-2
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
}
@@ -198,7 +201,8 @@ function(${args}) {
198201
const sig = LibraryManager.library[symbol + '__sig'];
199202
const i53abi = LibraryManager.library[symbol + '__i53abi'];
200203
if (sig && ((i53abi && sig.includes('j')) ||
201-
(MEMORY64 && sig.includes('p')))) {
204+
(MEMORY64 && sig.includes('p')) ||
205+
CAN_ADDRESS_2GB)) {
202206
snippet = handleI64Signatures(symbol, snippet, sig, i53abi);
203207
i53ConversionDeps.forEach((d) => deps.push(d))
204208
}

src/library.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,6 @@ mergeInto(LibraryManager.library, {
199199
emscripten_resize_heap: 'ip',
200200
emscripten_resize_heap: (requestedSize) => {
201201
var oldSize = HEAPU8.length;
202-
#if MEMORY64 != 1
203-
requestedSize = requestedSize >>> 0;
204-
#endif
205202
#if ALLOW_MEMORY_GROWTH == 0
206203
#if ABORTING_MALLOC
207204
abortOnCannotGrowMemory(requestedSize);
@@ -3090,7 +3087,6 @@ mergeInto(LibraryManager.library, {
30903087
// Used by wasm-emscripten-finalize to implement STACK_OVERFLOW_CHECK
30913088
__handle_stack_overflow__deps: ['emscripten_stack_get_base', 'emscripten_stack_get_end', '$ptrToString'],
30923089
__handle_stack_overflow: (requested) => {
3093-
requested = requested >>> 0;
30943090
var base = _emscripten_stack_get_base();
30953091
var end = _emscripten_stack_get_end();
30963092
abort(`stack overflow (Attempt to set SP to ${ptrToString(requested)}` +
@@ -3548,6 +3544,9 @@ mergeInto(LibraryManager.library, {
35483544
#if hasExportedSymbol('emscripten_builtin_memalign')
35493545
size = alignMemory(size, {{{ WASM_PAGE_SIZE }}});
35503546
var ptr = _emscripten_builtin_memalign({{{ WASM_PAGE_SIZE }}}, size);
3547+
#if CAN_ADDRESS_2GB
3548+
ptr >>>= 0;
3549+
#endif
35513550
if (!ptr) return 0;
35523551
return zeroMemory(ptr, size);
35533552
#elif ASSERTIONS

src/library_fs.js

-9
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

-9
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

-15
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

-6
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

-3
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)
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6195
1+
6188

0 commit comments

Comments
 (0)