1570af302Sopenharmony_ci#include <string.h>
2570af302Sopenharmony_ci#include <stdint.h>
3570af302Sopenharmony_ci#include "pthread_impl.h"
4570af302Sopenharmony_ci
5570af302Sopenharmony_ciuintptr_t __stack_chk_guard;
6570af302Sopenharmony_ci
7570af302Sopenharmony_civoid __init_ssp(void *entropy)
8570af302Sopenharmony_ci{
9570af302Sopenharmony_ci	if (entropy) memcpy(&__stack_chk_guard, entropy, sizeof(uintptr_t));
10570af302Sopenharmony_ci	else __stack_chk_guard = (uintptr_t)&__stack_chk_guard * 1103515245;
11570af302Sopenharmony_ci
12570af302Sopenharmony_ci#if UINTPTR_MAX >= 0xffffffffffffffff
13570af302Sopenharmony_ci	/* Sacrifice 8 bits of entropy on 64bit to prevent leaking/
14570af302Sopenharmony_ci	 * overwriting the canary via string-manipulation functions.
15570af302Sopenharmony_ci	 * The NULL byte is on the second byte so that off-by-ones can
16570af302Sopenharmony_ci	 * still be detected. Endianness is taken care of
17570af302Sopenharmony_ci	 * automatically. */
18570af302Sopenharmony_ci	((char *)&__stack_chk_guard)[1] = 0;
19570af302Sopenharmony_ci#endif
20570af302Sopenharmony_ci
21570af302Sopenharmony_ci	__pthread_self()->canary = __stack_chk_guard;
22570af302Sopenharmony_ci}
23570af302Sopenharmony_ci
24570af302Sopenharmony_civoid __stack_chk_fail(void)
25570af302Sopenharmony_ci{
26570af302Sopenharmony_ci	a_crash();
27570af302Sopenharmony_ci}
28570af302Sopenharmony_ci
29570af302Sopenharmony_cihidden void __stack_chk_fail_local(void);
30570af302Sopenharmony_ci
31570af302Sopenharmony_ciweak_alias(__stack_chk_fail, __stack_chk_fail_local);
32