Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 1 | /* |
| 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 Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 26 | #include <sys/mman.h> |
| 27 | #include <sys/syscall.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/wait.h> |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 30 | #include <unistd.h> |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 31 | |
| 32 | #include "android-base/macros.h" |
| 33 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 34 | #include "PtracerThread.h" |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 35 | #include "anon_vma_naming.h" |
| 36 | #include "log.h" |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 37 | |
| 38 | class Stack { |
| 39 | public: |
Chih-Hung Hsieh | 1c563d9 | 2016-04-29 15:44:04 -0700 | [diff] [blame] | 40 | explicit Stack(size_t size) : size_(size) { |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 41 | int prot = PROT_READ | PROT_WRITE; |
| 42 | int flags = MAP_PRIVATE | MAP_ANONYMOUS; |
| 43 | page_size_ = sysconf(_SC_PAGE_SIZE); |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 44 | size_ += page_size_ * 2; // guard pages |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 45 | base_ = mmap(NULL, size_, prot, flags, -1, 0); |
| 46 | if (base_ == MAP_FAILED) { |
| 47 | base_ = NULL; |
| 48 | size_ = 0; |
| 49 | return; |
| 50 | } |
| 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); |
| 54 | }; |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 55 | ~Stack() { munmap(base_, size_); }; |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 56 | void* top() { |
| 57 | return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(base_) + size_ - page_size_); |
| 58 | }; |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 59 | |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 60 | private: |
| 61 | DISALLOW_COPY_AND_ASSIGN(Stack); |
| 62 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 63 | void* base_; |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 64 | size_t size_; |
| 65 | size_t page_size_; |
| 66 | }; |
| 67 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 68 | PtracerThread::PtracerThread(const std::function<int()>& func) : child_pid_(0) { |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 69 | stack_ = std::make_unique<Stack>(PTHREAD_STACK_MIN); |
| 70 | if (stack_->top() == nullptr) { |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 71 | MEM_LOG_ALWAYS_FATAL("failed to mmap child stack: %s", strerror(errno)); |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | func_ = std::function<int()>{[&, func]() -> int { |
| 75 | // In the child thread, lock and unlock the mutex to wait for the parent |
| 76 | // to finish setting up for the child thread |
| 77 | std::unique_lock<std::mutex> lk(m_); |
| 78 | lk.unlock(); |
| 79 | _exit(func()); |
| 80 | }}; |
| 81 | } |
| 82 | |
| 83 | PtracerThread::~PtracerThread() { |
| 84 | Kill(); |
| 85 | Join(); |
| 86 | ClearTracer(); |
| 87 | stack_ = nullptr; |
| 88 | } |
| 89 | |
| 90 | bool PtracerThread::Start() { |
| 91 | std::unique_lock<std::mutex> lk(m_); |
| 92 | |
| 93 | // Convert from void(*)(void*) to lambda with captures |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 94 | auto proxy = [](void* arg) -> int { |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 95 | prctl(PR_SET_NAME, "libmemunreachable ptrace thread"); |
| 96 | return (*reinterpret_cast<std::function<int()>*>(arg))(); |
| 97 | }; |
| 98 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame^] | 99 | child_pid_ = clone(proxy, stack_->top(), CLONE_VM | CLONE_FS | CLONE_FILES /*|CLONE_UNTRACED*/, |
| 100 | reinterpret_cast<void*>(&func_)); |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 101 | if (child_pid_ < 0) { |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 102 | MEM_ALOGE("failed to clone child: %s", strerror(errno)); |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 103 | return false; |
| 104 | } |
| 105 | |
| 106 | SetTracer(child_pid_); |
| 107 | |
| 108 | lk.unlock(); |
| 109 | |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | int PtracerThread::Join() { |
| 114 | if (child_pid_ == -1) { |
| 115 | return -1; |
| 116 | } |
| 117 | int status; |
| 118 | int ret = TEMP_FAILURE_RETRY(waitpid(child_pid_, &status, __WALL)); |
| 119 | if (ret < 0) { |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 120 | MEM_ALOGE("waitpid %d failed: %s", child_pid_, strerror(errno)); |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 121 | return -1; |
| 122 | } |
| 123 | |
| 124 | child_pid_ = -1; |
| 125 | |
| 126 | if (WIFEXITED(status)) { |
| 127 | return WEXITSTATUS(status); |
| 128 | } else if (WIFSIGNALED(status)) { |
| 129 | return -WTERMSIG(status); |
| 130 | } else { |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 131 | MEM_ALOGE("unexpected status %x", status); |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 132 | return -1; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void PtracerThread::Kill() { |
| 137 | if (child_pid_ == -1) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | syscall(SYS_tkill, child_pid_, SIGKILL); |
| 142 | } |
| 143 | |
| 144 | void PtracerThread::SetTracer(pid_t tracer_pid) { |
| 145 | prctl(PR_SET_PTRACER, tracer_pid); |
| 146 | } |
| 147 | |
| 148 | void PtracerThread::ClearTracer() { |
| 149 | prctl(PR_SET_PTRACER, 0); |
| 150 | } |