use of org.mapleir.asm.MethodNode in project maple-ir by LLVM-but-worse.
the class CleanBoot method main.
public static void main(String[] args) throws Exception {
ClassNode cn = ClassHelper.create(new FileInputStream(new File("MemeIn.class")));
IRCache irFactory = new IRCache();
for (MethodNode mn : cn.getMethods()) {
// if (!mn.getName().equals("merge"))
// continue;
// if (mn.getName().equals("merge"))
// System.out.println(InsnListUtils.insnListToString(mn.node.instructions));
ControlFlowGraph cfg = irFactory.getNonNull(mn);
// if (mn.getName().equals("merge"))
// System.out.println(cfg);
// if (mn.getName().equals("merge"))
// CFGUtils.easyDumpCFG(cfg, "pre-destruct");
cfg.verify();
BoissinotDestructor.leaveSSA(cfg);
// if (mn.getName().equals("merge"))
// CFGUtils.easyDumpCFG(cfg, "pre-reaalloc");
LocalsReallocator.realloc(cfg);
// if (mn.getName().equals("merge"))
// CFGUtils.easyDumpCFG(cfg, "post-reaalloc");
// System.out.println(cfg);
cfg.verify();
System.out.println("Rewriting " + mn.getName());
(new ControlFlowGraphDumper(cfg, mn)).dump();
System.out.println(InsnListUtils.insnListToString(mn.node.instructions));
}
new FileOutputStream(new File("Meme.class")).write(ClassHelper.toByteArray(cn, ClassWriter.COMPUTE_FRAMES));
}
use of org.mapleir.asm.MethodNode in project maple-ir by LLVM-but-worse.
the class CompilationDemo method main.
public static void main(String[] args) throws IOException {
// File f = new File("res/Bad.jar");
// SingleJarDownloader<ClassNode> dl = new SingleJarDownloader<>(new JarInfo(f));
// dl.download();
// for (ClassNode cn : dl.getJarContents().getClassContents().namedMap().values()) {
String className = "HelloWorld";
JavaClassCompiler compiler = new JavaClassCompiler();
byte[] bytes = compiler.compile(className, "public class " + className + " { public static void main(String[] args) { System.out.println(\"Hello world\"); } }");
if (bytes == null) {
System.out.println("Compilation failed!");
} else {
ASMFactory cnFactory = new DefaultASMFactory();
ClassNode cn = cnFactory.create(bytes, className);
for (MethodNode mn : cn.getMethods()) {
System.out.println(mn.getJavaDesc());
ControlFlowGraphBuilder builder = new ControlFlowGraphBuilder(mn, false);
ControlFlowGraph cfg = builder.buildImpl();
System.out.println(cfg);
BoissinotDestructor.leaveSSA(cfg);
LocalsReallocator.realloc(cfg);
System.out.println(cfg);
}
}
}
use of org.mapleir.asm.MethodNode in project maple-ir by LLVM-but-worse.
the class DefaultInvocationResolver method resolve.
public MethodNode resolve(ClassNode receiver, String name, String desc, boolean strict) {
/*if(strict && receiver.isAbstract()) {
throw new UnsupportedOperationException(String.format("Tried to call method on abstract receiver: %s.%s %s", receiver, name, desc));
}*/
Selector selector = new Selector(name, desc);
if (!hasVisited(receiver)) {
throw new UnsupportedOperationException(String.format("No table for %s", receiver));
} else {
Map<Selector, MethodNode> cvtable = concreteVTables.get(receiver);
Map<Selector, MethodNode> avtable = abstractVTables.get(receiver);
MethodNode cm = cvtable.get(selector);
MethodNode am = avtable.get(selector);
if (cm == null && am == null) {
if (strict) {
throw new NoSuchMethodError(receiver.getName() + "." + name + desc);
}
} else if (cm != null) {
return cm;
} else if (am != null) {
if (strict) {
throw new AbstractMethodError(receiver.getName() + "." + name + desc);
} else {
return am;
}
} else {
if (strict) {
throw new IllegalStateException(String.format("Multiple target sites %s and %s", cm, am));
}
}
}
return null;
}
use of org.mapleir.asm.MethodNode in project maple-ir by LLVM-but-worse.
the class DefaultInvocationResolver method resolveVirtualInitCall.
@Override
public MethodNode resolveVirtualInitCall(String owner, String desc) {
ClassNode cn = app.findClassNode(owner);
if (!checkNullClass(cn, owner)) {
return null;
}
MethodNode mn = resolve(cn, "<init>", desc, true);
if (mn == null) {
return null;
} else if (!mn.owner.getName().equals(owner)) {
throw new UnsupportedOperationException(mn.toString());
} else {
return mn;
}
}
use of org.mapleir.asm.MethodNode in project maple-ir by LLVM-but-worse.
the class HierarchyMethods method findMethods.
/**
* Finds methods in cn matching name and desc.
* @param cn ClassNode
* @param name name of method
* @param desc method descriptor
* @param returnTypes One of ANY_TYPE, CONGRUENT_TYPES, EXACT_TYPES, or LOOSELY_RELATED_TYPES
* @param allowedMask Mask of allowed attributes for modifiers; bit 1 = allowed, 0 = not allowed
* @param requiredMask Mask of required attributes for modifiers; bit 1 = allowed, 0 = not allowed
* @return Set of methods matching specifications
*/
private Set<MethodNode> findMethods(ClassNode cn, String name, String desc, int returnTypes, int allowedMask, int requiredMask) {
allowedMask |= requiredMask;
Set<MethodNode> findM = new HashSet<>();
Type[] expectedParams = Type.getArgumentTypes(desc);
for (MethodNode m : cn.getMethods()) {
// no bits unset in m.access that are in requiredMask
if (((m.node.access ^ allowedMask) & m.node.access) == 0 && ((m.node.access ^ requiredMask) & requiredMask) == 0) {
if (!Modifier.isStatic(allowedMask) && Modifier.isStatic(m.node.access))
throw new IllegalStateException("B0i");
if (!Modifier.isAbstract(allowedMask) && Modifier.isAbstract(m.node.access))
throw new IllegalStateException("B0i");
if (!isBridge(allowedMask) && isBridge(m.node.access))
throw new IllegalStateException("B0i");
if (Modifier.isStatic(requiredMask) && !Modifier.isStatic(m.node.access))
throw new IllegalStateException("B0i");
if (!m.getName().equals(name) || !Arrays.equals(expectedParams, Type.getArgumentTypes(m.getDesc()))) {
continue;
}
switch(returnTypes) {
case ANY_TYPES:
break;
case CONGRUENT_TYPES:
if (!areTypesCongruent(Type.getReturnType(desc), Type.getReturnType(m.getDesc()))) {
continue;
}
break;
case EXACT_TYPES:
if (!desc.equals(m.getDesc())) {
continue;
}
break;
case LOOSELY_RELATED_TYPES:
if (!areTypesLooselyRelated(Type.getReturnType(desc), Type.getReturnType(m.getDesc()))) {
continue;
}
break;
}
// sanity check
if (returnTypes == EXACT_TYPES && !isBridge(allowedMask) && !findM.isEmpty()) {
System.err.println("==findM==");
debugCong(desc, findM.iterator().next().getDesc());
System.err.println("==m==");
debugCong(desc, m.getDesc());
{
byte[] bs = ClassHelper.toByteArray(cn);
try {
FileOutputStream fos = new FileOutputStream(new File("out/broken.class"));
fos.write(bs);
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
throw new IllegalStateException(String.format("%s contains %s(br=%b) and %s(br=%b)", cn.getName(), findM, isBridge(findM.iterator().next().node.access), m, isBridge(m.node.access)));
}
findM.add(m);
}
}
return findM;
}
Aggregations