Skip to content

Commit 666855d

Browse files
committed
Shared: Improvements to content-sensitive model generation
1 parent 464d2cd commit 666855d

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

rust/ql/test/utils-tests/modelgenerator/option.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,7 @@ impl<T> MyOption<T> {
299299
}
300300

301301
// summary=<test::option::MyOption>::insert;Argument[0];Argument[self].Reference.Field[test::option::MyOption::MySome(0)];value;dfc-generated
302-
// This summary is currently missing because of access path limit
303-
// summary-MISSING=<test::option::MyOption>::insert;Argument[0];ReturnValue.Reference;value;dfc-generated
302+
// summary=<test::option::MyOption>::insert;Argument[0];ReturnValue.Reference;value;dfc-generated
304303
// The content of `self` is overwritten so it does not flow to the return value.
305304
// SPURIOUS-summary=<test::option::MyOption>::insert;Argument[self].Reference.Field[test::option::MyOption::MySome(0)];ReturnValue.Reference;value;dfc-generated
306305
pub fn insert(&mut self, value: T) -> &mut T {
@@ -311,8 +310,7 @@ impl<T> MyOption<T> {
311310
}
312311

313312
// summary=<test::option::MyOption>::get_or_insert;Argument[0];Argument[self].Reference.Field[test::option::MyOption::MySome(0)];value;dfc-generated
314-
// This summary is currently missing because of access path limit
315-
// summary-MISSING=<test::option::MyOption>::get_or_insert;Argument[0];ReturnValue.Reference;value;dfc-generated
313+
// summary=<test::option::MyOption>::get_or_insert;Argument[0];ReturnValue.Reference;value;dfc-generated
316314
// summary=<test::option::MyOption>::get_or_insert;Argument[self].Reference.Field[test::option::MyOption::MySome(0)];ReturnValue.Reference;value;dfc-generated
317315
pub fn get_or_insert(&mut self, value: T) -> &mut T {
318316
self.get_or_insert_with(|| value)
@@ -328,7 +326,7 @@ impl<T> MyOption<T> {
328326

329327
// summary=<test::option::MyOption>::get_or_insert_with;Argument[self].Reference.Field[test::option::MyOption::MySome(0)];ReturnValue.Reference;value;dfc-generated
330328
// summary=<test::option::MyOption>::get_or_insert_with;Argument[0].ReturnValue;Argument[self].Reference.Field[test::option::MyOption::MySome(0)];value;dfc-generated
331-
// SPURIOUS-summary=<test::option::MyOption>::get_or_insert_with;Argument[0];Argument[self].Reference.Field[test::option::MyOption::MySome(0)];value;dfc-generated
329+
// summary=<test::option::MyOption>::get_or_insert_with;Argument[0].ReturnValue;ReturnValue.Reference;value;dfc-generated
332330
pub fn get_or_insert_with<F>(&mut self, f: F) -> &mut T
333331
where
334332
F: FnOnce() -> T,

shared/dataflow/codeql/dataflow/internal/ContentDataFlowImpl.qll

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ module MakeImplContentDataFlow<LocationSig Location, InputSig<Location> Lang> {
7575
/** Gets a limit on the number of reads out of sources and number of stores into sinks. */
7676
default int accessPathLimit() { result = Lang::accessPathLimit() }
7777

78+
/** Gets the access path limit used in the internal invocation of the standard data flow library. */
79+
default int accessPathLimitInternal() { result = Lang::accessPathLimit() }
80+
7881
/** Holds if `c` is relevant for reads out of sources or stores into sinks. */
7982
default predicate isRelevantContent(ContentSet c) { any() }
8083
}
@@ -110,7 +113,7 @@ module MakeImplContentDataFlow<LocationSig Location, InputSig<Location> Lang> {
110113

111114
FlowFeature getAFeature() { result = ContentConfig::getAFeature() }
112115

113-
predicate accessPathLimit = ContentConfig::accessPathLimit/0;
116+
predicate accessPathLimit = ContentConfig::accessPathLimitInternal/0;
114117

115118
// needed to record reads/stores inside summarized callables
116119
predicate includeHiddenNodes() { any() }
@@ -274,6 +277,16 @@ module MakeImplContentDataFlow<LocationSig Location, InputSig<Location> Lang> {
274277
)
275278
}
276279

280+
/**
281+
* Gets the length of this access path.
282+
*/
283+
int length() {
284+
this = TAccessPathNil() and
285+
result = 0
286+
or
287+
result = this.getTail().length() + 1
288+
}
289+
277290
/**
278291
* Gets the content set at index `i` in this access path, if any.
279292
*/

shared/mad/codeql/mad/modelgenerator/internal/ModelGeneratorImpl.qll

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -703,14 +703,17 @@ module MakeModelGeneratorFactory<
703703
}
704704

705705
/**
706-
* Holds if the access path `ap` is not a parameter or returnvalue of a callback
707-
* stored in a field.
706+
* Holds if `ap` is valid for generating summary models.
708707
*
709-
* That is, we currently don't include summaries that rely on parameters or return values
710-
* of callbacks stored in fields.
708+
* We currently don't include summaries that rely on parameters or return values
709+
* of callbacks stored in fields, as those are not supported by the data flow
710+
* library.
711+
*
712+
* We also exclude access paths with contents not supported by `printContent`.
711713
*/
712714
private predicate validateAccessPath(PropagateContentFlow::AccessPath ap) {
713-
not (mentionsField(ap) and mentionsCallback(ap))
715+
not (mentionsField(ap) and mentionsCallback(ap)) and
716+
forall(int i | i in [0 .. ap.length() - 1] | exists(getContent(ap, i)))
714717
}
715718

716719
private predicate apiFlow(
@@ -720,7 +723,9 @@ module MakeModelGeneratorFactory<
720723
) {
721724
PropagateContentFlow::flow(p, reads, returnNodeExt, stores, preservesValue) and
722725
getEnclosingCallable(returnNodeExt) = api and
723-
getEnclosingCallable(p) = api
726+
getEnclosingCallable(p) = api and
727+
validateAccessPath(reads) and
728+
validateAccessPath(stores)
724729
}
725730

726731
/**
@@ -763,9 +768,7 @@ module MakeModelGeneratorFactory<
763768
PropagateContentFlow::AccessPath reads, ReturnNodeExt returnNodeExt,
764769
PropagateContentFlow::AccessPath stores, boolean preservesValue
765770
) {
766-
PropagateContentFlow::flow(p, reads, returnNodeExt, stores, preservesValue) and
767-
getEnclosingCallable(returnNodeExt) = api and
768-
getEnclosingCallable(p) = api and
771+
apiFlow(api, p, reads, returnNodeExt, stores, preservesValue) and
769772
p = api.getARelevantParameterNode()
770773
}
771774

@@ -956,8 +959,6 @@ module MakeModelGeneratorFactory<
956959
input = parameterNodeAsExactInput(p) + printReadAccessPath(reads) and
957960
output = getExactOutput(returnNodeExt) + printStoreAccessPath(stores) and
958961
input != output and
959-
validateAccessPath(reads) and
960-
validateAccessPath(stores) and
961962
(
962963
if mentionsField(reads) or mentionsField(stores)
963964
then lift = false and api.isRelevant()

0 commit comments

Comments
 (0)