Skip to content

Commit deb2808

Browse files
committed
Hopefully the last fix for the type refinement
1 parent 148d160 commit deb2808

File tree

4 files changed

+46
-43
lines changed

4 files changed

+46
-43
lines changed

soot-infoflow-summaries/src/soot/jimple/infoflow/methodSummary/taintWrappers/SummaryTaintWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1720,7 +1720,7 @@ protected Taint addSinkTaint(MethodFlow flow, Taint taint, GapDefinition gap, St
17201720
String sBaseType = sinkType == null ? null : "" + sinkType;
17211721
if (!flow.getIgnoreTypes()) {
17221722
// Compute the new base type
1723-
Type newBaseType = manager.getTypeUtils().getMorePreciseType(sinkType, taintType);
1723+
Type newBaseType = manager.getTypeUtils().getMorePreciseType(taintType, sinkType);
17241724
if (newBaseType == null)
17251725
newBaseType = sinkType;
17261726

soot-infoflow-summaries/test/soot/jimple/infoflow/test/methodSummary/ApiClassClient.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,4 +604,10 @@ public void testTypeNarrowing() {
604604
Object[] splitted = secret.split(";");
605605
sink(splitted);
606606
}
607+
608+
public void testTypeNarrowing2() {
609+
int secret = intSource();
610+
String formatted = String.format("%d", secret);
611+
sink(formatted);
612+
}
607613
}

soot-infoflow-summaries/test/soot/jimple/infoflow/test/methodSummary/junit/SummaryTaintWrapperTests.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,11 @@ public void testTypeNarrowing() {
346346
testFlowForMethod("<soot.jimple.infoflow.test.methodSummary.ApiClassClient: void testTypeNarrowing()>", 1);
347347
}
348348

349+
@Test(timeout = 30000)
350+
public void testTypeNarrowing2() {
351+
testFlowForMethod("<soot.jimple.infoflow.test.methodSummary.ApiClassClient: void testTypeNarrowing2()>", 1);
352+
}
353+
349354
@Test
350355
public void testAllSummaries() throws URISyntaxException, IOException {
351356
EagerSummaryProvider provider = new EagerSummaryProvider(TaintWrapperFactory.DEFAULT_SUMMARY_DIR);

soot-infoflow/src/soot/jimple/infoflow/typing/TypeUtils.java

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -206,49 +206,41 @@ public boolean hasCompatibleTypesForCall(AccessPath apBase, SootClass dest) {
206206
* @return The more precise one of the two given types
207207
*/
208208
public Type getMorePreciseType(Type possibleRefinement, Type declType) {
209-
final FastHierarchy fastHierarchy = scene.getOrMakeFastHierarchy();
210-
211-
if (declType == null)
212-
return possibleRefinement;
213-
else if (possibleRefinement == null)
214-
return declType;
215-
else if (declType == possibleRefinement)
216-
return declType;
217-
else if (TypeUtils.isObjectLikeType(declType))
218-
return possibleRefinement;
219-
else if (TypeUtils.isObjectLikeType(possibleRefinement))
220-
return declType;
221-
else if (declType instanceof PrimType && possibleRefinement instanceof PrimType)
222-
return null;
223-
else if (fastHierarchy.canStoreType(possibleRefinement, declType))
224-
return possibleRefinement;
225-
else if (fastHierarchy.canStoreType(declType, possibleRefinement))
226-
return declType;
227-
else {
228-
// If one type is an array type and the other one is the base type,
229-
// we still accept the cast
230-
if (declType instanceof ArrayType && possibleRefinement instanceof ArrayType) {
231-
ArrayType at1 = (ArrayType) possibleRefinement;
232-
ArrayType at2 = (ArrayType) declType;
233-
if (at1.numDimensions != at2.numDimensions)
234-
return null;
235-
Type preciseType = getMorePreciseType(at1.getElementType(), at2.getElementType());
236-
if (preciseType == null)
237-
return null;
238-
239-
return ArrayType.v(preciseType, at2.numDimensions);
240-
} else if (declType instanceof ArrayType) {
241-
ArrayType at = (ArrayType) declType;
242-
Type preciseType = getMorePreciseType(possibleRefinement, at.getElementType());
243-
if (preciseType == null)
244-
return null;
245-
246-
return ArrayType.v(preciseType, at.numDimensions);
247-
} else if (possibleRefinement instanceof ArrayType) {
248-
ArrayType at = (ArrayType) possibleRefinement;
249-
return getMorePreciseType(at.getElementType(), declType);
250-
}
209+
if (declType instanceof ArrayType && possibleRefinement instanceof ArrayType) {
210+
ArrayType at = (ArrayType) declType;
211+
Type morePreciseType = getMorePreciseType(((ArrayType) possibleRefinement).baseType, at.baseType);
212+
if (morePreciseType != null)
213+
return ArrayType.v(morePreciseType, at.numDimensions);
214+
} else if (declType instanceof ArrayType) {
215+
ArrayType at = (ArrayType) declType;
216+
Type morePreciseType = getMorePreciseType(possibleRefinement, at.baseType);
217+
if (morePreciseType != null)
218+
return ArrayType.v(morePreciseType, at.numDimensions);
219+
} else if (possibleRefinement instanceof ArrayType) {
220+
return getMorePreciseType(((ArrayType) possibleRefinement).baseType, declType);
221+
} else {
222+
final FastHierarchy fastHierarchy = scene.getOrMakeFastHierarchy();
223+
224+
if (declType == null)
225+
return possibleRefinement;
226+
else if (possibleRefinement == null)
227+
return declType;
228+
else if (declType == possibleRefinement)
229+
return declType;
230+
// Prevent declType=Object and refinement=String[] from returning String[]
231+
// See testTypeNarrowing2
232+
else if (TypeUtils.isObjectLikeType(declType))
233+
return possibleRefinement;
234+
else if (TypeUtils.isObjectLikeType(possibleRefinement))
235+
return declType;
236+
else if (declType instanceof PrimType && possibleRefinement instanceof PrimType)
237+
return null;
238+
else if (fastHierarchy.canStoreType(possibleRefinement, declType))
239+
return possibleRefinement;
240+
else if (fastHierarchy.canStoreType(declType, possibleRefinement))
241+
return declType;
251242
}
243+
252244
return null;
253245
}
254246

0 commit comments

Comments
 (0)