xref: /llvm-project/compiler-rt/test/ubsan/TestCases/Integer/bit-int-pass.c (revision 290e4858fd3d1278ca8306da351f7ab89240306e)
1 // REQUIRES: x86_64-target-arch
2 // RUN: %clang -Wno-constant-conversion -Wno-array-bounds -Wno-division-by-zero -Wno-shift-negative-value -Wno-shift-count-negative -Wno-int-to-pointer-cast -O0 -fsanitize=alignment,array-bounds,bool,float-cast-overflow,implicit-integer-sign-change,implicit-signed-integer-truncation,implicit-unsigned-integer-truncation,integer-divide-by-zero,nonnull-attribute,null,nullability-arg,nullability-assign,nullability-return,pointer-overflow,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,unreachable,unsigned-integer-overflow,unsigned-shift-base,vla-bound %s -o %t1 && %run %t1 2>&1 | FileCheck %s
3 
4 #include <stdint.h>
5 #include <stdio.h>
6 
7 // In this test there is an expectation of assignment of _BitInt not producing any output.
8 uint32_t nullability_arg(_BitInt(37) *_Nonnull x)
9     __attribute__((no_sanitize("address")))
10     __attribute__((no_sanitize("memory"))) {
11   _BitInt(37) y = *(_BitInt(37) *)&x;
12   return (y > 0) ? y : 0;
13 }
14 
15 // In this test there is an expectation of ubsan not triggeting on returning random address which is inside address space of the process.
16 _BitInt(37) nonnull_attribute(__attribute__((nonnull)) _BitInt(37) * x)
17     __attribute__((no_sanitize("address")))
18     __attribute__((no_sanitize("memory"))) {
19   return *(_BitInt(37) *)&x;
20 }
21 
22 // In this test there is an expectation of assignment of uint32_t from "invalid" _BitInt is not producing any output.
23 uint32_t nullability_assign(_BitInt(7) * x)
24     __attribute__((no_sanitize("address")))
25     __attribute__((no_sanitize("memory"))) {
26   _BitInt(7) *_Nonnull y = x;
27   int32_t r = *(_BitInt(7) *)&y;
28   return (r > 0) ? r : 0;
29 }
30 
31 // In those examples the file is expected to compile & run with no diagnostics
32 // CHECK-NOT: runtime error:
33 
34 int main(int argc, char **argv) {
35   // clang-format off
36   uint64_t result =
37       1ULL +
38       nullability_arg((_BitInt(37) *)argc) +
39       ((uint64_t)nonnull_attribute((_BitInt(37) *)argc) & 0xFFFFFFFF) +
40       nullability_assign((_BitInt(7) *)argc);
41   // clang-format on
42   printf("%u\n", (uint32_t)(result & 0xFFFFFFFF));
43 }
44