diff options
4 files changed, 84 insertions, 7 deletions
diff --git a/compiler-rt/test/fuzzer/fuzzer-implicit-integer-truncation.test b/compiler-rt/test/fuzzer/fuzzer-implicit-integer-truncation.test index 212559bdca3..c968ea7d654 100644 --- a/compiler-rt/test/fuzzer/fuzzer-implicit-integer-truncation.test +++ b/compiler-rt/test/fuzzer/fuzzer-implicit-integer-truncation.test @@ -1,5 +1,5 @@ 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: 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: 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- diff --git a/compiler-rt/test/ubsan/TestCases/ImplicitConversion/integer-conversion.c b/compiler-rt/test/ubsan/TestCases/ImplicitConversion/integer-conversion.c new file mode 100644 index 00000000000..e3260a95f8f --- /dev/null +++ b/compiler-rt/test/ubsan/TestCases/ImplicitConversion/integer-conversion.c @@ -0,0 +1,70 @@ +// RUN: %clang -x c -fsanitize=implicit-integer-truncation %s -DV0 -o %t && %run %t 2>&1 | not FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V0 +// RUN: %clang -x c -fsanitize=implicit-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-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-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-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-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-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-integer-truncation %s -DV0 -o %t && %run %t 2>&1 | not FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V0 +// RUN: %clang -x c++ -fsanitize=implicit-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-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-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-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-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-integer-truncation %s -DV6 -o %t && %run %t 2>&1 | FileCheck %s --implicit-check-not="implicit conversion" --check-prefixes=CHECK-V6 + +#include <stdint.h> + +int8_t positive6_convert_unsigned_int_to_signed_char(uint32_t x) { +#line 100 + return x; +} + +#line 1120 // !!! + +void test_positives() { + // No bits set. + positive6_convert_unsigned_int_to_signed_char(0); + + // One lowest bit set. + positive6_convert_unsigned_int_to_signed_char(1); + +#if defined(V0) + // All source bits set. + positive6_convert_unsigned_int_to_signed_char((uint32_t)UINT32_MAX); +#elif defined(V1) + // Source 'Sign' bit set. + positive6_convert_unsigned_int_to_signed_char((uint32_t)INT32_MIN); +// CHECK-V1: {{.*}}integer-conversion.c:100:10: runtime error: implicit conversion from type 'uint32_t' (aka 'unsigned int') of value 2147483648 (32-bit, unsigned) to type 'int8_t' (aka 'signed char') changed the value to 0 (8-bit, signed) +#elif defined(V2) + // All bits except the source 'Sign' bit are set. + positive6_convert_unsigned_int_to_signed_char((uint32_t)INT32_MAX); +// CHECK-V2: {{.*}}integer-conversion.c:100:10: runtime error: implicit conversion from type 'uint32_t' (aka 'unsigned int') of value 2147483647 (32-bit, unsigned) to type 'int8_t' (aka 'signed char') changed the value to -1 (8-bit, signed) +#elif defined(V3) + // All destination bits set. + positive6_convert_unsigned_int_to_signed_char((uint32_t)UINT8_MAX); +// CHECK-V3: {{.*}}integer-conversion.c:100:10: runtime error: implicit conversion from type 'uint32_t' (aka 'unsigned int') of value 255 (32-bit, unsigned) to type 'int8_t' (aka 'signed char') changed the value to -1 (8-bit, signed) +#elif defined(V4) + // Destination 'sign' bit set. + positive6_convert_unsigned_int_to_signed_char((uint32_t)INT8_MIN); +#elif defined(V5) + // All bits except the destination 'sign' bit are set. + positive6_convert_unsigned_int_to_signed_char(~((uint32_t)(uint8_t)INT8_MIN)); +// CHECK-V5: {{.*}}integer-conversion.c:100:10: runtime error: implicit conversion from type 'uint32_t' (aka 'unsigned int') of value 4294967167 (32-bit, unsigned) to type 'int8_t' (aka 'signed char') changed the value to 127 (8-bit, signed) +#elif defined(V6) + // Only the source and destination sign bits are set. + positive6_convert_unsigned_int_to_signed_char((uint32_t)((uint32_t)INT32_MIN | (uint32_t)((uint8_t)INT8_MIN))); +// CHECK-V6: {{.*}}integer-conversion.c:100:10: runtime error: implicit conversion from type 'uint32_t' (aka 'unsigned int') of value 2147483776 (32-bit, unsigned) to type 'int8_t' (aka 'signed char') changed the value to -128 (8-bit, signed) +#else +#error Some V* needs to be defined! +#endif +} + +// CHECK-NOT: implicit conversion + +int main() { + test_positives(); + + return 0; +} diff --git a/compiler-rt/test/ubsan/TestCases/ImplicitConversion/integer-truncation-blacklist.c b/compiler-rt/test/ubsan/TestCases/ImplicitConversion/integer-truncation-blacklist.c index 13d4dca4ff1..a3650e0a925 100644 --- a/compiler-rt/test/ubsan/TestCases/ImplicitConversion/integer-truncation-blacklist.c +++ b/compiler-rt/test/ubsan/TestCases/ImplicitConversion/integer-truncation-blacklist.c @@ -3,16 +3,22 @@ // 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 -// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O1 %s -o %t && not %run %t 2>&1 -// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O2 %s -o %t && not %run %t 2>&1 -// RUN: %clang -fsanitize=implicit-integer-truncation -fno-sanitize-recover=implicit-integer-truncation -fsanitize-blacklist=%tmp -O3 %s -o %t && not %run %t 2>&1 +// 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) { diff --git a/compiler-rt/test/ubsan_minimal/TestCases/implicit-integer-truncation.c b/compiler-rt/test/ubsan_minimal/TestCases/implicit-integer-truncation.c index 1db6e697681..28711d2eeda 100644 --- a/compiler-rt/test/ubsan_minimal/TestCases/implicit-integer-truncation.c +++ b/compiler-rt/test/ubsan_minimal/TestCases/implicit-integer-truncation.c @@ -3,7 +3,7 @@ #include <stdint.h> int main() { -// CHECK-NOT: integer-truncation.c +// 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. @@ -19,6 +19,7 @@ int main() { // Positive tests. uint8_t t0 = (~((uint32_t)(0))); // CHECK: implicit-conversion +// CHECK-NOT: implicit-conversion return 0; } |