1 /*
2  * Copyright (C) 2023 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 #include "gtest/gtest.h"
18 
19 #include <signal.h>
20 #include <string.h>
21 #include <sys/mman.h>
22 
23 namespace {
24 
25 // Verify that nested signal handlers on an alternate stack do not interfere with others' stack
26 // variables.
27 
28 constexpr size_t kStackSize = 16 * 4096;  // from bionic/tests/pthread_test.cpp, fails if less.
29 void* g_ss = nullptr;
30 
31 constexpr size_t kAccessSize = 4096;
32 void* g_access_1;
33 void* g_access_2;
34 
HandleSignalOnAccess(int,siginfo_t *,void *)35 void HandleSignalOnAccess(int, siginfo_t*, void*) {
36   char ss_var[32];
37 
38   // Check handler runs on alternate stack.
39   const char* ss_start = static_cast<const char*>(g_ss);
40   EXPECT_GE(ss_var, ss_start);
41   EXPECT_LT(ss_var, ss_start + kStackSize);
42 
43   if (g_access_2 == nullptr) {
44     // First signal.
45     // Initialize stack.
46     strncpy(ss_var, "firstfirstfirst", sizeof(ss_var));
47 
48     // Force second signal.
49     // Because of SA_NODEFER, it should be a nested signal handler call.
50     g_access_2 = mmap(nullptr, kAccessSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
51     ASSERT_NE(g_access_2, MAP_FAILED);
52     *static_cast<int*>(g_access_2) = 2;
53 
54     // Check second signal didn't screw the stack up.
55     ASSERT_EQ(strncmp(ss_var, "firstfirstfirst", sizeof(ss_var)), 0);
56 
57     // Fix first signal.
58     ASSERT_EQ(mprotect(g_access_1, kAccessSize, PROT_READ | PROT_WRITE), 0);
59   } else {
60     // Second signal.
61     // Initialize stack with something different.
62     strncpy(ss_var, "secondsecondsecond", sizeof(ss_var));
63 
64     // Fix second signal.
65     ASSERT_EQ(mprotect(g_access_2, kAccessSize, PROT_READ | PROT_WRITE), 0);
66   }
67 }
68 
TEST(Signal,Sigaltstack)69 TEST(Signal, Sigaltstack) {
70   // Set signal handler for failed access.
71   // Use alternate stack if available.
72   // Allow nested handler call.
73   struct sigaction sa {};
74   sa.sa_sigaction = HandleSignalOnAccess;
75   sa.sa_flags = SA_SIGINFO | SA_NODEFER | SA_ONSTACK;
76   struct sigaction old_sa {};
77   ASSERT_EQ(sigaction(SIGSEGV, &sa, &old_sa), 0);
78 
79   // Install alternate signal stack.
80   g_ss = mmap(nullptr, kStackSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
81   ASSERT_NE(g_ss, MAP_FAILED);
82   stack_t ss{};
83   ss.ss_sp = g_ss;
84   ss.ss_size = kStackSize;
85   stack_t old_ss{};
86   ASSERT_EQ(sigaltstack(&ss, &old_ss), 0);
87 
88   g_access_2 = nullptr;
89 
90   // Force first signal.
91   g_access_1 = mmap(nullptr, kAccessSize, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
92   ASSERT_NE(g_access_1, MAP_FAILED);
93   *static_cast<int*>(g_access_1) = 1;
94 
95   ASSERT_EQ(sigaction(SIGSEGV, &old_sa, nullptr), 0);
96   ASSERT_EQ(sigaltstack(&old_ss, nullptr), 0);
97   ASSERT_EQ(munmap(g_ss, kStackSize), 0);
98   ASSERT_EQ(munmap(g_access_1, kAccessSize), 0);
99   ASSERT_EQ(munmap(g_access_2, kAccessSize), 0);
100 }
101 
102 }  // namespace
103