Skip to content

Commit 3e11c0c

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 c043654 commit 3e11c0c

File tree

6 files changed

+20
-28
lines changed

6 files changed

+20
-28
lines changed

src/jsifier.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ function runJSify() {
133133
if (sig[i] == 'j' && i53abi) {
134134
argConvertions += ` ${receiveI64ParamAsI53(name, undefined, false)}\n`;
135135
newArgs.push(defineI64Param(name));
136+
} else if (sig[i] == 'p' && CAN_ADDRESS_2GB) {
137+
argConvertions += ` ${name} >>>= 0;\n`;
138+
newArgs.push(name);
136139
} else {
137140
newArgs.push(name);
138141
}
@@ -191,8 +194,8 @@ function(${args}) {
191194

192195
const sig = LibraryManager.library[symbol + '__sig'];
193196
const i53abi = LibraryManager.library[symbol + '__i53abi'];
194-
if (sig && ((i53abi && sig.includes('j')) ||
195-
(MEMORY64 && sig.includes('p')))) {
197+
if (sig &&
198+
((i53abi && sig.includes('j')) || ((MEMORY64 || CAN_ADDRESS_2GB) && sig.includes('p')))) {
196199
snippet = handleI64Signatures(symbol, snippet, sig, i53abi);
197200
i53ConversionDeps.forEach((d) => deps.push(d))
198201
}

src/library.js

Lines changed: 6 additions & 2 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,7 +203,8 @@ mergeInto(LibraryManager.library, {
199203
emscripten_resize_heap: 'ip',
200204
emscripten_resize_heap: (requestedSize) => {
201205
var oldSize = HEAPU8.length;
202-
#if MEMORY64 != 1
206+
#if !MEMORY64 && !CAN_ADDRESS_2GB
207+
// With CAN_ADDRESS_2GB or MEMORY64, pointers are already unsigned.
203208
requestedSize >>>= 0;
204209
#endif
205210
#if ALLOW_MEMORY_GROWTH == 0
@@ -3090,7 +3095,6 @@ mergeInto(LibraryManager.library, {
30903095
// Used by wasm-emscripten-finalize to implement STACK_OVERFLOW_CHECK
30913096
__handle_stack_overflow__deps: ['emscripten_stack_get_base', 'emscripten_stack_get_end', '$ptrToString'],
30923097
__handle_stack_overflow: (requested) => {
3093-
requested >>>= 0;
30943098
var base = _emscripten_stack_get_base();
30953099
var end = _emscripten_stack_get_end();
30963100
abort(`stack overflow (Attempt to set SP to ${ptrToString(requested)}` +

src/library_fs.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,8 +1132,9 @@ FS.staticInit();` +
11321132
return stream.position;
11331133
},
11341134
read: (stream, buffer, offset, length, position) => {
1135-
#if CAN_ADDRESS_2GB
1136-
offset >>>= 0;
1135+
#if ASSERTIONS
1136+
assert(offset >= 0);
1137+
assert(buffer >= 0);
11371138
#endif
11381139
if (length < 0 || position < 0) {
11391140
throw new FS.ErrnoError({{{ cDefs.EINVAL }}});
@@ -1166,8 +1167,9 @@ FS.staticInit();` +
11661167
return bytesRead;
11671168
},
11681169
write: (stream, buffer, offset, length, position, canOwn) => {
1169-
#if CAN_ADDRESS_2GB
1170-
offset >>>= 0;
1170+
#if ASSERTIONS
1171+
assert(offset >= 0);
1172+
assert(buffer >= 0);
11711173
#endif
11721174
if (length < 0 || position < 0) {
11731175
throw new FS.ErrnoError({{{ cDefs.EINVAL }}});
@@ -1242,8 +1244,9 @@ FS.staticInit();` +
12421244
return stream.stream_ops.mmap(stream, length, position, prot, flags);
12431245
},
12441246
msync: (stream, buffer, offset, length, mmapFlags) => {
1245-
#if CAN_ADDRESS_2GB
1246-
offset >>>= 0;
1247+
#if ASSERTIONS
1248+
assert(offset >= 0);
1249+
assert(buffer >= 0);
12471250
#endif
12481251
if (!stream.stream_ops.msync) {
12491252
return 0;

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_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 >>>= 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 >>>= 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 >>>= 0;
19201917
if (size === {{{ gpu.WHOLE_MAP_SIZE }}}) size = undefined;
19211918

19221919
// `callback` takes (WGPUBufferMapAsyncStatus status, void * userdata)

0 commit comments

Comments
 (0)