Skip to content

C++: Factor must-flow predicates out of two queries#8368

Merged
MathiasVP merged 8 commits into
github:mainfrom
MathiasVP:add-must-flow-lib
Mar 9, 2022
Merged

C++: Factor must-flow predicates out of two queries#8368
MathiasVP merged 8 commits into
github:mainfrom
MathiasVP:add-must-flow-lib

Conversation

@MathiasVP

Copy link
Copy Markdown
Contributor

This PR factors out the common functionality from cpp/unsafe-use-of-this and cpp/return-stack-allocated-memory. This shouldn't change any behavior1, but it should mean that we re-evaluate a tiny little bit less stuff in the Code Scanning suite.

Footnotes

  1. Apart from the path explanation for cpp/unsafe-use-of-this which is now a lot better as it doesn't just use the toString given by the IR.

@MathiasVP MathiasVP added the C++ label Mar 8, 2022
@MathiasVP MathiasVP requested a review from a team as a code owner March 8, 2022 11:46
@MathiasVP MathiasVP added the no-change-note-required This PR does not need a change note label Mar 8, 2022

@jketema jketema left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Only a few small comments.

Comment thread cpp/ql/lib/semmle/code/cpp/ir/dataflow/MustFlow.qll Outdated
Comment thread cpp/ql/lib/semmle/code/cpp/ir/dataflow/MustFlow.qll Outdated
Comment thread cpp/ql/lib/semmle/code/cpp/ir/dataflow/MustFlow.qll Outdated
source.getNode().asInstruction() = var and
// Only raise an alert if we're returning from the _same_ callable as the on that
// declared the stack variable.
var.getEnclosingFunction() = sink.getNode().getEnclosingCallable()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This seems new, or what this somewhere else before in a disguise that I didn't pick up on?

@MathiasVP MathiasVP Mar 8, 2022

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, it's new. The old must-flow predicates in cpp/return-stack-allocated-memory didn't have flow into function calls, but that was implemented (and indeed required) for cpp/unsafe-use-of-this. So I've ported flow into function calls to this new library.

This means that we now track flow into function calls in cpp/return-stack-allocated-memory as well, and we could now get a result from this snippet without this extra condition:

int* id(int* px) {
  return px; // this returns the local variable `x`, but it's fine as the local variable isn't declared in this scope.
}
void f() {
  int x;
  int* px = id(&x);
}

Hm, I should actually add this test to the test cases for the cpp/return-stack-allocated-memory query. Let me go ahead and do that!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

69417e1 adds the above test.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is this something that needs wider testing? LGTM or at least DCA?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't think this particular change warrants extended testing, but this PR as a whole definitely needs to be tested with DCA. I've already started a run which should hopefully be backlinked from this PR.

MathiasVP and others added 4 commits March 8, 2022 13:06
Co-authored-by: Jeroen Ketema <93738568+jketema@users.noreply.github.com>
Co-authored-by: Jeroen Ketema <93738568+jketema@users.noreply.github.com>
Co-authored-by: Jeroen Ketema <93738568+jketema@users.noreply.github.com>

@jketema jketema left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM assuming the DCA run ends well. I'll leave it to you to judge whether my opinion is sufficient for a merge 😄

* Holds if `instr` is an argument (or argument indirection) to a call, and
* `succ` is the corresponding initialization instruction in the call target.
*/
private predicate flowThroughCallable(Instruction argument, Instruction parameter) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This only handles flow into callables - should we name it something else, or do you expect to support flow out at some future point?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point. I would like to add flow out at some point, but I'm not exactly sure how to do that in a must-flow way.

@MathiasVP

Copy link
Copy Markdown
Contributor Author

DCA looks good. Merging 🎉

@MathiasVP MathiasVP merged commit dfb20f7 into github:main Mar 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C++ no-change-note-required This PR does not need a change note

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants