Asm-based api comparison tool (wip)

This commit is contained in:
Raven Szewczyk committed 2023-01-10 21:24:30 +00:00
1 parent 06de3197fa
commit ab644d6d43
3 files changed
+227

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
sourceSets {
create("util") {
}
}
dependencies {
utilImplementation("org.ow2.asm:asm:9.4")
utilImplementation("org.ow2.asm:asm-tree:9.4")
utilImplementation("org.apache.commons:commons-lang3:3.12.0")
utilImplementation("commons-io:commons-io:2.11.0")
utilImplementation("commons-collections:commons-collections:3.2.2")
utilImplementation("com.google.guava:guava:31.1-jre")
}
tasks.register("runComparisonTool", JavaExec) {
classpath(sourceSets.util.runtimeClasspath)
mainClass.set("me.eigenraven.lwjgl3ify.ComparisonTool")
minHeapSize = "2G"
maxHeapSize = "2G"
}
afterEvaluate {
def lwjgl2Zips = configurations.named("lwjglMcCompileClasspath").get().resolve().findAll {!it.name.contains("natives") && !it.path.contains("net.java.j")}
def lwjgl3Zips = configurations.named("lwjglModClasspath").get().resolve().findAll {!it.name.contains("natives")}
def lwjgl2Args = lwjgl2Zips.collect {"--2:" + it}
def lwjgl3Args = lwjgl3Zips.collect {"--3:" + it}
def allArgs = lwjgl2Args + lwjgl3Args
tasks.named("runComparisonTool", JavaExec).configure {
args(allArgs)
}
}
@@ -0,0 +1,116 @@
package me.eigenraven.lwjgl3ify;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;
import java.util.TreeMap;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.CloseShieldInputStream;
import org.apache.commons.lang3.StringUtils;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.FieldNode;
import org.objectweb.asm.tree.MethodNode;
public class ApiSet {
public final TreeMap<String, ClassApi> classes = new TreeMap<>();
public void addJar(File jarFile) {
if (!jarFile.exists()) {
throw new UnsupportedOperationException("Input does not exist: " + jarFile);
}
try (FileInputStream fis = FileUtils.openInputStream(jarFile);
BufferedInputStream bis = new BufferedInputStream(fis);
JarInputStream jis = new JarInputStream(bis)) {
JarEntry entry;
while ((entry = jis.getNextJarEntry()) != null) {
if (entry.isDirectory()) {
continue;
}
if (entry.getName().endsWith(".class")) {
byte[] classBytes = IOUtils.toByteArray(CloseShieldInputStream.wrap(jis));
addClassBytes(classBytes);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public void addClassBytes(byte[] bytes) {
ClassReader classReader = new ClassReader(bytes);
ClassNode root = new ClassNode();
classReader.accept(root, ClassReader.EXPAND_FRAMES);
if (root.name.contains("module-info") || root.name.contains("package-info")) {
return;
}
if ((root.access & Opcodes.ACC_PUBLIC) == 0) {
return;
}
String className = root.name;
if (className.startsWith("org/lwjglx/")) {
className = "org/lwjgl/" + StringUtils.removeStart(className, "org/lwjglx/");
}
final ClassApi api = new ClassApi(root);
this.classes.put(className, api);
}
public static class ClassApi {
public final String className;
public final TreeMap<String, MethodNode> methods = new TreeMap<>();
public final TreeMap<String, FieldNode> fields = new TreeMap<>();
public ClassApi(ClassNode node) {
className = node.name;
for (MethodNode method : node.methods) {
if ((method.access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) == 0) {
continue;
}
final String sig = method.name + ":" + method.desc;
methods.put(sig, method);
}
for (FieldNode field : node.fields) {
if ((field.access & (Opcodes.ACC_PUBLIC | Opcodes.ACC_PROTECTED)) == 0) {
continue;
}
final String sig = field.name + ":" + field.desc;
fields.put(sig, field);
}
}
public String dump() {
StringBuilder builder = new StringBuilder(1024);
builder.append("## METHODS\n");
methods.keySet().forEach(key -> {
builder.append(' ');
builder.append(key);
builder.append('\n');
});
builder.append("## FIELDS\n");
fields.keySet().forEach(key -> {
builder.append(' ');
builder.append(key);
builder.append('\n');
});
return builder.toString();
}
}
public String dump() {
StringBuilder builder = new StringBuilder(1024);
for (Map.Entry<String, ClassApi> entry : classes.entrySet()) {
builder.append("##### ");
builder.append(entry.getKey());
builder.append('\n');
builder.append(entry.getValue().dump());
builder.append('\n');
}
return builder.toString();
}
}
@@ -0,0 +1,79 @@
package me.eigenraven.lwjgl3ify;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.objectweb.asm.tree.AbstractInsnNode;
import org.objectweb.asm.tree.MethodInsnNode;
import org.objectweb.asm.tree.MethodNode;
public class ComparisonTool {
public static void main(String[] args) {
new ComparisonTool().run(args);
}
public void run(String[] args) {
new File("run").mkdirs();
final ArrayList<String> gl2Jars = new ArrayList<>();
final ArrayList<String> gl3Jars = new ArrayList<>();
for (String arg : args) {
if (arg.startsWith("--2:")) {
gl2Jars.add(StringUtils.removeStart(arg, "--2:"));
} else if (arg.startsWith("--3:")) {
gl3Jars.add(StringUtils.removeStart(arg, "--3:"));
}
}
final ApiSet gl2 = new ApiSet();
final ApiSet gl3 = new ApiSet();
gl2Jars.stream().forEach(path -> gl2.addJar(new File(path)));
gl3Jars.stream().forEach(path -> gl3.addJar(new File(path)));
try {
FileUtils.writeStringToFile(new File("run/gl2dump.txt"), gl2.dump(), StandardCharsets.UTF_8);
FileUtils.writeStringToFile(new File("run/gl3dump.txt"), gl3.dump(), StandardCharsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException(e);
}
for (Map.Entry<String, ApiSet.ClassApi> gl2Entry : gl2.classes.entrySet()) {
final ApiSet.ClassApi gl2Class = gl2Entry.getValue();
final ApiSet.ClassApi gl3Class = gl3.classes.get(gl2Entry.getKey());
if (gl3Class == null) {
System.out.println("Missing gl3: " + gl2Entry.getKey());
continue;
}
System.out.println(gl2Entry.getKey());
for (Map.Entry<String, MethodNode> gl2Method : gl2Class.methods.entrySet()) {
final MethodNode gl3Method = gl3Class.methods.get(gl2Method.getKey());
String nativeCallName = null;
if (gl2Method.getKey().startsWith("glGetProgramStage")) {
System.out.println('x');
}
for (AbstractInsnNode insn : gl2Method.getValue().instructions) {
if (insn instanceof MethodInsnNode) {
MethodInsnNode min = (MethodInsnNode) insn;
if (min.name.startsWith("ngl")) {
nativeCallName = min.name;
}
}
}
if (gl3Method == null) {
if (nativeCallName != null) {
final String maybeNewKey = nativeCallName.substring(1) + ":" + gl2Method.getValue().desc;
final MethodNode newGl3Method = gl3Class.methods.get(maybeNewKey);
if (newGl3Method != null) {
System.out.println(" * " + gl2Method.getKey() + " --> " + maybeNewKey);
continue;
}
}
System.out.println(" - " + gl2Method.getKey());
} else {
// System.out.println(" + " + gl2Method.getKey());
}
}
}
}
}