blob: 34e3c11536b4e3da294452ccc6a3e0a157095262 [file] [log] [blame]
Elliott Hughesad88a082012-10-24 18:37:21 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * Contributed by: Intel Corporation
19 */
20
21#include <gtest/gtest.h>
Yabin Cui9df70402014-11-05 18:01:01 -080022#include "BionicDeathTest.h"
Elliott Hughesad88a082012-10-24 18:37:21 -070023
24#include <pthread.h>
25#include <stdint.h>
26#include <stdio.h>
Elliott Hughesad88a082012-10-24 18:37:21 -070027#include <unistd.h>
28#include <set>
29
Elliott Hughesfc69a8a2016-03-04 11:53:09 -080030#include "private/bionic_tls.h"
Elliott Hughesad88a082012-10-24 18:37:21 -070031
Elliott Hughesfc69a8a2016-03-04 11:53:09 -080032extern "C" pid_t gettid(); // glibc defines this but doesn't declare it anywhere.
33
34#if defined(__BIONIC__)
35extern uintptr_t __stack_chk_guard;
36#endif
Elliott Hughesad88a082012-10-24 18:37:21 -070037
38struct stack_protector_checker {
39 std::set<pid_t> tids;
Elliott Hughesfc69a8a2016-03-04 11:53:09 -080040 std::set<void*> guards;
Elliott Hughesad88a082012-10-24 18:37:21 -070041
42 void Check() {
43 pid_t tid = gettid();
Elliott Hughesfc69a8a2016-03-04 11:53:09 -080044 void* guard = __get_tls()[TLS_SLOT_STACK_GUARD];
Elliott Hughesad88a082012-10-24 18:37:21 -070045
Elliott Hughesfc69a8a2016-03-04 11:53:09 -080046 printf("[thread %d] TLS stack guard = %p\n", tid, guard);
Elliott Hughesad88a082012-10-24 18:37:21 -070047
48 // Duplicate tid. gettid(2) bug? Seeing this would be very upsetting.
49 ASSERT_TRUE(tids.find(tid) == tids.end());
Elliott Hughesd3920b32013-02-07 18:39:34 -080050
Elliott Hughesfc69a8a2016-03-04 11:53:09 -080051 // Uninitialized guard. Our bug. Note this is potentially flaky; we _could_
52 // get four random zero bytes, but it should be vanishingly unlikely.
53 ASSERT_NE(guard, nullptr);
54
55#if defined(__BIONIC__)
56 // bionic always has the global too.
57 ASSERT_EQ(__stack_chk_guard, reinterpret_cast<uintptr_t>(guard));
58#endif
Elliott Hughesad88a082012-10-24 18:37:21 -070059
60 tids.insert(tid);
61 guards.insert(guard);
62 }
63};
64
Elliott Hughesd3920b32013-02-07 18:39:34 -080065TEST(stack_protector, same_guard_per_thread) {
Elliott Hughesfc69a8a2016-03-04 11:53:09 -080066 // Everyone has the TLS slot set, even if their stack protector
67 // implementation doesn't yet use it.
Elliott Hughesad88a082012-10-24 18:37:21 -070068 stack_protector_checker checker;
Elliott Hughesfc69a8a2016-03-04 11:53:09 -080069
70 // Check the main thread.
71 ASSERT_EQ(getpid(), gettid()); // We are the main thread, right?
72 checker.Check();
73
74 size_t thread_count = 9;
75 for (size_t i = 1; i < thread_count; ++i) {
Elliott Hughesad88a082012-10-24 18:37:21 -070076 pthread_t t;
Yi Kong32bc0fc2018-08-02 17:31:13 -070077 ASSERT_EQ(0, pthread_create(&t, nullptr, [](void* arg) -> void* {
Elliott Hughesfc69a8a2016-03-04 11:53:09 -080078 stack_protector_checker* checker = reinterpret_cast<stack_protector_checker*>(arg);
79 checker->Check();
80 return nullptr;
81 }, &checker));
Elliott Hughesad88a082012-10-24 18:37:21 -070082 void* result;
83 ASSERT_EQ(0, pthread_join(t, &result));
Yi Kong32bc0fc2018-08-02 17:31:13 -070084 ASSERT_EQ(nullptr, result);
Elliott Hughesad88a082012-10-24 18:37:21 -070085 }
86 ASSERT_EQ(thread_count, checker.tids.size());
87
Elliott Hughesfc69a8a2016-03-04 11:53:09 -080088 // Both bionic and glibc use the same guard for every thread.
Elliott Hughesad88a082012-10-24 18:37:21 -070089 ASSERT_EQ(1U, checker.guards.size());
Elliott Hughesad88a082012-10-24 18:37:21 -070090}
91
Christopher Ferrisf04935c2013-12-20 18:43:21 -080092TEST(stack_protector, global_guard) {
Elliott Hughesfc69a8a2016-03-04 11:53:09 -080093#if defined(__BIONIC__)
94 // Bionic always has a global, even if it's using TLS.
Christopher Ferrisf04935c2013-12-20 18:43:21 -080095 ASSERT_NE(0, gettid());
96 ASSERT_NE(0U, __stack_chk_guard);
Elliott Hughesfc69a8a2016-03-04 11:53:09 -080097#else
98 GTEST_LOG_(INFO) << "glibc doesn't have a global __stack_chk_guard.\n";
99#endif
Nick Kralevichdcab1b22013-01-10 17:12:29 -0800100}
101
Yabin Cui9df70402014-11-05 18:01:01 -0800102class stack_protector_DeathTest : public BionicDeathTest {};
103
104TEST_F(stack_protector_DeathTest, modify_stack_protector) {
Elliott Hughesfc69a8a2016-03-04 11:53:09 -0800105 // In another file to prevent inlining, which removes stack protection.
106 extern void modify_stack_protector_test();
107 ASSERT_EXIT(modify_stack_protector_test(),
108 testing::KilledBySignal(SIGABRT), "stack corruption detected");
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800109}