From 0e376b32d2968382730cdfd9af1affee5b0984c8 Mon Sep 17 00:00:00 2001 From: Anders Schack-Mulligen Date: Tue, 13 Sep 2022 13:14:22 +0200 Subject: [PATCH] Java: extend typeflow tests to cover union types. --- .../library-tests/typeflow/UnionTypes.java | 54 +++++++++++++++++++ .../library-tests/typeflow/typeflow.expected | 1 + .../typeflow/uniontypeflow.expected | 24 +++++++++ .../library-tests/typeflow/uniontypeflow.ql | 10 ++++ 4 files changed, 89 insertions(+) create mode 100644 java/ql/test/library-tests/typeflow/UnionTypes.java create mode 100644 java/ql/test/library-tests/typeflow/uniontypeflow.expected create mode 100644 java/ql/test/library-tests/typeflow/uniontypeflow.ql diff --git a/java/ql/test/library-tests/typeflow/UnionTypes.java b/java/ql/test/library-tests/typeflow/UnionTypes.java new file mode 100644 index 00000000000..a82b3828d2f --- /dev/null +++ b/java/ql/test/library-tests/typeflow/UnionTypes.java @@ -0,0 +1,54 @@ +import java.util.*; +import java.util.concurrent.*; + +public class UnionTypes { + public void m1() { + m1_map_put(new LinkedHashMap<>(), "k", "v"); + m1_map_put(new ConcurrentHashMap<>(), "k", "v"); + } + + private void m1_map_put(Map m, String k, String v) { + m.put(k, v); + } + + static class Sup { } + interface Inter { } + + static class A1 extends Sup implements Inter { } + static class A2 extends Sup implements Inter { } + static class A3 extends Sup { } + static class A4 extends Sup implements Inter { } + static class A2sub extends A2 { } + + private void m2(boolean b) { + scc1(new A1(), 10); + Sup x = b ? new A2() : new A3(); + scc2(x, 10); + } + + private void scc1(Sup x1, int i) { + scc2(x1, i); + } + + private void scc2(Sup x2, int i) { + scc3(x2, i); + } + + private void scc3(Sup x3, int i) { + next(x3); + if (i > 0) + scc1(x3, --i); + } + + private void next(Sup x) { + if (x instanceof Inter) { + x.hashCode(); + } + } + + void m3(Object d) { + if (d instanceof A1 || d instanceof A2 || d instanceof A3) { + d.hashCode(); + } + } +} diff --git a/java/ql/test/library-tests/typeflow/typeflow.expected b/java/ql/test/library-tests/typeflow/typeflow.expected index 1f879fe37ea..021d04b55d3 100644 --- a/java/ql/test/library-tests/typeflow/typeflow.expected +++ b/java/ql/test/library-tests/typeflow/typeflow.expected @@ -13,3 +13,4 @@ | A.java:67:22:67:22 | x | Integer | false | | A.java:70:23:70:24 | x2 | Integer | false | | A.java:92:18:92:18 | n | Integer | false | +| UnionTypes.java:45:7:45:7 | x | Inter | false | diff --git a/java/ql/test/library-tests/typeflow/uniontypeflow.expected b/java/ql/test/library-tests/typeflow/uniontypeflow.expected new file mode 100644 index 00000000000..7c595175301 --- /dev/null +++ b/java/ql/test/library-tests/typeflow/uniontypeflow.expected @@ -0,0 +1,24 @@ +| UnionTypes.java:11:5:11:5 | m | 2 | ConcurrentHashMap | false | +| UnionTypes.java:11:5:11:5 | m | 2 | LinkedHashMap | false | +| UnionTypes.java:26:10:26:10 | x | 2 | A2 | true | +| UnionTypes.java:26:10:26:10 | x | 2 | A3 | false | +| UnionTypes.java:30:10:30:11 | x1 | 3 | A1 | false | +| UnionTypes.java:30:10:30:11 | x1 | 3 | A2 | true | +| UnionTypes.java:30:10:30:11 | x1 | 3 | A3 | false | +| UnionTypes.java:34:10:34:11 | x2 | 3 | A1 | false | +| UnionTypes.java:34:10:34:11 | x2 | 3 | A2 | true | +| UnionTypes.java:34:10:34:11 | x2 | 3 | A3 | false | +| UnionTypes.java:38:10:38:11 | x3 | 3 | A1 | false | +| UnionTypes.java:38:10:38:11 | x3 | 3 | A2 | true | +| UnionTypes.java:38:10:38:11 | x3 | 3 | A3 | false | +| UnionTypes.java:40:12:40:13 | x3 | 3 | A1 | false | +| UnionTypes.java:40:12:40:13 | x3 | 3 | A2 | true | +| UnionTypes.java:40:12:40:13 | x3 | 3 | A3 | false | +| UnionTypes.java:44:9:44:9 | x | 3 | A1 | false | +| UnionTypes.java:44:9:44:9 | x | 3 | A2 | true | +| UnionTypes.java:44:9:44:9 | x | 3 | A3 | false | +| UnionTypes.java:45:7:45:7 | x | 2 | A1 | false | +| UnionTypes.java:45:7:45:7 | x | 2 | A2 | true | +| UnionTypes.java:51:7:51:7 | d | 3 | A1 | false | +| UnionTypes.java:51:7:51:7 | d | 3 | A2 | false | +| UnionTypes.java:51:7:51:7 | d | 3 | A3 | false | diff --git a/java/ql/test/library-tests/typeflow/uniontypeflow.ql b/java/ql/test/library-tests/typeflow/uniontypeflow.ql new file mode 100644 index 00000000000..769ac89172c --- /dev/null +++ b/java/ql/test/library-tests/typeflow/uniontypeflow.ql @@ -0,0 +1,10 @@ +import java +import semmle.code.java.dataflow.TypeFlow + +int countUnionTypes(Expr e) { + result = strictcount(RefType t, boolean exact | exprUnionTypeFlow(e, t, exact)) +} + +from RValue e, RefType t, boolean exact +where exprUnionTypeFlow(e, t, exact) +select e, countUnionTypes(e), t.toString(), exact