Swift: Add Dictionary models

This commit is contained in:
Robert Marsh
2023-08-10 20:52:04 +00:00
parent 70c2ef599a
commit d3c68c773a
3 changed files with 35 additions and 4 deletions

View File

@@ -7,3 +7,17 @@ private import codeql.swift.dataflow.ExternalFlow
class CanonicalDictionaryType extends BoundGenericType {
CanonicalDictionaryType() { this.getName().matches("Dictionary<%") }
}
/**
* A model for `Dictionary` and related class members that permit data flow.
*/
private class DictionarySummaries extends SummaryModelCsv {
override predicate row(string row) {
row =
[
";Dictionary;true;updateValue(_:forKey:);;;Argument[0];Argument[-1].CollectionElement.TupleElement[1];value",
";Dictionary;true;updateValue(_:forKey:);;;Argument[1];Argument[-1].CollectionElement.TupleElement[0];value",
";Dictionary;true;updateValue(_:forKey:);;;Argument[-1].CollectionElement.TupleElement[1];ReturnValue.OptionalSome;value"
]
}
}

View File

@@ -415,6 +415,13 @@ edges
| test.swift:789:17:789:28 | [...] [Collection element, Tuple element at index 1] | test.swift:793:15:793:15 | dict4 [Collection element, Tuple element at index 1] |
| test.swift:789:18:789:27 | (...) [Tuple element at index 1] | test.swift:789:17:789:28 | [...] [Collection element, Tuple element at index 1] |
| test.swift:789:20:789:27 | call to source() | test.swift:789:18:789:27 | (...) [Tuple element at index 1] |
| test.swift:790:15:790:15 | [post] dict4 [Collection element, Tuple element at index 0] | test.swift:792:15:792:15 | dict4 [Collection element, Tuple element at index 0] |
| test.swift:790:44:790:51 | call to source() | test.swift:790:15:790:15 | [post] dict4 [Collection element, Tuple element at index 0] |
| test.swift:791:15:791:15 | [post] dict4 [Collection element, Tuple element at index 1] | test.swift:793:15:793:15 | dict4 [Collection element, Tuple element at index 1] |
| test.swift:791:33:791:40 | call to source() | test.swift:791:15:791:15 | [post] dict4 [Collection element, Tuple element at index 1] |
| test.swift:792:15:792:15 | dict4 [Collection element, Tuple element at index 0] | test.swift:792:15:792:35 | call to randomElement() [some:0, Tuple element at index 0] |
| test.swift:792:15:792:35 | call to randomElement() [some:0, Tuple element at index 0] | test.swift:792:15:792:36 | ...! [Tuple element at index 0] |
| test.swift:792:15:792:36 | ...! [Tuple element at index 0] | test.swift:792:15:792:38 | .0 |
| test.swift:793:15:793:15 | dict4 [Collection element, Tuple element at index 1] | test.swift:793:15:793:35 | call to randomElement() [some:0, Tuple element at index 1] |
| test.swift:793:15:793:35 | call to randomElement() [some:0, Tuple element at index 1] | test.swift:793:15:793:36 | ...! [Tuple element at index 1] |
| test.swift:793:15:793:36 | ...! [Tuple element at index 1] | test.swift:793:15:793:38 | .1 |
@@ -876,6 +883,14 @@ nodes
| test.swift:789:17:789:28 | [...] [Collection element, Tuple element at index 1] | semmle.label | [...] [Collection element, Tuple element at index 1] |
| test.swift:789:18:789:27 | (...) [Tuple element at index 1] | semmle.label | (...) [Tuple element at index 1] |
| test.swift:789:20:789:27 | call to source() | semmle.label | call to source() |
| test.swift:790:15:790:15 | [post] dict4 [Collection element, Tuple element at index 0] | semmle.label | [post] dict4 [Collection element, Tuple element at index 0] |
| test.swift:790:44:790:51 | call to source() | semmle.label | call to source() |
| test.swift:791:15:791:15 | [post] dict4 [Collection element, Tuple element at index 1] | semmle.label | [post] dict4 [Collection element, Tuple element at index 1] |
| test.swift:791:33:791:40 | call to source() | semmle.label | call to source() |
| test.swift:792:15:792:15 | dict4 [Collection element, Tuple element at index 0] | semmle.label | dict4 [Collection element, Tuple element at index 0] |
| test.swift:792:15:792:35 | call to randomElement() [some:0, Tuple element at index 0] | semmle.label | call to randomElement() [some:0, Tuple element at index 0] |
| test.swift:792:15:792:36 | ...! [Tuple element at index 0] | semmle.label | ...! [Tuple element at index 0] |
| test.swift:792:15:792:38 | .0 | semmle.label | .0 |
| test.swift:793:15:793:15 | dict4 [Collection element, Tuple element at index 1] | semmle.label | dict4 [Collection element, Tuple element at index 1] |
| test.swift:793:15:793:35 | call to randomElement() [some:0, Tuple element at index 1] | semmle.label | call to randomElement() [some:0, Tuple element at index 1] |
| test.swift:793:15:793:36 | ...! [Tuple element at index 1] | semmle.label | ...! [Tuple element at index 1] |
@@ -1029,4 +1044,6 @@ subpaths
| test.swift:757:15:757:19 | .v3 | test.swift:747:14:747:21 | call to source() | test.swift:757:15:757:19 | .v3 | result |
| test.swift:769:15:769:22 | ...[...] | test.swift:767:16:767:23 | call to source() | test.swift:769:15:769:22 | ...[...] | result |
| test.swift:780:15:780:22 | ...[...] | test.swift:779:21:779:28 | call to source() | test.swift:780:15:780:22 | ...[...] | result |
| test.swift:792:15:792:38 | .0 | test.swift:790:44:790:51 | call to source() | test.swift:792:15:792:38 | .0 | result |
| test.swift:793:15:793:38 | .1 | test.swift:789:20:789:27 | call to source() | test.swift:793:15:793:38 | .1 | result |
| test.swift:793:15:793:38 | .1 | test.swift:791:33:791:40 | call to source() | test.swift:793:15:793:38 | .1 | result |

View File

@@ -789,8 +789,8 @@ func testDictionary() {
var dict4 = [1:source()]
sink(arg: dict4.updateValue(1, forKey: source())!)
sink(arg: dict4.updateValue(source(), forKey: 2)!)
sink(arg: dict4.randomElement()!.0) // $ MISSING: flow=791
sink(arg: dict4.randomElement()!.1) // $ flow=789 MISSING: flow=790
sink(arg: dict4.keys.randomElement()) // $ MISSING: flow=791
sink(arg: dict4.values.randomElement()) // $ MISSING: flow=789 flow=790
sink(arg: dict4.randomElement()!.0) // $ flow=790
sink(arg: dict4.randomElement()!.1) // $ flow=789 flow=791
sink(arg: dict4.keys.randomElement()) // $ MISSING: flow=790
sink(arg: dict4.values.randomElement()) // $ MISSING: flow=789 flow=791
}