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 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 17 | #include "thread.h" |
| 18 | |
Ian Rogers | c7dd295 | 2014-10-21 23:31:19 -0700 | [diff] [blame] | 19 | #include <signal.h> |
| 20 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 21 | #include "base/logging.h" // For VLOG. |
David Sehr | c431b9d | 2018-03-02 12:01:51 -0800 | [diff] [blame] | 22 | #include "base/utils.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 23 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 24 | namespace art { |
| 25 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 26 | void Thread::SetNativePriority(int) { |
| 27 | // Do nothing. |
| 28 | } |
| 29 | |
| 30 | int Thread::GetNativePriority() { |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 31 | return kNormThreadPriority; |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 32 | } |
| 33 | |
Elliott Hughes | d6a23bd | 2013-07-16 14:19:52 -0700 | [diff] [blame] | 34 | static void SigAltStack(stack_t* new_stack, stack_t* old_stack) { |
| 35 | if (sigaltstack(new_stack, old_stack) == -1) { |
| 36 | PLOG(FATAL) << "sigaltstack failed"; |
| 37 | } |
| 38 | } |
| 39 | |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 40 | // The default SIGSTKSZ on linux is 8K. If we do any logging in a signal |
Christopher Ferris | e34c71c | 2015-06-24 09:37:39 -0700 | [diff] [blame] | 41 | // handler or do a stack unwind, this is too small. We allocate 32K |
| 42 | // instead of the minimum signal stack size. |
Ian Rogers | c24a1e0 | 2014-08-13 14:37:26 -0700 | [diff] [blame] | 43 | // TODO: We shouldn't do logging (with locks) in signal handlers. |
| 44 | static constexpr int kHostAltSigStackSize = |
Christopher Ferris | e34c71c | 2015-06-24 09:37:39 -0700 | [diff] [blame] | 45 | 32 * KB < MINSIGSTKSZ ? MINSIGSTKSZ : 32 * KB; |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 46 | |
Elliott Hughes | d6a23bd | 2013-07-16 14:19:52 -0700 | [diff] [blame] | 47 | void Thread::SetUpAlternateSignalStack() { |
| 48 | // Create and set an alternate signal stack. |
Bilyan Borisov | bb661c0 | 2016-04-04 16:27:32 +0100 | [diff] [blame] | 49 | #ifdef ART_TARGET_ANDROID |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 50 | LOG(FATAL) << "Invalid use of alternate signal stack on Android"; |
| 51 | #endif |
Elliott Hughes | d6a23bd | 2013-07-16 14:19:52 -0700 | [diff] [blame] | 52 | stack_t ss; |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 53 | ss.ss_sp = new uint8_t[kHostAltSigStackSize]; |
| 54 | ss.ss_size = kHostAltSigStackSize; |
Elliott Hughes | d6a23bd | 2013-07-16 14:19:52 -0700 | [diff] [blame] | 55 | ss.ss_flags = 0; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 56 | CHECK(ss.ss_sp != nullptr); |
| 57 | SigAltStack(&ss, nullptr); |
Elliott Hughes | d6a23bd | 2013-07-16 14:19:52 -0700 | [diff] [blame] | 58 | |
| 59 | // Double-check that it worked. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 60 | ss.ss_sp = nullptr; |
| 61 | SigAltStack(nullptr, &ss); |
Elliott Hughes | d6a23bd | 2013-07-16 14:19:52 -0700 | [diff] [blame] | 62 | VLOG(threads) << "Alternate signal stack is " << PrettySize(ss.ss_size) << " at " << ss.ss_sp; |
| 63 | } |
| 64 | |
| 65 | void Thread::TearDownAlternateSignalStack() { |
| 66 | // Get the pointer so we can free the memory. |
| 67 | stack_t ss; |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 68 | SigAltStack(nullptr, &ss); |
Elliott Hughes | d6a23bd | 2013-07-16 14:19:52 -0700 | [diff] [blame] | 69 | uint8_t* allocated_signal_stack = reinterpret_cast<uint8_t*>(ss.ss_sp); |
| 70 | |
| 71 | // Tell the kernel to stop using it. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 72 | ss.ss_sp = nullptr; |
Elliott Hughes | d6a23bd | 2013-07-16 14:19:52 -0700 | [diff] [blame] | 73 | ss.ss_flags = SS_DISABLE; |
Dave Allison | dfd3b47 | 2014-07-16 16:04:32 -0700 | [diff] [blame] | 74 | ss.ss_size = kHostAltSigStackSize; // Avoid ENOMEM failure with Mac OS' buggy libc. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 75 | SigAltStack(&ss, nullptr); |
Elliott Hughes | d6a23bd | 2013-07-16 14:19:52 -0700 | [diff] [blame] | 76 | |
| 77 | // Free it. |
| 78 | delete[] allocated_signal_stack; |
| 79 | } |
| 80 | |
Elliott Hughes | 8daa092 | 2011-09-11 13:46:25 -0700 | [diff] [blame] | 81 | } // namespace art |