blob: fea24d88742e29462b887dd7c5e8820bff700472 [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>
22
23#include <pthread.h>
24#include <stdint.h>
25#include <stdio.h>
26#include <sys/syscall.h>
27#include <unistd.h>
28#include <set>
29
Christopher Ferrisf04935c2013-12-20 18:43:21 -080030#if defined(__GLIBC__)
Elliott Hughesad88a082012-10-24 18:37:21 -070031// glibc doesn't expose gettid(2).
32pid_t gettid() { return syscall(__NR_gettid); }
Christopher Ferrisf04935c2013-12-20 18:43:21 -080033#endif // __GLIBC__
Elliott Hughesad88a082012-10-24 18:37:21 -070034
Elliott Hughesfb7eb5e2013-02-14 14:37:34 -080035// For x86, bionic and glibc have per-thread stack guard values (all identical).
Christopher Ferrisf04935c2013-12-20 18:43:21 -080036#if defined(__i386__)
Elliott Hughesad88a082012-10-24 18:37:21 -070037static uint32_t GetGuardFromTls() {
38 uint32_t guard;
39 asm ("mov %%gs:0x14, %0": "=d" (guard));
40 return guard;
41}
42
43struct stack_protector_checker {
44 std::set<pid_t> tids;
45 std::set<uint32_t> guards;
46
47 void Check() {
48 pid_t tid = gettid();
49 uint32_t guard = GetGuardFromTls();
50
51 printf("[thread %d] %%gs:0x14 = 0x%08x\n", tid, guard);
52
53 // Duplicate tid. gettid(2) bug? Seeing this would be very upsetting.
54 ASSERT_TRUE(tids.find(tid) == tids.end());
Elliott Hughesd3920b32013-02-07 18:39:34 -080055
Elliott Hughesad88a082012-10-24 18:37:21 -070056 // Uninitialized guard. Our bug. Note this is potentially flaky; we _could_ get
57 // four random zero bytes, but it should be vanishingly unlikely.
58 ASSERT_NE(guard, 0U);
59
60 tids.insert(tid);
61 guards.insert(guard);
62 }
63};
64
65static void* ThreadGuardHelper(void* arg) {
66 stack_protector_checker* checker = reinterpret_cast<stack_protector_checker*>(arg);
67 checker->Check();
68 return NULL;
69}
Christopher Ferrisf04935c2013-12-20 18:43:21 -080070#endif // __i386__
Elliott Hughesad88a082012-10-24 18:37:21 -070071
Elliott Hughesd3920b32013-02-07 18:39:34 -080072TEST(stack_protector, same_guard_per_thread) {
Christopher Ferrisf04935c2013-12-20 18:43:21 -080073#if defined(__i386__)
Elliott Hughesad88a082012-10-24 18:37:21 -070074 stack_protector_checker checker;
75 size_t thread_count = 10;
76 for (size_t i = 0; i < thread_count; ++i) {
77 pthread_t t;
78 ASSERT_EQ(0, pthread_create(&t, NULL, ThreadGuardHelper, &checker));
79 void* result;
80 ASSERT_EQ(0, pthread_join(t, &result));
81 ASSERT_EQ(NULL, result);
82 }
83 ASSERT_EQ(thread_count, checker.tids.size());
84
Elliott Hughesd3920b32013-02-07 18:39:34 -080085 // bionic and glibc use the same guard for every thread.
Elliott Hughesad88a082012-10-24 18:37:21 -070086 ASSERT_EQ(1U, checker.guards.size());
Christopher Ferrisf04935c2013-12-20 18:43:21 -080087#else // __i386__
88 GTEST_LOG_(INFO) << "This test does nothing.\n";
89#endif // __i386__
Elliott Hughesad88a082012-10-24 18:37:21 -070090}
91
Elliott Hughesad88a082012-10-24 18:37:21 -070092// For ARM and MIPS, glibc has a global stack check guard value.
Christopher Ferrisf04935c2013-12-20 18:43:21 -080093#if defined(__BIONIC__) || defined(__arm__) || defined(__mips__)
94#define TEST_STACK_CHK_GUARD
Elliott Hughesad88a082012-10-24 18:37:21 -070095
96// Bionic has the global for x86 too, to support binaries that can run on
97// Android releases that didn't implement the TLS guard value.
Elliott Hughesd3920b32013-02-07 18:39:34 -080098extern "C" uintptr_t __stack_chk_guard;
Elliott Hughesad88a082012-10-24 18:37:21 -070099
Nick Kralevichdcab1b22013-01-10 17:12:29 -0800100/*
101 * When this function returns, the stack canary will be inconsistent
102 * with the previous value, which will generate a call to __stack_chk_fail(),
103 * eventually resulting in a SIGABRT.
104 *
105 * This must be marked with "__attribute__ ((noinline))", to ensure the
106 * compiler generates the proper stack guards around this function.
107 */
108__attribute__ ((noinline))
109static void do_modify_stack_chk_guard() {
Elliott Hughesd3920b32013-02-07 18:39:34 -0800110 __stack_chk_guard = 0x12345678;
Nick Kralevichdcab1b22013-01-10 17:12:29 -0800111}
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800112#endif
Nick Kralevichdcab1b22013-01-10 17:12:29 -0800113
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800114TEST(stack_protector, global_guard) {
115#if defined(TEST_STACK_CHK_GUARD)
116 ASSERT_NE(0, gettid());
117 ASSERT_NE(0U, __stack_chk_guard);
118#else // TEST_STACK_CHK_GUARD
119 GTEST_LOG_(INFO) << "This test does nothing.\n";
120#endif // TEST_STACK_CHK_GUARD
Nick Kralevichdcab1b22013-01-10 17:12:29 -0800121}
122
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800123TEST(stack_protector_DeathTest, modify_stack_protector) {
124#if defined(TEST_STACK_CHK_GUARD)
125 ::testing::FLAGS_gtest_death_test_style = "threadsafe";
126 ASSERT_EXIT(do_modify_stack_chk_guard(), testing::KilledBySignal(SIGABRT), "");
127#else // TEST_STACK_CHK_GUARD
128 GTEST_LOG_(INFO) << "This test does nothing.\n";
129#endif // TEST_STACK_CHK_GUARD
130}