Skip to content

Comments

C++: Multiplication overflow not possible due to type width#2577

Merged
geoffw0 merged 8 commits intogithub:masterfrom
MathiasVP:multiplication-overflow-not-possible-due-to-type-width
Jan 8, 2020
Merged

C++: Multiplication overflow not possible due to type width#2577
geoffw0 merged 8 commits intogithub:masterfrom
MathiasVP:multiplication-overflow-not-possible-due-to-type-width

Conversation

@MathiasVP
Copy link
Contributor

@MathiasVP MathiasVP commented Jan 2, 2020

This PR fixes the false positive reported in #2561.

Differences query: https://lgtm.com/query/3029067271222385694/

I have included 3 tests (from @jbj), all of which are currently not detected due to excluding values of "small types":

predicate likelySmall(Expr e) {
  ...
  or
  e.getType().getSize() <= 1
  or
  ...
}

However, it might be worth discussing whether we should still exclude such values from the analysis now that we're using SimpleRangeAnalysis.

@MathiasVP MathiasVP requested a review from a team as a code owner January 2, 2020 15:16
Copy link
Contributor

@jbj jbj left a comment

Choose a reason for hiding this comment

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

I think this approach introduces too many false negatives, so I hope we can do better.

@jbj jbj added the C++ label Jan 3, 2020
x2 = this.getLeftOperand().getFullyConverted().(AnalyzableExpr).maxValue() and
y1 = this.getRightOperand().getFullyConverted().(AnalyzableExpr).minValue() and
y2 = this.getRightOperand().getFullyConverted().(AnalyzableExpr).maxValue() and
result = (x1 * y1).maximum(x1 * y2).maximum(x2 * y1).maximum(x2 * y2)
Copy link
Contributor

Choose a reason for hiding this comment

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

I was surprised to find that all four cases are necessary (e.g. multiplying a number in the range [-10 .. -5] by a number in the range [5 .. 10] has a maximum result of -25).

Copy link
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.

Looks promising.

Any idea why ffmpeg.c line 187 is no longer reported?

@MathiasVP
Copy link
Contributor Author

MathiasVP commented Jan 7, 2020

Looks promising.

Any idea why ffmpeg.c line 187 is no longer reported?

Hmmm... It seems like VarAnalyzableExpr fails for accesses of the form a->b (and probably also a.b, although I haven't checked it), causing the analysis to discard multiplications that involve computing upper/lower bounds for such expressions. I'll dig into this!

I fear that this might make the results less impressive. 👎I'll update the PR when I'm at the bottom of this!

Edit: I've updated the PR and produced a new differences query.

@MathiasVP MathiasVP added WIP This is a work-in-progress, do not merge yet! and removed WIP This is a work-in-progress, do not merge yet! labels Jan 7, 2020
@geoffw0
Copy link
Contributor

geoffw0 commented Jan 8, 2020

Changes and new query differences LGTM. We have lost the bulk of the differences, but perhaps that's to be expected.

Performance looks to be fine as well - the predicates in this file take negligible time compared to computing SimpleRangeAnalysis, the CFG and other things (tested Qt, wxWidgets, wireshark).

In the long run (not in this PR), support for multiplication should probably be moved into a library file. I have a feeling support it was deliberately left out for the benefit of some uses cases, but perhaps we could provide an extension library?

@MathiasVP
Copy link
Contributor Author

In the long run (not in this PR), support for multiplication should probably be moved into a library file. I have a feeling support it was deliberately left out for the benefit of some uses cases, but perhaps we could provide an extension library?

AFAIK support for multiplication was attempted in https://git.semmle.com/Semmle/code/pull/17898, but was abandoned for performance reasons.

I do agree that it would be a useful extension library to be used when the number of query candidates has been reduced significantly (as it was in this case by the original predicate).

Copy link
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.

LGTM. I'm going to merge this.

I think this approach introduces too many false negatives, so I hope we can do better.

@jbj I think we've made considerable improvements since you posted this, but please do raise any concerns you still have.

["small types"] it might be worth discussing whether we should still exclude such values from the analysis now that we're using SimpleRangeAnalysis.

This might still be worth exploring as follow-up.

@geoffw0 geoffw0 merged commit cf5dd85 into github:master Jan 8, 2020
@MathiasVP MathiasVP changed the title Multiplication overflow not possible due to type width C++: Multiplication overflow not possible due to type width Jan 9, 2020
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.

3 participants