1*7c3d14c8STreehugger Robot // RUN: %clang_scudo %s -o %t
2*7c3d14c8STreehugger Robot // RUN: %run %t pointers 2>&1
3*7c3d14c8STreehugger Robot // RUN: %run %t contents 2>&1
4*7c3d14c8STreehugger Robot // RUN: not %run %t memalign 2>&1 | FileCheck %s
5*7c3d14c8STreehugger Robot
6*7c3d14c8STreehugger Robot // Tests that our reallocation function returns the same pointer when the
7*7c3d14c8STreehugger Robot // requested size can fit into the previously allocated chunk. Also tests that
8*7c3d14c8STreehugger Robot // a new chunk is returned if the size is greater, and that the contents of the
9*7c3d14c8STreehugger Robot // chunk are left unchanged.
10*7c3d14c8STreehugger Robot // As a final test, make sure that a chunk allocated by memalign cannot be
11*7c3d14c8STreehugger Robot // reallocated.
12*7c3d14c8STreehugger Robot
13*7c3d14c8STreehugger Robot #include <assert.h>
14*7c3d14c8STreehugger Robot #include <malloc.h>
15*7c3d14c8STreehugger Robot #include <string.h>
16*7c3d14c8STreehugger Robot
main(int argc,char ** argv)17*7c3d14c8STreehugger Robot int main(int argc, char **argv)
18*7c3d14c8STreehugger Robot {
19*7c3d14c8STreehugger Robot void *p, *old_p;
20*7c3d14c8STreehugger Robot size_t size = 32;
21*7c3d14c8STreehugger Robot
22*7c3d14c8STreehugger Robot assert(argc == 2);
23*7c3d14c8STreehugger Robot if (!strcmp(argv[1], "pointers")) {
24*7c3d14c8STreehugger Robot old_p = p = realloc(nullptr, size);
25*7c3d14c8STreehugger Robot if (!p)
26*7c3d14c8STreehugger Robot return 1;
27*7c3d14c8STreehugger Robot size = malloc_usable_size(p);
28*7c3d14c8STreehugger Robot // Our realloc implementation will return the same pointer if the size
29*7c3d14c8STreehugger Robot // requested is lower or equal to the usable size of the associated chunk.
30*7c3d14c8STreehugger Robot p = realloc(p, size - 1);
31*7c3d14c8STreehugger Robot if (p != old_p)
32*7c3d14c8STreehugger Robot return 1;
33*7c3d14c8STreehugger Robot p = realloc(p, size);
34*7c3d14c8STreehugger Robot if (p != old_p)
35*7c3d14c8STreehugger Robot return 1;
36*7c3d14c8STreehugger Robot // And a new one if the size is greater.
37*7c3d14c8STreehugger Robot p = realloc(p, size + 1);
38*7c3d14c8STreehugger Robot if (p == old_p)
39*7c3d14c8STreehugger Robot return 1;
40*7c3d14c8STreehugger Robot // A size of 0 will free the chunk and return nullptr.
41*7c3d14c8STreehugger Robot p = realloc(p, 0);
42*7c3d14c8STreehugger Robot if (p)
43*7c3d14c8STreehugger Robot return 1;
44*7c3d14c8STreehugger Robot old_p = nullptr;
45*7c3d14c8STreehugger Robot }
46*7c3d14c8STreehugger Robot if (!strcmp(argv[1], "contents")) {
47*7c3d14c8STreehugger Robot p = realloc(nullptr, size);
48*7c3d14c8STreehugger Robot if (!p)
49*7c3d14c8STreehugger Robot return 1;
50*7c3d14c8STreehugger Robot for (int i = 0; i < size; i++)
51*7c3d14c8STreehugger Robot reinterpret_cast<char *>(p)[i] = 'A';
52*7c3d14c8STreehugger Robot p = realloc(p, size + 1);
53*7c3d14c8STreehugger Robot // The contents of the reallocated chunk must match the original one.
54*7c3d14c8STreehugger Robot for (int i = 0; i < size; i++)
55*7c3d14c8STreehugger Robot if (reinterpret_cast<char *>(p)[i] != 'A')
56*7c3d14c8STreehugger Robot return 1;
57*7c3d14c8STreehugger Robot }
58*7c3d14c8STreehugger Robot if (!strcmp(argv[1], "memalign")) {
59*7c3d14c8STreehugger Robot // A chunk coming from memalign cannot be reallocated.
60*7c3d14c8STreehugger Robot p = memalign(16, size);
61*7c3d14c8STreehugger Robot if (!p)
62*7c3d14c8STreehugger Robot return 1;
63*7c3d14c8STreehugger Robot p = realloc(p, size);
64*7c3d14c8STreehugger Robot free(p);
65*7c3d14c8STreehugger Robot }
66*7c3d14c8STreehugger Robot return 0;
67*7c3d14c8STreehugger Robot }
68*7c3d14c8STreehugger Robot
69*7c3d14c8STreehugger Robot // CHECK: ERROR: invalid chunk type when reallocating address
70