Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package run.endive.testing;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.objectweb.asm.Type.getInternalName;
import static run.endive.corpus.WatGenerator.methodTooLarge;

Expand All @@ -14,6 +15,7 @@
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.util.TraceClassVisitor;
import run.endive.compiler.InterpreterFallback;
import run.endive.compiler.internal.Compiler;
import run.endive.compiler.internal.Shaded;
import run.endive.wabt.Wat2Wasm;
Expand All @@ -33,6 +35,29 @@ public void testBigFunc() {
verifyClass(result.classBytes(), true);
}

@Test
public void testFuncWithFarJumpsAndManyLocals() {
var wat = new StringBuilder("(module (func (param i32) (local");
wat.append(" i32".repeat(1500)).append(") (block (br_if 0 (local.get 0))");
for (int i = 1; i <= 3000; i++) {
int local = (i % 1500) + 1;
wat.append(
String.format(
" (local.set %d (i32.add (local.get %d) (i32.const %d)))",
local, local, i));
}
wat.append(")))");
var module = Parser.parse(Wat2Wasm.parse(wat.toString()));

var result =
Compiler.builder(module)
.withInterpreterFallback(InterpreterFallback.FAIL)
.build()
.compile();

assertTrue(result.interpretedFunctions().isEmpty());
}

private static void verifyClass(Map<String, byte[]> classBytes, boolean skipMethodsClss) {
var writer = new StringWriter();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import static run.endive.compiler.internal.CompilerUtil.localType;
import static run.endive.compiler.internal.CompilerUtil.methodNameForFunc;
import static run.endive.compiler.internal.CompilerUtil.methodTypeFor;
import static run.endive.compiler.internal.CompilerUtil.newClassWriter;
import static run.endive.compiler.internal.CompilerUtil.rawMethodTypeFor;
import static run.endive.compiler.internal.CompilerUtil.slotCount;
import static run.endive.compiler.internal.CompilerUtil.valueMethodName;
Expand Down Expand Up @@ -574,7 +575,7 @@ private Consumer<ClassVisitor> emitFunctionGroup(int start, int end, String inte
private byte[] compileClass() {
var internalClassName = internalClassName(className);

ClassWriter binaryWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
ClassWriter binaryWriter = newClassWriter();
ClassVisitor classWriter = shadedClassRemapper(binaryWriter, className);

classWriter.visit(
Expand Down Expand Up @@ -1077,7 +1078,7 @@ private void compileMachineCallWithTailCalls(
}

private void compileMachineCallClass() {
ClassWriter binaryWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
ClassWriter binaryWriter = newClassWriter();
ClassVisitor classWriter = shadedClassRemapper(binaryWriter, className);
String machineCallClassName = internalClassName(className + "MachineCall");

Expand Down Expand Up @@ -1169,7 +1170,7 @@ private void compileMachineCallClass() {

private void compileExtraClass(
ClassCollector collector, String name, Consumer<ClassVisitor> consumer) {
ClassWriter binaryWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
ClassWriter binaryWriter = newClassWriter();
ClassVisitor classWriter = shadedClassRemapper(binaryWriter, className);
String internalClassName = internalClassName(name);
classWriter.visit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Locale;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
Expand Down Expand Up @@ -55,6 +56,30 @@ private CompilerUtil() {}
}
}

// ClassWriter.setComputeLimits exists since ASM 9.11
private static final boolean HAS_COMPUTE_LIMITS = hasComputeLimits();

private static boolean hasComputeLimits() {
try {
ClassWriter.class.getMethod("setComputeLimits", int.class, long.class);

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.

@forax sorry for the disturb again, do you have any better advise?

return true;
} catch (NoSuchMethodException e) {
return false;
}
}

/**
* Returns a {@link ClassWriter} computing stack map frames, with no limit on the work ASM does
* to compute them.
*/
public static ClassWriter newClassWriter() {
var writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
if (HAS_COMPUTE_LIMITS) {
writer.setComputeLimits(Integer.MAX_VALUE, Long.MAX_VALUE);
}
return writer;
}

public static Class<?> jvmType(ValType type) {
switch (type.opcode()) {
case ValType.ID.I32:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.objectweb.asm.Type.getInternalName;
import static run.endive.compiler.internal.CompilerUtil.internalClassName;
import static run.endive.compiler.internal.CompilerUtil.newClassWriter;

import java.io.IOException;
import org.objectweb.asm.ClassReader;
Expand All @@ -21,7 +22,7 @@ private Shader() {}

public static void createShadedClass(String className, ClassCollector collector) {
String shadedClassName = internalClassName(className + "Shaded");
ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
ClassWriter writer = newClassWriter();
ClassVisitor visitor = shadedClassRemapper(writer, className);

visitor =
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<maven.dependency.failOnWarning>true</maven.dependency.failOnWarning>

<approvaltests.version>31.0.0</approvaltests.version>
<asm.version>9.10.1</asm.version>
<asm.version>9.11.1</asm.version>
<!--
OCI tag of the prebuilt cranelift_bridge.wasm that redline/bridge compiles
against, published by .github/workflows/wasm-publish.yaml. Must be valid
Expand Down
Loading