use of org.mapleir.asm.ClassNode in project maple-ir by LLVM-but-worse.
the class CompleteResolvingJarDumper method dump.
/**
* Dumps the jars contents.
*
* @param file File to dump it to.
*/
@Override
public void dump(File file) throws IOException {
if (file.exists())
file.delete();
file.createNewFile();
JarOutputStream jos = new JarOutputStream(new FileOutputStream(file));
int classesDumped = 0;
int resourcesDumped = 0;
for (ClassNode cn : contents.getClassContents()) {
classesDumped += dumpClass(jos, cn.getName(), cn);
}
for (JarResource res : contents.getResourceContents()) {
resourcesDumped += dumpResource(jos, res.getName(), res.getData());
}
if (!Debug.debugging)
System.out.println("Dumped " + classesDumped + " classes and " + resourcesDumped + " resources to " + file.getAbsolutePath());
jos.close();
}
use of org.mapleir.asm.ClassNode in project maple-ir by LLVM-but-worse.
the class InstalledRuntimeClassSource method __resolve.
/* (very) internal class loading method. doesn't
* poll cache at all. */
private LocateableClassNode __resolve(String name) {
if (name.startsWith("[")) {
/* calling Object methods. (clone() etc)
* that we haven't already resolved.
*
* Cache it so that contains() can
* quick check whether we have it
* and the next call to findClass0
* can quickly resolve it too. */
LocateableClassNode node = findClass0("java/lang/Object");
nodeMap.put(name, node.node);
return node;
}
/* try to resolve the class from the runtime. */
try {
ClassNode cn = ClassHelper.create(name, ClassReader.SKIP_CODE);
/* cache it. */
nodeMap.put(cn.getName(), cn);
ClassTree tree = parent._getClassTree();
if (tree == null) {
if (!cn.getName().equals("java/lang/Object")) {
LOGGER.error(String.format("Tried to load %s before initialisation", cn));
throw new IllegalStateException("Only Object may be loaded during tree initialisation.");
}
} else {
if (!tree.containsVertex(cn)) {
tree.addVertex(cn);
}
}
LocateableClassNode node = new LocateableClassNode(this, cn, true);
return node;
} catch (IOException e) {
LOGGER.error(String.format("Could not load class from calling classloader: %s", name));
LOGGER.error(e);
notContains.add(name);
return null;
}
}
use of org.mapleir.asm.ClassNode in project maple-ir by LLVM-but-worse.
the class ExtendedType method isIncluded.
/**
* Checks whether this extended type is included into the set of types
* represented by the other extended type.
* @param source
* @param other
* @return
*/
public boolean isIncluded(ApplicationClassSource source, ExtendedType other) {
if (other == null) {
return false;
} else if (this.equals(other)) {
return true;
}
if (other.getSort() == Sort.MONO) {
return false;
}
/* other is CONE, we need to check if our
* type is included in the other's type cone*/
ClassNode klass = findNode(source, type);
LinkedList<ClassNode> wl = new LinkedList<>();
wl.addLast(klass);
while (!wl.isEmpty()) {
ClassNode cur = wl.removeFirst();
ClassNode parent = source.findClassNode(cur.node.superName);
if (parent != null) {
if (parent.getName().equals(type.getInternalName())) {
return true;
}
wl.addLast(parent);
}
for (String siface : cur.node.interfaces) {
if (siface.equals(type.getInternalName())) {
return true;
}
wl.addLast(source.findClassNode(siface));
}
}
return false;
}
use of org.mapleir.asm.ClassNode in project maple-ir by LLVM-but-worse.
the class FieldRSADecryptionPass method transform.
private void transform(AnalysisContext cxt) {
for (ClassNode cn : cxt.getApplication().iterate()) {
for (MethodNode m : cn.getMethods()) {
ControlFlowGraph cfg = cxt.getIRCache().getFor(m);
for (BasicBlock b : cfg.vertices()) {
for (Stmt stmt : b) {
// String fsKey = "";
if (stmt.getOpcode() == Opcode.FIELD_STORE) {
FieldStoreStmt fs = (FieldStoreStmt) stmt;
// [enc, dec]
Number[] p = pairs.get(key(fs));
if (p != null) {
Expr e = fs.getValueExpression();
e.unlink();
ArithmeticExpr ae = new ArithmeticExpr(new ConstantExpr(p[1], ConstantExpr.computeType(p[1])), e, Operator.MUL);
fs.setValueExpression(ae);
// fsKey = key(fs);
}
}
for (Expr e : stmt.enumerateOnlyChildren()) {
if (e.getOpcode() == FIELD_LOAD) {
CodeUnit par = e.getParent();
FieldLoadExpr fl = (FieldLoadExpr) e;
// [enc, dec]
Number[] p = pairs.get(key(fl));
if (p == null) {
continue;
}
if (par.getOpcode() == ARITHMETIC) {
ArithmeticExpr ae = (ArithmeticExpr) par;
if (ae.getRight().getOpcode() == CONST_LOAD) {
ConstantExpr ce = (ConstantExpr) ae.getRight();
Number cst = (Number) ce.getConstant();
Number res = __mul(cst, p[0], p[0].getClass().equals(Long.class));
// if(!__eq(res, 1, p[0].getClass().equals(Long.class))) {
// System.out.println(cst + " -> " + res);
// System.out.println(" expr: " + fl.getRootParent());
// }
par.writeAt(new ConstantExpr(res, ConstantExpr.computeType(res)), par.indexOf(ce));
continue;
}
}
ArithmeticExpr ae = new ArithmeticExpr(new ConstantExpr(p[0], ConstantExpr.computeType(p[0])), fl.copy(), Operator.MUL);
par.writeAt(ae, par.indexOf(fl));
}
}
}
}
}
}
// for(ClassNode cn : cxt.getClassTree().getClasses().values()) {
// for(MethodNode m : cn.getMethods()) {
// ControlFlowGraph cfg = cxt.getCFGS().getIR(m);
//
// for(BasicBlock b : cfg.vertices()) {
// for(Stmt stmt : b) {
// for(Expr e : stmt.enumerateOnlyChildren()) {
// if(e.getOpcode() == Opcode.ARITHMETIC) {
// ArithmeticExpr ae = (ArithmeticExpr) e;
// if(ae.getRight().getOpcode() == Opcode.CONST_LOAD) {
// ConstantExpr c = (ConstantExpr) ae.getRight();
// Object o = c.getConstant();
//
// if(o instanceof Long || o instanceof Integer) {
// Number n = (Number) o;
// if(__eq(n, 1, ae.getType().equals(DescType.LONG_TYPE))) {
// Expr l = ae.getLeft();
// l.unlink();
//
// CodeUnit aePar = ae.getParent();
// aePar.writeAt(l, aePar.indexOf(ae));
// } else if(__eq(n, 0, ae.getType().equals(DescType.LONG_TYPE))) {
// c.unlink();
//
// CodeUnit aePar = ae.getParent();
// aePar.writeAt(c, aePar.indexOf(ae));
// }
// }
// }
// }
// }
// }
// }
// }
// }
}
use of org.mapleir.asm.ClassNode in project maple-ir by LLVM-but-worse.
the class ClassRenamerPass method accept.
/*private String getClassName(String name) {
int i = name.lastIndexOf('/');
if(i == -1) {
return name;
} else {
return name.substring(i + 1, name.length());
}
}*/
@Override
public PassResult accept(PassContext pcxt) {
AnalysisContext cxt = pcxt.getAnalysis();
ApplicationClassSource source = cxt.getApplication();
Collection<ClassNode> classes = CollectionUtils.collate(source.iterator());
// int min = RenamingUtil.computeMinimum(classes.size());
int n = RenamingUtil.numeric("aaa");
int step = 27;
for (ClassNode cn : classes) {
String className = RenamingUtil.getClassName(cn.getName());
if (!heuristic.shouldRename(className, cn.node.access)) {
System.out.println("Heuristic bypass " + cn.getName());
}
String newName = heuristic.shouldRename(className, cn.node.access) ? RenamingUtil.createName(n) : className;
String s = RenamingUtil.getPackage(cn.getName()) + newName;
n += step;
remapping.put(cn.getName(), s);
// System.out.println(cn.getName() + " -> " + s);
cn.node.name = s;
}
for (ClassNode cn : classes) {
cn.node.superName = remapping.getOrDefault(cn.node.superName, cn.node.superName);
{
List<String> ifaces = new ArrayList<>();
for (int i = 0; i < cn.node.interfaces.size(); i++) {
String s = cn.node.interfaces.get(i);
ifaces.add(remapping.getOrDefault(s, s));
}
cn.node.interfaces = ifaces;
}
unsupported(cn.node.signature);
// unsupported(cn.sourceFile);
// unsupported(cn.sourceDebug);
cn.node.outerClass = remapping.getOrDefault(cn.node.outerClass, cn.node.outerClass);
// unsupported(cn.outerMethod);
// unsupported(cn.outerMethodDesc);
unsupported(cn.node.visibleAnnotations);
unsupported(cn.node.invisibleAnnotations);
unsupported(cn.node.visibleTypeAnnotations);
unsupported(cn.node.invisibleTypeAnnotations);
unsupported(cn.node.attrs);
unsupported(cn.node.innerClasses);
for (FieldNode f : cn.getFields()) {
unsupported(cn.node.signature);
{
Type type = Type.getType(f.node.desc);
String newType = resolveType(type, remapping);
if (newType != null) {
f.node.desc = newType;
}
}
unsupported(f.node.visibleAnnotations);
unsupported(f.node.invisibleAnnotations);
unsupported(f.node.visibleTypeAnnotations);
unsupported(f.node.invisibleTypeAnnotations);
unsupported(f.node.attrs);
}
for (MethodNode m : cn.getMethods()) {
m.node.desc = resolveMethod(m.node.desc, remapping);
unsupported(m.node.signature);
{
List<String> exceptions = new ArrayList<>();
for (int i = 0; i < m.node.exceptions.size(); i++) {
String s = m.node.exceptions.get(i);
exceptions.add(remapping.getOrDefault(s, s));
}
m.node.exceptions = exceptions;
}
unsupported(m.node.parameters);
unsupported(m.node.visibleAnnotations);
unsupported(m.node.invisibleAnnotations);
unsupported(m.node.visibleTypeAnnotations);
unsupported(m.node.invisibleTypeAnnotations);
unsupported(m.node.attrs);
unsupported(m.node.annotationDefault);
unsupported(m.node.visibleParameterAnnotations);
unsupported(m.node.invisibleParameterAnnotations);
for (TryCatchBlockNode tcbn : m.node.tryCatchBlocks) {
tcbn.type = remapping.getOrDefault(tcbn.type, tcbn.type);
}
ControlFlowGraph cfg = cxt.getIRCache().getFor(m);
for (ExceptionRange<BasicBlock> er : cfg.getRanges()) {
Set<Type> newTypeSet = new HashSet<>();
for (Type t : er.getTypes()) {
// FIXME:
String s = t.getInternalName();
if (remapping.containsKey(s)) {
newTypeSet.add(Type.getType("L" + remapping.get(s) + ";"));
} else {
newTypeSet.add(t);
}
}
er.setTypes(newTypeSet);
}
if (m.node.localVariables != null) {
m.node.localVariables.clear();
for (LocalVariableNode lvn : m.node.localVariables) {
String newDesc = resolveType(Type.getType(lvn.desc), remapping);
if (newDesc != null) {
lvn.desc = newDesc;
}
unsupported(lvn.signature);
}
}
unsupported(m.node.visibleLocalVariableAnnotations);
unsupported(m.node.invisibleLocalVariableAnnotations);
for (BasicBlock b : cfg.vertices()) {
for (Stmt stmt : b) {
if (stmt.getOpcode() == Opcode.FIELD_STORE) {
FieldStoreStmt fs = (FieldStoreStmt) stmt;
String owner = fs.getOwner();
fs.setOwner(remapping.getOrDefault(owner, owner));
{
Type type = Type.getType(fs.getDesc());
String newType = resolveType(type, remapping);
if (newType != null) {
fs.setDesc(newType);
}
}
} else if (stmt.getOpcode() == Opcode.RETURN) {
ReturnStmt ret = (ReturnStmt) stmt;
String newType = resolveType(ret.getType(), remapping);
if (newType != null) {
ret.setType(Type.getType(newType));
}
} else if (stmt instanceof AbstractCopyStmt) {
AbstractCopyStmt copy = (AbstractCopyStmt) stmt;
VarExpr v = copy.getVariable();
String newType = resolveType(v.getType(), remapping);
if (newType != null) {
v.setType(Type.getType(newType));
}
}
for (Expr e : stmt.enumerateOnlyChildren()) {
if (e.getOpcode() == Opcode.CAST) {
CastExpr cast = (CastExpr) e;
String newType = resolveType(cast.getType(), remapping);
if (newType != null) {
cast.setType(Type.getType(newType));
}
} else if (e.getOpcode() == Opcode.CATCH) {
CaughtExceptionExpr caught = (CaughtExceptionExpr) e;
String newType = resolveType(caught.getType(), remapping);
if (newType != null) {
caught.setType(Type.getType(newType));
}
} else if (e.getOpcode() == Opcode.INVOKE) {
InvocationExpr invoke = (InvocationExpr) e;
if (invoke.isDynamic())
throw new UnsupportedOperationException();
invoke.setOwner(remapping.getOrDefault(invoke.getOwner(), invoke.getOwner()));
invoke.setDesc(resolveMethod(invoke.getDesc(), remapping));
} else if (e.getOpcode() == Opcode.FIELD_LOAD) {
FieldLoadExpr fl = (FieldLoadExpr) e;
fl.setOwner(remapping.getOrDefault(fl.getOwner(), fl.getOwner()));
String newType = resolveType(fl.getType(), remapping);
if (newType != null) {
fl.setDesc(newType);
}
} else if (e.getOpcode() == Opcode.INIT_OBJ) {
InitialisedObjectExpr init = (InitialisedObjectExpr) e;
init.setOwner(remapping.getOrDefault(init.getOwner(), init.getOwner()));
init.setDesc(resolveMethod(init.getDesc(), remapping));
} else if (e.getOpcode() == Opcode.INSTANCEOF) {
InstanceofExpr inst = (InstanceofExpr) e;
String newType = resolveType(inst.getCheckType(), remapping);
if (newType != null) {
inst.setCheckType(Type.getType(newType));
}
} else if (e.getOpcode() == Opcode.NEW_ARRAY) {
NewArrayExpr na = (NewArrayExpr) e;
String newType = resolveType(na.getType(), remapping);
if (newType != null) {
na.setType(Type.getType(newType));
}
} else if (e.getOpcode() == Opcode.ALLOC_OBJ) {
AllocObjectExpr uninit = (AllocObjectExpr) e;
String newType = resolveType(uninit.getType(), remapping);
if (newType != null) {
uninit.setType(Type.getType(newType));
}
} else if (e.getOpcode() == Opcode.LOCAL_LOAD) {
VarExpr v = (VarExpr) e;
String newType = resolveType(v.getType(), remapping);
if (newType != null) {
v.setType(Type.getType(newType));
}
} else if (e.getOpcode() == Opcode.CONST_LOAD) {
ConstantExpr c = (ConstantExpr) e;
Object cst = c.getConstant();
if (cst instanceof Type) {
Type t = (Type) cst;
if (t.getSort() == Type.OBJECT) {
String newType = resolveType(t, remapping);
if (newType != null) {
c.setConstant(Type.getType(newType));
}
} else {
throw new UnsupportedOperationException(String.format("Unsupported ctype %s (%d)", t, t.getSort()));
}
}
}
}
}
}
}
}
source.rebuildTable();
return PassResult.with(pcxt, this).finished().make();
}
Aggregations