Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ But first, read this page (including the small print at the end).
+ [Coding Guidelines](#coding-guidelines)
+ [Continuous Integration](#continuous-integration)
+ [Tests and documentation are not optional](#tests-and-documentation-are-not-optional)
+ [Current status](#current-status)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Section did not exist anymore

* [Reporting an issue](#reporting-an-issue)
* [AI Tool Use Policy](#ai-tool-use-policy)
* [Legal](#legal)
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<p align="center">
<picture>
<img width="200" src="endive.png">
<img width="200" src="endive.png" alt="Endive logo">
</picture>
<br>
<strong>A <a href="https://bytecodealliance.org/">Bytecode Alliance</a> hosted project</strong>
Expand Down
2 changes: 1 addition & 1 deletion cli/src/main/java/run/endive/experimental/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void run() {
var export = instance.export(functionName);
var params = new long[type.params().size()];
for (var i = 0; i < type.params().size(); i++) {
params[i] = Long.valueOf(arguments[i]);
params[i] = arguments[i];

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

params is long[], arguments is int[]. No need for an explicit Long.valueOf call.

}

var result = export.apply(params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ public Map<String, CompilationUnit> generate() {
"applyWithRefs",
NodeList.nodeList(longArrayExpr, refArrayExpr));

if (exportType.returns().size() == 0) {
if (exportType.returns().isEmpty()) {
exportMethod.setType(void.class);
methodBody.addStatement(applyWithRefsCall);
methodBody.addStatement(new ReturnStmt());
Expand Down Expand Up @@ -352,7 +352,7 @@ public Map<String, CompilationUnit> generate() {
new MethodCallExpr(
exportFieldName, "apply", NodeList.nodeList(handleCallArguments));

if (exportType.returns().size() == 0) {
if (exportType.returns().isEmpty()) {
exportMethod.setType(void.class);
methodBody.addStatement(exportApplyHandle).addStatement(new ReturnStmt());
} else if (exportType.returns().size() > 1) {
Expand Down Expand Up @@ -400,7 +400,7 @@ exportApplyHandle, new IntegerLiteralExpr("0")),
});
}

if (importedModules.size() > 0) {
if (!importedModules.isEmpty()) {
var toImportValuesBody = new BlockStmt();
toImportValuesBody.addStatement(
new AssignExpr(
Expand Down Expand Up @@ -552,7 +552,7 @@ exportApplyHandle, new IntegerLiteralExpr("0")),
importsCu.addImport("run.endive.runtime.WasmFunctionHandle");

// Set interface method return type
if (importType.returns().size() == 0) {
if (importType.returns().isEmpty()) {
importMethod.setType(void.class);
} else if (importType.returns().size() == 1) {
importMethod.setType(
Expand All @@ -564,7 +564,7 @@ exportApplyHandle, new IntegerLiteralExpr("0")),

// Build applyWithRefs body
var refsBody = new BlockStmt();
if (importType.returns().size() == 0) {
if (importType.returns().isEmpty()) {
refsBody.addStatement(importApplyHandle);
refsBody.addStatement(
new ReturnStmt(
Expand Down Expand Up @@ -639,7 +639,7 @@ exportApplyHandle, new IntegerLiteralExpr("0")),
// No object refs - use original lambda path
var functionBodyStatement = new BlockStmt();

if (importType.returns().size() == 0) {
if (importType.returns().isEmpty()) {
importMethod.setType(void.class);
functionBodyStatement.addStatement(importApplyHandle);
functionBodyStatement.addStatement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ private void generateAll(Generator generator) throws IOException {
generator.generateMetaWasm(interpretedFunctions);
}

private String expectedMessageContent = "interpreter fallback mode: WASM function index: 2";
private final String expectedMessageContent =
"interpreter fallback mode: WASM function index: 2";

@Test
public void testDefaultInterpreterFallback() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ private String classNameForFuncGroup(String prefix, int funcId) {
private Consumer<ClassVisitor> emitFunctionGroup(int start, int end, String internalClassName) {
return (classWriter) -> {
for (int i = start; i < end; i++) {
FunctionBody body = null;
FunctionBody body;
try {
int funcId = i;
var type = functionTypes.get(funcId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public static int i32_ge_u(int a, int b) {
var prop = System.getProperty("endive.memCopyWorkaround");

if (prop != null) {
memCopyWorkaround = Boolean.valueOf(prop);
memCopyWorkaround = Boolean.parseBoolean(prop);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Boolean.valueOf returns boxed Boolean, parseBoolean returns primitive boolean

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

} else {
memCopyWorkaround = shouldUseMemWorkaround();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public void callLotsOfArgs() throws InterruptedException {
}

@Test
@SuppressWarnings("deprecation")
public void callLotsOfArgsOnDeprecatedAotMachine() throws InterruptedException {
var module = Parser.parse(CorpusResources.getResource("compiled/lots-of-args.wat.wasm"));
var instance =
Expand Down
4 changes: 2 additions & 2 deletions fuzz/src/main/java/run/endive/fuzz/TestResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

public class TestResult {

private String oracleResult;
private String engineResult;
private final String oracleResult;
private final String engineResult;

public TestResult(String oracleResult, String engineResult) {
this.oracleResult = oracleResult;
Expand Down
2 changes: 1 addition & 1 deletion fuzz/src/test/java/run/endive/fuzz/RegressionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private static Stream<Arguments> crashFolders() {
.flatMap(
dir -> {
var dirs = dir.listFiles();
return dirs != null ? Arrays.stream(dirs) : Stream.<File>empty();
return dirs != null ? Arrays.stream(dirs) : Stream.empty();
})
.filter(f -> f.isDirectory() && f.getName().startsWith("crash"))
.filter(f -> new File(f, "test.wasm").exists())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ interface LockWithTimeout {
int lock(Instance instance, int mutexAddr, long expected);
}

private static MemoryLimits memoryLimits = new MemoryLimits(1, 1, true);
private static List<Supplier<Memory>> memories =
private static final MemoryLimits memoryLimits = new MemoryLimits(1, 1, true);
private static final List<Supplier<Memory>> memories =
List.of(
() -> new ByteArrayMemory(memoryLimits),
() -> new ByteBufferMemory(memoryLimits));
Expand All @@ -53,7 +53,7 @@ interface LockWithTimeout {
instBuilder.withMachineFactory(MachineFactoryCompiler::compile),
// build time compiler
(instBuilder) -> instBuilder.withMachineFactory(ThreadsExampleModule::create));
private static List<LockWithTimeout> locks =
private static final List<LockWithTimeout> locks =
List.of(
ThreadsProposalTest::lockMutexWithTimeout,
ThreadsProposalTest::lock64MutexWithTimeout);
Expand Down Expand Up @@ -309,7 +309,7 @@ public void atomicFenceOrder(
() -> {
long a;
do {
a = (long) fencedReadAndVerify.apply()[0];
a = fencedReadAndVerify.apply()[0];

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Redundant: apply()[0] is already a long

} while (a < minIterations);
});
done.set(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public class TestModule {

private WasmModule module;
private final WasmModule module;

private static final String HACK_MATCH_ALL_MALFORMED_EXCEPTION_TEXT =
"Matching keywords to get the WebAssembly testsuite to pass: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* It is an address denoting either a function instance, table instance, memory instance,
* or global instances in the shared store.
*
* See also <a href="https://webassembly.github.io/spec/core/exec/runtime.html#syntax-externval">External Values</a>.
* <p>See also <a href="https://webassembly.github.io/spec/core/exec/runtime.html#syntax-externval">External Values</a>.
*
* @see ExportFunction
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3922,8 +3922,7 @@ private static void pushExceptionArgs(WasmException exception, MStack stack) {
var tagType = exception.instance().type(tag.tagType().typeIdx());
var params = tagType.params();
int slot = 0;
for (int i = 0; i < params.size(); i++) {
var p = params.get(i);
for (ValType p : params) {
if (p.isObjectRef()) {
// This position is a ref (even if the value is null)
stack.pushRef(refArgs != null && slot < refArgs.length ? refArgs[slot] : null);
Expand Down
3 changes: 1 addition & 2 deletions runtime/src/main/java/run/endive/runtime/StackFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ public StackFrame(Instance instance, int funcId, long[] args) {

// initialize codesegment locals.
int j = 0;
for (var i = 0; i < localTypes.size(); i++) {
ValType type = localTypes.get(i);
for (ValType type : localTypes) {
var idx = j + sizeOf(argsTypes);
if (!type.equals(ValType.V128)) {
if (type.isReference()) {
Expand Down
3 changes: 1 addition & 2 deletions runtime/src/main/java/run/endive/runtime/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,8 @@ public ImportValues toImportValues() {
* All the exported functions, globals, memories, and tables are added to the store
* with the given name.
*
* For instance, if a module named "myModule" exports a function
* <p>For instance, if a module named "myModule" exports a function
* named "myFunction", the function will be added to the store with the name "myFunction.myModule".
*
*/
public Store register(String name, Instance instance) {
ExportSection exportSection = instance.module().exportSection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void rwLockSurvivesContention(String name, Supplier<Memory> memorySupplie
final RwLock lock = new RwLock(memorySupplier.get(), 0);
final long deadline = System.currentTimeMillis() + 3_000;

final List<Thread> threads = new ArrayList<Thread>();
final List<Thread> threads = new ArrayList<>();
for (int r = 0; r < 6; r++) {
threads.add(
new Thread(
Expand Down
18 changes: 8 additions & 10 deletions runtime/src/test/java/run/endive/runtime/WasmModuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public void shouldSupportMemoryFactoryOverride() {
return new ByteBufferMemory(limits);
})
.build();
assertEquals(true, memoryCreated.get());
assertTrue(memoryCreated.get());
}

@Test
Expand Down Expand Up @@ -396,17 +396,17 @@ public void shouldConsumeStackLoopOperations() {
assertEquals(factorial(number), result[0]);

// IIUC: 3 values returning from last CALL + 1 result
assertTrue(finalStackSize.get() == 4L);
assertEquals(4L, finalStackSize.get());
}

@Test
public void shouldEasilyObtainExportedEntities() {
var instance = Instance.builder(loadModule("compiled/exports.wat.wasm")).build();

assertNotNull(instance.exports().memory("mem").pages());
assertNotNull(instance.exports().table("tab").size());
assertNotNull(instance.exports().tag("tag").tagType());
assertNotNull(instance.exports().global("glob1").getValue());
instance.exports().memory("mem").pages();
instance.exports().table("tab").size();
instance.exports().tag("tag").tagType();
instance.exports().global("glob1").getValue();
assertNotNull(instance.exports().function("get-1").apply());
}

Expand Down Expand Up @@ -576,6 +576,7 @@ public void timeoutExecution() throws Exception {
ExecutorService service = Executors.newSingleThreadExecutor();
var future = service.submit(() -> function.apply());
assertThrows(TimeoutException.class, () -> future.get(100, TimeUnit.MILLISECONDS));
service.shutdown();
}

// Testing tail call edge cases
Expand Down Expand Up @@ -858,10 +859,7 @@ public void hostFunctionStackOverflowShouldBeWrapped() {
});
var instance =
Instance.builder(loadModule("compiled/host-function.wat.wasm"))
.withImportValues(
ImportValues.builder()
.addFunction(new HostFunction[] {func})
.build())
.withImportValues(ImportValues.builder().addFunction(func).build())
Comment on lines -861 to +862

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the explicit new HostFunction[] { ... } for a varargs call

.build();
var logIt = instance.export("logIt");
var e = assertThrows(WasmEngineException.class, logIt::apply);
Expand Down
10 changes: 3 additions & 7 deletions simd/src/main/java/run/endive/simd/SimdInterpreterMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -1376,13 +1376,9 @@ private static byte addSatU(byte a, byte b) {
}
}

private static long addSatU(short a, Short b) {
private static long addSatU(short a, short b) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this boxing was unintentional?

Side note: This SimdInterpreterMachine also has a bunch of BiFunction<Float, Float, Long> and alike. My naive impression (without any performance testing) would be that this is not so efficient, and custom functional interfaces with primitive float, ... would be more efficient.
What do you think?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this boxing was unintentional?

Correct, thanks for fixing.

this is not so efficient

We haven't been using a lot of SIMD so far, that's an area that would benefit some love(hooking them up in the compiler/s etc.) if you are interested in a bigger challenge 🙂 .
Custom functional interfaces with primitives sounds like an efficient approach, but, I'd measure before jumping on a conclusion.

int result = Short.toUnsignedInt(a) + Short.toUnsignedInt(b);
if (result >= 0xFFFF) {
return 0xFFFF;
} else {
return result;
}
return Math.min(result, 0xFFFF);
}

private static byte subSatS(byte a, byte b) {
Expand Down Expand Up @@ -3000,7 +2996,7 @@ private static void I8x16_SWIZZLE(MStack stack) {
long resultHigh = 0L;

for (int i = 0; i < 16; i++) {
long id = 0;
long id;
if (i < 8) {
id = (idxLow >> (i * 8)) & 0xFFL;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public CompilationUnit generate(String name, Wast wast, String wasmClasspath) {
"store",
new NameExpr("new Store().addImportValues(Spectest.toImportValues())"));

String currentWasmFile = null;
String currentWasmFile;
for (var cmd : wast.commands()) {
switch (cmd.type()) {
case MODULE:
Expand Down Expand Up @@ -428,7 +428,7 @@ private List<Expression> generateAssert(String varName, Command cmd, String modu
.collect(Collectors.toList())
: List.<String>of();
var adaptedArgs =
(args == null || args.size() == 0)
args.isEmpty()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

args is always assigned a non-null value above.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

? ""
: args.stream().collect(Collectors.joining(").add(", ".add(", ")"));
invocationMethod = ".apply(ArgsAdapter.builder()" + adaptedArgs + ".build()" + ")";
Expand Down Expand Up @@ -601,11 +601,11 @@ private void generateAssertThrows(

String wasmFile = getWasmFile(cmd, wasmClasspath);

var assignementStmt = (cmd.text() != null) ? "var exception = " : "";
var assignmentStmt = (cmd.text() != null) ? "var exception = " : "";

var assertThrows =
new NameExpr(
assignementStmt
assignmentStmt
+ "assertThrows("
+ exceptionType
+ ".class, () -> "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public NameExpr toAssertion(String resultVar, String moduleName) {
if (value == null) {
// according to
// https://github.com/WebAssembly/spec/blob/05949f507908aac3ad2a21661b5c39fa013da950/interpreter/script/js.ml#L150
// ref.func should check that its a function, and ref.extern should check the returned
// ref.func should check that it's a function, and ref.extern should check the returned
// reference is not null
switch (type) {
case FUNC_REF:
Expand Down Expand Up @@ -285,14 +285,14 @@ public String shortLaneValue(String v) {

public String intLaneValue(String v) {
var longValue = Long.parseLong(v);
return Integer.toUnsignedString((int) (0xFFFFFFFF & longValue)) + "L";
return Integer.toUnsignedString((int) longValue) + "L";

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems this was redundant; 0xFFFFFFFF selects the lower 32-bits and is therefore equivalent to directly casting to int (?).

Or should this be kept to make the intention explicit?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Direct casting seems correct to me, please add this fix 🙏

}

/**
* Generate assertion for CallResult from applyWithRefs.
*
* Object ref types use cr.refResult(i) and compare with Java null/not-null.
* Numeric types use cr.longResult(i) and compare with expected values.
* <p>Object ref types use {@code cr.refResult(i)} and compare with Java null/not-null.
* Numeric types use {@code cr.longResult(i)} and compare with expected values.
*/
public NameExpr toRefAssertion(String resultVar, String moduleName) {
if (value == null) {
Expand Down Expand Up @@ -392,7 +392,7 @@ public String toArgsValue() {
case EQ_REF:
case I31_REF:
case FUNC_REF:
if (value[0].toString().equals("null")) {
if (value[0].equals("null")) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was calling String#toString

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return "Value.REF_NULL_VALUE";
}
return value[0];
Expand Down
5 changes: 3 additions & 2 deletions test-gen-plugin/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# test-gen-plugin

<internal-usage-only>
> [!WARNING]
> internal-usage-only

A maven plugin that handles the test generation that exercises both the wasm package and the runtime package. Tests are parsed from the [Wasm testsuite](https://github.com/WebAssembly/testsuite) and generate Java JUnit tests.
A Maven plugin that handles the test generation that exercises both the wasm package and the runtime package. Tests are parsed from the [Wasm testsuite](https://github.com/WebAssembly/testsuite) and generate Java JUnit tests.
2 changes: 2 additions & 0 deletions wasi/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
requires static run.endive.annotations;
requires run.endive.log;
requires transitive run.endive.runtime;

// Needed for `javax.annotation.processing.Generated` annotation in generated ModuleFactory
requires static java.compiler;

exports run.endive.wasi;
Expand Down
Loading
Loading