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 #include <sanitizer/allocator_interface.h>
20 int main(int argc
, char **argv
) {
22 // Those sizes will exercise both allocators (Primary & Secondary).
23 std::vector
<size_t> sizes
{1, 16, 1024, 32768, 1 << 16, 1 << 17, 1 << 20};
27 if (!strcmp(argv
[1], "usablesize")) {
28 // This tests a sketchy behavior inherited from poorly written libraries
29 // that have become somewhat standard. When realloc'ing a chunk, the
30 // copied contents should span the usable size of the chunk, not the
32 size_t size
= 496, usable_size
;
34 // Make sure we get a chunk with a usable size actually larger than size.
40 usable_size
= __sanitizer_get_allocated_size(p
);
41 assert(usable_size
>= size
);
42 } while (usable_size
== size
);
43 for (int i
= 0; i
< usable_size
; i
++)
44 reinterpret_cast<char *>(p
)[i
] = 'A';
46 // Make sure we get a different chunk so that the data is actually copied.
52 // The contents of the new chunk must match the old one up to usable_size.
53 for (int i
= 0; i
< usable_size
; i
++)
54 assert(reinterpret_cast<char *>(p
)[i
] == 'A');
57 for (size_t size
: sizes
) {
58 if (!strcmp(argv
[1], "pointers")) {
59 old_p
= p
= realloc(nullptr, size
);
61 size
= __sanitizer_get_allocated_size(p
);
62 // Our realloc implementation will return the same pointer if the size
63 // requested is lower than or equal to the usable size of the associated
65 p
= realloc(p
, size
- 1);
69 // And a new one if the size is greater.
70 p
= realloc(p
, size
+ 1);
72 // A size of 0 will free the chunk and return nullptr.
77 if (!strcmp(argv
[1], "contents")) {
78 p
= realloc(nullptr, size
);
80 for (int i
= 0; i
< size
; i
++)
81 reinterpret_cast<char *>(p
)[i
] = 'A';
82 p
= realloc(p
, size
+ 1);
83 // The contents of the reallocated chunk must match the original one.
84 for (int i
= 0; i
< size
; i
++)
85 assert(reinterpret_cast<char *>(p
)[i
] == 'A');
92 // CHECK: ERROR: invalid chunk type when reallocating address