-
Notifications
You must be signed in to change notification settings - Fork 67
Implement package expressions2 #906
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MichaelRFairhurst
wants to merge
3
commits into
main
Choose a base branch
from
michaelrfairhurst/implement-package-expressions2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,2 @@ | ||
- All queries related to integer suffixes: | ||
- No visible changes expected: the regex for parsing integer suffixes, and how they are treated after lexing, has been refactored. |
2 changes: 2 additions & 0 deletions
2
change_notes/2025-06-03-refactor-temporary-object-flow-step.md
This file contains hidden or 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,2 @@ | ||
- `RULE-18-9` - `ArraytoPointerConversionOfTemporaryObject.ql` | ||
- The behavior for finding flow steps of temporary objects (for example, from ternary branches to the ternary expr result) has been extracted for reuse in other rules, no visible changes expected. |
4 changes: 2 additions & 2 deletions
4
cpp/cert/test/rules/OOP54-CPP/GracefullyHandleSelfCopyAssignment.expected
This file contains hidden or 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
| test.cpp:57:6:57:14 | operator= | User defined copy or user defined move does not handle self-assignment correctly. | | ||
| test.cpp:66:6:66:14 | operator= | User defined copy or user defined move does not handle self-assignment correctly. | | ||
| test.cpp:56:6:56:14 | operator= | User defined copy or user defined move does not handle self-assignment correctly. | | ||
| test.cpp:65:6:65:14 | operator= | User defined copy or user defined move does not handle self-assignment correctly. | |
This file contains hidden or 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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
#include <functional> | ||
#include <string> | ||
#include <utility> | ||
|
||
class A { | ||
public: | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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,238 @@ | ||
import codingstandards.cpp.alertreporting.CustomPathStateProblem | ||
import codingstandards.cpp.Expr | ||
import codingstandards.cpp.Scope | ||
|
||
signature class LambdaSig extends LambdaExpression; | ||
|
||
class ImplicitThisCapturingLambdaExpr extends LambdaExpression { | ||
ImplicitThisCapturingLambdaExpr() { | ||
exists(LambdaCapture capture | | ||
capture = getACapture() and | ||
capture.getField().getName() = "(captured this)" and | ||
capture.isImplicit() | ||
) | ||
} | ||
} | ||
|
||
class ImplicitCaptureLambdaExpr extends LambdaExpression { | ||
LambdaCapture capture; | ||
|
||
ImplicitCaptureLambdaExpr() { capture = getCapture(_) and capture.isImplicit() } | ||
|
||
LambdaCapture getImplicitCapture() { result = capture } | ||
} | ||
|
||
/** | ||
* A module to find lambda expressions that are eventually copied or moved. | ||
* | ||
* Unfortunately, CodeQL does not capture all lambda flow or all lambda copies/moves. However, | ||
* since lambdas can only be used in an extremely limited number of ways, we can easily roll our | ||
* own dataflow-like analysis as a custom path problem, to match lambdas to stores. | ||
*/ | ||
module TransientLambda<LambdaSig Source> { | ||
final class FinalLocatable = Locatable; | ||
|
||
/** | ||
* Create a custom path problem, which inherently performs a graph search to find paths from start | ||
* nodes (in this case, lambda expressions) to end nodes (in this case, copies and moves of | ||
* lambdas). | ||
*/ | ||
private module TransientLambdaConfig implements CustomPathStateProblemConfigSig { | ||
class Node extends FinalLocatable { | ||
Node() { | ||
this instanceof Source | ||
or | ||
this instanceof Variable | ||
or | ||
this.(VariableAccess).getTarget() instanceof Parameter | ||
or | ||
this instanceof Expr | ||
or | ||
this instanceof Function | ||
or | ||
this instanceof NewExpr | ||
} | ||
} | ||
|
||
class State = TranslationUnit; | ||
|
||
// Do not search past the first copy or move of a lambda. | ||
predicate searchPastEnd() { none() } | ||
|
||
predicate start(Node n, TranslationUnit state) { n instanceof Source and state = n.getFile() } | ||
|
||
bindingset[state] | ||
pragma[inline_late] | ||
predicate end(Node n, TranslationUnit state) { | ||
n instanceof Variable and not n instanceof Parameter | ||
or | ||
n instanceof Function and | ||
not functionDefinedInTranslationUnit(n, state) | ||
or | ||
exists(NewExpr alloc | | ||
alloc = n and | ||
alloc.getAllocatedType().stripTopLevelSpecifiers() instanceof Closure | ||
) | ||
} | ||
|
||
predicate edge(Node a, TranslationUnit s1, Node b, TranslationUnit s2) { | ||
s2 = s1 and | ||
( | ||
a = b.(Variable).getInitializer().getExpr() | ||
or | ||
exists(Call fc, int i | | ||
a = fc.getArgument(i) and | ||
( | ||
b = fc.getTarget().getParameter(i) | ||
or | ||
b = fc.getTarget() | ||
) | ||
) | ||
or | ||
exists(Call fc, Function f, ReturnStmt ret | | ||
f = fc.getTarget() and | ||
ret.getEnclosingFunction() = f and | ||
a = ret.getExpr() and | ||
b = fc | ||
) | ||
or | ||
b = a.(Parameter).getAnAccess() | ||
or | ||
temporaryObjectFlowStep(a, b) | ||
or | ||
a = b.(NewExpr).getInitializer() | ||
) | ||
} | ||
} | ||
|
||
import CustomPathStateProblem<TransientLambdaConfig> as TransientFlow | ||
|
||
module PathProblem { | ||
import TransientFlow | ||
} | ||
|
||
predicate isStored(Source lambda, Element store, string reason) { | ||
TransientFlow::problem(lambda, store) and | ||
( | ||
if store instanceof Function and not functionDefinedInTranslationUnit(store, lambda.getFile()) | ||
then reason = "passed to a different translation unit" | ||
else reason = "copied or moved" | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* An alterate module for detecting transient lambdas which uses the standard CodeQL dataflow | ||
* library. | ||
* | ||
* Ideally, this module eventually replaces `TransientLambda`, however, current CodeQL support for | ||
* flow of lambdas is unreliable and incomplete/inconsistent. This implementation does not detect | ||
* all cases correctly, but it is a starting place to revisit at a later time. | ||
* | ||
* In the current dataflow library, there are many missing nodes and edges, making this currently | ||
* difficult or impossible to implement correctly. | ||
*/ | ||
module TransientLambdaDataFlow<LambdaSig Source> { | ||
import semmle.code.cpp.dataflow.new.DataFlow as DataFlow | ||
import DataFlow::DataFlow as NewDataFlow | ||
|
||
final class FinalLocatable = Locatable; | ||
|
||
/** | ||
* Create a custom path problem, which inherently performs a graph search to find paths from start | ||
* nodes (in this case, lambda expressions) to end nodes (in this case, copies and moves of | ||
* lambdas). | ||
*/ | ||
module TransientLambdaConfig implements NewDataFlow::StateConfigSig { | ||
class FlowState = TranslationUnit; | ||
|
||
predicate isSource(NewDataFlow::Node n, TranslationUnit state) { | ||
n.asExpr() instanceof Source and state = n.asExpr().getFile() | ||
} | ||
|
||
predicate isSink(NewDataFlow::Node n) { | ||
n.asOperand().getDef().getAst() instanceof VariableDeclarationEntry | ||
or | ||
exists(n.asVariable()) and not exists(n.asParameter()) | ||
or | ||
exists(NewExpr alloc | | ||
alloc.getAllocatedType() instanceof Closure and | ||
alloc.getInitializer() = n.asExpr() | ||
) | ||
or | ||
// Detect casting to std::function, which results in a copy of the lambda. | ||
exists(Conversion conv | conv.getExpr() = n.asExpr()) | ||
or | ||
// Detect all function calls, in case the definition is in a different translation unit. | ||
// We cannot detect this with stateful dataflow, for performance reasons. | ||
exists(FunctionCall fc | fc.getAnArgument() = n.asExpr()) | ||
} | ||
|
||
predicate isSink(NewDataFlow::Node n, TranslationUnit state) { | ||
// Ideally, we would be able to check here for calls to functions defined outside of the | ||
// translation unit, but in the current stateful dataflow library, this will result in a | ||
// cartesian product of all nodes with all translation units. This limitation doesn't exist | ||
// in the alternate `TransientLambda` module which uses `CustomPathStateProblem`. | ||
// | ||
// Since this predicate holds for none(), it may seem that we don't need to use stateful flow. | ||
// However, stateful flow is still a good idea so that we can add isBarrier() to prevent flow | ||
// out of the translation unit. That should be possible to do without introducing a | ||
// cartesian product. | ||
// | ||
// To work around the cartesian product, this predicate holds for none() and `isSink(n)` | ||
// should hold for all function calls. After flow has found lambda/function call pairs, we | ||
// can filter out those pairs where the function is defined in a different translation unit. | ||
// | ||
// This isn't quite implemented yet. | ||
none() | ||
} | ||
|
||
predicate isBarrierOut(NewDataFlow::Node n, TranslationUnit state) { | ||
// TODO: Implement a barrier to prevent flow out of the translation unit. | ||
none() | ||
} | ||
|
||
predicate isAdditionalFlowStep( | ||
NewDataFlow::Node a, TranslationUnit s1, NewDataFlow::Node b, TranslationUnit s2 | ||
) { | ||
// Add additional flow steps to handle: | ||
// | ||
// auto x = []() { ... }; | ||
// | ||
// Which isn't represented in the dataflow graph otherwise. | ||
pragma[only_bind_out](s2) = s1 and | ||
( | ||
pragma[only_bind_out](a.asExpr()) = | ||
b.asOperand() | ||
.getDef() | ||
.getAst() | ||
.(VariableDeclarationEntry) | ||
.getVariable() | ||
.getInitializer() | ||
.getExpr() | ||
or | ||
a.asExpr().(Conversion).getExpr() = b.asExpr() | ||
) | ||
} | ||
} | ||
|
||
import NewDataFlow::GlobalWithState<TransientLambdaConfig> as TransientFlow | ||
|
||
module PathProblem { | ||
import TransientFlow::PathGraph | ||
} | ||
|
||
predicate isStored(Source lambda, Element store) { | ||
exists(NewDataFlow::Node sink | | ||
TransientFlow::flow(NewDataFlow::exprNode(lambda), sink) and | ||
store = sink.asOperand().getDef().getAst() and | ||
not exists(FunctionCall fc | | ||
fc.getAnArgument() = sink.asExpr() and | ||
exists(FunctionDeclarationEntry funcDef | | ||
funcDef = fc.getTarget().getDefinition() and | ||
funcDef.getFile() = lambda.getFile().(TranslationUnit).getATransitivelyIncludedFile() | ||
) | ||
) | ||
) | ||
} | ||
} |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicate predicate 'fde = f.getDefinition()' is redundant. Consider removing one of the identical checks to simplify the clause.
Copilot uses AI. Check for mistakes.