java: test type tracking through flow summaries

This commit is contained in:
Rasmus Lerchedahl Petersen
2023-06-07 11:18:53 +02:00
parent 16bc584bd1
commit 76e1c6f76f
3 changed files with 42 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
import java.util.*;
public class CallableViaSummary {
public interface Element {
public void handle(String message);
}
public void main(String[] args) {
List<Element> elements = new ArrayList<>();
List<Element> elements2 = new ArrayList<>();
elements.add(new Element() {
@Override
public void handle(String message) {
System.out.println(message);
}
});
elements.add(message -> System.out.println(message));
// This dispatches to the two added elements because
// the summary of ArrayList causes flow via type tracking.
elements.get(0).handle("Hello, world!");
// This does not dispatch to anything, showing that the
// open-world assumption does not apply
// (and hence that type tracking is necessary above).
elements2.get(0).handle("Hello, world!");
}
}

View File

@@ -0,0 +1,2 @@
| CallableViaSummary.java:24:9:24:47 | handle(...) | CallableViaSummary.java:15:25:15:30 | handle |
| CallableViaSummary.java:24:9:24:47 | handle(...) | CallableViaSummary.java:20:22:20:59 | handle |

View File

@@ -0,0 +1,9 @@
import java
import semmle.code.java.dispatch.VirtualDispatch
from MethodAccess ma, Method m
where
m = viableImpl(ma) and
m.fromSource() and
ma.getFile().toString().matches("CallableViaSummary")
select ma, m