From 09ffeb90be2396d59e1ac4c22c781148695c80e0 Mon Sep 17 00:00:00 2001 From: Marc Miltenberger Date: Thu, 10 Oct 2024 23:26:40 +0200 Subject: [PATCH 1/3] Fix a bug where the types in Dalvik code are not accurate for floats in certain conditions --- src/main/java/soot/dexpler/DexBody.java | 31 ++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main/java/soot/dexpler/DexBody.java b/src/main/java/soot/dexpler/DexBody.java index 469cc8714e3..409fe043001 100755 --- a/src/main/java/soot/dexpler/DexBody.java +++ b/src/main/java/soot/dexpler/DexBody.java @@ -94,6 +94,7 @@ 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; @@ -101,6 +102,7 @@ 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; @@ -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; @@ -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); @@ -1005,8 +1012,9 @@ 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; @@ -1014,6 +1022,27 @@ public void convertFloatsAndDoubles(Body b, final Jimple jimple) { 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("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("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()) { From b522c59a1e965d3f461050a03253a4701e67c33b Mon Sep 17 00:00:00 2001 From: Marc Miltenberger Date: Thu, 10 Oct 2024 23:36:34 +0200 Subject: [PATCH 2/3] Avoid naming conflicts --- src/main/java/soot/dexpler/DexBody.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/soot/dexpler/DexBody.java b/src/main/java/soot/dexpler/DexBody.java index 409fe043001..477ffdb3f13 100755 --- a/src/main/java/soot/dexpler/DexBody.java +++ b/src/main/java/soot/dexpler/DexBody.java @@ -1026,7 +1026,7 @@ public void convertFloatsAndDoubles(Body b, final Jimple jimple) { || rop instanceof RemExpr) { if (isDouble) { if (convResultDouble == null) { - convResultDouble = jimple.newLocal("lclConvToDouble", DoubleType.v()); + convResultDouble = jimple.newLocal(freshLocalName("lclConvToDouble"), DoubleType.v()); b.getLocals().add(convResultDouble); } Value prev = def.getLeftOp(); @@ -1035,7 +1035,7 @@ public void convertFloatsAndDoubles(Body b, final Jimple jimple) { } if (isFloat) { if (convResultFloat == null) { - convResultFloat = jimple.newLocal("lclConvToFloat", FloatType.v()); + convResultFloat = jimple.newLocal(freshLocalName("lclConvToFloat"), FloatType.v()); b.getLocals().add(convResultFloat); } Value prev = def.getLeftOp(); @@ -1061,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( @@ -1072,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(("lclConvToFloat" + idxConvVar), FloatType.v()); b.getLocals().add(convFloat[idxConvVar]); } units.insertBefore( From 5e5a50e3af4fd90f1d4a6d60abc540631595ab9c Mon Sep 17 00:00:00 2001 From: Marc Miltenberger Date: Thu, 10 Oct 2024 23:52:38 +0200 Subject: [PATCH 3/3] One freshLocalName call was missing --- src/main/java/soot/dexpler/DexBody.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/soot/dexpler/DexBody.java b/src/main/java/soot/dexpler/DexBody.java index 477ffdb3f13..20c2c58e640 100755 --- a/src/main/java/soot/dexpler/DexBody.java +++ b/src/main/java/soot/dexpler/DexBody.java @@ -1072,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(