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