Skip to content

Commit 25b2018

Browse files
authored
Merge pull request #16861 from michaelnebel/modelgen/sourcesinklift
C#/Java: Do not lift source and sink models.
2 parents b4707ab + 9cb7018 commit 25b2018

File tree

17 files changed

+183
-42
lines changed

17 files changed

+183
-42
lines changed

csharp/ql/src/utils/modelgenerator/internal/CaptureModels.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ class DataFlowSourceTargetApi = SourceTargetApi;
3838
class DataFlowSinkTargetApi = SinkTargetApi;
3939

4040
private module ModelPrintingInput implements ModelPrintingSig {
41-
class Api = TargetApiBase;
41+
class SummaryApi = DataFlowSummaryTargetApi;
42+
43+
class SourceOrSinkApi = SourceOrSinkTargetApi;
4244

4345
string getProvenance() { result = "df-generated" }
4446
}

csharp/ql/src/utils/modelgenerator/internal/CaptureModelsSpecific.qll

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,38 +112,41 @@ predicate isUninterestingForDataFlowModels(CS::Callable api) { isHigherOrder(api
112112
predicate isUninterestingForTypeBasedFlowModels(CS::Callable api) { none() }
113113

114114
/**
115-
* A class of callables that are potentially relevant for generating summary and
116-
* neutral models.
115+
* A class of callables that are potentially relevant for generating source or
116+
* sink models.
117117
*/
118-
class SummaryTargetApi extends TargetApiBase {
119-
SummaryTargetApi() { not hasManualSummaryModel(this.lift()) }
118+
class SourceOrSinkTargetApi extends Callable {
119+
SourceOrSinkTargetApi() { relevant(this) }
120120
}
121121

122122
/**
123123
* A class of callables that are potentially relevant for generating sink models.
124124
*/
125-
class SinkTargetApi extends TargetApiBase {
126-
SinkTargetApi() { not hasManualSinkModel(this.lift()) }
125+
class SinkTargetApi extends SourceOrSinkTargetApi {
126+
SinkTargetApi() { not hasManualSinkModel(this) }
127127
}
128128

129129
/**
130130
* A class of callables that are potentially relevant for generating source models.
131131
*/
132-
class SourceTargetApi extends TargetApiBase {
133-
SourceTargetApi() { not hasManualSourceModel(this.lift()) }
132+
class SourceTargetApi extends SourceOrSinkTargetApi {
133+
SourceTargetApi() { not hasManualSourceModel(this) }
134134
}
135135

136136
/**
137-
* A class of callables that are potentially relevant for generating summary, source, sink
138-
* and neutral models.
137+
* A class of callables that are potentially relevant for generating summary or
138+
* neutral models.
139139
*
140140
* In the Standard library and 3rd party libraries it is the callables (or callables that have a
141141
* super implementation) that can be called from outside the library itself.
142142
*/
143-
class TargetApiBase extends Callable {
143+
class SummaryTargetApi extends Callable {
144144
private Callable lift;
145145

146-
TargetApiBase() { lift = liftedImpl(this) }
146+
SummaryTargetApi() {
147+
lift = liftedImpl(this) and
148+
not hasManualSummaryModel(lift)
149+
}
147150

148151
/**
149152
* Gets the callable that a model will be lifted to.

csharp/ql/src/utils/modelgenerator/internal/CaptureTypeBasedSummaryModels.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ private predicate output(Callable callable, TypeParameter tp, string output) {
178178
}
179179

180180
private module ModelPrintingInput implements ModelPrintingSig {
181-
class Api = TypeBasedFlowTargetApi;
181+
class SummaryApi = TypeBasedFlowTargetApi;
182+
183+
class SourceOrSinkApi = TypeBasedFlowTargetApi;
182184

183185
string getProvenance() { result = "tb-generated" }
184186
}

csharp/ql/test/utils/modelgenerator/dataflow/CaptureSinkModels.ext.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ extensions:
44
extensible: sinkModel
55
data:
66
- [ "Sinks", "NewSinks", False, "Sink", "(System.Object)", "", "Argument[0]", "test-sink", "manual"]
7+
- [ "Sinks", "NewSinks", False, "Sink2", "(System.Object)", "", "Argument[0]", "test-sink2", "manual"]
78
- [ "Sinks", "NewSinks", False, "ManualSinkAlreadyDefined", "(System.Object)", "", "Argument[0]", "test-sink", "manual"]
89

910
- addsTo:

csharp/ql/test/utils/modelgenerator/dataflow/CaptureSourceModels.ext.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ extensions:
44
extensible: sourceModel
55
data:
66
- ["Sources", "NewSources", False, "ManualSourceAlreadyDefined", "()", "", "ReturnValue", "test-source", "manual"]
7+
- ["Sources", "NewSources", False, "Source1", "()", "", "ReturnValue", "source-kind-1", "manual"]
8+
- ["Sources", "NewSources", False, "Source2", "()", "", "ReturnValue", "source-kind-2", "manual"]
79

810
- addsTo:
911
pack: codeql/csharp-all

csharp/ql/test/utils/modelgenerator/dataflow/Sinks.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ public class NewSinks
1414

1515
// Sink defined in the extensible file next to the test.
1616
// neutral=Sinks;NewSinks;Sink;(System.Object);summary;df-generated
17-
public void Sink(object o) => throw null;
17+
public static void Sink(object o) => throw null;
18+
19+
// Sink defined in the extensible file next to the test.
20+
// neutral=Sinks;NewSinks;Sink2;(System.Object);summary;df-generated
21+
public static void Sink2(object o) => throw null;
1822

1923
// New sink
2024
// sink=Sinks;NewSinks;false;WrapResponseWrite;(System.Object);;Argument[0];html-injection;df-generated
@@ -105,6 +109,32 @@ public void ManualSinkAlreadyDefined(object o)
105109
{
106110
Sink(o);
107111
}
112+
113+
public abstract class DataWriter
114+
{
115+
// neutral=Sinks;NewSinks+DataWriter;Write;(System.Object);summary;df-generated
116+
public abstract void Write(object o);
117+
}
118+
119+
public class DataWriterKind1 : DataWriter
120+
{
121+
// sink=Sinks;NewSinks+DataWriterKind1;true;Write;(System.Object);;Argument[0];test-sink;df-generated
122+
// neutral=Sinks;NewSinks+DataWriterKind1;Write;(System.Object);summary;df-generated
123+
public override void Write(object o)
124+
{
125+
Sink(o);
126+
}
127+
}
128+
129+
public class DataWriterKind2 : DataWriter
130+
{
131+
// sink=Sinks;NewSinks+DataWriterKind2;true;Write;(System.Object);;Argument[0];test-sink2;df-generated
132+
// neutral=Sinks;NewSinks+DataWriterKind2;Write;(System.Object);summary;df-generated
133+
public override void Write(object o)
134+
{
135+
Sink2(o);
136+
}
137+
}
108138
}
109139

110140
public class CompoundSinks

csharp/ql/test/utils/modelgenerator/dataflow/Sources.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ namespace Sources;
44

55
public class NewSources
66
{
7+
// Defined as source in the extensions file next to the test.
8+
// neutral=Sources;NewSources;Source1;();summary;df-generated
9+
public static string Source1() => throw null;
10+
11+
// Defined as source in the extensions file next to the test.
12+
// neutral=Sources;NewSources;Source2;();summary;df-generated
13+
public static string Source2() => throw null;
14+
15+
716
// New source
817
// source=Sources;NewSources;false;WrapConsoleReadLine;();;ReturnValue;local;df-generated
918
// neutral=Sources;NewSources;WrapConsoleReadLine;();summary;df-generated
@@ -79,4 +88,30 @@ public string ManualSourceAlreadyDefined()
7988
{
8089
return Console.ReadLine();
8190
}
91+
92+
public abstract class DataReader
93+
{
94+
// neutral=Sources;NewSources+DataReader;Read;();summary;df-generated
95+
public abstract string Read();
96+
}
97+
98+
public class DataReaderKind1 : DataReader
99+
{
100+
// source=Sources;NewSources+DataReaderKind1;true;Read;();;ReturnValue;source-kind-1;df-generated
101+
// neutral=Sources;NewSources+DataReaderKind1;Read;();summary;df-generated
102+
public override string Read()
103+
{
104+
return Source1();
105+
}
106+
}
107+
108+
public class DataReaderKind2 : DataReader
109+
{
110+
// source=Sources;NewSources+DataReaderKind2;true;Read;();;ReturnValue;source-kind-2;df-generated
111+
// neutral=Sources;NewSources+DataReaderKind2;Read;();summary;df-generated
112+
public override string Read()
113+
{
114+
return Source2();
115+
}
116+
}
82117
}

java/ql/src/utils/modelgenerator/internal/CaptureModels.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ class DataFlowSourceTargetApi = SourceTargetApi;
3838
class DataFlowSinkTargetApi = SinkTargetApi;
3939

4040
private module ModelPrintingInput implements ModelPrintingSig {
41-
class Api = TargetApiBase;
41+
class SummaryApi = DataFlowSummaryTargetApi;
42+
43+
class SourceOrSinkApi = SourceOrSinkTargetApi;
4244

4345
string getProvenance() { result = "df-generated" }
4446
}

java/ql/src/utils/modelgenerator/internal/CaptureModelsSpecific.qll

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,25 +83,25 @@ predicate isUninterestingForDataFlowModels(Callable api) {
8383
}
8484

8585
/**
86-
* A class of callables that are potentially relevant for generating summary and
87-
* neutral models.
86+
* A class of callables that are potentially relevant for generating source or
87+
* sink models.
8888
*/
89-
class SummaryTargetApi extends TargetApiBase {
90-
SummaryTargetApi() { not hasManualSummaryModel(this.lift()) }
89+
class SourceOrSinkTargetApi extends Callable {
90+
SourceOrSinkTargetApi() { relevant(this) }
9191
}
9292

9393
/**
9494
* A class of callables that are potentially relevant for generating sink models.
9595
*/
96-
class SinkTargetApi extends TargetApiBase {
97-
SinkTargetApi() { not hasManualSinkModel(this.lift()) }
96+
class SinkTargetApi extends SourceOrSinkTargetApi {
97+
SinkTargetApi() { not hasManualSinkModel(this) }
9898
}
9999

100100
/**
101101
* A class of callables that are potentially relevant for generating source models.
102102
*/
103-
class SourceTargetApi extends TargetApiBase {
104-
SourceTargetApi() { not hasManualSourceModel(this.lift()) }
103+
class SourceTargetApi extends SourceOrSinkTargetApi {
104+
SourceTargetApi() { not hasManualSourceModel(this) }
105105
}
106106

107107
/**
@@ -112,16 +112,19 @@ class SourceTargetApi extends TargetApiBase {
112112
predicate isUninterestingForTypeBasedFlowModels(Callable api) { none() }
113113

114114
/**
115-
* A class of callables that are potentially relevant for generating summary, source, sink
116-
* and neutral models.
115+
* A class of callables that are potentially relevant for generating summary or
116+
* neutral models.
117117
*
118118
* In the Standard library and 3rd party libraries it is the callables (or callables that have a
119119
* super implementation) that can be called from outside the library itself.
120120
*/
121-
class TargetApiBase extends Callable {
121+
class SummaryTargetApi extends Callable {
122122
private Callable lift;
123123

124-
TargetApiBase() { lift = liftedImpl(this) }
124+
SummaryTargetApi() {
125+
lift = liftedImpl(this) and
126+
not hasManualSummaryModel(lift)
127+
}
125128

126129
/**
127130
* Gets the callable that a model will be lifted to.

java/ql/src/utils/modelgenerator/internal/CaptureTypeBasedSummaryModels.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,9 @@ private predicate output(Callable callable, TypeVariable tv, string output) {
284284
}
285285

286286
module ModelPrintingInput implements ModelPrintingSig {
287-
class Api = TypeBasedFlowTargetApi;
287+
class SummaryApi = TypeBasedFlowTargetApi;
288+
289+
class SourceOrSinkApi = Specific::SourceOrSinkTargetApi;
288290

289291
string getProvenance() { result = "tb-generated" }
290292
}

0 commit comments

Comments
 (0)