-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17034 from MathiasVP/more-alias-and-side-effect-m…
…odels C++: Add more alias and side effect models
- Loading branch information
Showing
14 changed files
with
2,299 additions
and
1,052 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
cpp/ql/lib/semmle/code/cpp/models/implementations/StdAlgorithm.qll
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/** | ||
* Provides models for C++ functions from the `algorithms` header. | ||
*/ | ||
|
||
import semmle.code.cpp.models.interfaces.Taint | ||
import semmle.code.cpp.models.interfaces.DataFlow | ||
import semmle.code.cpp.models.interfaces.Iterator | ||
import semmle.code.cpp.models.interfaces.SideEffect | ||
import semmle.code.cpp.models.interfaces.Alias | ||
|
||
private class StdPartialSort extends Function, SideEffectFunction, AliasFunction { | ||
StdPartialSort() { this.hasGlobalOrStdName("partial_sort") } | ||
|
||
override predicate hasOnlySpecificReadSideEffects() { any() } | ||
|
||
override predicate hasOnlySpecificWriteSideEffects() { any() } | ||
|
||
override predicate hasSpecificWriteSideEffect(ParameterIndex i, boolean buffer, boolean mustWrite) { | ||
i = this.getAnIteratorParameterIndex() and buffer = true and mustWrite = false | ||
} | ||
|
||
override predicate hasSpecificReadSideEffect(ParameterIndex i, boolean buffer) { | ||
i = this.getAnIteratorParameterIndex() and | ||
buffer = true and | ||
this.getParameter(i).getUnspecifiedType() instanceof ReferenceType and | ||
buffer = false | ||
} | ||
|
||
private int getAnIteratorParameterIndex() { | ||
this.getParameter(result).getUnspecifiedType() instanceof Iterator | ||
} | ||
|
||
override predicate parameterNeverEscapes(int index) { | ||
index = this.getAnIteratorParameterIndex() | ||
or | ||
this.getParameter(index).getUnspecifiedType() instanceof ReferenceType | ||
} | ||
|
||
override predicate parameterEscapesOnlyViaReturn(int index) { none() } | ||
} | ||
|
||
private class StdSortHeap extends Function, SideEffectFunction, AliasFunction { | ||
StdSortHeap() { this.hasGlobalOrStdName("sort_heap") } | ||
|
||
override predicate hasOnlySpecificReadSideEffects() { any() } | ||
|
||
override predicate hasOnlySpecificWriteSideEffects() { any() } | ||
|
||
override predicate hasSpecificWriteSideEffect(ParameterIndex i, boolean buffer, boolean mustWrite) { | ||
i = this.getAnIteratorParameterIndex() and buffer = true and mustWrite = false | ||
} | ||
|
||
override predicate hasSpecificReadSideEffect(ParameterIndex i, boolean buffer) { | ||
i = this.getAnIteratorParameterIndex() and | ||
buffer = true | ||
} | ||
|
||
private int getAnIteratorParameterIndex() { | ||
this.getParameter(result).getUnspecifiedType() instanceof Iterator | ||
} | ||
|
||
override predicate parameterNeverEscapes(int index) { index = this.getAnIteratorParameterIndex() } | ||
|
||
override predicate parameterEscapesOnlyViaReturn(int index) { none() } | ||
} | ||
|
||
private class StdGenerateN extends Function, SideEffectFunction, AliasFunction { | ||
StdGenerateN() { this.hasGlobalOrStdName("generate_n") } | ||
|
||
override predicate hasOnlySpecificReadSideEffects() { any() } | ||
|
||
override predicate hasOnlySpecificWriteSideEffects() { any() } | ||
|
||
override predicate hasSpecificWriteSideEffect(ParameterIndex i, boolean buffer, boolean mustWrite) { | ||
i = this.getAnIteratorParameterIndex() and buffer = true and mustWrite = false | ||
} | ||
|
||
override predicate hasSpecificReadSideEffect(ParameterIndex i, boolean buffer) { | ||
this.getParameter(i).getUnspecifiedType() instanceof ReferenceType and buffer = false | ||
} | ||
|
||
private int getAnIteratorParameterIndex() { | ||
this.getParameter(result).getUnspecifiedType() instanceof Iterator | ||
} | ||
|
||
override predicate parameterNeverEscapes(int index) { index = this.getAnIteratorParameterIndex() } | ||
|
||
override predicate parameterEscapesOnlyViaReturn(int index) { none() } | ||
} | ||
|
||
private class StdFindIfOrIfNot extends Function, SideEffectFunction, AliasFunction { | ||
StdFindIfOrIfNot() { this.hasGlobalOrStdName(["find_if", "find_if_not"]) } | ||
|
||
override predicate hasOnlySpecificReadSideEffects() { any() } | ||
|
||
override predicate hasOnlySpecificWriteSideEffects() { any() } | ||
|
||
override predicate hasSpecificReadSideEffect(ParameterIndex i, boolean buffer) { | ||
i = this.getAnIteratorParameterIndex() and buffer = true | ||
or | ||
this.getParameter(i).getUnspecifiedType() instanceof ReferenceType and buffer = false | ||
} | ||
|
||
private int getAnIteratorParameterIndex() { | ||
this.getParameter(result).getUnspecifiedType() instanceof Iterator | ||
} | ||
|
||
override predicate parameterNeverEscapes(int index) { | ||
this.getParameter(index).getUnspecifiedType() instanceof ReferenceType | ||
} | ||
|
||
override predicate parameterEscapesOnlyViaReturn(int index) { | ||
index = this.getAnIteratorParameterIndex() | ||
} | ||
} |
Oops, something went wrong.