Introduce `OverflowBehaviorType` (OBT), a new type attribute in Clang that provides developers with fine-grained control over the overflow behavior of integer types. This feature allows for a more nuanced approach to integer safety, achieving better granularity than global compiler flags like `-fwrapv` and `-ftrapv`. Type specifiers are also available as keywords `__ob_wrap` and `__ob_trap`. These can be applied to integer types (both signed and unsigned) as well as typedef declarations, where the behavior is one of the following: * `wrap`: Guarantees that arithmetic operations on the type will wrap on overflow, similar to `-fwrapv`. This suppresses UBSan's integer overflow checks for the attributed type and prevents eager compiler optimizations. * `trap`: Enforces overflow checking for the type, even when global flags like `-fwrapv` would otherwise suppress it. A key aspect of this feature is its interaction with existing mechanisms. `OverflowBehaviorType` takes precedence over global flags and, notably, over entries in the Sanitizer Special Case List (SSCL). This allows developers to "allowlist" critical types for overflow instrumentation, even if they are disabled by a broad rule in an SSCL. Signed-off-by: Justin Stitt <justinstitt@google.com>
13 lines
513 B
C++
13 lines
513 B
C++
// RUN: %clang_cc1 -emit-llvm %s -o - -fms-extensions -triple=x86_64-pc-win32 -fexperimental-overflow-behavior-types | FileCheck %s
|
|
|
|
#define __wrap __attribute__((overflow_behavior(wrap)))
|
|
#define __trap __attribute__((overflow_behavior(trap)))
|
|
|
|
typedef int __ob_wrap int_wrap;
|
|
|
|
// CHECK: define dso_local void @"?test_wrap_int@@YAXU?$ObtWrap_@H@__clang@@@Z"
|
|
void test_wrap_int(int_wrap x) {}
|
|
|
|
// CHECK: define dso_local void @"?test_trap_int@@YAXU?$ObtTrap_@H@__clang@@@Z"
|
|
void test_trap_int(int __ob_trap y) {}
|