Index: lib/ubsan/ubsan_checks.inc =================================================================== --- lib/ubsan/ubsan_checks.inc +++ lib/ubsan/ubsan_checks.inc @@ -30,8 +30,12 @@ "integer-divide-by-zero") UBSAN_CHECK(FloatDivideByZero, "float-divide-by-zero", "float-divide-by-zero") UBSAN_CHECK(InvalidBuiltin, "invalid-builtin-use", "invalid-builtin-use") -UBSAN_CHECK(ImplicitIntegerTruncation, "implicit-integer-truncation", - "implicit-integer-truncation") +UBSAN_CHECK(ImplicitUnsignedIntegerTruncation, + "implicit-unsigned-integer-truncation", + "implicit-unsigned-integer-truncation") +UBSAN_CHECK(ImplicitSignedIntegerTruncation, + "implicit-signed-integer-truncation", + "implicit-signed-integer-truncation") UBSAN_CHECK(InvalidShiftBase, "invalid-shift-base", "shift-base") UBSAN_CHECK(InvalidShiftExponent, "invalid-shift-exponent", "shift-exponent") UBSAN_CHECK(OutOfBoundsIndex, "out-of-bounds-index", "bounds") Index: lib/ubsan/ubsan_handlers.h =================================================================== --- lib/ubsan/ubsan_handlers.h +++ lib/ubsan/ubsan_handlers.h @@ -125,7 +125,9 @@ /// Known implicit conversion check kinds. /// Keep in sync with the enum of the same name in CGExprScalar.cpp enum ImplicitConversionCheckKind : unsigned char { - ICCK_IntegerTruncation = 0, + // Kind 0 was used briefly. + ICCK_UnsignedIntegerTruncation = 1, + ICCK_SignedIntegerTruncation = 2, }; struct ImplicitConversionData { Index: lib/ubsan/ubsan_handlers.cc =================================================================== --- lib/ubsan/ubsan_handlers.cc +++ lib/ubsan/ubsan_handlers.cc @@ -458,8 +458,12 @@ ErrorType ET = ErrorType::GenericUB; switch (Data->Kind) { - case ICCK_IntegerTruncation: - ET = ErrorType::ImplicitIntegerTruncation; + + case ICCK_UnsignedIntegerTruncation: + ET = ErrorType::ImplicitUnsignedIntegerTruncation; + break; + case ICCK_SignedIntegerTruncation: + ET = ErrorType::ImplicitSignedIntegerTruncation; break; } Index: test/fuzzer/ImplicitUnsignedIntegerTruncationTest.cpp =================================================================== --- /dev/null +++ test/fuzzer/ImplicitUnsignedIntegerTruncationTest.cpp @@ -0,0 +1,27 @@ +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. + +// Test for unsigned-integer-overflow. +#include +#include +#include +#include +#include +#include + +static volatile int Sink; +static unsigned char Large = UINT8_MAX; + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { + assert(Data); + if (Size > 0 && Data[0] == 'H') { + Sink = 1; + if (Size > 1 && Data[1] == 'i') { + Sink = 2; + if (Size > 2 && Data[2] == '!') { + Large = (unsigned int)Large + 1U; // 'char overflow'. + } + } + } + return 0; +} Index: test/fuzzer/fuzzer-implicit-integer-truncation.test =================================================================== --- test/fuzzer/fuzzer-implicit-integer-truncation.test +++ /dev/null @@ -1,5 +0,0 @@ -RUN: rm -f %t-ImplicitIntegerTruncationTest-Ubsan -RUN: %cpp_compiler -fsanitize=implicit-integer-truncation -fno-sanitize-recover=all %S/ImplicitIntegerTruncationTest.cpp -o %t-ImplicitIntegerTruncationTest-Ubsan -RUN: not %run %t-ImplicitIntegerTruncationTest-Ubsan 2>&1 | FileCheck %s -CHECK: ImplicitIntegerTruncationTest.cpp:22:17: runtime error: implicit conversion from type 'int' of value 256 (32-bit, signed) to type 'unsigned char' changed the value to 0 (8-bit, unsigned) -CHECK: Test unit written to ./crash- Index: test/fuzzer/fuzzer-implicit-signed-integer-truncation.test =================================================================== --- /dev/null +++ test/fuzzer/fuzzer-implicit-signed-integer-truncation.test @@ -0,0 +1,5 @@ +RUN: rm -f %t-ImplicitSignedIntegerTruncationTest-Ubsan +RUN: %cpp_compiler -fsanitize=implicit-signed-integer-truncation -fno-sanitize-recover=all %S/ImplicitSignedIntegerTruncationTest.cpp -o %t-ImplicitSignedIntegerTruncationTest-Ubsan +RUN: not %run %t-ImplicitSignedIntegerTruncationTest-Ubsan 2>&1 | FileCheck %s +CHECK: ImplicitSignedIntegerTruncationTest.cpp:22:17: runtime error: implicit conversion from type 'int' of value 256 (32-bit, signed) to type 'unsigned char' changed the value to 0 (8-bit, unsigned) +CHECK: Test unit written to ./crash- Index: test/fuzzer/fuzzer-implicit-unsigned-integer-truncation.test =================================================================== --- /dev/null +++ test/fuzzer/fuzzer-implicit-unsigned-integer-truncation.test @@ -0,0 +1,5 @@ +RUN: rm -f %t-ImplicitUnsignedIntegerTruncationTest-Ubsan +RUN: %cpp_compiler -fsanitize=implicit-unsigned-integer-truncation -fno-sanitize-recover=all %S/ImplicitUnsignedIntegerTruncationTest.cpp -o %t-ImplicitUnsignedIntegerTruncationTest-Ubsan +RUN: not %run %t-ImplicitUnsignedIntegerTruncationTest-Ubsan 2>&1 | FileCheck %s +CHECK: ImplicitUnsignedIntegerTruncationTest.cpp:22:17: runtime error: implicit conversion from type 'unsigned int' of value 256 (32-bit, unsigned) to type 'unsigned char' changed the value to 0 (8-bit, unsigned) +CHECK: Test unit written to ./crash- Index: test/ubsan/TestCases/ImplicitConversion/integer-truncation-blacklist.c =================================================================== --- test/ubsan/TestCases/ImplicitConversion/integer-truncation-blacklist.c +++ /dev/null @@ -1,26 +0,0 @@ -// FIXME: https://code.google.com/p/address-sanitizer/issues/detail?id=316 -// I'm not sure this is actually *that* issue, but this seems oddly similar to the other XFAIL'ed cases. -// XFAIL: android -// UNSUPPORTED: ios - -// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" -// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" -// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" -// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" - -// RUN: rm -f %tmp -// RUN: echo "[implicit-integer-truncation]" >> %tmp -// RUN: echo "fun:implicitTruncation" >> %tmp -// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O0 %s -o %t && not %run %t 2>&1 | not FileCheck %s -// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O1 %s -o %t && not %run %t 2>&1 | not FileCheck %s -// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O2 %s -o %t && not %run %t 2>&1 | not FileCheck %s -// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O3 %s -o %t && not %run %t 2>&1 | not FileCheck %s - -unsigned char implicitTruncation(unsigned int argc) { - return argc; // BOOM -// CHECK: {{.*}}integer-truncation-blacklist.c:[[@LINE-1]]:10: runtime error: implicit conversion from type 'unsigned int' of value 4294967295 (32-bit, unsigned) to type 'unsigned char' changed the value to 255 (8-bit, unsigned) -} - -int main(int argc, char **argv) { - return implicitTruncation(~0U); -} Index: test/ubsan/TestCases/ImplicitConversion/signed-integer-truncation-blacklist.c =================================================================== --- /dev/null +++ test/ubsan/TestCases/ImplicitConversion/signed-integer-truncation-blacklist.c @@ -0,0 +1,58 @@ +// FIXME: https://code.google.com/p/address-sanitizer/issues/detail?id=316 +// I'm not sure this is actually *that* issue, but this seems oddly similar to the other XFAIL'ed cases. +// XFAIL: android +// UNSUPPORTED: ios + +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" + +// RUN: rm -f %tmp +// RUN: echo "[integer]" >> %tmp +// RUN: echo "fun:implicitUnsignedTruncation" >> %tmp +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O0 %s -o %t && not %run %t 2>&1 | not FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O1 %s -o %t && not %run %t 2>&1 | not FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O2 %s -o %t && not %run %t 2>&1 | not FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O3 %s -o %t && not %run %t 2>&1 | not FileCheck %s + +// RUN: rm -f %tmp +// RUN: echo "[implicit-conversion]" >> %tmp +// RUN: echo "fun:implicitUnsignedTruncation" >> %tmp +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O0 %s -o %t && not %run %t 2>&1 | not FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O1 %s -o %t && not %run %t 2>&1 | not FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O2 %s -o %t && not %run %t 2>&1 | not FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O3 %s -o %t && not %run %t 2>&1 | not FileCheck %s + +// RUN: rm -f %tmp +// RUN: echo "[implicit-integer-truncation]" >> %tmp +// RUN: echo "fun:implicitUnsignedTruncation" >> %tmp +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O0 %s -o %t && not %run %t 2>&1 | not FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O1 %s -o %t && not %run %t 2>&1 | not FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O2 %s -o %t && not %run %t 2>&1 | not FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O3 %s -o %t && not %run %t 2>&1 | not FileCheck %s + +// RUN: rm -f %tmp +// RUN: echo "[implicit-signed-integer-truncation]" >> %tmp +// RUN: echo "fun:implicitUnsignedTruncation" >> %tmp +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O0 %s -o %t && not %run %t 2>&1 | not FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O1 %s -o %t && not %run %t 2>&1 | not FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O2 %s -o %t && not %run %t 2>&1 | not FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O3 %s -o %t && not %run %t 2>&1 | not FileCheck %s + +// RUN: rm -f %tmp +// RUN: echo "[implicit-unsigned-integer-truncation]" >> %tmp +// RUN: echo "fun:implicitUnsignedTruncation" >> %tmp +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s + +unsigned char implicitUnsignedTruncation(signed int argc) { + return argc; // BOOM +// CHECK: {{.*}}signed-integer-truncation-blacklist.c:[[@LINE-1]]:10: runtime error: implicit conversion from type 'int' of value -1 (32-bit, signed) to type 'unsigned char' changed the value to 255 (8-bit, unsigned) +} + +int main(int argc, char **argv) { + return implicitUnsignedTruncation(-1); +} Index: test/ubsan/TestCases/ImplicitConversion/signed-integer-truncation-summary.cpp =================================================================== --- /dev/null +++ test/ubsan/TestCases/ImplicitConversion/signed-integer-truncation-summary.cpp @@ -0,0 +1,13 @@ +// RUN: %clangxx -fsanitize=implicit-signed-integer-truncation %s -o %t +// RUN: %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-NOTYPE +// RUN: %env_ubsan_opts=report_error_type=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-TYPE +// REQUIRES: !ubsan-standalone && !ubsan-standalone-static + +#include + +int main() { + uint8_t t0 = int32_t(-1); + // CHECK-NOTYPE: SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior {{.*}}summary.cpp:[[@LINE-1]]:16 + // CHECK-TYPE: SUMMARY: UndefinedBehaviorSanitizer: implicit-signed-integer-truncation {{.*}}summary.cpp:[[@LINE-2]]:16 + return 0; +} Index: test/ubsan/TestCases/ImplicitConversion/signed-integer-truncation.c =================================================================== --- /dev/null +++ test/ubsan/TestCases/ImplicitConversion/signed-integer-truncation.c @@ -0,0 +1,318 @@ +// RUN: %clang -x c -fsanitize=implicit-signed-integer-truncation %s -DV0 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V0 +// RUN: %clang -x c -fsanitize=implicit-signed-integer-truncation %s -DV1 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V1 +// RUN: %clang -x c -fsanitize=implicit-signed-integer-truncation %s -DV2 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V2 +// RUN: %clang -x c -fsanitize=implicit-signed-integer-truncation %s -DV3 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V3 +// RUN: %clang -x c -fsanitize=implicit-signed-integer-truncation %s -DV4 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V4 +// RUN: %clang -x c -fsanitize=implicit-signed-integer-truncation %s -DV5 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V5 +// RUN: %clang -x c -fsanitize=implicit-signed-integer-truncation %s -DV6 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V6 +// RUN: %clang -x c -fsanitize=implicit-signed-integer-truncation %s -DV7 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V7 + +// RUN: %clang -x c++ -fsanitize=implicit-signed-integer-truncation %s -DV0 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V0 +// RUN: %clang -x c++ -fsanitize=implicit-signed-integer-truncation %s -DV1 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V1 +// RUN: %clang -x c++ -fsanitize=implicit-signed-integer-truncation %s -DV2 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V2 +// RUN: %clang -x c++ -fsanitize=implicit-signed-integer-truncation %s -DV3 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V3 +// RUN: %clang -x c++ -fsanitize=implicit-signed-integer-truncation %s -DV4 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V4 +// RUN: %clang -x c++ -fsanitize=implicit-signed-integer-truncation %s -DV5 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V5 +// RUN: %clang -x c++ -fsanitize=implicit-signed-integer-truncation %s -DV6 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V6 +// RUN: %clang -x c++ -fsanitize=implicit-signed-integer-truncation %s -DV7 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V7 + +#include + +// Test plan: +// * Two types - int and char +// * Two signs - signed and unsigned +// * Square that - we have input and output types. +// Thus, there are total of (2*2)^2 == 16 tests. +// These are all the possible variations/combinations of casts. +// However, not all of them should result in the check. +// So here, we *only* check which should and which should not result in checks. + +unsigned int convert_unsigned_int_to_unsigned_int(unsigned int x) { +#line 100 + return x; +} + +unsigned char convert_unsigned_char_to_unsigned_char(unsigned char x) { +#line 200 + return x; +} + +signed int convert_signed_int_to_signed_int(signed int x) { +#line 300 + return x; +} + +signed char convert_signed_char_to_signed_char(signed char x) { +#line 400 + return x; +} + +unsigned char convert_unsigned_int_to_unsigned_char(unsigned int x) { +#line 500 + return x; +} + +unsigned int convert_unsigned_char_to_unsigned_int(unsigned char x) { +#line 600 + return x; +} + +signed int convert_unsigned_char_to_signed_int(unsigned char x) { +#line 700 + return x; +} + +signed int convert_signed_char_to_signed_int(signed char x) { +#line 800 + return x; +} + +signed int convert_unsigned_int_to_signed_int(unsigned int x) { +#line 900 + return x; +} + +unsigned int convert_signed_int_to_unsigned_int(signed int x) { +#line 1000 + return x; +} + +unsigned char convert_signed_int_to_unsigned_char(signed int x) { +#line 1100 + return x; +} + +unsigned char convert_signed_char_to_unsigned_char(signed char x) { +#line 1200 + return x; +} + +signed char convert_unsigned_char_to_signed_char(unsigned char x) { +#line 1300 + return x; +} + +unsigned int convert_signed_char_to_unsigned_int(signed char x) { +#line 1400 + return x; +} + +signed char convert_unsigned_int_to_signed_char(unsigned int x) { +#line 1500 + return x; +} + +signed char convert_signed_int_to_signed_char(signed int x) { +#line 1600 + return x; +} + +#line 1111 // !!! + +int main() { + // No bits set. + convert_unsigned_int_to_unsigned_int(0); + convert_unsigned_char_to_unsigned_char(0); + convert_signed_int_to_signed_int(0); + convert_signed_char_to_signed_char(0); + convert_unsigned_int_to_unsigned_char(0); + convert_unsigned_char_to_unsigned_int(0); + convert_unsigned_char_to_signed_int(0); + convert_signed_char_to_signed_int(0); + convert_unsigned_int_to_signed_int(0); + convert_signed_int_to_unsigned_int(0); + convert_signed_int_to_unsigned_char(0); + convert_signed_char_to_unsigned_char(0); + convert_unsigned_char_to_signed_char(0); + convert_signed_char_to_unsigned_int(0); + convert_unsigned_int_to_signed_char(0); + convert_signed_int_to_signed_char(0); + + // One lowest bit set. + convert_unsigned_int_to_unsigned_int(1); + convert_unsigned_char_to_unsigned_char(1); + convert_signed_int_to_signed_int(1); + convert_signed_char_to_signed_char(1); + convert_unsigned_int_to_unsigned_char(1); + convert_unsigned_char_to_unsigned_int(1); + convert_unsigned_char_to_signed_int(1); + convert_signed_char_to_signed_int(1); + convert_unsigned_int_to_signed_int(1); + convert_signed_int_to_unsigned_int(1); + convert_signed_int_to_unsigned_char(1); + convert_signed_char_to_unsigned_char(1); + convert_unsigned_char_to_signed_char(1); + convert_signed_char_to_unsigned_int(1); + convert_unsigned_int_to_signed_char(1); + convert_signed_int_to_signed_char(1); + +#if defined(V0) + // All source bits set. + convert_unsigned_int_to_unsigned_int((uint32_t)UINT32_MAX); + convert_unsigned_char_to_unsigned_char((uint8_t)UINT8_MAX); + convert_signed_int_to_signed_int((int32_t)(uint32_t)UINT32_MAX); + convert_signed_char_to_signed_char((int8_t)UINT8_MAX); + convert_unsigned_int_to_unsigned_char((uint32_t)UINT32_MAX); + convert_unsigned_char_to_unsigned_int((uint8_t)UINT8_MAX); + convert_unsigned_char_to_signed_int((uint8_t)UINT8_MAX); + convert_signed_char_to_signed_int((int8_t)UINT8_MAX); + convert_unsigned_int_to_signed_int((uint32_t)UINT32_MAX); + convert_signed_int_to_unsigned_int((int32_t)(uint32_t)UINT32_MAX); + convert_signed_int_to_unsigned_char((int32_t)(uint32_t)UINT32_MAX); +// CHECK-V0: {{.*}}signed-integer-truncation.c:1100:10: runtime error: implicit conversion from type 'int' of value -1 (32-bit, signed) to type 'unsigned char' changed the value to 255 (8-bit, unsigned) + convert_signed_char_to_unsigned_char((int8_t)UINT8_MAX); + convert_unsigned_char_to_signed_char((uint8_t)UINT8_MAX); + convert_signed_char_to_unsigned_int((int8_t)UINT8_MAX); + convert_unsigned_int_to_signed_char((uint32_t)UINT32_MAX); + convert_signed_int_to_signed_char((int32_t)(uint32_t)UINT32_MAX); +#elif defined(V1) + // Source 'Sign' bit set. + convert_unsigned_int_to_unsigned_int((uint32_t)INT32_MIN); + convert_unsigned_char_to_unsigned_char((uint8_t)INT8_MIN); + convert_signed_int_to_signed_int((int32_t)(uint32_t)INT32_MIN); + convert_signed_char_to_signed_char((int8_t)INT8_MIN); + convert_unsigned_int_to_unsigned_char((uint32_t)INT32_MIN); + convert_unsigned_char_to_unsigned_int((uint8_t)INT8_MIN); + convert_unsigned_char_to_signed_int((uint8_t)INT8_MIN); + convert_signed_char_to_signed_int((int8_t)INT8_MIN); + convert_unsigned_int_to_signed_int((uint32_t)INT32_MIN); + convert_signed_int_to_unsigned_int((int32_t)(uint32_t)INT32_MIN); + convert_signed_int_to_unsigned_char((int32_t)(uint32_t)INT32_MIN); +// CHECK-V1: {{.*}}signed-integer-truncation.c:1100:10: runtime error: implicit conversion from type 'int' of value -2147483648 (32-bit, signed) to type 'unsigned char' changed the value to 0 (8-bit, unsigned) + convert_signed_char_to_unsigned_char((int8_t)INT8_MIN); + convert_unsigned_char_to_signed_char((uint8_t)INT8_MIN); + convert_signed_char_to_unsigned_int((int8_t)INT8_MIN); + convert_unsigned_int_to_signed_char((uint32_t)INT32_MIN); +// CHECK-V1: {{.*}}signed-integer-truncation.c:1500:10: runtime error: implicit conversion from type 'unsigned int' of value 2147483648 (32-bit, unsigned) to type 'signed char' changed the value to 0 (8-bit, signed) + convert_signed_int_to_signed_char((int32_t)(uint32_t)INT32_MIN); +// CHECK-V1: {{.*}}signed-integer-truncation.c:1600:10: runtime error: implicit conversion from type 'int' of value -2147483648 (32-bit, signed) to type 'signed char' changed the value to 0 (8-bit, signed) +#elif defined(V2) + // All bits except the source 'Sign' bit are set. + convert_unsigned_int_to_unsigned_int((uint32_t)INT32_MAX); + convert_unsigned_char_to_unsigned_char((uint8_t)INT8_MAX); + convert_signed_int_to_signed_int((int32_t)(uint32_t)INT32_MAX); + convert_signed_char_to_signed_char((int8_t)INT8_MAX); + convert_unsigned_int_to_unsigned_char((uint32_t)INT32_MAX); + convert_unsigned_char_to_unsigned_int((uint8_t)INT8_MAX); + convert_unsigned_char_to_signed_int((uint8_t)INT8_MAX); + convert_signed_char_to_signed_int((int8_t)INT8_MAX); + convert_unsigned_int_to_signed_int((uint32_t)INT32_MAX); + convert_signed_int_to_unsigned_int((int32_t)(uint32_t)INT32_MAX); + convert_signed_int_to_unsigned_char((int32_t)(uint32_t)INT32_MAX); +// CHECK-V2: {{.*}}signed-integer-truncation.c:1100:10: runtime error: implicit conversion from type 'int' of value 2147483647 (32-bit, signed) to type 'unsigned char' changed the value to 255 (8-bit, unsigned) + convert_signed_char_to_unsigned_char((int8_t)INT8_MAX); + convert_unsigned_char_to_signed_char((uint8_t)INT8_MAX); + convert_signed_char_to_unsigned_int((int8_t)INT8_MAX); + convert_unsigned_int_to_signed_char((uint32_t)INT32_MAX); +// CHECK-V2: {{.*}}signed-integer-truncation.c:1500:10: runtime error: implicit conversion from type 'unsigned int' of value 2147483647 (32-bit, unsigned) to type 'signed char' changed the value to -1 (8-bit, signed) + convert_signed_int_to_signed_char((int32_t)(uint32_t)INT32_MAX); +// CHECK-V2: {{.*}}signed-integer-truncation.c:1600:10: runtime error: implicit conversion from type 'int' of value 2147483647 (32-bit, signed) to type 'signed char' changed the value to -1 (8-bit, signed) +#elif defined(V3) + // All destination bits set. + convert_unsigned_int_to_unsigned_int((uint32_t)UINT8_MAX); + convert_unsigned_char_to_unsigned_char((uint8_t)UINT8_MAX); + convert_signed_int_to_signed_int((int32_t)(uint32_t)UINT8_MAX); + convert_signed_char_to_signed_char((int8_t)UINT8_MAX); + convert_unsigned_int_to_unsigned_char((uint32_t)UINT8_MAX); + convert_unsigned_char_to_unsigned_int((uint8_t)UINT8_MAX); + convert_unsigned_char_to_signed_int((uint8_t)UINT8_MAX); + convert_signed_char_to_signed_int((int8_t)UINT8_MAX); + convert_unsigned_int_to_signed_int((uint32_t)UINT8_MAX); + convert_signed_int_to_unsigned_int((int32_t)(uint32_t)UINT8_MAX); + convert_signed_int_to_unsigned_char((int32_t)(uint32_t)UINT8_MAX); + convert_signed_char_to_unsigned_char((int8_t)UINT8_MAX); + convert_unsigned_char_to_signed_char((uint8_t)UINT8_MAX); + convert_signed_char_to_unsigned_int((int8_t)UINT8_MAX); + convert_unsigned_int_to_signed_char((uint32_t)UINT8_MAX); +// CHECK-V3: {{.*}}signed-integer-truncation.c:1500:10: runtime error: implicit conversion from type 'unsigned int' of value 255 (32-bit, unsigned) to type 'signed char' changed the value to -1 (8-bit, signed) + convert_signed_int_to_signed_char((int32_t)(uint32_t)UINT8_MAX); +// CHECK-V3: {{.*}}signed-integer-truncation.c:1600:10: runtime error: implicit conversion from type 'int' of value 255 (32-bit, signed) to type 'signed char' changed the value to -1 (8-bit, signed) +#elif defined(V4) + // Destination 'sign' bit set. + convert_unsigned_int_to_unsigned_int((uint32_t)(uint8_t)INT8_MIN); + convert_unsigned_char_to_unsigned_char((uint8_t)(uint8_t)INT8_MIN); + convert_signed_int_to_signed_int((int32_t)(uint32_t)(uint8_t)INT8_MIN); + convert_signed_char_to_signed_char((int8_t)(uint8_t)INT8_MIN); + convert_unsigned_int_to_unsigned_char((uint32_t)(uint8_t)INT8_MIN); + convert_unsigned_char_to_unsigned_int((uint8_t)(uint8_t)INT8_MIN); + convert_unsigned_char_to_signed_int((uint8_t)(uint8_t)INT8_MIN); + convert_signed_char_to_signed_int((int8_t)(uint8_t)INT8_MIN); + convert_unsigned_int_to_signed_int((uint32_t)(uint8_t)INT8_MIN); + convert_signed_int_to_unsigned_int((int32_t)(uint32_t)(uint8_t)INT8_MIN); + convert_signed_int_to_unsigned_char((int32_t)(uint32_t)(uint8_t)INT8_MIN); + convert_signed_char_to_unsigned_char((int8_t)(uint8_t)INT8_MIN); + convert_unsigned_char_to_signed_char((uint8_t)(uint8_t)INT8_MIN); + convert_signed_char_to_unsigned_int((int8_t)(uint8_t)INT8_MIN); + convert_unsigned_int_to_signed_char((uint32_t)(uint8_t)INT8_MIN); +// CHECK-V4: {{.*}}signed-integer-truncation.c:1500:10: runtime error: implicit conversion from type 'unsigned int' of value 128 (32-bit, unsigned) to type 'signed char' changed the value to -128 (8-bit, signed) + convert_signed_int_to_signed_char((int32_t)(uint32_t)(uint8_t)INT8_MIN); +// CHECK-V4: {{.*}}signed-integer-truncation.c:1600:10: runtime error: implicit conversion from type 'int' of value 128 (32-bit, signed) to type 'signed char' changed the value to -128 (8-bit, signed) +#elif defined(V5) + // All bits except the destination 'sign' bit are set. + convert_unsigned_int_to_unsigned_int((~((uint32_t)(uint8_t)INT8_MIN))); + convert_unsigned_char_to_unsigned_char((uint8_t)(uint8_t)INT8_MIN); + convert_signed_int_to_signed_int((int32_t)(~((uint32_t)(uint8_t)INT8_MIN))); + convert_signed_char_to_signed_char((int8_t)(uint8_t)INT8_MIN); + convert_unsigned_int_to_unsigned_char((~((uint32_t)(uint8_t)INT8_MIN))); + convert_unsigned_char_to_unsigned_int((uint8_t)(uint8_t)INT8_MIN); + convert_unsigned_char_to_signed_int((uint8_t)(uint8_t)INT8_MIN); + convert_signed_char_to_signed_int((int8_t)(uint8_t)INT8_MIN); + convert_unsigned_int_to_signed_int((~((uint32_t)(uint8_t)INT8_MIN))); + convert_signed_int_to_unsigned_int((int32_t)(~((uint32_t)(uint8_t)INT8_MIN))); + convert_signed_int_to_unsigned_char((int32_t)(~((uint32_t)(uint8_t)INT8_MIN))); +// CHECK-V5: {{.*}}signed-integer-truncation.c:1100:10: runtime error: implicit conversion from type 'int' of value -129 (32-bit, signed) to type 'unsigned char' changed the value to 127 (8-bit, unsigned) + convert_signed_char_to_unsigned_char((int8_t)(uint8_t)INT8_MIN); + convert_unsigned_char_to_signed_char((uint8_t)(uint8_t)INT8_MIN); + convert_signed_char_to_unsigned_int((int8_t)(uint8_t)INT8_MIN); + convert_unsigned_int_to_signed_char((~((uint32_t)(uint8_t)INT8_MIN))); +// CHECK-V5: {{.*}}signed-integer-truncation.c:1500:10: runtime error: implicit conversion from type 'unsigned int' of value 4294967167 (32-bit, unsigned) to type 'signed char' changed the value to 127 (8-bit, signed) + convert_signed_int_to_signed_char((int32_t)(~((uint32_t)(uint8_t)INT8_MIN))); +// CHECK-V5: {{.*}}signed-integer-truncation.c:1600:10: runtime error: implicit conversion from type 'int' of value -129 (32-bit, signed) to type 'signed char' changed the value to 127 (8-bit, signed) +#elif defined(V6) + // Only the source and destination sign bits are set. + convert_unsigned_int_to_unsigned_int((uint32_t)INT32_MIN); + convert_unsigned_char_to_unsigned_char((uint8_t)INT8_MIN); + convert_signed_int_to_signed_int((int32_t)INT32_MIN); + convert_signed_char_to_signed_char((int8_t)INT8_MIN); + convert_unsigned_int_to_unsigned_char(((uint32_t)INT32_MIN) | ((uint32_t)(uint8_t)INT8_MIN)); + convert_unsigned_char_to_unsigned_int((uint8_t)INT8_MIN); + convert_unsigned_char_to_signed_int((uint8_t)INT8_MIN); + convert_signed_char_to_signed_int((int8_t)INT8_MIN); + convert_unsigned_int_to_signed_int((uint32_t)INT32_MIN); + convert_signed_int_to_unsigned_int((uint32_t)INT32_MIN); + convert_signed_int_to_unsigned_char((int32_t)(((uint32_t)INT32_MIN) | ((uint32_t)(uint8_t)INT8_MIN))); +// CHECK-V6: {{.*}}signed-integer-truncation.c:1100:10: runtime error: implicit conversion from type 'int' of value -2147483520 (32-bit, signed) to type 'unsigned char' changed the value to 128 (8-bit, unsigned) + convert_signed_char_to_unsigned_char((int8_t)INT8_MIN); + convert_unsigned_char_to_signed_char((uint8_t)INT8_MIN); + convert_signed_char_to_unsigned_int((int8_t)INT8_MIN); + convert_unsigned_int_to_signed_char((((uint32_t)INT32_MIN) | ((uint32_t)(uint8_t)INT8_MIN))); +// CHECK-V6: {{.*}}signed-integer-truncation.c:1500:10: runtime error: implicit conversion from type 'unsigned int' of value 2147483776 (32-bit, unsigned) to type 'signed char' changed the value to -128 (8-bit, signed) + convert_signed_int_to_signed_char((int32_t)(((uint32_t)INT32_MIN) | ((uint32_t)(uint8_t)INT8_MIN))); +// CHECK-V6: {{.*}}signed-integer-truncation.c:1600:10: runtime error: implicit conversion from type 'int' of value -2147483520 (32-bit, signed) to type 'signed char' changed the value to -128 (8-bit, signed) +#elif defined(V7) + // All bits except the source and destination sign bits are set. + convert_unsigned_int_to_unsigned_int((uint32_t)INT32_MAX); + convert_unsigned_char_to_unsigned_char((uint8_t)INT8_MAX); + convert_signed_int_to_signed_int((int32_t)INT32_MAX); + convert_signed_char_to_signed_char((int8_t)INT8_MAX); + convert_unsigned_int_to_unsigned_char(~(((uint32_t)INT32_MIN) | ((uint32_t)(uint8_t)INT8_MIN))); + convert_unsigned_char_to_unsigned_int((uint8_t)INT8_MAX); + convert_unsigned_char_to_signed_int((uint8_t)INT8_MAX); + convert_signed_char_to_signed_int((int8_t)INT8_MAX); + convert_unsigned_int_to_signed_int((uint32_t)INT32_MAX); + convert_signed_int_to_unsigned_int((uint32_t)INT32_MAX); + convert_signed_int_to_unsigned_char((int32_t)(~(((uint32_t)INT32_MIN) | ((uint32_t)(uint8_t)INT8_MIN)))); +// CHECK-V7: {{.*}}signed-integer-truncation.c:1100:10: runtime error: implicit conversion from type 'int' of value 2147483519 (32-bit, signed) to type 'unsigned char' changed the value to 127 (8-bit, unsigned) + convert_signed_char_to_unsigned_char((int8_t)INT8_MAX); + convert_unsigned_char_to_signed_char((uint8_t)INT8_MAX); + convert_signed_char_to_unsigned_int((int8_t)INT8_MAX); + convert_unsigned_int_to_signed_char(~(((uint32_t)INT32_MIN) | ((uint32_t)(uint8_t)INT8_MIN))); +// CHECK-V7: {{.*}}signed-integer-truncation.c:1500:10: runtime error: implicit conversion from type 'unsigned int' of value 2147483519 (32-bit, unsigned) to type 'signed char' changed the value to 127 (8-bit, signed) + convert_signed_int_to_signed_char((int32_t)~(((uint32_t)INT32_MIN) | ((uint32_t)(uint8_t)INT8_MIN))); +// CHECK-V7: {{.*}}signed-integer-truncation.c:1600:10: runtime error: implicit conversion from type 'int' of value 2147483519 (32-bit, signed) to type 'signed char' changed the value to 127 (8-bit, signed) +#else +#error Some V* needs to be defined! +#endif + + return 0; +} Index: test/ubsan/TestCases/ImplicitConversion/unsigned-integer-truncation-blacklist.c =================================================================== --- /dev/null +++ test/ubsan/TestCases/ImplicitConversion/unsigned-integer-truncation-blacklist.c @@ -0,0 +1,58 @@ +// FIXME: https://code.google.com/p/address-sanitizer/issues/detail?id=316 +// I'm not sure this is actually *that* issue, but this seems oddly similar to the other XFAIL'ed cases. +// XFAIL: android +// UNSUPPORTED: ios + +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" + +// RUN: rm -f %tmp +// RUN: echo "[integer]" >> %tmp +// RUN: echo "fun:implicitUnsignedTruncation" >> %tmp +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O0 %s -o %t && not %run %t 2>&1 | not FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O1 %s -o %t && not %run %t 2>&1 | not FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O2 %s -o %t && not %run %t 2>&1 | not FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O3 %s -o %t && not %run %t 2>&1 | not FileCheck %s + +// RUN: rm -f %tmp +// RUN: echo "[implicit-conversion]" >> %tmp +// RUN: echo "fun:implicitUnsignedTruncation" >> %tmp +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O0 %s -o %t && not %run %t 2>&1 | not FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O1 %s -o %t && not %run %t 2>&1 | not FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O2 %s -o %t && not %run %t 2>&1 | not FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O3 %s -o %t && not %run %t 2>&1 | not FileCheck %s + +// RUN: rm -f %tmp +// RUN: echo "[implicit-integer-truncation]" >> %tmp +// RUN: echo "fun:implicitUnsignedTruncation" >> %tmp +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O0 %s -o %t && not %run %t 2>&1 | not FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O1 %s -o %t && not %run %t 2>&1 | not FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O2 %s -o %t && not %run %t 2>&1 | not FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O3 %s -o %t && not %run %t 2>&1 | not FileCheck %s + +// RUN: rm -f %tmp +// RUN: echo "[implicit-unsigned-integer-truncation]" >> %tmp +// RUN: echo "fun:implicitUnsignedTruncation" >> %tmp +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O0 %s -o %t && not %run %t 2>&1 | not FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O1 %s -o %t && not %run %t 2>&1 | not FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O2 %s -o %t && not %run %t 2>&1 | not FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O3 %s -o %t && not %run %t 2>&1 | not FileCheck %s + +// RUN: rm -f %tmp +// RUN: echo "[implicit-signed-integer-truncation]" >> %tmp +// RUN: echo "fun:implicitUnsignedTruncation" >> %tmp +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O0 %s -o %t && not %run %t 2>&1 | FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s +// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O3 %s -o %t && not %run %t 2>&1 | FileCheck %s + +unsigned char implicitUnsignedTruncation(unsigned int argc) { + return argc; // BOOM +// CHECK: {{.*}}unsigned-integer-truncation-blacklist.c:[[@LINE-1]]:10: runtime error: implicit conversion from type 'unsigned int' of value 4294967295 (32-bit, unsigned) to type 'unsigned char' changed the value to 255 (8-bit, unsigned) +} + +int main(int argc, char **argv) { + return implicitUnsignedTruncation(~0U); +} Index: test/ubsan/TestCases/ImplicitConversion/unsigned-integer-truncation-summary.cpp =================================================================== --- test/ubsan/TestCases/ImplicitConversion/unsigned-integer-truncation-summary.cpp +++ test/ubsan/TestCases/ImplicitConversion/unsigned-integer-truncation-summary.cpp @@ -1,4 +1,4 @@ -// RUN: %clangxx -fsanitize=implicit-integer-truncation %s -o %t +// RUN: %clangxx -fsanitize=implicit-unsigned-integer-truncation %s -o %t // RUN: %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-NOTYPE // RUN: %env_ubsan_opts=report_error_type=1 %run %t 2>&1 | FileCheck %s --check-prefix=CHECK-TYPE // REQUIRES: !ubsan-standalone && !ubsan-standalone-static @@ -8,6 +8,6 @@ int main() { uint8_t t0 = (~(uint32_t(0))); // CHECK-NOTYPE: SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior {{.*}}summary.cpp:[[@LINE-1]]:16 - // CHECK-TYPE: SUMMARY: UndefinedBehaviorSanitizer: implicit-integer-truncation {{.*}}summary.cpp:[[@LINE-2]]:16 + // CHECK-TYPE: SUMMARY: UndefinedBehaviorSanitizer: implicit-unsigned-integer-truncation {{.*}}summary.cpp:[[@LINE-2]]:16 return 0; } Index: test/ubsan/TestCases/ImplicitConversion/unsigned-integer-truncation.c =================================================================== --- /dev/null +++ test/ubsan/TestCases/ImplicitConversion/unsigned-integer-truncation.c @@ -0,0 +1,304 @@ +// RUN: %clang -x c -fsanitize=implicit-unsigned-integer-truncation %s -DV0 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V0 +// RUN: %clang -x c -fsanitize=implicit-unsigned-integer-truncation %s -DV1 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V1 +// RUN: %clang -x c -fsanitize=implicit-unsigned-integer-truncation %s -DV2 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V2 +// RUN: %clang -x c -fsanitize=implicit-unsigned-integer-truncation %s -DV3 -o %t && %run %t 2>&1 | not FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V3 +// RUN: %clang -x c -fsanitize=implicit-unsigned-integer-truncation %s -DV4 -o %t && %run %t 2>&1 | not FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V4 +// RUN: %clang -x c -fsanitize=implicit-unsigned-integer-truncation %s -DV5 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V5 +// RUN: %clang -x c -fsanitize=implicit-unsigned-integer-truncation %s -DV6 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V6 +// RUN: %clang -x c -fsanitize=implicit-unsigned-integer-truncation %s -DV7 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V7 + +// RUN: %clang -x c++ -fsanitize=implicit-unsigned-integer-truncation %s -DV0 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V0 +// RUN: %clang -x c++ -fsanitize=implicit-unsigned-integer-truncation %s -DV1 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V1 +// RUN: %clang -x c++ -fsanitize=implicit-unsigned-integer-truncation %s -DV2 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V2 +// RUN: %clang -x c++ -fsanitize=implicit-unsigned-integer-truncation %s -DV3 -o %t && %run %t 2>&1 | not FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V3 +// RUN: %clang -x c++ -fsanitize=implicit-unsigned-integer-truncation %s -DV4 -o %t && %run %t 2>&1 | not FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V4 +// RUN: %clang -x c++ -fsanitize=implicit-unsigned-integer-truncation %s -DV5 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V5 +// RUN: %clang -x c++ -fsanitize=implicit-unsigned-integer-truncation %s -DV6 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V6 +// RUN: %clang -x c++ -fsanitize=implicit-unsigned-integer-truncation %s -DV7 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V7 + +#include + +// Test plan: +// * Two types - int and char +// * Two signs - signed and unsigned +// * Square that - we have input and output types. +// Thus, there are total of (2*2)^2 == 16 tests. +// These are all the possible variations/combinations of casts. +// However, not all of them should result in the check. +// So here, we *only* check which should and which should not result in checks. + +unsigned int convert_unsigned_int_to_unsigned_int(unsigned int x) { +#line 100 + return x; +} + +unsigned char convert_unsigned_char_to_unsigned_char(unsigned char x) { +#line 200 + return x; +} + +signed int convert_signed_int_to_signed_int(signed int x) { +#line 300 + return x; +} + +signed char convert_signed_char_to_signed_char(signed char x) { +#line 400 + return x; +} + +unsigned char convert_unsigned_int_to_unsigned_char(unsigned int x) { +#line 500 + return x; +} + +unsigned int convert_unsigned_char_to_unsigned_int(unsigned char x) { +#line 600 + return x; +} + +signed int convert_unsigned_char_to_signed_int(unsigned char x) { +#line 700 + return x; +} + +signed int convert_signed_char_to_signed_int(signed char x) { +#line 800 + return x; +} + +signed int convert_unsigned_int_to_signed_int(unsigned int x) { +#line 900 + return x; +} + +unsigned int convert_signed_int_to_unsigned_int(signed int x) { +#line 1000 + return x; +} + +unsigned char convert_signed_int_to_unsigned_char(signed int x) { +#line 1100 + return x; +} + +unsigned char convert_signed_char_to_unsigned_char(signed char x) { +#line 1200 + return x; +} + +signed char convert_unsigned_char_to_signed_char(unsigned char x) { +#line 1300 + return x; +} + +unsigned int convert_signed_char_to_unsigned_int(signed char x) { +#line 1400 + return x; +} + +signed char convert_unsigned_int_to_signed_char(unsigned int x) { +#line 1500 + return x; +} + +signed char convert_signed_int_to_signed_char(signed int x) { +#line 1600 + return x; +} + +#line 1111 // !!! + +int main() { + // No bits set. + convert_unsigned_int_to_unsigned_int(0); + convert_unsigned_char_to_unsigned_char(0); + convert_signed_int_to_signed_int(0); + convert_signed_char_to_signed_char(0); + convert_unsigned_int_to_unsigned_char(0); + convert_unsigned_char_to_unsigned_int(0); + convert_unsigned_char_to_signed_int(0); + convert_signed_char_to_signed_int(0); + convert_unsigned_int_to_signed_int(0); + convert_signed_int_to_unsigned_int(0); + convert_signed_int_to_unsigned_char(0); + convert_signed_char_to_unsigned_char(0); + convert_unsigned_char_to_signed_char(0); + convert_signed_char_to_unsigned_int(0); + convert_unsigned_int_to_signed_char(0); + convert_signed_int_to_signed_char(0); + + // One lowest bit set. + convert_unsigned_int_to_unsigned_int(1); + convert_unsigned_char_to_unsigned_char(1); + convert_signed_int_to_signed_int(1); + convert_signed_char_to_signed_char(1); + convert_unsigned_int_to_unsigned_char(1); + convert_unsigned_char_to_unsigned_int(1); + convert_unsigned_char_to_signed_int(1); + convert_signed_char_to_signed_int(1); + convert_unsigned_int_to_signed_int(1); + convert_signed_int_to_unsigned_int(1); + convert_signed_int_to_unsigned_char(1); + convert_signed_char_to_unsigned_char(1); + convert_unsigned_char_to_signed_char(1); + convert_signed_char_to_unsigned_int(1); + convert_unsigned_int_to_signed_char(1); + convert_signed_int_to_signed_char(1); + +#if defined(V0) + // All source bits set. + convert_unsigned_int_to_unsigned_int((uint32_t)UINT32_MAX); + convert_unsigned_char_to_unsigned_char((uint8_t)UINT8_MAX); + convert_signed_int_to_signed_int((int32_t)(uint32_t)UINT32_MAX); + convert_signed_char_to_signed_char((int8_t)UINT8_MAX); + convert_unsigned_int_to_unsigned_char((uint32_t)UINT32_MAX); +// CHECK-V0: {{.*}}unsigned-integer-truncation.c:500:10: runtime error: implicit conversion from type 'unsigned int' of value 4294967295 (32-bit, unsigned) to type 'unsigned char' changed the value to 255 (8-bit, unsigned + convert_unsigned_char_to_unsigned_int((uint8_t)UINT8_MAX); + convert_unsigned_char_to_signed_int((uint8_t)UINT8_MAX); + convert_signed_char_to_signed_int((int8_t)UINT8_MAX); + convert_unsigned_int_to_signed_int((uint32_t)UINT32_MAX); + convert_signed_int_to_unsigned_int((int32_t)(uint32_t)UINT32_MAX); + convert_signed_int_to_unsigned_char((int32_t)(uint32_t)UINT32_MAX); + convert_signed_char_to_unsigned_char((int8_t)UINT8_MAX); + convert_unsigned_char_to_signed_char((uint8_t)UINT8_MAX); + convert_signed_char_to_unsigned_int((int8_t)UINT8_MAX); + convert_unsigned_int_to_signed_char((uint32_t)UINT32_MAX); + convert_signed_int_to_signed_char((int32_t)(uint32_t)UINT32_MAX); +#elif defined(V1) + // Source 'Sign' bit set. + convert_unsigned_int_to_unsigned_int((uint32_t)INT32_MIN); + convert_unsigned_char_to_unsigned_char((uint8_t)INT8_MIN); + convert_signed_int_to_signed_int((int32_t)(uint32_t)INT32_MIN); + convert_signed_char_to_signed_char((int8_t)INT8_MIN); + convert_unsigned_int_to_unsigned_char((uint32_t)INT32_MIN); +// CHECK-V1: {{.*}}unsigned-integer-truncation.c:500:10: runtime error: implicit conversion from type 'unsigned int' of value 2147483648 (32-bit, unsigned) to type 'unsigned char' changed the value to 0 (8-bit, unsigned) + convert_unsigned_char_to_unsigned_int((uint8_t)INT8_MIN); + convert_unsigned_char_to_signed_int((uint8_t)INT8_MIN); + convert_signed_char_to_signed_int((int8_t)INT8_MIN); + convert_unsigned_int_to_signed_int((uint32_t)INT32_MIN); + convert_signed_int_to_unsigned_int((int32_t)(uint32_t)INT32_MIN); + convert_signed_int_to_unsigned_char((int32_t)(uint32_t)INT32_MIN); + convert_signed_char_to_unsigned_char((int8_t)INT8_MIN); + convert_unsigned_char_to_signed_char((uint8_t)INT8_MIN); + convert_signed_char_to_unsigned_int((int8_t)INT8_MIN); + convert_unsigned_int_to_signed_char((uint32_t)INT32_MIN); + convert_signed_int_to_signed_char((int32_t)(uint32_t)INT32_MIN); +#elif defined(V2) + // All bits except the source 'Sign' bit are set. + convert_unsigned_int_to_unsigned_int((uint32_t)INT32_MAX); + convert_unsigned_char_to_unsigned_char((uint8_t)INT8_MAX); + convert_signed_int_to_signed_int((int32_t)(uint32_t)INT32_MAX); + convert_signed_char_to_signed_char((int8_t)INT8_MAX); + convert_unsigned_int_to_unsigned_char((uint32_t)INT32_MAX); +// CHECK-V2: {{.*}}unsigned-integer-truncation.c:500:10: runtime error: implicit conversion from type 'unsigned int' of value 2147483647 (32-bit, unsigned) to type 'unsigned char' changed the value to 255 (8-bit, unsigned) + convert_unsigned_char_to_unsigned_int((uint8_t)INT8_MAX); + convert_unsigned_char_to_signed_int((uint8_t)INT8_MAX); + convert_signed_char_to_signed_int((int8_t)INT8_MAX); + convert_unsigned_int_to_signed_int((uint32_t)INT32_MAX); + convert_signed_int_to_unsigned_int((int32_t)(uint32_t)INT32_MAX); + convert_signed_int_to_unsigned_char((int32_t)(uint32_t)INT32_MAX); + convert_signed_char_to_unsigned_char((int8_t)INT8_MAX); + convert_unsigned_char_to_signed_char((uint8_t)INT8_MAX); + convert_signed_char_to_unsigned_int((int8_t)INT8_MAX); + convert_unsigned_int_to_signed_char((uint32_t)INT32_MAX); + convert_signed_int_to_signed_char((int32_t)(uint32_t)INT32_MAX); +#elif defined(V3) + // All destination bits set. + convert_unsigned_int_to_unsigned_int((uint32_t)UINT8_MAX); + convert_unsigned_char_to_unsigned_char((uint8_t)UINT8_MAX); + convert_signed_int_to_signed_int((int32_t)(uint32_t)UINT8_MAX); + convert_signed_char_to_signed_char((int8_t)UINT8_MAX); + convert_unsigned_int_to_unsigned_char((uint32_t)UINT8_MAX); + convert_unsigned_char_to_unsigned_int((uint8_t)UINT8_MAX); + convert_unsigned_char_to_signed_int((uint8_t)UINT8_MAX); + convert_signed_char_to_signed_int((int8_t)UINT8_MAX); + convert_unsigned_int_to_signed_int((uint32_t)UINT8_MAX); + convert_signed_int_to_unsigned_int((int32_t)(uint32_t)UINT8_MAX); + convert_signed_int_to_unsigned_char((int32_t)(uint32_t)UINT8_MAX); + convert_signed_char_to_unsigned_char((int8_t)UINT8_MAX); + convert_unsigned_char_to_signed_char((uint8_t)UINT8_MAX); + convert_signed_char_to_unsigned_int((int8_t)UINT8_MAX); + convert_unsigned_int_to_signed_char((uint32_t)UINT8_MAX); + convert_signed_int_to_signed_char((int32_t)(uint32_t)UINT8_MAX); +#elif defined(V4) + // Destination 'sign' bit set. + convert_unsigned_int_to_unsigned_int((uint32_t)(uint8_t)INT8_MIN); + convert_unsigned_char_to_unsigned_char((uint8_t)(uint8_t)INT8_MIN); + convert_signed_int_to_signed_int((int32_t)(uint32_t)(uint8_t)INT8_MIN); + convert_signed_char_to_signed_char((int8_t)(uint8_t)INT8_MIN); + convert_unsigned_int_to_unsigned_char((uint32_t)(uint8_t)INT8_MIN); + convert_unsigned_char_to_unsigned_int((uint8_t)(uint8_t)INT8_MIN); + convert_unsigned_char_to_signed_int((uint8_t)(uint8_t)INT8_MIN); + convert_signed_char_to_signed_int((int8_t)(uint8_t)INT8_MIN); + convert_unsigned_int_to_signed_int((uint32_t)(uint8_t)INT8_MIN); + convert_signed_int_to_unsigned_int((int32_t)(uint32_t)(uint8_t)INT8_MIN); + convert_signed_int_to_unsigned_char((int32_t)(uint32_t)(uint8_t)INT8_MIN); + convert_signed_char_to_unsigned_char((int8_t)(uint8_t)INT8_MIN); + convert_unsigned_char_to_signed_char((uint8_t)(uint8_t)INT8_MIN); + convert_signed_char_to_unsigned_int((int8_t)(uint8_t)INT8_MIN); + convert_unsigned_int_to_signed_char((uint32_t)(uint8_t)INT8_MIN); + convert_signed_int_to_signed_char((int32_t)(uint32_t)(uint8_t)INT8_MIN); +#elif defined(V5) + // All bits except the destination 'sign' bit are set. + convert_unsigned_int_to_unsigned_int((~((uint32_t)(uint8_t)INT8_MIN))); + convert_unsigned_char_to_unsigned_char((uint8_t)(uint8_t)INT8_MIN); + convert_signed_int_to_signed_int((int32_t)(~((uint32_t)(uint8_t)INT8_MIN))); + convert_signed_char_to_signed_char((int8_t)(uint8_t)INT8_MIN); + convert_unsigned_int_to_unsigned_char((~((uint32_t)(uint8_t)INT8_MIN))); +// CHECK-V5: {{.*}}unsigned-integer-truncation.c:500:10: runtime error: implicit conversion from type 'unsigned int' of value 4294967167 (32-bit, unsigned) to type 'unsigned char' changed the value to 127 (8-bit, unsigned) + convert_unsigned_char_to_unsigned_int((uint8_t)(uint8_t)INT8_MIN); + convert_unsigned_char_to_signed_int((uint8_t)(uint8_t)INT8_MIN); + convert_signed_char_to_signed_int((int8_t)(uint8_t)INT8_MIN); + convert_unsigned_int_to_signed_int((~((uint32_t)(uint8_t)INT8_MIN))); + convert_signed_int_to_unsigned_int((int32_t)(~((uint32_t)(uint8_t)INT8_MIN))); + convert_signed_int_to_unsigned_char((int32_t)(~((uint32_t)(uint8_t)INT8_MIN))); + convert_signed_char_to_unsigned_char((int8_t)(uint8_t)INT8_MIN); + convert_unsigned_char_to_signed_char((uint8_t)(uint8_t)INT8_MIN); + convert_signed_char_to_unsigned_int((int8_t)(uint8_t)INT8_MIN); + convert_unsigned_int_to_signed_char((~((uint32_t)(uint8_t)INT8_MIN))); + convert_signed_int_to_signed_char((int32_t)(~((uint32_t)(uint8_t)INT8_MIN))); +#elif defined(V6) + // Only the source and destination sign bits are set. + convert_unsigned_int_to_unsigned_int((uint32_t)INT32_MIN); + convert_unsigned_char_to_unsigned_char((uint8_t)INT8_MIN); + convert_signed_int_to_signed_int((int32_t)INT32_MIN); + convert_signed_char_to_signed_char((int8_t)INT8_MIN); + convert_unsigned_int_to_unsigned_char(((uint32_t)INT32_MIN) | ((uint32_t)(uint8_t)INT8_MIN)); +// CHECK-V6: {{.*}}unsigned-integer-truncation.c:500:10: runtime error: implicit conversion from type 'unsigned int' of value 2147483776 (32-bit, unsigned) to type 'unsigned char' changed the value to 128 (8-bit, unsigned) + convert_unsigned_char_to_unsigned_int((uint8_t)INT8_MIN); + convert_unsigned_char_to_signed_int((uint8_t)INT8_MIN); + convert_signed_char_to_signed_int((int8_t)INT8_MIN); + convert_unsigned_int_to_signed_int((uint32_t)INT32_MIN); + convert_signed_int_to_unsigned_int((uint32_t)INT32_MIN); + convert_signed_int_to_unsigned_char((int32_t)(((uint32_t)INT32_MIN) | ((uint32_t)(uint8_t)INT8_MIN))); + convert_signed_char_to_unsigned_char((int8_t)INT8_MIN); + convert_unsigned_char_to_signed_char((uint8_t)INT8_MIN); + convert_signed_char_to_unsigned_int((int8_t)INT8_MIN); + convert_unsigned_int_to_signed_char((((uint32_t)INT32_MIN) | ((uint32_t)(uint8_t)INT8_MIN))); + convert_signed_int_to_signed_char((int32_t)(((uint32_t)INT32_MIN) | ((uint32_t)(uint8_t)INT8_MIN))); +#elif defined(V7) + // All bits except the source and destination sign bits are set. + convert_unsigned_int_to_unsigned_int((uint32_t)INT32_MAX); + convert_unsigned_char_to_unsigned_char((uint8_t)INT8_MAX); + convert_signed_int_to_signed_int((int32_t)INT32_MAX); + convert_signed_char_to_signed_char((int8_t)INT8_MAX); + convert_unsigned_int_to_unsigned_char(~(((uint32_t)INT32_MIN) | ((uint32_t)(uint8_t)INT8_MIN))); +// CHECK-V7: {{.*}}unsigned-integer-truncation.c:500:10: runtime error: implicit conversion from type 'unsigned int' of value 2147483519 (32-bit, unsigned) to type 'unsigned char' changed the value to 127 (8-bit, unsigned) + convert_unsigned_char_to_unsigned_int((uint8_t)INT8_MAX); + convert_unsigned_char_to_signed_int((uint8_t)INT8_MAX); + convert_signed_char_to_signed_int((int8_t)INT8_MAX); + convert_unsigned_int_to_signed_int((uint32_t)INT32_MAX); + convert_signed_int_to_unsigned_int((uint32_t)INT32_MAX); + convert_signed_int_to_unsigned_char((int32_t)(~(((uint32_t)INT32_MIN) | ((uint32_t)(uint8_t)INT8_MIN)))); + convert_signed_char_to_unsigned_char((int8_t)INT8_MAX); + convert_unsigned_char_to_signed_char((uint8_t)INT8_MAX); + convert_signed_char_to_unsigned_int((int8_t)INT8_MAX); + convert_unsigned_int_to_signed_char(~(((uint32_t)INT32_MIN) | ((uint32_t)(uint8_t)INT8_MIN))); + convert_signed_int_to_signed_char((int32_t)~(((uint32_t)INT32_MIN) | ((uint32_t)(uint8_t)INT8_MIN))); +#else +#error Some V* needs to be defined! +#endif + + return 0; +} Index: test/ubsan_minimal/TestCases/implicit-signed-integer-truncation.c =================================================================== --- /dev/null +++ test/ubsan_minimal/TestCases/implicit-signed-integer-truncation.c @@ -0,0 +1,25 @@ +// RUN: %clang -fsanitize=implicit-signed-integer-truncation %s -o %t && %run %t 2>&1 | FileCheck %s --check-prefixes=CHECK + +#include + +int main() { +// CHECK-NOT: implicit-conversion + + // Negative tests. Even if they produce unexpected results, this sanitizer does not care. + int8_t n0 = (~((uint32_t)(0))); // ~0 -> -1, but do not warn. + uint8_t n2 = 128; + uint8_t n3 = 255; + // Bools do not count + _Bool b0 = (~((uint32_t)(0))); + _Bool b1 = 255; + + // Explicit and-ing of bits will silence it. + uint8_t nc0 = ((int32_t)(-1)) & 255; + + // Positive tests. + uint8_t t0 = (int32_t)(-1); +// CHECK: implicit-conversion +// CHECK-NOT: implicit-conversion + + return 0; +} Index: test/ubsan_minimal/TestCases/implicit-unsigned-integer-truncation.c =================================================================== --- test/ubsan_minimal/TestCases/implicit-unsigned-integer-truncation.c +++ test/ubsan_minimal/TestCases/implicit-unsigned-integer-truncation.c @@ -1,4 +1,4 @@ -// RUN: %clang -fsanitize=implicit-integer-truncation %s -o %t && %run %t 2>&1 | FileCheck %s --check-prefixes=CHECK +// RUN: %clang -fsanitize=implicit-unsigned-integer-truncation %s -o %t && %run %t 2>&1 | FileCheck %s --check-prefixes=CHECK #include