Skip to content

Commit

Permalink
Improve typer
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcMil committed Oct 21, 2024
1 parent 989fc81 commit 3299680
Showing 1 changed file with 60 additions and 2 deletions.
62 changes: 60 additions & 2 deletions src/main/java/soot/dexpler/DexBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
import soot.dexpler.tags.FloatOpTag;
import soot.dexpler.tags.IntOpTag;
import soot.dexpler.tags.IntOrFloatOpTag;
import soot.dexpler.tags.LongOpTag;
import soot.dexpler.tags.LongOrDoubleOpTag;
import soot.dexpler.tags.ShortOpTag;
import soot.dexpler.typing.DalvikTyper;
Expand All @@ -125,6 +126,7 @@
import soot.jimple.DoubleConstant;
import soot.jimple.EqExpr;
import soot.jimple.FloatConstant;
import soot.jimple.GotoStmt;
import soot.jimple.IfStmt;
import soot.jimple.IntConstant;
import soot.jimple.InvokeExpr;
Expand All @@ -133,6 +135,8 @@
import soot.jimple.LongConstant;
import soot.jimple.MulExpr;
import soot.jimple.NeExpr;
import soot.jimple.NegExpr;
import soot.jimple.NopStmt;
import soot.jimple.NullConstant;
import soot.jimple.NumericConstant;
import soot.jimple.OrExpr;
Expand Down Expand Up @@ -1005,6 +1009,23 @@ public Value visit(Value op, Type useType, Stmt stmt, boolean checkOnly) {

}
}.inferTypes();
for (Unit u : jBody.getUnits()) {
Stmt s = (Stmt) u;
if (s.containsArrayRef() && s instanceof AssignStmt) {
AssignStmt assign = (AssignStmt) s;
Value lop = assign.getLeftOp();
Value rop = assign.getRightOp();
if (lop.getType() instanceof FloatType && rop instanceof IntConstant) {
IntConstant intC = (IntConstant) rop;
assign.setRightOp(FloatConstant.v(Float.intBitsToFloat(intC.value)));
}
if (lop.getType() instanceof DoubleType && rop instanceof LongConstant) {
LongConstant longC = (LongConstant) rop;
assign.setRightOp(DoubleConstant.v(Double.longBitsToDouble(longC.value)));
}
}
}

checkUnrealizableCasts();

// Shortcut: Reduce array initializations
Expand Down Expand Up @@ -1235,6 +1256,20 @@ public Value visit(Value op, Type useType, Stmt stmt, boolean checkOnly) {
// Check that we don't have anything weird
checkUnrealizableCasts();

UnitPatchingChain units = jBody.getUnits();
Unit u = units.getFirst();
while (u != null) {
if (u instanceof GotoStmt) {
GotoStmt gt = (GotoStmt) u;
if (gt.getTarget() == gt) {
//There are crazy cases like that in the wild.
NopStmt nop = jimple.newNopStmt();
units.insertBefore(nop, u);
gt.setTarget(nop);
}
}
u = units.getSuccOf(u);
}
// t_whole_jimplification.end();

return jBody;
Expand Down Expand Up @@ -1269,6 +1304,7 @@ private void handleKnownDexArrayTypes(Body b, Jimple jimple, MultiMap<Local, Typ
while (u != null) {
if (u instanceof AssignStmt) {
AssignStmt assign = ((AssignStmt) u);
Value lop = assign.getLeftOp();
Value rop = assign.getRightOp();
if (rop instanceof ArrayRef) {
for (Tag tg : u.getTags()) {
Expand Down Expand Up @@ -1333,6 +1369,28 @@ private void handleKnownDexTypes(Body b, final Jimple jimple) {
if (u instanceof AssignStmt) {
AssignStmt def = (AssignStmt) u;
Value rop = def.getRightOp();
if (rop instanceof NegExpr) {
boolean isDouble = u.hasTag(DoubleOpTag.NAME);
boolean isFloat = u.hasTag(FloatOpTag.NAME);
boolean isLong = u.hasTag(LongOpTag.NAME);
NegExpr neg = ((NegExpr) rop);
Value op = neg.getOp();
Type t = null;
//As for ints, shorts etc.: the type assigner
//already handles this automatically
if (isDouble) {
t = DoubleType.v();
} else if (isFloat) {
t = FloatType.v();
} else if (isLong) {
t = LongType.v();
}
if (t != null) {
Local l = (Local) op;
l.setType(t);
}

}
if (rop instanceof BinopExpr) {
boolean isDouble = u.hasTag(DoubleOpTag.NAME);
boolean isFloat = u.hasTag(FloatOpTag.NAME);
Expand Down Expand Up @@ -1446,10 +1504,10 @@ private void handleKnownDexTypes(Body b, final Jimple jimple) {
}
if (rop instanceof Constant) {
Constant c = (Constant) assign.getRightOp();
if (tl instanceof DoubleType) {
if (tl instanceof DoubleType && c instanceof LongConstant) {
long vVal = ((LongConstant) c).value;
assign.setRightOp(DoubleConstant.v(Double.longBitsToDouble(vVal)));
} else if (tl instanceof FloatType) {
} else if (tl instanceof FloatType && c instanceof IntConstant) {
int vVal = ((IntConstant) c).value;
assign.setRightOp(FloatConstant.v(Float.intBitsToFloat(vVal)));
}
Expand Down

0 comments on commit 3299680

Please sign in to comment.