Visitar URL original
C++: Support `(bool)x` and `!x` in SimpleRangeAnalysis by jbj · Pull Request #4227 · github/codeql · GitHub
The Wayback Machine - https://web.archive.org/web/20260528063925/https://github.com/github/codeql/pull/4227
Skip to content

C++: Support (bool)x and !x in SimpleRangeAnalysis#4227

Merged
MathiasVP merged 3 commits into
github:mainfrom
jbj:SimpleRangeAnalysis-NotExpr
Sep 11, 2020
Merged

C++: Support (bool)x and !x in SimpleRangeAnalysis#4227
MathiasVP merged 3 commits into
github:mainfrom
jbj:SimpleRangeAnalysis-NotExpr

Conversation

@jbj
Copy link
Copy Markdown
Contributor

@jbj jbj commented Sep 8, 2020

This was harder than I thought because of the complication that in if (x) there is no particular expression that compares x to zero, and SimpleRangeAnalysis is built around having such an expression. It's also different in C and C++: in C++ there's a BoolConversion while in C there is none.

@jbj jbj requested a review from a team as a code owner September 8, 2020 15:07
@github-actions github-actions Bot added the C++ label Sep 8, 2020
@jbj
Copy link
Copy Markdown
Contributor Author

jbj commented Sep 8, 2020 •

I've learned my lesson from #4204, so I'm running https://jenkins.internal.semmle.com/job/Changes/job/CPP-Differences/1419/ before we merge this PR.

exists(VariableAccess access, float neConstant, float upper |
isNEPhi(v, phi, access, neConstant) and
upper = getFullyConvertedUpperBounds(access) and
upper = getTruncatedUpperBounds(access) and
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.

This change is required because in C++ when we write if (myInt) there will be a BoolConversion on access, and so the fully-converted upper bounds will be at most 1.

I thought getTruncatedUpperBounds would be right for the upperBoundFromGuard case above too, but if I make that change then I break the tests that this PR adds in two_bounds_from_one_test. No other tests break, so our coverage was lacking before.

@rdmarsh2
Copy link
Copy Markdown
Contributor

rdmarsh2 commented Sep 8, 2020

Can you add a test using !! as well? It's a fairly common substitute for (bool) in C code.

@jbj
Copy link
Copy Markdown
Contributor Author

jbj commented Sep 9, 2020

Can you add a test using !! as well? It's a fairly common substitute for (bool) in C code.

Based on the code I've seen, I don't think !! or !(x < 4) will work. To support those, we can either teach RangeSSA about negations or teach the CFG construction about them. In both cases, I think negation support belongs in a follow-up PR.

Copy link
Copy Markdown
Contributor

@geoffw0 geoffw0 left a comment

Choose a reason for hiding this comment

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

This is quite a complicated change. LGTM, but I wouldn't mind a second opinion.

// both an upper bound and a lower bound even when there is only one
// inequality in the source. For example `signedInt < 4U` establishes that
// `signedInt >= 0` since if `signedInt` were negative then it would be
// greater than 4 in the unsigned comparison.
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.

These are great tests!

Copy link
Copy Markdown
Contributor

@MathiasVP MathiasVP left a comment

Choose a reason for hiding this comment

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

I second Geoffrey's LGTM (but also his comment that it's a complicated PR on top of an already complicated library).

@geoffw0
Copy link
Copy Markdown
Contributor

geoffw0 commented Sep 10, 2020

In which case, I propose one of us merges this once the diff job is complete, assuming the results of it are good.

@jbj
Copy link
Copy Markdown
Contributor Author

jbj commented Sep 11, 2020

The CPP-Differences job has finally succeeded. The result changes all LGTM. I especially like that it's finding this bug in Linux, which has since been fixed.

The performance changes LGTM also. While the overall numbers vary within the noise threshold, the per-query numbers show a 10-30s slowdown in SignedOverflowCheck.ql. I'll attribute that slowdown to the extra work being done as a result of this PR.

Copy link
Copy Markdown
Contributor

@MathiasVP MathiasVP left a comment

Choose a reason for hiding this comment

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

That's awesome. LGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants