C++: Implement MaD support for flow through perfect-forwarding functions - #22525
Draft
MathiasVP wants to merge 10 commits into
Draft
C++: Implement MaD support for flow through perfect-forwarding functions#22525MathiasVP wants to merge 10 commits into
MathiasVP wants to merge 10 commits into
Conversation
In the upcoming commits we will add a new extensional predicate which allows us to model that a function forwards it arguments to the constructor of a given type. This initial commit adds the test YAML models for this new extensional predicate.
signature to implement the forwardsModel. So instead of recursing on the number of elements in the signature we will recurse on the number of elements in the type (or name) columns. For well-formed models this will be equivalent.
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Constructor matching mishandles reference overloads and default parameters, while forwarding model validation and key test branches are incomplete.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review tier: Balanced
Findings: 2
New issues introduced by this change (3)
| Severity | Finding |
|---|---|
cpp/ql/lib/semmle/code/cpp/dataflow/ExternalFlow.qll — forwardsModel is included in element interpretation here but is omitted from… |
|
cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowPrivate.qll — Requiring the number of forwarded arguments to equal the constructor's full parameter count drops… |
|
cpp/ql/test/library-tests/dataflow/external-models/flow.ext.yml — Both forwarding fixtures use start = 0, leaving the new offset behavior entirely untested even… |
What changed in this PR
Adds C++ MaD support for modeling perfect-forwarding APIs through synthetic constructor arguments.
Changes:
- Introduces
forwardsModelandArgument[forward]. - Resolves forwarded constructor targets and connects their data flow.
- Adds external-model fixtures and expected results.
| File | Description |
|---|---|
cpp/ql/test/library-tests/dataflow/external-models/test.cpp |
Adds forwarding test fixtures. |
cpp/ql/test/library-tests/dataflow/external-models/sources.expected |
Updates expected sources. |
cpp/ql/test/library-tests/dataflow/external-models/sinks.expected |
Updates expected sinks. |
cpp/ql/test/library-tests/dataflow/external-models/flow.ext.yml |
Adds forwarding models. |
cpp/ql/test/library-tests/dataflow/external-models/flow.expected |
Updates expected flow graph. |
cpp/ql/lib/semmle/code/cpp/ir/implementation/unaliased_ssa/Instruction.qll |
Adds positional-argument helpers. |
cpp/ql/lib/semmle/code/cpp/ir/implementation/raw/Instruction.qll |
Adds positional-argument helpers. |
cpp/ql/lib/semmle/code/cpp/ir/implementation/aliased_ssa/Instruction.qll |
Adds positional-argument helpers. |
cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowPrivate.qll |
Implements synthetic constructor flow. |
cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowNodes.qll |
Registers forwarding nodes. |
cpp/ql/lib/semmle/code/cpp/dataflow/internal/FlowSummaryImpl.qll |
Parses forward positions. |
cpp/ql/lib/semmle/code/cpp/dataflow/internal/ExternalFlowExtensions.qll |
Declares forwardsModel. |
cpp/ql/lib/semmle/code/cpp/dataflow/ExternalFlow.qll |
Interprets forwarding models and constructor types. |
cpp/ql/lib/ext/empty.model.yml |
Registers an empty forwarding extension. |
Suppressed comments (2)
cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowPrivate.qll:616
- This equality check does not preserve C++ overload resolution.
stripReferencesalso deeply removes cv-qualifiers, so an lvalueFooargument makes bothFoo(const Foo&)andFoo(Foo&&)look likeFoo; both constructor bodies/summaries can then contribute flow even though only the lvalue overload is callable. Conversely, valid implicit conversions match no constructor. Please resolve viability with reference binding/value-category and conversion rules rather than comparing stripped types.
typeCall = stripReferences(call.getPositionalArgument(start + i).getResultType()) and
typeConstructor = stripReferences(constructor.getParameter(i).getUnspecifiedType())
|
typeCall = typeConstructor
cpp/ql/lib/semmle/code/cpp/dataflow/ExternalFlow.qll:1088
- This function-template branch is not exercised by the added tests: both fixtures obtain the constructor type from a class template argument. Add a free-function forwarding case such as the
make_unique<T, Args>shape described by the PR so regressions inforwarder.getTemplateArgument(index)are covered.
exists(string nameArguments, int index |
parseAngles(name, _, nameArguments, "") and
constructorType = getAtIndex(nameArguments, index) and
result = forwarder.getTemplateArgument(index)
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
…nd implement forwarding.
MathiasVP
force-pushed
the
flow-through-forwards-using-callbacks-2
branch
from
September 8, 2026 18:43
cb6ca91 to
afdaabf
Compare
MathiasVP
marked this pull request as draft
September 8, 2026 22:57
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.


This PR implements the necessary library changes to support MaD summaries for functions such as vector::emplace_back or make_unique. These functions receive a list of arguments and then forwards them to a constructor call.
@hvitved had a super cool implementation idea. Given:
we model a call such as
v.emplace_back(42)as:v.emplace_back(42, &Foo)and give
emplace_backtwo summaries:Argument[0] -> Argument[1].Parameter[0]Argument[1].Parameter[this] -> Argument[this].ElementThe first summary states that
42goes into the 0'th parameter of theFooconstructor, and the second summary states that thethisparameter of the constructed object goes into thethisargument ofvwith anElementcontent.(A few lines I told in the above paragraph:
forwardin MaD. I'm happy to change this name to something else if anyone has any strong opinions about this.)In order to know which constructor to forward to we need to know what type is being constructed. For example:
To know which type is constructed we add a new extensible called
forwardsModelwith rows very much like what we have for MaD summaries. For example, I've added this as row as a test:["", "Container<T>", True, "emplace<Args>", "(Args &&)", "", "0", "T", "manual"]this says that a call to
Container<T>::emplace(args0, ..., argsN)forwards its arguments to a call toT(args0, ..., argsN). The0specifies an offset so we can support cases likev.emplace(v.begin(), 42)where we need to ignore the first argument.This PR doesn't actually add any non-test MaD summaries. I'll delay that to a future PR.