Skip to content

Commit

Permalink
WIP p1
Browse files Browse the repository at this point in the history
  • Loading branch information
terminalsin committed May 16, 2022
1 parent 6a27b5a commit 24bd659
Show file tree
Hide file tree
Showing 42 changed files with 929 additions and 292 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ApplicationClassSource(String name, Collection<ClassNode> classes) {
public ApplicationClassSource(String name, Map<String, ClassNode> nodeMap) {
super(nodeMap);
this.name = (name == null ? "unknown" : name);
libraries = new ArrayList<>();
libraries = new LinkedList<>();
}

public List<LibraryClassSource> getLibraries() {
Expand Down Expand Up @@ -57,14 +57,17 @@ public void addLibraries(LibraryClassSource... libs) {

libraries.sort(Comparator.comparingInt(LibraryClassSource::getPriority));
Collections.reverse(libraries);

for (LibraryClassSource library : libraries) {
System.out.println("LIBRARY --> " + library.parent.getName() + " [priority: " + library.getPriority() + "]");
}
}

public ClassNode findClassNode(String name) {
LocateableClassNode n = findClass(name);

if(n != null) {
ClassNode cn = n.node;
return cn;
return n.node;
} else {
return null;
}
Expand All @@ -83,7 +86,7 @@ public LocateableClassNode findClass(String name) {
} else {
for(LibraryClassSource cs : libraries) {
if(cs.contains(name)) {
return cs.findClass0(name);
return cs.findClass(name);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Iterable<ClassNode> iterateInterfaces(ClassNode cn) {
}

public Iterable<ClassNode> iterateChildren(ClassNode cn) {
return () -> getReverseEdges(cn).stream().map(e -> e.src()).iterator();
return () -> getReverseEdges(cn).stream().map(FastGraphEdge::src).iterator();
}

// rip beautiful do/while loop.
Expand Down Expand Up @@ -161,7 +161,7 @@ protected ClassNode findClass(String name) {

private ClassNode requestClass0(String name, String from) {
try {
return findClass(name);
return source.findClassNode(name);
} catch(RuntimeException e) {
throw new RuntimeException("request from " + from, e);
}
Expand All @@ -179,7 +179,9 @@ public boolean addVertex(ClassNode cn) {

if(cn != rootNode) {
Set<InheritanceEdge> edges = new HashSet<>();
ClassNode sup = cn.node.superName != null ? requestClass0(cn.node.superName, cn.getName()) : rootNode;
ClassNode sup = cn.node.superName != null
? requestClass0(cn.node.superName, cn.getName())
: rootNode;
if(sup == null) {
LOGGER.error(String.format("No superclass %s for %s", cn.node.superName, cn.getName()));
removeVertex(cn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class InstalledRuntimeClassSource extends LibraryClassSource {
private final HashSet<String> notContains;

public InstalledRuntimeClassSource(ApplicationClassSource parent) {
super(parent);
super(parent, 0);
notContains = new HashSet<>();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.mapleir.app.service;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.*;

import org.mapleir.asm.ClassHelper;
import org.mapleir.asm.ClassNode;
Expand All @@ -12,25 +10,22 @@ public class LibraryClassSource extends ClassSource {
protected final ApplicationClassSource parent;
protected final int priority;

protected LibraryClassSource(ApplicationClassSource parent) {
this(parent, 0, new HashMap<>());
}

public LibraryClassSource(ApplicationClassSource parent, int priority) {
this(parent, priority, new HashMap<>());
public LibraryClassSource(Collection<ClassNode> classes, ApplicationClassSource parent, int priority) {
super(classes);
this.parent = parent;
this.priority = priority;
}

public LibraryClassSource(ApplicationClassSource parent, Collection<ClassNode> classes) {
this(parent, 0, ClassHelper.convertToMap(classes));
}

public LibraryClassSource(ApplicationClassSource parent, int priority, Map<String, ClassNode> nodeMap) {
public LibraryClassSource(Map<String, ClassNode> nodeMap, ApplicationClassSource parent, int priority) {
super(nodeMap);

this.parent = parent;
this.priority = priority;
}


public LibraryClassSource(ApplicationClassSource parent, int priority) {
this(Collections.emptySet(), parent, priority);
}

/* public lookup method, polls parent first (which can
* call it's children to look for the */
@Override
Expand All @@ -40,13 +35,32 @@ public LocateableClassNode findClass(String name) {
}

if(parent != null) {
return parent.findClass(name);
return parent.findClass0(name);
} else {
throwNoParent();
return null;
}
}


@Override
protected LocateableClassNode findClass0(String name) {
if(name == null) {
return null;
}

if(parent != null) {
return parent.findClass0(name);
} else {
throwNoParent();
return null;
}
}

@Override
public boolean contains(String name) {
return parent.contains(name);
}

public boolean isIterable() {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,7 @@ protected void process(BasicBlock b, AbstractInsnNode ain) {
break;
}

case JSR:
case GOTO:
_jump_uncond(resolveTarget(((JumpInsnNode) ain).label));
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,9 @@ public int getChildPointer() {

public void overwrite(final Expr previous, final Expr newest) {
final int index = this.indexOf(previous);
children[index] = newest;
writeAt(newest, index);
assert children[index] == newest;
//children[index] = newest;
previous.setParent(null);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private static LibraryClassSource rt(ApplicationClassSource app, File rtjar) thr
SingleJarDownloader<ClassNode> dl = new SingleJarDownloader<>(new JarInfo(rtjar));
dl.download();

return new LibraryClassSource(app, dl.getJarContents().getClassContents());
return null; //new LibraryClassSource(app, dl.getJarContents().getClassContents());
}

public static void main(String[] args) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private static LibraryClassSource rt(ApplicationClassSource app, File rtjar) thr
SingleJarDownloader<ClassNode> dl = new SingleJarDownloader<>(new JarInfo(rtjar));
dl.download();

return new LibraryClassSource(app, dl.getJarContents().getClassContents());
return null; //new LibraryClassSource(app, dl.getJarContents().getClassContents());
}

public static void main(String[] args) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private static LibraryClassSource rt(ApplicationClassSource app, File rtjar) thr
SingleJarDownloader<ClassNode> dl = new SingleJarDownloader<>(new JarInfo(rtjar));
dl.download();

return new LibraryClassSource(app, dl.getJarContents().getClassContents());
return null; //new LibraryClassSource(app, dl.getJarContents().getClassContents());
}

public static void main(String[] args) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.*;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.mapleir.dot4j.model.DotGraph;
Expand Down Expand Up @@ -217,7 +218,131 @@ public void clear() {
map.clear();
reverseMap.clear();
}


@Override
public Collection<N> getCommonAncestor(Collection<N> nodes) {
/*
* Really shite O(3h) algorithm because I'm too lazy to actually learn one
*
* h: maximum height of graph
*
*
*/
final Map<N, Integer> depthMap = new HashMap<>();
final Map<Integer, Map<N, List<N>>> ancestorMap = new HashMap<>();

int lowestDepth = 9999;

/*
* Here we first compute the max depth of all the branches
* since we know they all have the common ancestor of
* java/lang/Object so yeah...
*
* Btw we have to get the depth before to be able to invert
* it
*/
for (N node : nodes) {
int maxDepth = 0;

List<N> above = new ArrayList<>();
above.add(node);

while (true) {
final List<N> stack = new ArrayList<>(above);
above.clear();
for (N n : stack) {
this.getSuccessors(n).forEach(above::add);
}

if (above.isEmpty())
break;

maxDepth++;
}

depthMap.put(node, maxDepth);

if (lowestDepth > maxDepth) {
lowestDepth = maxDepth;
}
}

/*
* Here we directly compute all the hierarchy they have
* at a specified branch height which is inverted
*/
for (N node : nodes) {
/* Get the max depth to properly invert it */
int maxDepth = depthMap.get(node);

List<N> above = new ArrayList<>();
above.add(node);

while (maxDepth >= 0) {
final List<N> stack = new ArrayList<>(above);
above.clear();
for (N n : stack) {
this.getSuccessors(n).forEach(above::add);
}

/* Slight optimization since we're not gonna bother with anything higher */
if (maxDepth <= lowestDepth) {
ancestorMap
.computeIfAbsent(maxDepth, e -> new HashMap<>())
.put(node, stack);
}

maxDepth--;
}
}

int depth = lowestDepth;
final List<N> common = new ArrayList<>();

while (depth >= 0) {
final Map<N, List<N>> checked = ancestorMap.get(depth);

final Set<N> visited = new HashSet<>();

/*
* If there's any common match at a specific height, then
* by default it is the lowest common node but since it's
* a graph, it can be multiple:
*
* O
* / \
* N1 N2
* \ /
* / \
* N4 N5
*
* Both N4 and N5 have N1 and N2 as common ancestors
*/
for (List<N> value : checked.values()) {
for (N n : value) {
if (visited.contains(n))
continue;

if (checked.values().stream().allMatch(e -> e.contains(n))) {
common.add(n);
}

visited.add(n);
}

}

/* We have a common ancestor, since we want the lowest one we grab this */
if (!common.isEmpty())
break;

depth--;
}


return common;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
import org.mapleir.propertyframework.api.IPropertyDictionary;
import org.mapleir.propertyframework.util.PropertyHelper;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.*;
import java.util.Map.Entry;
import java.util.Set;

public interface FastGraph<N extends FastGraphVertex, E extends FastGraphEdge<N>> {

Expand Down Expand Up @@ -37,6 +34,8 @@ default E clone(E edge, N newSrc, N newDst) {
void replace(N old, N n);

void clear();

Collection<N> getCommonAncestor(Collection<N> nodes);

default FastGraph<N, E> copy() {
throw new UnsupportedOperationException();
Expand Down
Loading

0 comments on commit 24bd659

Please sign in to comment.