annotate clang/test/Sema/builtin-object-size.c @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900
parents 1d019706d866
children c4bab56944e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 // RUN: %clang_cc1 -fsyntax-only -verify %s
anatofuz
parents:
diff changeset
2 // RUN: %clang_cc1 -fsyntax-only -triple x86_64-apple-darwin9 -verify %s
anatofuz
parents:
diff changeset
3 // RUN: %clang_cc1 -DDYNAMIC -fsyntax-only -triple x86_64-apple-darwin9 -verify %s
anatofuz
parents:
diff changeset
4
anatofuz
parents:
diff changeset
5 #ifndef DYNAMIC
anatofuz
parents:
diff changeset
6 #define OBJECT_SIZE_BUILTIN __builtin_object_size
anatofuz
parents:
diff changeset
7 #else
anatofuz
parents:
diff changeset
8 #define OBJECT_SIZE_BUILTIN __builtin_dynamic_object_size
anatofuz
parents:
diff changeset
9 #endif
anatofuz
parents:
diff changeset
10
anatofuz
parents:
diff changeset
11 int a[10];
anatofuz
parents:
diff changeset
12
anatofuz
parents:
diff changeset
13 int f0() {
anatofuz
parents:
diff changeset
14 return OBJECT_SIZE_BUILTIN(&a); // expected-error {{too few arguments to function}}
anatofuz
parents:
diff changeset
15 }
anatofuz
parents:
diff changeset
16 int f1() {
anatofuz
parents:
diff changeset
17 return (OBJECT_SIZE_BUILTIN(&a, 0) +
anatofuz
parents:
diff changeset
18 OBJECT_SIZE_BUILTIN(&a, 1) +
anatofuz
parents:
diff changeset
19 OBJECT_SIZE_BUILTIN(&a, 2) +
anatofuz
parents:
diff changeset
20 OBJECT_SIZE_BUILTIN(&a, 3));
anatofuz
parents:
diff changeset
21 }
anatofuz
parents:
diff changeset
22 int f2() {
anatofuz
parents:
diff changeset
23 return OBJECT_SIZE_BUILTIN(&a, -1); // expected-error {{argument value -1 is outside the valid range [0, 3]}}
anatofuz
parents:
diff changeset
24 }
anatofuz
parents:
diff changeset
25 int f3() {
anatofuz
parents:
diff changeset
26 return OBJECT_SIZE_BUILTIN(&a, 4); // expected-error {{argument value 4 is outside the valid range [0, 3]}}
anatofuz
parents:
diff changeset
27 }
anatofuz
parents:
diff changeset
28
anatofuz
parents:
diff changeset
29
anatofuz
parents:
diff changeset
30 // rdar://6252231 - cannot call vsnprintf with va_list on x86_64
anatofuz
parents:
diff changeset
31 void f4(const char *fmt, ...) {
anatofuz
parents:
diff changeset
32 __builtin_va_list args;
anatofuz
parents:
diff changeset
33 __builtin___vsnprintf_chk (0, 42, 0, 11, fmt, args); // expected-warning {{'vsnprintf' will always overflow; destination buffer has size 11, but size argument is 42}}
anatofuz
parents:
diff changeset
34 }
anatofuz
parents:
diff changeset
35
anatofuz
parents:
diff changeset
36 // rdar://18334276
anatofuz
parents:
diff changeset
37 typedef __typeof__(sizeof(int)) size_t;
anatofuz
parents:
diff changeset
38 void * memcset(void *restrict dst, int src, size_t n);
anatofuz
parents:
diff changeset
39 void * memcpy(void *restrict dst, const void *restrict src, size_t n);
anatofuz
parents:
diff changeset
40
anatofuz
parents:
diff changeset
41 #define memset(dest, src, len) __builtin___memset_chk(dest, src, len, OBJECT_SIZE_BUILTIN(dest, 0))
anatofuz
parents:
diff changeset
42 #define memcpy(dest, src, len) __builtin___memcpy_chk(dest, src, len, OBJECT_SIZE_BUILTIN(dest, 0))
anatofuz
parents:
diff changeset
43 #define memcpy1(dest, src, len) __builtin___memcpy_chk(dest, src, len, OBJECT_SIZE_BUILTIN(dest, 4))
anatofuz
parents:
diff changeset
44 #define NULL ((void *)0)
anatofuz
parents:
diff changeset
45
anatofuz
parents:
diff changeset
46 void f5(void)
anatofuz
parents:
diff changeset
47 {
anatofuz
parents:
diff changeset
48 char buf[10];
anatofuz
parents:
diff changeset
49 memset((void *)0x100000000ULL, 0, 0x1000);
anatofuz
parents:
diff changeset
50 memcpy((char *)NULL + 0x10000, buf, 0x10);
anatofuz
parents:
diff changeset
51 memcpy1((char *)NULL + 0x10000, buf, 0x10); // expected-error {{argument value 4 is outside the valid range [0, 3]}}
anatofuz
parents:
diff changeset
52 }
anatofuz
parents:
diff changeset
53
anatofuz
parents:
diff changeset
54 // rdar://18431336
anatofuz
parents:
diff changeset
55 void f6(void)
anatofuz
parents:
diff changeset
56 {
anatofuz
parents:
diff changeset
57 char b[5];
anatofuz
parents:
diff changeset
58 char buf[10];
anatofuz
parents:
diff changeset
59 __builtin___memccpy_chk (buf, b, '\0', sizeof(b), OBJECT_SIZE_BUILTIN (buf, 0));
anatofuz
parents:
diff changeset
60 __builtin___memccpy_chk (b, buf, '\0', sizeof(buf), OBJECT_SIZE_BUILTIN (b, 0)); // expected-warning {{'memccpy' will always overflow; destination buffer has size 5, but size argument is 10}}
anatofuz
parents:
diff changeset
61 }
anatofuz
parents:
diff changeset
62
anatofuz
parents:
diff changeset
63 int pr28314(void) {
anatofuz
parents:
diff changeset
64 struct {
anatofuz
parents:
diff changeset
65 struct InvalidField a; // expected-error{{has incomplete type}} expected-note 3{{forward declaration of 'struct InvalidField'}}
anatofuz
parents:
diff changeset
66 char b[0];
anatofuz
parents:
diff changeset
67 } *p;
anatofuz
parents:
diff changeset
68
anatofuz
parents:
diff changeset
69 struct {
anatofuz
parents:
diff changeset
70 struct InvalidField a; // expected-error{{has incomplete type}}
anatofuz
parents:
diff changeset
71 char b[1];
anatofuz
parents:
diff changeset
72 } *p2;
anatofuz
parents:
diff changeset
73
anatofuz
parents:
diff changeset
74 struct {
anatofuz
parents:
diff changeset
75 struct InvalidField a; // expected-error{{has incomplete type}}
anatofuz
parents:
diff changeset
76 char b[2];
anatofuz
parents:
diff changeset
77 } *p3;
anatofuz
parents:
diff changeset
78
anatofuz
parents:
diff changeset
79 int a = 0;
anatofuz
parents:
diff changeset
80 a += OBJECT_SIZE_BUILTIN(&p->a, 0);
anatofuz
parents:
diff changeset
81 a += OBJECT_SIZE_BUILTIN(p->b, 0);
anatofuz
parents:
diff changeset
82 a += OBJECT_SIZE_BUILTIN(p2->b, 0);
anatofuz
parents:
diff changeset
83 a += OBJECT_SIZE_BUILTIN(p3->b, 0);
anatofuz
parents:
diff changeset
84 return a;
anatofuz
parents:
diff changeset
85 }
anatofuz
parents:
diff changeset
86
anatofuz
parents:
diff changeset
87 int pr31843() {
anatofuz
parents:
diff changeset
88 int n = 0;
anatofuz
parents:
diff changeset
89
anatofuz
parents:
diff changeset
90 struct { int f; } a;
anatofuz
parents:
diff changeset
91 int b;
anatofuz
parents:
diff changeset
92 n += OBJECT_SIZE_BUILTIN(({&(b ? &a : &a)->f; pr31843;}), 0); // expected-warning{{expression result unused}}
anatofuz
parents:
diff changeset
93
anatofuz
parents:
diff changeset
94 struct statfs { char f_mntonname[1024];};
anatofuz
parents:
diff changeset
95 struct statfs *outStatFSBuf;
anatofuz
parents:
diff changeset
96 n += OBJECT_SIZE_BUILTIN(outStatFSBuf->f_mntonname ? "" : "", 1); // expected-warning{{address of array}}
anatofuz
parents:
diff changeset
97 n += OBJECT_SIZE_BUILTIN(outStatFSBuf->f_mntonname ?: "", 1);
anatofuz
parents:
diff changeset
98
anatofuz
parents:
diff changeset
99 return n;
anatofuz
parents:
diff changeset
100 }
anatofuz
parents:
diff changeset
101
anatofuz
parents:
diff changeset
102 typedef struct {
anatofuz
parents:
diff changeset
103 char string[512];
anatofuz
parents:
diff changeset
104 } NestedArrayStruct;
anatofuz
parents:
diff changeset
105
anatofuz
parents:
diff changeset
106 typedef struct {
anatofuz
parents:
diff changeset
107 int x;
anatofuz
parents:
diff changeset
108 NestedArrayStruct session[];
anatofuz
parents:
diff changeset
109 } IncompleteArrayStruct;
anatofuz
parents:
diff changeset
110
anatofuz
parents:
diff changeset
111 void rd36094951_IAS_builtin_object_size_assertion(IncompleteArrayStruct *p) {
anatofuz
parents:
diff changeset
112 #define rd36094951_CHECK(mode) \
anatofuz
parents:
diff changeset
113 __builtin___strlcpy_chk(p->session[0].string, "ab", 2, \
anatofuz
parents:
diff changeset
114 OBJECT_SIZE_BUILTIN(p->session[0].string, mode))
anatofuz
parents:
diff changeset
115 rd36094951_CHECK(0);
anatofuz
parents:
diff changeset
116 rd36094951_CHECK(1);
anatofuz
parents:
diff changeset
117 rd36094951_CHECK(2);
anatofuz
parents:
diff changeset
118 rd36094951_CHECK(3);
anatofuz
parents:
diff changeset
119 }