72 lines
3.7 KiB
Java
72 lines
3.7 KiB
Java
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.*;
|
|
import org.objectweb.asm.*;
|
|
import org.objectweb.asm.tree.*;
|
|
|
|
/**
|
|
* Standalone Stage 4 bytecode pass (Zero baseJar dependency):
|
|
* Removes source-only $novoline$booleanArgument(I)Z conversions when the original JVM call consumed an int as Z.
|
|
*/
|
|
public class BooleanArgumentBytecode implements Opcodes {
|
|
private static final String MARKER = "toBooleanArg";
|
|
private static final String CONVERSION = "(I)Z";
|
|
|
|
static void verifyMarker(MethodNode method) {
|
|
var operations = new ArrayList<Integer>();
|
|
for (var insn : method.instructions) if (insn.getOpcode() >= 0) operations.add(insn.getOpcode());
|
|
if ((method.access & (ACC_PRIVATE | ACC_STATIC)) != (ACC_PRIVATE | ACC_STATIC) || !method.desc.equals(CONVERSION)
|
|
|| !operations.equals(List.of(ILOAD, IFEQ, ICONST_1, GOTO, ICONST_0, IRETURN))) {
|
|
throw new IllegalStateException("Invalid boolean conversion marker");
|
|
}
|
|
}
|
|
|
|
private static int restore(Path path, List<String> rows) throws Exception {
|
|
ClassReader reader = new ClassReader(Files.readAllBytes(path));
|
|
var node = new ClassNode();
|
|
reader.accept(node, 0);
|
|
List<MethodNode> markers = node.methods.stream().filter(method -> method.name.equals(MARKER)).toList();
|
|
if (markers.isEmpty()) return 0;
|
|
if (markers.size() != 1) throw new IllegalStateException("Multiple conversion markers: " + node.name);
|
|
verifyMarker(markers.get(0));
|
|
int changes = 0;
|
|
for (MethodNode method : node.methods) for (var insn : method.instructions.toArray()) {
|
|
if (!(insn instanceof MethodInsnNode marker) || !marker.owner.equals(node.name) || !marker.name.equals(MARKER)) continue;
|
|
if (marker.getOpcode() != INVOKESTATIC || !marker.desc.equals(CONVERSION)) {
|
|
throw new IllegalStateException("Invalid marker invocation in " + node.name + "." + method.name);
|
|
}
|
|
AbstractInsnNode next = marker.getNext();
|
|
while (next != null && next.getOpcode() < 0) {
|
|
if (next instanceof FrameNode) throw new IllegalStateException("Stack map between marker and target");
|
|
next = next.getNext();
|
|
}
|
|
if (!(next instanceof MethodInsnNode call)) throw new IllegalStateException("Expected a direct call after the marker");
|
|
Type[] parameters = Type.getArgumentTypes(call.desc);
|
|
if (parameters.length == 0 || !parameters[parameters.length - 1].equals(Type.BOOLEAN_TYPE)) {
|
|
throw new IllegalStateException("Expected a boolean as the last target parameter");
|
|
}
|
|
method.instructions.remove(marker);
|
|
rows.add(node.name + "\t" + method.name + method.desc + "\t" + call.owner + "." + call.name + call.desc);
|
|
changes++;
|
|
}
|
|
if (changes == 0) throw new IllegalStateException("Unused conversion marker: " + node.name);
|
|
node.methods.remove(markers.get(0));
|
|
var writer = new ClassWriter(reader, 0);
|
|
node.accept(writer);
|
|
Files.write(path, writer.toByteArray());
|
|
return changes;
|
|
}
|
|
|
|
public static void main(String[] args) throws Exception {
|
|
int changed = 0;
|
|
var report = new ArrayList<String>(List.of("class\tcaller\toriginal-target"));
|
|
try (var paths = Files.walk(Path.of(args[0]))) {
|
|
for (Path path : paths.filter(p -> p.toString().endsWith(".class")).sorted().toList()) {
|
|
changed += restore(path, report);
|
|
}
|
|
}
|
|
if (args.length > 1) Files.write(Path.of(args[1]), report);
|
|
System.out.println("Preserved the original integer arguments of " + changed + " boolean call sites");
|
|
}
|
|
}
|