Update condition to remove addition that may cause an overflow.
Resolves PR42814.
Details
Diff Detail
- Repository
- rL LLVM
Event Timeline
lib/Analysis/ConstantFolding.cpp | ||
---|---|---|
550–552 ↗ | (On Diff #212220) | Is Offset==0 == 0'th byte of the constant? |
lib/Analysis/ConstantFolding.cpp | ||
---|---|---|
551 ↗ | (On Diff #212226) | This check looks suspicious to me. |
lib/Analysis/ConstantFolding.cpp | ||
---|---|---|
551 ↗ | (On Diff #212226) | If Offset can be a negative value, it's possible to get an OOB access when Offset + BytesLoaded <= 0. |
lib/Analysis/ConstantFolding.cpp | ||
---|---|---|
551 ↗ | (On Diff #212226) | Okay, sounds plausible. |
lib/Analysis/ConstantFolding.cpp | ||
---|---|---|
551 ↗ | (On Diff #212226) | I think with the check as it is, there still could be an overflow *in theory*, e.g. for constants that are int64_max big. Given that we only allow global constants with initializers, this would mean a huge bitcode file, but while you're at it, it might be worth changing the comparisons to if (Offfset <= -BytesLoaded) as well |
test/Transforms/SCCP/ubsan_overflow.ll | ||
---|---|---|
1 ↗ | (On Diff #212226) | Shouldn't this be opt < %s -sccp -S |
Address comment.
test/Transforms/SCCP/ubsan_overflow.ll | ||
---|---|---|
1 ↗ | (On Diff #212226) | The behavior is the same. |
lib/Analysis/ConstantFolding.cpp | ||
---|---|---|
551 ↗ | (On Diff #212428) | Use static_cast? That also makes it obvious whether the cast happens before or after the multiply. |
lgtm with some nits
lib/Analysis/ConstantFolding.cpp | ||
---|---|---|
547 ↗ | (On Diff #212609) | OOC is there a reason to flip the order of the two checks? If yes, can you please update the commit message (so as to not confuse folks digging through the commit history in the future)? |
test/Transforms/SCCP/ubsan_overflow.ll | ||
12 ↗ | (On Diff #212609) | We probably should remove the unreachable. As written the function has unconditional UB which means some other optimization within SCCP could kick in. Probably the best thing to do is to return the value you loaded so that it can't get DCE'd away either (SCCP is unlikely to do that, but you never know). |