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 <signal.h>
18 #include <sys/mman.h>
19
20 #include "base/globals.h"
21 #include "base/bit_utils.h"
22 #include "thread.h"
23
24 namespace art HIDDEN {
25
SetUpAlternateSignalStack()26 void Thread::SetUpAlternateSignalStack() {
27 // Bionic does this for us.
28 }
29
TearDownAlternateSignalStack()30 void Thread::TearDownAlternateSignalStack() {
31 // Bionic does this for us.
32 }
33
MadviseAwayAlternateSignalStack()34 void Thread::MadviseAwayAlternateSignalStack() {
35 stack_t old_ss;
36 int result = sigaltstack(nullptr, &old_ss);
37 CHECK_EQ(result, 0);
38 // Only call `madvise()` on enabled page-aligned alternate signal stack. Processes can
39 // create different arbitrary alternate signal stacks and we do not want to erroneously
40 // `madvise()` away pages that may hold data other than the alternate signal stack.
41 if ((old_ss.ss_flags & SS_DISABLE) == 0 &&
42 IsAlignedParam(old_ss.ss_sp, gPageSize) &&
43 IsAlignedParam(old_ss.ss_size, gPageSize)) {
44 CHECK_EQ(old_ss.ss_flags & SS_ONSTACK, 0);
45 // Note: We're testing and benchmarking ART on devices with old kernels
46 // which may not support `MADV_FREE`, so we do not check the result.
47 // It should succeed on devices with Android 12+.
48 madvise(old_ss.ss_sp, old_ss.ss_size, MADV_FREE);
49 }
50 }
51
52 } // namespace art
53