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