Skip to content

Commit e265b07

Browse files
authored
Merge pull request #10127 from michaelnebel/csharp/clearscontent
C#: Replace clears content with CSV summaries.
2 parents 9534f31 + 0e52d2c commit e265b07

File tree

29 files changed

+365
-187
lines changed

29 files changed

+365
-187
lines changed

csharp/ql/lib/semmle/code/csharp/dataflow/ExternalFlow.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ private module Frameworks {
102102
private import semmle.code.csharp.frameworks.ServiceStack
103103
private import semmle.code.csharp.frameworks.Sql
104104
private import semmle.code.csharp.frameworks.System
105+
private import semmle.code.csharp.frameworks.system.CodeDom
105106
private import semmle.code.csharp.frameworks.system.Collections
106107
private import semmle.code.csharp.frameworks.system.collections.Concurrent
107108
private import semmle.code.csharp.frameworks.system.collections.Generic
@@ -110,6 +111,7 @@ private module Frameworks {
110111
private import semmle.code.csharp.frameworks.system.collections.Specialized
111112
private import semmle.code.csharp.frameworks.system.ComponentModel
112113
private import semmle.code.csharp.frameworks.system.componentmodel.Design
114+
private import semmle.code.csharp.frameworks.system.Configuration
113115
private import semmle.code.csharp.frameworks.system.Data
114116
private import semmle.code.csharp.frameworks.system.data.Common
115117
private import semmle.code.csharp.frameworks.system.Diagnostics
@@ -121,6 +123,7 @@ private module Frameworks {
121123
private import semmle.code.csharp.frameworks.system.IO
122124
private import semmle.code.csharp.frameworks.system.io.Compression
123125
private import semmle.code.csharp.frameworks.system.runtime.CompilerServices
126+
private import semmle.code.csharp.frameworks.system.Security
124127
private import semmle.code.csharp.frameworks.system.security.Cryptography
125128
private import semmle.code.csharp.frameworks.system.security.cryptography.X509Certificates
126129
private import semmle.code.csharp.frameworks.system.Text

csharp/ql/lib/semmle/code/csharp/dataflow/internal/FlowSummaryImpl.qll

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,6 @@ module Public {
226226
none()
227227
}
228228

229-
/**
230-
* Holds if values stored inside `content` are cleared on objects passed as
231-
* arguments at position `pos` to this callable.
232-
*
233-
* TODO: Remove once all languages support `WithoutContent` tokens.
234-
*/
235-
pragma[nomagic]
236-
predicate clearsContent(ParameterPosition pos, ContentSet content) { none() }
237-
238229
/**
239230
* Holds if the summary is auto generated.
240231
*/
@@ -328,23 +319,6 @@ module Private {
328319
SummaryComponentStack::singleton(TArgumentSummaryComponent(_))) and
329320
preservesValue = preservesValue1.booleanAnd(preservesValue2)
330321
)
331-
or
332-
exists(ParameterPosition ppos, ContentSet cs |
333-
c.clearsContent(ppos, cs) and
334-
input = SummaryComponentStack::push(SummaryComponent::withoutContent(cs), output) and
335-
output = SummaryComponentStack::argument(ppos) and
336-
preservesValue = true
337-
)
338-
}
339-
340-
private class MkClearStack extends RequiredSummaryComponentStack {
341-
override predicate required(SummaryComponent head, SummaryComponentStack tail) {
342-
exists(SummarizedCallable sc, ParameterPosition ppos, ContentSet cs |
343-
sc.clearsContent(ppos, cs) and
344-
head = SummaryComponent::withoutContent(cs) and
345-
tail = SummaryComponentStack::argument(ppos)
346-
)
347-
}
348322
}
349323

350324
/**
@@ -945,8 +919,7 @@ module Private {
945919
AccessPath inSpec, AccessPath outSpec, string kind
946920
) {
947921
summaryElement(this, inSpec, outSpec, kind, true) and
948-
not summaryElement(this, _, _, _, false) and
949-
not this.clearsContent(_, _)
922+
not summaryElement(this, _, _, _, false)
950923
}
951924

952925
private predicate relevantSummaryElement(AccessPath inSpec, AccessPath outSpec, string kind) {

csharp/ql/lib/semmle/code/csharp/dataflow/internal/FlowSummaryImplSpecific.qll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ bindingset[c]
163163
SummaryComponent interpretComponentSpecific(AccessPathToken c) {
164164
c = "Element" and result = SummaryComponent::content(any(ElementContent ec))
165165
or
166+
c = "WithoutElement" and result = SummaryComponent::withoutContent(any(ElementContent ec))
167+
or
168+
c = "WithElement" and result = SummaryComponent::withContent(any(ElementContent ec))
169+
or
166170
// Qualified names may contain commas,such as in `Tuple<,>`, so get the entire argument list
167171
// rather than an individual argument.
168172
exists(Field f |
@@ -199,6 +203,10 @@ private string getContentSpecificCsv(Content c) {
199203
string getComponentSpecificCsv(SummaryComponent sc) {
200204
exists(Content c | sc = TContentSummaryComponent(c) and result = getContentSpecificCsv(c))
201205
or
206+
exists(Content c | sc = TWithoutContentSummaryComponent(c) and result = "WithoutElement")
207+
or
208+
exists(Content c | sc = TWithContentSummaryComponent(c) and result = "WithElement")
209+
or
202210
exists(ReturnKind rk |
203211
sc = TReturnSummaryComponent(rk) and
204212
result = "ReturnValue[" + rk + "]" and

csharp/ql/lib/semmle/code/csharp/frameworks/System.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ private class SystemArrayFlowModelCsv extends SummaryModelCsv {
7171
row =
7272
[
7373
"System;Array;false;AsReadOnly<>;(T[]);;Argument[0].Element;ReturnValue.Element;value;manual",
74+
"System;Array;false;Clear;(System.Array,System.Int32,System.Int32);;Argument[0].WithoutElement;Argument[0];value;manual",
75+
"System;Array;false;Clear;(System.Array);;Argument[0].WithoutElement;Argument[0];value;manual",
7476
"System;Array;false;Clone;();;Argument[0].Element;ReturnValue.Element;value;manual",
7577
"System;Array;false;CopyTo;(System.Array,System.Int64);;Argument[this].Element;Argument[0].Element;value;manual",
7678
"System;Array;false;Find<>;(T[],System.Predicate<T>);;Argument[0].Element;Argument[1].Parameter[0];value;manual",

csharp/ql/lib/semmle/code/csharp/frameworks/microsoft/VisualBasic.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ private class MicrosoftVisualBasicCollectionFlowModelCsv extends SummaryModelCsv
77
override predicate row(string row) {
88
row =
99
[
10+
"Microsoft.VisualBasic;Collection;false;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
1011
"Microsoft.VisualBasic;Collection;false;GetEnumerator;();;Argument[this].Element;ReturnValue.Property[System.Collections.IEnumerator.Current];value;manual",
1112
"Microsoft.VisualBasic;Collection;false;get_Item;(System.Int32);;Argument[this].Element;ReturnValue;value;manual",
1213
"Microsoft.VisualBasic;Collection;false;get_Item;(System.Object);;Argument[this].Element;ReturnValue;value;manual",

csharp/ql/lib/semmle/code/csharp/frameworks/system/CodeDom.qll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import csharp
44
private import semmle.code.csharp.frameworks.System
5+
private import semmle.code.csharp.dataflow.ExternalFlow
56

67
/** The `System.CodeDome` namespace. */
78
class SystemCodeDomNamespace extends Namespace {
@@ -10,3 +11,11 @@ class SystemCodeDomNamespace extends Namespace {
1011
this.hasName("CodeDom")
1112
}
1213
}
14+
15+
/** Data flow for `System.CodeDom.CodeNamespaceImportCollection`. */
16+
private class SystemCodeDomCodeNamespaceImportCollectionFlowModelCsv extends SummaryModelCsv {
17+
override predicate row(string row) {
18+
row =
19+
"System.CodeDom;CodeNamespaceImportCollection;false;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual"
20+
}
21+
}

csharp/ql/lib/semmle/code/csharp/frameworks/system/Collections.qll

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,6 @@ private class SystemCollectionIEnumerableFlowModelCsv extends SummaryModelCsv {
4646
}
4747
}
4848

49-
/** Clear content for Clear methods in all subtypes of `System.Collections.IEnumerable`. */
50-
private class SystemCollectionsIEnumerableClearFlow extends SummarizedCallable {
51-
SystemCollectionsIEnumerableClearFlow() {
52-
this.getDeclaringType().(RefType).getABaseType*() instanceof
53-
SystemCollectionsIEnumerableInterface and
54-
this.hasName("Clear")
55-
}
56-
57-
override predicate clearsContent(ParameterPosition pos, DataFlow::ContentSet content) {
58-
(if this.(Modifiable).isStatic() then pos.getPosition() = 0 else pos.isThisParameter()) and
59-
content instanceof DataFlow::ElementContent
60-
}
61-
}
62-
6349
/** The `System.Collections.IEnumerator` interface. */
6450
class SystemCollectionsIEnumeratorInterface extends SystemCollectionsInterface {
6551
SystemCollectionsIEnumeratorInterface() { this.hasName("IEnumerator") }
@@ -96,6 +82,7 @@ private class SystemCollectionsIListFlowModelCsv extends SummaryModelCsv {
9682
row =
9783
[
9884
"System.Collections;IList;true;Add;(System.Object);;Argument[0];Argument[this].Element;value;manual",
85+
"System.Collections;IList;true;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
9986
"System.Collections;IList;true;Insert;(System.Int32,System.Object);;Argument[1];Argument[this].Element;value;manual",
10087
"System.Collections;IList;true;get_Item;(System.Int32);;Argument[this].Element;ReturnValue;value;manual",
10188
"System.Collections;IList;true;set_Item;(System.Int32,System.Object);;Argument[1];Argument[this].Element;value;manual",
@@ -115,6 +102,7 @@ private class SystemCollectionsIDictionaryFlowModelCsv extends SummaryModelCsv {
115102
[
116103
"System.Collections;IDictionary;true;Add;(System.Object,System.Object);;Argument[0];Argument[this].Element.Property[System.Collections.Generic.KeyValuePair<,>.Key];value;manual",
117104
"System.Collections;IDictionary;true;Add;(System.Object,System.Object);;Argument[1];Argument[this].Element.Property[System.Collections.Generic.KeyValuePair<,>.Value];value;manual",
105+
"System.Collections;IDictionary;true;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
118106
"System.Collections;IDictionary;true;get_Item;(System.Object);;Argument[this].Element.Property[System.Collections.Generic.KeyValuePair<,>.Value];ReturnValue;value;manual",
119107
"System.Collections;IDictionary;true;get_Keys;();;Argument[this].Element.Property[System.Collections.Generic.KeyValuePair<,>.Key];ReturnValue.Element;value;manual",
120108
"System.Collections;IDictionary;true;get_Values;();;Argument[this].Element.Property[System.Collections.Generic.KeyValuePair<,>.Value];ReturnValue.Element;value;manual",
@@ -194,6 +182,7 @@ private class SystemCollectionsQueueFlowModelCsv extends SummaryModelCsv {
194182
override predicate row(string row) {
195183
row =
196184
[
185+
"System.Collections;Queue;true;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
197186
"System.Collections;Queue;false;Clone;();;Argument[0].Element;ReturnValue.Element;value;manual",
198187
"System.Collections;Queue;false;Peek;();;Argument[this].Element;ReturnValue;value;manual",
199188
]
@@ -205,6 +194,7 @@ private class SystemCollectionsStackFlowModelCsv extends SummaryModelCsv {
205194
override predicate row(string row) {
206195
row =
207196
[
197+
"System.Collections;Stack;false;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
208198
"System.Collections;Stack;false;Clone;();;Argument[0].Element;ReturnValue.Element;value;manual",
209199
"System.Collections;Stack;false;Peek;();;Argument[this].Element;ReturnValue;value;manual",
210200
"System.Collections;Stack;false;Pop;();;Argument[this].Element;ReturnValue;value;manual",

csharp/ql/lib/semmle/code/csharp/frameworks/system/ComponentModel.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ private class SystemComponentModelPropertyDescriptorCollectionFlowModelCsv exten
1212
"System.ComponentModel;PropertyDescriptorCollection;false;Add;(System.ComponentModel.PropertyDescriptor);;Argument[0].Property[System.Collections.Generic.KeyValuePair<,>.Value];Argument[this].Element.Property[System.Collections.Generic.KeyValuePair<,>.Value];value;manual",
1313
"System.ComponentModel;PropertyDescriptorCollection;false;Add;(System.Object);;Argument[0].Property[System.Collections.Generic.KeyValuePair<,>.Key];Argument[this].Element.Property[System.Collections.Generic.KeyValuePair<,>.Key];value;manual",
1414
"System.ComponentModel;PropertyDescriptorCollection;false;Add;(System.Object);;Argument[0].Property[System.Collections.Generic.KeyValuePair<,>.Value];Argument[this].Element.Property[System.Collections.Generic.KeyValuePair<,>.Value];value;manual",
15+
"System.ComponentModel;PropertyDescriptorCollection;false;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
1516
"System.ComponentModel;PropertyDescriptorCollection;false;Find;(System.String,System.Boolean);;Argument[this].Element;ReturnValue;value;manual",
1617
"System.ComponentModel;PropertyDescriptorCollection;false;GetEnumerator;();;Argument[this].Element;ReturnValue.Property[System.Collections.IEnumerator.Current];value;manual",
1718
"System.ComponentModel;PropertyDescriptorCollection;false;Insert;(System.Int32,System.ComponentModel.PropertyDescriptor);;Argument[1];Argument[this].Element;value;manual",
@@ -37,6 +38,7 @@ private class SystemComponentModelEventDescriptorCollectionFlowModelCsv extends
3738
row =
3839
[
3940
"System.ComponentModel;EventDescriptorCollection;false;Add;(System.ComponentModel.EventDescriptor);;Argument[0];Argument[this].Element;value;manual",
41+
"System.ComponentModel;EventDescriptorCollection;false;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
4042
"System.ComponentModel;EventDescriptorCollection;false;Find;(System.String,System.Boolean);;Argument[this].Element;ReturnValue;value;manual",
4143
"System.ComponentModel;EventDescriptorCollection;false;GetEnumerator;();;Argument[this].Element;ReturnValue.Property[System.Collections.IEnumerator.Current];value;manual",
4244
"System.ComponentModel;EventDescriptorCollection;false;Insert;(System.Int32,System.ComponentModel.EventDescriptor);;Argument[1];Argument[this].Element;value;manual",
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/** Provides definitions related to the namespace `System.Configuration`. */
2+
3+
import csharp
4+
private import semmle.code.csharp.dataflow.ExternalFlow
5+
6+
/** Data flow for some collection classes in `System.Configuration.*`. */
7+
private class SystemClearFlowModelCsv extends SummaryModelCsv {
8+
override predicate row(string row) {
9+
row =
10+
[
11+
"System.Configuration;CommaDelimitedStringCollection;false;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
12+
"System.Configuration;ConfigurationLockCollection;false;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
13+
"System.Configuration;ConfigurationPropertyCollection;false;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
14+
"System.Configuration;ConfigurationSectionCollection;false;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
15+
"System.Configuration;ConfigurationSectionGroupCollection;false;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
16+
"System.Configuration;ConnectionStringSettingsCollection;false;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
17+
"System.Configuration;KeyValueConfigurationCollection;false;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
18+
"System.Configuration;NameValueConfigurationCollection;false;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
19+
"System.Configuration;ProviderSettingsCollection;false;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
20+
"System.Configuration;SettingElementCollection;false;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
21+
"System.Configuration;SettingsPropertyCollection;false;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
22+
"System.Configuration;SettingsPropertyValueCollection;false;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
23+
"System.Configuration.Provider;ProviderCollection;false;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
24+
]
25+
}
26+
}

csharp/ql/lib/semmle/code/csharp/frameworks/system/Data.qll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ private class SystemDataConstraintCollectionFlowModelCsv extends SummaryModelCsv
150150
[
151151
"System.Data;ConstraintCollection;false;Add;(System.Data.Constraint);;Argument[0];Argument[this].Element;value;manual",
152152
"System.Data;ConstraintCollection;false;AddRange;(System.Data.Constraint[]);;Argument[0].Element;Argument[this].Element;value;manual",
153+
"System.Data;ConstraintCollection;false;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
153154
"System.Data;ConstraintCollection;false;CopyTo;(System.Data.Constraint[],System.Int32);;Argument[this].Element;Argument[0].Element;value;manual",
154155
]
155156
}
@@ -163,6 +164,7 @@ private class SystemDataDataColumnCollectionFlowModelCsv extends SummaryModelCsv
163164
"System.Data;DataColumnCollection;false;Add;(System.Data.DataColumn);;Argument[0];Argument[this].Element;value;manual",
164165
"System.Data;DataColumnCollection;false;Add;(System.String);;Argument[0];Argument[this].Element;value;manual",
165166
"System.Data;DataColumnCollection;false;AddRange;(System.Data.DataColumn[]);;Argument[0].Element;Argument[this].Element;value;manual",
167+
"System.Data;DataColumnCollection;false;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
166168
"System.Data;DataColumnCollection;false;CopyTo;(System.Data.DataColumn[],System.Int32);;Argument[this].Element;Argument[0].Element;value;manual",
167169
]
168170
}
@@ -174,6 +176,7 @@ private class SystemDataDataRelationCollectionFlowModelCsv extends SummaryModelC
174176
row =
175177
[
176178
"System.Data;DataRelationCollection;false;Add;(System.Data.DataRelation);;Argument[0];Argument[this].Element;value;manual",
179+
"System.Data;DataRelationCollection;true;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
177180
"System.Data;DataRelationCollection;false;CopyTo;(System.Data.DataRelation[],System.Int32);;Argument[this].Element;Argument[0].Element;value;manual",
178181
"System.Data;DataRelationCollection;true;AddRange;(System.Data.DataRelation[]);;Argument[0].Element;Argument[this].Element;value;manual",
179182
]
@@ -187,6 +190,7 @@ private class SystemDataDataRawCollectionFlowModelCsv extends SummaryModelCsv {
187190
[
188191
"System.Data;DataRowCollection;false;Add;(System.Data.DataRow);;Argument[0];Argument[this].Element;value;manual",
189192
"System.Data;DataRowCollection;false;Add;(System.Object[]);;Argument[0];Argument[this].Element;value;manual",
193+
"System.Data;DataRowCollection;false;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
190194
"System.Data;DataRowCollection;false;CopyTo;(System.Data.DataRow[],System.Int32);;Argument[this].Element;Argument[0].Element;value;manual",
191195
"System.Data;DataRowCollection;false;Find;(System.Object);;Argument[this].Element;ReturnValue;value;manual",
192196
"System.Data;DataRowCollection;false;Find;(System.Object[]);;Argument[this].Element;ReturnValue;value;manual",
@@ -202,6 +206,7 @@ private class SystemDataDataTableCollectionFlowModelCsv extends SummaryModelCsv
202206
"System.Data;DataTableCollection;false;Add;(System.Data.DataTable);;Argument[0];Argument[this].Element;value;manual",
203207
"System.Data;DataTableCollection;false;Add;(System.String);;Argument[0];Argument[this].Element;value;manual",
204208
"System.Data;DataTableCollection;false;AddRange;(System.Data.DataTable[]);;Argument[0].Element;Argument[this].Element;value;manual",
209+
"System.Data;DataTableCollection;false;Clear;();;Argument[this].WithoutElement;Argument[this];value;manual",
205210
"System.Data;DataTableCollection;false;CopyTo;(System.Data.DataTable[],System.Int32);;Argument[this].Element;Argument[0].Element;value;manual",
206211
]
207212
}

0 commit comments

Comments
 (0)