Detect integer demotions in UBSan
| Bugzilla Link | 21530 |
| Version | trunk |
| OS | All |
| Depends On | llvm/llvm-project#37365 llvm/llvm-project#37514 |
| Reporter | LLVM Bugzilla Contributor |
| CC | @filcab,@kcc,@LebedevRI,@RKSimon,@regehr,@zygoloid,@seanm,@vedantk |
| Fixed by commit(s) | 338288 338287 345660 345659 345661 9872ea4ed1de4c49300430e4f1f4dfc110a79ab9 |
Extended Description
UBSan currently does not check integer demotions i.e. casts from wider integer type to a smaller one. This is indeed not undefined behavior but rather implementation defined. Still, loosing higher bits when downcasting ints to chars is a constant source of bugs and it would be nice to have an optional check for it (like we already have for float-cast-overflow).
mentioned in issue llvm/llvm-project#37514
mentioned in issue llvm/llvm-project#37365
inc/dec: done For plain integers i think only bitfield handling is left.
And the sign change half is finally here (trunk) too. For scalars, i claim that the sanitizer is feature-complete. Only bitfield handling is missing.
Implicit sign change: https://reviews.llvm.org/D50250
So i have committed the first part in https://reviews.llvm.org/rC338288 & https://reviews.llvm.org/rCRT338287 (yay, before the 7.0 branch, unless there are still bots that didn't caught up yet..)
TODO: implicit-sign-change, and bitfield handling.
List of runtime errors detected during a check-llvm run I've attached a list of runtime errors found with this check during check-llvm. There appear to be 41 unique issue locations, although several reports appear to have common root causes.
One issue in TargetLoweringBase.cpp stands out as being especially troubling: getVectorTypeBreakdownMVT returns 512, but this is implicitly truncated to 0. We can clone this bug/file a separate bug for this issue.
I haven't seen any glaring FP's in the reports I've spot-checked and the compile-time numbers look good (see my last comment in https://reviews.llvm.org/D48958 for details).
https://reviews.llvm.org/D48958
Roman, can you put the patch(es) into phabricator so we can talk about them?
I got fully fed up with this, and hacked something up. It does detect #37552: /tmp/test.cpp:12:18: runtime error: implicit cast from type 'int' of value 256 (32-bit, signed) to type 'uint8_t' (aka 'unsigned char') changed the value to 0 (8-bit, unsigned)
And #35409 runtime error: implicit cast from type 'int' of value 510 (32-bit, signed) to type 'unsigned char' changed the value to 254 (8-bit, unsigned)
That would be incredibly useful. Is there a chance that this could make it into a release in the not too distant future?
I got fully fed up with this, and hacked something up. It does detect #37552: /tmp/test.cpp:12:18: runtime error: implicit cast from type 'int' of value 256 (32-bit, signed) to type 'uint8_t' (aka 'unsigned char') changed the value to 0 (8-bit, unsigned)
And #35409 runtime error: implicit cast from type 'int' of value 510 (32-bit, signed) to type 'unsigned char' changed the value to 254 (8-bit, unsigned)
Perhaps it would be useful to split conversion checking into explicit and implicit variants.
Yes, I think your reasoning makes a lot of sense. For people who are really worried about these kinds of errors, this seems like a useful mode.
Awesome, thanks Richard.
FWIW a prototype with implicit integer conversion checks led to these PR's in LLVM:
http://llvm.org/bugs/show_bug.cgi?id=14994 http://llvm.org/bugs/show_bug.cgi?id=15006
.. => useful! :)
Perhaps it would be useful to split conversion checking into explicit and implicit variants.
Yes, I think your reasoning makes a lot of sense. For people who are really worried about these kinds of errors, this seems like a useful mode.
I agree this would be a useful addition to Clang! Unsure of whether it belongs in -fsanitize=integer, but it's presently described to report "undefined or suspicious integer behavior".. which is a bit nebulous :).
There was code for this, but as Regehr mentioned it's against an LLVM version sufficiently old that I'm not sure how useful it would be these days.
While the subject is being discussed, I'd like to revive an idea I suggested previously on the mailing list[1]:
Perhaps it would be useful to split conversion checking into explicit and implicit variants.
The motivation for this is the hypothesis that implicit errors are "more likely" to be true positives. The reasoning is that implicit errors might be given less attention (by not being as obvious), and that explicit conversions may be used more frequently when a lossy conversion is intended (and therefore, hopefully, have been reasoned about at least as far as "I mean to truncate this integer").
A side benefit is that this provides a natural mechanism to mark implicit conversion false positives as "safe" by making them explicit.
I believe similar reasoning is used to explain why "-Wconversion" warns about implicit but not explicit truncations.
To continue this line of thought, the suggested way to move forward would be to implement -fsanitize={implicit-,explicit-,}integer-conversion and include only the implicit checks in -fsanitize=integer by default.
What do you guys think?
[1] http://lists.cs.uiuc.edu/pipermail/cfe-dev/2014-January/034808.html
At least one version of IOC did have checks for value-changing casts. Whether or not that code has any value 3-4 years later sounds doubtful, though, given that this seems pretty easy to re-implement. It's definitely a flag I'd use if it came back though.
This seems to be in a similar category to -fsanitize=unsigned-integer-overflow -- not UB, but something we might want to provide the ability to detect anyway. As a result, this shouldn't be part of -fsanitize=undefined, but we could consider including it in -fsanitize=integer.
Will, John: does IOC have anything like this?
assigned to @LebedevRI