using a function which calculates the target address of the IfType instructions

This commit is contained in:
honfika@gmail.com
2015-12-02 14:19:09 +01:00
parent d0f2786673
commit 35b917eca1
10 changed files with 29 additions and 23 deletions

View File

@@ -733,14 +733,14 @@ public class AVM2Code implements Cloneable {
}
if (ins.definition instanceof JumpIns) {
try {
pos = adr2pos(pos2adr(pos) + ins.getBytesLength() + ins.operands[0]);
pos = adr2pos(ins.getTargetAddress());
continue;
} catch (ConvertException ex) {
return false;
}
} else if (ins.definition instanceof IfTypeIns) {
try {
int newpos = adr2pos(pos2adr(pos) + ins.getBytesLength() + ins.operands[0]);
int newpos = adr2pos(ins.getTargetAddress());
calculateDebugFileLine(debugFile, debugLine, newpos, abc, seen);
} catch (ConvertException ex) {
return false;
@@ -1978,8 +1978,8 @@ public class AVM2Code implements Cloneable {
}
}*/
//Faster, but not so universal
if ((ins.definition instanceof JumpIns) || (ins.definition instanceof IfTypeIns)) {
long target = ins.getOffset() + ins.getBytesLength() + ins.operands[0];
if (ins.definition instanceof IfTypeIns) {
long target = ins.getTargetAddress();
try {
ins.operands[0] = updater.updateOperandOffset(ins.getOffset(), target, ins.operands[0]);
} catch (ConvertException cex) {
@@ -2322,14 +2322,14 @@ public class AVM2Code implements Cloneable {
}
if (ins.definition instanceof JumpIns) {
try {
pos = adr2pos(pos2adr(pos) + ins.getBytesLength() + ins.operands[0]);
pos = adr2pos(ins.getTargetAddress());
continue;
} catch (ConvertException ex) {
return false;
}
} else if (ins.definition instanceof IfTypeIns) {
try {
int newpos = adr2pos(pos2adr(pos) + ins.getBytesLength() + ins.operands[0]);
int newpos = adr2pos(ins.getTargetAddress());
walkCode(stats, newpos, stack, scope, abc);
} catch (ConvertException ex) {
return false;
@@ -2459,14 +2459,14 @@ public class AVM2Code implements Cloneable {
}
if (ins.definition instanceof JumpIns) {
try {
ip = adr2pos(pos2adr(ip) + ins.getBytesLength() + ins.operands[0]);
ip = adr2pos(ins.getTargetAddress());
continue;
} catch (ConvertException ex) {
logger.log(Level.FINE, null, ex);
}
} else if (ins.definition instanceof IfTypeIns) {
try {
toVisit.add(adr2pos(pos2adr(ip) + ins.getBytesLength() + ins.operands[0]));
toVisit.add(adr2pos(ins.getTargetAddress()));
toVisitLast.add(ip);
} catch (ConvertException ex) {
logger.log(Level.FINE, null, ex);

View File

@@ -185,7 +185,7 @@ public class AVM2DeobfuscatorGetSet extends SWFDecompilerAdapter {
boolean ifed = false;
if (def instanceof JumpIns) {
long address = ins.getOffset() + ins.getBytesLength() + ins.operands[0];
long address = ins.getTargetAddress();
idx = code.adr2pos(address);
if (idx == -1) {

View File

@@ -50,7 +50,7 @@ public class AVM2DeobfuscatorJumps extends SWFDecompilerAdapter {
for (int i = 0; i < code.code.size(); i++) {
AVM2Instruction ins = code.code.get(i);
if (ins.definition instanceof JumpIns) {
long targetAddr = ins.getOffset() + ins.operands[0] + ins.getBytesLength();
long targetAddr = ins.getTargetAddress();
{
for (int r : refs.get(i)) {
if (r >= 0) { //Not Exception start/end

View File

@@ -264,7 +264,7 @@ public class AVM2DeobfuscatorRegisters extends AVM2DeobfuscatorSimple {
if (ins.definition instanceof JumpIns) {
long address = ins.getOffset() + ins.getBytesLength() + ins.operands[0];
long address = ins.getTargetAddress();
idx = code.adr2pos(address);//code.indexOf(code.getByAddress(address));
if (idx == -1) {
throw new TranslateException("Jump target not found: " + address);

View File

@@ -228,7 +228,7 @@ public class AVM2DeobfuscatorRegistersOld extends AVM2DeobfuscatorSimpleOld {
if (ins.definition instanceof JumpIns) {
long address = ins.getOffset() + ins.getBytesLength() + ins.operands[0];
long address = ins.getTargetAddress();
idx = code.adr2pos(address);//code.indexOf(code.getByAddress(address));
if (idx == -1) {
throw new TranslateException("Jump target not found: " + address);

View File

@@ -303,7 +303,7 @@ public class AVM2DeobfuscatorSimple extends SWFDecompilerAdapter {
boolean ifed = false;
if (def instanceof IfTypeIns && !(def instanceof JumpIns)) {
long address = ins.getOffset() + ins.getBytesLength() + ins.operands[0];
long address = ins.getTargetAddress();
int nidx = code.adr2pos(address);
AVM2Instruction tarIns = code.code.get(nidx);
@@ -320,7 +320,7 @@ public class AVM2DeobfuscatorSimple extends SWFDecompilerAdapter {
code.insertInstruction(idx, new AVM2Instruction(ins.getOffset(), DeobfuscatePopIns.getInstance(), null), true, body);
}
idx = code.adr2pos(jumpIns.getOffset() + jumpIns.getBytesLength() + jumpIns.operands[0]);
idx = code.adr2pos(jumpIns.getTargetAddress());
} else {
//System.err.println("replacing " + ins + " on " + idx + " with pop");
code.replaceInstruction(idx, new AVM2Instruction(ins.getOffset(), DeobfuscatePopIns.getInstance(), null), body);

View File

@@ -342,7 +342,7 @@ public class AVM2DeobfuscatorSimpleOld extends SWFDecompilerAdapter {
boolean ifed = false;
if (def instanceof JumpIns) {
long address = ins.getOffset() + ins.getBytesLength() + ins.operands[0];
long address = ins.getTargetAddress();
idx = code.adr2pos(address);
if (idx == -1) {
@@ -355,7 +355,7 @@ public class AVM2DeobfuscatorSimpleOld extends SWFDecompilerAdapter {
GraphTargetItem top = stack.pop();
Object res = top.getResult();
long address = ins.getOffset() + ins.getBytesLength() + ins.operands[0];
long address = ins.getTargetAddress();
int nidx = code.adr2pos(address);//code.indexOf(code.getByAddress(address));
AVM2Instruction tarIns = code.code.get(nidx);
@@ -372,7 +372,7 @@ public class AVM2DeobfuscatorSimpleOld extends SWFDecompilerAdapter {
code.insertInstruction(idx, new AVM2Instruction(ins.getOffset(), DeobfuscatePopIns.getInstance(), null), true, body);
}
idx = code.adr2pos(jumpIns.getOffset() + jumpIns.getBytesLength() + jumpIns.operands[0]);
idx = code.adr2pos(jumpIns.getTargetAddress());
} else {
//System.err.println("replacing " + ins + " on " + idx + " with pop");
code.replaceInstruction(idx, new AVM2Instruction(ins.getOffset(), DeobfuscatePopIns.getInstance(), null), body);

View File

@@ -379,12 +379,16 @@ public class AVM2Instruction implements Cloneable, GraphSourceItem {
this.offset = offset;
}
public long getTargetAddress() {
return offset + 4 /*getBytesLength()*/ + operands[0];
}
@Override
public List<Integer> getBranches(GraphSource code) {
List<Integer> ret = new ArrayList<>();
if (definition instanceof IfTypeIns) {
ret.add(code.adr2pos(offset + getBytesLength() + operands[0]));
ret.add(code.adr2pos(getTargetAddress()));
if (!(definition instanceof JumpIns)) {
ret.add(code.adr2pos(offset + getBytesLength()));
}

View File

@@ -126,10 +126,11 @@ public class ActionIf extends Action {
public List<Integer> getBranches(GraphSource code) {
List<Integer> ret = super.getBranches(code);
int length = getTotalActionLength();
int jmp = code.adr2pos(getAddress() + length + offset);
long targetAddress = getTargetAddress();
int jmp = code.adr2pos(targetAddress);
int after = code.adr2pos(getAddress() + length);
if (jmp == -1) {
Logger.getLogger(ActionIf.class.getName()).log(Level.SEVERE, "Invalid IF jump to ofs" + Helper.formatAddress(getAddress() + length + offset));
Logger.getLogger(ActionIf.class.getName()).log(Level.SEVERE, "Invalid IF jump to ofs" + Helper.formatAddress(targetAddress));
ret.add(after);
} else {
ret.add(jmp);

View File

@@ -117,11 +117,12 @@ public class ActionJump extends Action {
@Override
public List<Integer> getBranches(GraphSource code) {
List<Integer> ret = super.getBranches(code);
int length = getBytesLength();
int ofs = code.adr2pos(getAddress() + length + offset);
long targetAddress = getTargetAddress();
int ofs = code.adr2pos(targetAddress);
if (ofs == -1) {
int length = getBytesLength();
ofs = code.adr2pos(getAddress() + length);
Logger.getLogger(ActionJump.class.getName()).log(Level.SEVERE, "Invalid jump to ofs" + Helper.formatAddress(getAddress() + length + offset) + " from ofs" + Helper.formatAddress(getAddress()));
Logger.getLogger(ActionJump.class.getName()).log(Level.SEVERE, "Invalid jump to ofs" + Helper.formatAddress(targetAddress) + " from ofs" + Helper.formatAddress(getAddress()));
}
ret.add(ofs);
return ret;