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