1 // RUN: %clang_scudo %s -o %t
2 // RUN: %run %t 2>&1
3 
4 // Verifies that calling malloc in a preinit_array function succeeds, and that
5 // the resulting pointer can be freed at program termination.
6 
7 // On some Android versions, calling mmap() from a preinit function segfaults.
8 // It looks like __mmap2.S ends up calling a NULL function pointer.
9 // UNSUPPORTED: android
10 
11 #include <assert.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 static void *global_p = NULL;
16 
__init(void)17 void __init(void) {
18   global_p = malloc(1);
19   if (!global_p)
20     exit(1);
21 }
22 
__fini(void)23 void __fini(void) {
24   if (global_p)
25     free(global_p);
26 }
27 
main(int argc,char ** argv)28 int main(int argc, char **argv)
29 {
30   void *p = malloc(1);
31   assert(p);
32   free(p);
33 
34   return 0;
35 }
36 
37 __attribute__((section(".preinit_array"), used))
38   void (*__local_preinit)(void) = __init;
39 __attribute__((section(".fini_array"), used))
40   void (*__local_fini)(void) = __fini;
41