| 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 |  | 
| Jason Parks | dcd3958 | 2009-11-03 12:14:38 -0800 | [diff] [blame] | 17 | #define LOG_TAG "IPCThreadState" | 
|  | 18 |  | 
| Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 19 | #include <binder/IPCThreadState.h> | 
| Jayant Chowdhary | dac6dc8 | 2018-10-01 22:52:44 +0000 | [diff] [blame] | 20 | #include <binderthreadstate/IPCThreadStateBase.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 |  | 
| Mathias Agopian | c5b2c0b | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 22 | #include <binder/Binder.h> | 
|  | 23 | #include <binder/BpBinder.h> | 
| Mathias Agopian | 002e1e5 | 2013-05-06 20:20:50 -0700 | [diff] [blame] | 24 | #include <binder/TextOutput.h> | 
|  | 25 |  | 
| Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 26 | #include <android-base/macros.h> | 
| Glenn Kasten | a26e1cf | 2012-03-16 07:15:23 -0700 | [diff] [blame] | 27 | #include <cutils/sched_policy.h> | 
| Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 28 | #include <utils/CallStack.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | #include <utils/Log.h> | 
| Colin Cross | 96e8322 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 30 | #include <utils/SystemClock.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | #include <utils/threads.h> | 
|  | 32 |  | 
| Mathias Agopian | 208059f | 2009-05-18 15:08:03 -0700 | [diff] [blame] | 33 | #include <private/binder/binder_module.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 |  | 
| Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 35 | #include <atomic> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | #include <errno.h> | 
| Colin Cross | 96e8322 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 37 | #include <inttypes.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | #include <pthread.h> | 
|  | 39 | #include <sched.h> | 
| Yabin Cui | 8fb2d25 | 2015-01-26 19:45:47 -0800 | [diff] [blame] | 40 | #include <signal.h> | 
|  | 41 | #include <stdio.h> | 
|  | 42 | #include <sys/ioctl.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | #include <sys/resource.h> | 
| Yabin Cui | 8fb2d25 | 2015-01-26 19:45:47 -0800 | [diff] [blame] | 44 | #include <unistd.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 |  | 
| Steven Moreland | a4853cd | 2019-07-12 15:44:37 -0700 | [diff] [blame] | 46 | #include "Static.h" | 
|  | 47 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | #if LOG_NDEBUG | 
|  | 49 |  | 
|  | 50 | #define IF_LOG_TRANSACTIONS() if (false) | 
|  | 51 | #define IF_LOG_COMMANDS() if (false) | 
|  | 52 | #define LOG_REMOTEREFS(...) | 
|  | 53 | #define IF_LOG_REMOTEREFS() if (false) | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 54 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | #define LOG_THREADPOOL(...) | 
|  | 56 | #define LOG_ONEWAY(...) | 
|  | 57 |  | 
|  | 58 | #else | 
|  | 59 |  | 
| Steve Block | 9f76015 | 2011-10-12 17:27:03 +0100 | [diff] [blame] | 60 | #define IF_LOG_TRANSACTIONS() IF_ALOG(LOG_VERBOSE, "transact") | 
|  | 61 | #define IF_LOG_COMMANDS() IF_ALOG(LOG_VERBOSE, "ipc") | 
|  | 62 | #define LOG_REMOTEREFS(...) ALOG(LOG_DEBUG, "remoterefs", __VA_ARGS__) | 
|  | 63 | #define IF_LOG_REMOTEREFS() IF_ALOG(LOG_DEBUG, "remoterefs") | 
|  | 64 | #define LOG_THREADPOOL(...) ALOG(LOG_DEBUG, "threadpool", __VA_ARGS__) | 
|  | 65 | #define LOG_ONEWAY(...) ALOG(LOG_DEBUG, "ipc", __VA_ARGS__) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 |  | 
|  | 67 | #endif | 
|  | 68 |  | 
|  | 69 | // --------------------------------------------------------------------------- | 
|  | 70 |  | 
|  | 71 | namespace android { | 
|  | 72 |  | 
| Chih-Hung Hsieh | 8e5337d | 2014-10-24 14:10:09 -0700 | [diff] [blame] | 73 | // Static const and functions will be optimized out if not used, | 
|  | 74 | // when LOG_NDEBUG and references in IF_LOG_COMMANDS() are optimized out. | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | static const char *kReturnStrings[] = { | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 76 | "BR_ERROR", | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | "BR_OK", | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | "BR_TRANSACTION", | 
|  | 79 | "BR_REPLY", | 
|  | 80 | "BR_ACQUIRE_RESULT", | 
|  | 81 | "BR_DEAD_REPLY", | 
|  | 82 | "BR_TRANSACTION_COMPLETE", | 
|  | 83 | "BR_INCREFS", | 
|  | 84 | "BR_ACQUIRE", | 
|  | 85 | "BR_RELEASE", | 
|  | 86 | "BR_DECREFS", | 
|  | 87 | "BR_ATTEMPT_ACQUIRE", | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | "BR_NOOP", | 
|  | 89 | "BR_SPAWN_LOOPER", | 
|  | 90 | "BR_FINISHED", | 
|  | 91 | "BR_DEAD_BINDER", | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 92 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", | 
| Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 93 | "BR_FAILED_REPLY", | 
|  | 94 | "BR_TRANSACTION_SEC_CTX", | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | }; | 
|  | 96 |  | 
|  | 97 | static const char *kCommandStrings[] = { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | "BC_TRANSACTION", | 
|  | 99 | "BC_REPLY", | 
|  | 100 | "BC_ACQUIRE_RESULT", | 
|  | 101 | "BC_FREE_BUFFER", | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | "BC_INCREFS", | 
|  | 103 | "BC_ACQUIRE", | 
|  | 104 | "BC_RELEASE", | 
|  | 105 | "BC_DECREFS", | 
|  | 106 | "BC_INCREFS_DONE", | 
|  | 107 | "BC_ACQUIRE_DONE", | 
|  | 108 | "BC_ATTEMPT_ACQUIRE", | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | "BC_REGISTER_LOOPER", | 
|  | 110 | "BC_ENTER_LOOPER", | 
|  | 111 | "BC_EXIT_LOOPER", | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | "BC_REQUEST_DEATH_NOTIFICATION", | 
|  | 113 | "BC_CLEAR_DEATH_NOTIFICATION", | 
|  | 114 | "BC_DEAD_BINDER_DONE" | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | }; | 
|  | 116 |  | 
| songjinshi | 73a7dde | 2016-10-18 21:05:56 +0800 | [diff] [blame] | 117 | static const char* getReturnString(uint32_t cmd) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | { | 
| songjinshi | 8e486c6 | 2019-04-04 11:22:52 +0800 | [diff] [blame] | 119 | size_t idx = cmd & _IOC_NRMASK; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | if (idx < sizeof(kReturnStrings) / sizeof(kReturnStrings[0])) | 
|  | 121 | return kReturnStrings[idx]; | 
|  | 122 | else | 
|  | 123 | return "unknown"; | 
|  | 124 | } | 
|  | 125 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | static const void* printBinderTransactionData(TextOutput& out, const void* data) | 
|  | 127 | { | 
|  | 128 | const binder_transaction_data* btd = | 
|  | 129 | (const binder_transaction_data*)data; | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 130 | if (btd->target.handle < 1024) { | 
|  | 131 | /* want to print descriptors in decimal; guess based on value */ | 
|  | 132 | out << "target.desc=" << btd->target.handle; | 
|  | 133 | } else { | 
|  | 134 | out << "target.ptr=" << btd->target.ptr; | 
|  | 135 | } | 
|  | 136 | out << " (cookie " << btd->cookie << ")" << endl | 
| Chih-Hung Hsieh | 8e5337d | 2014-10-24 14:10:09 -0700 | [diff] [blame] | 137 | << "code=" << TypeCode(btd->code) << ", flags=" << (void*)(long)btd->flags << endl | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | << "data=" << btd->data.ptr.buffer << " (" << (void*)btd->data_size | 
|  | 139 | << " bytes)" << endl | 
|  | 140 | << "offsets=" << btd->data.ptr.offsets << " (" << (void*)btd->offsets_size | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 141 | << " bytes)"; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | return btd+1; | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | static const void* printReturnCommand(TextOutput& out, const void* _cmd) | 
|  | 146 | { | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 147 | static const size_t N = sizeof(kReturnStrings)/sizeof(kReturnStrings[0]); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | const int32_t* cmd = (const int32_t*)_cmd; | 
| Bernhard RosenkrƤnzer | 74debb0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 149 | uint32_t code = (uint32_t)*cmd++; | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 150 | size_t cmdIndex = code & 0xff; | 
| Bernhard RosenkrƤnzer | 74debb0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 151 | if (code == BR_ERROR) { | 
| Chih-Hung Hsieh | 8e5337d | 2014-10-24 14:10:09 -0700 | [diff] [blame] | 152 | out << "BR_ERROR: " << (void*)(long)(*cmd++) << endl; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | return cmd; | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 154 | } else if (cmdIndex >= N) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | out << "Unknown reply: " << code << endl; | 
|  | 156 | return cmd; | 
|  | 157 | } | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 158 | out << kReturnStrings[cmdIndex]; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 159 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | switch (code) { | 
|  | 161 | case BR_TRANSACTION: | 
|  | 162 | case BR_REPLY: { | 
|  | 163 | out << ": " << indent; | 
|  | 164 | cmd = (const int32_t *)printBinderTransactionData(out, cmd); | 
|  | 165 | out << dedent; | 
|  | 166 | } break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 167 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | case BR_ACQUIRE_RESULT: { | 
|  | 169 | const int32_t res = *cmd++; | 
|  | 170 | out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)"); | 
|  | 171 | } break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 172 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | case BR_INCREFS: | 
|  | 174 | case BR_ACQUIRE: | 
|  | 175 | case BR_RELEASE: | 
|  | 176 | case BR_DECREFS: { | 
|  | 177 | const int32_t b = *cmd++; | 
|  | 178 | const int32_t c = *cmd++; | 
| Chih-Hung Hsieh | 8e5337d | 2014-10-24 14:10:09 -0700 | [diff] [blame] | 179 | out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")"; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | } break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 181 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 182 | case BR_ATTEMPT_ACQUIRE: { | 
|  | 183 | const int32_t p = *cmd++; | 
|  | 184 | const int32_t b = *cmd++; | 
|  | 185 | const int32_t c = *cmd++; | 
| Chih-Hung Hsieh | 8e5337d | 2014-10-24 14:10:09 -0700 | [diff] [blame] | 186 | out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | << "), pri=" << p; | 
|  | 188 | } break; | 
|  | 189 |  | 
|  | 190 | case BR_DEAD_BINDER: | 
|  | 191 | case BR_CLEAR_DEATH_NOTIFICATION_DONE: { | 
|  | 192 | const int32_t c = *cmd++; | 
| Chih-Hung Hsieh | 8e5337d | 2014-10-24 14:10:09 -0700 | [diff] [blame] | 193 | out << ": death cookie " << (void*)(long)c; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | } break; | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 195 |  | 
|  | 196 | default: | 
|  | 197 | // no details to show for: BR_OK, BR_DEAD_REPLY, | 
|  | 198 | // BR_TRANSACTION_COMPLETE, BR_FINISHED | 
|  | 199 | break; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 201 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | out << endl; | 
|  | 203 | return cmd; | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | static const void* printCommand(TextOutput& out, const void* _cmd) | 
|  | 207 | { | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 208 | static const size_t N = sizeof(kCommandStrings)/sizeof(kCommandStrings[0]); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | const int32_t* cmd = (const int32_t*)_cmd; | 
| Bernhard RosenkrƤnzer | 74debb0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 210 | uint32_t code = (uint32_t)*cmd++; | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 211 | size_t cmdIndex = code & 0xff; | 
|  | 212 |  | 
|  | 213 | if (cmdIndex >= N) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | out << "Unknown command: " << code << endl; | 
|  | 215 | return cmd; | 
|  | 216 | } | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 217 | out << kCommandStrings[cmdIndex]; | 
|  | 218 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | switch (code) { | 
|  | 220 | case BC_TRANSACTION: | 
|  | 221 | case BC_REPLY: { | 
|  | 222 | out << ": " << indent; | 
|  | 223 | cmd = (const int32_t *)printBinderTransactionData(out, cmd); | 
|  | 224 | out << dedent; | 
|  | 225 | } break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 226 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | case BC_ACQUIRE_RESULT: { | 
|  | 228 | const int32_t res = *cmd++; | 
|  | 229 | out << ": " << res << (res ? " (SUCCESS)" : " (FAILURE)"); | 
|  | 230 | } break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 231 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | case BC_FREE_BUFFER: { | 
|  | 233 | const int32_t buf = *cmd++; | 
| Chih-Hung Hsieh | 8e5337d | 2014-10-24 14:10:09 -0700 | [diff] [blame] | 234 | out << ": buffer=" << (void*)(long)buf; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | } break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 236 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | case BC_INCREFS: | 
|  | 238 | case BC_ACQUIRE: | 
|  | 239 | case BC_RELEASE: | 
|  | 240 | case BC_DECREFS: { | 
|  | 241 | const int32_t d = *cmd++; | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 242 | out << ": desc=" << d; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | } break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 244 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | case BC_INCREFS_DONE: | 
|  | 246 | case BC_ACQUIRE_DONE: { | 
|  | 247 | const int32_t b = *cmd++; | 
|  | 248 | const int32_t c = *cmd++; | 
| Chih-Hung Hsieh | 8e5337d | 2014-10-24 14:10:09 -0700 | [diff] [blame] | 249 | out << ": target=" << (void*)(long)b << " (cookie " << (void*)(long)c << ")"; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | } break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 251 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | case BC_ATTEMPT_ACQUIRE: { | 
|  | 253 | const int32_t p = *cmd++; | 
|  | 254 | const int32_t d = *cmd++; | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 255 | out << ": desc=" << d << ", pri=" << p; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | } break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 257 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | case BC_REQUEST_DEATH_NOTIFICATION: | 
|  | 259 | case BC_CLEAR_DEATH_NOTIFICATION: { | 
|  | 260 | const int32_t h = *cmd++; | 
|  | 261 | const int32_t c = *cmd++; | 
| Chih-Hung Hsieh | 8e5337d | 2014-10-24 14:10:09 -0700 | [diff] [blame] | 262 | out << ": handle=" << h << " (death cookie " << (void*)(long)c << ")"; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | } break; | 
|  | 264 |  | 
|  | 265 | case BC_DEAD_BINDER_DONE: { | 
|  | 266 | const int32_t c = *cmd++; | 
| Chih-Hung Hsieh | 8e5337d | 2014-10-24 14:10:09 -0700 | [diff] [blame] | 267 | out << ": death cookie " << (void*)(long)c; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 268 | } break; | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 269 |  | 
|  | 270 | default: | 
|  | 271 | // no details to show for: BC_REGISTER_LOOPER, BC_ENTER_LOOPER, | 
|  | 272 | // BC_EXIT_LOOPER | 
|  | 273 | break; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 275 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | out << endl; | 
|  | 277 | return cmd; | 
|  | 278 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 |  | 
|  | 280 | static pthread_mutex_t gTLSMutex = PTHREAD_MUTEX_INITIALIZER; | 
| Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 281 | static std::atomic<bool> gHaveTLS(false); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | static pthread_key_t gTLS = 0; | 
| Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 283 | static std::atomic<bool> gShutdown = false; | 
|  | 284 | static std::atomic<bool> gDisableBackgroundScheduling = false; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 |  | 
|  | 286 | IPCThreadState* IPCThreadState::self() | 
|  | 287 | { | 
| Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 288 | if (gHaveTLS.load(std::memory_order_acquire)) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | restart: | 
|  | 290 | const pthread_key_t k = gTLS; | 
|  | 291 | IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k); | 
|  | 292 | if (st) return st; | 
|  | 293 | return new IPCThreadState; | 
|  | 294 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 295 |  | 
| Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 296 | // Racey, heuristic test for simultaneous shutdown. | 
|  | 297 | if (gShutdown.load(std::memory_order_relaxed)) { | 
| Andreas Gampe | f31a3eb | 2016-02-01 13:21:56 -0800 | [diff] [blame] | 298 | ALOGW("Calling IPCThreadState::self() during shutdown is dangerous, expect a crash.\n"); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 299 | return nullptr; | 
| Andreas Gampe | f31a3eb | 2016-02-01 13:21:56 -0800 | [diff] [blame] | 300 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 301 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | pthread_mutex_lock(&gTLSMutex); | 
| Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 303 | if (!gHaveTLS.load(std::memory_order_relaxed)) { | 
| Andreas Gampe | f31a3eb | 2016-02-01 13:21:56 -0800 | [diff] [blame] | 304 | int key_create_value = pthread_key_create(&gTLS, threadDestructor); | 
|  | 305 | if (key_create_value != 0) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | pthread_mutex_unlock(&gTLSMutex); | 
| Andreas Gampe | f31a3eb | 2016-02-01 13:21:56 -0800 | [diff] [blame] | 307 | ALOGW("IPCThreadState::self() unable to create TLS key, expect a crash: %s\n", | 
|  | 308 | strerror(key_create_value)); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 309 | return nullptr; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | } | 
| Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 311 | gHaveTLS.store(true, std::memory_order_release); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | } | 
|  | 313 | pthread_mutex_unlock(&gTLSMutex); | 
|  | 314 | goto restart; | 
|  | 315 | } | 
|  | 316 |  | 
| Brad Fitzpatrick | 1b60843 | 2010-12-13 16:52:35 -0800 | [diff] [blame] | 317 | IPCThreadState* IPCThreadState::selfOrNull() | 
|  | 318 | { | 
| Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 319 | if (gHaveTLS.load(std::memory_order_acquire)) { | 
| Brad Fitzpatrick | 1b60843 | 2010-12-13 16:52:35 -0800 | [diff] [blame] | 320 | const pthread_key_t k = gTLS; | 
|  | 321 | IPCThreadState* st = (IPCThreadState*)pthread_getspecific(k); | 
|  | 322 | return st; | 
|  | 323 | } | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 324 | return nullptr; | 
| Brad Fitzpatrick | 1b60843 | 2010-12-13 16:52:35 -0800 | [diff] [blame] | 325 | } | 
|  | 326 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | void IPCThreadState::shutdown() | 
|  | 328 | { | 
| Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 329 | gShutdown.store(true, std::memory_order_relaxed); | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 330 |  | 
| Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 331 | if (gHaveTLS.load(std::memory_order_acquire)) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | // XXX Need to wait for all thread pool threads to exit! | 
|  | 333 | IPCThreadState* st = (IPCThreadState*)pthread_getspecific(gTLS); | 
|  | 334 | if (st) { | 
|  | 335 | delete st; | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 336 | pthread_setspecific(gTLS, nullptr); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | } | 
| zhongjie | ff40578 | 2016-03-09 15:05:04 +0800 | [diff] [blame] | 338 | pthread_key_delete(gTLS); | 
| Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 339 | gHaveTLS.store(false, std::memory_order_release); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | } | 
|  | 341 | } | 
|  | 342 |  | 
| Dianne Hackborn | 8c6cedc | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 343 | void IPCThreadState::disableBackgroundScheduling(bool disable) | 
|  | 344 | { | 
| Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 345 | gDisableBackgroundScheduling.store(disable, std::memory_order_relaxed); | 
| Dianne Hackborn | 8c6cedc | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 346 | } | 
|  | 347 |  | 
| Martijn Coenen | 2b63174 | 2017-05-05 11:16:59 -0700 | [diff] [blame] | 348 | bool IPCThreadState::backgroundSchedulingDisabled() | 
|  | 349 | { | 
| Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 350 | return gDisableBackgroundScheduling.load(std::memory_order_relaxed); | 
| Martijn Coenen | 2b63174 | 2017-05-05 11:16:59 -0700 | [diff] [blame] | 351 | } | 
|  | 352 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | sp<ProcessState> IPCThreadState::process() | 
|  | 354 | { | 
|  | 355 | return mProcess; | 
|  | 356 | } | 
|  | 357 |  | 
|  | 358 | status_t IPCThreadState::clearLastError() | 
|  | 359 | { | 
|  | 360 | const status_t err = mLastError; | 
|  | 361 | mLastError = NO_ERROR; | 
|  | 362 | return err; | 
|  | 363 | } | 
|  | 364 |  | 
| Dan Stoza | 9c634fd | 2014-11-26 12:23:23 -0800 | [diff] [blame] | 365 | pid_t IPCThreadState::getCallingPid() const | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | { | 
|  | 367 | return mCallingPid; | 
|  | 368 | } | 
|  | 369 |  | 
| Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 370 | const char* IPCThreadState::getCallingSid() const | 
|  | 371 | { | 
|  | 372 | return mCallingSid; | 
|  | 373 | } | 
|  | 374 |  | 
| Dan Stoza | 9c634fd | 2014-11-26 12:23:23 -0800 | [diff] [blame] | 375 | uid_t IPCThreadState::getCallingUid() const | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | { | 
|  | 377 | return mCallingUid; | 
|  | 378 | } | 
|  | 379 |  | 
|  | 380 | int64_t IPCThreadState::clearCallingIdentity() | 
|  | 381 | { | 
| Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 382 | // ignore mCallingSid for legacy reasons | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | int64_t token = ((int64_t)mCallingUid<<32) | mCallingPid; | 
|  | 384 | clearCaller(); | 
|  | 385 | return token; | 
|  | 386 | } | 
|  | 387 |  | 
| Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 388 | void IPCThreadState::setStrictModePolicy(int32_t policy) | 
|  | 389 | { | 
|  | 390 | mStrictModePolicy = policy; | 
|  | 391 | } | 
|  | 392 |  | 
| Brad Fitzpatrick | a877cd8 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 393 | int32_t IPCThreadState::getStrictModePolicy() const | 
|  | 394 | { | 
| Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 395 | return mStrictModePolicy; | 
|  | 396 | } | 
|  | 397 |  | 
| Brad Fitzpatrick | 5273603 | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 398 | void IPCThreadState::setLastTransactionBinderFlags(int32_t flags) | 
|  | 399 | { | 
|  | 400 | mLastTransactionBinderFlags = flags; | 
|  | 401 | } | 
|  | 402 |  | 
|  | 403 | int32_t IPCThreadState::getLastTransactionBinderFlags() const | 
|  | 404 | { | 
|  | 405 | return mLastTransactionBinderFlags; | 
|  | 406 | } | 
|  | 407 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | void IPCThreadState::restoreCallingIdentity(int64_t token) | 
|  | 409 | { | 
|  | 410 | mCallingUid = (int)(token>>32); | 
| Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 411 | mCallingSid = nullptr;  // not enough data to restore | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | mCallingPid = (int)token; | 
|  | 413 | } | 
|  | 414 |  | 
|  | 415 | void IPCThreadState::clearCaller() | 
|  | 416 | { | 
| Marco Nelissen | d43b194 | 2009-07-17 07:59:17 -0700 | [diff] [blame] | 417 | mCallingPid = getpid(); | 
| Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 418 | mCallingSid = nullptr;  // expensive to lookup | 
| Marco Nelissen | d43b194 | 2009-07-17 07:59:17 -0700 | [diff] [blame] | 419 | mCallingUid = getuid(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 420 | } | 
|  | 421 |  | 
|  | 422 | void IPCThreadState::flushCommands() | 
|  | 423 | { | 
| Alexandre Baião | c60c4fc | 2019-07-31 12:29:31 -0200 | [diff] [blame] | 424 | if (mProcess->mDriverFD < 0) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | return; | 
|  | 426 | talkWithDriver(false); | 
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 427 | // The flush could have caused post-write refcount decrements to have | 
|  | 428 | // been executed, which in turn could result in BC_RELEASE/BC_DECREFS | 
|  | 429 | // being queued in mOut. So flush again, if we need to. | 
|  | 430 | if (mOut.dataSize() > 0) { | 
|  | 431 | talkWithDriver(false); | 
|  | 432 | } | 
|  | 433 | if (mOut.dataSize() > 0) { | 
|  | 434 | ALOGW("mOut.dataSize() > 0 after flushCommands()"); | 
|  | 435 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | } | 
|  | 437 |  | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 438 | void IPCThreadState::blockUntilThreadAvailable() | 
|  | 439 | { | 
|  | 440 | pthread_mutex_lock(&mProcess->mThreadCountLock); | 
|  | 441 | while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) { | 
| Wale Ogunwale | a3206e6 | 2015-04-21 12:29:50 -0700 | [diff] [blame] | 442 | ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n", | 
|  | 443 | static_cast<unsigned long>(mProcess->mExecutingThreadsCount), | 
|  | 444 | static_cast<unsigned long>(mProcess->mMaxThreads)); | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 445 | pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock); | 
|  | 446 | } | 
|  | 447 | pthread_mutex_unlock(&mProcess->mThreadCountLock); | 
|  | 448 | } | 
|  | 449 |  | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 450 | status_t IPCThreadState::getAndExecuteCommand() | 
|  | 451 | { | 
|  | 452 | status_t result; | 
|  | 453 | int32_t cmd; | 
|  | 454 |  | 
|  | 455 | result = talkWithDriver(); | 
|  | 456 | if (result >= NO_ERROR) { | 
|  | 457 | size_t IN = mIn.dataAvail(); | 
|  | 458 | if (IN < sizeof(int32_t)) return result; | 
|  | 459 | cmd = mIn.readInt32(); | 
|  | 460 | IF_LOG_COMMANDS() { | 
|  | 461 | alog << "Processing top-level Command: " | 
|  | 462 | << getReturnString(cmd) << endl; | 
|  | 463 | } | 
|  | 464 |  | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 465 | pthread_mutex_lock(&mProcess->mThreadCountLock); | 
|  | 466 | mProcess->mExecutingThreadsCount++; | 
| Colin Cross | 96e8322 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 467 | if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads && | 
|  | 468 | mProcess->mStarvationStartTimeMs == 0) { | 
|  | 469 | mProcess->mStarvationStartTimeMs = uptimeMillis(); | 
|  | 470 | } | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 471 | pthread_mutex_unlock(&mProcess->mThreadCountLock); | 
|  | 472 |  | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 473 | result = executeCommand(cmd); | 
|  | 474 |  | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 475 | pthread_mutex_lock(&mProcess->mThreadCountLock); | 
|  | 476 | mProcess->mExecutingThreadsCount--; | 
| Colin Cross | 96e8322 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 477 | if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads && | 
|  | 478 | mProcess->mStarvationStartTimeMs != 0) { | 
|  | 479 | int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs; | 
|  | 480 | if (starvationTimeMs > 100) { | 
|  | 481 | ALOGE("binder thread pool (%zu threads) starved for %" PRId64 " ms", | 
|  | 482 | mProcess->mMaxThreads, starvationTimeMs); | 
|  | 483 | } | 
|  | 484 | mProcess->mStarvationStartTimeMs = 0; | 
|  | 485 | } | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 486 | pthread_cond_broadcast(&mProcess->mThreadCountDecrement); | 
|  | 487 | pthread_mutex_unlock(&mProcess->mThreadCountLock); | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 488 | } | 
|  | 489 |  | 
|  | 490 | return result; | 
|  | 491 | } | 
|  | 492 |  | 
|  | 493 | // When we've cleared the incoming command queue, process any pending derefs | 
|  | 494 | void IPCThreadState::processPendingDerefs() | 
|  | 495 | { | 
|  | 496 | if (mIn.dataPosition() >= mIn.dataSize()) { | 
| Martijn Coenen | 0791fbf | 2017-08-08 15:36:16 +0200 | [diff] [blame] | 497 | /* | 
|  | 498 | * The decWeak()/decStrong() calls may cause a destructor to run, | 
|  | 499 | * which in turn could have initiated an outgoing transaction, | 
|  | 500 | * which in turn could cause us to add to the pending refs | 
|  | 501 | * vectors; so instead of simply iterating, loop until they're empty. | 
|  | 502 | * | 
|  | 503 | * We do this in an outer loop, because calling decStrong() | 
|  | 504 | * may result in something being added to mPendingWeakDerefs, | 
|  | 505 | * which could be delayed until the next incoming command | 
|  | 506 | * from the driver if we don't process it now. | 
|  | 507 | */ | 
|  | 508 | while (mPendingWeakDerefs.size() > 0 || mPendingStrongDerefs.size() > 0) { | 
|  | 509 | while (mPendingWeakDerefs.size() > 0) { | 
|  | 510 | RefBase::weakref_type* refs = mPendingWeakDerefs[0]; | 
|  | 511 | mPendingWeakDerefs.removeAt(0); | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 512 | refs->decWeak(mProcess.get()); | 
|  | 513 | } | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 514 |  | 
| Martijn Coenen | 0791fbf | 2017-08-08 15:36:16 +0200 | [diff] [blame] | 515 | if (mPendingStrongDerefs.size() > 0) { | 
|  | 516 | // We don't use while() here because we don't want to re-order | 
|  | 517 | // strong and weak decs at all; if this decStrong() causes both a | 
|  | 518 | // decWeak() and a decStrong() to be queued, we want to process | 
|  | 519 | // the decWeak() first. | 
|  | 520 | BBinder* obj = mPendingStrongDerefs[0]; | 
|  | 521 | mPendingStrongDerefs.removeAt(0); | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 522 | obj->decStrong(mProcess.get()); | 
|  | 523 | } | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 524 | } | 
|  | 525 | } | 
|  | 526 | } | 
|  | 527 |  | 
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 528 | void IPCThreadState::processPostWriteDerefs() | 
|  | 529 | { | 
|  | 530 | for (size_t i = 0; i < mPostWriteWeakDerefs.size(); i++) { | 
|  | 531 | RefBase::weakref_type* refs = mPostWriteWeakDerefs[i]; | 
|  | 532 | refs->decWeak(mProcess.get()); | 
|  | 533 | } | 
|  | 534 | mPostWriteWeakDerefs.clear(); | 
|  | 535 |  | 
|  | 536 | for (size_t i = 0; i < mPostWriteStrongDerefs.size(); i++) { | 
|  | 537 | RefBase* obj = mPostWriteStrongDerefs[i]; | 
|  | 538 | obj->decStrong(mProcess.get()); | 
|  | 539 | } | 
|  | 540 | mPostWriteStrongDerefs.clear(); | 
|  | 541 | } | 
|  | 542 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 543 | void IPCThreadState::joinThreadPool(bool isMain) | 
|  | 544 | { | 
|  | 545 | LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid()); | 
|  | 546 |  | 
|  | 547 | mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER); | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 548 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 549 | status_t result; | 
|  | 550 | do { | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 551 | processPendingDerefs(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 552 | // now get the next command to be processed, waiting if necessary | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 553 | result = getAndExecuteCommand(); | 
| Jason Parks | dcd3958 | 2009-11-03 12:14:38 -0800 | [diff] [blame] | 554 |  | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 555 | if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) { | 
|  | 556 | ALOGE("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting", | 
| Jeff Tinker | ef07386 | 2013-06-11 11:30:21 -0700 | [diff] [blame] | 557 | mProcess->mDriverFD, result); | 
|  | 558 | abort(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 559 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 560 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | // Let this thread exit the thread pool if it is no longer | 
|  | 562 | // needed and it is not the main process thread. | 
|  | 563 | if(result == TIMED_OUT && !isMain) { | 
|  | 564 | break; | 
|  | 565 | } | 
|  | 566 | } while (result != -ECONNREFUSED && result != -EBADF); | 
|  | 567 |  | 
| Wei Wang | c734143 | 2016-10-19 10:23:59 -0700 | [diff] [blame] | 568 | LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%d\n", | 
|  | 569 | (void*)pthread_self(), getpid(), result); | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 570 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | mOut.writeInt32(BC_EXIT_LOOPER); | 
|  | 572 | talkWithDriver(false); | 
|  | 573 | } | 
|  | 574 |  | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 575 | int IPCThreadState::setupPolling(int* fd) | 
|  | 576 | { | 
| Alexandre Baião | c60c4fc | 2019-07-31 12:29:31 -0200 | [diff] [blame] | 577 | if (mProcess->mDriverFD < 0) { | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 578 | return -EBADF; | 
|  | 579 | } | 
|  | 580 |  | 
|  | 581 | mOut.writeInt32(BC_ENTER_LOOPER); | 
|  | 582 | *fd = mProcess->mDriverFD; | 
|  | 583 | return 0; | 
|  | 584 | } | 
|  | 585 |  | 
|  | 586 | status_t IPCThreadState::handlePolledCommands() | 
|  | 587 | { | 
|  | 588 | status_t result; | 
|  | 589 |  | 
|  | 590 | do { | 
|  | 591 | result = getAndExecuteCommand(); | 
|  | 592 | } while (mIn.dataPosition() < mIn.dataSize()); | 
|  | 593 |  | 
|  | 594 | processPendingDerefs(); | 
|  | 595 | flushCommands(); | 
|  | 596 | return result; | 
|  | 597 | } | 
|  | 598 |  | 
| Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 599 | void IPCThreadState::stopProcess(bool /*immediate*/) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 600 | { | 
| Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 601 | //ALOGI("**** STOPPING PROCESS"); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 602 | flushCommands(); | 
|  | 603 | int fd = mProcess->mDriverFD; | 
|  | 604 | mProcess->mDriverFD = -1; | 
|  | 605 | close(fd); | 
|  | 606 | //kill(getpid(), SIGKILL); | 
|  | 607 | } | 
|  | 608 |  | 
|  | 609 | status_t IPCThreadState::transact(int32_t handle, | 
|  | 610 | uint32_t code, const Parcel& data, | 
|  | 611 | Parcel* reply, uint32_t flags) | 
|  | 612 | { | 
| Ganesh Mahendran | 58e5daa | 2017-10-11 18:05:13 +0800 | [diff] [blame] | 613 | status_t err; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 |  | 
|  | 615 | flags |= TF_ACCEPT_FDS; | 
|  | 616 |  | 
|  | 617 | IF_LOG_TRANSACTIONS() { | 
|  | 618 | TextOutput::Bundle _b(alog); | 
|  | 619 | alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand " | 
|  | 620 | << handle << " / code " << TypeCode(code) << ": " | 
|  | 621 | << indent << data << dedent << endl; | 
|  | 622 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 623 |  | 
| Ganesh Mahendran | 58e5daa | 2017-10-11 18:05:13 +0800 | [diff] [blame] | 624 | LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(), | 
|  | 625 | (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY"); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 626 | err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, nullptr); | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 627 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 628 | if (err != NO_ERROR) { | 
|  | 629 | if (reply) reply->setError(err); | 
|  | 630 | return (mLastError = err); | 
|  | 631 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 632 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 633 | if ((flags & TF_ONE_WAY) == 0) { | 
| Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 634 | if (UNLIKELY(mCallRestriction != ProcessState::CallRestriction::NONE)) { | 
|  | 635 | if (mCallRestriction == ProcessState::CallRestriction::ERROR_IF_NOT_ONEWAY) { | 
| Steven Moreland | 8cb34fc | 2019-05-13 11:44:55 -0700 | [diff] [blame] | 636 | ALOGE("Process making non-oneway call (code: %u) but is restricted.", code); | 
| Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 637 | CallStack::logStack("non-oneway call", CallStack::getCurrent(10).get(), | 
|  | 638 | ANDROID_LOG_ERROR); | 
|  | 639 | } else /* FATAL_IF_NOT_ONEWAY */ { | 
| Steven Moreland | 8cb34fc | 2019-05-13 11:44:55 -0700 | [diff] [blame] | 640 | LOG_ALWAYS_FATAL("Process may not make oneway calls (code: %u).", code); | 
| Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 641 | } | 
|  | 642 | } | 
|  | 643 |  | 
| Dianne Hackborn | 67f78c4 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 644 | #if 0 | 
|  | 645 | if (code == 4) { // relayout | 
| Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 646 | ALOGI(">>>>>> CALLING transaction 4"); | 
| Dianne Hackborn | 67f78c4 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 647 | } else { | 
| Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 648 | ALOGI(">>>>>> CALLING transaction %d", code); | 
| Dianne Hackborn | 67f78c4 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 649 | } | 
|  | 650 | #endif | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 651 | if (reply) { | 
|  | 652 | err = waitForResponse(reply); | 
|  | 653 | } else { | 
|  | 654 | Parcel fakeReply; | 
|  | 655 | err = waitForResponse(&fakeReply); | 
|  | 656 | } | 
| Dianne Hackborn | 67f78c4 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 657 | #if 0 | 
|  | 658 | if (code == 4) { // relayout | 
| Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 659 | ALOGI("<<<<<< RETURNING transaction 4"); | 
| Dianne Hackborn | 67f78c4 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 660 | } else { | 
| Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 661 | ALOGI("<<<<<< RETURNING transaction %d", code); | 
| Dianne Hackborn | 67f78c4 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 662 | } | 
|  | 663 | #endif | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 664 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 665 | IF_LOG_TRANSACTIONS() { | 
|  | 666 | TextOutput::Bundle _b(alog); | 
|  | 667 | alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand " | 
|  | 668 | << handle << ": "; | 
|  | 669 | if (reply) alog << indent << *reply << dedent << endl; | 
|  | 670 | else alog << "(none requested)" << endl; | 
|  | 671 | } | 
|  | 672 | } else { | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 673 | err = waitForResponse(nullptr, nullptr); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 674 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 675 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 676 | return err; | 
|  | 677 | } | 
|  | 678 |  | 
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 679 | void IPCThreadState::incStrongHandle(int32_t handle, BpBinder *proxy) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 680 | { | 
|  | 681 | LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle); | 
|  | 682 | mOut.writeInt32(BC_ACQUIRE); | 
|  | 683 | mOut.writeInt32(handle); | 
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 684 | // Create a temp reference until the driver has handled this command. | 
|  | 685 | proxy->incStrong(mProcess.get()); | 
|  | 686 | mPostWriteStrongDerefs.push(proxy); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 687 | } | 
|  | 688 |  | 
|  | 689 | void IPCThreadState::decStrongHandle(int32_t handle) | 
|  | 690 | { | 
|  | 691 | LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle); | 
|  | 692 | mOut.writeInt32(BC_RELEASE); | 
|  | 693 | mOut.writeInt32(handle); | 
|  | 694 | } | 
|  | 695 |  | 
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 696 | void IPCThreadState::incWeakHandle(int32_t handle, BpBinder *proxy) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 697 | { | 
|  | 698 | LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle); | 
|  | 699 | mOut.writeInt32(BC_INCREFS); | 
|  | 700 | mOut.writeInt32(handle); | 
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 701 | // Create a temp reference until the driver has handled this command. | 
|  | 702 | proxy->getWeakRefs()->incWeak(mProcess.get()); | 
|  | 703 | mPostWriteWeakDerefs.push(proxy->getWeakRefs()); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 704 | } | 
|  | 705 |  | 
|  | 706 | void IPCThreadState::decWeakHandle(int32_t handle) | 
|  | 707 | { | 
|  | 708 | LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle); | 
|  | 709 | mOut.writeInt32(BC_DECREFS); | 
|  | 710 | mOut.writeInt32(handle); | 
|  | 711 | } | 
|  | 712 |  | 
|  | 713 | status_t IPCThreadState::attemptIncStrongHandle(int32_t handle) | 
|  | 714 | { | 
| Arve HjønnevÄg | 11cfdcc | 2014-02-14 20:14:02 -0800 | [diff] [blame] | 715 | #if HAS_BC_ATTEMPT_ACQUIRE | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 716 | LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 717 | mOut.writeInt32(BC_ATTEMPT_ACQUIRE); | 
|  | 718 | mOut.writeInt32(0); // xxx was thread priority | 
|  | 719 | mOut.writeInt32(handle); | 
|  | 720 | status_t result = UNKNOWN_ERROR; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 721 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 722 | waitForResponse(NULL, &result); | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 723 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 724 | #if LOG_REFCOUNTS | 
| liangweikang | a43ee15 | 2016-10-25 16:37:54 +0800 | [diff] [blame] | 725 | ALOGV("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n", | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 726 | handle, result == NO_ERROR ? "SUCCESS" : "FAILURE"); | 
|  | 727 | #endif | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 728 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 729 | return result; | 
| Arve HjønnevÄg | 11cfdcc | 2014-02-14 20:14:02 -0800 | [diff] [blame] | 730 | #else | 
|  | 731 | (void)handle; | 
|  | 732 | ALOGE("%s(%d): Not supported\n", __func__, handle); | 
|  | 733 | return INVALID_OPERATION; | 
|  | 734 | #endif | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 735 | } | 
|  | 736 |  | 
|  | 737 | void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder) | 
|  | 738 | { | 
|  | 739 | #if LOG_REFCOUNTS | 
| liangweikang | a43ee15 | 2016-10-25 16:37:54 +0800 | [diff] [blame] | 740 | ALOGV("IPCThreadState::expungeHandle(%ld)\n", handle); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | #endif | 
| Manoj Gupta | 9cec85b | 2017-09-19 16:34:29 -0700 | [diff] [blame] | 742 | self()->mProcess->expungeHandle(handle, binder); // NOLINT | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 743 | } | 
|  | 744 |  | 
|  | 745 | status_t IPCThreadState::requestDeathNotification(int32_t handle, BpBinder* proxy) | 
|  | 746 | { | 
|  | 747 | mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION); | 
|  | 748 | mOut.writeInt32((int32_t)handle); | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 749 | mOut.writePointer((uintptr_t)proxy); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 750 | return NO_ERROR; | 
|  | 751 | } | 
|  | 752 |  | 
|  | 753 | status_t IPCThreadState::clearDeathNotification(int32_t handle, BpBinder* proxy) | 
|  | 754 | { | 
|  | 755 | mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION); | 
|  | 756 | mOut.writeInt32((int32_t)handle); | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 757 | mOut.writePointer((uintptr_t)proxy); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 758 | return NO_ERROR; | 
|  | 759 | } | 
|  | 760 |  | 
|  | 761 | IPCThreadState::IPCThreadState() | 
| Brad Fitzpatrick | 5273603 | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 762 | : mProcess(ProcessState::self()), | 
| Brad Fitzpatrick | 5273603 | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 763 | mStrictModePolicy(0), | 
| Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 764 | mLastTransactionBinderFlags(0), | 
|  | 765 | mCallRestriction(mProcess->mCallRestriction) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 766 | { | 
|  | 767 | pthread_setspecific(gTLS, this); | 
| Dianne Hackborn | 8c6cedc | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 768 | clearCaller(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 769 | mIn.setDataCapacity(256); | 
|  | 770 | mOut.setDataCapacity(256); | 
| Jayant Chowdhary | dac6dc8 | 2018-10-01 22:52:44 +0000 | [diff] [blame] | 771 | mIPCThreadStateBase = IPCThreadStateBase::self(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 772 | } | 
|  | 773 |  | 
|  | 774 | IPCThreadState::~IPCThreadState() | 
|  | 775 | { | 
|  | 776 | } | 
|  | 777 |  | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 778 | status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags) | 
|  | 779 | { | 
|  | 780 | status_t err; | 
|  | 781 | status_t statusBuffer; | 
|  | 782 | err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer); | 
|  | 783 | if (err < NO_ERROR) return err; | 
|  | 784 |  | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 785 | return waitForResponse(nullptr, nullptr); | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 786 | } | 
|  | 787 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 788 | status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult) | 
|  | 789 | { | 
| Bernhard RosenkrƤnzer | 74debb0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 790 | uint32_t cmd; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 791 | int32_t err; | 
|  | 792 |  | 
|  | 793 | while (1) { | 
|  | 794 | if ((err=talkWithDriver()) < NO_ERROR) break; | 
|  | 795 | err = mIn.errorCheck(); | 
|  | 796 | if (err < NO_ERROR) break; | 
|  | 797 | if (mIn.dataAvail() == 0) continue; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 798 |  | 
| Bernhard RosenkrƤnzer | 74debb0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 799 | cmd = (uint32_t)mIn.readInt32(); | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 800 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 801 | IF_LOG_COMMANDS() { | 
|  | 802 | alog << "Processing waitForResponse Command: " | 
|  | 803 | << getReturnString(cmd) << endl; | 
|  | 804 | } | 
|  | 805 |  | 
|  | 806 | switch (cmd) { | 
|  | 807 | case BR_TRANSACTION_COMPLETE: | 
|  | 808 | if (!reply && !acquireResult) goto finish; | 
|  | 809 | break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 810 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 811 | case BR_DEAD_REPLY: | 
|  | 812 | err = DEAD_OBJECT; | 
|  | 813 | goto finish; | 
|  | 814 |  | 
|  | 815 | case BR_FAILED_REPLY: | 
|  | 816 | err = FAILED_TRANSACTION; | 
|  | 817 | goto finish; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 818 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 819 | case BR_ACQUIRE_RESULT: | 
|  | 820 | { | 
| Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 821 | ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT"); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 822 | const int32_t result = mIn.readInt32(); | 
|  | 823 | if (!acquireResult) continue; | 
|  | 824 | *acquireResult = result ? NO_ERROR : INVALID_OPERATION; | 
|  | 825 | } | 
|  | 826 | goto finish; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 827 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 828 | case BR_REPLY: | 
|  | 829 | { | 
|  | 830 | binder_transaction_data tr; | 
|  | 831 | err = mIn.read(&tr, sizeof(tr)); | 
| Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 832 | ALOG_ASSERT(err == NO_ERROR, "Not enough command data for brREPLY"); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 833 | if (err != NO_ERROR) goto finish; | 
|  | 834 |  | 
|  | 835 | if (reply) { | 
|  | 836 | if ((tr.flags & TF_STATUS_CODE) == 0) { | 
|  | 837 | reply->ipcSetDataReference( | 
|  | 838 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), | 
|  | 839 | tr.data_size, | 
| Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 840 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), | 
|  | 841 | tr.offsets_size/sizeof(binder_size_t), | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 842 | freeBuffer, this); | 
|  | 843 | } else { | 
| Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 844 | err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 845 | freeBuffer(nullptr, | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 846 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), | 
|  | 847 | tr.data_size, | 
| Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 848 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), | 
|  | 849 | tr.offsets_size/sizeof(binder_size_t), this); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 850 | } | 
|  | 851 | } else { | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 852 | freeBuffer(nullptr, | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 853 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), | 
|  | 854 | tr.data_size, | 
| Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 855 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), | 
|  | 856 | tr.offsets_size/sizeof(binder_size_t), this); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 857 | continue; | 
|  | 858 | } | 
|  | 859 | } | 
|  | 860 | goto finish; | 
|  | 861 |  | 
|  | 862 | default: | 
|  | 863 | err = executeCommand(cmd); | 
|  | 864 | if (err != NO_ERROR) goto finish; | 
|  | 865 | break; | 
|  | 866 | } | 
|  | 867 | } | 
|  | 868 |  | 
|  | 869 | finish: | 
|  | 870 | if (err != NO_ERROR) { | 
|  | 871 | if (acquireResult) *acquireResult = err; | 
|  | 872 | if (reply) reply->setError(err); | 
|  | 873 | mLastError = err; | 
|  | 874 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 875 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 876 | return err; | 
|  | 877 | } | 
|  | 878 |  | 
|  | 879 | status_t IPCThreadState::talkWithDriver(bool doReceive) | 
|  | 880 | { | 
| Alexandre Baião | c60c4fc | 2019-07-31 12:29:31 -0200 | [diff] [blame] | 881 | if (mProcess->mDriverFD < 0) { | 
| Johannes Carlsson | db1597a | 2011-02-17 14:06:53 +0100 | [diff] [blame] | 882 | return -EBADF; | 
|  | 883 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 884 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 885 | binder_write_read bwr; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 886 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 887 | // Is the read buffer empty? | 
|  | 888 | const bool needRead = mIn.dataPosition() >= mIn.dataSize(); | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 889 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 890 | // We don't want to write anything if we are still reading | 
|  | 891 | // from data left in the input buffer and the caller | 
|  | 892 | // has requested to read the next data. | 
|  | 893 | const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 894 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 895 | bwr.write_size = outAvail; | 
| Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 896 | bwr.write_buffer = (uintptr_t)mOut.data(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 897 |  | 
|  | 898 | // This is what we'll read. | 
|  | 899 | if (doReceive && needRead) { | 
|  | 900 | bwr.read_size = mIn.dataCapacity(); | 
| Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 901 | bwr.read_buffer = (uintptr_t)mIn.data(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 902 | } else { | 
|  | 903 | bwr.read_size = 0; | 
| Ben Cheng | d640f89 | 2011-12-01 17:11:32 -0800 | [diff] [blame] | 904 | bwr.read_buffer = 0; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 905 | } | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 906 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 907 | IF_LOG_COMMANDS() { | 
|  | 908 | TextOutput::Bundle _b(alog); | 
|  | 909 | if (outAvail != 0) { | 
|  | 910 | alog << "Sending commands to driver: " << indent; | 
|  | 911 | const void* cmds = (const void*)bwr.write_buffer; | 
|  | 912 | const void* end = ((const uint8_t*)cmds)+bwr.write_size; | 
|  | 913 | alog << HexDump(cmds, bwr.write_size) << endl; | 
|  | 914 | while (cmds < end) cmds = printCommand(alog, cmds); | 
|  | 915 | alog << dedent; | 
|  | 916 | } | 
|  | 917 | alog << "Size of receive buffer: " << bwr.read_size | 
|  | 918 | << ", needRead: " << needRead << ", doReceive: " << doReceive << endl; | 
|  | 919 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 920 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 | // Return immediately if there is nothing to do. | 
|  | 922 | if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR; | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 923 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 924 | bwr.write_consumed = 0; | 
|  | 925 | bwr.read_consumed = 0; | 
|  | 926 | status_t err; | 
|  | 927 | do { | 
|  | 928 | IF_LOG_COMMANDS() { | 
|  | 929 | alog << "About to read/write, write size = " << mOut.dataSize() << endl; | 
|  | 930 | } | 
| Elliott Hughes | 6071da7 | 2015-08-12 15:27:47 -0700 | [diff] [blame] | 931 | #if defined(__ANDROID__) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 932 | if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0) | 
|  | 933 | err = NO_ERROR; | 
|  | 934 | else | 
|  | 935 | err = -errno; | 
|  | 936 | #else | 
|  | 937 | err = INVALID_OPERATION; | 
|  | 938 | #endif | 
| Alexandre Baião | c60c4fc | 2019-07-31 12:29:31 -0200 | [diff] [blame] | 939 | if (mProcess->mDriverFD < 0) { | 
| Johannes Carlsson | db1597a | 2011-02-17 14:06:53 +0100 | [diff] [blame] | 940 | err = -EBADF; | 
|  | 941 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 942 | IF_LOG_COMMANDS() { | 
|  | 943 | alog << "Finished read/write, write size = " << mOut.dataSize() << endl; | 
|  | 944 | } | 
|  | 945 | } while (err == -EINTR); | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 946 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 947 | IF_LOG_COMMANDS() { | 
| Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 948 | alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: " | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 949 | << bwr.write_consumed << " (of " << mOut.dataSize() | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 950 | << "), read consumed: " << bwr.read_consumed << endl; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 951 | } | 
|  | 952 |  | 
|  | 953 | if (err >= NO_ERROR) { | 
|  | 954 | if (bwr.write_consumed > 0) { | 
| Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 955 | if (bwr.write_consumed < mOut.dataSize()) | 
| Steven Moreland | 80afc6f | 2019-07-15 13:28:44 -0700 | [diff] [blame] | 956 | LOG_ALWAYS_FATAL("Driver did not consume write buffer"); | 
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 957 | else { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 958 | mOut.setDataSize(0); | 
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 959 | processPostWriteDerefs(); | 
|  | 960 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 961 | } | 
|  | 962 | if (bwr.read_consumed > 0) { | 
|  | 963 | mIn.setDataSize(bwr.read_consumed); | 
|  | 964 | mIn.setDataPosition(0); | 
|  | 965 | } | 
|  | 966 | IF_LOG_COMMANDS() { | 
|  | 967 | TextOutput::Bundle _b(alog); | 
|  | 968 | alog << "Remaining data size: " << mOut.dataSize() << endl; | 
|  | 969 | alog << "Received commands from driver: " << indent; | 
|  | 970 | const void* cmds = mIn.data(); | 
|  | 971 | const void* end = mIn.data() + mIn.dataSize(); | 
|  | 972 | alog << HexDump(cmds, mIn.dataSize()) << endl; | 
|  | 973 | while (cmds < end) cmds = printReturnCommand(alog, cmds); | 
|  | 974 | alog << dedent; | 
|  | 975 | } | 
|  | 976 | return NO_ERROR; | 
|  | 977 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 978 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 979 | return err; | 
|  | 980 | } | 
|  | 981 |  | 
|  | 982 | status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags, | 
|  | 983 | int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer) | 
|  | 984 | { | 
|  | 985 | binder_transaction_data tr; | 
|  | 986 |  | 
| Arve HjønnevÄg | 07fd0f1 | 2014-02-18 21:10:29 -0800 | [diff] [blame] | 987 | tr.target.ptr = 0; /* Don't pass uninitialized stack data to a remote process */ | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 988 | tr.target.handle = handle; | 
|  | 989 | tr.code = code; | 
|  | 990 | tr.flags = binderFlags; | 
| Evgeniy Stepanov | d547432 | 2011-04-21 14:15:00 +0400 | [diff] [blame] | 991 | tr.cookie = 0; | 
|  | 992 | tr.sender_pid = 0; | 
|  | 993 | tr.sender_euid = 0; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 994 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 995 | const status_t err = data.errorCheck(); | 
|  | 996 | if (err == NO_ERROR) { | 
|  | 997 | tr.data_size = data.ipcDataSize(); | 
|  | 998 | tr.data.ptr.buffer = data.ipcData(); | 
| Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 999 | tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1000 | tr.data.ptr.offsets = data.ipcObjects(); | 
|  | 1001 | } else if (statusBuffer) { | 
|  | 1002 | tr.flags |= TF_STATUS_CODE; | 
|  | 1003 | *statusBuffer = err; | 
|  | 1004 | tr.data_size = sizeof(status_t); | 
| Arve HjønnevÄg | 87b30d0 | 2014-02-18 21:04:31 -0800 | [diff] [blame] | 1005 | tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1006 | tr.offsets_size = 0; | 
| Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1007 | tr.data.ptr.offsets = 0; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1008 | } else { | 
|  | 1009 | return (mLastError = err); | 
|  | 1010 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1011 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1012 | mOut.writeInt32(cmd); | 
|  | 1013 | mOut.write(&tr, sizeof(tr)); | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1014 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1015 | return NO_ERROR; | 
|  | 1016 | } | 
|  | 1017 |  | 
|  | 1018 | sp<BBinder> the_context_object; | 
|  | 1019 |  | 
|  | 1020 | void setTheContextObject(sp<BBinder> obj) | 
|  | 1021 | { | 
|  | 1022 | the_context_object = obj; | 
|  | 1023 | } | 
|  | 1024 |  | 
|  | 1025 | status_t IPCThreadState::executeCommand(int32_t cmd) | 
|  | 1026 | { | 
|  | 1027 | BBinder* obj; | 
|  | 1028 | RefBase::weakref_type* refs; | 
|  | 1029 | status_t result = NO_ERROR; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1030 |  | 
| Bernhard RosenkrƤnzer | 74debb0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 1031 | switch ((uint32_t)cmd) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1032 | case BR_ERROR: | 
|  | 1033 | result = mIn.readInt32(); | 
|  | 1034 | break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1035 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1036 | case BR_OK: | 
|  | 1037 | break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1038 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1039 | case BR_ACQUIRE: | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1040 | refs = (RefBase::weakref_type*)mIn.readPointer(); | 
|  | 1041 | obj = (BBinder*)mIn.readPointer(); | 
| Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1042 | ALOG_ASSERT(refs->refBase() == obj, | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1043 | "BR_ACQUIRE: object %p does not match cookie %p (expected %p)", | 
|  | 1044 | refs, obj, refs->refBase()); | 
|  | 1045 | obj->incStrong(mProcess.get()); | 
|  | 1046 | IF_LOG_REMOTEREFS() { | 
|  | 1047 | LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj); | 
|  | 1048 | obj->printRefs(); | 
|  | 1049 | } | 
|  | 1050 | mOut.writeInt32(BC_ACQUIRE_DONE); | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1051 | mOut.writePointer((uintptr_t)refs); | 
|  | 1052 | mOut.writePointer((uintptr_t)obj); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1053 | break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1054 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1055 | case BR_RELEASE: | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1056 | refs = (RefBase::weakref_type*)mIn.readPointer(); | 
|  | 1057 | obj = (BBinder*)mIn.readPointer(); | 
| Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1058 | ALOG_ASSERT(refs->refBase() == obj, | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1059 | "BR_RELEASE: object %p does not match cookie %p (expected %p)", | 
|  | 1060 | refs, obj, refs->refBase()); | 
|  | 1061 | IF_LOG_REMOTEREFS() { | 
|  | 1062 | LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj); | 
|  | 1063 | obj->printRefs(); | 
|  | 1064 | } | 
|  | 1065 | mPendingStrongDerefs.push(obj); | 
|  | 1066 | break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1067 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1068 | case BR_INCREFS: | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1069 | refs = (RefBase::weakref_type*)mIn.readPointer(); | 
|  | 1070 | obj = (BBinder*)mIn.readPointer(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1071 | refs->incWeak(mProcess.get()); | 
|  | 1072 | mOut.writeInt32(BC_INCREFS_DONE); | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1073 | mOut.writePointer((uintptr_t)refs); | 
|  | 1074 | mOut.writePointer((uintptr_t)obj); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1075 | break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1076 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1077 | case BR_DECREFS: | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1078 | refs = (RefBase::weakref_type*)mIn.readPointer(); | 
|  | 1079 | obj = (BBinder*)mIn.readPointer(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1080 | // NOTE: This assertion is not valid, because the object may no | 
|  | 1081 | // longer exist (thus the (BBinder*)cast above resulting in a different | 
|  | 1082 | // memory address). | 
| Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1083 | //ALOG_ASSERT(refs->refBase() == obj, | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1084 | //           "BR_DECREFS: object %p does not match cookie %p (expected %p)", | 
|  | 1085 | //           refs, obj, refs->refBase()); | 
|  | 1086 | mPendingWeakDerefs.push(refs); | 
|  | 1087 | break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1088 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1089 | case BR_ATTEMPT_ACQUIRE: | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1090 | refs = (RefBase::weakref_type*)mIn.readPointer(); | 
|  | 1091 | obj = (BBinder*)mIn.readPointer(); | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1092 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1093 | { | 
|  | 1094 | const bool success = refs->attemptIncStrong(mProcess.get()); | 
| Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1095 | ALOG_ASSERT(success && refs->refBase() == obj, | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1096 | "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)", | 
|  | 1097 | refs, obj, refs->refBase()); | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1098 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1099 | mOut.writeInt32(BC_ACQUIRE_RESULT); | 
|  | 1100 | mOut.writeInt32((int32_t)success); | 
|  | 1101 | } | 
|  | 1102 | break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1103 |  | 
| Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 1104 | case BR_TRANSACTION_SEC_CTX: | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1105 | case BR_TRANSACTION: | 
|  | 1106 | { | 
| Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 1107 | binder_transaction_data_secctx tr_secctx; | 
|  | 1108 | binder_transaction_data& tr = tr_secctx.transaction_data; | 
|  | 1109 |  | 
|  | 1110 | if (cmd == (int) BR_TRANSACTION_SEC_CTX) { | 
|  | 1111 | result = mIn.read(&tr_secctx, sizeof(tr_secctx)); | 
|  | 1112 | } else { | 
|  | 1113 | result = mIn.read(&tr, sizeof(tr)); | 
|  | 1114 | tr_secctx.secctx = 0; | 
|  | 1115 | } | 
|  | 1116 |  | 
| Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1117 | ALOG_ASSERT(result == NO_ERROR, | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1118 | "Not enough command data for brTRANSACTION"); | 
|  | 1119 | if (result != NO_ERROR) break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1120 |  | 
| Jayant Chowdhary | dac6dc8 | 2018-10-01 22:52:44 +0000 | [diff] [blame] | 1121 | //Record the fact that we're in a binder call. | 
|  | 1122 | mIPCThreadStateBase->pushCurrentState( | 
|  | 1123 | IPCThreadStateBase::CallState::BINDER); | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1124 | Parcel buffer; | 
|  | 1125 | buffer.ipcSetDataReference( | 
|  | 1126 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), | 
|  | 1127 | tr.data_size, | 
|  | 1128 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), | 
|  | 1129 | tr.offsets_size/sizeof(binder_size_t), freeBuffer, this); | 
|  | 1130 |  | 
|  | 1131 | const pid_t origPid = mCallingPid; | 
| Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 1132 | const char* origSid = mCallingSid; | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1133 | const uid_t origUid = mCallingUid; | 
|  | 1134 | const int32_t origStrictModePolicy = mStrictModePolicy; | 
|  | 1135 | const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags; | 
|  | 1136 |  | 
|  | 1137 | mCallingPid = tr.sender_pid; | 
| Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 1138 | mCallingSid = reinterpret_cast<const char*>(tr_secctx.secctx); | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1139 | mCallingUid = tr.sender_euid; | 
|  | 1140 | mLastTransactionBinderFlags = tr.flags; | 
|  | 1141 |  | 
| Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 1142 | // ALOGI(">>>> TRANSACT from pid %d sid %s uid %d\n", mCallingPid, | 
|  | 1143 | //    (mCallingSid ? mCallingSid : "<N/A>"), mCallingUid); | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1144 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1145 | Parcel reply; | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1146 | status_t error; | 
|  | 1147 | IF_LOG_TRANSACTIONS() { | 
|  | 1148 | TextOutput::Bundle _b(alog); | 
|  | 1149 | alog << "BR_TRANSACTION thr " << (void*)pthread_self() | 
|  | 1150 | << " / obj " << tr.target.ptr << " / code " | 
|  | 1151 | << TypeCode(tr.code) << ": " << indent << buffer | 
|  | 1152 | << dedent << endl | 
|  | 1153 | << "Data addr = " | 
|  | 1154 | << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer) | 
|  | 1155 | << ", offsets addr=" | 
|  | 1156 | << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl; | 
|  | 1157 | } | 
|  | 1158 | if (tr.target.ptr) { | 
|  | 1159 | // We only have a weak reference on the target object, so we must first try to | 
|  | 1160 | // safely acquire a strong reference before doing anything else with it. | 
|  | 1161 | if (reinterpret_cast<RefBase::weakref_type*>( | 
|  | 1162 | tr.target.ptr)->attemptIncStrong(this)) { | 
|  | 1163 | error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer, | 
|  | 1164 | &reply, tr.flags); | 
|  | 1165 | reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this); | 
| Dianne Hackborn | c111461 | 2016-03-21 10:36:54 -0700 | [diff] [blame] | 1166 | } else { | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1167 | error = UNKNOWN_TRANSACTION; | 
| Dianne Hackborn | c111461 | 2016-03-21 10:36:54 -0700 | [diff] [blame] | 1168 | } | 
| Brad Fitzpatrick | 5273603 | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 1169 |  | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1170 | } else { | 
|  | 1171 | error = the_context_object->transact(tr.code, buffer, &reply, tr.flags); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1172 | } | 
| Dianne Hackborn | 5ee2c9d | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1173 |  | 
| Jayant Chowdhary | dac6dc8 | 2018-10-01 22:52:44 +0000 | [diff] [blame] | 1174 | mIPCThreadStateBase->popCurrentState(); | 
| Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 1175 | //ALOGI("<<<< TRANSACT from pid %d restore pid %d sid %s uid %d\n", | 
|  | 1176 | //     mCallingPid, origPid, (origSid ? origSid : "<N/A>"), origUid); | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1177 |  | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1178 | if ((tr.flags & TF_ONE_WAY) == 0) { | 
|  | 1179 | LOG_ONEWAY("Sending reply to %d!", mCallingPid); | 
|  | 1180 | if (error < NO_ERROR) reply.setError(error); | 
|  | 1181 | sendReply(reply, 0); | 
|  | 1182 | } else { | 
|  | 1183 | LOG_ONEWAY("NOT sending reply to %d!", mCallingPid); | 
|  | 1184 | } | 
|  | 1185 |  | 
|  | 1186 | mCallingPid = origPid; | 
| Steven Moreland | 3085a47 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 1187 | mCallingSid = origSid; | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1188 | mCallingUid = origUid; | 
|  | 1189 | mStrictModePolicy = origStrictModePolicy; | 
|  | 1190 | mLastTransactionBinderFlags = origTransactionBinderFlags; | 
| Christopher Tate | 440fd87 | 2010-03-18 17:55:03 -0700 | [diff] [blame] | 1191 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1192 | IF_LOG_TRANSACTIONS() { | 
|  | 1193 | TextOutput::Bundle _b(alog); | 
|  | 1194 | alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj " | 
|  | 1195 | << tr.target.ptr << ": " << indent << reply << dedent << endl; | 
|  | 1196 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1197 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1198 | } | 
|  | 1199 | break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1200 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1201 | case BR_DEAD_BINDER: | 
|  | 1202 | { | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1203 | BpBinder *proxy = (BpBinder*)mIn.readPointer(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1204 | proxy->sendObituary(); | 
|  | 1205 | mOut.writeInt32(BC_DEAD_BINDER_DONE); | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1206 | mOut.writePointer((uintptr_t)proxy); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1207 | } break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1208 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1209 | case BR_CLEAR_DEATH_NOTIFICATION_DONE: | 
|  | 1210 | { | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1211 | BpBinder *proxy = (BpBinder*)mIn.readPointer(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1212 | proxy->getWeakRefs()->decWeak(proxy); | 
|  | 1213 | } break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1214 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1215 | case BR_FINISHED: | 
|  | 1216 | result = TIMED_OUT; | 
|  | 1217 | break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1218 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1219 | case BR_NOOP: | 
|  | 1220 | break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1221 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1222 | case BR_SPAWN_LOOPER: | 
|  | 1223 | mProcess->spawnPooledThread(false); | 
|  | 1224 | break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1225 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1226 | default: | 
| liangweikang | a43ee15 | 2016-10-25 16:37:54 +0800 | [diff] [blame] | 1227 | ALOGE("*** BAD COMMAND %d received from Binder driver\n", cmd); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1228 | result = UNKNOWN_ERROR; | 
|  | 1229 | break; | 
|  | 1230 | } | 
|  | 1231 |  | 
|  | 1232 | if (result != NO_ERROR) { | 
|  | 1233 | mLastError = result; | 
|  | 1234 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1235 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1236 | return result; | 
|  | 1237 | } | 
|  | 1238 |  | 
| Jayant Chowdhary | dac6dc8 | 2018-10-01 22:52:44 +0000 | [diff] [blame] | 1239 | bool IPCThreadState::isServingCall() const { | 
|  | 1240 | return mIPCThreadStateBase->getCurrentBinderCallState() == IPCThreadStateBase::CallState::BINDER; | 
|  | 1241 | } | 
|  | 1242 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1243 | void IPCThreadState::threadDestructor(void *st) | 
|  | 1244 | { | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 1245 | IPCThreadState* const self = static_cast<IPCThreadState*>(st); | 
|  | 1246 | if (self) { | 
|  | 1247 | self->flushCommands(); | 
| Elliott Hughes | 6071da7 | 2015-08-12 15:27:47 -0700 | [diff] [blame] | 1248 | #if defined(__ANDROID__) | 
| Alexandre Baião | c60c4fc | 2019-07-31 12:29:31 -0200 | [diff] [blame] | 1249 | if (self->mProcess->mDriverFD >= 0) { | 
| Johannes Carlsson | db1597a | 2011-02-17 14:06:53 +0100 | [diff] [blame] | 1250 | ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0); | 
|  | 1251 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1252 | #endif | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 1253 | delete self; | 
|  | 1254 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1255 | } | 
|  | 1256 |  | 
|  | 1257 |  | 
| Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 1258 | void IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data, | 
|  | 1259 | size_t /*dataSize*/, | 
|  | 1260 | const binder_size_t* /*objects*/, | 
|  | 1261 | size_t /*objectsSize*/, void* /*cookie*/) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1262 | { | 
| Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1263 | //ALOGI("Freeing parcel %p", &parcel); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1264 | IF_LOG_COMMANDS() { | 
|  | 1265 | alog << "Writing BC_FREE_BUFFER for " << data << endl; | 
|  | 1266 | } | 
| Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1267 | ALOG_ASSERT(data != NULL, "Called with NULL data"); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 1268 | if (parcel != nullptr) parcel->closeFileDescriptors(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1269 | IPCThreadState* state = self(); | 
|  | 1270 | state->mOut.writeInt32(BC_FREE_BUFFER); | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1271 | state->mOut.writePointer((uintptr_t)data); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1272 | } | 
|  | 1273 |  | 
|  | 1274 | }; // namespace android |