xref: /aosp_15_r20/external/compiler-rt/test/scudo/preinit.cpp (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot // RUN: %clang_scudo %s -o %t
2*7c3d14c8STreehugger Robot // RUN: %run %t 2>&1
3*7c3d14c8STreehugger Robot 
4*7c3d14c8STreehugger Robot // Verifies that calling malloc in a preinit_array function succeeds, and that
5*7c3d14c8STreehugger Robot // the resulting pointer can be freed at program termination.
6*7c3d14c8STreehugger Robot 
7*7c3d14c8STreehugger Robot #include <malloc.h>
8*7c3d14c8STreehugger Robot #include <stdlib.h>
9*7c3d14c8STreehugger Robot #include <string.h>
10*7c3d14c8STreehugger Robot 
11*7c3d14c8STreehugger Robot static void *global_p = nullptr;
12*7c3d14c8STreehugger Robot 
__init(void)13*7c3d14c8STreehugger Robot void __init(void) {
14*7c3d14c8STreehugger Robot   global_p = malloc(1);
15*7c3d14c8STreehugger Robot   if (!global_p)
16*7c3d14c8STreehugger Robot     exit(1);
17*7c3d14c8STreehugger Robot }
18*7c3d14c8STreehugger Robot 
__fini(void)19*7c3d14c8STreehugger Robot void __fini(void) {
20*7c3d14c8STreehugger Robot   if (global_p)
21*7c3d14c8STreehugger Robot     free(global_p);
22*7c3d14c8STreehugger Robot }
23*7c3d14c8STreehugger Robot 
main(int argc,char ** argv)24*7c3d14c8STreehugger Robot int main(int argc, char **argv)
25*7c3d14c8STreehugger Robot {
26*7c3d14c8STreehugger Robot   void *p = malloc(1);
27*7c3d14c8STreehugger Robot   if (!p)
28*7c3d14c8STreehugger Robot     return 1;
29*7c3d14c8STreehugger Robot   free(p);
30*7c3d14c8STreehugger Robot 
31*7c3d14c8STreehugger Robot   return 0;
32*7c3d14c8STreehugger Robot }
33*7c3d14c8STreehugger Robot 
34*7c3d14c8STreehugger Robot __attribute__((section(".preinit_array"), used))
35*7c3d14c8STreehugger Robot   void (*__local_preinit)(void) = __init;
36*7c3d14c8STreehugger Robot __attribute__((section(".fini_array"), used))
37*7c3d14c8STreehugger Robot   void (*__local_fini)(void) = __fini;
38*7c3d14c8STreehugger Robot 
39