]>
git.proxmox.com Git - rustc.git/blob - src/libcompiler_builtins/compiler-rt/test/scudo/realloc.cpp
1 // RUN: %clangxx_scudo %s -lstdc++ -o %t
2 // RUN: %run %t pointers 2>&1
3 // RUN: %run %t contents 2>&1
4 // RUN: %run %t usablesize 2>&1
6 // Tests that our reallocation function returns the same pointer when the
7 // requested size can fit into the previously allocated chunk. Also tests that
8 // a new chunk is returned if the size is greater, and that the contents of the
9 // chunk are left unchanged. Finally, checks that realloc copies the usable
10 // size of the old chunk to the new one (as opposed to the requested size).
18 int main(int argc
, char **argv
)
21 // Those sizes will exercise both allocators (Primary & Secondary).
22 std::vector
<size_t> sizes
{1, 16, 1024, 32768, 1 << 16, 1 << 17, 1 << 20};
26 if (!strcmp(argv
[1], "usablesize")) {
27 // This tests a sketchy behavior inherited from poorly written libraries
28 // that have become somewhat standard. When realloc'ing a chunk, the
29 // copied contents should span the usable size of the chunk, not the
31 size_t size
= 496, usable_size
;
33 // Make sure we get a chunk with a usable size actually larger than size.
38 usable_size
= malloc_usable_size(p
);
39 assert(usable_size
>= size
);
40 } while (usable_size
== size
);
41 for (int i
= 0; i
< usable_size
; i
++)
42 reinterpret_cast<char *>(p
)[i
] = 'A';
44 // Make sure we get a different chunk so that the data is actually copied.
50 // The contents of the new chunk must match the old one up to usable_size.
51 for (int i
= 0; i
< usable_size
; i
++)
52 assert(reinterpret_cast<char *>(p
)[i
] == 'A');
55 for (size_t size
: sizes
) {
56 if (!strcmp(argv
[1], "pointers")) {
57 old_p
= p
= realloc(nullptr, size
);
59 size
= malloc_usable_size(p
);
60 // Our realloc implementation will return the same pointer if the size
61 // requested is lower than or equal to the usable size of the associated
63 p
= realloc(p
, size
- 1);
67 // And a new one if the size is greater.
68 p
= realloc(p
, size
+ 1);
70 // A size of 0 will free the chunk and return nullptr.
75 if (!strcmp(argv
[1], "contents")) {
76 p
= realloc(nullptr, size
);
78 for (int i
= 0; i
< size
; i
++)
79 reinterpret_cast<char *>(p
)[i
] = 'A';
80 p
= realloc(p
, size
+ 1);
81 // The contents of the reallocated chunk must match the original one.
82 for (int i
= 0; i
< size
; i
++)
83 assert(reinterpret_cast<char *>(p
)[i
] == 'A');
90 // CHECK: ERROR: invalid chunk type when reallocating address