blob: 9563b994f8ea9e49aaa279553ddbd0c215494ed5 [file] [log] [blame]
Elliott Hughes8daa0922011-09-11 13:46:25 -07001/*
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
Ian Rogersc7dd2952014-10-21 23:31:19 -070017#include <signal.h>
18
Vladimir Marko80afd022015-05-19 18:08:00 +010019#include "thread.h"
20#include "utils.h"
21
Elliott Hughes8daa0922011-09-11 13:46:25 -070022namespace art {
23
Elliott Hughes8daa0922011-09-11 13:46:25 -070024void Thread::SetNativePriority(int) {
25 // Do nothing.
26}
27
28int Thread::GetNativePriority() {
Elliott Hughes34e06962012-04-09 13:55:55 -070029 return kNormThreadPriority;
Elliott Hughes8daa0922011-09-11 13:46:25 -070030}
31
Elliott Hughesd6a23bd2013-07-16 14:19:52 -070032static void SigAltStack(stack_t* new_stack, stack_t* old_stack) {
33 if (sigaltstack(new_stack, old_stack) == -1) {
34 PLOG(FATAL) << "sigaltstack failed";
35 }
36}
37
Dave Allisondfd3b472014-07-16 16:04:32 -070038// The default SIGSTKSZ on linux is 8K. If we do any logging in a signal
Christopher Ferrise34c71c2015-06-24 09:37:39 -070039// handler or do a stack unwind, this is too small. We allocate 32K
40// instead of the minimum signal stack size.
Ian Rogersc24a1e02014-08-13 14:37:26 -070041// TODO: We shouldn't do logging (with locks) in signal handlers.
42static constexpr int kHostAltSigStackSize =
Christopher Ferrise34c71c2015-06-24 09:37:39 -070043 32 * KB < MINSIGSTKSZ ? MINSIGSTKSZ : 32 * KB;
Dave Allisondfd3b472014-07-16 16:04:32 -070044
Elliott Hughesd6a23bd2013-07-16 14:19:52 -070045void Thread::SetUpAlternateSignalStack() {
46 // Create and set an alternate signal stack.
Andreas Gampec60e1b72015-07-30 08:57:50 -070047#ifdef __ANDROID__
Dave Allisondfd3b472014-07-16 16:04:32 -070048 LOG(FATAL) << "Invalid use of alternate signal stack on Android";
49#endif
Elliott Hughesd6a23bd2013-07-16 14:19:52 -070050 stack_t ss;
Dave Allisondfd3b472014-07-16 16:04:32 -070051 ss.ss_sp = new uint8_t[kHostAltSigStackSize];
52 ss.ss_size = kHostAltSigStackSize;
Elliott Hughesd6a23bd2013-07-16 14:19:52 -070053 ss.ss_flags = 0;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070054 CHECK(ss.ss_sp != nullptr);
55 SigAltStack(&ss, nullptr);
Elliott Hughesd6a23bd2013-07-16 14:19:52 -070056
57 // Double-check that it worked.
Mathieu Chartier2cebb242015-04-21 16:50:40 -070058 ss.ss_sp = nullptr;
59 SigAltStack(nullptr, &ss);
Elliott Hughesd6a23bd2013-07-16 14:19:52 -070060 VLOG(threads) << "Alternate signal stack is " << PrettySize(ss.ss_size) << " at " << ss.ss_sp;
61}
62
63void Thread::TearDownAlternateSignalStack() {
64 // Get the pointer so we can free the memory.
65 stack_t ss;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070066 SigAltStack(nullptr, &ss);
Elliott Hughesd6a23bd2013-07-16 14:19:52 -070067 uint8_t* allocated_signal_stack = reinterpret_cast<uint8_t*>(ss.ss_sp);
68
69 // Tell the kernel to stop using it.
Mathieu Chartier2cebb242015-04-21 16:50:40 -070070 ss.ss_sp = nullptr;
Elliott Hughesd6a23bd2013-07-16 14:19:52 -070071 ss.ss_flags = SS_DISABLE;
Dave Allisondfd3b472014-07-16 16:04:32 -070072 ss.ss_size = kHostAltSigStackSize; // Avoid ENOMEM failure with Mac OS' buggy libc.
Mathieu Chartier2cebb242015-04-21 16:50:40 -070073 SigAltStack(&ss, nullptr);
Elliott Hughesd6a23bd2013-07-16 14:19:52 -070074
75 // Free it.
76 delete[] allocated_signal_stack;
77}
78
Elliott Hughes8daa0922011-09-11 13:46:25 -070079} // namespace art