-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodeGen] [ubsan] Respect integer overflow handling in abs builtin
Currenly both Clang and GCC support the following set of flags that control code gen of signed overflow: * -fwrapv: overflow is defined as in two-complement * -ftrapv: overflow traps * -fsanitize=signed-integer-overflow: if undefined (no -fwrapv), then overflow behaviour is controlled by UBSan runtime, overrides -ftrapv Howerver, clang ignores these flags for __builtin_abs(int) and its higher-width versions, so passing minimum integer value always causes poison. The same holds for *abs(), which are not handled in frontend at all but folded to llvm.abs.* intrinsics during InstCombinePass. The intrinsics are not instrumented by UBSan, so the functions need special handling as well. This patch does a few things: * Handle *abs() in CGBuiltin the same way as __builtin_*abs() * -fsanitize=signed-integer-overflow now properly instruments abs() with UBSan * -fwrapv and -ftrapv handling for abs() is made consistent with GCC Fixes #45129 and #45794 Reviewed By: efriedma, MaskRay Differential Revision: https://reviews.llvm.org/D156821
- Loading branch information
Showing
4 changed files
with
144 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// RUN: %clang_cc1 -triple x86_64 -emit-llvm %s -o - -fwrapv -fsanitize=signed-integer-overflow | FileCheck %s --check-prefix=WRAPV | ||
// RUN: %clang_cc1 -triple x86_64 -emit-llvm %s -o - -ftrapv | FileCheck %s --check-prefixes=BOTH-TRAP,TRAPV | ||
// RUN: %clang_cc1 -triple x86_64 -emit-llvm %s -o - -fsanitize=signed-integer-overflow | FileCheck %s --check-prefixes=BOTH-TRAP,CATCH_UB | ||
// COM: TODO: Support -ftrapv-handler. | ||
|
||
extern int abs(int x); | ||
|
||
int absi(int x) { | ||
// WRAPV: [[NEG:%.*]] = sub i32 0, [[X:%.*]] | ||
// WRAPV: [[CMP:%.*]] = icmp slt i32 [[X]], 0 | ||
// WRAPV: [[SEL:%.*]] = select i1 [[CMP]], i32 [[NEG]], i32 [[X]] | ||
// | ||
// BOTH-TRAP: [[NEG:%.*]] = call { i32, i1 } @llvm.ssub.with.overflow.i32(i32 0, i32 [[X:%.*]]) | ||
// BOTH-TRAP: [[NEGV:%.*]] = extractvalue { i32, i1 } [[NEG]], 0 | ||
// BOTH-TRAP: [[OFL:%.*]] = extractvalue { i32, i1 } [[NEG]], 1 | ||
// BOTH-TRAP: [[NOFL:%.*]] = xor i1 [[OFL]], true | ||
// BOTH-TRAP: br i1 [[NOFL]], label %[[CONT:.*]], label %[[TRAP:[a-zA-Z_.]*]] | ||
// BOTH-TRAP: [[TRAP]]: | ||
// TRAPV-NEXT: llvm.ubsantrap | ||
// CATCH_UB: @__ubsan_handle_negate_overflow | ||
// BOTH-TRAP-NEXT: unreachable | ||
// BOTH-TRAP: [[CONT]]: | ||
// BOTH-TRAP-NEXT: [[ABSCOND:%.*]] = icmp slt i32 [[X]], 0 | ||
// BOTH-TRAP-NEXT: select i1 [[ABSCOND]], i32 [[NEGV]], i32 [[X]] | ||
return abs(x); | ||
} | ||
|
||
int babsi(int x) { | ||
// WRAPV: [[NEG:%.*]] = sub i32 0, [[X:%.*]] | ||
// WRAPV: [[CMP:%.*]] = icmp slt i32 [[X]], 0 | ||
// WRAPV: [[SEL:%.*]] = select i1 [[CMP]], i32 [[NEG]], i32 [[X]] | ||
// | ||
// BOTH-TRAP: [[NEG:%.*]] = call { i32, i1 } @llvm.ssub.with.overflow.i32(i32 0, i32 [[X:%.*]]) | ||
// BOTH-TRAP: [[NEGV:%.*]] = extractvalue { i32, i1 } [[NEG]], 0 | ||
// BOTH-TRAP: [[OFL:%.*]] = extractvalue { i32, i1 } [[NEG]], 1 | ||
// BOTH-TRAP: [[NOFL:%.*]] = xor i1 [[OFL]], true | ||
// BOTH-TRAP: br i1 [[NOFL]], label %[[CONT:.*]], label %[[TRAP:[a-zA-Z_.]*]] | ||
// BOTH-TRAP: [[TRAP]]: | ||
// TRAPV-NEXT: llvm.ubsantrap | ||
// CATCH_UB: @__ubsan_handle_negate_overflow | ||
// BOTH-TRAP-NEXT: unreachable | ||
// BOTH-TRAP: [[CONT]]: | ||
// BOTH-TRAP-NEXT: [[ABSCOND:%.*]] = icmp slt i32 [[X]], 0 | ||
// BOTH-TRAP-NEXT: select i1 [[ABSCOND]], i32 [[NEGV]], i32 [[X]] | ||
return __builtin_abs(x); | ||
} |
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,28 @@ | ||
// RUN: %clangxx -fsanitize=signed-integer-overflow -w %s -O3 -o %t | ||
// RUN: %run %t 2>&1 | FileCheck %s --check-prefix=RECOVER | ||
|
||
// RUN: %clangxx -fsanitize=signed-integer-overflow -fno-sanitize-recover=signed-integer-overflow -w %s -O3 -o %t.abort | ||
// RUN: not %run %t.abort 2>&1 | FileCheck %s --check-prefix=ABORT | ||
|
||
#include <limits.h> | ||
#include <stdlib.h> | ||
|
||
int main() { | ||
// ABORT: abs.cpp:[[#@LINE+3]]:17: runtime error: negation of -[[#]] cannot be represented in type 'int'; cast to an unsigned type to negate this value to itself | ||
// RECOVER: abs.cpp:[[#@LINE+2]]:17: runtime error: negation of -[[#]] cannot be represented in type 'int'; cast to an unsigned type to negate this value to itself | ||
// RECOVER: abs.cpp:[[#@LINE+2]]:7: runtime error: negation of -[[#]] cannot be represented in type 'int'; cast to an unsigned type to negate this value to itself | ||
__builtin_abs(INT_MIN); | ||
abs(INT_MIN); | ||
|
||
// RECOVER: abs.cpp:[[#@LINE+2]]:18: runtime error: negation of -[[#]] cannot be represented in type 'long'; cast to an unsigned type to negate this value to itself | ||
// RECOVER: abs.cpp:[[#@LINE+2]]:8: runtime error: negation of -[[#]] cannot be represented in type 'long'; cast to an unsigned type to negate this value to itself | ||
__builtin_labs(LONG_MIN); | ||
labs(LONG_MIN); | ||
|
||
// RECOVER: abs.cpp:[[#@LINE+2]]:19: runtime error: negation of -[[#]] cannot be represented in type 'long long'; cast to an unsigned type to negate this value to itself | ||
// RECOVER: abs.cpp:[[#@LINE+2]]:9: runtime error: negation of -[[#]] cannot be represented in type 'long long'; cast to an unsigned type to negate this value to itself | ||
__builtin_llabs(LLONG_MIN); | ||
llabs(LLONG_MIN); | ||
|
||
return 0; | ||
} |