use of soot.toDex.instructions.InsnWithOffset in project soot by Sable.
the class ExprVisitor method buildComparingBinaryInsn.
private Insn buildComparingBinaryInsn(String binaryOperation, Value firstOperand, Value secondOperand) {
constantV.setOrigStmt(origStmt);
Value realFirstOperand = fixNullConstant(firstOperand);
Value realSecondOperand = fixNullConstant(secondOperand);
Register firstOpReg = regAlloc.asImmediate(realFirstOperand, constantV);
// select fitting opcode ("if" or "if-zero")
InsnWithOffset comparingBinaryInsn;
String opcodeName = "IF_" + binaryOperation;
boolean secondOpIsInt = realSecondOperand instanceof IntConstant;
boolean secondOpIsZero = secondOpIsInt && ((IntConstant) realSecondOperand).value == 0;
if (secondOpIsZero) {
Opcode opc = Opcode.valueOf(opcodeName.concat("Z"));
comparingBinaryInsn = new Insn21t(opc, firstOpReg);
comparingBinaryInsn.setTarget(targetForOffset);
} else {
Opcode opc = Opcode.valueOf(opcodeName);
Register secondOpReg = regAlloc.asImmediate(realSecondOperand, constantV);
comparingBinaryInsn = new Insn22t(opc, firstOpReg, secondOpReg);
comparingBinaryInsn.setTarget(targetForOffset);
}
return comparingBinaryInsn;
}
use of soot.toDex.instructions.InsnWithOffset in project soot by Sable.
the class DexPrinter method insertIntermediateJump.
/**
* Creates an intermediate jump instruction between the original jump
* instruction and its target
*
* @param targetInsPos
* The jump target index
* @param jumpInsPos
* The position of the jump instruction
* @param stmtV
* The statement visitor used for constructing the instructions
* @param instructions
* The list of Dalvik instructions
* @param labelAssigner
* The label assigner to be used for creating new labels
*/
private void insertIntermediateJump(int targetInsPos, int jumpInsPos, StmtVisitor stmtV, List<BuilderInstruction> instructions, LabelAssigner labelAssigner) {
// Get the original jump instruction
BuilderInstruction originalJumpInstruction = instructions.get(jumpInsPos);
Insn originalJumpInsn = stmtV.getInsnForInstruction(originalJumpInstruction);
if (originalJumpInsn == null)
return;
if (!(originalJumpInsn instanceof InsnWithOffset))
throw new RuntimeException("Unexpected jump instruction target");
InsnWithOffset offsetInsn = (InsnWithOffset) originalJumpInsn;
// If this is goto instruction, we can just replace it
if (originalJumpInsn instanceof Insn10t) {
if (originalJumpInsn.getOpcode() == Opcode.GOTO) {
Insn30t newJump = new Insn30t(Opcode.GOTO_32);
newJump.setTarget(((Insn10t) originalJumpInsn).getTarget());
BuilderInstruction newJumpInstruction = newJump.getRealInsn(labelAssigner);
instructions.remove(jumpInsPos);
instructions.add(jumpInsPos, newJumpInstruction);
stmtV.fakeNewInsn(stmtV.getStmtForInstruction(originalJumpInstruction), newJump, newJumpInstruction);
return;
}
}
// Find a position where we can jump to
int distance = Math.max(targetInsPos, jumpInsPos) - Math.min(targetInsPos, jumpInsPos);
if (distance == 0)
return;
int newJumpIdx = Math.min(targetInsPos, jumpInsPos) + (distance / 2);
int sign = (int) Math.signum(targetInsPos - jumpInsPos);
// label may otherwise be attached to the wrong statement
do {
Stmt newStmt = stmtV.getStmtForInstruction(instructions.get(newJumpIdx));
Stmt prevStmt = newJumpIdx > 0 ? stmtV.getStmtForInstruction(instructions.get(newJumpIdx - 1)) : null;
if (newStmt == null || newStmt == prevStmt) {
newJumpIdx -= sign;
if (newJumpIdx < 0 || newJumpIdx >= instructions.size())
throw new RuntimeException("No position for inserting intermediate " + "jump instruction found");
} else
break;
} while (true);
// Create a jump instruction from the middle to the end
NopStmt nop = Jimple.v().newNopStmt();
Insn30t newJump = new Insn30t(Opcode.GOTO_32);
newJump.setTarget(stmtV.getStmtForInstruction(instructions.get(targetInsPos)));
BuilderInstruction newJumpInstruction = newJump.getRealInsn(labelAssigner);
instructions.add(newJumpIdx, newJumpInstruction);
stmtV.fakeNewInsn(nop, newJump, newJumpInstruction);
// We have added something, so we need to fix indices
if (newJumpIdx <= jumpInsPos)
jumpInsPos++;
if (newJumpIdx <= targetInsPos)
targetInsPos++;
// Jump from the original instruction to the new one in the middle
offsetInsn.setTarget(nop);
BuilderInstruction replacementJumpInstruction = offsetInsn.getRealInsn(labelAssigner);
assert instructions.get(jumpInsPos) == originalJumpInstruction;
instructions.remove(jumpInsPos);
instructions.add(jumpInsPos, replacementJumpInstruction);
stmtV.fakeNewInsn(stmtV.getStmtForInstruction(originalJumpInstruction), originalJumpInsn, replacementJumpInstruction);
// Our indices are still fine, because we just replaced something
Stmt afterNewJump = stmtV.getStmtForInstruction(instructions.get(newJumpIdx + 1));
// Make the original control flow jump around the new artificial jump
// instruction
Insn10t jumpAround = new Insn10t(Opcode.GOTO);
jumpAround.setTarget(afterNewJump);
BuilderInstruction jumpAroundInstruction = jumpAround.getRealInsn(labelAssigner);
instructions.add(newJumpIdx, jumpAroundInstruction);
stmtV.fakeNewInsn(Jimple.v().newNopStmt(), jumpAround, jumpAroundInstruction);
}
use of soot.toDex.instructions.InsnWithOffset in project soot by Sable.
the class DexPrinter method fixLongJumps.
/**
* Fixes long jumps that exceed the maximum distance for the respective jump
* type
*
* @param instructions
* The list of generated dalvik instructions
* @param labelAssigner
* The label assigner that maps statements to labels
* @param stmtV
* The statement visitor used to produce the dalvik instructions
*/
private void fixLongJumps(List<BuilderInstruction> instructions, LabelAssigner labelAssigner, StmtVisitor stmtV) {
// Only construct the maps once and update them afterwards
Map<Instruction, Integer> instructionsToIndex = new HashMap<Instruction, Integer>();
List<Integer> instructionsToOffsets = new ArrayList<Integer>();
Map<Label, Integer> labelsToOffsets = new HashMap<Label, Integer>();
Map<Label, Integer> labelsToIndex = new HashMap<Label, Integer>();
boolean hasChanged;
l0: do {
// Look for changes anew every time
hasChanged = false;
instructionsToOffsets.clear();
// Build a mapping between instructions and offsets
{
int offset = 0;
int idx = 0;
for (BuilderInstruction bi : instructions) {
instructionsToIndex.put(bi, idx);
instructionsToOffsets.add(offset);
Stmt origStmt = stmtV.getStmtForInstruction(bi);
if (origStmt != null) {
Label lbl = labelAssigner.getLabelUnsafe(origStmt);
if (lbl != null) {
labelsToOffsets.put(lbl, offset);
labelsToIndex.put(lbl, idx);
}
}
offset += (bi.getFormat().size / 2);
idx++;
}
}
// Look for references to labels
for (int j = 0; j < instructions.size(); j++) {
BuilderInstruction bj = instructions.get(j);
if (bj instanceof BuilderOffsetInstruction) {
BuilderOffsetInstruction boj = (BuilderOffsetInstruction) bj;
// Compute the distance between the instructions
Insn jumpInsn = stmtV.getInsnForInstruction(boj);
if (jumpInsn instanceof InsnWithOffset) {
InsnWithOffset offsetInsn = (InsnWithOffset) jumpInsn;
Integer targetOffset = labelsToOffsets.get(boj.getTarget());
if (targetOffset == null)
continue;
int distance = instructionsToOffsets.get(j) - targetOffset;
if (Math.abs(distance) > offsetInsn.getMaxJumpOffset()) {
// We need intermediate jumps
insertIntermediateJump(labelsToIndex.get(boj.getTarget()), j, stmtV, instructions, labelAssigner);
hasChanged = true;
continue l0;
}
}
}
}
} while (hasChanged);
}
Aggregations