Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Retry: [ubsan] Detect UB loads from bitfields
It's possible to load out-of-range values from bitfields backed by a boolean or an enum. Check for UB loads from bitfields. This is the motivating example: struct S { BOOL b : 1; // Signed ObjC BOOL. }; S s; s.b = 1; // This is actually stored as -1. if (s.b == 1) // Evaluates to false, -1 != 1. ... Changes since the original commit: - Single-bit bools are a special case (see CGF::EmitFromMemory), and we can't avoid dealing with them when loading from a bitfield. Don't try to insert a check in this case. Differential Revision: https://reviews.llvm.org/D30423 llvm-svn: 297389
- Loading branch information
Showing
5 changed files
with
102 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// RUN: %clang_cc1 -std=c++11 -triple x86_64-apple-darwin10 -emit-llvm -o - %s -fsanitize=bool,enum | FileCheck %s | ||
|
||
enum E { | ||
a = 1, | ||
b = 2, | ||
c = 3 | ||
}; | ||
|
||
struct S { | ||
E e1 : 10; | ||
}; | ||
|
||
// CHECK-LABEL: define i32 @_Z4loadP1S | ||
E load(S *s) { | ||
// CHECK: [[LOAD:%.*]] = load i16, i16* {{.*}} | ||
// CHECK: [[CLEAR:%.*]] = and i16 [[LOAD]], 1023 | ||
// CHECK: [[CAST:%.*]] = zext i16 [[CLEAR]] to i32 | ||
// CHECK: icmp ule i32 [[CAST]], 3, !nosanitize | ||
// CHECK: call void @__ubsan_handle_load_invalid_value | ||
return s->e1; | ||
} | ||
|
||
struct Bool { | ||
bool b1 : 1; | ||
bool b2 : 7; | ||
bool b3 : 16; | ||
}; | ||
|
||
// CHECK-LABEL: define zeroext i1 @_Z13load_cpp_boolP4Bool | ||
bool load_cpp_bool(Bool *b) { | ||
// CHECK-NOT: call void @__ubsan_handle_load_invalid_value | ||
// CHECK-NOT: !nosanitize | ||
return b->b1 || b->b2 || b->b3; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters