Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Vladimir Marko | e45883e | 2022-01-11 12:38:35 +0000 | [diff] [blame] | 17 | #include <signal.h> |
| 18 | #include <sys/mman.h> |
| 19 | |
Vladimir Marko | ebdaa2b | 2022-01-17 10:16:34 +0000 | [diff] [blame] | 20 | #include "base/globals.h" |
| 21 | #include "base/bit_utils.h" |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 22 | #include "thread.h" |
| 23 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 24 | namespace art { |
| 25 | |
Elliott Hughes | d6a23bd | 2013-07-16 14:19:52 -0700 | [diff] [blame] | 26 | void Thread::SetUpAlternateSignalStack() { |
| 27 | // Bionic does this for us. |
| 28 | } |
| 29 | |
| 30 | void Thread::TearDownAlternateSignalStack() { |
| 31 | // Bionic does this for us. |
| 32 | } |
| 33 | |
Vladimir Marko | e45883e | 2022-01-11 12:38:35 +0000 | [diff] [blame] | 34 | void Thread::MadviseAwayAlternateSignalStack() { |
| 35 | stack_t old_ss; |
| 36 | int result = sigaltstack(nullptr, &old_ss); |
| 37 | CHECK_EQ(result, 0); |
Vladimir Marko | ebdaa2b | 2022-01-17 10:16:34 +0000 | [diff] [blame] | 38 | // Only call `madvise()` on enabled page-aligned alternate signal stack. Processes can |
| 39 | // create different arbitrary alternate signal stacks and we do not want to erroneously |
| 40 | // `madvise()` away pages that may hold data other than the alternate signal stack. |
| 41 | if ((old_ss.ss_flags & SS_DISABLE) == 0 && |
| 42 | IsAligned<kPageSize>(old_ss.ss_sp) && |
| 43 | IsAligned<kPageSize>(old_ss.ss_size)) { |
| 44 | CHECK_EQ(old_ss.ss_flags & SS_ONSTACK, 0); |
| 45 | result = madvise(old_ss.ss_sp, old_ss.ss_size, MADV_DONTNEED); |
| 46 | CHECK_EQ(result, 0); |
| 47 | } |
Vladimir Marko | e45883e | 2022-01-11 12:38:35 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 50 | } // namespace art |