| 1 |
// RUN: %clang_cc1 -fsyntax-only -verify %s |
| 2 |
// RUN: %clang_cc1 -fexperimental-late-parse-attributes -fsyntax-only -verify %s |
| 3 |
|
| 4 |
#define __sized_by_or_null(f) __attribute__((sized_by_or_null(f))) |
| 5 |
#define __counted_by(f) __attribute__((counted_by(f))) |
| 6 |
|
| 7 |
struct size_unknown; |
| 8 |
struct size_known { |
| 9 |
int field; |
| 10 |
}; |
| 11 |
|
| 12 |
typedef void(*fn_ptr_ty)(void); |
| 13 |
|
| 14 |
//============================================================================== |
| 15 |
// __sized_by_or_null on struct member pointer in decl attribute position |
| 16 |
//============================================================================== |
| 17 |
|
| 18 |
struct on_member_pointer_complete_ty { |
| 19 |
int size; |
| 20 |
struct size_known * buf __sized_by_or_null(size); |
| 21 |
}; |
| 22 |
|
| 23 |
struct on_member_pointer_incomplete_ty { |
| 24 |
int size; |
| 25 |
struct size_unknown * buf __sized_by_or_null(size); |
| 26 |
}; |
| 27 |
|
| 28 |
struct on_member_pointer_const_incomplete_ty { |
| 29 |
int size; |
| 30 |
const struct size_unknown * buf __sized_by_or_null(size); |
| 31 |
}; |
| 32 |
|
| 33 |
struct on_member_pointer_void_ty { |
| 34 |
int size; |
| 35 |
void* buf __sized_by_or_null(size); |
| 36 |
}; |
| 37 |
|
| 38 |
struct on_member_pointer_fn_ptr_ty { |
| 39 |
int size; |
| 40 |
// buffer of function pointers with size `size` is allowed |
| 41 |
void (**fn_ptr)(void) __sized_by_or_null(size); |
| 42 |
}; |
| 43 |
|
| 44 |
struct on_member_pointer_fn_ptr_ty_ptr_ty { |
| 45 |
int size; |
| 46 |
// buffer of function pointers with size `size` is allowed |
| 47 |
fn_ptr_ty* fn_ptr __sized_by_or_null(size); |
| 48 |
}; |
| 49 |
|
| 50 |
struct on_member_pointer_fn_ty { |
| 51 |
int size; |
| 52 |
// buffer of functions with size `size` is allowed |
| 53 |
// expected-error@+1{{'sized_by_or_null' cannot be applied to a pointer with pointee of unknown size because 'void (void)' is a function type}} |
| 54 |
void (*fn_ptr)(void) __sized_by_or_null(size); |
| 55 |
}; |
| 56 |
|
| 57 |
struct on_member_pointer_fn_ptr_ty_ty { |
| 58 |
int size; |
| 59 |
// buffer of functions with size `size` is allowed |
| 60 |
// expected-error@+1{{'sized_by_or_null' cannot be applied to a pointer with pointee of unknown size because 'void (void)' is a function type}} |
| 61 |
fn_ptr_ty fn_ptr __sized_by_or_null(size); |
| 62 |
}; |
| 63 |
|
| 64 |
struct has_unannotated_vla { |
| 65 |
int count; |
| 66 |
int buffer[]; |
| 67 |
}; |
| 68 |
|
| 69 |
struct on_member_pointer_struct_with_vla { |
| 70 |
int size; |
| 71 |
// we know the size so this is fine for tracking size, however indexing would be an issue |
| 72 |
// expected-error@+1{{'sized_by_or_null' 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}} |
| 73 |
struct has_unannotated_vla* objects __sized_by_or_null(size); |
| 74 |
}; |
| 75 |
|
| 76 |
struct has_annotated_vla { |
| 77 |
int count; |
| 78 |
int buffer[] __counted_by(count); |
| 79 |
}; |
| 80 |
|
| 81 |
struct on_member_pointer_struct_with_annotated_vla { |
| 82 |
int size; |
| 83 |
// we know the size so this is fine for tracking size, however indexing would be an issue |
| 84 |
// expected-error@+1{{'sized_by_or_null' 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}} |
| 85 |
struct has_annotated_vla* objects __sized_by_or_null(size); |
| 86 |
}; |
| 87 |
|
| 88 |
struct on_pointer_anon_buf { |
| 89 |
int size; |
| 90 |
struct { |
| 91 |
struct size_known *buf __sized_by_or_null(size); |
| 92 |
}; |
| 93 |
}; |
| 94 |
|
| 95 |
struct on_pointer_anon_size { |
| 96 |
struct { |
| 97 |
int size; |
| 98 |
}; |
| 99 |
struct size_known *buf __sized_by_or_null(size); |
| 100 |
}; |
| 101 |
|
| 102 |
//============================================================================== |
| 103 |
// __sized_by_or_null on struct member pointer in type attribute position |
| 104 |
//============================================================================== |
| 105 |
// TODO: Correctly parse sized_by_or_null as a type attribute. Currently it is parsed |
| 106 |
// as a declaration attribute |
| 107 |
|
| 108 |
struct on_member_pointer_complete_ty_ty_pos { |
| 109 |
int size; |
| 110 |
struct size_known *__sized_by_or_null(size) buf; |
| 111 |
}; |
| 112 |
|
| 113 |
struct on_member_pointer_incomplete_ty_ty_pos { |
| 114 |
int size; |
| 115 |
struct size_unknown * __sized_by_or_null(size) buf; |
| 116 |
}; |
| 117 |
|
| 118 |
struct on_member_pointer_const_incomplete_ty_ty_pos { |
| 119 |
int size; |
| 120 |
const struct size_unknown * __sized_by_or_null(size) buf; |
| 121 |
}; |
| 122 |
|
| 123 |
struct on_member_pointer_void_ty_ty_pos { |
| 124 |
int size; |
| 125 |
void *__sized_by_or_null(size) buf; |
| 126 |
}; |
| 127 |
|
| 128 |
// - |
| 129 |
|
| 130 |
struct on_member_pointer_fn_ptr_ty_pos { |
| 131 |
int size; |
| 132 |
// buffer of `size` function pointers is allowed |
| 133 |
void (** __sized_by_or_null(size) fn_ptr)(void); |
| 134 |
}; |
| 135 |
|
| 136 |
struct on_member_pointer_fn_ptr_ty_ptr_ty_pos { |
| 137 |
int size; |
| 138 |
// buffer of `size` function pointers is allowed |
| 139 |
fn_ptr_ty* __sized_by_or_null(size) fn_ptr; |
| 140 |
}; |
| 141 |
|
| 142 |
struct on_member_pointer_fn_ty_ty_pos { |
| 143 |
int size; |
| 144 |
// expected-error@+1{{'sized_by_or_null' cannot be applied to a pointer with pointee of unknown size because 'void (void)' is a function type}} |
| 145 |
void (* __sized_by_or_null(size) fn_ptr)(void); |
| 146 |
}; |
| 147 |
|
| 148 |
struct on_member_pointer_fn_ptr_ty_ty_pos { |
| 149 |
int size; |
| 150 |
// expected-error@+1{{'sized_by_or_null' cannot be applied to a pointer with pointee of unknown size because 'void (void)' is a function type}} |
| 151 |
fn_ptr_ty __sized_by_or_null(size) fn_ptr; |
| 152 |
}; |
| 153 |
|
| 154 |
// TODO: This should be forbidden but isn't due to sized_by_or_null being treated |
| 155 |
// as a declaration attribute. |
| 156 |
struct on_member_pointer_fn_ptr_ty_ty_pos_inner { |
| 157 |
int size; |
| 158 |
void (* __sized_by_or_null(size) * fn_ptr)(void); |
| 159 |
}; |
| 160 |
|
| 161 |
struct on_member_pointer_struct_with_vla_ty_pos { |
| 162 |
int size; |
| 163 |
// expected-error@+1{{'sized_by_or_null' 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}} |
| 164 |
struct has_unannotated_vla *__sized_by_or_null(size) objects; |
| 165 |
}; |
| 166 |
|
| 167 |
struct on_member_pointer_struct_with_annotated_vla_ty_pos { |
| 168 |
int size; |
| 169 |
// expected-error@+1{{'sized_by_or_null' 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}} |
| 170 |
struct has_annotated_vla* __sized_by_or_null(size) objects; |
| 171 |
}; |
| 172 |
|
| 173 |
struct on_nested_pointer_inner { |
| 174 |
// TODO: This should be disallowed because in the `-fbounds-safety` model |
| 175 |
// `__sized_by_or_null` can only be nested when used in function parameters. |
| 176 |
int size; |
| 177 |
struct size_known *__sized_by_or_null(size) *buf; |
| 178 |
}; |
| 179 |
|
| 180 |
struct on_nested_pointer_outer { |
| 181 |
int size; |
| 182 |
struct size_known **__sized_by_or_null(size) buf; |
| 183 |
}; |
| 184 |
|
| 185 |
struct on_pointer_anon_buf_ty_pos { |
| 186 |
int size; |
| 187 |
struct { |
| 188 |
struct size_known * __sized_by_or_null(size) buf; |
| 189 |
}; |
| 190 |
}; |
| 191 |
|
| 192 |
struct on_pointer_anon_size_ty_pos { |
| 193 |
struct { |
| 194 |
int size; |
| 195 |
}; |
| 196 |
struct size_known *__sized_by_or_null(size) buf; |
| 197 |
}; |
| 198 |
|
| 199 |
//============================================================================== |
| 200 |
// __sized_by_or_null on struct non-pointer members |
| 201 |
//============================================================================== |
| 202 |
|
| 203 |
struct on_pod_ty { |
| 204 |
int size; |
| 205 |
// expected-error-re@+1{{'sized_by_or_null' only applies to pointers{{$}}}} |
| 206 |
int wrong_ty __sized_by_or_null(size); |
| 207 |
}; |
| 208 |
|
| 209 |
struct on_void_ty { |
| 210 |
int size; |
| 211 |
// expected-error-re@+2{{'sized_by_or_null' only applies to pointers{{$}}}} |
| 212 |
// expected-error@+1{{field has incomplete type 'void'}} |
| 213 |
void wrong_ty __sized_by_or_null(size); |
| 214 |
}; |
| 215 |
|
| 216 |
struct on_member_array_complete_ty { |
| 217 |
int size; |
| 218 |
// expected-error@+1{{'sized_by_or_null' only applies to pointers; did you mean to use 'counted_by'?}} |
| 219 |
struct size_known array[] __sized_by_or_null(size); |
| 220 |
}; |