Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / ssp / stack_protector.c
blob5e9d75f0f3ff2803fb5df5d6b53d61d0d454ce89
1 #include <sys/cdefs.h>
2 #include <sys/param.h>
3 #include <signal.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
8 #if defined(__AMDGCN__) || defined(__nvptx__)
9 /* Global constructors not supported on this target, yet. */
10 uintptr_t __stack_chk_guard = 0x00000aff; /* 0, 0, '\n', 255 */
12 #else
13 uintptr_t __stack_chk_guard = 0;
15 void
16 __attribute__((__constructor__))
17 __stack_chk_init (void)
19 if (__stack_chk_guard != 0)
20 return;
22 #if defined(__CYGWIN__) || defined(__rtems__)
23 arc4random_buf(&__stack_chk_guard, sizeof(__stack_chk_guard));
24 #else
25 /* If getentropy is not available, use the "terminator canary". */
26 ((unsigned char *)&__stack_chk_guard)[0] = 0;
27 ((unsigned char *)&__stack_chk_guard)[1] = 0;
28 ((unsigned char *)&__stack_chk_guard)[2] = '\n';
29 ((unsigned char *)&__stack_chk_guard)[3] = 255;
30 #endif
32 #endif
34 void
35 __attribute__((__noreturn__))
36 __stack_chk_fail (void)
38 char msg[] = "*** stack smashing detected ***: terminated\n";
39 write (2, msg, strlen (msg));
40 raise (SIGABRT);
41 _exit (127);
44 #ifdef __ELF__
45 void
46 __attribute__((visibility ("hidden")))
47 __stack_chk_fail_local (void)
49 __stack_chk_fail();
51 #endif