Skip to content

Commit 9a5a328

Browse files
committed
[GR-61846] Backport to 24.2: Handle WebAssembly.Memory sizes >= 2GB when accessing buffer property.
PullRequest: js/3398
2 parents 90547cb + 0129894 commit 9a5a328

File tree

3 files changed

+47
-19
lines changed

3 files changed

+47
-19
lines changed

graal-js/src/com.oracle.truffle.js/src/com/oracle/truffle/js/runtime/builtins/wasm/JSWebAssemblyMemoryObject.java

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -42,7 +42,9 @@
4242

4343
import java.nio.ByteBuffer;
4444

45+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
4546
import com.oracle.truffle.api.interop.InteropLibrary;
47+
import com.oracle.truffle.api.interop.UnsupportedMessageException;
4648
import com.oracle.truffle.api.object.Shape;
4749
import com.oracle.truffle.api.strings.TruffleString;
4850
import com.oracle.truffle.js.runtime.Errors;
@@ -74,22 +76,38 @@ public boolean isShared() {
7476
return shared;
7577
}
7678

77-
public JSArrayBufferObject getBufferObject(JSContext context, JSRealm realm) {
78-
if (bufferObject == null) {
79-
if (!shared) {
80-
bufferObject = JSArrayBuffer.createInteropArrayBuffer(context, realm, wasmMemory);
81-
} else {
82-
synchronized (wasmMemory) {
83-
InteropLibrary lib = InteropLibrary.getUncached();
84-
ByteBuffer buffer = JSInteropUtil.foreignInteropBufferAsByteBuffer(wasmMemory, lib, realm);
85-
bufferObject = JSSharedArrayBuffer.createSharedArrayBuffer(context, realm, buffer);
86-
boolean status = bufferObject.setIntegrityLevel(true, false);
87-
if (!status) {
88-
throw Errors.createTypeError("Failed to set integrity level of buffer object");
89-
}
79+
@TruffleBoundary
80+
private JSArrayBufferObject createBufferObject(JSContext context, JSRealm realm) {
81+
InteropLibrary lib = InteropLibrary.getUncached();
82+
try {
83+
if (lib.getBufferSize(wasmMemory) > Integer.MAX_VALUE) {
84+
throw Errors.createRangeErrorInvalidBufferSize();
85+
}
86+
} catch (UnsupportedMessageException e) {
87+
throw Errors.createTypeErrorInteropException(wasmMemory, e, "WebAssembly.Memory underlying buffer object is not an interop buffer", null);
88+
}
89+
if (!shared) {
90+
return JSArrayBuffer.createInteropArrayBuffer(context, realm, wasmMemory);
91+
} else {
92+
synchronized (wasmMemory) {
93+
ByteBuffer buffer = JSInteropUtil.foreignInteropBufferAsByteBuffer(wasmMemory, lib, realm);
94+
if (buffer == null) {
95+
throw Errors.createTypeError("No ByteBuffer exposed from WebAssembly memory");
96+
}
97+
JSArrayBufferObject bufferObj = JSSharedArrayBuffer.createSharedArrayBuffer(context, realm, buffer);
98+
boolean status = bufferObj.setIntegrityLevel(true, false);
99+
if (!status) {
100+
throw Errors.createTypeError("Failed to set integrity level of buffer object");
90101
}
102+
return bufferObj;
91103
}
92104
}
105+
}
106+
107+
public JSArrayBufferObject getBufferObject(JSContext context, JSRealm realm) {
108+
if (bufferObject == null) {
109+
bufferObject = createBufferObject(context, realm);
110+
}
93111
return bufferObject;
94112
}
95113

graal-js/test/testV8.json

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5721,19 +5721,26 @@
57215721
}, {
57225722
"filePath" : "mjsunit/wasm/memory_1gb_oob.js",
57235723
"status" : "SKIP",
5724-
"comment" : "long-running test, deactivated"
5724+
"statusOverrides" : {
5725+
"POLYGLOT" : "PASS"
5726+
},
5727+
"runInIsolation" : true
57255728
}, {
57265729
"filePath" : "mjsunit/wasm/memory_2gb_oob.js",
57275730
"status" : "SKIP",
57285731
"statusOverrides" : {
5729-
"POLYGLOT" : "PASS"
5730-
}
5732+
"POLYGLOT" : "FAIL"
5733+
},
5734+
"runInIsolation" : true,
5735+
"comment" : "Fails when trying to access the underlying ArrayBuffer of a WebAssembly.Memory. This is because GraalJS ArrayBuffers are currently capped at 2GB (- 1 byte) and this test exercises 2GB large memories."
57315736
}, {
57325737
"filePath" : "mjsunit/wasm/memory_4gb_oob.js",
57335738
"status" : "SKIP",
57345739
"statusOverrides" : {
5735-
"POLYGLOT" : "PASS"
5736-
}
5740+
"POLYGLOT" : "FAIL"
5741+
},
5742+
"runInIsolation" : true,
5743+
"comment" : "Fails when trying to access the underlying ArrayBuffer of a WebAssembly.Memory. This is because GraalJS ArrayBuffers are currently capped at 2GB (- 1 byte) and this test exercises 4GB large memories."
57375744
}, {
57385745
"filePath" : "mjsunit/wasm/module-memory.js",
57395746
"status" : "SKIP",

graal-nodejs/test/es-module/es-module.status

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ test-typescript-module : FAIL
3838
# Unclassified
3939
test-require-module-tla : SKIP
4040

41+
# transient test failure tracked under GR-61384
42+
test-esm-long-path-win : SKIP
43+
4144
[$system==linux || $system==freebsd]
4245
# https://github.com/nodejs/node/issues/47836
4346
test-esm-loader-http-imports: PASS,FLAKY

0 commit comments

Comments
 (0)