Skip to content

Commit

Permalink
Merge origin/master
Browse files Browse the repository at this point in the history
  • Loading branch information
honfika committed Jul 9, 2015
2 parents 45a19de + eb557e3 commit c8db079
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1992,12 +1992,13 @@ public void updateOffsets(OffsetUpdater updater, MethodBody body) {
}
}

public void fixJumps(MethodBody body) {
public void fixJumps(final String path, MethodBody body) throws InterruptedException {
buildCache();
if (code.isEmpty()) {
return;
}
AVM2Instruction lastInstuction = code.get(code.size() - 1);
final List<Long> insAddrToRemove = new ArrayList<>();
final long endOffset = lastInstuction.offset + lastInstuction.getBytesLength();
updateOffsets(new OffsetUpdater() {

Expand All @@ -2008,25 +2009,26 @@ public long updateInstructionOffset(long address) {

@Override
public int updateOperandOffset(long insAddr, long targetAddress, int offset) {
if (targetAddress > endOffset) {
// jump to the end
return (int) (offset - targetAddress + endOffset);
if (targetAddress > endOffset || targetAddress < 0 || posCache.indexOf(targetAddress) == -1) {
insAddrToRemove.add(insAddr);
}
if (targetAddress < 0) {
// jump to the first instuction
return (int) (offset - targetAddress);
}

int targetPos = posCache.indexOf(targetAddress);
if (targetPos == -1) {
// jump to the end
return (int) (offset - targetAddress + endOffset);
}

return offset;
}

}, body);

boolean someIgnored = false;
for (Long insAddr : insAddrToRemove) {
int pos = posCache.indexOf(insAddr);
if (pos > -1) {
code.get(pos).ignored = true;
someIgnored = true;
}
}
if (someIgnored) {
Logger.getLogger(AVM2Code.class.getName()).log(Level.WARNING, path + ": One or more invalid jump offsets found in the code. Those instructions were ignored.");
}
removeIgnored(body);
}

public void checkValidOffsets(MethodBody body) {
Expand Down Expand Up @@ -2227,7 +2229,7 @@ private static AVM2LocalData prepareBranchLocalData(AVM2LocalData localData) {
}

private int removeTrapsOld(AVM2ConstantPool constants, Trait trait, MethodInfo info, MethodBody body, ABC abc, int scriptIndex, int classIndex, boolean isStatic, String path) throws InterruptedException {
removeDeadCode(constants, trait, info, body);
removeDeadCode(body);
AVM2LocalData localData = new AVM2LocalData();
localData.isStatic = isStatic;
localData.classIndex = classIndex;
Expand All @@ -2250,9 +2252,9 @@ private int removeTrapsOld(AVM2ConstantPool constants, Trait trait, MethodInfo i
localData.refs = refs;
localData.code = this;
int ret = 0;
ret += removeTraps(constants, trait, info, body, localData, new AVM2GraphSource(this, false, -1, -1, new HashMap<>(), new ScopeStack(), abc, body, new HashMap<>(), new ArrayList<>(), new HashMap<>(), refs), 0, path, refs);
removeIgnored(constants, trait, info, body);
removeDeadCode(constants, trait, info, body);
ret += removeTrapsOld(constants, trait, info, body, localData, new AVM2GraphSource(this, false, -1, -1, new HashMap<>(), new ScopeStack(), abc, body, new HashMap<>(), new ArrayList<>(), new HashMap<>(), refs), 0, path, refs);
removeIgnored(body);
removeDeadCode(body);

return ret;
}
Expand Down Expand Up @@ -2783,7 +2785,7 @@ private void restoreControlFlowPass(AVM2ConstantPool constants, Trait trait, Met
logger.log(Level.FINE, null, ex);
}
invalidateCache();
removeDeadCode(constants, trait, info, body);
removeDeadCode(body);
}

public void restoreControlFlow(AVM2ConstantPool constants, Trait trait, MethodInfo info, MethodBody body) throws InterruptedException {
Expand Down Expand Up @@ -2815,7 +2817,7 @@ public void removeIgnored(AVM2ConstantPool constants, Trait trait, MethodInfo in
invalidateCache();
}*/

public void removeIgnored(AVM2ConstantPool constants, Trait trait, MethodInfo info, MethodBody body) throws InterruptedException {
public void removeIgnored(MethodBody body) throws InterruptedException {
//System.err.println("removing ignored...");
for (int i = 0; i < code.size(); i++) {
if (code.get(i).ignored) {
Expand All @@ -2826,7 +2828,7 @@ public void removeIgnored(AVM2ConstantPool constants, Trait trait, MethodInfo in
//System.err.println("/ignored removed");
}

public int removeDeadCode(AVM2ConstantPool constants, Trait trait, MethodInfo info, MethodBody body) throws InterruptedException {
public int removeDeadCode(MethodBody body) throws InterruptedException {
invalidateCache();
HashMap<Integer, List<Integer>> refs = visitCode(body);
int cnt = 0;
Expand All @@ -2838,7 +2840,7 @@ public int removeDeadCode(AVM2ConstantPool constants, Trait trait, MethodInfo in
}
}

removeIgnored(constants, trait, info, body);
removeIgnored(body);

for (int i = code.size() - 1; i >= 0; i--) {
AVM2Instruction ins = code.get(i);
Expand All @@ -2850,7 +2852,7 @@ public int removeDeadCode(AVM2ConstantPool constants, Trait trait, MethodInfo in
}
}

removeIgnored(constants, trait, info, body);
removeIgnored(body);

return cnt;
}
Expand Down Expand Up @@ -3053,7 +3055,7 @@ public static boolean getPreviousReachableIps(int currentIp, HashMap<Integer, Li
}

@SuppressWarnings("unchecked")
private static int removeTraps(HashMap<Integer, List<Integer>> refs, boolean secondPass, boolean indeterminate, AVM2LocalData localData, TranslateStack stack, List<GraphTargetItem> output, AVM2GraphSource code, int ip, HashMap<Integer, Integer> visited, HashMap<Integer, HashMap<Integer, GraphTargetItem>> visitedStates, HashMap<AVM2Instruction, Decision> decisions, String path, int recursionLevel) throws InterruptedException {
private static int removeTrapsOld(HashMap<Integer, List<Integer>> refs, boolean secondPass, boolean indeterminate, AVM2LocalData localData, TranslateStack stack, List<GraphTargetItem> output, AVM2GraphSource code, int ip, HashMap<Integer, Integer> visited, HashMap<Integer, HashMap<Integer, GraphTargetItem>> visitedStates, HashMap<AVM2Instruction, Decision> decisions, String path, int recursionLevel) throws InterruptedException {
if (Thread.currentThread().isInterrupted()) {
throw new InterruptedException();
}
Expand Down Expand Up @@ -3288,7 +3290,7 @@ private static int removeTraps(HashMap<Integer, List<Integer>> refs, boolean sec
for (int b : branches) {
TranslateStack brStack = (TranslateStack) stack.clone();
if (b >= 0) { //useVisited || (!ins.isJump())
ret += removeTraps(refs, secondPass, indeterminate, prepareBranchLocalData(localData), brStack, output, code, b, visited, visitedStates, decisions, path, recursionLevel + 1);
ret += removeTrapsOld(refs, secondPass, indeterminate, prepareBranchLocalData(localData), brStack, output, code, b, visited, visitedStates, decisions, path, recursionLevel + 1);
} else {
if (debugMode) {
System.out.println("Negative branch:" + b);
Expand All @@ -3306,9 +3308,9 @@ private static int removeTraps(HashMap<Integer, List<Integer>> refs, boolean sec
return ret;
}

public static int removeTraps(AVM2ConstantPool constants, Trait trait, MethodInfo info, MethodBody body, AVM2LocalData localData, AVM2GraphSource code, int addr, String path, HashMap<Integer, List<Integer>> refs) throws InterruptedException {
public static int removeTrapsOld(AVM2ConstantPool constants, Trait trait, MethodInfo info, MethodBody body, AVM2LocalData localData, AVM2GraphSource code, int addr, String path, HashMap<Integer, List<Integer>> refs) throws InterruptedException {
HashMap<AVM2Instruction, AVM2Code.Decision> decisions = new HashMap<>();
removeTraps(refs, false, false, localData, new TranslateStack(path), new ArrayList<>(), code, code.adr2pos(addr), new HashMap<>(), new HashMap<>(), decisions, path, 0);
removeTrapsOld(refs, false, false, localData, new TranslateStack(path), new ArrayList<>(), code, code.adr2pos(addr), new HashMap<>(), new HashMap<>(), decisions, path, 0);
int cnt = 0;
for (AVM2Instruction src : decisions.keySet()) {
Decision dec = decisions.get(src);
Expand All @@ -3332,8 +3334,7 @@ public static int removeTraps(AVM2ConstantPool constants, Trait trait, MethodInf
}
}
}
//int cnt = removeTraps(refs, true, false, localData, new TranslateStack(), new ArrayList<GraphTargetItem>(), code, code.adr2pos(addr), new HashMap<Integer, Integer>(), new HashMap<Integer, HashMap<Integer, GraphTargetItem>>(), decisions, path);
code.getCode().removeIgnored(constants, trait, info, body);
code.getCode().removeIgnored(body);
return cnt;
}
/*public static int removeTraps(AVM2LocalData localData, AVM2GraphSource code, int addr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ protected boolean removeObfuscationIfs(int classIndex, boolean isStatic, int scr
}

protected void removeUnreachableActions(AVM2Code code, AVM2ConstantPool cpool, Trait trait, MethodInfo minfo, MethodBody body) throws InterruptedException {
code.removeDeadCode(cpool, trait, minfo, body);
code.removeDeadCode(body);
}

protected boolean removeZeroJumps(AVM2Code actions, MethodBody body) {
Expand Down Expand Up @@ -422,7 +422,6 @@ public void methodBodyParsed(MethodBody body, SWF swf) {

public void deobfuscate(String path, int classIndex, boolean isStatic, int scriptIndex, ABC abc, AVM2ConstantPool cpool, Trait trait, MethodInfo minfo, MethodBody body) throws InterruptedException {
AVM2Code code = body.getCode();
code.fixJumps(body);
removeUnreachableActions(code, cpool, trait, minfo, body);
removeObfuscationIfs(classIndex, isStatic, scriptIndex, abc, cpool, trait, minfo, body, new ArrayList<>());
removeZeroJumps(code, body);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public String toString() {
}

public int removeDeadCode(AVM2ConstantPool constants, Trait trait, MethodInfo info) throws InterruptedException {
return getCode().removeDeadCode(constants, trait, info, this);
return getCode().removeDeadCode(this);
}

public void restoreControlFlow(AVM2ConstantPool constants, Trait trait, MethodInfo info) throws InterruptedException {
Expand Down Expand Up @@ -349,6 +349,7 @@ public GraphTextWriter toString(final String path, ScriptExportMode exportMode,
public MethodBody convertMethodBody(String path, boolean isStatic, int scriptIndex, int classIndex, ABC abc, Trait trait, AVM2ConstantPool constants, List<MethodInfo> method_info, ScopeStack scopeStack, boolean isStaticInitializer, List<String> fullyQualifiedNames, Traits initTraits) throws InterruptedException {
MethodBody body = clone();
body.getCode().markMappedOffsets();
body.getCode().fixJumps(path, body);

if (Configuration.autoDeobfuscate.get()) {
try {
Expand Down

0 comments on commit c8db079

Please sign in to comment.