xref: /aosp_15_r20/external/compiler-rt/test/ubsan/TestCases/Misc/nonnull-arg.cpp (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clangxx -fsanitize=nonnull-attribute -fno-sanitize-recover=all %s -O3 -o %t
2*7c3d14c8STreehugger Robot // RUN: %run %t nc
3*7c3d14c8STreehugger Robot // RUN: %run %t nm
4*7c3d14c8STreehugger Robot // RUN: %run %t nf
5*7c3d14c8STreehugger Robot // RUN: %run %t nv
6*7c3d14c8STreehugger Robot // RUN: not %run %t 0c 2>&1 | FileCheck %s --check-prefix=CTOR
7*7c3d14c8STreehugger Robot // RUN: not %run %t 0m 2>&1 | FileCheck %s --check-prefix=METHOD
8*7c3d14c8STreehugger Robot // RUN: not %run %t 0f 2>&1 | FileCheck %s --check-prefix=FUNC
9*7c3d14c8STreehugger Robot // RUN: not %run %t 0v 2>&1 | FileCheck %s --check-prefix=VARIADIC
10*7c3d14c8STreehugger Robot //
11*7c3d14c8STreehugger Robot // AArch64 lacks variadic instrumentation for MSAN.
12*7c3d14c8STreehugger Robot // REQUIRES: stable-runtime
13*7c3d14c8STreehugger Robot 
14*7c3d14c8STreehugger Robot class C {
15*7c3d14c8STreehugger Robot   int *null_;
16*7c3d14c8STreehugger Robot   int *nonnull_;
17*7c3d14c8STreehugger Robot 
18*7c3d14c8STreehugger Robot public:
C(int * null,int * nonnull)19*7c3d14c8STreehugger Robot   C(int *null, __attribute__((nonnull)) int *nonnull)
20*7c3d14c8STreehugger Robot       : null_(null), nonnull_(nonnull) {}
value()21*7c3d14c8STreehugger Robot   int value() { return *nonnull_; }
method(int * nonnull,int * null)22*7c3d14c8STreehugger Robot   int method(int *nonnull, int *null) __attribute__((nonnull(2))) {
23*7c3d14c8STreehugger Robot     return *nonnull_ + *nonnull;
24*7c3d14c8STreehugger Robot   }
25*7c3d14c8STreehugger Robot };
26*7c3d14c8STreehugger Robot 
func(int * nonnull)27*7c3d14c8STreehugger Robot __attribute__((nonnull)) int func(int *nonnull) { return *nonnull; }
28*7c3d14c8STreehugger Robot 
29*7c3d14c8STreehugger Robot #include <stdarg.h>
variadic(int x,...)30*7c3d14c8STreehugger Robot __attribute__((nonnull)) int variadic(int x, ...) {
31*7c3d14c8STreehugger Robot   va_list args;
32*7c3d14c8STreehugger Robot   va_start(args, x);
33*7c3d14c8STreehugger Robot   int *nonnull = va_arg(args, int*);
34*7c3d14c8STreehugger Robot   int res = *nonnull;
35*7c3d14c8STreehugger Robot   va_end(args);
36*7c3d14c8STreehugger Robot   return res;
37*7c3d14c8STreehugger Robot }
38*7c3d14c8STreehugger Robot 
main(int argc,char * argv[])39*7c3d14c8STreehugger Robot int main(int argc, char *argv[]) {
40*7c3d14c8STreehugger Robot   int local = 0;
41*7c3d14c8STreehugger Robot   int *arg = (argv[1][0] == '0') ? 0x0 : &local;
42*7c3d14c8STreehugger Robot   switch (argv[1][1]) {
43*7c3d14c8STreehugger Robot     case 'c':
44*7c3d14c8STreehugger Robot       return C(0x0, arg).value();
45*7c3d14c8STreehugger Robot       // CTOR: {{.*}}nonnull-arg.cpp:[[@LINE-1]]:21: runtime error: null pointer passed as argument 2, which is declared to never be null
46*7c3d14c8STreehugger Robot       // CTOR-NEXT: {{.*}}nonnull-arg.cpp:19:31: note: nonnull attribute specified here
47*7c3d14c8STreehugger Robot     case 'm':
48*7c3d14c8STreehugger Robot       return C(0x0, &local).method(arg, 0x0);
49*7c3d14c8STreehugger Robot       // METHOD: {{.*}}nonnull-arg.cpp:[[@LINE-1]]:36: runtime error: null pointer passed as argument 1, which is declared to never be null
50*7c3d14c8STreehugger Robot       // METHOD-NEXT: {{.*}}nonnull-arg.cpp:22:54: note: nonnull attribute specified here
51*7c3d14c8STreehugger Robot     case 'f':
52*7c3d14c8STreehugger Robot       return func(arg);
53*7c3d14c8STreehugger Robot       // FUNC: {{.*}}nonnull-arg.cpp:[[@LINE-1]]:19: runtime error: null pointer passed as argument 1, which is declared to never be null
54*7c3d14c8STreehugger Robot       // FUNC-NEXT: {{.*}}nonnull-arg.cpp:27:16: note: nonnull attribute specified here
55*7c3d14c8STreehugger Robot     case 'v':
56*7c3d14c8STreehugger Robot       return variadic(42, arg);
57*7c3d14c8STreehugger Robot     // VARIADIC: {{.*}}nonnull-arg.cpp:[[@LINE-1]]:27: runtime error: null pointer passed as argument 2, which is declared to never be null
58*7c3d14c8STreehugger Robot     // VARIADIC-NEXT: {{.*}}nonnull-arg.cpp:30:16: note: nonnull attribute specified here
59*7c3d14c8STreehugger Robot   }
60*7c3d14c8STreehugger Robot   return 0;
61*7c3d14c8STreehugger Robot }
62