Skip to content

Rust: Fix default source and sink in inline flow test #17995

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

Merged
merged 2 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
models
edges
nodes
| main.rs:17:10:17:18 | CallExpr | semmle.label | CallExpr |
subpaths
testFailures
| main.rs:17:22:17:40 | Comment | Missing result: hasValueFlow=1 |
| main.rs:22:14:22:32 | Comment | Missing result: hasValueFlow=1 |
| main.rs:33:14:33:32 | Comment | Missing result: hasValueFlow=1 |
#select
| main.rs:17:10:17:18 | CallExpr | main.rs:17:10:17:18 | CallExpr | main.rs:17:10:17:18 | CallExpr | $@ | main.rs:17:10:17:18 | CallExpr | CallExpr |
13 changes: 7 additions & 6 deletions rust/ql/test/utils/InlineFlowTest.qll
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ private import codeql.rust.dataflow.internal.DataFlowImpl
private import codeql.rust.dataflow.internal.TaintTrackingImpl
private import internal.InlineExpectationsTestImpl as InlineExpectationsTestImpl

// Holds if the target expression of `call` is a path and the string representation of the path is `name`.
private predicate callTargetName(CallExpr call, string name) {
call.getExpr().(PathExpr).getPath().toString() = name
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should define the toString of PathExpr to be result = this.getPath().toString() .

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds good to me. I'll update the PR to do that instead :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, we ought to implement a lot of more sensible toStrings, but that is of course not for this PR...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added the suggested toString on PathExpr. Note that this added two inconsistencies here because the Path nodes on the last two lines in gen_path_expr.rs doesn't have a string representation. I don't know if this is a bug in the extractor? When I explore the AST for that file I also see that the last two let statements doesn't have an initializer which seems odd?

Copy link
Contributor

@aibaars aibaars Nov 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not an extractor error. The qualifier of a path like <TypeRef as Trait>::foo does not have a nameRef so the toString() fails. See also:

// TODO: this does not cover everything
if this.hasGenericArgList()
then result = this.getNameRef().toString() + "::<...>"
else result = this.getNameRef().toString()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's too much hassle to address this in the current PR, then we can also accept the inconsistencies and fix them in a follow-up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that makes sense. I've accepted the inconsistencies for now.

}

private module FlowTestImpl implements InputSig<Location, RustDataFlow> {
predicate defaultSource(DataFlow::Node source) {
source.asExpr().(CallExpr).getExpr().toString() = "source"
}
predicate defaultSource(DataFlow::Node source) { callTargetName(source.asExpr(), "source") }

predicate defaultSink(DataFlow::Node sink) {
any(CallExpr call | call = sink.asExpr() and call.getExpr().toString() = "sink")
.getArgList()
.getAnArg() = sink.asExpr()
any(CallExpr call | callTargetName(call, "sink")).getArgList().getAnArg() = sink.asExpr()
}

private string getSourceArgString(DataFlow::Node src) {
Expand Down