Skip to content

Commit e21e84c

Browse files
committedNov 28, 2024
Fix a bug in dexpler where it could use a too narrow type
1 parent df1a36e commit e21e84c

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed
 

‎src/main/java/soot/dexpler/DexBody.java

+20-2
Original file line numberDiff line numberDiff line change
@@ -1307,7 +1307,15 @@ private static BooleanConstant fixBooleanConstant(IntConstant arg) {
13071307
* type constraints (these might be multiple valid possibilities)
13081308
*/
13091309
private void handleKnownDexArrayTypes(Body b, Jimple jimple, MultiMap<Local, Type> typeConstraints) {
1310-
1310+
Set<Local> localsSingleDefinitions = new HashSet<>(b.getLocals());
1311+
for (Unit u : b.getUnits()) {
1312+
if (u instanceof DefinitionStmt) {
1313+
Value l = ((DefinitionStmt) u).getLeftOp();
1314+
if (l instanceof Local) {
1315+
localsSingleDefinitions.remove(l);
1316+
}
1317+
}
1318+
}
13111319
UnitPatchingChain units = jBody.getUnits();
13121320
Unit u = units.getFirst();
13131321
while (u != null) {
@@ -1321,7 +1329,17 @@ private void handleKnownDexArrayTypes(Body b, Jimple jimple, MultiMap<Local, Typ
13211329
Type definiteType = dexplerTypeTag.getDefiniteType();
13221330
if (definiteType != null) {
13231331
Local prev = (Local) assign.getLeftOp();
1324-
prev.setType(definiteType);
1332+
if (!(definiteType instanceof PrimType) || localsSingleDefinitions.contains(prev)) {
1333+
prev.setType(definiteType);
1334+
} else {
1335+
//Since there are multiple definitions, e.g. for a byte retrieved from a byte[],
1336+
//there could be another non-distinct definition which uses the same variable as an int.
1337+
PrimType[] wider = DexType.getWiderTypes((PrimType) definiteType);
1338+
if (wider.length == 1) {
1339+
prev.setType(wider[0]);
1340+
}
1341+
}
1342+
13251343
ArrayType tp = ArrayType.v(definiteType, 1);
13261344

13271345
ArrayRef array = (ArrayRef) rop;

‎src/main/java/soot/dexpler/DexType.java

+11
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import soot.FloatType;
3838
import soot.IntType;
3939
import soot.LongType;
40+
import soot.PrimType;
4041
import soot.RefType;
4142
import soot.ShortType;
4243
import soot.Type;
@@ -226,4 +227,14 @@ public static String toSootAT(String type) {
226227
public String toString() {
227228
return name;
228229
}
230+
231+
public static PrimType[] getWiderTypes(PrimType tp) {
232+
if (tp instanceof ByteType) {
233+
return new PrimType[] { tp, IntType.v(), ShortType.v() };
234+
}
235+
if (tp instanceof ShortType) {
236+
return new PrimType[] { tp, IntType.v() };
237+
}
238+
return new PrimType[] { tp };
239+
}
229240
}

0 commit comments

Comments
 (0)
Please sign in to comment.