File: attr-counted-by-struct-ptrs.c

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,998,520 kB
  • sloc: cpp: 6,951,680; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,167; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (224 lines) | stat: -rw-r--r-- 7,650 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// RUN: %clang_cc1 -fsyntax-only -verify %s

#define __counted_by(f)  __attribute__((counted_by(f)))

struct size_unknown;
struct size_known {
  int field;
};

typedef void(*fn_ptr_ty)(void);

//==============================================================================
// __counted_by on struct member pointer in decl attribute position
//==============================================================================

struct on_member_pointer_complete_ty {
  int count;
  struct size_known * buf __counted_by(count);
};

struct on_member_pointer_incomplete_ty {
  int count;
  // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'struct size_unknown' is an incomplete type}}
  struct size_unknown * buf __counted_by(count);
};

struct on_member_pointer_const_incomplete_ty {
  int count;
  // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'const struct size_unknown' is an incomplete type}}
  const struct size_unknown * buf __counted_by(count);
};

struct on_member_pointer_void_ty {
  int count;
  // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'void' is an incomplete type}}
  void* buf __counted_by(count);
};

struct on_member_pointer_fn_ptr_ty {
  int count;
  // buffer of `count` function pointers is allowed
  void (**fn_ptr)(void) __counted_by(count);
};

struct on_member_pointer_fn_ptr_ty_ptr_ty {
  int count;
  // buffer of `count` function pointers is allowed
  fn_ptr_ty* fn_ptr __counted_by(count);
};

struct on_member_pointer_fn_ty {
  int count;
  // buffer of `count` functions is not allowed
  // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'void (void)' is a function type}}
  void (*fn_ptr)(void) __counted_by(count);
};

struct on_member_pointer_fn_ptr_ty_ty {
  int count;
  // buffer of `count` functions is not allowed
  // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'void (void)' is a function type}}
  fn_ptr_ty fn_ptr __counted_by(count);
};

struct has_unannotated_vla {
  int count;
  int buffer[];
};

struct on_member_pointer_struct_with_vla {
  int count;
  // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'struct has_unannotated_vla' is a struct type with a flexible array member}}
  struct has_unannotated_vla* objects __counted_by(count);
};

struct has_annotated_vla {
  int count;
  int buffer[] __counted_by(count);
};

// Currently prevented because computing the size of `objects` at runtime would
// require an O(N) walk of `objects` to take into account the length of the VLA
// in each struct instance.
struct on_member_pointer_struct_with_annotated_vla {
  int count;
  // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'struct has_annotated_vla' is a struct type with a flexible array member}}
  struct has_annotated_vla* objects __counted_by(count);
};

struct on_pointer_anon_buf {
  int count;
  struct {
    struct size_known *buf __counted_by(count);
  };
};

struct on_pointer_anon_count {
  struct {
    int count;
  };
  struct size_known *buf __counted_by(count);
};

//==============================================================================
// __counted_by on struct member pointer in type attribute position
//==============================================================================
// TODO: Correctly parse counted_by as a type attribute. Currently it is parsed
// as a declaration attribute

struct on_member_pointer_complete_ty_ty_pos {
  int count;
  struct size_known *__counted_by(count) buf;
};

struct on_member_pointer_incomplete_ty_ty_pos {
  int count;
  // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'struct size_unknown' is an incomplete type}}
  struct size_unknown * __counted_by(count) buf;
};

struct on_member_pointer_const_incomplete_ty_ty_pos {
  int count;
  // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'const struct size_unknown' is an incomplete type}}
  const struct size_unknown * __counted_by(count) buf;
};

struct on_member_pointer_void_ty_ty_pos {
  int count;
  // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'void' is an incomplete type}}
  void *__counted_by(count) buf;
};

// -

struct on_member_pointer_fn_ptr_ty_pos {
  int count;
  // buffer of `count` function pointers is allowed
  void (** __counted_by(count) fn_ptr)(void);
};

struct on_member_pointer_fn_ptr_ty_ptr_ty_pos {
  int count;
  // buffer of `count` function pointers is allowed
  fn_ptr_ty* __counted_by(count) fn_ptr;
};

struct on_member_pointer_fn_ty_ty_pos {
  int count;
  // buffer of `count` functions is not allowed
  // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'void (void)' is a function type}}
  void (* __counted_by(count) fn_ptr)(void);
};

struct on_member_pointer_fn_ptr_ty_ty_pos {
  int count;
  // buffer of `count` functions is not allowed
  // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'void (void)' is a function type}}
  fn_ptr_ty __counted_by(count) fn_ptr;
};

// TODO: This should be forbidden but isn't due to counted_by being treated
// as a declaration attribute.
struct on_member_pointer_fn_ptr_ty_ty_pos_inner {
  int count;
  void (* __counted_by(count) * fn_ptr)(void);
};

struct on_member_pointer_struct_with_vla_ty_pos {
  int count;
  // expected-error@+1{{'counted_by' cannot be applied to a pointer with pointee of unknown size because 'struct has_unannotated_vla' is a struct type with a flexible array member}}
  struct has_unannotated_vla *__counted_by(count) objects;
};

// Currently prevented because computing the size of `objects` at runtime would
// require an O(N) walk of `objects` to take into account the length of the VLA
// in each struct instance.
struct on_member_pointer_struct_with_annotated_vla_ty_pos {
  int count;
  // expected-error@+1{{counted_by' cannot be applied to a pointer with pointee of unknown size because 'struct has_annotated_vla' is a struct type with a flexible array member}}
  struct has_annotated_vla* __counted_by(count) objects;
};

struct on_nested_pointer_inner {
  // TODO: This should be disallowed because in the `-fbounds-safety` model
  // `__counted_by` can only be nested when used in function parameters.
  int count;
  struct size_known *__counted_by(count) *buf;
};

struct on_nested_pointer_outer {
  int count;
  struct size_known **__counted_by(count) buf;
};

struct on_pointer_anon_buf_ty_pos {
  int count;
  struct {
    struct size_known * __counted_by(count) buf;
  };
};

struct on_pointer_anon_count_ty_pos {
  struct {
    int count;
  };
  struct size_known *__counted_by(count) buf;
};

//==============================================================================
// __counted_by on struct non-pointer members
//==============================================================================

struct on_pod_ty {
  int count;
  // expected-error@+1{{'counted_by' only applies to pointers or C99 flexible array members}}
  int wrong_ty __counted_by(count);
};

struct on_void_ty {
  int count;
  // expected-error@+2{{'counted_by' only applies to pointers or C99 flexible array members}}
  // expected-error@+1{{field has incomplete type 'void'}}
  void wrong_ty __counted_by(count);
};