Skip to content

C++: Make exprMightOverflowPositively sound for unanalyzable expressions#5678

Merged
geoffw0 merged 5 commits into
github:mainfrom
MathiasVP:sound-expr-might-overflow-predicate
Apr 26, 2021
Merged

C++: Make exprMightOverflowPositively sound for unanalyzable expressions#5678
geoffw0 merged 5 commits into
github:mainfrom
MathiasVP:sound-expr-might-overflow-predicate

Conversation

@MathiasVP

@MathiasVP MathiasVP commented Apr 14, 2021

Copy link
Copy Markdown
Contributor

(and similarly for the exprMightOverflowNegatively predicate.)

This hopefully means that expressions that do not satisfy these predicates will never overflow/underflow.

This might Break Everything. But let's wait and see what CPP-differences says: https://jenkins.internal.semmle.com/job/Changes/job/CPP-Differences/1917/

…d for unanalyzable expressions. This hopefully means that expressions that do not satisfy these predicates will never overflow/underflow.
@MathiasVP MathiasVP added the C++ label Apr 14, 2021
geoffw0
geoffw0 previously approved these changes Apr 15, 2021

@geoffw0 geoffw0 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.

Change LGTM.

Surprisingly there's only one change in CPP differences - a heavily templated comparison in Boost that we lose, that looks like it was indeed a false positive (though I'm not sure the exact mechanics of it being ruled out by this change).

@MathiasVP

Copy link
Copy Markdown
Contributor Author

I think the exprMightOverFlow predicates become a bit too noisy when we include all unanalyzable expressions. Consider this example from https://github.com/github/codeql/blob/main/cpp/ql/test/query-tests/Security/CWE/CWE-190/semmle/tainted/test2.cpp:

typedef struct _myStruct {
	s64 val;
} MyStruct;

void test2_sink(s64 v, MyStruct s, MyStruct &s_r, MyStruct *s_p)
{
	s64 v1 = v * 2; // bad
	s64 v2 = s.val * 2; // bad
	s64 v3 = s_r.val * 2; // bad
	s64 v4 = s_p->val * 2; // bad
}

We now flag all of the field accesses in this function as possibly overflowing, which doesn't make a lot of sense.

@MathiasVP

MathiasVP commented Apr 15, 2021

Copy link
Copy Markdown
Contributor Author

Surprisingly there's only one change in CPP differences - a heavily templated comparison in Boost that we lose, that looks like it was indeed a false positive (though I'm not sure the exact mechanics of it being ruled out by this change).

Yeah, I agree that the CPP-differences change looks good (the query should probably filter out templates anyway). I don't like why it's being ruled out, though. We rule it out because we now consider both of these operations as potentially overflowing:

bool CondV = (thread_access_mode_of< LeftLockT >::value < thread_access_mode_of< RightLockT >::value)

but really they're just constant values that will be deduced in every template instantiation.

I'm also not sure I like the changes in our tests.

One thing we could do is to only say that unanalyzable Operations are potentially overflowing/underflowing. Although this is starting to feel less principled.

@geoffw0

geoffw0 commented Apr 15, 2021

Copy link
Copy Markdown
Contributor

Should there be two versions of the predicates then? It seems legitimate to only want cases where we're least / most confident an overflow can occur (even if accuracy is not guaranteed either way), but on the other hand this will clutter up the interface to this library and give users more opportunity to make the wrong choice.

@MathiasVP

Copy link
Copy Markdown
Contributor Author

Should there be two versions of the predicates then? It seems legitimate to only want cases where we're least / most confident an overflow can occur (even if accuracy is not guaranteed either way), but on the other hand this will clutter up the interface to this library and give users more opportunity to make the wrong choice.

Yeah, there seem to be situations where we want the old semantics and other situations that call for the new semantics of those predicates. I was hoping that we didn't have to add a whole new predicate to the public interface, though :( This seems to be a discussion that would be helpful to have for the future IR-based range analysis as well, so maybe we should bring the discussion to a team meeting (as a very low priority topic).

@MathiasVP

Copy link
Copy Markdown
Contributor Author

Note to self if we end up merging a version of this: Use this PR to make bb447d7 more aesthetically pleasing.

     reported as overflowing when we cannot analyze them.
@MathiasVP MathiasVP force-pushed the sound-expr-might-overflow-predicate branch from fd6368f to e607712 Compare April 23, 2021 09:14
@MathiasVP

Copy link
Copy Markdown
Contributor Author

@MathiasVP MathiasVP marked this pull request as ready for review April 26, 2021 06:39
@MathiasVP MathiasVP requested a review from a team as a code owner April 26, 2021 06:39
@MathiasVP

Copy link
Copy Markdown
Contributor Author

New CPP-difference: https://jenkins.internal.semmle.com/job/Changes/job/CPP-Differences/1938/

CPP-differences looks good. There is one query result change, and it's in a query that doesn't actually use range analysis so it looks like wobble.

There was a single test change. This is fixed by 86822f6 following a Slack discussion here.

@MathiasVP MathiasVP added the no-change-note-required This PR does not need a change note label Apr 26, 2021
@jbj

jbj commented Apr 26, 2021

Copy link
Copy Markdown
Contributor

Why is no change note required? The range analysis library has external users, and they might be affected by this new behaviour.

@MathiasVP

Copy link
Copy Markdown
Contributor Author

Why is no change note required? The range analysis library has external users, and they might be affected by this new behaviour.

Good point. We should have a change note for this.

@MathiasVP MathiasVP removed the no-change-note-required This PR does not need a change note label Apr 26, 2021
@jbj

jbj commented Apr 26, 2021

Copy link
Copy Markdown
Contributor

LGTM. I'll leave the merging to @geoffw0 when he's satisfied.

@geoffw0 geoffw0 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.

It's a shame library changes like this are difficult to explore on LGTM, but the unit tests + CPP-Differences do cover us.

@geoffw0 geoffw0 merged commit 0e7eeb3 into github:main Apr 26, 2021
@geoffw0

geoffw0 commented Apr 26, 2021

Copy link
Copy Markdown
Contributor

Note to self if we end up merging a version of this: Use this PR to make bb447d7 more aesthetically pleasing.

^ reminder.

MathiasVP added a commit to MathiasVP/ql that referenced this pull request Apr 27, 2021
@MathiasVP

Copy link
Copy Markdown
Contributor Author

Note to self if we end up merging a version of this: Use this PR to make bb447d7 more aesthetically pleasing.

^ reminder.

Thanks for the reminder. That fix is now up: #5780

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants