summaryrefslogtreecommitdiff
path: root/malloc/tst-alloc_buffer.c
blob: 9b2bd2046a12c0f2e19f006009174cc74c4c0e08 (plain)
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
/* Tests for struct alloc_buffer.
   Copyright (C) 2017 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */

#include <arpa/inet.h>
#include <alloc_buffer.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <support/check.h>
#include <support/support.h>
#include <support/test-driver.h>

/* Return true if PTR is sufficiently aligned for TYPE.  */
#define IS_ALIGNED(ptr, type) \
  ((((uintptr_t) ptr) & (__alloc_buffer_assert_align (__alignof (type)) - 1)) \
   == 0)

/* Structure with non-power-of-two size.  */
struct twelve
{
  uint32_t buffer[3] __attribute__ ((aligned (4)));
};
_Static_assert (sizeof (struct twelve) == 12, "struct twelve");
_Static_assert (__alignof__ (struct twelve) == 4, "struct twelve");

/* Check for success obtaining empty arrays.  Does not assume the
   buffer is empty.  */
static void
test_empty_array (struct alloc_buffer refbuf)
{
  bool refbuf_failed = alloc_buffer_has_failed (&refbuf);
  if (test_verbose)
    printf ("info: %s: current=0x%llx end=0x%llx refbuf_failed=%d\n",
            __func__, (unsigned long long) refbuf.__alloc_buffer_current,
            (unsigned long long) refbuf.__alloc_buffer_end, refbuf_failed);
  {
    struct alloc_buffer buf = refbuf;
    TEST_VERIFY ((alloc_buffer_alloc_bytes (&buf, 0) == NULL)
                 == refbuf_failed);
    TEST_VERIFY (alloc_buffer_has_failed (&buf) == refbuf_failed);
  }
  {
    struct alloc_buffer buf = refbuf;
    TEST_VERIFY ((alloc_buffer_alloc_array (&buf, char, 0) == NULL)
                 == refbuf_failed);
    TEST_VERIFY (alloc_buffer_has_failed (&buf) == refbuf_failed);
  }
  /* The following tests can fail due to the need for aligning the
     returned pointer.  */
  {
    struct alloc_buffer buf = refbuf;
    bool expect_failure = refbuf_failed
      || !IS_ALIGNED (alloc_buffer_next (&buf, void), double);
    double *ptr = alloc_buffer_alloc_array (&buf, double, 0);
    TEST_VERIFY (IS_ALIGNED (ptr, double));
    TEST_VERIFY ((ptr == NULL) == expect_failure);
    TEST_VERIFY (alloc_buffer_has_failed (&buf) == expect_failure);
  }
  {
    struct alloc_buffer buf = refbuf;
    bool expect_failure = refbuf_failed
      || !IS_ALIGNED (alloc_buffer_next (&buf, void), struct twelve);
    struct twelve *ptr = alloc_buffer_alloc_array (&buf, struct twelve, 0);
    TEST_VERIFY (IS_ALIGNED (ptr, struct twelve));
    TEST_VERIFY ((ptr == NULL) == expect_failure);
    TEST_VERIFY (alloc_buffer_has_failed (&buf) == expect_failure);
  }
}

/* Test allocation of impossibly large arrays.  */
static void
test_impossible_array (struct alloc_buffer refbuf)
{
  if (test_verbose)
    printf ("info: %s: current=0x%llx end=0x%llx\n",
            __func__, (unsigned long long) refbuf.__alloc_buffer_current,
            (unsigned long long) refbuf.__alloc_buffer_end);
  static const size_t counts[] =
    { SIZE_MAX, SIZE_MAX - 1, SIZE_MAX - 2, SIZE_MAX - 3, SIZE_MAX - 4,
      SIZE_MAX / 2, SIZE_MAX / 2 + 1, SIZE_MAX / 2 - 1, 0};

  for (int i = 0; counts[i] != 0; ++i)
    {
      size_t count = counts[i];
      if (test_verbose)
        printf ("info: %s: count=%zu\n", __func__, count);
      {
        struct alloc_buffer buf = refbuf;
        TEST_VERIFY (alloc_buffer_alloc_bytes (&buf, count) == NULL);
        TEST_VERIFY (alloc_buffer_has_failed (&buf));
      }
      {
        struct alloc_buffer buf = refbuf;
        TEST_VERIFY (alloc_buffer_alloc_array (&buf, char, count) == NULL);
        TEST_VERIFY (alloc_buffer_has_failed (&buf));
      }
      {
        struct alloc_buffer buf = refbuf;
        TEST_VERIFY (alloc_buffer_alloc_array (&buf, short, count) == NULL);
        TEST_VERIFY (alloc_buffer_has_failed (&buf));
      }
      {
        struct alloc_buffer buf = refbuf;
        TEST_VERIFY (alloc_buffer_alloc_array (&buf, double, count) == NULL);
        TEST_VERIFY (alloc_buffer_has_failed (&buf));
      }
      {
        struct alloc_buffer buf = refbuf;
        TEST_VERIFY (alloc_buffer_alloc_array (&buf, struct twelve, count)
                     == NULL);
        TEST_VERIFY (alloc_buffer_has_failed (&buf));
      }
    }
}

/* Check for failure to obtain anything from a failed buffer.  */
static void
test_after_failure (struct alloc_buffer refbuf)
{
  if (test_verbose)
    printf ("info: %s: current=0x%llx end=0x%llx\n",
            __func__, (unsigned long long) refbuf.__alloc_buffer_current,
            (unsigned long long) refbuf.__alloc_buffer_end);
  TEST_VERIFY (alloc_buffer_has_failed (&refbuf));
  {
    struct alloc_buffer buf = refbuf;
    alloc_buffer_add_byte (&buf, 17);
    TEST_VERIFY (alloc_buffer_has_failed (&buf));
  }
  {
    struct alloc_buffer buf = refbuf;
    TEST_VERIFY (alloc_buffer_alloc (&buf, char) == NULL);
    TEST_VERIFY (alloc_buffer_has_failed (&buf));
  }
  {
    struct alloc_buffer buf = refbuf;
    TEST_VERIFY (alloc_buffer_alloc (&buf, double) == NULL);
    TEST_VERIFY (alloc_buffer_has_failed (&buf));
  }
  {
    struct alloc_buffer buf = refbuf;
    TEST_VERIFY (alloc_buffer_alloc (&buf, struct twelve) == NULL);
    TEST_VERIFY (alloc_buffer_has_failed (&buf));
  }

  test_impossible_array (refbuf);
  for (int count = 0; count <= 4; ++count)
    {
      {
        struct alloc_buffer buf = refbuf;
        TEST_VERIFY (alloc_buffer_alloc_bytes (&buf, count) == NULL);
        TEST_VERIFY (alloc_buffer_has_failed (&buf));
      }
      {
        struct alloc_buffer buf = refbuf;
        TEST_VERIFY (alloc_buffer_alloc_array (&buf, char, count) == NULL);
        TEST_VERIFY (alloc_buffer_has_failed (&buf));
      }
      {
        struct alloc_buffer buf = refbuf;
        TEST_VERIFY (alloc_buffer_alloc_array (&buf, double, count) == NULL);
        TEST_VERIFY (alloc_buffer_has_failed (&buf));
      }
      {
        struct alloc_buffer buf = refbuf;
        TEST_VERIFY (alloc_buffer_alloc_array (&buf, struct twelve, count)
                     == NULL);
        TEST_VERIFY (alloc_buffer_has_failed (&buf));
      }
    }
}

static void
test_empty (struct alloc_buffer refbuf)
{
  TEST_VERIFY (alloc_buffer_size (&refbuf) == 0);
  if (alloc_buffer_next (&refbuf, void) != NULL)
    TEST_VERIFY (!alloc_buffer_has_failed (&refbuf));
  test_empty_array (refbuf);
  test_impossible_array (refbuf);

  /* Failure to obtain non-empty objects.  */
  {
    struct alloc_buffer buf = refbuf;
    alloc_buffer_add_byte (&buf, 17);
    test_after_failure (buf);
  }
  {
    struct alloc_buffer buf = refbuf;
    TEST_VERIFY (alloc_buffer_alloc (&buf, char) == NULL);
    test_after_failure (buf);
  }
  {
    struct alloc_buffer buf = refbuf;
    TEST_VERIFY (alloc_buffer_alloc (&buf, double) == NULL);
    test_after_failure (buf);
  }
  {
    struct alloc_buffer buf = refbuf;
    TEST_VERIFY (alloc_buffer_alloc (&buf, struct twelve) == NULL);
    test_after_failure (buf);
  }
  {
    struct alloc_buffer buf = refbuf;
    TEST_VERIFY (alloc_buffer_alloc_array (&buf, char, 1) == NULL);
    test_after_failure (buf);
  }
  {
    struct alloc_buffer buf = refbuf;
    TEST_VERIFY (alloc_buffer_alloc_array (&buf, double, 1) == NULL);
    test_after_failure (buf);
  }
  {
    struct alloc_buffer buf = refbuf;
    TEST_VERIFY (alloc_buffer_alloc_array (&buf, struct twelve, 1) == NULL);
    test_after_failure (buf);
  }
}

static void
test_size_1 (struct alloc_buffer refbuf)
{
  TEST_VERIFY (!alloc_buffer_has_failed (&refbuf));
  TEST_VERIFY (alloc_buffer_size (&refbuf) == 1);
  test_empty_array (refbuf);
  test_impossible_array (refbuf);

  /* Success adding a single byte.  */
  {
    struct alloc_buffer buf = refbuf;
    alloc_buffer_add_byte (&buf, 17);
    TEST_VERIFY (!alloc_buffer_has_failed (&buf));
    test_empty (buf);
  }
  TEST_VERIFY (memcmp (alloc_buffer_next (&refbuf, void), "\x11", 1) == 0);
  {
    struct alloc_buffer buf = refbuf;
    signed char *ptr = alloc_buffer_alloc (&buf, signed char);
    TEST_VERIFY_EXIT (ptr != NULL);
    TEST_VERIFY (!alloc_buffer_has_failed (&buf));
    *ptr = 126;
    test_empty (buf);
  }
  TEST_VERIFY (memcmp (alloc_buffer_next (&refbuf, void), "\176", 1) == 0);
  {
    struct alloc_buffer buf = refbuf;
    char *ptr = alloc_buffer_alloc_array (&buf, char, 1);
    TEST_VERIFY_EXIT (ptr != NULL);
    TEST_VERIFY (!alloc_buffer_has_failed (&buf));
    *ptr = (char) 253;
    test_empty (buf);
  }
  TEST_VERIFY (memcmp (alloc_buffer_next (&refbuf, void), "\xfd", 1) == 0);

  /* Failure with larger objects.  */
  {
    struct alloc_buffer buf = refbuf;
    TEST_VERIFY (alloc_buffer_alloc (&buf, short) == NULL);
    test_after_failure (buf);
  }
  {
    struct alloc_buffer buf = refbuf;
    TEST_VERIFY (alloc_buffer_alloc (&buf, double) == NULL);
    test_after_failure (buf);
  }
  {
    struct alloc_buffer buf = refbuf;
    TEST_VERIFY (alloc_buffer_alloc (&buf, struct twelve) == NULL);
    test_after_failure (buf);
  }
  {
    struct alloc_buffer buf = refbuf;
    TEST_VERIFY (alloc_buffer_alloc_array (&buf, short, 1) == NULL);
    test_after_failure (buf);
  }
  {
    struct alloc_buffer buf = refbuf;
    TEST_VERIFY (alloc_buffer_alloc_array (&buf, double, 1) == NULL);
    test_after_failure (buf);
  }
  {
    struct alloc_buffer buf = refbuf;
    TEST_VERIFY (alloc_buffer_alloc_array (&buf, struct twelve, 1) == NULL);
    test_after_failure (buf);
  }
}

static void
test_size_2 (struct alloc_buffer refbuf)
{
  TEST_VERIFY (!alloc_buffer_has_failed (&refbuf));
  TEST_VERIFY (alloc_buffer_size (&refbuf) == 2);
  TEST_VERIFY (IS_ALIGNED (alloc_buffer_next (&refbuf, void), short));
  test_empty_array (refbuf);
  test_impossible_array (refbuf);

  /* Success adding two bytes.  */
  {
    struct alloc_buffer buf = refbuf;
    alloc_buffer_add_byte (&buf, '@');
    TEST_VERIFY (!alloc_buffer_has_failed (&buf));
    test_size_1 (buf);
  }
  TEST_VERIFY (memcmp (alloc_buffer_next (&refbuf, void), "@\xfd", 2) == 0);
  {
    struct alloc_buffer buf = refbuf;
    signed char *ptr = alloc_buffer_alloc (&buf, signed char);
    TEST_VERIFY_EXIT (ptr != NULL);
    TEST_VERIFY (!alloc_buffer_has_failed (&buf));
    *ptr = 'A';
    test_size_1 (buf);
  }
  TEST_VERIFY (memcmp (alloc_buffer_next (&refbuf, void), "A\xfd", 2) == 0);
  {
    struct alloc_buffer buf = refbuf;
    char *ptr = alloc_buffer_alloc_array (&buf, char, 1);
    TEST_VERIFY_EXIT (ptr != NULL);
    TEST_VERIFY (!alloc_buffer_has_failed (&buf));
    *ptr = 'B';
    test_size_1 (buf);
  }
  TEST_VERIFY (memcmp (alloc_buffer_next (&refbuf, void), "B\xfd", 2) == 0);
  {
    struct alloc_buffer buf = refbuf;
    unsigned short *ptr = alloc_buffer_alloc (&buf, unsigned short);
    TEST_VERIFY_EXIT (ptr != NULL);
    TEST_VERIFY (IS_ALIGNED (ptr, unsigned short));
    TEST_VERIFY (!alloc_buffer_has_failed (&buf));
    *ptr = htons (0x12f4);
    test_empty (buf);
  }
  TEST_VERIFY (memcmp (alloc_buffer_next (&refbuf, void), "\x12\xf4", 2) == 0);
  {
    struct alloc_buffer buf = refbuf;
    unsigned short *ptr = alloc_buffer_alloc_array (&buf, unsigned short, 1);
    TEST_VERIFY_EXIT (ptr != NULL);
    TEST_VERIFY (IS_ALIGNED (ptr, unsigned short));
    TEST_VERIFY (!alloc_buffer_has_failed (&buf));
    *ptr = htons (0x13f5);
    test_empty (buf);
  }
  TEST_VERIFY (memcmp (alloc_buffer_next (&refbuf, void), "\x13\xf5", 2) == 0);
  {
    struct alloc_buffer buf = refbuf;
    char *ptr = alloc_buffer_alloc_array (&buf, char, 2);
    TEST_VERIFY_EXIT (ptr != NULL);
    TEST_VERIFY (!alloc_buffer_has_failed (&buf));
    memcpy (ptr, "12", 2);
    test_empty (buf);
  }
  TEST_VERIFY (memcmp (alloc_buffer_next (&refbuf, void), "12", 2) == 0);
}

static void
test_misaligned (char pad)
{
  enum { SIZE = 23 };
  char *backing = xmalloc (SIZE + 2);
  backing[0] = ~pad;
  backing[SIZE + 1] = pad;
  struct alloc_buffer refbuf = alloc_buffer_create (backing + 1, SIZE);

  {
    struct alloc_buffer buf = refbuf;
    short *ptr = alloc_buffer_alloc_array (&buf, short, SIZE / sizeof (short));
    TEST_VERIFY_EXIT (ptr != NULL);
    TEST_VERIFY (IS_ALIGNED (ptr, short));
    TEST_VERIFY (!alloc_buffer_has_failed (&buf));
    for (int i = 0; i < SIZE / sizeof (short); ++i)
      ptr[i] = htons (0xff01 + i);
    TEST_VERIFY (memcmp (ptr,
                         "\xff\x01\xff\x02\xff\x03\xff\x04"
                         "\xff\x05\xff\x06\xff\x07\xff\x08"
                         "\xff\x09\xff\x0a\xff\x0b", 22) == 0);
  }
  {
    struct alloc_buffer buf = refbuf;
    uint32_t *ptr = alloc_buffer_alloc_array
      (&buf, uint32_t, SIZE / sizeof (uint32_t));
    TEST_VERIFY_EXIT (ptr != NULL);
    TEST_VERIFY (IS_ALIGNED (ptr, uint32_t));
    TEST_VERIFY (!alloc_buffer_has_failed (&buf));
    for (int i = 0; i < SIZE / sizeof (uint32_t); ++i)
      ptr[i] = htonl (0xf1e2d301 + i);
    TEST_VERIFY (memcmp (ptr,
                         "\xf1\xe2\xd3\x01\xf1\xe2\xd3\x02"
                         "\xf1\xe2\xd3\x03\xf1\xe2\xd3\x04"
                         "\xf1\xe2\xd3\x05", 20) == 0);
  }
  {
    struct alloc_buffer buf = refbuf;
    struct twelve *ptr = alloc_buffer_alloc (&buf, struct twelve);
    TEST_VERIFY_EXIT (ptr != NULL);
    TEST_VERIFY (IS_ALIGNED (ptr, struct twelve));
    TEST_VERIFY (!alloc_buffer_has_failed (&buf));
    ptr->buffer[0] = htonl (0x11223344);
    ptr->buffer[1] = htonl (0x55667788);
    ptr->buffer[2] = htonl (0x99aabbcc);
    TEST_VERIFY (memcmp (ptr,
                         "\x11\x22\x33\x44"
                         "\x55\x66\x77\x88"
                         "\x99\xaa\xbb\xcc", 12) == 0);
  }
  {
    static const double nums[] = { 1, 2 };
    struct alloc_buffer buf = refbuf;
    double *ptr = alloc_buffer_alloc_array (&buf, double, 2);
    TEST_VERIFY_EXIT (ptr != NULL);
    TEST_VERIFY (IS_ALIGNED (ptr, double));
    TEST_VERIFY (!alloc_buffer_has_failed (&buf));
    ptr[0] = nums[0];
    ptr[1] = nums[1];
    TEST_VERIFY (memcmp (ptr, nums, sizeof (nums)) == 0);
  }

  /* Verify that padding was not overwritten.  */
  TEST_VERIFY (backing[0] == (char) ~pad);
  TEST_VERIFY (backing[SIZE + 1] == pad);
  free (backing);
}

/* Check that overflow during alignment is handled properly.  */
static void
test_large_misaligned (void)
{
  uintptr_t minus1 = -1;
  uintptr_t start = minus1 & ~0xfe;
  struct alloc_buffer refbuf = alloc_buffer_create ((void *) start, 16);
  TEST_VERIFY (!alloc_buffer_has_failed (&refbuf));

  struct __attribute__ ((aligned (256))) align256
  {
    int dymmy;
  };

  {
    struct alloc_buffer buf = refbuf;
    TEST_VERIFY (alloc_buffer_alloc (&buf, struct align256) == NULL);
    test_after_failure (buf);
  }
  for (int count = 0; count < 3; ++count)
    {
      struct alloc_buffer buf = refbuf;
      TEST_VERIFY (alloc_buffer_alloc_array (&buf, struct align256, count)
                   == NULL);
      test_after_failure (buf);
    }
}

/* Check behavior of large allocations.  */
static void
test_large (void)
{
  {
    /* Allocation which wraps around.  */
    struct alloc_buffer buf = { 1, SIZE_MAX };
    TEST_VERIFY (alloc_buffer_alloc_array (&buf, char, SIZE_MAX) == NULL);
    TEST_VERIFY (alloc_buffer_has_failed (&buf));
  }

  {
    /* Successful very large allocation.  */
    struct alloc_buffer buf = { 1, SIZE_MAX };
    uintptr_t val = (uintptr_t) alloc_buffer_alloc_array
      (&buf, char, SIZE_MAX - 1);
    TEST_VERIFY (val == 1);
    TEST_VERIFY (!alloc_buffer_has_failed (&buf));
    test_empty (buf);
  }

  {
    typedef char __attribute__ ((aligned (2))) char2;

    /* Overflow in array size computation.   */
    struct alloc_buffer buf = { 1, SIZE_MAX };
    TEST_VERIFY (alloc_buffer_alloc_array (&buf, char2, SIZE_MAX - 1) == NULL);
    TEST_VERIFY (alloc_buffer_has_failed (&buf));

    /* Successful allocation after alignment.  */
    buf = (struct alloc_buffer) { 1, SIZE_MAX };
    uintptr_t val = (uintptr_t) alloc_buffer_alloc_array
      (&buf, char2, SIZE_MAX - 2);
    TEST_VERIFY (val == 2);
    test_empty (buf);

    /* Alignment behavior near the top of the address space.  */
    buf = (struct alloc_buffer) { SIZE_MAX, SIZE_MAX };
    TEST_VERIFY (alloc_buffer_next (&buf, char2) == NULL);
    TEST_VERIFY (alloc_buffer_has_failed (&buf));
    buf = (struct alloc_buffer) { SIZE_MAX, SIZE_MAX };
    TEST_VERIFY (alloc_buffer_alloc_array (&buf, char2, 0) == NULL);
    TEST_VERIFY (alloc_buffer_has_failed (&buf));
  }

  {
    typedef short __attribute__ ((aligned (2))) short2;

    /* Test overflow in size computation.  */
    struct alloc_buffer buf = { 1, SIZE_MAX };
    TEST_VERIFY (alloc_buffer_alloc_array (&buf, short2, SIZE_MAX / 2)
                 == NULL);
    TEST_VERIFY (alloc_buffer_has_failed (&buf));

    /* A slightly smaller array fits within the allocation.  */
    buf = (struct alloc_buffer) { 2, SIZE_MAX - 1 };
    uintptr_t val = (uintptr_t) alloc_buffer_alloc_array
      (&buf, short2, SIZE_MAX / 2 - 1);
    TEST_VERIFY (val == 2);
    test_empty (buf);
  }
}

static void
test_copy_bytes (void)
{
  char backing[4];
  {
    memset (backing, '@', sizeof (backing));
    struct alloc_buffer buf = alloc_buffer_create (backing, sizeof (backing));
    alloc_buffer_copy_bytes (&buf, "1", 1);
    TEST_VERIFY (!alloc_buffer_has_failed (&buf));
    TEST_VERIFY (alloc_buffer_size (&buf) == 3);
    TEST_VERIFY (memcmp (backing, "1@@@", 4) == 0);
  }
  {
    memset (backing, '@', sizeof (backing));
    struct alloc_buffer buf = alloc_buffer_create (backing, sizeof (backing));
    alloc_buffer_copy_bytes (&buf, "12", 3);
    TEST_VERIFY (!alloc_buffer_has_failed (&buf));
    TEST_VERIFY (alloc_buffer_size (&buf) == 1);
    TEST_VERIFY (memcmp (backing, "12\0@", 4) == 0);
  }
  {
    memset (backing, '@', sizeof (backing));
    struct alloc_buffer buf = alloc_buffer_create (backing, sizeof (backing));
    alloc_buffer_copy_bytes (&buf, "1234", 4);
    TEST_VERIFY (!alloc_buffer_has_failed (&buf));
    TEST_VERIFY (alloc_buffer_size (&buf) == 0);
    TEST_VERIFY (memcmp (backing, "1234", 4) == 0);
  }
  {
    memset (backing, '@', sizeof (backing));
    struct alloc_buffer buf = alloc_buffer_create (backing, sizeof (backing));
    alloc_buffer_copy_bytes (&buf, "1234", 5);
    TEST_VERIFY (alloc_buffer_has_failed (&buf));
    TEST_VERIFY (memcmp (backing, "@@@@", 4) == 0);
  }
  {
    memset (backing, '@', sizeof (backing));
    struct alloc_buffer buf = alloc_buffer_create (backing, sizeof (backing));
    alloc_buffer_copy_bytes (&buf, "1234", -1);
    TEST_VERIFY (alloc_buffer_has_failed (&buf));
    TEST_VERIFY (memcmp (backing, "@@@@", 4) == 0);
  }
}

static void
test_copy_string (void)
{
  char backing[4];
  {
    memset (backing, '@', sizeof (backing));
    struct alloc_buffer buf = alloc_buffer_create (backing, sizeof (backing));
    const char *p = alloc_buffer_copy_string (&buf, "");
    TEST_VERIFY (p == backing);
    TEST_VERIFY (strcmp (p, "") == 0);
    TEST_VERIFY (!alloc_buffer_has_failed (&buf));
    TEST_VERIFY (alloc_buffer_size (&buf) == 3);
    TEST_VERIFY (memcmp (backing, "\0@@@", 4) == 0);
  }
  {
    memset (backing, '@', sizeof (backing));
    struct alloc_buffer buf = alloc_buffer_create (backing, sizeof (backing));
    const char *p = alloc_buffer_copy_string (&buf, "1");
    TEST_VERIFY (p == backing);
    TEST_VERIFY (strcmp (p, "1") == 0);
    TEST_VERIFY (!alloc_buffer_has_failed (&buf));
    TEST_VERIFY (alloc_buffer_size (&buf) == 2);
    TEST_VERIFY (memcmp (backing, "1\0@@", 4) == 0);
  }
  {
    memset (backing, '@', sizeof (backing));
    struct alloc_buffer buf = alloc_buffer_create (backing, sizeof (backing));
    const char *p = alloc_buffer_copy_string (&buf, "12");
    TEST_VERIFY (p == backing);
    TEST_VERIFY (strcmp (p, "12") == 0);
    TEST_VERIFY (!alloc_buffer_has_failed (&buf));
    TEST_VERIFY (alloc_buffer_size (&buf) == 1);
    TEST_VERIFY (memcmp (backing, "12\0@", 4) == 0);
  }
  {
    memset (backing, '@', sizeof (backing));
    struct alloc_buffer buf = alloc_buffer_create (backing, sizeof (backing));
    const char *p = alloc_buffer_copy_string (&buf, "123");
    TEST_VERIFY (p == backing);
    TEST_VERIFY (strcmp (p, "123") == 0);
    TEST_VERIFY (!alloc_buffer_has_failed (&buf));
    TEST_VERIFY (alloc_buffer_size (&buf) == 0);
    TEST_VERIFY (memcmp (backing, "123", 4) == 0);
  }
  {
    memset (backing, '@', sizeof (backing));
    struct alloc_buffer buf = alloc_buffer_create (backing, sizeof (backing));
    TEST_VERIFY (alloc_buffer_copy_string (&buf, "1234") == NULL);
    TEST_VERIFY (alloc_buffer_has_failed (&buf));
    TEST_VERIFY (memcmp (backing, "@@@@", 4) == 0);
  }
  {
    memset (backing, '@', sizeof (backing));
    struct alloc_buffer buf = alloc_buffer_create (backing, sizeof (backing));
    TEST_VERIFY (alloc_buffer_copy_string (&buf, "12345") == NULL);
    TEST_VERIFY (alloc_buffer_has_failed (&buf));
    TEST_VERIFY (memcmp (backing, "@@@@", 4) == 0);
  }
}

static int
do_test (void)
{
  test_empty (alloc_buffer_create (NULL, 0));
  test_empty (alloc_buffer_create ((char *) "", 0));
  test_empty (alloc_buffer_create ((void *) 1, 0));

  {
    void *ptr = (void *) "";    /* Cannot be freed. */
    struct alloc_buffer buf = alloc_buffer_allocate (1, &ptr);
    test_size_1 (buf);
    free (ptr);                 /* Should have been overwritten.  */
  }

  {
    void *ptr= (void *) "";     /* Cannot be freed.  */
    struct alloc_buffer buf = alloc_buffer_allocate (2, &ptr);
    test_size_2 (buf);
    free (ptr);                 /* Should have been overwritten.  */
  }

  test_misaligned (0);
  test_misaligned (0xc7);
  test_misaligned (0xff);

  test_large_misaligned ();
  test_large ();
  test_copy_bytes ();
  test_copy_string ();

  return 0;
}

#include <support/test-driver.c>