Skip to content

Commit

Permalink
Patch cast insertion checker for Dex, where no casts are necessary be…
Browse files Browse the repository at this point in the history
…tween int <-> float and long <-> double
  • Loading branch information
MarcMil committed Oct 18, 2024
1 parent 1a61203 commit f818776
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
39 changes: 38 additions & 1 deletion src/main/java/soot/dexpler/DexBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
import soot.jimple.NullConstant;
import soot.jimple.NumericConstant;
import soot.jimple.RemExpr;
import soot.jimple.Stmt;
import soot.jimple.SubExpr;
import soot.jimple.internal.JIdentityStmt;
import soot.jimple.toolkits.base.Aggregator;
Expand Down Expand Up @@ -845,6 +846,24 @@ protected Collection<Type> reduceToAllowedTypesForLocal(Collection<Type> lcas, L
protected soot.jimple.toolkits.typing.fast.ITypingStrategy getTypingStrategy() {
ITypingStrategy useTyping = DexBody.this.getTypingStrategy();
return useTyping;
}

protected CastInsertionUseVisitor createCastInsertionUseVisitor(soot.jimple.toolkits.typing.fast.Typing tg,
soot.jimple.toolkits.typing.fast.IHierarchy h, boolean countOnly) {
return new CastInsertionUseVisitor(countOnly, jBody, tg, h) {
@Override
public Value visit(Value op, Type useType, Stmt stmt, boolean checkOnly) {
if (op instanceof LongConstant && useType instanceof DoubleType) {
//no cast necessary for Dex
return op;
}
if (op instanceof IntConstant && useType instanceof FloatType) {
//no cast necessary for Dex
return op;
}
return super.visit(op, useType, stmt, checkOnly);
}
};

}
}.inferTypes();
Expand Down Expand Up @@ -1209,7 +1228,25 @@ private void handleKnownDexTypes(Body b, final Jimple jimple) {
if (u1 instanceof AssignStmt) {
AssignStmt assign = (AssignStmt) u1;
Type tl = assign.getLeftOp().getType();
if (assign.getRightOp() instanceof Constant) {
Value rop = assign.getRightOp();
if (rop instanceof CastExpr) {
CastExpr ce = (CastExpr) rop;
if (ce.getCastType() instanceof DoubleType) {
if (ce.getOp() instanceof LongConstant) {
LongConstant lc = (LongConstant) ce.getOp();
long vVal = lc.value;
assign.setRightOp(DoubleConstant.v(Double.longBitsToDouble(vVal)));
}
}
if (ce.getCastType() instanceof FloatType) {
if (ce.getOp() instanceof IntConstant) {
IntConstant ic = (IntConstant) ce.getOp();
int vVal = ic.value;
assign.setRightOp(FloatConstant.v(Float.intBitsToFloat(vVal)));
}
}
}
if (rop instanceof Constant) {
Constant c = (Constant) assign.getRightOp();
if (tl instanceof DoubleType) {
long vVal = ((LongConstant) c).value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private void addDepend(Local v, int stmtIndex) {
public void inferTypes() {
this.split_new();
ITypingStrategy typingStrategy = getTypingStrategy();
AugEvalFunction ef = new AugEvalFunction(this.jb);
AugEvalFunction ef = createAugEvalFunction(this.jb);
BytecodeHierarchy bh = new BytecodeHierarchy();
Collection<Typing> sigma = this.applyAssignmentConstraints(typingStrategy.createTyping(this.jb.getLocals()), ef, bh);

Expand Down Expand Up @@ -183,6 +183,10 @@ public void inferTypes() {
}
}

protected AugEvalFunction createAugEvalFunction(JimpleBody jb2) {
return new AugEvalFunction(this.jb);
}

protected ITypingStrategy getTypingStrategy() {
return DefaultTypingStrategy.INSTANCE;
}
Expand Down

0 comments on commit f818776

Please sign in to comment.