| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2005 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 | #define LOG_TAG "ProcessState" | 
|  | 18 |  | 
| Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 19 | #include <binder/ProcessState.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 |  | 
| Steven Moreland | 13a43c9 | 2021-08-30 13:21:48 -0700 | [diff] [blame] | 21 | #include <android-base/result.h> | 
| Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 22 | #include <binder/BpBinder.h> | 
|  | 23 | #include <binder/IPCThreadState.h> | 
| Steven Moreland | 2716e11 | 2018-02-23 14:57:20 -0800 | [diff] [blame] | 24 | #include <binder/IServiceManager.h> | 
| Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 25 | #include <binder/Stability.h> | 
| Steven Moreland | 2716e11 | 2018-02-23 14:57:20 -0800 | [diff] [blame] | 26 | #include <cutils/atomic.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | #include <utils/Log.h> | 
|  | 28 | #include <utils/String8.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | #include <utils/threads.h> | 
|  | 30 |  | 
| Steven Moreland | a4853cd | 2019-07-12 15:44:37 -0700 | [diff] [blame] | 31 | #include "Static.h" | 
| Steven Moreland | 6ba5a25 | 2021-05-04 22:49:00 +0000 | [diff] [blame] | 32 | #include "binder_module.h" | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 |  | 
|  | 34 | #include <errno.h> | 
|  | 35 | #include <fcntl.h> | 
| Steven Moreland | 4fdb12f | 2020-07-21 02:21:48 +0000 | [diff] [blame] | 36 | #include <mutex> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | #include <stdio.h> | 
|  | 38 | #include <stdlib.h> | 
|  | 39 | #include <unistd.h> | 
|  | 40 | #include <sys/ioctl.h> | 
|  | 41 | #include <sys/mman.h> | 
|  | 42 | #include <sys/stat.h> | 
| Philip Cuadra | a082fa8 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 43 | #include <sys/types.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 |  | 
| Steven Moreland | 072cc7e | 2019-07-12 21:01:54 +0000 | [diff] [blame] | 45 | #define BINDER_VM_SIZE ((1 * 1024 * 1024) - sysconf(_SC_PAGE_SIZE) * 2) | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 46 | #define DEFAULT_MAX_BINDER_THREADS 15 | 
| Hang Lu | b185ac0 | 2021-03-24 13:17:22 +0800 | [diff] [blame] | 47 | #define DEFAULT_ENABLE_ONEWAY_SPAM_DETECTION 1 | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 |  | 
| Martijn Coenen | 7bca77a | 2019-03-13 11:51:08 +0000 | [diff] [blame] | 49 | #ifdef __ANDROID_VNDK__ | 
|  | 50 | const char* kDefaultDriver = "/dev/vndbinder"; | 
|  | 51 | #else | 
| Steven Moreland | 2ae2f5e | 2018-07-06 13:02:53 -0700 | [diff] [blame] | 52 | const char* kDefaultDriver = "/dev/binder"; | 
| Martijn Coenen | 7bca77a | 2019-03-13 11:51:08 +0000 | [diff] [blame] | 53 | #endif | 
| Steven Moreland | 2ae2f5e | 2018-07-06 13:02:53 -0700 | [diff] [blame] | 54 |  | 
| Philip Cuadra | a082fa8 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 55 | // ------------------------------------------------------------------------- | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 |  | 
|  | 57 | namespace android { | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 58 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | class PoolThread : public Thread | 
|  | 60 | { | 
|  | 61 | public: | 
| Chih-Hung Hsieh | e2347b7 | 2016-04-25 15:41:05 -0700 | [diff] [blame] | 62 | explicit PoolThread(bool isMain) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | : mIsMain(isMain) | 
|  | 64 | { | 
|  | 65 | } | 
| Jooyung Han | 1b228f4 | 2020-01-30 13:41:12 +0900 | [diff] [blame] | 66 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | protected: | 
|  | 68 | virtual bool threadLoop() | 
|  | 69 | { | 
|  | 70 | IPCThreadState::self()->joinThreadPool(mIsMain); | 
|  | 71 | return false; | 
|  | 72 | } | 
| Jooyung Han | 1b228f4 | 2020-01-30 13:41:12 +0900 | [diff] [blame] | 73 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | const bool mIsMain; | 
|  | 75 | }; | 
|  | 76 |  | 
|  | 77 | sp<ProcessState> ProcessState::self() | 
|  | 78 | { | 
| Steven Moreland | 4fdb12f | 2020-07-21 02:21:48 +0000 | [diff] [blame] | 79 | return init(kDefaultDriver, false /*requireDefault*/); | 
| Martijn Coenen | 55d871c | 2017-03-21 15:56:40 -0700 | [diff] [blame] | 80 | } | 
|  | 81 |  | 
|  | 82 | sp<ProcessState> ProcessState::initWithDriver(const char* driver) | 
|  | 83 | { | 
| Steven Moreland | 4fdb12f | 2020-07-21 02:21:48 +0000 | [diff] [blame] | 84 | return init(driver, true /*requireDefault*/); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | } | 
|  | 86 |  | 
| Steven Moreland | 072cc7e | 2019-07-12 21:01:54 +0000 | [diff] [blame] | 87 | sp<ProcessState> ProcessState::selfOrNull() | 
|  | 88 | { | 
| Steven Moreland | 4fdb12f | 2020-07-21 02:21:48 +0000 | [diff] [blame] | 89 | return init(nullptr, false /*requireDefault*/); | 
|  | 90 | } | 
|  | 91 |  | 
| Steven Moreland | ee9df90 | 2021-10-14 14:00:08 -0700 | [diff] [blame] | 92 | [[clang::no_destroy]] static sp<ProcessState> gProcess; | 
|  | 93 | [[clang::no_destroy]] static std::mutex gProcessMutex; | 
|  | 94 |  | 
|  | 95 | static void verifyNotForked(bool forked) { | 
| Steven Moreland | bd98e0f | 2021-10-14 14:24:15 -0700 | [diff] [blame] | 96 | LOG_ALWAYS_FATAL_IF(forked, "libbinder ProcessState can not be used after fork"); | 
| Steven Moreland | ee9df90 | 2021-10-14 14:00:08 -0700 | [diff] [blame] | 97 | } | 
|  | 98 |  | 
| Steven Moreland | 4fdb12f | 2020-07-21 02:21:48 +0000 | [diff] [blame] | 99 | sp<ProcessState> ProcessState::init(const char *driver, bool requireDefault) | 
|  | 100 | { | 
| Steven Moreland | 4fdb12f | 2020-07-21 02:21:48 +0000 | [diff] [blame] | 101 |  | 
|  | 102 | if (driver == nullptr) { | 
|  | 103 | std::lock_guard<std::mutex> l(gProcessMutex); | 
| Steven Moreland | ee9df90 | 2021-10-14 14:00:08 -0700 | [diff] [blame] | 104 | if (gProcess) { | 
|  | 105 | verifyNotForked(gProcess->mForked); | 
|  | 106 | } | 
| Steven Moreland | 4fdb12f | 2020-07-21 02:21:48 +0000 | [diff] [blame] | 107 | return gProcess; | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | [[clang::no_destroy]] static std::once_flag gProcessOnce; | 
|  | 111 | std::call_once(gProcessOnce, [&](){ | 
|  | 112 | if (access(driver, R_OK) == -1) { | 
|  | 113 | ALOGE("Binder driver %s is unavailable. Using /dev/binder instead.", driver); | 
|  | 114 | driver = "/dev/binder"; | 
|  | 115 | } | 
|  | 116 |  | 
| Steven Moreland | ee9df90 | 2021-10-14 14:00:08 -0700 | [diff] [blame] | 117 | // we must install these before instantiating the gProcess object, | 
|  | 118 | // otherwise this would race with creating it, and there could be the | 
|  | 119 | // possibility of an invalid gProcess object forked by another thread | 
|  | 120 | // before these are installed | 
|  | 121 | int ret = pthread_atfork(ProcessState::onFork, ProcessState::parentPostFork, | 
|  | 122 | ProcessState::childPostFork); | 
|  | 123 | LOG_ALWAYS_FATAL_IF(ret != 0, "pthread_atfork error %s", strerror(ret)); | 
|  | 124 |  | 
| Steven Moreland | 4fdb12f | 2020-07-21 02:21:48 +0000 | [diff] [blame] | 125 | std::lock_guard<std::mutex> l(gProcessMutex); | 
| Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 126 | gProcess = sp<ProcessState>::make(driver); | 
| Steven Moreland | 4fdb12f | 2020-07-21 02:21:48 +0000 | [diff] [blame] | 127 | }); | 
|  | 128 |  | 
|  | 129 | if (requireDefault) { | 
|  | 130 | // Detect if we are trying to initialize with a different driver, and | 
|  | 131 | // consider that an error. ProcessState will only be initialized once above. | 
|  | 132 | LOG_ALWAYS_FATAL_IF(gProcess->getDriverName() != driver, | 
|  | 133 | "ProcessState was already initialized with %s," | 
|  | 134 | " can't initialize with %s.", | 
|  | 135 | gProcess->getDriverName().c_str(), driver); | 
|  | 136 | } | 
|  | 137 |  | 
| Steven Moreland | ee9df90 | 2021-10-14 14:00:08 -0700 | [diff] [blame] | 138 | verifyNotForked(gProcess->mForked); | 
| Colin Cross | 9d45ccc | 2017-06-20 17:48:33 -0700 | [diff] [blame] | 139 | return gProcess; | 
|  | 140 | } | 
|  | 141 |  | 
| Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 142 | sp<IBinder> ProcessState::getContextObject(const sp<IBinder>& /*caller*/) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | { | 
| Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 144 | sp<IBinder> context = getStrongProxyForHandle(0); | 
|  | 145 |  | 
| Steven Moreland | 4da8fb0 | 2020-12-29 22:51:32 +0000 | [diff] [blame] | 146 | if (context) { | 
|  | 147 | // The root object is special since we get it directly from the driver, it is never | 
|  | 148 | // written by Parcell::writeStrongBinder. | 
|  | 149 | internal::Stability::markCompilationUnit(context.get()); | 
|  | 150 | } else { | 
|  | 151 | ALOGW("Not able to get context object on %s.", mDriverName.c_str()); | 
| Steven Moreland | 8d93a71 | 2020-02-19 15:16:15 -0800 | [diff] [blame] | 152 | } | 
|  | 153 |  | 
| Steven Moreland | c709dd8 | 2019-08-05 20:30:14 -0700 | [diff] [blame] | 154 | return context; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | } | 
|  | 156 |  | 
| Steven Moreland | ee9df90 | 2021-10-14 14:00:08 -0700 | [diff] [blame] | 157 | void ProcessState::onFork() { | 
|  | 158 | // make sure another thread isn't currently retrieving ProcessState | 
|  | 159 | gProcessMutex.lock(); | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | void ProcessState::parentPostFork() { | 
|  | 163 | gProcessMutex.unlock(); | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | void ProcessState::childPostFork() { | 
|  | 167 | // another thread might call fork before gProcess is instantiated, but after | 
|  | 168 | // the thread handler is installed | 
|  | 169 | if (gProcess) { | 
|  | 170 | gProcess->mForked = true; | 
|  | 171 | } | 
|  | 172 | gProcessMutex.unlock(); | 
|  | 173 | } | 
|  | 174 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | void ProcessState::startThreadPool() | 
|  | 176 | { | 
|  | 177 | AutoMutex _l(mLock); | 
|  | 178 | if (!mThreadPoolStarted) { | 
| Steven Moreland | f5a5bb8 | 2021-12-16 00:04:29 +0000 | [diff] [blame] | 179 | if (mMaxThreads == 0) { | 
|  | 180 | ALOGW("Extra binder thread started, but 0 threads requested. Do not use " | 
|  | 181 | "*startThreadPool when zero threads are requested."); | 
|  | 182 | } | 
|  | 183 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 184 | mThreadPoolStarted = true; | 
|  | 185 | spawnPooledThread(true); | 
|  | 186 | } | 
|  | 187 | } | 
|  | 188 |  | 
| Steven Moreland | 6109662 | 2020-08-31 23:36:39 +0000 | [diff] [blame] | 189 | bool ProcessState::becomeContextManager() | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | { | 
| Steven Moreland | 4411d71 | 2019-07-12 14:02:53 -0700 | [diff] [blame] | 191 | AutoMutex _l(mLock); | 
| Jeff Brown | e16986c | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 192 |  | 
| Steven Moreland | 4411d71 | 2019-07-12 14:02:53 -0700 | [diff] [blame] | 193 | flat_binder_object obj { | 
|  | 194 | .flags = FLAT_BINDER_FLAG_TXN_SECURITY_CTX, | 
|  | 195 | }; | 
| Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 196 |  | 
| Steven Moreland | 4411d71 | 2019-07-12 14:02:53 -0700 | [diff] [blame] | 197 | int result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR_EXT, &obj); | 
| Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 198 |  | 
| Steven Moreland | 4411d71 | 2019-07-12 14:02:53 -0700 | [diff] [blame] | 199 | // fallback to original method | 
|  | 200 | if (result != 0) { | 
|  | 201 | android_errorWriteLog(0x534e4554, "121035042"); | 
| Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 202 |  | 
| Hungming Chen | 28b4252 | 2020-08-28 17:29:55 +0800 | [diff] [blame] | 203 | int unused = 0; | 
|  | 204 | result = ioctl(mDriverFD, BINDER_SET_CONTEXT_MGR, &unused); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 205 | } | 
| Steven Moreland | 4411d71 | 2019-07-12 14:02:53 -0700 | [diff] [blame] | 206 |  | 
|  | 207 | if (result == -1) { | 
| Steven Moreland | 4411d71 | 2019-07-12 14:02:53 -0700 | [diff] [blame] | 208 | ALOGE("Binder ioctl to become context manager failed: %s\n", strerror(errno)); | 
|  | 209 | } | 
|  | 210 |  | 
|  | 211 | return result == 0; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | } | 
|  | 213 |  | 
| Colin Cross | 9d45ccc | 2017-06-20 17:48:33 -0700 | [diff] [blame] | 214 | // Get references to userspace objects held by the kernel binder driver | 
|  | 215 | // Writes up to count elements into buf, and returns the total number | 
|  | 216 | // of references the kernel has, which may be larger than count. | 
|  | 217 | // buf may be NULL if count is 0.  The pointers returned by this method | 
|  | 218 | // should only be used for debugging and not dereferenced, they may | 
|  | 219 | // already be invalid. | 
|  | 220 | ssize_t ProcessState::getKernelReferences(size_t buf_count, uintptr_t* buf) | 
|  | 221 | { | 
| Colin Cross | 9d45ccc | 2017-06-20 17:48:33 -0700 | [diff] [blame] | 222 | binder_node_debug_info info = {}; | 
|  | 223 |  | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 224 | uintptr_t* end = buf ? buf + buf_count : nullptr; | 
| Colin Cross | 9d45ccc | 2017-06-20 17:48:33 -0700 | [diff] [blame] | 225 | size_t count = 0; | 
|  | 226 |  | 
|  | 227 | do { | 
|  | 228 | status_t result = ioctl(mDriverFD, BINDER_GET_NODE_DEBUG_INFO, &info); | 
|  | 229 | if (result < 0) { | 
|  | 230 | return -1; | 
|  | 231 | } | 
|  | 232 | if (info.ptr != 0) { | 
|  | 233 | if (buf && buf < end) | 
|  | 234 | *buf++ = info.ptr; | 
|  | 235 | count++; | 
|  | 236 | if (buf && buf < end) | 
|  | 237 | *buf++ = info.cookie; | 
|  | 238 | count++; | 
|  | 239 | } | 
|  | 240 | } while (info.ptr != 0); | 
|  | 241 |  | 
|  | 242 | return count; | 
|  | 243 | } | 
|  | 244 |  | 
| Jon Spivack | 902e6fc | 2019-10-18 21:22:37 -0700 | [diff] [blame] | 245 | // Queries the driver for the current strong reference count of the node | 
|  | 246 | // that the handle points to. Can only be used by the servicemanager. | 
|  | 247 | // | 
|  | 248 | // Returns -1 in case of failure, otherwise the strong reference count. | 
| Steven Moreland | e839388 | 2020-12-18 02:27:20 +0000 | [diff] [blame] | 249 | ssize_t ProcessState::getStrongRefCountForNode(const sp<BpBinder>& binder) { | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 250 | if (binder->isRpcBinder()) return -1; | 
|  | 251 |  | 
| Jon Spivack | 902e6fc | 2019-10-18 21:22:37 -0700 | [diff] [blame] | 252 | binder_node_info_for_ref info; | 
|  | 253 | memset(&info, 0, sizeof(binder_node_info_for_ref)); | 
|  | 254 |  | 
| Steven Moreland | 9915762 | 2021-09-13 16:27:34 -0700 | [diff] [blame] | 255 | info.handle = binder->getPrivateAccessor().binderHandle(); | 
| Jon Spivack | 902e6fc | 2019-10-18 21:22:37 -0700 | [diff] [blame] | 256 |  | 
|  | 257 | status_t result = ioctl(mDriverFD, BINDER_GET_NODE_INFO_FOR_REF, &info); | 
|  | 258 |  | 
|  | 259 | if (result != OK) { | 
|  | 260 | static bool logged = false; | 
|  | 261 | if (!logged) { | 
|  | 262 | ALOGW("Kernel does not support BINDER_GET_NODE_INFO_FOR_REF."); | 
|  | 263 | logged = true; | 
|  | 264 | } | 
|  | 265 | return -1; | 
|  | 266 | } | 
|  | 267 |  | 
|  | 268 | return info.strong_count; | 
|  | 269 | } | 
|  | 270 |  | 
| Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 271 | void ProcessState::setCallRestriction(CallRestriction restriction) { | 
| Steven Moreland | 28723ae | 2019-04-01 18:52:30 -0700 | [diff] [blame] | 272 | LOG_ALWAYS_FATAL_IF(IPCThreadState::selfOrNull() != nullptr, | 
|  | 273 | "Call restrictions must be set before the threadpool is started."); | 
| Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 274 |  | 
|  | 275 | mCallRestriction = restriction; | 
|  | 276 | } | 
|  | 277 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | ProcessState::handle_entry* ProcessState::lookupHandleLocked(int32_t handle) | 
|  | 279 | { | 
|  | 280 | const size_t N=mHandleToObject.size(); | 
|  | 281 | if (N <= (size_t)handle) { | 
|  | 282 | handle_entry e; | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 283 | e.binder = nullptr; | 
|  | 284 | e.refs = nullptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | status_t err = mHandleToObject.insertAt(e, N, handle+1-N); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 286 | if (err < NO_ERROR) return nullptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 287 | } | 
|  | 288 | return &mHandleToObject.editItemAt(handle); | 
|  | 289 | } | 
|  | 290 |  | 
|  | 291 | sp<IBinder> ProcessState::getStrongProxyForHandle(int32_t handle) | 
|  | 292 | { | 
|  | 293 | sp<IBinder> result; | 
|  | 294 |  | 
|  | 295 | AutoMutex _l(mLock); | 
|  | 296 |  | 
|  | 297 | handle_entry* e = lookupHandleLocked(handle); | 
|  | 298 |  | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 299 | if (e != nullptr) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | // We need to create a new BpBinder if there isn't currently one, OR we | 
| Steven Moreland | e171d62 | 2019-07-17 16:06:01 -0700 | [diff] [blame] | 301 | // are unable to acquire a weak reference on this current one.  The | 
|  | 302 | // attemptIncWeak() is safe because we know the BpBinder destructor will always | 
|  | 303 | // call expungeHandle(), which acquires the same lock we are holding now. | 
|  | 304 | // We need to do this because there is a race condition between someone | 
|  | 305 | // releasing a reference on this BpBinder, and a new reference on its handle | 
|  | 306 | // arriving from the driver. | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 307 | IBinder* b = e->binder; | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 308 | if (b == nullptr || !e->refs->attemptIncWeak(this)) { | 
| Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 309 | if (handle == 0) { | 
|  | 310 | // Special case for context manager... | 
|  | 311 | // The context manager is the only object for which we create | 
|  | 312 | // a BpBinder proxy without already holding a reference. | 
|  | 313 | // Perform a dummy transaction to ensure the context manager | 
|  | 314 | // is registered before we create the first local reference | 
|  | 315 | // to it (which will occur when creating the BpBinder). | 
|  | 316 | // If a local reference is created for the BpBinder when the | 
|  | 317 | // context manager is not present, the driver will fail to | 
|  | 318 | // provide a reference to the context manager, but the | 
|  | 319 | // driver API does not return status. | 
|  | 320 | // | 
|  | 321 | // Note that this is not race-free if the context manager | 
|  | 322 | // dies while this code runs. | 
| Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 323 |  | 
| Steven Moreland | 9514b20 | 2020-09-21 18:03:27 +0000 | [diff] [blame] | 324 | IPCThreadState* ipc = IPCThreadState::self(); | 
|  | 325 |  | 
|  | 326 | CallRestriction originalCallRestriction = ipc->getCallRestriction(); | 
|  | 327 | ipc->setCallRestriction(CallRestriction::NONE); | 
|  | 328 |  | 
| Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 329 | Parcel data; | 
| Steven Moreland | 9514b20 | 2020-09-21 18:03:27 +0000 | [diff] [blame] | 330 | status_t status = ipc->transact( | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 331 | 0, IBinder::PING_TRANSACTION, data, nullptr, 0); | 
| Steven Moreland | 9514b20 | 2020-09-21 18:03:27 +0000 | [diff] [blame] | 332 |  | 
|  | 333 | ipc->setCallRestriction(originalCallRestriction); | 
|  | 334 |  | 
| Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 335 | if (status == DEAD_OBJECT) | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 336 | return nullptr; | 
| Todd Poynor | a7b0f04 | 2013-06-18 17:25:37 -0700 | [diff] [blame] | 337 | } | 
|  | 338 |  | 
| Steven Moreland | 9915762 | 2021-09-13 16:27:34 -0700 | [diff] [blame] | 339 | sp<BpBinder> b = BpBinder::PrivateAccessor::create(handle); | 
| Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 340 | e->binder = b.get(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | if (b) e->refs = b->getWeakRefs(); | 
|  | 342 | result = b; | 
|  | 343 | } else { | 
|  | 344 | // This little bit of nastyness is to allow us to add a primary | 
|  | 345 | // reference to the remote proxy when this team doesn't have one | 
|  | 346 | // but another team is sending the handle to us. | 
|  | 347 | result.force_set(b); | 
|  | 348 | e->refs->decWeak(this); | 
|  | 349 | } | 
|  | 350 | } | 
|  | 351 |  | 
|  | 352 | return result; | 
|  | 353 | } | 
|  | 354 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | void ProcessState::expungeHandle(int32_t handle, IBinder* binder) | 
|  | 356 | { | 
|  | 357 | AutoMutex _l(mLock); | 
| Jooyung Han | 1b228f4 | 2020-01-30 13:41:12 +0900 | [diff] [blame] | 358 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | handle_entry* e = lookupHandleLocked(handle); | 
|  | 360 |  | 
|  | 361 | // This handle may have already been replaced with a new BpBinder | 
|  | 362 | // (if someone failed the AttemptIncWeak() above); we don't want | 
|  | 363 | // to overwrite it. | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 364 | if (e && e->binder == binder) e->binder = nullptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | } | 
|  | 366 |  | 
| Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 367 | String8 ProcessState::makeBinderThreadName() { | 
|  | 368 | int32_t s = android_atomic_add(1, &mThreadPoolSeq); | 
| Philip Cuadra | a082fa8 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 369 | pid_t pid = getpid(); | 
| Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 370 | String8 name; | 
| Philip Cuadra | a082fa8 | 2016-04-08 10:29:14 -0700 | [diff] [blame] | 371 | name.appendFormat("Binder:%d_%X", pid, s); | 
| Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 372 | return name; | 
|  | 373 | } | 
|  | 374 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | void ProcessState::spawnPooledThread(bool isMain) | 
|  | 376 | { | 
|  | 377 | if (mThreadPoolStarted) { | 
| Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 378 | String8 name = makeBinderThreadName(); | 
|  | 379 | ALOGV("Spawning new pooled thread, name=%s\n", name.string()); | 
| Steven Moreland | 1a3a8ef | 2021-04-02 02:52:46 +0000 | [diff] [blame] | 380 | sp<Thread> t = sp<PoolThread>::make(isMain); | 
| Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 381 | t->run(name.string()); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 382 | } | 
|  | 383 | } | 
|  | 384 |  | 
| Mathias Agopian | 1b80f79 | 2012-04-17 16:11:08 -0700 | [diff] [blame] | 385 | status_t ProcessState::setThreadPoolMaxThreadCount(size_t maxThreads) { | 
| Steven Moreland | 1fdb98b | 2020-03-06 21:42:12 +0000 | [diff] [blame] | 386 | LOG_ALWAYS_FATAL_IF(mThreadPoolStarted && maxThreads < mMaxThreads, | 
|  | 387 | "Binder threadpool cannot be shrunk after starting"); | 
| Mathias Agopian | 1b80f79 | 2012-04-17 16:11:08 -0700 | [diff] [blame] | 388 | status_t result = NO_ERROR; | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 389 | if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &maxThreads) != -1) { | 
|  | 390 | mMaxThreads = maxThreads; | 
|  | 391 | } else { | 
| Mathias Agopian | 1b80f79 | 2012-04-17 16:11:08 -0700 | [diff] [blame] | 392 | result = -errno; | 
|  | 393 | ALOGE("Binder ioctl to set max threads failed: %s", strerror(-result)); | 
|  | 394 | } | 
|  | 395 | return result; | 
|  | 396 | } | 
|  | 397 |  | 
| Yifan Hong | 84bedeb | 2021-04-21 21:37:17 -0700 | [diff] [blame] | 398 | size_t ProcessState::getThreadPoolMaxThreadCount() const { | 
|  | 399 | // may actually be one more than this, if join is called | 
|  | 400 | if (mThreadPoolStarted) return mMaxThreads; | 
|  | 401 | // must not be initialized or maybe has poll thread setup, we | 
|  | 402 | // currently don't track this in libbinder | 
|  | 403 | return 0; | 
|  | 404 | } | 
|  | 405 |  | 
| Hang Lu | b185ac0 | 2021-03-24 13:17:22 +0800 | [diff] [blame] | 406 | status_t ProcessState::enableOnewaySpamDetection(bool enable) { | 
|  | 407 | uint32_t enableDetection = enable ? 1 : 0; | 
|  | 408 | if (ioctl(mDriverFD, BINDER_ENABLE_ONEWAY_SPAM_DETECTION, &enableDetection) == -1) { | 
| Martijn Coenen | de0a39e | 2021-04-22 14:38:46 +0200 | [diff] [blame] | 409 | ALOGI("Binder ioctl to enable oneway spam detection failed: %s", strerror(errno)); | 
| Hang Lu | b185ac0 | 2021-03-24 13:17:22 +0800 | [diff] [blame] | 410 | return -errno; | 
|  | 411 | } | 
|  | 412 | return NO_ERROR; | 
|  | 413 | } | 
|  | 414 |  | 
| Mathias Agopian | e3e43b3 | 2013-03-07 15:34:28 -0800 | [diff] [blame] | 415 | void ProcessState::giveThreadPoolName() { | 
|  | 416 | androidSetThreadName( makeBinderThreadName().string() ); | 
|  | 417 | } | 
|  | 418 |  | 
| Iliyan Malchev | 3206224 | 2017-04-10 14:06:11 -0700 | [diff] [blame] | 419 | String8 ProcessState::getDriverName() { | 
|  | 420 | return mDriverName; | 
|  | 421 | } | 
|  | 422 |  | 
| Steven Moreland | 13a43c9 | 2021-08-30 13:21:48 -0700 | [diff] [blame] | 423 | static base::Result<int> open_driver(const char* driver) { | 
| Martijn Coenen | 55d871c | 2017-03-21 15:56:40 -0700 | [diff] [blame] | 424 | int fd = open(driver, O_RDWR | O_CLOEXEC); | 
| Steven Moreland | 13a43c9 | 2021-08-30 13:21:48 -0700 | [diff] [blame] | 425 | if (fd < 0) { | 
|  | 426 | return base::ErrnoError() << "Opening '" << driver << "' failed"; | 
|  | 427 | } | 
|  | 428 | int vers = 0; | 
|  | 429 | status_t result = ioctl(fd, BINDER_VERSION, &vers); | 
|  | 430 | if (result == -1) { | 
|  | 431 | close(fd); | 
|  | 432 | return base::ErrnoError() << "Binder ioctl to obtain version failed"; | 
|  | 433 | } | 
|  | 434 | if (result != 0 || vers != BINDER_CURRENT_PROTOCOL_VERSION) { | 
|  | 435 | close(fd); | 
|  | 436 | return base::Error() << "Binder driver protocol(" << vers | 
|  | 437 | << ") does not match user space protocol(" | 
|  | 438 | << BINDER_CURRENT_PROTOCOL_VERSION | 
|  | 439 | << ")! ioctl() return value: " << result; | 
|  | 440 | } | 
|  | 441 | size_t maxThreads = DEFAULT_MAX_BINDER_THREADS; | 
|  | 442 | result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads); | 
|  | 443 | if (result == -1) { | 
|  | 444 | ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno)); | 
|  | 445 | } | 
|  | 446 | uint32_t enable = DEFAULT_ENABLE_ONEWAY_SPAM_DETECTION; | 
|  | 447 | result = ioctl(fd, BINDER_ENABLE_ONEWAY_SPAM_DETECTION, &enable); | 
|  | 448 | if (result == -1) { | 
|  | 449 | ALOGV("Binder ioctl to enable oneway spam detection failed: %s", strerror(errno)); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 450 | } | 
|  | 451 | return fd; | 
|  | 452 | } | 
|  | 453 |  | 
| Steven Moreland | 13a43c9 | 2021-08-30 13:21:48 -0700 | [diff] [blame] | 454 | ProcessState::ProcessState(const char* driver) | 
|  | 455 | : mDriverName(String8(driver)), | 
|  | 456 | mDriverFD(-1), | 
|  | 457 | mVMStart(MAP_FAILED), | 
|  | 458 | mThreadCountLock(PTHREAD_MUTEX_INITIALIZER), | 
|  | 459 | mThreadCountDecrement(PTHREAD_COND_INITIALIZER), | 
|  | 460 | mExecutingThreadsCount(0), | 
|  | 461 | mWaitingForThreads(0), | 
|  | 462 | mMaxThreads(DEFAULT_MAX_BINDER_THREADS), | 
|  | 463 | mStarvationStartTimeMs(0), | 
| Steven Moreland | ee9df90 | 2021-10-14 14:00:08 -0700 | [diff] [blame] | 464 | mForked(false), | 
| Steven Moreland | 13a43c9 | 2021-08-30 13:21:48 -0700 | [diff] [blame] | 465 | mThreadPoolStarted(false), | 
|  | 466 | mThreadPoolSeq(1), | 
|  | 467 | mCallRestriction(CallRestriction::NONE) { | 
|  | 468 | base::Result<int> opened = open_driver(driver); | 
|  | 469 |  | 
|  | 470 | if (opened.ok()) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | // mmap the binder, providing a chunk of virtual address space to receive transactions. | 
| Steven Moreland | 13a43c9 | 2021-08-30 13:21:48 -0700 | [diff] [blame] | 472 | mVMStart = mmap(nullptr, BINDER_VM_SIZE, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, | 
|  | 473 | opened.value(), 0); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | if (mVMStart == MAP_FAILED) { | 
| Steven Moreland | 13a43c9 | 2021-08-30 13:21:48 -0700 | [diff] [blame] | 475 | close(opened.value()); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | // *sigh* | 
| Steven Moreland | 13a43c9 | 2021-08-30 13:21:48 -0700 | [diff] [blame] | 477 | opened = base::Error() | 
|  | 478 | << "Using " << driver << " failed: unable to mmap transaction memory."; | 
| Iliyan Malchev | 3206224 | 2017-04-10 14:06:11 -0700 | [diff] [blame] | 479 | mDriverName.clear(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | } | 
| Jeff Brown | e16986c | 2011-07-08 18:52:57 -0700 | [diff] [blame] | 482 |  | 
| Steven Moreland | 24bc0d1 | 2019-10-11 12:29:20 -0700 | [diff] [blame] | 483 | #ifdef __ANDROID__ | 
| Steven Moreland | 13a43c9 | 2021-08-30 13:21:48 -0700 | [diff] [blame] | 484 | LOG_ALWAYS_FATAL_IF(!opened.ok(), "Binder driver '%s' could not be opened. Terminating: %s", | 
|  | 485 | driver, opened.error().message().c_str()); | 
| Steven Moreland | 24bc0d1 | 2019-10-11 12:29:20 -0700 | [diff] [blame] | 486 | #endif | 
| Steven Moreland | 13a43c9 | 2021-08-30 13:21:48 -0700 | [diff] [blame] | 487 |  | 
|  | 488 | if (opened.ok()) { | 
|  | 489 | mDriverFD = opened.value(); | 
|  | 490 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 491 | } | 
|  | 492 |  | 
|  | 493 | ProcessState::~ProcessState() | 
|  | 494 | { | 
| zhongjie | ff40578 | 2016-03-09 15:05:04 +0800 | [diff] [blame] | 495 | if (mDriverFD >= 0) { | 
|  | 496 | if (mVMStart != MAP_FAILED) { | 
| Steven Moreland | 072cc7e | 2019-07-12 21:01:54 +0000 | [diff] [blame] | 497 | munmap(mVMStart, BINDER_VM_SIZE); | 
| zhongjie | ff40578 | 2016-03-09 15:05:04 +0800 | [diff] [blame] | 498 | } | 
|  | 499 | close(mDriverFD); | 
|  | 500 | } | 
|  | 501 | mDriverFD = -1; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 502 | } | 
| Jooyung Han | 1b228f4 | 2020-01-30 13:41:12 +0900 | [diff] [blame] | 503 |  | 
| Steven Moreland | 61ff849 | 2019-09-26 16:05:45 -0700 | [diff] [blame] | 504 | } // namespace android |