2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
28 #include <sys/syscall.h>
29 #include <sys/types.h>
32 #include "android-base/macros.h"
34 #include "anon_vma_naming.h"
36 #include "PtracerThread.h"
40 explicit Stack(size_t size) : size_(size) {
41 int prot = PROT_READ | PROT_WRITE;
42 int flags = MAP_PRIVATE | MAP_ANONYMOUS;
43 page_size_ = sysconf(_SC_PAGE_SIZE);
44 size_ += page_size_*2; // guard pages
45 base_ = mmap(NULL, size_, prot, flags, -1, 0);
46 if (base_ == MAP_FAILED) {
51 prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, base_, size_, "libmemunreachable stack");
52 mprotect(base_, page_size_, PROT_NONE);
53 mprotect(top(), page_size_, PROT_NONE);
59 return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(base_) + size_ - page_size_);
62 DISALLOW_COPY_AND_ASSIGN(Stack);
69 PtracerThread::PtracerThread(const std::function<int()>& func) :
71 stack_ = std::make_unique<Stack>(PTHREAD_STACK_MIN);
72 if (stack_->top() == nullptr) {
73 LOG_ALWAYS_FATAL("failed to mmap child stack: %s", strerror(errno));
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_);
85 PtracerThread::~PtracerThread() {
92 bool PtracerThread::Start() {
93 std::unique_lock<std::mutex> lk(m_);
95 // Convert from void(*)(void*) to lambda with captures
96 auto proxy = [](void *arg) -> int {
97 prctl(PR_SET_NAME, "libmemunreachable ptrace thread");
98 return (*reinterpret_cast<std::function<int()>*>(arg))();
101 child_pid_ = clone(proxy, stack_->top(),
102 CLONE_VM|CLONE_FS|CLONE_FILES/*|CLONE_UNTRACED*/,
103 reinterpret_cast<void*>(&func_));
104 if (child_pid_ < 0) {
105 ALOGE("failed to clone child: %s", strerror(errno));
109 SetTracer(child_pid_);
116 int PtracerThread::Join() {
117 if (child_pid_ == -1) {
121 int ret = TEMP_FAILURE_RETRY(waitpid(child_pid_, &status, __WALL));
123 ALOGE("waitpid %d failed: %s", child_pid_, strerror(errno));
129 if (WIFEXITED(status)) {
130 return WEXITSTATUS(status);
131 } else if (WIFSIGNALED(status)) {
132 return -WTERMSIG(status);
134 ALOGE("unexpected status %x", status);
139 void PtracerThread::Kill() {
140 if (child_pid_ == -1) {
144 syscall(SYS_tkill, child_pid_, SIGKILL);
147 void PtracerThread::SetTracer(pid_t tracer_pid) {
148 prctl(PR_SET_PTRACER, tracer_pid);
151 void PtracerThread::ClearTracer() {
152 prctl(PR_SET_PTRACER, 0);