use of com.googlecode.dex2jar.ir.Trap in project dex2jar by pxb1988.
the class Dex2IrAdapter method visitTryCatch.
@Override
public void visitTryCatch(DexLabel start, DexLabel end, DexLabel[] handlers, String[] types) {
LabelStmt[] xlabelStmts = new LabelStmt[types.length];
for (int i = 0; i < types.length; i++) {
xlabelStmts[i] = toLabelStmt(handlers[i]);
}
irMethod.traps.add(new Trap(toLabelStmt(start), toLabelStmt(end), xlabelStmts, types));
}
use of com.googlecode.dex2jar.ir.Trap in project dex2jar by pxb1988.
the class EndRemover method transform.
@Override
public void transform(IrMethod irMethod) {
for (Trap trap : new ArrayList<Trap>(irMethod.traps)) {
// copy the list and we can remove one from original list
LabelStmt start = null;
boolean removeTrap = true;
for (Stmt p = trap.start.getNext(); p != null && p != trap.end; ) {
boolean notThrow = Cfg.notThrow(p);
if (!notThrow) {
start = null;
p = p.getNext();
removeTrap = false;
continue;
}
switch(p.st) {
case LABEL:
if (start != null) {
move4Label(irMethod.stmts, start, p.getPre(), (LabelStmt) p);
}
start = (LabelStmt) p;
p = p.getNext();
break;
case GOTO:
case RETURN:
case RETURN_VOID:
if (start != null) {
Stmt tmp = p.getNext();
move4End(irMethod.stmts, start, p);
start = null;
p = tmp;
} else {
p = p.getNext();
}
break;
default:
p = p.getNext();
}
}
if (removeTrap) {
irMethod.traps.remove(trap);
}
}
StmtList stmts = irMethod.stmts;
for (Stmt p = stmts.getFirst(); p != null; p = p.getNext()) {
if (p.st == ST.GOTO) {
LabelStmt target = ((GotoStmt) p).target;
Stmt next = target.getNext();
if (next != null && (next.st == ST.RETURN || next.st == ST.RETURN_VOID)) {
Stmt nnext = next.clone(keepLocal);
stmts.insertAfter(p, nnext);
stmts.remove(p);
p = nnext;
}
}
}
}
use of com.googlecode.dex2jar.ir.Trap in project dex2jar by pxb1988.
the class ExceptionHandlerTrim method transform.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public void transform(IrMethod irMethod) {
List<Trap> trips = irMethod.traps;
irMethod.traps = new ArrayList();
LabelAndLocalMapper map = new LabelAndLocalMapper() {
@Override
public LabelStmt map(LabelStmt label) {
return label;
}
};
for (Trap trap : trips) {
Trap ntrap = trap.clone(map);
int status = 0;
for (Stmt p = trap.start.getNext(); p != trap.end; p = p.getNext()) {
if (!Cfg.notThrow(p)) {
if (status == 0) {
Stmt pre = p.getPre();
if (pre == null || pre.st != ST.LABEL) {
pre = Stmts.nLabel();
irMethod.stmts.insertBefore(p, pre);
}
ntrap.start = (LabelStmt) pre;
status = 1;
} else if (status == 1) {
// continue;
}
} else if (status == 1) {
Stmt pre = p.getPre();
if (pre == null || pre.st != ST.LABEL) {
pre = Stmts.nLabel();
irMethod.stmts.insertBefore(p, pre);
}
ntrap.end = (LabelStmt) pre;
irMethod.traps.add(ntrap);
status = 0;
ntrap = trap.clone(map);
}
}
if (status == 1) {
ntrap.end = trap.end;
irMethod.traps.add(ntrap);
status = 0;
}
}
}
use of com.googlecode.dex2jar.ir.Trap in project dex2jar by pxb1988.
the class SSATransformerTest method test06PhiInTrap.
@Test
public void test06PhiInTrap() {
String exType = "Ljava/lang/Exception;";
LabelStmt L1 = newLabel();
LabelStmt L2 = newLabel();
LabelStmt L3 = newLabel();
LabelStmt L4 = newLabel();
method.traps.add(new Trap(L1, L2, new LabelStmt[] { L3 }, new String[] { exType }));
Local b = addLocal("a");
Local ex = addLocal("ex");
addStmt(L1);
addStmt(nAssign(b, nString("123")));
addStmt(Stmts.nLock(b));
addStmt(L2);
addStmt(nGoto(L4));
addStmt(L3);
addStmt(nIdentity(ex, nExceptionRef(exType)));
addStmt(nAssign(b, nNull()));
addStmt(L4);
addStmt(nReturn(b));
transform();
// phi inserted
Assert.assertEquals(4, method.locals.size());
assertPhiStmt(L4);
}
use of com.googlecode.dex2jar.ir.Trap in project dex2jar by pxb1988.
the class J2IRConverter method convert0.
IrMethod convert0(String owner, MethodNode methodNode) throws AnalyzerException {
this.methodNode = methodNode;
target = populate(owner, methodNode);
if (methodNode.instructions.size() == 0) {
return target;
}
insnList = methodNode.instructions;
BitSet[] exBranch = new BitSet[insnList.size()];
parentCount = new int[insnList.size()];
initParentCount(parentCount);
BitSet handlers = new BitSet(insnList.size());
if (methodNode.tryCatchBlocks != null) {
for (TryCatchBlockNode tcb : methodNode.tryCatchBlocks) {
target.traps.add(new Trap(getLabel(tcb.start), getLabel(tcb.end), new LabelStmt[] { getLabel(tcb.handler) }, new String[] { tcb.type == null ? null : Type.getObjectType(tcb.type).getDescriptor() }));
int handlerIdx = insnList.indexOf(tcb.handler);
handlers.set(handlerIdx);
for (AbstractInsnNode p = tcb.start.getNext(); p != tcb.end; p = p.getNext()) {
BitSet x = exBranch[insnList.indexOf(p)];
if (x == null) {
x = exBranch[insnList.indexOf(p)] = new BitSet(insnList.size());
}
x.set(handlerIdx);
parentCount[handlerIdx]++;
}
}
}
Interpreter<JvmValue> interpreter = buildInterpreter();
frames = new JvmFrame[insnList.size()];
emitStmts = new ArrayList[insnList.size()];
BitSet access = new BitSet(insnList.size());
dfs(exBranch, handlers, access, interpreter);
StmtList stmts = target.stmts;
stmts.addAll(preEmit);
for (int i = 0; i < insnList.size(); i++) {
AbstractInsnNode p = insnList.get(i);
if (access.get(i)) {
List<Stmt> es = emitStmts[i];
if (es != null) {
stmts.addAll(es);
}
} else {
if (p.getType() == AbstractInsnNode.LABEL) {
stmts.add(getLabel((LabelNode) p));
}
}
}
emitStmts = null;
Queue<JvmValue> queue = new LinkedList<>();
for (int i1 = 0; i1 < frames.length; i1++) {
JvmFrame frame = frames[i1];
if (parentCount[i1] > 1 && frame != null && access.get(i1)) {
for (int j = 0; j < frame.getLocals(); j++) {
JvmValue v = frame.getLocal(j);
addToQueue(queue, v);
}
for (int j = 0; j < frame.getStackSize(); j++) {
addToQueue(queue, frame.getStack(j));
}
}
}
while (!queue.isEmpty()) {
JvmValue v = queue.poll();
getLocal(v);
if (v.parent != null) {
if (v.parent.local == null) {
queue.add(v.parent);
}
}
if (v.otherParent != null) {
for (JvmValue v2 : v.otherParent) {
if (v2.local == null) {
queue.add(v2);
}
}
}
}
Set<com.googlecode.dex2jar.ir.expr.Value> phiValues = new HashSet<>();
List<LabelStmt> phiLabels = new ArrayList<>();
for (int i = 0; i < frames.length; i++) {
JvmFrame frame = frames[i];
if (parentCount[i] > 1 && frame != null && access.get(i)) {
AbstractInsnNode p = insnList.get(i);
LabelStmt labelStmt = getLabel((LabelNode) p);
List<AssignStmt> phis = new ArrayList<>();
for (int j = 0; j < frame.getLocals(); j++) {
JvmValue v = frame.getLocal(j);
addPhi(v, phiValues, phis);
}
for (int j = 0; j < frame.getStackSize(); j++) {
addPhi(frame.getStack(j), phiValues, phis);
}
labelStmt.phis = phis;
phiLabels.add(labelStmt);
}
}
if (phiLabels.size() > 0) {
target.phiLabels = phiLabels;
}
return target;
}
Aggregations