This is an archive of the discontinued LLVM Phabricator instance.

[ubsan] Skip range checks for width-limited values
Needs ReviewPublic

Authored by vsk on Mar 7 2017, 5:36 PM.

Details

Reviewers
jroelofs
arphaman
Summary

UBSan can check that scalar loads provide in-range values. When we load
a value from a bitfield, we know that the range of the value is
constrained by the bitfield's width. This patch teaches UBSan how to use
that information to skip emitting some range checks.

This depends on / is a follow-up to: https://reviews.llvm.org/D30423

Diff Detail

Event Timeline

vsk created this revision.Mar 7 2017, 5:36 PM
vsk updated this revision to Diff 90975.Mar 7 2017, 6:04 PM
  • Fix an incorrect comment on the field "e2" in struct S. We emit a check for it because 0b11 = -1 = 3.
  • Skip the range check on the field "e2" in struct S2, because the range of the bitfield is the same as the range clang infers for the enum.
arphaman edited edge metadata.Mar 8 2017, 7:28 AM

You should also add a test for enum E : unsigned.

filcab added a subscriber: filcab.Mar 8 2017, 8:19 AM
filcab added inline comments.
test/CodeGenCXX/ubsan-bitfields.cpp
39

Add checks/check-nots to make sure the thing you don't want isn't emitted, not just !nosanitize
The checks help document what you're trying to get in each test case.
Here I'd prefer to have a CHECK-NOT: __ubsan_handle_load_invalid_value than a check-not for !nosanitize, since checking for the handler call is more explicit.

vsk added a comment.Mar 8 2017, 9:11 AM

You should also add a test for enum E : unsigned.

Ubsan doesn't try to infer the range of enums with a fixed, underlying type. I'll add a test case to make sure we don't insert a check.

test/CodeGenCXX/ubsan-bitfields.cpp
39

Good point, I will update the patch shortly.

vsk updated this revision to Diff 91030.Mar 8 2017, 9:12 AM
  • Make check-not's a bit stricter per Filipe's comment.
  • Add a negative test for fixed enums.