blob: aca2a824253e92a8ce93b1fb439198a02db447bc [file] [log] [blame]
Colin Crossbcb4ed32016-01-14 15:35:40 -08001/*
2 * Copyright (C) 2016 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 <errno.h>
18#include <fcntl.h>
19#include <inttypes.h>
20#include <pthread.h>
21#include <sched.h>
22#include <stddef.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Colin Crossbcb4ed32016-01-14 15:35:40 -080026#include <sys/mman.h>
27#include <sys/syscall.h>
28#include <sys/types.h>
29#include <sys/wait.h>
Colin Crossa83881e2017-06-22 10:50:05 -070030#include <unistd.h>
Colin Crossbcb4ed32016-01-14 15:35:40 -080031
32#include "android-base/macros.h"
33
Colin Crossa83881e2017-06-22 10:50:05 -070034#include "PtracerThread.h"
Colin Crossbcb4ed32016-01-14 15:35:40 -080035#include "anon_vma_naming.h"
36#include "log.h"
Colin Crossbcb4ed32016-01-14 15:35:40 -080037
Colin Crossa9939e92017-06-21 13:13:00 -070038namespace android {
39
Colin Crossbcb4ed32016-01-14 15:35:40 -080040class Stack {
41 public:
Chih-Hung Hsieh1c563d92016-04-29 15:44:04 -070042 explicit Stack(size_t size) : size_(size) {
Colin Crossbcb4ed32016-01-14 15:35:40 -080043 int prot = PROT_READ | PROT_WRITE;
44 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
45 page_size_ = sysconf(_SC_PAGE_SIZE);
Colin Crossa83881e2017-06-22 10:50:05 -070046 size_ += page_size_ * 2; // guard pages
Colin Crossbcb4ed32016-01-14 15:35:40 -080047 base_ = mmap(NULL, size_, prot, flags, -1, 0);
48 if (base_ == MAP_FAILED) {
49 base_ = NULL;
50 size_ = 0;
51 return;
52 }
53 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, base_, size_, "libmemunreachable stack");
54 mprotect(base_, page_size_, PROT_NONE);
55 mprotect(top(), page_size_, PROT_NONE);
56 };
Colin Crossa83881e2017-06-22 10:50:05 -070057 ~Stack() { munmap(base_, size_); };
Colin Crossbcb4ed32016-01-14 15:35:40 -080058 void* top() {
59 return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(base_) + size_ - page_size_);
60 };
Colin Crossa83881e2017-06-22 10:50:05 -070061
Colin Crossbcb4ed32016-01-14 15:35:40 -080062 private:
63 DISALLOW_COPY_AND_ASSIGN(Stack);
64
Colin Crossa83881e2017-06-22 10:50:05 -070065 void* base_;
Colin Crossbcb4ed32016-01-14 15:35:40 -080066 size_t size_;
67 size_t page_size_;
68};
69
Colin Crossa83881e2017-06-22 10:50:05 -070070PtracerThread::PtracerThread(const std::function<int()>& func) : child_pid_(0) {
Colin Crossbcb4ed32016-01-14 15:35:40 -080071 stack_ = std::make_unique<Stack>(PTHREAD_STACK_MIN);
72 if (stack_->top() == nullptr) {
Christopher Ferris47dea712017-05-03 17:34:29 -070073 MEM_LOG_ALWAYS_FATAL("failed to mmap child stack: %s", strerror(errno));
Colin Crossbcb4ed32016-01-14 15:35:40 -080074 }
75
76 func_ = std::function<int()>{[&, func]() -> int {
77 // In the child thread, lock and unlock the mutex to wait for the parent
78 // to finish setting up for the child thread
79 std::unique_lock<std::mutex> lk(m_);
80 lk.unlock();
81 _exit(func());
82 }};
83}
84
85PtracerThread::~PtracerThread() {
86 Kill();
87 Join();
88 ClearTracer();
89 stack_ = nullptr;
90}
91
92bool PtracerThread::Start() {
93 std::unique_lock<std::mutex> lk(m_);
94
95 // Convert from void(*)(void*) to lambda with captures
Colin Crossa83881e2017-06-22 10:50:05 -070096 auto proxy = [](void* arg) -> int {
Colin Crossbcb4ed32016-01-14 15:35:40 -080097 prctl(PR_SET_NAME, "libmemunreachable ptrace thread");
98 return (*reinterpret_cast<std::function<int()>*>(arg))();
99 };
100
Colin Crossa83881e2017-06-22 10:50:05 -0700101 child_pid_ = clone(proxy, stack_->top(), CLONE_VM | CLONE_FS | CLONE_FILES /*|CLONE_UNTRACED*/,
102 reinterpret_cast<void*>(&func_));
Colin Crossbcb4ed32016-01-14 15:35:40 -0800103 if (child_pid_ < 0) {
Christopher Ferris47dea712017-05-03 17:34:29 -0700104 MEM_ALOGE("failed to clone child: %s", strerror(errno));
Colin Crossbcb4ed32016-01-14 15:35:40 -0800105 return false;
106 }
107
108 SetTracer(child_pid_);
109
110 lk.unlock();
111
112 return true;
113}
114
115int PtracerThread::Join() {
116 if (child_pid_ == -1) {
117 return -1;
118 }
119 int status;
120 int ret = TEMP_FAILURE_RETRY(waitpid(child_pid_, &status, __WALL));
121 if (ret < 0) {
Christopher Ferris47dea712017-05-03 17:34:29 -0700122 MEM_ALOGE("waitpid %d failed: %s", child_pid_, strerror(errno));
Colin Crossbcb4ed32016-01-14 15:35:40 -0800123 return -1;
124 }
125
126 child_pid_ = -1;
127
128 if (WIFEXITED(status)) {
129 return WEXITSTATUS(status);
130 } else if (WIFSIGNALED(status)) {
131 return -WTERMSIG(status);
132 } else {
Christopher Ferris47dea712017-05-03 17:34:29 -0700133 MEM_ALOGE("unexpected status %x", status);
Colin Crossbcb4ed32016-01-14 15:35:40 -0800134 return -1;
135 }
136}
137
138void PtracerThread::Kill() {
139 if (child_pid_ == -1) {
140 return;
141 }
142
143 syscall(SYS_tkill, child_pid_, SIGKILL);
144}
145
146void PtracerThread::SetTracer(pid_t tracer_pid) {
147 prctl(PR_SET_PTRACER, tracer_pid);
148}
149
150void PtracerThread::ClearTracer() {
151 prctl(PR_SET_PTRACER, 0);
152}
Colin Crossa9939e92017-06-21 13:13:00 -0700153
154} // namespace android