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