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