Skip to content

Commit

Permalink
Merge pull request #2110 from MarcMil/fixes
Browse files Browse the repository at this point in the history
Fix a bug where the types in Dalvik code are not accurate for floats …
  • Loading branch information
StevenArzt authored Oct 11, 2024
2 parents e5714cb + 0fd35bf commit 9fde961
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions src/main/java/soot/dexpler/DexBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,15 @@
import soot.dexpler.tags.DoubleOpTag;
import soot.dexpler.tags.FloatOpTag;
import soot.dexpler.typing.DalvikTyper;
import soot.jimple.AddExpr;
import soot.jimple.AssignStmt;
import soot.jimple.BinopExpr;
import soot.jimple.CastExpr;
import soot.jimple.CaughtExceptionRef;
import soot.jimple.ConditionExpr;
import soot.jimple.Constant;
import soot.jimple.DefinitionStmt;
import soot.jimple.DivExpr;
import soot.jimple.DoubleConstant;
import soot.jimple.EqExpr;
import soot.jimple.FloatConstant;
Expand All @@ -109,9 +111,12 @@
import soot.jimple.Jimple;
import soot.jimple.JimpleBody;
import soot.jimple.LongConstant;
import soot.jimple.MulExpr;
import soot.jimple.NeExpr;
import soot.jimple.NullConstant;
import soot.jimple.NumericConstant;
import soot.jimple.RemExpr;
import soot.jimple.SubExpr;
import soot.jimple.internal.JIdentityStmt;
import soot.jimple.toolkits.base.Aggregator;
import soot.jimple.toolkits.scalar.ConditionalBranchFolder;
Expand Down Expand Up @@ -784,6 +789,8 @@ public Body jimplify(Body b, SootMethod m) {
UnconditionalBranchFolder.v().transform(jBody);
}
DexFillArrayDataTransformer.v().transform(jBody);
//SharedInitializationLocalSplitter destroys the inserted casts, so we have to reintroduce them
convertFloatsAndDoubles(b, jimple);

TypeAssigner.v().transform(jBody);

Expand Down Expand Up @@ -1005,15 +1012,37 @@ public Body jimplify(Body b, SootMethod m) {
public void convertFloatsAndDoubles(Body b, final Jimple jimple) {
UnitPatchingChain units = jBody.getUnits();
Unit u = units.getFirst();
Local convResultFloat = null;
Local convResultDouble = null;
Local[] convFloat = new Local[2], convDouble = new Local[2];

while (u != null) {
if (u instanceof AssignStmt) {
AssignStmt def = (AssignStmt) u;
Value rop = def.getRightOp();
if (rop instanceof BinopExpr) {
boolean isDouble = u.hasTag(DoubleOpTag.NAME);
boolean isFloat = u.hasTag(FloatOpTag.NAME);
if (rop instanceof AddExpr || rop instanceof SubExpr || rop instanceof MulExpr || rop instanceof DivExpr
|| rop instanceof RemExpr) {
if (isDouble) {
if (convResultDouble == null) {
convResultDouble = jimple.newLocal(freshLocalName("lclConvToDouble"), DoubleType.v());
b.getLocals().add(convResultDouble);
}
Value prev = def.getLeftOp();
def.setLeftOp(convResultDouble);
units.insertAfter(jimple.newAssignStmt(prev, jimple.newCastExpr(convResultDouble, DoubleType.v())), u);
}
if (isFloat) {
if (convResultFloat == null) {
convResultFloat = jimple.newLocal(freshLocalName("lclConvToFloat"), FloatType.v());
b.getLocals().add(convResultFloat);
}
Value prev = def.getLeftOp();
def.setLeftOp(convResultFloat);
units.insertAfter(jimple.newAssignStmt(prev, jimple.newCastExpr(convResultFloat, FloatType.v())), u);
}
}
BinopExpr bop = (BinopExpr) rop;
int idxConvVar = 0;
for (ValueBox cmp : bop.getUseBoxes()) {
Expand All @@ -1032,7 +1061,7 @@ public void convertFloatsAndDoubles(Body b, final Jimple jimple) {
if (isDouble) {
if (!(c.getType() instanceof DoubleType)) {
if (convDouble[idxConvVar] == null) {
convDouble[idxConvVar] = jimple.newLocal("lclConvToDouble" + idxConvVar, DoubleType.v());
convDouble[idxConvVar] = jimple.newLocal(freshLocalName("lclConvToDouble" + idxConvVar), DoubleType.v());
b.getLocals().add(convDouble[idxConvVar]);
}
units.insertBefore(
Expand All @@ -1043,7 +1072,7 @@ public void convertFloatsAndDoubles(Body b, final Jimple jimple) {
} else if (isFloat) {
if (!(c.getType() instanceof FloatType)) {
if (convFloat[idxConvVar] == null) {
convFloat[idxConvVar] = jimple.newLocal("lclConvToFloat" + idxConvVar, FloatType.v());
convFloat[idxConvVar] = jimple.newLocal(freshLocalName("lclConvToFloat" + idxConvVar), FloatType.v());
b.getLocals().add(convFloat[idxConvVar]);
}
units.insertBefore(
Expand Down

0 comments on commit 9fde961

Please sign in to comment.