From a94601c8c0059ddad530f81852dfc91cf4799c3a Mon Sep 17 00:00:00 2001 From: Chris Smowton Date: Tue, 3 Sep 2024 17:56:24 +0100 Subject: [PATCH] Add test checking for entity counts and def-use matches under aliasing --- .../semmle/go/aliases/defsuses.go | 16 ++++++++ .../semmle/go/aliases/test.expected | 33 +++++++++++++++++ .../library-tests/semmle/go/aliases/test.ql | 37 +++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 go/ql/test/library-tests/semmle/go/aliases/defsuses.go create mode 100644 go/ql/test/library-tests/semmle/go/aliases/test.expected create mode 100644 go/ql/test/library-tests/semmle/go/aliases/test.ql diff --git a/go/ql/test/library-tests/semmle/go/aliases/defsuses.go b/go/ql/test/library-tests/semmle/go/aliases/defsuses.go new file mode 100644 index 000000000000..9672b82504c9 --- /dev/null +++ b/go/ql/test/library-tests/semmle/go/aliases/defsuses.go @@ -0,0 +1,16 @@ +package aliases + +type IntAlias = int + +type S1 = struct{ x int } +type S2 = struct{ x IntAlias } + +func Test1() int { + obj := S1{1} + obj.x = 2 + + var ptr *S2 + ptr = &S1 + + return ptr.x +} diff --git a/go/ql/test/library-tests/semmle/go/aliases/test.expected b/go/ql/test/library-tests/semmle/go/aliases/test.expected new file mode 100644 index 000000000000..92ffe12acdf1 --- /dev/null +++ b/go/ql/test/library-tests/semmle/go/aliases/test.expected @@ -0,0 +1,33 @@ +lowLevelDefs +| defsuses.go:3:6:3:13 | IntAlias | defsuses.go:3:6:3:13 | IntAlias (1 declaration sites) | +| defsuses.go:5:6:5:7 | S1 | defsuses.go:5:6:5:7 | S1 (1 declaration sites) | +| defsuses.go:5:20:5:20 | x | defsuses.go:5:20:5:20 | x (2 declaration sites) | +| defsuses.go:5:20:5:20 | x | defsuses.go:6:20:6:20 | x (2 declaration sites) | +| defsuses.go:6:6:6:7 | S2 | defsuses.go:6:6:6:7 | S2 (1 declaration sites) | +| defsuses.go:6:20:6:20 | x | defsuses.go:5:20:5:20 | x (2 declaration sites) | +| defsuses.go:6:20:6:20 | x | defsuses.go:6:20:6:20 | x (2 declaration sites) | +| defsuses.go:8:6:8:10 | Test1 | defsuses.go:8:6:8:10 | Test1 (1 declaration sites) | +| defsuses.go:9:2:9:4 | obj | defsuses.go:9:2:9:4 | obj (1 declaration sites) | +| defsuses.go:12:6:12:8 | ptr | defsuses.go:12:6:12:8 | ptr (1 declaration sites) | +lowLevelUses +| defsuses.go:3:17:3:19 | int | file://:0:0:0:0 | int (0 declaration sites) | +| defsuses.go:5:22:5:24 | int | file://:0:0:0:0 | int (0 declaration sites) | +| defsuses.go:6:22:6:29 | IntAlias | defsuses.go:3:6:3:13 | IntAlias (1 declaration sites) | +| defsuses.go:8:14:8:16 | int | file://:0:0:0:0 | int (0 declaration sites) | +| defsuses.go:9:9:9:10 | S1 | defsuses.go:5:6:5:7 | S1 (1 declaration sites) | +| defsuses.go:10:2:10:4 | obj | defsuses.go:9:2:9:4 | obj (1 declaration sites) | +| defsuses.go:10:6:10:6 | x | defsuses.go:5:20:5:20 | x (2 declaration sites) | +| defsuses.go:10:6:10:6 | x | defsuses.go:6:20:6:20 | x (2 declaration sites) | +| defsuses.go:12:11:12:12 | S2 | defsuses.go:6:6:6:7 | S2 (1 declaration sites) | +| defsuses.go:13:2:13:4 | ptr | defsuses.go:12:6:12:8 | ptr (1 declaration sites) | +| defsuses.go:13:9:13:10 | S1 | defsuses.go:5:6:5:7 | S1 (1 declaration sites) | +| defsuses.go:15:9:15:11 | ptr | defsuses.go:12:6:12:8 | ptr (1 declaration sites) | +| defsuses.go:15:13:15:13 | x | defsuses.go:5:20:5:20 | x (2 declaration sites) | +| defsuses.go:15:13:15:13 | x | defsuses.go:6:20:6:20 | x (2 declaration sites) | +distinctDefinedXs +| 1 | +distinctUsedXs +| 1 | +fieldUseUsePairs +| defsuses.go:10:6:10:6 | x | defsuses.go:15:13:15:13 | x | +| defsuses.go:15:13:15:13 | x | defsuses.go:10:6:10:6 | x | diff --git a/go/ql/test/library-tests/semmle/go/aliases/test.ql b/go/ql/test/library-tests/semmle/go/aliases/test.ql new file mode 100644 index 000000000000..8881baa7b4d5 --- /dev/null +++ b/go/ql/test/library-tests/semmle/go/aliases/test.ql @@ -0,0 +1,37 @@ +import go + +newtype TEntityWithDeclInfo = MkEntityWithDeclInfo(Entity e) + +class EntityWithDeclInfo extends TEntityWithDeclInfo { + string toString() { + exists(Entity e | this = MkEntityWithDeclInfo(e) | + result = e.toString() + " (" + count(e.getDeclaration()) + " declaration sites)" + ) + } + + predicate hasLocationInfo( + string filepath, int startline, int startcolumn, int endline, int endcolumn + ) { + exists(Entity e | this = MkEntityWithDeclInfo(e) | + e.hasLocationInfo(filepath, startline, startcolumn, endline, endcolumn) + ) + } +} + +query predicate lowLevelDefs(Ident i, EntityWithDeclInfo ewrapped) { + exists(Entity e | ewrapped = MkEntityWithDeclInfo(e) | defs(i, e)) +} + +query predicate lowLevelUses(Ident i, EntityWithDeclInfo ewrapped) { + exists(Entity e | ewrapped = MkEntityWithDeclInfo(e) | uses(i, e)) +} + +query predicate distinctDefinedXs(int ct) { + ct = count(Entity e | defs(_, e) and e.toString() = "x") +} + +query predicate distinctUsedXs(int ct) { ct = count(Entity e | uses(_, e) and e.toString() = "x") } + +query predicate fieldUseUsePairs(Ident i1, Ident i2) { + exists(Field e | uses(i1, e) and uses(i2, e) and i1 != i2) +}