blob: 0284364de7e27c601f0b400eb5b3e12b3a2ce44f [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
17#include "thread.h"
18
Ian Rogersc7dd2952014-10-21 23:31:19 -070019#include <signal.h>
20
Elliott Hughes8daa0922011-09-11 13:46:25 -070021namespace art {
22
Elliott Hughes8daa0922011-09-11 13:46:25 -070023void Thread::SetNativePriority(int) {
24 // Do nothing.
25}
26
27int Thread::GetNativePriority() {
Elliott Hughes34e06962012-04-09 13:55:55 -070028 return kNormThreadPriority;
Elliott Hughes8daa0922011-09-11 13:46:25 -070029}
30
Elliott Hughesd6a23bd2013-07-16 14:19:52 -070031static 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 Allisondfd3b472014-07-16 16:04:32 -070037// The default SIGSTKSZ on linux is 8K. If we do any logging in a signal
Ian Rogersc24a1e02014-08-13 14:37:26 -070038// 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.
41static constexpr int kHostAltSigStackSize =
42 16 * KB < MINSIGSTKSZ ? MINSIGSTKSZ : 16 * KB;
Dave Allisondfd3b472014-07-16 16:04:32 -070043
Elliott Hughesd6a23bd2013-07-16 14:19:52 -070044void Thread::SetUpAlternateSignalStack() {
45 // Create and set an alternate signal stack.
Dave Allisondfd3b472014-07-16 16:04:32 -070046#ifdef HAVE_ANDROID_OS
47 LOG(FATAL) << "Invalid use of alternate signal stack on Android";
48#endif
Elliott Hughesd6a23bd2013-07-16 14:19:52 -070049 stack_t ss;
Dave Allisondfd3b472014-07-16 16:04:32 -070050 ss.ss_sp = new uint8_t[kHostAltSigStackSize];
51 ss.ss_size = kHostAltSigStackSize;
Elliott Hughesd6a23bd2013-07-16 14:19:52 -070052 ss.ss_flags = 0;
53 CHECK(ss.ss_sp != NULL);
54 SigAltStack(&ss, NULL);
55
56 // Double-check that it worked.
57 ss.ss_sp = NULL;
58 SigAltStack(NULL, &ss);
59 VLOG(threads) << "Alternate signal stack is " << PrettySize(ss.ss_size) << " at " << ss.ss_sp;
60}
61
62void Thread::TearDownAlternateSignalStack() {
63 // Get the pointer so we can free the memory.
64 stack_t ss;
65 SigAltStack(NULL, &ss);
66 uint8_t* allocated_signal_stack = reinterpret_cast<uint8_t*>(ss.ss_sp);
67
68 // Tell the kernel to stop using it.
69 ss.ss_sp = NULL;
70 ss.ss_flags = SS_DISABLE;
Dave Allisondfd3b472014-07-16 16:04:32 -070071 ss.ss_size = kHostAltSigStackSize; // Avoid ENOMEM failure with Mac OS' buggy libc.
Elliott Hughesd6a23bd2013-07-16 14:19:52 -070072 SigAltStack(&ss, NULL);
73
74 // Free it.
75 delete[] allocated_signal_stack;
76}
77
Elliott Hughes8daa0922011-09-11 13:46:25 -070078} // namespace art