llvm-project/clang/test/CodeGenCXX/ubsan-bitfields.cpp
Vedant Kumar 5c13623a69 [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.
    ...

Differential Revision: https://reviews.llvm.org/D30423

llvm-svn: 297298
2017-03-08 17:38:57 +00:00

22 lines
510 B
C++

// RUN: %clang_cc1 -std=c++11 -triple x86_64-apple-darwin10 -emit-llvm -o - %s -fsanitize=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;
}