-
Notifications
You must be signed in to change notification settings - Fork 8
/
array-bounds.c
672 lines (561 loc) · 19.3 KB
/
array-bounds.c
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/* Build with -Wall -O2 -fstrict-flex-arrays=3 -fsanitize=bounds -fsanitize=object-size -fstrict-flex-arrays=3 */
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include "harness.h"
typedef unsigned char u8;
typedef signed char s8;
#define noinline __attribute__((__noinline__))
/* Used to stop optimizer from seeing constant expressions. */
volatile int unconst = 0;
volatile int debug = 0;
/*
* Used make sure allocations are seen as "escaping" the local function
* to avoid Dead Store Elimination.
*/
volatile void *escape;
#define REPORT_SIZE(p) do { \
const size_t bdos = __builtin_dynamic_object_size(p, 1); \
escape = p; \
if (__builtin_constant_p(bdos)) { \
if (bdos == SIZE_MAX) { \
if (debug) TH_LOG(#p " has an unknown-to-bdos size"); \
} else { \
if (debug) TH_LOG(#p " has a fixed size: %zu (%zu elements of size %zu)", bdos, \
bdos / sizeof(*(p)), sizeof(*(p))); \
} \
} else { \
if (debug) TH_LOG(#p " has a dynamic size: %zu (%zu elements of size %zu)", bdos, \
bdos / sizeof(*(p)), sizeof(*(p))); \
} \
if (debug) fflush(NULL); \
} while (0)
#if __has_attribute(__counted_by__)
# define __counted_by(member) __attribute__((__counted_by__(member)))
#else
# define __counted_by(member) /* __attribute__((__counted_by__(member))) */
#endif
#define DECLARE_FLEX_ARRAY(TYPE, NAME) \
struct { \
struct { } __empty_ ## NAME; \
TYPE NAME[]; \
}
#define DECLARE_BOUNDED_FLEX_ARRAY(COUNT_TYPE, COUNT, TYPE, NAME) \
struct { \
COUNT_TYPE COUNT; \
TYPE NAME[] __counted_by(COUNT); \
}
#define DECLARE_FLEX_ARRAY_COUNTED_BY(TYPE, NAME, COUNTED_BY) \
struct { \
struct { } __empty_ ## NAME; \
TYPE NAME[] __counted_by(COUNTED_BY); \
}
#define MAX_INDEX 16
#define SIZE_BUMP 2
enum enforcement {
SHOULD_NOT_TRAP = 0,
SHOULD_TRAP,
};
struct fixed {
unsigned long flags;
size_t count;
int array[MAX_INDEX];
};
struct flex {
unsigned long flags;
long count;
int array[];
};
struct annotated {
unsigned long flags;
long count;
int array[] __counted_by(count);
};
struct multi {
unsigned long flags;
union {
/* count member type intentionally mismatched to induce padding */
DECLARE_BOUNDED_FLEX_ARRAY(int, count_bytes, unsigned char, bytes);
DECLARE_BOUNDED_FLEX_ARRAY(s8, count_ints, unsigned char, ints);
DECLARE_FLEX_ARRAY(unsigned char, unsafe);
};
};
struct anon_struct {
unsigned long flags;
long count;
int array[] __counted_by(count);
//gcc: DECLARE_FLEX_ARRAY_COUNTED_BY(int, array, count);
};
struct composite {
unsigned stuff;
struct annotated inner;
};
/* Not supported yet. */
#if 0
struct ptr_annotated {
unsigned long flags;
int *array __counted_by(count);
unsigned char count;
};
#endif
/*
* Test safe accesses at index 0 and index-1, then optionally check
* what happens when accessing "index".
*/
#define TEST_ACCESS(p, array, index, enforcement) do { \
\
/* Index zero is in the array. */ \
/*TH_LOG("array address: %p", (p)->array);*/ \
if (debug) TH_LOG("safe: array[0] = 0xFF"); \
(p)->array[0] = 0xFF; \
ASSERT_EQ((p)->array[0], 0xFF); \
\
if (index > 1) { \
if (debug) TH_LOG("safe: array[%d] = 0xFF", index - 1); \
(p)->array[index - 1] = 0xFF; \
ASSERT_EQ((p)->array[index - 1], 0xFF); \
} \
\
if (enforcement == SHOULD_TRAP) { \
/* "index" is expected to trap. */ \
if (debug) TH_LOG("traps: array[%d] = 0xFF", index); \
} else { \
if (debug) TH_LOG("ignored: array[%d] = 0xFF", index); \
} \
(p)->array[index] = 0xFF; \
if (enforcement == SHOULD_TRAP) { \
/* Don't assert: test for lack of signal. */ \
if (debug) TH_LOG("this should have been unreachable"); \
} \
} while (0)
/* Helper to hide the allocation size by using a leaf function. */
static struct flex * noinline alloc_flex(int index)
{
struct flex *f;
return malloc(sizeof(*f) + index * sizeof(*f->array));
}
/* Helper to hide the allocation size by using a leaf function. */
static struct annotated * noinline alloc_annotated(int index)
{
struct annotated *p;
p = malloc(sizeof(*p) + index * sizeof(*p->array));
p->count = index;
return p;
}
/* Helper to hide the allocation size by using a leaf function. */
static struct multi * noinline alloc_multi_ints(int index)
{
struct multi *p;
p = malloc(sizeof(*p) + index * sizeof(*p->ints));
p->count_ints = index;
return p;
}
/* Helper to hide the allocation size by using a leaf function. */
static struct multi * noinline alloc_multi_bytes(int index)
{
struct multi *p;
p = malloc(sizeof(*p) + index * sizeof(*p->bytes));
p->count_bytes = index;
return p;
}
/* Helper to hide the allocation size by using a leaf function. */
static struct anon_struct * noinline alloc_anon_struct(int index)
{
struct anon_struct *p;
p = malloc(sizeof(*p) + index * sizeof(*p->array));
p->count = index;
return p;
}
/* Helper to hide the allocation size by using a leaf function. */
static struct composite * noinline alloc_composite(int index)
{
struct composite *p;
p = malloc(sizeof(*p) + index * sizeof(*p->inner.array));
p->inner.count = index;
return p;
}
/*
* For a structure ending with a fixed-size array, sizeof(*p) should
* match __builtin_object_size(p, 1), which should also match
* __builtin_dynamic_object_size(p, 1). This should work for both the
* array itself and the object as a whole.
*/
TEST(fixed_size_seen_by_bdos)
{
struct fixed f = { };
REPORT_SIZE(f.array);
EXPECT_EQ(sizeof(f) - sizeof(f.array), offsetof(typeof(f), array));
/* Check array size alone. */
EXPECT_EQ(__builtin_object_size(f.array, 1), sizeof(f.array));
EXPECT_EQ(__builtin_dynamic_object_size(f.array, 1), sizeof(f.array));
/* Check check entire object size. */
EXPECT_EQ(__builtin_object_size(&f, 1), sizeof(f));
EXPECT_EQ(__builtin_dynamic_object_size(&f, 1), sizeof(f));
}
/*
* For a fixed-size array, the sanitizer should trap when accessing
* beyond the largest index.
*/
TEST_SIGNAL(fixed_size_enforced_by_sanitizer, SIGILL)
{
struct fixed f = { };
int index = MAX_INDEX + unconst;
REPORT_SIZE(f.array);
TEST_ACCESS(&f, array, index, SHOULD_TRAP);
}
/*
* For a structure ending with a flexible array, sizeof(*p) should
* match the offset of the flexible array. Since the size of the
* array is unknown, both __builtin_object_size(p, 1) and
* __builtin_dynamic_object_size(p, 1) should report SIZE_MAX. This
* should be true for both the array itself and the object as a whole.
*/
TEST(unknown_size_unknown_to_bdos)
{
struct flex *p;
int index = MAX_INDEX + unconst;
/* Hide actual allocation size from compiler. */
p = alloc_flex(index);
REPORT_SIZE(p->array);
EXPECT_EQ(sizeof(*p), offsetof(typeof(*p), array));
/* Check array size alone. */
EXPECT_EQ(__builtin_object_size(p->array, 1), SIZE_MAX);
EXPECT_EQ(__builtin_dynamic_object_size(p->array, 1), SIZE_MAX);
/* Check check entire object size. */
EXPECT_EQ(__builtin_object_size(p, 1), SIZE_MAX);
EXPECT_EQ(__builtin_dynamic_object_size(p, 1), SIZE_MAX);
}
/*
* For a structure ending with a flexible array, the sanitizer has
* no information about max indexes and should not trap for any
* accesses.
*/
TEST(unknown_size_ignored_by_sanitizer)
{
struct flex *p;
int index = MAX_INDEX + unconst;
/* Hide actual allocation size from compiler. */
p = alloc_flex(index);
REPORT_SIZE(p->array);
TEST_ACCESS(p, array, index, SHOULD_NOT_TRAP);
}
/*
* For a structure ending with a flexible array where the allocation
* is visible (via the __alloc_size attribute), this size should be
* visible to __builtin_dynamic_object_size(p, 1) for both the array
* itself and the object as a whole. The result of sizeof() and
* __builtin_object_size() should remain unchanged compared to the
* "unknown" cases above.
*/
TEST(alloc_size_seen_by_bdos)
{
int count = MAX_INDEX + unconst;
/* malloc() is marked with __attribute__((alloc_size(1))) */
struct flex *p = malloc(sizeof(*p) + count * sizeof(*p->array));
REPORT_SIZE(p->array);
EXPECT_EQ(sizeof(*p), offsetof(typeof(*p), array));
/* Check array size alone. */
EXPECT_EQ(__builtin_object_size(p->array, 1), SIZE_MAX);
EXPECT_EQ(__builtin_dynamic_object_size(p->array, 1), count * sizeof(*p->array));
/* Check check entire object size. */
EXPECT_EQ(__builtin_object_size(p, 1), SIZE_MAX);
EXPECT_EQ(__builtin_dynamic_object_size(p, 1), sizeof(*p) + count * sizeof(*p->array));
}
/*
* For a structure ending with a flexible array where the allocation
* is visible (via the __alloc_size attribute), the sanitizer should
* trap when accessing beyond the highest index still contained by the
* allocation.
*/
TEST_SIGNAL(alloc_size_enforced_by_sanitizer, SIGILL)
{
int count = MAX_INDEX + unconst;
/* malloc() is marked with __attribute__((alloc_size(1))) */
struct flex *p = malloc(sizeof(*p) + count * sizeof(*p->array));
REPORT_SIZE(p->array);
TEST_ACCESS(p, array, count, SHOULD_TRAP);
}
/*
* For a structure ending with a flexible array where the allocation
* is hidden, but the array size member (identified with the
* __counted_by attribute) has been set, the calculated size should
* be visible to __builtin_dynamic_object_size(p, 1) for both the array
* itself and the object as a whole. The result of sizeof() and
* __builtin_object_size() should remain unchanged compared to the
* "unknown" cases above.
*/
TEST(counted_by_seen_by_bdos)
{
struct annotated *p;
struct multi *m;
struct anon_struct *s;
struct composite *c;
int index = MAX_INDEX + unconst;
int negative = -3 + unconst;
#define CHECK(p, array, count) \
REPORT_SIZE(p->array); \
\
EXPECT_GE(sizeof(*p), offsetof(typeof(*p), array)); \
/* Check single array indexes. */ \
EXPECT_EQ(__builtin_object_size(&p->array[index - 1], 1), SIZE_MAX); \
EXPECT_EQ(__builtin_dynamic_object_size(&p->array[index - 1], 1), sizeof(*p->array)); \
EXPECT_EQ(__builtin_dynamic_object_size(&p->array[index], 1), 0); \
\
/* GCC's sanitizer trips even in __bdos */ \
/*EXPECT_EQ(__builtin_dynamic_object_size(&p->array[negative], 1), 0);*/ \
/* Check array size alone. */ \
EXPECT_EQ(__builtin_object_size(p->array, 1), SIZE_MAX); \
EXPECT_EQ(__builtin_dynamic_object_size(p->array, 1), p->count * sizeof(*p->array)); \
/* Check check entire object size. */ \
EXPECT_EQ(__builtin_object_size(p, 1), SIZE_MAX); \
/* EXPECT_EQ(__builtin_dynamic_object_size(p, 1), sizeof(*p) + p->count * sizeof(*p->array)); */ \
/* Check for out of bounds count. */ \
p->count = negative; \
EXPECT_EQ(__builtin_dynamic_object_size(p->array, 1), 0) { \
TH_LOG("Failed when " #p "->" #count "=%d", negative); \
} \
/* Check for reduced counts. */ \
p->count = 1 + unconst; \
EXPECT_EQ(__builtin_dynamic_object_size(p->array, 1), sizeof(*p->array)); \
p->count = 0 + unconst; \
EXPECT_EQ(__builtin_dynamic_object_size(p->array, 1), 0) { \
TH_LOG("Failed when count=0"); \
} \
do { } while (0)
p = alloc_annotated(index);
CHECK(p, array, count);
m = alloc_multi_ints(index);
CHECK(m, ints, count_ints);
m = alloc_multi_bytes(index);
CHECK(m, bytes, count_bytes);
s = alloc_anon_struct(index);
CHECK(s, array, count);
c = alloc_composite(index);
CHECK(c, inner.array, inner.count);
#undef CHECK
}
/*
* For a structure ending with a flexible array where the allocation
* is hidden, but the array size member (identified with the
* __counted_by attribute) has been set, the sanitizer should trap
* when accessing beyond the index stored in the counted_by member.
*/
TEST_SIGNAL(counted_by_enforced_by_sanitizer, SIGILL)
{
struct annotated *p;
int index = MAX_INDEX + unconst;
p = alloc_annotated(index);
REPORT_SIZE(p->array);
TEST_ACCESS(p, array, index, SHOULD_TRAP);
}
TEST_SIGNAL(counted_by_enforced_by_sanitizer_with_negative_index, SIGILL)
{
struct annotated *p;
int index = MAX_INDEX + unconst;
int negative_one = -1 + unconst;
p = alloc_annotated(index);
REPORT_SIZE(p->array);
TEST_ACCESS(p, array, negative_one, SHOULD_TRAP);
}
TEST_SIGNAL(counted_by_enforced_by_sanitizer_with_negative_bounds, SIGILL)
{
struct annotated *p;
int index = MAX_INDEX + unconst;
int negative_one = -1 + unconst;
p = alloc_annotated(index);
REPORT_SIZE(p->array);
TEST_ACCESS(p, array, index - 1, SHOULD_NOT_TRAP);
p->count = negative_one;
if (debug) TH_LOG("traps: array[%d] = 0xEE (when count == -1)", index - 1);
p->array[index - 1] = 0xEE;
#ifdef __clang__
XFAIL(return, "Clang doesn't pass this yet");
#endif
TH_LOG("this should have been unreachable");
}
TEST_SIGNAL(counted_by_enforced_by_sanitizer_multi_ints, SIGILL)
{
struct multi *m;
int index = MAX_INDEX + unconst;
m = alloc_multi_ints(index);
REPORT_SIZE(m->ints);
TEST_ACCESS(m, ints, index, SHOULD_TRAP);
}
TEST_SIGNAL(counted_by_enforced_by_sanitizer_multi_bytes, SIGILL)
{
struct multi *m;
int index = MAX_INDEX + unconst;
m = alloc_multi_bytes(index);
REPORT_SIZE(m->bytes);
TEST_ACCESS(m, bytes, index, SHOULD_TRAP);
}
TEST_SIGNAL(counted_by_enforced_by_sanitizer_anon_struct, SIGILL)
{
struct anon_struct *s;
int index = MAX_INDEX + unconst;
s = alloc_anon_struct(index);
REPORT_SIZE(s->array);
TEST_ACCESS(s, array, index, SHOULD_TRAP);
}
TEST_SIGNAL(counted_by_enforced_by_sanitizer_composite, SIGILL)
{
struct composite *c;
int index = MAX_INDEX + unconst;
c = alloc_composite(index);
REPORT_SIZE(c->inner.array);
TEST_ACCESS(c, inner.array, index, SHOULD_TRAP);
}
/*
* When both __alloc_size and __counted_by are available to calculate
* sizes, the smaller of the two should take precedence. Check that when
* the visible allocation is larger than the size calculated from the
* counted_by member's value, __builtin_dynamic_object_size(p, 1) sees
* the smaller of the two (the allocation), with everything else unchanged.
*/
TEST(alloc_size_with_smaller_counted_by_seen_by_bdos)
{
int count = MAX_INDEX + unconst;
/* malloc() is marked with __attribute__((alloc_size(1))) */
struct annotated *p = malloc(sizeof(*p) + (count + SIZE_BUMP) * sizeof(*p->array));
p->count = count;
REPORT_SIZE(p->array);
EXPECT_EQ(sizeof(*p), offsetof(typeof(*p), array));
/* Check array size alone. */
EXPECT_EQ(__builtin_object_size(p->array, 1), SIZE_MAX);
EXPECT_EQ(__builtin_dynamic_object_size(p->array, 1), p->count * sizeof(*p->array));
/* Check check entire object size: this is not limited by __counted_by. */
EXPECT_EQ(__builtin_object_size(p, 1), SIZE_MAX);
EXPECT_EQ(__builtin_dynamic_object_size(p, 1), sizeof(*p) + (p->count + SIZE_BUMP) * sizeof(*p->array));
#ifdef __clang__
/* This fails on Clang currently due to sub-object __bdos() bug. */
if (_metadata->passed)
TH_LOG("Unexpected pass!");
else
XFAIL(return, "Clang doesn't pass this yet");
#endif
}
/*
* When both __alloc_size and __counted_by are available to calculate
* sizes, the smaller of the two should take precedence. Check that when
* the visible allocation is larger than the size calculated from the
* counted_by member's value, the sanitizer should trap when accessing
* beyond the index stored in the counted_by member.
*/
TEST_SIGNAL(alloc_size_with_smaller_counted_by_enforced_by_sanitizer, SIGILL)
{
int count = MAX_INDEX + unconst;
/* malloc() is marked with __attribute__((alloc_size(1))) */
struct annotated *p = malloc(sizeof(*p) + (count + SIZE_BUMP) * sizeof(*p->array));
p->count = count;
REPORT_SIZE(p->array);
TEST_ACCESS(p, array, count, SHOULD_TRAP);
}
/*
* When both __alloc_size and __counted_by are available to calculate
* sizes, the smaller of the two should take precedence. Check that when
* the visible allocation that is smaller than the size calculated from
* the counted_by member's value, __builtin_dynamic_object_size(p, 1)
* sees the smaller of the two (the counted_by member size calculation),
* with everything else unchanged.
*/
TEST(alloc_size_with_bigger_counted_by_seen_by_bdos)
{
int count = MAX_INDEX + unconst;
/* malloc() is marked with __attribute__((alloc_size(1))) */
struct annotated *p = malloc(sizeof(*p) + count * sizeof(*p->array));
p->count = count + SIZE_BUMP;
REPORT_SIZE(p->array);
EXPECT_EQ(sizeof(*p), offsetof(typeof(*p), array));
/* Check array size alone. */
EXPECT_EQ(__builtin_object_size(p->array, 1), SIZE_MAX);
EXPECT_EQ(__builtin_dynamic_object_size(p->array, 1), (p->count - SIZE_BUMP) * sizeof(*p->array));
/* Check check entire object size. */
EXPECT_EQ(__builtin_object_size(p, 1), SIZE_MAX);
EXPECT_EQ(__builtin_dynamic_object_size(p, 1), sizeof(*p) + (p->count - SIZE_BUMP) * sizeof(*p->array));
/*
* Both Clang and GCC prioritize counted_by above alloc_size.
* If this is actually a bug, we'll need to very clearly define
* how alloc_size and counted_by interact.
*/
if (_metadata->passed)
TH_LOG("Unexpected pass!");
else
XFAIL(/* */, "Not expected yet for GCC nor Clang");
}
/*
* When both __alloc_size and __counted_by are available to calculate
* sizes, the smaller of the two should take precedence. Check that when
* the visible allocation is smaller than the size calculated from the
* counted_by member's value, the sanitizer should trap when accessing
* beyond the highest index still contained by the allocation.
*/
TEST_SIGNAL(alloc_size_with_bigger_counted_by_enforced_by_sanitizer, SIGILL)
{
int count = MAX_INDEX + unconst;
/* malloc() is marked with __attribute__((alloc_size(1))) */
struct annotated *p = malloc(sizeof(*p) + count * sizeof(*p->array));
p->count = count + SIZE_BUMP;
REPORT_SIZE(p->array);
TEST_ACCESS(p, array, count, SHOULD_TRAP);
#if !defined(__clang__)
XFAIL(return, "Not expected yet for GCC");
#endif
TH_LOG("this should have been unreachable");
}
/*
* When both __alloc_size and __counted_by are available to calculate
* sizes, both GCC and Clang agree about taking alloc_size on an outer
* variable. Test that this is distinguished.
*/
TEST_SIGNAL(alloc_size_outside_counted_by, SIGILL)
{
int count = MAX_INDEX + unconst;
struct annotated *p;
size_t bytes = sizeof(*p) + (count + SIZE_BUMP) * sizeof(*p->array);
void *whole;
u8 *b;
/* malloc() is marked with __attribute__((alloc_size(1))) */
p = whole = malloc(bytes);
p->count = count;
b = whole;
REPORT_SIZE(p->array);
TEST_ACCESS(p, array, count - 1, SHOULD_NOT_TRAP);
/* Check sizes */
EXPECT_EQ(sizeof(*p), offsetof(typeof(*p), array));
/* Check array size alone. */
EXPECT_EQ(__builtin_object_size(p->array, 1), SIZE_MAX);
EXPECT_EQ(__builtin_dynamic_object_size(p->array, 1), p->count * sizeof(*p->array));
/* Check check entire object size. */
EXPECT_EQ(__builtin_object_size(p, 1), SIZE_MAX);
EXPECT_EQ(__builtin_dynamic_object_size(p, 1), sizeof(*p) + p->count * sizeof(*p->array));
/* Check whole allocation object size. */
EXPECT_EQ(__builtin_object_size(b, 1), SIZE_MAX);
EXPECT_EQ(__builtin_dynamic_object_size(b, 1), bytes);
/* Verify sanitizer */
memset(b, 0xAA, bytes);
EXPECT_EQ(b[bytes - 1], 0xAA);
/* This should trap. */
EXPECT_NE(b[bytes], 0xAA);
TH_LOG("this should have been unreachable");
}
#if 0
/*
* It would be nice to have a way to verify expected compile time failures.
* For example, we want this warning:
*
* array-bounds.c: In function 'invalid_assignment_order':
* array-bounds.c:366:17: warning: '*p.count' is used uninitialized [-Wuninitialized]
* 366 | p->array[0] = 0;
* | ~~~~~~~~^~~
*/
TEST(invalid_assignment_order)
{
int count = MAX_INDEX + unconst;
struct annotated *p = malloc(sizeof(*p) + count * sizeof(*p->array));
/* It should be a build error to access "array" before "count" is set. */
p->array[0] = 0;
p->count = 1;
}
#endif
TEST_HARNESS_MAIN