Improve and fix the stack-protector tests.
Bug: http://b/26888853
Change-Id: I505dbf7d5934f7247fb639f55dd6a9341df3947b
diff --git a/libc/bionic/__libc_init_main_thread.cpp b/libc/bionic/__libc_init_main_thread.cpp
index e1445cb..a29d8a7 100644
--- a/libc/bionic/__libc_init_main_thread.cpp
+++ b/libc/bionic/__libc_init_main_thread.cpp
@@ -74,6 +74,10 @@
main_thread.attr.stack_size = 0; // User code should never see this; we'll compute it when asked.
// TODO: the main thread's sched_policy and sched_priority need to be queried.
+ // The TLS stack guard is set from the global, so ensure that we've initialized the global
+ // before we initialize the TLS.
+ __libc_init_global_stack_chk_guard(args);
+
__init_thread(&main_thread);
__init_tls(&main_thread);
diff --git a/libc/bionic/libc_init_common.cpp b/libc/bionic/libc_init_common.cpp
index ebd1595..c2a5fed 100644
--- a/libc/bionic/libc_init_common.cpp
+++ b/libc/bionic/libc_init_common.cpp
@@ -64,10 +64,17 @@
// Declared in "private/bionic_ssp.h".
uintptr_t __stack_chk_guard = 0;
+void __libc_init_global_stack_chk_guard(KernelArgumentBlock& args) {
+ // AT_RANDOM is a pointer to 16 bytes of randomness on the stack.
+ // Take the first 4/8 for the -fstack-protector implementation.
+ __stack_chk_guard = *reinterpret_cast<uintptr_t*>(args.getauxval(AT_RANDOM));
+}
+
void __libc_init_globals(KernelArgumentBlock& args) {
// Initialize libc globals that are needed in both the linker and in libc.
// In dynamic binaries, this is run at least twice for different copies of the
// globals, once for the linker's copy and once for the one in libc.so.
+ __libc_init_global_stack_chk_guard(args);
__libc_auxv = args.auxv;
__libc_globals.initialize();
__libc_globals.mutate([&args](libc_globals* globals) {
@@ -83,9 +90,6 @@
__progname = args.argv[0] ? args.argv[0] : "<unknown>";
__abort_message_ptr = args.abort_message_ptr;
- // AT_RANDOM is a pointer to 16 bytes of randomness on the stack.
- __stack_chk_guard = *reinterpret_cast<uintptr_t*>(getauxval(AT_RANDOM));
-
// Get the main thread from TLS and add it to the thread list.
pthread_internal_t* main_thread = __get_thread();
__pthread_internal_add(main_thread);