Skip to content

Commit 12fd275

Browse files
committed
[GR-62733] Avoid rereading code entries of already instantiated wasm modules.
PullRequest: graal/20227
2 parents db977c4 + a2b5659 commit 12fd275

File tree

13 files changed

+172
-97
lines changed

13 files changed

+172
-97
lines changed

wasm/src/org.graalvm.wasm.test/src/org/graalvm/wasm/test/WasmJsApiSuite.java

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,13 @@
5757
import org.graalvm.wasm.ModuleLimits;
5858
import org.graalvm.wasm.WasmConstant;
5959
import org.graalvm.wasm.WasmContext;
60+
import org.graalvm.wasm.WasmFunction;
6061
import org.graalvm.wasm.WasmFunctionInstance;
6162
import org.graalvm.wasm.WasmInstance;
6263
import org.graalvm.wasm.WasmLanguage;
6364
import org.graalvm.wasm.WasmModule;
6465
import org.graalvm.wasm.WasmTable;
66+
import org.graalvm.wasm.WasmType;
6567
import org.graalvm.wasm.api.ByteArrayBuffer;
6668
import org.graalvm.wasm.api.Dictionary;
6769
import org.graalvm.wasm.api.Executable;
@@ -102,6 +104,15 @@
102104
public class WasmJsApiSuite {
103105
private static final String REF_TYPES_OPTION = "wasm.BulkMemoryAndRefTypes";
104106

107+
private static WasmFunctionInstance createWasmFunctionInstance(WasmContext context, byte[] paramTypes, byte[] resultTypes, RootNode functionRootNode) {
108+
WasmModule module = WasmModule.createBuiltin("dummyModule");
109+
module.allocateFunctionType(paramTypes, resultTypes, context.getContextOptions().supportMultiValue());
110+
WasmFunction func = module.declareExportedFunction(0, "dummyFunction");
111+
func.setTarget(functionRootNode.getCallTarget());
112+
WasmInstance moduleInstance = new WasmInstance(context, module, context.environment().getContext());
113+
return new WasmFunctionInstance(moduleInstance, func, functionRootNode.getCallTarget());
114+
}
115+
105116
@Test
106117
public void testCompile() throws IOException {
107118
runTest(context -> {
@@ -212,13 +223,13 @@ public void testInstantiateWithImportTable() throws IOException {
212223
"defaultTable", table
213224
}),
214225
});
215-
wasm.tableWrite(table, 0, new WasmFunctionInstance(context,
226+
wasm.tableWrite(table, 0, createWasmFunctionInstance(context, WasmType.VOID_TYPE_ARRAY, WasmType.I32_TYPE_ARRAY,
216227
new RootNode(context.language()) {
217228
@Override
218229
public Object execute(VirtualFrame frame) {
219230
return 210;
220231
}
221-
}.getCallTarget()));
232+
}));
222233
final WasmInstance instance = moduleInstantiate(wasm, binaryWithTableImport, importObject);
223234
try {
224235
final Object callFirst = WebAssembly.instanceExport(instance, "callFirst");
@@ -419,12 +430,13 @@ public void testGlobalWriteAnyfuncToI32() throws IOException {
419430
final WebAssembly wasm = new WebAssembly(context);
420431
final WasmGlobal global = wasm.globalAlloc(ValueType.i32, true, 0);
421432
try {
422-
wasm.globalWrite(global, new WasmFunctionInstance(context, new RootNode(context.language()) {
423-
@Override
424-
public Object execute(VirtualFrame frame) {
425-
return 0;
426-
}
427-
}.getCallTarget()));
433+
wasm.globalWrite(global, createWasmFunctionInstance(context, WasmType.VOID_TYPE_ARRAY, WasmType.I32_TYPE_ARRAY,
434+
new RootNode(context.language()) {
435+
@Override
436+
public Object execute(VirtualFrame frame) {
437+
return 0;
438+
}
439+
}));
428440
Assert.fail("Should have failed - invalid global type");
429441
} catch (WasmJsApiException e) {
430442
Assert.assertEquals("Type error expected", WasmJsApiException.Kind.TypeError, e.kind());
@@ -606,13 +618,13 @@ public void testExportTableTwice() throws IOException, InterruptedException {
606618
final WasmInstance instance = moduleInstantiate(wasm, exportMemoryTwice, null);
607619
final InteropLibrary lib = InteropLibrary.getUncached();
608620
try {
609-
final Object f = new WasmFunctionInstance(context,
621+
final Object f = createWasmFunctionInstance(context, WasmType.VOID_TYPE_ARRAY, WasmType.I32_TYPE_ARRAY,
610622
new RootNode(context.language()) {
611623
@Override
612624
public Object execute(VirtualFrame frame) {
613625
return 42;
614626
}
615-
}.getCallTarget());
627+
});
616628
final Object writeTable = wasm.readMember("table_write");
617629
final Object readTable = wasm.readMember("table_read");
618630
final Object a = WebAssembly.instanceExport(instance, "a");
@@ -891,14 +903,13 @@ public void testTableInstanceOutOfBoundsSet() throws IOException {
891903
final InteropLibrary lib = InteropLibrary.getUncached();
892904

893905
WasmContext wasmContext = WasmContext.get(null);
894-
final WasmFunctionInstance functionInstance = new WasmFunctionInstance(
895-
wasmContext,
906+
final WasmFunctionInstance functionInstance = createWasmFunctionInstance(wasmContext, WasmType.VOID_TYPE_ARRAY, WasmType.I32_TYPE_ARRAY,
896907
new RootNode(wasmContext.language()) {
897908
@Override
898909
public Object execute(VirtualFrame frame) {
899910
return 42;
900911
}
901-
}.getCallTarget());
912+
});
902913

903914
// We should be able to set element 1.
904915
try {

wasm/src/org.graalvm.wasm/src/org/graalvm/wasm/Assert.java

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 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
@@ -40,43 +40,52 @@
4040
*/
4141
package org.graalvm.wasm;
4242

43-
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
43+
import java.util.Locale;
44+
4445
import org.graalvm.wasm.exception.Failure;
4546
import org.graalvm.wasm.exception.WasmException;
4647

48+
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
49+
4750
public class Assert {
4851

4952
public static void assertByteEqual(byte b1, byte b2, Failure failure) throws WasmException {
5053
if (b1 != b2) {
51-
fail(failure, format("%s: 0x%02X should = 0x%02X", failure.name, b1, b2));
54+
fail(failure, "%s: 0x%02X should = 0x%02X", failure.name, b1, b2);
5255
}
5356
}
5457

5558
public static void assertByteEqual(byte b1, byte b2, String message, Failure failure) throws WasmException {
5659
if (b1 != b2) {
57-
fail(failure, format("%s: 0x%02X should = 0x%02X", message, b1, b2));
60+
fail(failure, "%s: 0x%02X should = 0x%02X", message, b1, b2);
5861
}
5962
}
6063

6164
public static void assertIntEqual(int actual, int expected, Failure failure) throws WasmException {
62-
assertIntEqual(actual, expected, failure.name, failure);
65+
assertIntEqual(actual, expected, failure, failure.name);
6366
}
6467

65-
public static void assertIntEqual(int actual, int expected, String message, Failure failure) throws WasmException {
68+
public static void assertIntEqual(int actual, int expected, Failure failure, String message) throws WasmException {
6669
if (actual != expected) {
67-
fail(failure, format("%s: %d should = %d", message, actual, expected));
70+
fail(failure, "%s: %d should = %d", message, actual, expected);
71+
}
72+
}
73+
74+
public static void assertIntEqual(int actual, int expected, Failure failure, String format, int arg) throws WasmException {
75+
if (actual != expected) {
76+
fail(failure, "%s: %d should = %d", format(format, arg), actual, expected);
6877
}
6978
}
7079

7180
public static void assertIntGreaterOrEqual(int n1, int n2, Failure failure) throws WasmException {
7281
if (n1 < n2) {
73-
fail(failure, format("%s: %d should be >= %d", failure.name, n1, n2));
82+
fail(failure, "%s: %d should be >= %d", failure.name, n1, n2);
7483
}
7584
}
7685

7786
public static void assertIntGreater(int n1, int n2, String message, Failure failure) throws WasmException {
7887
if (n1 <= n2) {
79-
fail(failure, format("%s: %d should be > %d", message, n1, n2));
88+
fail(failure, "%s: %d should be > %d", message, n1, n2);
8089
}
8190
}
8291

@@ -86,7 +95,7 @@ public static void assertIntLessOrEqual(int n1, int n2, Failure failure) throws
8695

8796
public static void assertIntLess(int n1, int n2, Failure failure) throws WasmException {
8897
if (n1 >= n2) {
89-
fail(failure, format("%s: %d should be < %d", failure.name, n1, n2));
98+
fail(failure, "%s: %d should be < %d", failure.name, n1, n2);
9099
}
91100
}
92101

@@ -96,13 +105,25 @@ public static void assertUnsignedIntLess(int n1, int n2, Failure failure) throws
96105

97106
public static void assertUnsignedIntLess(int n1, int n2, Failure failure, String message) throws WasmException {
98107
if (Integer.compareUnsigned(n1, n2) >= 0) {
99-
fail(failure, format("%s: %s should be < %s", message, Integer.toUnsignedString(n1), Integer.toUnsignedString(n2)));
108+
fail(failure, "%s: %s should be < %s", message, Integer.toUnsignedString(n1), Integer.toUnsignedString(n2));
109+
}
110+
}
111+
112+
public static void assertUnsignedIntLess(int n1, int n2, Failure failure, String format, Object arg) throws WasmException {
113+
if (Integer.compareUnsigned(n1, n2) >= 0) {
114+
fail(failure, "%s: %s should be < %s", format(format, arg), Integer.toUnsignedString(n1), Integer.toUnsignedString(n2));
115+
}
116+
}
117+
118+
public static void assertUnsignedIntLess(int n1, int n2, Failure failure, String format, Object arg1, Object arg2) throws WasmException {
119+
if (Integer.compareUnsigned(n1, n2) >= 0) {
120+
fail(failure, "%s: %s should be < %s", format(format, arg1, arg2), Integer.toUnsignedString(n1), Integer.toUnsignedString(n2));
100121
}
101122
}
102123

103124
public static void assertIntLessOrEqual(int n1, int n2, String message, Failure failure) throws WasmException {
104125
if (n1 > n2) {
105-
fail(failure, format("%s: %d should be <= %d", message, n1, n2));
126+
fail(failure, "%s: %d should be <= %d", message, n1, n2);
106127
}
107128
}
108129

@@ -112,7 +133,7 @@ public static void assertUnsignedIntLessOrEqual(int n1, int n2, Failure failure)
112133

113134
public static void assertUnsignedIntLessOrEqual(int n1, int n2, Failure failure, String message) throws WasmException {
114135
if (Integer.compareUnsigned(n1, n2) > 0) {
115-
fail(failure, format("%s: %s should be <= %s", message, Integer.toUnsignedString(n1), Integer.toUnsignedString(n2)));
136+
fail(failure, "%s: %s should be <= %s", message, Integer.toUnsignedString(n1), Integer.toUnsignedString(n2));
116137
}
117138
}
118139

@@ -122,7 +143,7 @@ public static void assertUnsignedLongLess(long n1, long n2, Failure failure) thr
122143

123144
public static void assertUnsignedLongLess(long n1, long n2, Failure failure, String message) throws WasmException {
124145
if (Long.compareUnsigned(n1, n2) >= 0) {
125-
fail(failure, format("%s: %s should be < %s", message, Long.toUnsignedString(n1), Long.toUnsignedString(n2)));
146+
fail(failure, "%s: %s should be < %s", message, Long.toUnsignedString(n1), Long.toUnsignedString(n2));
126147
}
127148
}
128149

@@ -132,7 +153,7 @@ public static void assertUnsignedLongLessOrEqual(long n1, long n2, Failure failu
132153

133154
public static void assertUnsignedLongLessOrEqual(long n1, long n2, Failure failure, String message) throws WasmException {
134155
if (Long.compareUnsigned(n1, n2) > 0) {
135-
fail(failure, format("%s: %s should be <= %s", message, Long.toUnsignedString(n1), Long.toUnsignedString(n2)));
156+
fail(failure, "%s: %s should be <= %s", message, Long.toUnsignedString(n1), Long.toUnsignedString(n2));
136157
}
137158
}
138159

@@ -142,7 +163,7 @@ public static void assertUnsignedIntGreaterOrEqual(int n1, int n2, Failure failu
142163

143164
public static void assertUnsignedIntGreaterOrEqual(int n1, int n2, Failure failure, String message) throws WasmException {
144165
if (Integer.compareUnsigned(n1, n2) < 0) {
145-
fail(failure, format("%s: %s should be >= %s", message, Integer.toUnsignedString(n1), Integer.toUnsignedString(n2)));
166+
fail(failure, "%s: %s should be >= %s", message, Integer.toUnsignedString(n1), Integer.toUnsignedString(n2));
146167
}
147168
}
148169

@@ -152,19 +173,19 @@ public static void assertUnsignedLongGreaterOrEqual(long n1, long n2, Failure fa
152173

153174
public static void assertUnsignedLongGreaterOrEqual(long n1, long n2, Failure failure, String message) throws WasmException {
154175
if (Long.compareUnsigned(n1, n2) < 0) {
155-
fail(failure, format("%s: %s should be >= %s", message, Long.toUnsignedString(n1), Long.toUnsignedString(n2)));
176+
fail(failure, "%s: %s should be >= %s", message, Long.toUnsignedString(n1), Long.toUnsignedString(n2));
156177
}
157178
}
158179

159180
public static void assertLongLessOrEqual(long n1, long n2, Failure failure) throws WasmException {
160181
if (n1 > n2) {
161-
fail(failure, format("%s: %d should be <= %d", failure.name, n1, n2));
182+
fail(failure, "%s: %d should be <= %d", failure.name, n1, n2);
162183
}
163184
}
164185

165186
public static void assertNotNull(Object object, String message, Failure failure) throws WasmException {
166187
if (object == null) {
167-
fail(failure, format("%s: expected a non-null value", message));
188+
fail(failure, "%s: expected a non-null value", message);
168189
}
169190
}
170191

@@ -179,13 +200,28 @@ public static void assertTrue(boolean condition, String message, Failure failure
179200
}
180201

181202
@TruffleBoundary
182-
public static RuntimeException fail(Failure failure, String message, Object... args) throws WasmException {
183-
throw WasmException.format(failure, message, args);
203+
public static RuntimeException fail(Failure failure, String format, Object... args) throws WasmException {
204+
throw WasmException.format(failure, format, args);
205+
}
206+
207+
@TruffleBoundary
208+
public static RuntimeException fail(Failure failure, String format, Object arg) throws WasmException {
209+
throw WasmException.format(failure, format, arg);
210+
}
211+
212+
@TruffleBoundary
213+
public static RuntimeException fail(Failure failure, String format, int arg) throws WasmException {
214+
throw WasmException.format(failure, format, arg);
215+
}
216+
217+
@TruffleBoundary
218+
public static RuntimeException fail(Failure failure, String message) throws WasmException {
219+
throw WasmException.create(failure, message);
184220
}
185221

186222
@TruffleBoundary
187223
private static String format(String format, Object... args) {
188-
return String.format(format, args);
224+
return String.format(Locale.ROOT, format, args);
189225
}
190226

191227
}

0 commit comments

Comments
 (0)