Skip to content

Instantly share code, notes, and snippets.

@Col-E
Created April 19, 2022 12:51
Show Gist options
  • Save Col-E/4239735d84247f2a8cc6b480dc3cdeb9 to your computer and use it in GitHub Desktop.
Save Col-E/4239735d84247f2a8cc6b480dc3cdeb9 to your computer and use it in GitHub Desktop.
SSVM/emulation of JDK fails with since NoSuchMethodException should be NoSuchFieldException
package me.coley.recaf.ssvm;
import dev.xdark.ssvm.VirtualMachine;
import dev.xdark.ssvm.api.VMInterface;
import dev.xdark.ssvm.execution.ExecutionContext;
import dev.xdark.ssvm.execution.Result;
import dev.xdark.ssvm.execution.VMException;
import dev.xdark.ssvm.fs.FileDescriptorManager;
import dev.xdark.ssvm.fs.HostFileDescriptorManager;
import dev.xdark.ssvm.mirror.InstanceJavaClass;
import dev.xdark.ssvm.util.VMHelper;
import dev.xdark.ssvm.value.IntValue;
import dev.xdark.ssvm.value.Value;
public class Test {
private static final int NF_UNSAFE = 10;
private static VirtualMachine vm;
public static void main(String[] args) {
vm = new VirtualMachine() {
@Override
protected FileDescriptorManager createFileDescriptorManager() {
return new HostFileDescriptorManager();
}
};
vm.bootstrap();
// Invoking the following fails due to 'wrong' ref kind?
// java/lang/invoke/DirectMethodHandle
// private static NamedFunction createFunction(byte func)
ExecutionContext[] lastCtx = new ExecutionContext[1];
VMHelper helper = vm.getHelper();
VMInterface vmi = vm.getInterface();
InstanceJavaClass stackTraceElement = (InstanceJavaClass) vm.findBootstrapClass(StackTraceElement.class.getName().replace('.', '/'));
vmi.setInvoker(stackTraceElement, "isHashedInJavaBase", "(Ljava/lang/Module;)Z", ctx -> {
ctx.setResult(IntValue.ZERO);
return Result.ABORT;
});
vmi.registerMethodEnter(ctx -> lastCtx[0] = ctx);
try {
helper.invokeStatic((InstanceJavaClass) vm.findBootstrapClass("java/lang/invoke/DirectMethodHandle"),
"createFunction",
"(B)Ljava/lang/invoke/LambdaForm$NamedFunction;",
new Value[0], new Value[]{IntValue.of(NF_UNSAFE)});
} catch (VMException ex) {
ExecutionContext ctx = lastCtx[0];
if (ctx != null) {
helper.invokeVirtual("printStackTrace", "()V", new Value[0], new Value[]{ex.getOop()});
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment