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
15static void *global_p = NULL;
16
17void __init(void) {
18 global_p = malloc(size: 1);
19 if (!global_p)
20 exit(status: 1);
21}
22
23void __fini(void) {
24 if (global_p)
25 free(ptr: global_p);
26}
27
28int main(int argc, char **argv) {
29 void *p = malloc(size: 1);
30 assert(p);
31 free(ptr: p);
32
33 return 0;
34}
35
36__attribute__((section(".preinit_array"), used)) void (*__local_preinit)(void) = __init;
37__attribute__((section(".fini_array"), used)) void (*__local_fini)(void) = __fini;
38

source code of compiler-rt/test/scudo/preinit.c