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