Skip to content

Commit 32a538b

Browse files
author
Stephan Brandauer
committed
Java: automodel fr mode: add mad output to extraction metadata
1 parent b2578f0 commit 32a538b

5 files changed

+46
-26
lines changed

java/ql/automodel/src/AutomodelFrameworkModeCharacteristics.qll

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ abstract class FrameworkModeEndpoint extends TFrameworkModeEndpoint {
3737
*/
3838
abstract int getIndex();
3939

40+
/**
41+
* Gets the input (if any) for this endpoint, eg.: `Argument[0]`.
42+
*
43+
* For endpoints that are source candidates, this will be `none()`.
44+
*/
45+
abstract string getMaDInput();
46+
47+
/**
48+
* Gets the output (if any) for this endpoint, eg.: `ReturnValue`.
49+
*
50+
* For endpoints that are sink candidates, this will be `none()`.
51+
*/
52+
abstract string getMaDOutput();
53+
4054
/**
4155
* Returns the name of the parameter of the endpoint.
4256
*/
@@ -63,6 +77,10 @@ class ExplicitParameterEndpoint extends FrameworkModeEndpoint, TExplicitParamete
6377

6478
override int getIndex() { result = param.getPosition() }
6579

80+
override string getMaDInput() { result = "Argument[" + param.getPosition() + "]" }
81+
82+
override string getMaDOutput() { none() }
83+
6684
override string getParamName() { result = param.getName() }
6785

6886
override Callable getEnclosingCallable() { result = param.getCallable() }
@@ -81,6 +99,10 @@ class QualifierEndpoint extends FrameworkModeEndpoint, TQualifier {
8199

82100
override int getIndex() { result = -1 }
83101

102+
override string getMaDInput() { result = "Argument[this]" }
103+
104+
override string getMaDOutput() { none() }
105+
84106
override string getParamName() { result = "this" }
85107

86108
override Callable getEnclosingCallable() { result = callable }
@@ -100,10 +122,11 @@ class ReturnValue extends FrameworkModeEndpoint, TReturnValue {
100122
result = -1
101123
}
102124

103-
override string getParamName() {
104-
// FIXME bogus value
105-
result = "return value"
106-
}
125+
override string getMaDInput() { none() }
126+
127+
override string getMaDOutput() { result = "ReturnValue" }
128+
129+
override string getParamName() { none() }
107130

108131
override Callable getEnclosingCallable() { result = callable }
109132

@@ -163,7 +186,7 @@ module FrameworkCandidatesImpl implements SharedCharacteristics::CandidateSig {
163186
FrameworkModeGetCallable::getCallable(e).hasQualifiedName(package, type, name) and
164187
signature = ExternalFlow::paramsString(FrameworkModeGetCallable::getCallable(e)) and
165188
ext = "" and
166-
input = AutomodelJavaUtil::getArgumentForIndex(e.getIndex())
189+
input = e.getMaDInput()
167190
}
168191

169192
/**
@@ -213,11 +236,12 @@ class FrameworkModeMetadataExtractor extends string {
213236

214237
predicate hasMetadata(
215238
Endpoint e, string package, string type, string subtypes, string name, string signature,
216-
string input, string parameterName
239+
string input, string output, string parameterName
217240
) {
218-
parameterName = e.getParamName() and
241+
(if exists(e.getParamName()) then parameterName = e.getParamName() else parameterName = "") and
219242
name = e.getEnclosingCallable().getName() and
220-
input = AutomodelJavaUtil::getArgumentForIndex(e.getIndex()) and
243+
(if exists(e.getMaDInput()) then input = e.getMaDInput() else input = "") and
244+
(if exists(e.getMaDOutput()) then output = e.getMaDOutput() else output = "") and
221245
package = e.getEnclosingCallable().getDeclaringType().getPackage().getName() and
222246
type = e.getEnclosingCallable().getDeclaringType().getErasure().(RefType).nestedName() and
223247
subtypes = AutomodelJavaUtil::considerSubtypes(e.getEnclosingCallable()).toString() and
@@ -285,8 +309,8 @@ private class ExceptionCharacteristic extends CharacteristicsImpl::NotASinkChara
285309
* A characteristic that limits candidates to parameters of methods that are recognized as `ModelApi`, iow., APIs that
286310
* are considered worth modeling.
287311
*/
288-
private class NotAModelApiParameter extends CharacteristicsImpl::UninterestingToModelCharacteristic {
289-
NotAModelApiParameter() { this = "not a model API parameter" }
312+
private class NotAModelApi extends CharacteristicsImpl::UninterestingToModelCharacteristic {
313+
NotAModelApi() { this = "not a model API" }
290314

291315
override predicate appliesToEndpoint(Endpoint e) {
292316
not e.getEnclosingCallable() instanceof ModelExclusions::ModelApi

java/ql/automodel/src/AutomodelFrameworkModeExtractCandidates.ql

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ private import AutomodelJavaUtil
1818
from
1919
Endpoint endpoint, string message, FrameworkModeMetadataExtractor meta, DollarAtString package,
2020
DollarAtString type, DollarAtString subtypes, DollarAtString name, DollarAtString signature,
21-
DollarAtString input, DollarAtString parameterName, DollarAtString alreadyAiModeled,
22-
DollarAtString extensibleType
21+
DollarAtString input, DollarAtString output, DollarAtString parameterName,
22+
DollarAtString alreadyAiModeled, DollarAtString extensibleType
2323
where
2424
endpoint.getExtensibleType() = extensibleType and
2525
not exists(CharacteristicsImpl::UninterestingToModelCharacteristic u |
@@ -36,7 +36,7 @@ where
3636
alreadyAiModeled.matches("%ai-%") and
3737
CharacteristicsImpl::isSink(endpoint, _, alreadyAiModeled)
3838
) and
39-
meta.hasMetadata(endpoint, package, type, subtypes, name, signature, input, parameterName) and
39+
meta.hasMetadata(endpoint, package, type, subtypes, name, signature, input, output, parameterName) and
4040
includeAutomodelCandidate(package, type, name, signature) and
4141
// The message is the concatenation of all sink types for which this endpoint is known neither to be a sink nor to be
4242
// a non-sink, and we surface only endpoints that have at least one such sink type.
@@ -48,7 +48,7 @@ where
4848
sinkType, ", "
4949
)
5050
select endpoint,
51-
message + "\nrelated locations: $@, $@." + "\nmetadata: $@, $@, $@, $@, $@, $@, $@, $@.", //
51+
message + "\nrelated locations: $@, $@." + "\nmetadata: $@, $@, $@, $@, $@, $@, $@, $@, $@.", //
5252
CharacteristicsImpl::getRelatedLocationOrCandidate(endpoint, MethodDoc()), "MethodDoc", //
5353
CharacteristicsImpl::getRelatedLocationOrCandidate(endpoint, ClassDoc()), "ClassDoc", //
5454
package, "package", //
@@ -57,6 +57,7 @@ select endpoint,
5757
name, "name", //
5858
signature, "signature", //
5959
input, "input", //
60+
output, "output", //
6061
parameterName, "parameterName", //
6162
alreadyAiModeled, "alreadyAiModeled", //
6263
extensibleType, "extensibleType"

java/ql/automodel/src/AutomodelFrameworkModeExtractNegativeExamples.ql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ from
1616
Endpoint endpoint, EndpointCharacteristic characteristic, float confidence,
1717
DollarAtString message, FrameworkModeMetadataExtractor meta, DollarAtString package,
1818
DollarAtString type, DollarAtString subtypes, DollarAtString name, DollarAtString signature,
19-
DollarAtString input, DollarAtString parameterName, DollarAtString extensibleType
19+
DollarAtString input, DollarAtString output, DollarAtString parameterName,
20+
DollarAtString extensibleType
2021
where
2122
endpoint.getExtensibleType() = extensibleType and
2223
characteristic.appliesToEndpoint(endpoint) and
@@ -25,7 +26,7 @@ where
2526
// Exclude endpoints that have contradictory endpoint characteristics, because we only want examples we're highly
2627
// certain about in the prompt.
2728
not erroneousEndpoints(endpoint, _, _, _, _, false) and
28-
meta.hasMetadata(endpoint, package, type, subtypes, name, signature, input, parameterName) and
29+
meta.hasMetadata(endpoint, package, type, subtypes, name, signature, input, output, parameterName) and
2930
// It's valid for a node to satisfy the logic for both `isSink` and `isSanitizer`, but in that case it will be
3031
// treated by the actual query as a sanitizer, since the final logic is something like
3132
// `isSink(n) and not isSanitizer(n)`. We don't want to include such nodes as negative examples in the prompt, because
@@ -47,5 +48,6 @@ select endpoint,
4748
name, "name", //
4849
signature, "signature", //
4950
input, "input", //
51+
output, "output", //
5052
parameterName, "parameterName", //
5153
extensibleType, "extensibleType"

java/ql/automodel/src/AutomodelFrameworkModeExtractPositiveExamples.ql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ private import AutomodelJavaUtil
1515
from
1616
Endpoint endpoint, SinkType sinkType, FrameworkModeMetadataExtractor meta, DollarAtString package,
1717
DollarAtString type, DollarAtString subtypes, DollarAtString name, DollarAtString signature,
18-
DollarAtString input, DollarAtString parameterName, DollarAtString extensibleType
18+
DollarAtString input, DollarAtString output, DollarAtString parameterName, DollarAtString extensibleType
1919
where
2020
endpoint.getExtensibleType() = extensibleType and
2121
// Exclude endpoints that have contradictory endpoint characteristics, because we only want examples we're highly
2222
// certain about in the prompt.
2323
not erroneousEndpoints(endpoint, _, _, _, _, false) and
24-
meta.hasMetadata(endpoint, package, type, subtypes, name, signature, input, parameterName) and
24+
meta.hasMetadata(endpoint, package, type, subtypes, name, signature, input, output, parameterName) and
2525
// Extract positive examples of sinks belonging to the existing ATM query configurations.
2626
CharacteristicsImpl::isKnownAs(endpoint, sinkType, _)
2727
select endpoint,
@@ -34,5 +34,6 @@ select endpoint,
3434
name, "name", //
3535
signature, "signature", //
3636
input, "input", //
37+
output, "output", //
3738
parameterName, "parameterName", //
3839
extensibleType, "extensibleType"

java/ql/automodel/src/AutomodelJavaUtil.qll

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,6 @@ predicate isKnownKind(string kind, AutomodelEndpointTypes::EndpointType type) {
4444
type instanceof AutomodelEndpointTypes::RemoteSourceType
4545
}
4646

47-
/** Gets the models-as-data description for the method argument with the index `index`. */
48-
bindingset[index]
49-
string getArgumentForIndex(int index) {
50-
index = -1 and result = "Argument[this]"
51-
or
52-
index >= 0 and result = "Argument[" + index + "]"
53-
}
54-
5547
/**
5648
* By convention, the subtypes property of the MaD declaration should only be
5749
* true when there _can_ exist any subtypes with a different implementation.

0 commit comments

Comments
 (0)