1// Test basic realloc functionality.
2// RUN: %clang_hwasan %s -o %t && %run %t
3// RUN: %clang_hwasan %s -DREALLOCARRAY -o %t && %run %t
4
5#include <assert.h>
6#include <sanitizer/hwasan_interface.h>
7#include <stdlib.h>
8
9#ifdef REALLOCARRAY
10extern "C" void *reallocarray(void *, size_t nmemb, size_t size);
11#define REALLOC(p, s) reallocarray(p, 1, s)
12#else
13#define REALLOC(p, s) realloc(p, s)
14#endif
15
16int main() {
17 __hwasan_enable_allocator_tagging();
18 char *x = (char*)REALLOC(nullptr, 4);
19 x[0] = 10;
20 x[1] = 20;
21 x[2] = 30;
22 x[3] = 40;
23 char *x1 = (char*)REALLOC(x, 5);
24 assert(x1 != x); // not necessary true for C,
25 // but true today for hwasan.
26 assert(x1[0] == 10 && x1[1] == 20 && x1[2] == 30 && x1[3] == 40);
27 x1[4] = 50;
28
29 char *x2 = (char*)REALLOC(x1, 6);
30 x2[5] = 60;
31 assert(x2 != x1);
32 assert(x2[0] == 10 && x2[1] == 20 && x2[2] == 30 && x2[3] == 40 &&
33 x2[4] == 50 && x2[5] == 60);
34
35 char *x3 = (char*)REALLOC(x2, 6);
36 assert(x3 != x2);
37 assert(x3[0] == 10 && x3[1] == 20 && x3[2] == 30 && x3[3] == 40 &&
38 x3[4] == 50 && x3[5] == 60);
39
40 char *x4 = (char*)REALLOC(x3, 5);
41 assert(x4 != x3);
42 assert(x4[0] == 10 && x4[1] == 20 && x4[2] == 30 && x4[3] == 40 &&
43 x4[4] == 50);
44 free(ptr: x4);
45}
46

source code of compiler-rt/test/hwasan/TestCases/realloc-test.cpp