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
|
/* Copyright (C) 2004, 2005 Free Software Foundation.
Ensure builtin __memcpy_chk performs correctly. */
extern void abort (void);
typedef __SIZE_TYPE__ size_t;
extern size_t strlen(const char *);
extern void *memcpy (void *, const void *, size_t);
extern int memcmp (const void *, const void *, size_t);
#include "chk.h"
const char s1[] = "123";
char p[32] = "";
volatile char *s2 = "defg"; /* prevent constant propagation to happen when whole program assumptions are made. */
volatile char *s3 = "FGH"; /* prevent constant propagation to happen when whole program assumptions are made. */
volatile size_t l1 = 1; /* prevent constant propagation to happen when whole program assumptions are made. */
void
__attribute__((noinline))
test1 (void)
{
int i;
#if defined __i386__ || defined __x86_64__
/* The functions below might not be optimized into direct stores on all
arches. It depends on how many instructions would be generated and
what limits the architecture chooses in STORE_BY_PIECES_P. */
memcpy_disallowed = 1;
#endif
/* All the memcpy calls in this routine except last have fixed length, so
object size checking should be done at compile time if optimizing. */
chk_calls = 0;
if (memcpy (p, "ABCDE", 6) != p || memcmp (p, "ABCDE", 6))
abort ();
if (memcpy (p + 16, "VWX" + 1, 2) != p + 16
|| memcmp (p + 16, "WX\0\0", 5))
abort ();
if (memcpy (p + 1, "", 1) != p + 1 || memcmp (p, "A\0CDE", 6))
abort ();
if (memcpy (p + 3, "FGHI", 4) != p + 3 || memcmp (p, "A\0CFGHI", 8))
abort ();
i = 8;
memcpy (p + 20, "qrstu", 6);
memcpy (p + 25, "QRSTU", 6);
if (memcpy (p + 25 + 1, s1, 3) != p + 25 + 1
|| memcmp (p + 25, "Q123U", 6))
abort ();
if (memcpy (memcpy (p, "abcdEFG", 4) + 4, "efg", 4) != p + 4
|| memcmp (p, "abcdefg", 8))
abort();
/* Test at least one instance of the __builtin_ style. We do this
to ensure that it works and that the prototype is correct. */
if (__builtin_memcpy (p, "ABCDE", 6) != p || memcmp (p, "ABCDE", 6))
abort ();
memcpy (p + 5, s3, 1);
if (memcmp (p, "ABCDEFg", 8))
abort ();
memcpy_disallowed = 0;
if (chk_calls)
abort ();
chk_calls = 0;
memcpy (p + 6, s1 + 1, l1);
if (memcmp (p, "ABCDEF2", 8))
abort ();
/* The above memcpy copies into an object with known size, but
unknown length, so it should be a __memcpy_chk call. */
if (chk_calls != 1)
abort ();
}
long buf1[64];
char *buf2 = (char *) (buf1 + 32);
long buf5[20];
char buf7[20];
void
__attribute__((noinline))
test2_sub (long *buf3, char *buf4, char *buf6, int n)
{
int i = 0;
/* All the memcpy/__builtin_memcpy/__builtin___memcpy_chk
calls in this routine are either fixed length, or have
side-effects in __builtin_object_size arguments, or
dst doesn't point into a known object. */
chk_calls = 0;
/* These should probably be handled by store_by_pieces on most arches. */
if (memcpy (buf1, "ABCDEFGHI", 9) != (char *) buf1
|| memcmp (buf1, "ABCDEFGHI\0", 11))
abort ();
if (memcpy (buf1, "abcdefghijklmnopq", 17) != (char *) buf1
|| memcmp (buf1, "abcdefghijklmnopq\0", 19))
abort ();
if (__builtin_memcpy (buf3, "ABCDEF", 6) != (char *) buf1
|| memcmp (buf1, "ABCDEFghijklmnopq\0", 19))
abort ();
if (__builtin_memcpy (buf3, "a", 1) != (char *) buf1
|| memcmp (buf1, "aBCDEFghijklmnopq\0", 19))
abort ();
if (memcpy ((char *) buf3 + 2, "bcd" + ++i, 2) != (char *) buf1 + 2
|| memcmp (buf1, "aBcdEFghijklmnopq\0", 19)
|| i != 1)
abort ();
/* These should probably be handled by move_by_pieces on most arches. */
if (memcpy ((char *) buf3 + 4, buf5, 6) != (char *) buf1 + 4
|| memcmp (buf1, "aBcdRSTUVWklmnopq\0", 19))
abort ();
if (__builtin_memcpy ((char *) buf1 + ++i + 8, (char *) buf5 + 1, 1)
!= (char *) buf1 + 10
|| memcmp (buf1, "aBcdRSTUVWSlmnopq\0", 19)
|| i != 2)
abort ();
if (memcpy ((char *) buf3 + 14, buf6, 2) != (char *) buf1 + 14
|| memcmp (buf1, "aBcdRSTUVWSlmnrsq\0", 19))
abort ();
if (memcpy (buf3, buf5, 8) != (char *) buf1
|| memcmp (buf1, "RSTUVWXYVWSlmnrsq\0", 19))
abort ();
if (memcpy (buf3, buf5, 17) != (char *) buf1
|| memcmp (buf1, "RSTUVWXYZ01234567\0", 19))
abort ();
__builtin_memcpy (buf3, "aBcdEFghijklmnopq\0", 19);
/* These should be handled either by movmemendM or memcpy
call. */
/* buf3 points to an unknown object, so __memcpy_chk should not be done. */
if (memcpy ((char *) buf3 + 4, buf5, n + 6) != (char *) buf1 + 4
|| memcmp (buf1, "aBcdRSTUVWklmnopq\0", 19))
abort ();
/* This call has side-effects in dst, therefore no checking. */
if (__builtin___memcpy_chk ((char *) buf1 + ++i + 8, (char *) buf5 + 1,
n + 1, os ((char *) buf1 + ++i + 8))
!= (char *) buf1 + 11
|| memcmp (buf1, "aBcdRSTUVWkSmnopq\0", 19)
|| i != 3)
abort ();
if (memcpy ((char *) buf3 + 14, buf6, n + 2) != (char *) buf1 + 14
|| memcmp (buf1, "aBcdRSTUVWkSmnrsq\0", 19))
abort ();
i = 1;
/* These might be handled by store_by_pieces. */
if (memcpy (buf2, "ABCDEFGHI", 9) != buf2
|| memcmp (buf2, "ABCDEFGHI\0", 11))
abort ();
if (memcpy (buf2, "abcdefghijklmnopq", 17) != buf2
|| memcmp (buf2, "abcdefghijklmnopq\0", 19))
abort ();
if (__builtin_memcpy (buf4, "ABCDEF", 6) != buf2
|| memcmp (buf2, "ABCDEFghijklmnopq\0", 19))
abort ();
if (__builtin_memcpy (buf4, "a", 1) != buf2
|| memcmp (buf2, "aBCDEFghijklmnopq\0", 19))
abort ();
if (memcpy (buf4 + 2, "bcd" + i++, 2) != buf2 + 2
|| memcmp (buf2, "aBcdEFghijklmnopq\0", 19)
|| i != 2)
abort ();
/* These might be handled by move_by_pieces. */
if (memcpy (buf4 + 4, buf7, 6) != buf2 + 4
|| memcmp (buf2, "aBcdRSTUVWklmnopq\0", 19))
abort ();
/* Side effect. */
if (__builtin___memcpy_chk (buf2 + i++ + 8, buf7 + 1, 1,
os (buf2 + i++ + 8))
!= buf2 + 10
|| memcmp (buf2, "aBcdRSTUVWSlmnopq\0", 19)
|| i != 3)
abort ();
if (memcpy (buf4 + 14, buf6, 2) != buf2 + 14
|| memcmp (buf2, "aBcdRSTUVWSlmnrsq\0", 19))
abort ();
__builtin_memcpy (buf4, "aBcdEFghijklmnopq\0", 19);
/* These should be handled either by movmemendM or memcpy
call. */
if (memcpy (buf4 + 4, buf7, n + 6) != buf2 + 4
|| memcmp (buf2, "aBcdRSTUVWklmnopq\0", 19))
abort ();
/* Side effect. */
if (__builtin___memcpy_chk (buf2 + i++ + 8, buf7 + 1, n + 1,
os (buf2 + i++ + 8))
!= buf2 + 11
|| memcmp (buf2, "aBcdRSTUVWkSmnopq\0", 19)
|| i != 4)
abort ();
if (memcpy (buf4 + 14, buf6, n + 2) != buf2 + 14
|| memcmp (buf2, "aBcdRSTUVWkSmnrsq\0", 19))
abort ();
if (chk_calls)
abort ();
}
void
__attribute__((noinline))
test2 (void)
{
long *x;
char *y;
int z;
__builtin_memcpy (buf5, "RSTUVWXYZ0123456789", 20);
__builtin_memcpy (buf7, "RSTUVWXYZ0123456789", 20);
__asm ("" : "=r" (x) : "0" (buf1));
__asm ("" : "=r" (y) : "0" (buf2));
__asm ("" : "=r" (z) : "0" (0));
test2_sub (x, y, "rstuvwxyz", z);
}
/* Test whether compile time checking is done where it should
and so is runtime object size checking. */
void
__attribute__((noinline))
test3 (void)
{
struct A { char buf1[10]; char buf2[10]; } a;
char *r = l1 == 1 ? &a.buf1[5] : &a.buf2[4];
char buf3[20];
int i;
size_t l;
/* The following calls should do runtime checking
- length is not known, but destination is. */
chk_calls = 0;
memcpy (a.buf1 + 2, s3, l1);
memcpy (r, s3, l1 + 1);
r = l1 == 1 ? __builtin_alloca (4) : &a.buf2[7];
memcpy (r, s2, l1 + 2);
memcpy (r + 2, s3, l1);
r = buf3;
for (i = 0; i < 4; ++i)
{
if (i == l1 - 1)
r = &a.buf1[1];
else if (i == l1)
r = &a.buf2[7];
else if (i == l1 + 1)
r = &buf3[5];
else if (i == l1 + 2)
r = &a.buf1[9];
}
memcpy (r, s2, l1);
if (chk_calls != 5)
abort ();
/* Following have known destination and known length,
so if optimizing certainly shouldn't result in the checking
variants. */
chk_calls = 0;
memcpy (a.buf1 + 2, s3, 1);
memcpy (r, s3, 2);
r = l1 == 1 ? __builtin_alloca (4) : &a.buf2[7];
memcpy (r, s2, 3);
r = buf3;
l = 4;
for (i = 0; i < 4; ++i)
{
if (i == l1 - 1)
r = &a.buf1[1], l = 2;
else if (i == l1)
r = &a.buf2[7], l = 3;
else if (i == l1 + 1)
r = &buf3[5], l = 4;
else if (i == l1 + 2)
r = &a.buf1[9], l = 1;
}
memcpy (r, s2, 1);
/* Here, l is known to be at most 4 and __builtin_object_size (&buf3[16], 0)
is 4, so this doesn't need runtime checking. */
memcpy (&buf3[16], s2, l);
if (chk_calls)
abort ();
chk_calls = 0;
}
/* Test whether runtime and/or compile time checking catches
buffer overflows. */
void
__attribute__((noinline))
test4 (void)
{
struct A { char buf1[10]; char buf2[10]; } a;
char buf3[20];
chk_fail_allowed = 1;
/* Runtime checks. */
if (__builtin_setjmp (chk_fail_buf) == 0)
{
memcpy (&a.buf2[9], s2, l1 + 1);
abort ();
}
if (__builtin_setjmp (chk_fail_buf) == 0)
{
memcpy (&a.buf2[7], s3, strlen (s3) + 1);
abort ();
}
/* This should be detectable at compile time already. */
if (__builtin_setjmp (chk_fail_buf) == 0)
{
memcpy (&buf3[19], "ab", 2);
abort ();
}
chk_fail_allowed = 0;
}
#ifndef MAX_OFFSET
#define MAX_OFFSET (sizeof (long long))
#endif
#ifndef MAX_COPY
#define MAX_COPY (10 * sizeof (long long))
#endif
#ifndef MAX_EXTRA
#define MAX_EXTRA (sizeof (long long))
#endif
#define MAX_LENGTH (MAX_OFFSET + MAX_COPY + MAX_EXTRA)
/* Use a sequence length that is not divisible by two, to make it more
likely to detect when words are mixed up. */
#define SEQUENCE_LENGTH 31
static union {
char buf[MAX_LENGTH];
long long align_int;
long double align_fp;
} u1, u2;
void
__attribute__((noinline))
test5 (void)
{
int off1, off2, len, i;
char *p, *q, c;
for (off1 = 0; off1 < MAX_OFFSET; off1++)
for (off2 = 0; off2 < MAX_OFFSET; off2++)
for (len = 1; len < MAX_COPY; len++)
{
for (i = 0, c = 'A'; i < MAX_LENGTH; i++, c++)
{
u1.buf[i] = 'a';
if (c >= 'A' + SEQUENCE_LENGTH)
c = 'A';
u2.buf[i] = c;
}
p = memcpy (u1.buf + off1, u2.buf + off2, len);
if (p != u1.buf + off1)
abort ();
q = u1.buf;
for (i = 0; i < off1; i++, q++)
if (*q != 'a')
abort ();
for (i = 0, c = 'A' + off2; i < len; i++, q++, c++)
{
if (c >= 'A' + SEQUENCE_LENGTH)
c = 'A';
if (*q != c)
abort ();
}
for (i = 0; i < MAX_EXTRA; i++, q++)
if (*q != 'a')
abort ();
}
}
#define TESTSIZE 80
char srcb[TESTSIZE] __attribute__ ((aligned));
char dstb[TESTSIZE] __attribute__ ((aligned));
void
__attribute__((noinline))
check (char *test, char *match, int n)
{
if (memcmp (test, match, n))
abort ();
}
#define TN(n) \
{ memset (dstb, 0, n); memcpy (dstb, srcb, n); check (dstb, srcb, n); }
#define T(n) \
TN (n) \
TN ((n) + 1) \
TN ((n) + 2) \
TN ((n) + 3)
void
__attribute__((noinline))
test6 (void)
{
int i;
chk_calls = 0;
for (i = 0; i < sizeof (srcb); ++i)
srcb[i] = 'a' + i % 26;
T (0);
T (4);
T (8);
T (12);
T (16);
T (20);
T (24);
T (28);
T (32);
T (36);
T (40);
T (44);
T (48);
T (52);
T (56);
T (60);
T (64);
T (68);
T (72);
T (76);
/* All memcpy calls in this routine have constant arguments. */
if (chk_calls)
abort ();
}
void
main_test (void)
{
#ifndef __OPTIMIZE__
/* Object size checking is only intended for -O[s123]. */
return;
#endif
__asm ("" : "=r" (l1) : "0" (l1));
test1 ();
test2 ();
test3 ();
test4 ();
test5 ();
test6 ();
}
|