1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2011 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker *
4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker *
8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker *
10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker */
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker #include <signal.h>
18*795d594fSAndroid Build Coastguard Worker #include <sys/mman.h>
19*795d594fSAndroid Build Coastguard Worker
20*795d594fSAndroid Build Coastguard Worker #include "base/globals.h"
21*795d594fSAndroid Build Coastguard Worker #include "base/bit_utils.h"
22*795d594fSAndroid Build Coastguard Worker #include "thread.h"
23*795d594fSAndroid Build Coastguard Worker
24*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
25*795d594fSAndroid Build Coastguard Worker
SetUpAlternateSignalStack()26*795d594fSAndroid Build Coastguard Worker void Thread::SetUpAlternateSignalStack() {
27*795d594fSAndroid Build Coastguard Worker // Bionic does this for us.
28*795d594fSAndroid Build Coastguard Worker }
29*795d594fSAndroid Build Coastguard Worker
TearDownAlternateSignalStack()30*795d594fSAndroid Build Coastguard Worker void Thread::TearDownAlternateSignalStack() {
31*795d594fSAndroid Build Coastguard Worker // Bionic does this for us.
32*795d594fSAndroid Build Coastguard Worker }
33*795d594fSAndroid Build Coastguard Worker
MadviseAwayAlternateSignalStack()34*795d594fSAndroid Build Coastguard Worker void Thread::MadviseAwayAlternateSignalStack() {
35*795d594fSAndroid Build Coastguard Worker stack_t old_ss;
36*795d594fSAndroid Build Coastguard Worker int result = sigaltstack(nullptr, &old_ss);
37*795d594fSAndroid Build Coastguard Worker CHECK_EQ(result, 0);
38*795d594fSAndroid Build Coastguard Worker // Only call `madvise()` on enabled page-aligned alternate signal stack. Processes can
39*795d594fSAndroid Build Coastguard Worker // create different arbitrary alternate signal stacks and we do not want to erroneously
40*795d594fSAndroid Build Coastguard Worker // `madvise()` away pages that may hold data other than the alternate signal stack.
41*795d594fSAndroid Build Coastguard Worker if ((old_ss.ss_flags & SS_DISABLE) == 0 &&
42*795d594fSAndroid Build Coastguard Worker IsAlignedParam(old_ss.ss_sp, gPageSize) &&
43*795d594fSAndroid Build Coastguard Worker IsAlignedParam(old_ss.ss_size, gPageSize)) {
44*795d594fSAndroid Build Coastguard Worker CHECK_EQ(old_ss.ss_flags & SS_ONSTACK, 0);
45*795d594fSAndroid Build Coastguard Worker // Note: We're testing and benchmarking ART on devices with old kernels
46*795d594fSAndroid Build Coastguard Worker // which may not support `MADV_FREE`, so we do not check the result.
47*795d594fSAndroid Build Coastguard Worker // It should succeed on devices with Android 12+.
48*795d594fSAndroid Build Coastguard Worker madvise(old_ss.ss_sp, old_ss.ss_size, MADV_FREE);
49*795d594fSAndroid Build Coastguard Worker }
50*795d594fSAndroid Build Coastguard Worker }
51*795d594fSAndroid Build Coastguard Worker
52*795d594fSAndroid Build Coastguard Worker } // namespace art
53