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