1 // RUN: %clang_scudo %s -o %t
2 // RUN: SCUDO_OPTIONS=QuarantineSizeMb=1 %run %t 2>&1
3 
4 // Tests that the quarantine prevents a chunk from being reused right away.
5 // Also tests that a chunk will eventually become available again for
6 // allocation when the recycling criteria has been met.
7 
8 #include <malloc.h>
9 #include <stdlib.h>
10 #include <string.h>
11 
main(int argc,char ** argv)12 int main(int argc, char **argv)
13 {
14   void *p, *old_p;
15   size_t size = 1U << 16;
16 
17   // The delayed freelist will prevent a chunk from being available right away
18   p = malloc(size);
19   if (!p)
20     return 1;
21   old_p = p;
22   free(p);
23   p = malloc(size);
24   if (!p)
25     return 1;
26   if (old_p == p)
27     return 1;
28   free(p);
29 
30   // Eventually the chunk should become available again
31   bool found = false;
32   for (int i = 0; i < 0x100 && found == false; i++) {
33     p = malloc(size);
34     if (!p)
35       return 1;
36     found = (p == old_p);
37     free(p);
38   }
39   if (found == false)
40     return 1;
41 
42   return 0;
43 }
44