| 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 |  | 
| Hans Boehm | a997b23 | 2019-04-12 16:59:00 -0700 | [diff] [blame] | 31 | #include <atomic> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | #include <errno.h> | 
| Colin Cross | 96e8322 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 33 | #include <inttypes.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | #include <pthread.h> | 
|  | 35 | #include <sched.h> | 
| Yabin Cui | 8fb2d25 | 2015-01-26 19:45:47 -0800 | [diff] [blame] | 36 | #include <signal.h> | 
|  | 37 | #include <stdio.h> | 
|  | 38 | #include <sys/ioctl.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | #include <sys/resource.h> | 
| Yabin Cui | 8fb2d25 | 2015-01-26 19:45:47 -0800 | [diff] [blame] | 40 | #include <unistd.h> | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 |  | 
| Steven Moreland | a4853cd | 2019-07-12 15:44:37 -0700 | [diff] [blame] | 42 | #include "Static.h" | 
| Steven Moreland | 6ba5a25 | 2021-05-04 22:49:00 +0000 | [diff] [blame] | 43 | #include "binder_module.h" | 
| Steven Moreland | a4853cd | 2019-07-12 15:44:37 -0700 | [diff] [blame] | 44 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | #if LOG_NDEBUG | 
|  | 46 |  | 
|  | 47 | #define IF_LOG_TRANSACTIONS() if (false) | 
|  | 48 | #define IF_LOG_COMMANDS() if (false) | 
|  | 49 | #define LOG_REMOTEREFS(...) | 
|  | 50 | #define IF_LOG_REMOTEREFS() if (false) | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 51 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | #define LOG_THREADPOOL(...) | 
|  | 53 | #define LOG_ONEWAY(...) | 
|  | 54 |  | 
|  | 55 | #else | 
|  | 56 |  | 
| Steve Block | 9f76015 | 2011-10-12 17:27:03 +0100 | [diff] [blame] | 57 | #define IF_LOG_TRANSACTIONS() IF_ALOG(LOG_VERBOSE, "transact") | 
|  | 58 | #define IF_LOG_COMMANDS() IF_ALOG(LOG_VERBOSE, "ipc") | 
|  | 59 | #define LOG_REMOTEREFS(...) ALOG(LOG_DEBUG, "remoterefs", __VA_ARGS__) | 
|  | 60 | #define IF_LOG_REMOTEREFS() IF_ALOG(LOG_DEBUG, "remoterefs") | 
|  | 61 | #define LOG_THREADPOOL(...) ALOG(LOG_DEBUG, "threadpool", __VA_ARGS__) | 
|  | 62 | #define LOG_ONEWAY(...) ALOG(LOG_DEBUG, "ipc", __VA_ARGS__) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 |  | 
|  | 64 | #endif | 
|  | 65 |  | 
|  | 66 | // --------------------------------------------------------------------------- | 
|  | 67 |  | 
|  | 68 | namespace android { | 
|  | 69 |  | 
| Chih-Hung Hsieh | 8e5337d | 2014-10-24 14:10:09 -0700 | [diff] [blame] | 70 | // Static const and functions will be optimized out if not used, | 
|  | 71 | // 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] | 72 | static const char *kReturnStrings[] = { | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 73 | "BR_ERROR", | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | "BR_OK", | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | "BR_TRANSACTION", | 
|  | 76 | "BR_REPLY", | 
|  | 77 | "BR_ACQUIRE_RESULT", | 
|  | 78 | "BR_DEAD_REPLY", | 
|  | 79 | "BR_TRANSACTION_COMPLETE", | 
|  | 80 | "BR_INCREFS", | 
|  | 81 | "BR_ACQUIRE", | 
|  | 82 | "BR_RELEASE", | 
|  | 83 | "BR_DECREFS", | 
|  | 84 | "BR_ATTEMPT_ACQUIRE", | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | "BR_NOOP", | 
|  | 86 | "BR_SPAWN_LOOPER", | 
|  | 87 | "BR_FINISHED", | 
|  | 88 | "BR_DEAD_BINDER", | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 89 | "BR_CLEAR_DEATH_NOTIFICATION_DONE", | 
| Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 90 | "BR_FAILED_REPLY", | 
| Hang Lu | b185ac0 | 2021-03-24 13:17:22 +0800 | [diff] [blame] | 91 | "BR_FROZEN_REPLY", | 
|  | 92 | "BR_ONEWAY_SPAM_SUSPECT", | 
| Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 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 | 
| Jiyong Park | 16c6e70 | 2020-11-13 20:53:12 +0900 | [diff] [blame] | 138 | << "code=" << TypeCode(btd->code) << ", flags=" << (void*)(uint64_t)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) { | 
| Jiyong Park | 16c6e70 | 2020-11-13 20:53:12 +0900 | [diff] [blame] | 153 | out << "BR_ERROR: " << (void*)(uint64_t)(*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++; | 
| Jiyong Park | 16c6e70 | 2020-11-13 20:53:12 +0900 | [diff] [blame] | 180 | out << ": target=" << (void*)(uint64_t)b << " (cookie " << (void*)(uint64_t)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++; | 
| Jiyong Park | 16c6e70 | 2020-11-13 20:53:12 +0900 | [diff] [blame] | 187 | out << ": target=" << (void*)(uint64_t)b << " (cookie " << (void*)(uint64_t)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++; | 
| Jiyong Park | 16c6e70 | 2020-11-13 20:53:12 +0900 | [diff] [blame] | 194 | out << ": death cookie " << (void*)(uint64_t)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++; | 
| Jiyong Park | 16c6e70 | 2020-11-13 20:53:12 +0900 | [diff] [blame] | 235 | out << ": buffer=" << (void*)(uint64_t)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++; | 
| Jiyong Park | 16c6e70 | 2020-11-13 20:53:12 +0900 | [diff] [blame] | 250 | out << ": target=" << (void*)(uint64_t)b << " (cookie " << (void*)(uint64_t)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++; | 
| Jiyong Park | 16c6e70 | 2020-11-13 20:53:12 +0900 | [diff] [blame] | 263 | out << ": handle=" << h << " (death cookie " << (void*)(uint64_t)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++; | 
| Jiyong Park | 16c6e70 | 2020-11-13 20:53:12 +0900 | [diff] [blame] | 268 | out << ": death cookie " << (void*)(uint64_t)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 | status_t IPCThreadState::clearLastError() | 
|  | 355 | { | 
|  | 356 | const status_t err = mLastError; | 
|  | 357 | mLastError = NO_ERROR; | 
|  | 358 | return err; | 
|  | 359 | } | 
|  | 360 |  | 
| Dan Stoza | 9c634fd | 2014-11-26 12:23:23 -0800 | [diff] [blame] | 361 | pid_t IPCThreadState::getCallingPid() const | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 362 | { | 
| Steven Moreland | 3562665 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 363 | checkContextIsBinderForUse(__func__); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 364 | return mCallingPid; | 
|  | 365 | } | 
|  | 366 |  | 
| Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 367 | const char* IPCThreadState::getCallingSid() const | 
|  | 368 | { | 
| Steven Moreland | 3562665 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 369 | checkContextIsBinderForUse(__func__); | 
| Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 370 | return mCallingSid; | 
|  | 371 | } | 
|  | 372 |  | 
| Dan Stoza | 9c634fd | 2014-11-26 12:23:23 -0800 | [diff] [blame] | 373 | uid_t IPCThreadState::getCallingUid() const | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | { | 
| Steven Moreland | 3562665 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 375 | checkContextIsBinderForUse(__func__); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 376 | return mCallingUid; | 
|  | 377 | } | 
|  | 378 |  | 
| Steven Moreland | 3562665 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 379 | const IPCThreadState::SpGuard* IPCThreadState::pushGetCallingSpGuard(const SpGuard* guard) { | 
|  | 380 | const SpGuard* orig = mServingStackPointerGuard; | 
|  | 381 | mServingStackPointerGuard = guard; | 
|  | 382 | return orig; | 
|  | 383 | } | 
|  | 384 |  | 
|  | 385 | void IPCThreadState::restoreGetCallingSpGuard(const SpGuard* guard) { | 
|  | 386 | mServingStackPointerGuard = guard; | 
|  | 387 | } | 
|  | 388 |  | 
|  | 389 | void IPCThreadState::checkContextIsBinderForUse(const char* use) const { | 
|  | 390 | if (LIKELY(mServingStackPointerGuard == nullptr)) return; | 
|  | 391 |  | 
|  | 392 | if (!mServingStackPointer || mServingStackPointerGuard->address < mServingStackPointer) { | 
|  | 393 | LOG_ALWAYS_FATAL("In context %s, %s does not make sense (binder sp: %p, guard: %p).", | 
|  | 394 | mServingStackPointerGuard->context, use, mServingStackPointer, | 
|  | 395 | mServingStackPointerGuard->address); | 
|  | 396 | } | 
|  | 397 |  | 
|  | 398 | // in the case mServingStackPointer is deeper in the stack than the guard, | 
|  | 399 | // we must be serving a binder transaction (maybe nested). This is a binder | 
|  | 400 | // context, so we don't abort | 
|  | 401 | } | 
|  | 402 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | int64_t IPCThreadState::clearCallingIdentity() | 
|  | 404 | { | 
| Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 405 | // ignore mCallingSid for legacy reasons | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | int64_t token = ((int64_t)mCallingUid<<32) | mCallingPid; | 
|  | 407 | clearCaller(); | 
|  | 408 | return token; | 
|  | 409 | } | 
|  | 410 |  | 
| Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 411 | void IPCThreadState::setStrictModePolicy(int32_t policy) | 
|  | 412 | { | 
|  | 413 | mStrictModePolicy = policy; | 
|  | 414 | } | 
|  | 415 |  | 
| Brad Fitzpatrick | a877cd8 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 416 | int32_t IPCThreadState::getStrictModePolicy() const | 
|  | 417 | { | 
| Brad Fitzpatrick | 702ea9d | 2010-06-18 13:07:53 -0700 | [diff] [blame] | 418 | return mStrictModePolicy; | 
|  | 419 | } | 
|  | 420 |  | 
| Olivier Gaillard | a8e7bf2 | 2018-11-14 15:35:50 +0000 | [diff] [blame] | 421 | int64_t IPCThreadState::setCallingWorkSourceUid(uid_t uid) | 
| Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 422 | { | 
| Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame] | 423 | int64_t token = setCallingWorkSourceUidWithoutPropagation(uid); | 
|  | 424 | mPropagateWorkSource = true; | 
|  | 425 | return token; | 
|  | 426 | } | 
|  | 427 |  | 
|  | 428 | int64_t IPCThreadState::setCallingWorkSourceUidWithoutPropagation(uid_t uid) | 
|  | 429 | { | 
|  | 430 | const int64_t propagatedBit = ((int64_t)mPropagateWorkSource) << kWorkSourcePropagatedBitIndex; | 
|  | 431 | int64_t token = propagatedBit | mWorkSource; | 
| Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 432 | mWorkSource = uid; | 
| Olivier Gaillard | a8e7bf2 | 2018-11-14 15:35:50 +0000 | [diff] [blame] | 433 | return token; | 
| Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 434 | } | 
|  | 435 |  | 
| Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame] | 436 | void IPCThreadState::clearPropagateWorkSource() | 
|  | 437 | { | 
|  | 438 | mPropagateWorkSource = false; | 
|  | 439 | } | 
|  | 440 |  | 
|  | 441 | bool IPCThreadState::shouldPropagateWorkSource() const | 
|  | 442 | { | 
|  | 443 | return mPropagateWorkSource; | 
|  | 444 | } | 
|  | 445 |  | 
| Olivier Gaillard | a8e7bf2 | 2018-11-14 15:35:50 +0000 | [diff] [blame] | 446 | uid_t IPCThreadState::getCallingWorkSourceUid() const | 
| Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 447 | { | 
|  | 448 | return mWorkSource; | 
|  | 449 | } | 
|  | 450 |  | 
| Olivier Gaillard | a8e7bf2 | 2018-11-14 15:35:50 +0000 | [diff] [blame] | 451 | int64_t IPCThreadState::clearCallingWorkSource() | 
| Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 452 | { | 
| Olivier Gaillard | a8e7bf2 | 2018-11-14 15:35:50 +0000 | [diff] [blame] | 453 | return setCallingWorkSourceUid(kUnsetWorkSource); | 
|  | 454 | } | 
|  | 455 |  | 
|  | 456 | void IPCThreadState::restoreCallingWorkSource(int64_t token) | 
|  | 457 | { | 
|  | 458 | uid_t uid = (int)token; | 
| Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame] | 459 | setCallingWorkSourceUidWithoutPropagation(uid); | 
|  | 460 | mPropagateWorkSource = ((token >> kWorkSourcePropagatedBitIndex) & 1) == 1; | 
| Olivier Gaillard | 0e0f1de | 2018-08-16 14:04:09 +0100 | [diff] [blame] | 461 | } | 
|  | 462 |  | 
| Brad Fitzpatrick | 5273603 | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 463 | void IPCThreadState::setLastTransactionBinderFlags(int32_t flags) | 
|  | 464 | { | 
|  | 465 | mLastTransactionBinderFlags = flags; | 
|  | 466 | } | 
|  | 467 |  | 
|  | 468 | int32_t IPCThreadState::getLastTransactionBinderFlags() const | 
|  | 469 | { | 
|  | 470 | return mLastTransactionBinderFlags; | 
|  | 471 | } | 
|  | 472 |  | 
| Steven Moreland | 9514b20 | 2020-09-21 18:03:27 +0000 | [diff] [blame] | 473 | void IPCThreadState::setCallRestriction(ProcessState::CallRestriction restriction) { | 
|  | 474 | mCallRestriction = restriction; | 
|  | 475 | } | 
|  | 476 |  | 
|  | 477 | ProcessState::CallRestriction IPCThreadState::getCallRestriction() const { | 
|  | 478 | return mCallRestriction; | 
|  | 479 | } | 
|  | 480 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 481 | void IPCThreadState::restoreCallingIdentity(int64_t token) | 
|  | 482 | { | 
|  | 483 | mCallingUid = (int)(token>>32); | 
| Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 484 | mCallingSid = nullptr;  // not enough data to restore | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 485 | mCallingPid = (int)token; | 
|  | 486 | } | 
|  | 487 |  | 
|  | 488 | void IPCThreadState::clearCaller() | 
|  | 489 | { | 
| Marco Nelissen | d43b194 | 2009-07-17 07:59:17 -0700 | [diff] [blame] | 490 | mCallingPid = getpid(); | 
| Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 491 | mCallingSid = nullptr;  // expensive to lookup | 
| Marco Nelissen | d43b194 | 2009-07-17 07:59:17 -0700 | [diff] [blame] | 492 | mCallingUid = getuid(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | } | 
|  | 494 |  | 
|  | 495 | void IPCThreadState::flushCommands() | 
|  | 496 | { | 
| Alexandre Baião | c60c4fc | 2019-07-31 12:29:31 -0200 | [diff] [blame] | 497 | if (mProcess->mDriverFD < 0) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 498 | return; | 
|  | 499 | talkWithDriver(false); | 
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 500 | // The flush could have caused post-write refcount decrements to have | 
|  | 501 | // been executed, which in turn could result in BC_RELEASE/BC_DECREFS | 
|  | 502 | // being queued in mOut. So flush again, if we need to. | 
|  | 503 | if (mOut.dataSize() > 0) { | 
|  | 504 | talkWithDriver(false); | 
|  | 505 | } | 
|  | 506 | if (mOut.dataSize() > 0) { | 
|  | 507 | ALOGW("mOut.dataSize() > 0 after flushCommands()"); | 
|  | 508 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | } | 
|  | 510 |  | 
| Martijn Coenen | 0442a86 | 2017-11-17 10:46:32 +0100 | [diff] [blame] | 511 | bool IPCThreadState::flushIfNeeded() | 
|  | 512 | { | 
| Frankie Chang | f4c8137 | 2021-05-18 13:08:05 +0800 | [diff] [blame] | 513 | if (mIsLooper || mServingStackPointer != nullptr || mIsFlushing) { | 
| Martijn Coenen | 0442a86 | 2017-11-17 10:46:32 +0100 | [diff] [blame] | 514 | return false; | 
|  | 515 | } | 
| Frankie Chang | f4c8137 | 2021-05-18 13:08:05 +0800 | [diff] [blame] | 516 | mIsFlushing = true; | 
| Martijn Coenen | 0442a86 | 2017-11-17 10:46:32 +0100 | [diff] [blame] | 517 | // In case this thread is not a looper and is not currently serving a binder transaction, | 
|  | 518 | // there's no guarantee that this thread will call back into the kernel driver any time | 
|  | 519 | // soon. Therefore, flush pending commands such as BC_FREE_BUFFER, to prevent them from getting | 
|  | 520 | // stuck in this thread's out buffer. | 
|  | 521 | flushCommands(); | 
| Frankie Chang | f4c8137 | 2021-05-18 13:08:05 +0800 | [diff] [blame] | 522 | mIsFlushing = false; | 
| Martijn Coenen | 0442a86 | 2017-11-17 10:46:32 +0100 | [diff] [blame] | 523 | return true; | 
|  | 524 | } | 
|  | 525 |  | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 526 | void IPCThreadState::blockUntilThreadAvailable() | 
|  | 527 | { | 
|  | 528 | pthread_mutex_lock(&mProcess->mThreadCountLock); | 
| Steven Moreland | c648a76 | 2021-01-16 02:39:45 +0000 | [diff] [blame] | 529 | mProcess->mWaitingForThreads++; | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 530 | while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) { | 
| Wale Ogunwale | a3206e6 | 2015-04-21 12:29:50 -0700 | [diff] [blame] | 531 | ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%lu mMaxThreads=%lu\n", | 
|  | 532 | static_cast<unsigned long>(mProcess->mExecutingThreadsCount), | 
|  | 533 | static_cast<unsigned long>(mProcess->mMaxThreads)); | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 534 | pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock); | 
|  | 535 | } | 
| Steven Moreland | c648a76 | 2021-01-16 02:39:45 +0000 | [diff] [blame] | 536 | mProcess->mWaitingForThreads--; | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 537 | pthread_mutex_unlock(&mProcess->mThreadCountLock); | 
|  | 538 | } | 
|  | 539 |  | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 540 | status_t IPCThreadState::getAndExecuteCommand() | 
|  | 541 | { | 
|  | 542 | status_t result; | 
|  | 543 | int32_t cmd; | 
|  | 544 |  | 
|  | 545 | result = talkWithDriver(); | 
|  | 546 | if (result >= NO_ERROR) { | 
|  | 547 | size_t IN = mIn.dataAvail(); | 
|  | 548 | if (IN < sizeof(int32_t)) return result; | 
|  | 549 | cmd = mIn.readInt32(); | 
|  | 550 | IF_LOG_COMMANDS() { | 
|  | 551 | alog << "Processing top-level Command: " | 
|  | 552 | << getReturnString(cmd) << endl; | 
|  | 553 | } | 
|  | 554 |  | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 555 | pthread_mutex_lock(&mProcess->mThreadCountLock); | 
|  | 556 | mProcess->mExecutingThreadsCount++; | 
| Colin Cross | 96e8322 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 557 | if (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads && | 
|  | 558 | mProcess->mStarvationStartTimeMs == 0) { | 
|  | 559 | mProcess->mStarvationStartTimeMs = uptimeMillis(); | 
|  | 560 | } | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 561 | pthread_mutex_unlock(&mProcess->mThreadCountLock); | 
|  | 562 |  | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 563 | result = executeCommand(cmd); | 
|  | 564 |  | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 565 | pthread_mutex_lock(&mProcess->mThreadCountLock); | 
|  | 566 | mProcess->mExecutingThreadsCount--; | 
| Colin Cross | 96e8322 | 2016-04-15 14:29:55 -0700 | [diff] [blame] | 567 | if (mProcess->mExecutingThreadsCount < mProcess->mMaxThreads && | 
|  | 568 | mProcess->mStarvationStartTimeMs != 0) { | 
|  | 569 | int64_t starvationTimeMs = uptimeMillis() - mProcess->mStarvationStartTimeMs; | 
|  | 570 | if (starvationTimeMs > 100) { | 
|  | 571 | ALOGE("binder thread pool (%zu threads) starved for %" PRId64 " ms", | 
|  | 572 | mProcess->mMaxThreads, starvationTimeMs); | 
|  | 573 | } | 
|  | 574 | mProcess->mStarvationStartTimeMs = 0; | 
|  | 575 | } | 
| Steven Moreland | c648a76 | 2021-01-16 02:39:45 +0000 | [diff] [blame] | 576 |  | 
|  | 577 | // Cond broadcast can be expensive, so don't send it every time a binder | 
|  | 578 | // call is processed. b/168806193 | 
|  | 579 | if (mProcess->mWaitingForThreads > 0) { | 
|  | 580 | pthread_cond_broadcast(&mProcess->mThreadCountDecrement); | 
|  | 581 | } | 
| Wale Ogunwale | 376b822 | 2015-04-13 16:16:10 -0700 | [diff] [blame] | 582 | pthread_mutex_unlock(&mProcess->mThreadCountLock); | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 583 | } | 
|  | 584 |  | 
|  | 585 | return result; | 
|  | 586 | } | 
|  | 587 |  | 
|  | 588 | // When we've cleared the incoming command queue, process any pending derefs | 
|  | 589 | void IPCThreadState::processPendingDerefs() | 
|  | 590 | { | 
|  | 591 | if (mIn.dataPosition() >= mIn.dataSize()) { | 
| Martijn Coenen | 0791fbf | 2017-08-08 15:36:16 +0200 | [diff] [blame] | 592 | /* | 
|  | 593 | * The decWeak()/decStrong() calls may cause a destructor to run, | 
|  | 594 | * which in turn could have initiated an outgoing transaction, | 
|  | 595 | * which in turn could cause us to add to the pending refs | 
|  | 596 | * vectors; so instead of simply iterating, loop until they're empty. | 
|  | 597 | * | 
|  | 598 | * We do this in an outer loop, because calling decStrong() | 
|  | 599 | * may result in something being added to mPendingWeakDerefs, | 
|  | 600 | * which could be delayed until the next incoming command | 
|  | 601 | * from the driver if we don't process it now. | 
|  | 602 | */ | 
|  | 603 | while (mPendingWeakDerefs.size() > 0 || mPendingStrongDerefs.size() > 0) { | 
|  | 604 | while (mPendingWeakDerefs.size() > 0) { | 
|  | 605 | RefBase::weakref_type* refs = mPendingWeakDerefs[0]; | 
|  | 606 | mPendingWeakDerefs.removeAt(0); | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 607 | refs->decWeak(mProcess.get()); | 
|  | 608 | } | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 609 |  | 
| Martijn Coenen | 0791fbf | 2017-08-08 15:36:16 +0200 | [diff] [blame] | 610 | if (mPendingStrongDerefs.size() > 0) { | 
|  | 611 | // We don't use while() here because we don't want to re-order | 
|  | 612 | // strong and weak decs at all; if this decStrong() causes both a | 
|  | 613 | // decWeak() and a decStrong() to be queued, we want to process | 
|  | 614 | // the decWeak() first. | 
|  | 615 | BBinder* obj = mPendingStrongDerefs[0]; | 
|  | 616 | mPendingStrongDerefs.removeAt(0); | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 617 | obj->decStrong(mProcess.get()); | 
|  | 618 | } | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 619 | } | 
|  | 620 | } | 
|  | 621 | } | 
|  | 622 |  | 
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 623 | void IPCThreadState::processPostWriteDerefs() | 
|  | 624 | { | 
|  | 625 | for (size_t i = 0; i < mPostWriteWeakDerefs.size(); i++) { | 
|  | 626 | RefBase::weakref_type* refs = mPostWriteWeakDerefs[i]; | 
|  | 627 | refs->decWeak(mProcess.get()); | 
|  | 628 | } | 
|  | 629 | mPostWriteWeakDerefs.clear(); | 
|  | 630 |  | 
|  | 631 | for (size_t i = 0; i < mPostWriteStrongDerefs.size(); i++) { | 
|  | 632 | RefBase* obj = mPostWriteStrongDerefs[i]; | 
|  | 633 | obj->decStrong(mProcess.get()); | 
|  | 634 | } | 
|  | 635 | mPostWriteStrongDerefs.clear(); | 
|  | 636 | } | 
|  | 637 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | void IPCThreadState::joinThreadPool(bool isMain) | 
|  | 639 | { | 
|  | 640 | LOG_THREADPOOL("**** THREAD %p (PID %d) IS JOINING THE THREAD POOL\n", (void*)pthread_self(), getpid()); | 
|  | 641 |  | 
|  | 642 | mOut.writeInt32(isMain ? BC_ENTER_LOOPER : BC_REGISTER_LOOPER); | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 643 |  | 
| Martijn Coenen | 0442a86 | 2017-11-17 10:46:32 +0100 | [diff] [blame] | 644 | mIsLooper = true; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 645 | status_t result; | 
|  | 646 | do { | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 647 | processPendingDerefs(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 648 | // now get the next command to be processed, waiting if necessary | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 649 | result = getAndExecuteCommand(); | 
| Jason Parks | dcd3958 | 2009-11-03 12:14:38 -0800 | [diff] [blame] | 650 |  | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 651 | if (result < NO_ERROR && result != TIMED_OUT && result != -ECONNREFUSED && result != -EBADF) { | 
| Steven Moreland | 6adf33c | 2019-09-25 13:18:09 -0700 | [diff] [blame] | 652 | LOG_ALWAYS_FATAL("getAndExecuteCommand(fd=%d) returned unexpected error %d, aborting", | 
| Jeff Tinker | ef07386 | 2013-06-11 11:30:21 -0700 | [diff] [blame] | 653 | mProcess->mDriverFD, result); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 655 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 656 | // Let this thread exit the thread pool if it is no longer | 
|  | 657 | // needed and it is not the main process thread. | 
|  | 658 | if(result == TIMED_OUT && !isMain) { | 
|  | 659 | break; | 
|  | 660 | } | 
|  | 661 | } while (result != -ECONNREFUSED && result != -EBADF); | 
|  | 662 |  | 
| Wei Wang | c734143 | 2016-10-19 10:23:59 -0700 | [diff] [blame] | 663 | LOG_THREADPOOL("**** THREAD %p (PID %d) IS LEAVING THE THREAD POOL err=%d\n", | 
|  | 664 | (void*)pthread_self(), getpid(), result); | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 665 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 666 | mOut.writeInt32(BC_EXIT_LOOPER); | 
| Martijn Coenen | 0442a86 | 2017-11-17 10:46:32 +0100 | [diff] [blame] | 667 | mIsLooper = false; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 668 | talkWithDriver(false); | 
|  | 669 | } | 
|  | 670 |  | 
| Steven Moreland | d8c8567 | 2020-07-24 21:30:41 +0000 | [diff] [blame] | 671 | status_t IPCThreadState::setupPolling(int* fd) | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 672 | { | 
| Alexandre Baião | c60c4fc | 2019-07-31 12:29:31 -0200 | [diff] [blame] | 673 | if (mProcess->mDriverFD < 0) { | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 674 | return -EBADF; | 
|  | 675 | } | 
|  | 676 |  | 
|  | 677 | mOut.writeInt32(BC_ENTER_LOOPER); | 
| Steven Moreland | f210b50 | 2021-01-15 23:40:32 +0000 | [diff] [blame] | 678 | flushCommands(); | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 679 | *fd = mProcess->mDriverFD; | 
|  | 680 | return 0; | 
|  | 681 | } | 
|  | 682 |  | 
|  | 683 | status_t IPCThreadState::handlePolledCommands() | 
|  | 684 | { | 
|  | 685 | status_t result; | 
|  | 686 |  | 
|  | 687 | do { | 
|  | 688 | result = getAndExecuteCommand(); | 
|  | 689 | } while (mIn.dataPosition() < mIn.dataSize()); | 
|  | 690 |  | 
|  | 691 | processPendingDerefs(); | 
|  | 692 | flushCommands(); | 
|  | 693 | return result; | 
|  | 694 | } | 
|  | 695 |  | 
| Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 696 | void IPCThreadState::stopProcess(bool /*immediate*/) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 697 | { | 
| Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 698 | //ALOGI("**** STOPPING PROCESS"); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 699 | flushCommands(); | 
|  | 700 | int fd = mProcess->mDriverFD; | 
|  | 701 | mProcess->mDriverFD = -1; | 
|  | 702 | close(fd); | 
|  | 703 | //kill(getpid(), SIGKILL); | 
|  | 704 | } | 
|  | 705 |  | 
|  | 706 | status_t IPCThreadState::transact(int32_t handle, | 
|  | 707 | uint32_t code, const Parcel& data, | 
|  | 708 | Parcel* reply, uint32_t flags) | 
|  | 709 | { | 
| Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 710 | LOG_ALWAYS_FATAL_IF(data.isForRpc(), "Parcel constructed for RPC, but being used with binder."); | 
|  | 711 |  | 
| Ganesh Mahendran | 58e5daa | 2017-10-11 18:05:13 +0800 | [diff] [blame] | 712 | status_t err; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 713 |  | 
|  | 714 | flags |= TF_ACCEPT_FDS; | 
|  | 715 |  | 
|  | 716 | IF_LOG_TRANSACTIONS() { | 
|  | 717 | TextOutput::Bundle _b(alog); | 
|  | 718 | alog << "BC_TRANSACTION thr " << (void*)pthread_self() << " / hand " | 
|  | 719 | << handle << " / code " << TypeCode(code) << ": " | 
|  | 720 | << indent << data << dedent << endl; | 
|  | 721 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 722 |  | 
| Ganesh Mahendran | 58e5daa | 2017-10-11 18:05:13 +0800 | [diff] [blame] | 723 | LOG_ONEWAY(">>>> SEND from pid %d uid %d %s", getpid(), getuid(), | 
|  | 724 | (flags & TF_ONE_WAY) == 0 ? "READ REPLY" : "ONE WAY"); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 725 | err = writeTransactionData(BC_TRANSACTION, flags, handle, code, data, nullptr); | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 726 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 727 | if (err != NO_ERROR) { | 
|  | 728 | if (reply) reply->setError(err); | 
|  | 729 | return (mLastError = err); | 
|  | 730 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 731 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 732 | if ((flags & TF_ONE_WAY) == 0) { | 
| Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 733 | if (UNLIKELY(mCallRestriction != ProcessState::CallRestriction::NONE)) { | 
|  | 734 | if (mCallRestriction == ProcessState::CallRestriction::ERROR_IF_NOT_ONEWAY) { | 
| Steven Moreland | 8cb34fc | 2019-05-13 11:44:55 -0700 | [diff] [blame] | 735 | ALOGE("Process making non-oneway call (code: %u) but is restricted.", code); | 
| Steven Moreland | 7732a09 | 2019-01-02 17:54:16 -0800 | [diff] [blame] | 736 | CallStack::logStack("non-oneway call", CallStack::getCurrent(10).get(), | 
|  | 737 | ANDROID_LOG_ERROR); | 
|  | 738 | } else /* FATAL_IF_NOT_ONEWAY */ { | 
| Steven Moreland | fcc77f1 | 2020-09-01 01:16:11 +0000 | [diff] [blame] | 739 | 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] | 740 | } | 
|  | 741 | } | 
|  | 742 |  | 
| Dianne Hackborn | 67f78c4 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 743 | #if 0 | 
|  | 744 | if (code == 4) { // relayout | 
| Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 745 | ALOGI(">>>>>> CALLING transaction 4"); | 
| Dianne Hackborn | 67f78c4 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 746 | } else { | 
| Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 747 | ALOGI(">>>>>> CALLING transaction %d", code); | 
| Dianne Hackborn | 67f78c4 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 748 | } | 
|  | 749 | #endif | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 750 | if (reply) { | 
|  | 751 | err = waitForResponse(reply); | 
|  | 752 | } else { | 
|  | 753 | Parcel fakeReply; | 
|  | 754 | err = waitForResponse(&fakeReply); | 
|  | 755 | } | 
| Dianne Hackborn | 67f78c4 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 756 | #if 0 | 
|  | 757 | if (code == 4) { // relayout | 
| Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 758 | ALOGI("<<<<<< RETURNING transaction 4"); | 
| Dianne Hackborn | 67f78c4 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 759 | } else { | 
| Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 760 | ALOGI("<<<<<< RETURNING transaction %d", code); | 
| Dianne Hackborn | 67f78c4 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 761 | } | 
|  | 762 | #endif | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 763 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 764 | IF_LOG_TRANSACTIONS() { | 
|  | 765 | TextOutput::Bundle _b(alog); | 
|  | 766 | alog << "BR_REPLY thr " << (void*)pthread_self() << " / hand " | 
|  | 767 | << handle << ": "; | 
|  | 768 | if (reply) alog << indent << *reply << dedent << endl; | 
|  | 769 | else alog << "(none requested)" << endl; | 
|  | 770 | } | 
|  | 771 | } else { | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 772 | err = waitForResponse(nullptr, nullptr); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 773 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 774 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 775 | return err; | 
|  | 776 | } | 
|  | 777 |  | 
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 778 | void IPCThreadState::incStrongHandle(int32_t handle, BpBinder *proxy) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 779 | { | 
|  | 780 | LOG_REMOTEREFS("IPCThreadState::incStrongHandle(%d)\n", handle); | 
|  | 781 | mOut.writeInt32(BC_ACQUIRE); | 
|  | 782 | mOut.writeInt32(handle); | 
| Martijn Coenen | 0442a86 | 2017-11-17 10:46:32 +0100 | [diff] [blame] | 783 | if (!flushIfNeeded()) { | 
|  | 784 | // Create a temp reference until the driver has handled this command. | 
|  | 785 | proxy->incStrong(mProcess.get()); | 
|  | 786 | mPostWriteStrongDerefs.push(proxy); | 
|  | 787 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 788 | } | 
|  | 789 |  | 
|  | 790 | void IPCThreadState::decStrongHandle(int32_t handle) | 
|  | 791 | { | 
|  | 792 | LOG_REMOTEREFS("IPCThreadState::decStrongHandle(%d)\n", handle); | 
|  | 793 | mOut.writeInt32(BC_RELEASE); | 
|  | 794 | mOut.writeInt32(handle); | 
| Martijn Coenen | 0442a86 | 2017-11-17 10:46:32 +0100 | [diff] [blame] | 795 | flushIfNeeded(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 796 | } | 
|  | 797 |  | 
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 798 | void IPCThreadState::incWeakHandle(int32_t handle, BpBinder *proxy) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 799 | { | 
|  | 800 | LOG_REMOTEREFS("IPCThreadState::incWeakHandle(%d)\n", handle); | 
|  | 801 | mOut.writeInt32(BC_INCREFS); | 
|  | 802 | mOut.writeInt32(handle); | 
| Martijn Coenen | 0442a86 | 2017-11-17 10:46:32 +0100 | [diff] [blame] | 803 | if (!flushIfNeeded()) { | 
|  | 804 | // Create a temp reference until the driver has handled this command. | 
|  | 805 | proxy->getWeakRefs()->incWeak(mProcess.get()); | 
|  | 806 | mPostWriteWeakDerefs.push(proxy->getWeakRefs()); | 
|  | 807 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 808 | } | 
|  | 809 |  | 
|  | 810 | void IPCThreadState::decWeakHandle(int32_t handle) | 
|  | 811 | { | 
|  | 812 | LOG_REMOTEREFS("IPCThreadState::decWeakHandle(%d)\n", handle); | 
|  | 813 | mOut.writeInt32(BC_DECREFS); | 
|  | 814 | mOut.writeInt32(handle); | 
| Martijn Coenen | 0442a86 | 2017-11-17 10:46:32 +0100 | [diff] [blame] | 815 | flushIfNeeded(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 816 | } | 
|  | 817 |  | 
|  | 818 | status_t IPCThreadState::attemptIncStrongHandle(int32_t handle) | 
|  | 819 | { | 
| Arve HjønnevÄg | 11cfdcc | 2014-02-14 20:14:02 -0800 | [diff] [blame] | 820 | #if HAS_BC_ATTEMPT_ACQUIRE | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 821 | LOG_REMOTEREFS("IPCThreadState::attemptIncStrongHandle(%d)\n", handle); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 822 | mOut.writeInt32(BC_ATTEMPT_ACQUIRE); | 
|  | 823 | mOut.writeInt32(0); // xxx was thread priority | 
|  | 824 | mOut.writeInt32(handle); | 
|  | 825 | status_t result = UNKNOWN_ERROR; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 826 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 827 | waitForResponse(NULL, &result); | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 828 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 829 | #if LOG_REFCOUNTS | 
| liangweikang | a43ee15 | 2016-10-25 16:37:54 +0800 | [diff] [blame] | 830 | ALOGV("IPCThreadState::attemptIncStrongHandle(%ld) = %s\n", | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 831 | handle, result == NO_ERROR ? "SUCCESS" : "FAILURE"); | 
|  | 832 | #endif | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 833 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 834 | return result; | 
| Arve HjønnevÄg | 11cfdcc | 2014-02-14 20:14:02 -0800 | [diff] [blame] | 835 | #else | 
|  | 836 | (void)handle; | 
|  | 837 | ALOGE("%s(%d): Not supported\n", __func__, handle); | 
|  | 838 | return INVALID_OPERATION; | 
|  | 839 | #endif | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 840 | } | 
|  | 841 |  | 
|  | 842 | void IPCThreadState::expungeHandle(int32_t handle, IBinder* binder) | 
|  | 843 | { | 
|  | 844 | #if LOG_REFCOUNTS | 
| liangweikang | a43ee15 | 2016-10-25 16:37:54 +0800 | [diff] [blame] | 845 | ALOGV("IPCThreadState::expungeHandle(%ld)\n", handle); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 846 | #endif | 
| Manoj Gupta | 9cec85b | 2017-09-19 16:34:29 -0700 | [diff] [blame] | 847 | self()->mProcess->expungeHandle(handle, binder); // NOLINT | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 848 | } | 
|  | 849 |  | 
|  | 850 | status_t IPCThreadState::requestDeathNotification(int32_t handle, BpBinder* proxy) | 
|  | 851 | { | 
|  | 852 | mOut.writeInt32(BC_REQUEST_DEATH_NOTIFICATION); | 
|  | 853 | mOut.writeInt32((int32_t)handle); | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 854 | mOut.writePointer((uintptr_t)proxy); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 855 | return NO_ERROR; | 
|  | 856 | } | 
|  | 857 |  | 
|  | 858 | status_t IPCThreadState::clearDeathNotification(int32_t handle, BpBinder* proxy) | 
|  | 859 | { | 
|  | 860 | mOut.writeInt32(BC_CLEAR_DEATH_NOTIFICATION); | 
|  | 861 | mOut.writeInt32((int32_t)handle); | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 862 | mOut.writePointer((uintptr_t)proxy); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 863 | return NO_ERROR; | 
|  | 864 | } | 
|  | 865 |  | 
|  | 866 | IPCThreadState::IPCThreadState() | 
| Steven Moreland | 3562665 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 867 | : mProcess(ProcessState::self()), | 
|  | 868 | mServingStackPointer(nullptr), | 
|  | 869 | mServingStackPointerGuard(nullptr), | 
|  | 870 | mWorkSource(kUnsetWorkSource), | 
|  | 871 | mPropagateWorkSource(false), | 
|  | 872 | mIsLooper(false), | 
| Frankie Chang | f4c8137 | 2021-05-18 13:08:05 +0800 | [diff] [blame] | 873 | mIsFlushing(false), | 
| Steven Moreland | 3562665 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 874 | mStrictModePolicy(0), | 
|  | 875 | mLastTransactionBinderFlags(0), | 
|  | 876 | mCallRestriction(mProcess->mCallRestriction) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 877 | pthread_setspecific(gTLS, this); | 
| Dianne Hackborn | 8c6cedc | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 878 | clearCaller(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 879 | mIn.setDataCapacity(256); | 
|  | 880 | mOut.setDataCapacity(256); | 
|  | 881 | } | 
|  | 882 |  | 
|  | 883 | IPCThreadState::~IPCThreadState() | 
|  | 884 | { | 
|  | 885 | } | 
|  | 886 |  | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 887 | status_t IPCThreadState::sendReply(const Parcel& reply, uint32_t flags) | 
|  | 888 | { | 
|  | 889 | status_t err; | 
|  | 890 | status_t statusBuffer; | 
|  | 891 | err = writeTransactionData(BC_REPLY, flags, -1, 0, reply, &statusBuffer); | 
|  | 892 | if (err < NO_ERROR) return err; | 
|  | 893 |  | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 894 | return waitForResponse(nullptr, nullptr); | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 895 | } | 
|  | 896 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 897 | status_t IPCThreadState::waitForResponse(Parcel *reply, status_t *acquireResult) | 
|  | 898 | { | 
| Bernhard RosenkrƤnzer | 74debb0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 899 | uint32_t cmd; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 900 | int32_t err; | 
|  | 901 |  | 
|  | 902 | while (1) { | 
|  | 903 | if ((err=talkWithDriver()) < NO_ERROR) break; | 
|  | 904 | err = mIn.errorCheck(); | 
|  | 905 | if (err < NO_ERROR) break; | 
|  | 906 | if (mIn.dataAvail() == 0) continue; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 907 |  | 
| Bernhard RosenkrƤnzer | 74debb0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 908 | cmd = (uint32_t)mIn.readInt32(); | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 909 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 910 | IF_LOG_COMMANDS() { | 
|  | 911 | alog << "Processing waitForResponse Command: " | 
|  | 912 | << getReturnString(cmd) << endl; | 
|  | 913 | } | 
|  | 914 |  | 
|  | 915 | switch (cmd) { | 
| Hang Lu | b185ac0 | 2021-03-24 13:17:22 +0800 | [diff] [blame] | 916 | case BR_ONEWAY_SPAM_SUSPECT: | 
|  | 917 | ALOGE("Process seems to be sending too many oneway calls."); | 
|  | 918 | CallStack::logStack("oneway spamming", CallStack::getCurrent().get(), | 
|  | 919 | ANDROID_LOG_ERROR); | 
|  | 920 | [[fallthrough]]; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 921 | case BR_TRANSACTION_COMPLETE: | 
|  | 922 | if (!reply && !acquireResult) goto finish; | 
|  | 923 | break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 924 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 925 | case BR_DEAD_REPLY: | 
|  | 926 | err = DEAD_OBJECT; | 
|  | 927 | goto finish; | 
|  | 928 |  | 
|  | 929 | case BR_FAILED_REPLY: | 
|  | 930 | err = FAILED_TRANSACTION; | 
|  | 931 | goto finish; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 932 |  | 
| Marco Ballesio | 7ee1757 | 2020-09-08 10:30:03 -0700 | [diff] [blame] | 933 | case BR_FROZEN_REPLY: | 
|  | 934 | err = FAILED_TRANSACTION; | 
|  | 935 | goto finish; | 
|  | 936 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 937 | case BR_ACQUIRE_RESULT: | 
|  | 938 | { | 
| Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 939 | ALOG_ASSERT(acquireResult != NULL, "Unexpected brACQUIRE_RESULT"); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 940 | const int32_t result = mIn.readInt32(); | 
|  | 941 | if (!acquireResult) continue; | 
|  | 942 | *acquireResult = result ? NO_ERROR : INVALID_OPERATION; | 
|  | 943 | } | 
|  | 944 | goto finish; | 
| 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 | case BR_REPLY: | 
|  | 947 | { | 
|  | 948 | binder_transaction_data tr; | 
|  | 949 | err = mIn.read(&tr, sizeof(tr)); | 
| Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 950 | 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] | 951 | if (err != NO_ERROR) goto finish; | 
|  | 952 |  | 
|  | 953 | if (reply) { | 
|  | 954 | if ((tr.flags & TF_STATUS_CODE) == 0) { | 
|  | 955 | reply->ipcSetDataReference( | 
|  | 956 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), | 
|  | 957 | tr.data_size, | 
| Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 958 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), | 
|  | 959 | tr.offsets_size/sizeof(binder_size_t), | 
| Steven Moreland | 161fe12 | 2020-11-12 23:16:47 +0000 | [diff] [blame] | 960 | freeBuffer); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 961 | } else { | 
| Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 962 | err = *reinterpret_cast<const status_t*>(tr.data.ptr.buffer); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 963 | freeBuffer(nullptr, | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 964 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), | 
|  | 965 | tr.data_size, | 
| Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 966 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), | 
| Steven Moreland | 161fe12 | 2020-11-12 23:16:47 +0000 | [diff] [blame] | 967 | tr.offsets_size/sizeof(binder_size_t)); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 968 | } | 
|  | 969 | } else { | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 970 | freeBuffer(nullptr, | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 971 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), | 
|  | 972 | tr.data_size, | 
| Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 973 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), | 
| Steven Moreland | 161fe12 | 2020-11-12 23:16:47 +0000 | [diff] [blame] | 974 | tr.offsets_size/sizeof(binder_size_t)); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 975 | continue; | 
|  | 976 | } | 
|  | 977 | } | 
|  | 978 | goto finish; | 
|  | 979 |  | 
|  | 980 | default: | 
|  | 981 | err = executeCommand(cmd); | 
|  | 982 | if (err != NO_ERROR) goto finish; | 
|  | 983 | break; | 
|  | 984 | } | 
|  | 985 | } | 
|  | 986 |  | 
|  | 987 | finish: | 
|  | 988 | if (err != NO_ERROR) { | 
|  | 989 | if (acquireResult) *acquireResult = err; | 
|  | 990 | if (reply) reply->setError(err); | 
|  | 991 | mLastError = err; | 
|  | 992 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 993 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 994 | return err; | 
|  | 995 | } | 
|  | 996 |  | 
|  | 997 | status_t IPCThreadState::talkWithDriver(bool doReceive) | 
|  | 998 | { | 
| Alexandre Baião | c60c4fc | 2019-07-31 12:29:31 -0200 | [diff] [blame] | 999 | if (mProcess->mDriverFD < 0) { | 
| Johannes Carlsson | db1597a | 2011-02-17 14:06:53 +0100 | [diff] [blame] | 1000 | return -EBADF; | 
|  | 1001 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1002 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1003 | binder_write_read bwr; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1004 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1005 | // Is the read buffer empty? | 
|  | 1006 | const bool needRead = mIn.dataPosition() >= mIn.dataSize(); | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1007 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1008 | // We don't want to write anything if we are still reading | 
|  | 1009 | // from data left in the input buffer and the caller | 
|  | 1010 | // has requested to read the next data. | 
|  | 1011 | const size_t outAvail = (!doReceive || needRead) ? mOut.dataSize() : 0; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1012 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1013 | bwr.write_size = outAvail; | 
| Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1014 | bwr.write_buffer = (uintptr_t)mOut.data(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1015 |  | 
|  | 1016 | // This is what we'll read. | 
|  | 1017 | if (doReceive && needRead) { | 
|  | 1018 | bwr.read_size = mIn.dataCapacity(); | 
| Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1019 | bwr.read_buffer = (uintptr_t)mIn.data(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1020 | } else { | 
|  | 1021 | bwr.read_size = 0; | 
| Ben Cheng | d640f89 | 2011-12-01 17:11:32 -0800 | [diff] [blame] | 1022 | bwr.read_buffer = 0; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1023 | } | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 1024 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1025 | IF_LOG_COMMANDS() { | 
|  | 1026 | TextOutput::Bundle _b(alog); | 
|  | 1027 | if (outAvail != 0) { | 
|  | 1028 | alog << "Sending commands to driver: " << indent; | 
|  | 1029 | const void* cmds = (const void*)bwr.write_buffer; | 
|  | 1030 | const void* end = ((const uint8_t*)cmds)+bwr.write_size; | 
|  | 1031 | alog << HexDump(cmds, bwr.write_size) << endl; | 
|  | 1032 | while (cmds < end) cmds = printCommand(alog, cmds); | 
|  | 1033 | alog << dedent; | 
|  | 1034 | } | 
|  | 1035 | alog << "Size of receive buffer: " << bwr.read_size | 
|  | 1036 | << ", needRead: " << needRead << ", doReceive: " << doReceive << endl; | 
|  | 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 immediately if there is nothing to do. | 
|  | 1040 | if ((bwr.write_size == 0) && (bwr.read_size == 0)) return NO_ERROR; | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 1041 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1042 | bwr.write_consumed = 0; | 
|  | 1043 | bwr.read_consumed = 0; | 
|  | 1044 | status_t err; | 
|  | 1045 | do { | 
|  | 1046 | IF_LOG_COMMANDS() { | 
|  | 1047 | alog << "About to read/write, write size = " << mOut.dataSize() << endl; | 
|  | 1048 | } | 
| Elliott Hughes | 6071da7 | 2015-08-12 15:27:47 -0700 | [diff] [blame] | 1049 | #if defined(__ANDROID__) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1050 | if (ioctl(mProcess->mDriverFD, BINDER_WRITE_READ, &bwr) >= 0) | 
|  | 1051 | err = NO_ERROR; | 
|  | 1052 | else | 
|  | 1053 | err = -errno; | 
|  | 1054 | #else | 
|  | 1055 | err = INVALID_OPERATION; | 
|  | 1056 | #endif | 
| Alexandre Baião | c60c4fc | 2019-07-31 12:29:31 -0200 | [diff] [blame] | 1057 | if (mProcess->mDriverFD < 0) { | 
| Johannes Carlsson | db1597a | 2011-02-17 14:06:53 +0100 | [diff] [blame] | 1058 | err = -EBADF; | 
|  | 1059 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1060 | IF_LOG_COMMANDS() { | 
|  | 1061 | alog << "Finished read/write, write size = " << mOut.dataSize() << endl; | 
|  | 1062 | } | 
|  | 1063 | } while (err == -EINTR); | 
| Andy McFadden | aefc9cd | 2011-08-31 07:43:40 -0700 | [diff] [blame] | 1064 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1065 | IF_LOG_COMMANDS() { | 
| Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 1066 | alog << "Our err: " << (void*)(intptr_t)err << ", write consumed: " | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1067 | << bwr.write_consumed << " (of " << mOut.dataSize() | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 1068 | << "), read consumed: " << bwr.read_consumed << endl; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1069 | } | 
|  | 1070 |  | 
|  | 1071 | if (err >= NO_ERROR) { | 
|  | 1072 | if (bwr.write_consumed > 0) { | 
| Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1073 | if (bwr.write_consumed < mOut.dataSize()) | 
| Steven Moreland | b077deb | 2020-04-16 16:22:52 -0700 | [diff] [blame] | 1074 | LOG_ALWAYS_FATAL("Driver did not consume write buffer. " | 
|  | 1075 | "err: %s consumed: %zu of %zu", | 
|  | 1076 | statusToString(err).c_str(), | 
|  | 1077 | (size_t)bwr.write_consumed, | 
|  | 1078 | mOut.dataSize()); | 
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 1079 | else { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1080 | mOut.setDataSize(0); | 
| Martijn Coenen | 7c170bb | 2018-05-04 17:28:55 -0700 | [diff] [blame] | 1081 | processPostWriteDerefs(); | 
|  | 1082 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1083 | } | 
|  | 1084 | if (bwr.read_consumed > 0) { | 
|  | 1085 | mIn.setDataSize(bwr.read_consumed); | 
|  | 1086 | mIn.setDataPosition(0); | 
|  | 1087 | } | 
|  | 1088 | IF_LOG_COMMANDS() { | 
|  | 1089 | TextOutput::Bundle _b(alog); | 
|  | 1090 | alog << "Remaining data size: " << mOut.dataSize() << endl; | 
|  | 1091 | alog << "Received commands from driver: " << indent; | 
|  | 1092 | const void* cmds = mIn.data(); | 
|  | 1093 | const void* end = mIn.data() + mIn.dataSize(); | 
|  | 1094 | alog << HexDump(cmds, mIn.dataSize()) << endl; | 
|  | 1095 | while (cmds < end) cmds = printReturnCommand(alog, cmds); | 
|  | 1096 | alog << dedent; | 
|  | 1097 | } | 
|  | 1098 | return NO_ERROR; | 
|  | 1099 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1100 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1101 | return err; | 
|  | 1102 | } | 
|  | 1103 |  | 
|  | 1104 | status_t IPCThreadState::writeTransactionData(int32_t cmd, uint32_t binderFlags, | 
|  | 1105 | int32_t handle, uint32_t code, const Parcel& data, status_t* statusBuffer) | 
|  | 1106 | { | 
|  | 1107 | binder_transaction_data tr; | 
|  | 1108 |  | 
| Arve HjønnevÄg | 07fd0f1 | 2014-02-18 21:10:29 -0800 | [diff] [blame] | 1109 | 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] | 1110 | tr.target.handle = handle; | 
|  | 1111 | tr.code = code; | 
|  | 1112 | tr.flags = binderFlags; | 
| Evgeniy Stepanov | d547432 | 2011-04-21 14:15:00 +0400 | [diff] [blame] | 1113 | tr.cookie = 0; | 
|  | 1114 | tr.sender_pid = 0; | 
|  | 1115 | tr.sender_euid = 0; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1116 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1117 | const status_t err = data.errorCheck(); | 
|  | 1118 | if (err == NO_ERROR) { | 
|  | 1119 | tr.data_size = data.ipcDataSize(); | 
|  | 1120 | tr.data.ptr.buffer = data.ipcData(); | 
| Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1121 | tr.offsets_size = data.ipcObjectsCount()*sizeof(binder_size_t); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1122 | tr.data.ptr.offsets = data.ipcObjects(); | 
|  | 1123 | } else if (statusBuffer) { | 
|  | 1124 | tr.flags |= TF_STATUS_CODE; | 
|  | 1125 | *statusBuffer = err; | 
|  | 1126 | tr.data_size = sizeof(status_t); | 
| Arve HjønnevÄg | 87b30d0 | 2014-02-18 21:04:31 -0800 | [diff] [blame] | 1127 | tr.data.ptr.buffer = reinterpret_cast<uintptr_t>(statusBuffer); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1128 | tr.offsets_size = 0; | 
| Arve HjønnevÄg | 84e625a | 2014-01-28 20:12:59 -0800 | [diff] [blame] | 1129 | tr.data.ptr.offsets = 0; | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1130 | } else { | 
|  | 1131 | return (mLastError = err); | 
|  | 1132 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1133 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1134 | mOut.writeInt32(cmd); | 
|  | 1135 | mOut.write(&tr, sizeof(tr)); | 
| 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 | return NO_ERROR; | 
|  | 1138 | } | 
|  | 1139 |  | 
|  | 1140 | sp<BBinder> the_context_object; | 
|  | 1141 |  | 
| Jiyong Park | 384328e | 2020-11-13 17:16:48 +0900 | [diff] [blame] | 1142 | void IPCThreadState::setTheContextObject(const sp<BBinder>& obj) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1143 | { | 
|  | 1144 | the_context_object = obj; | 
|  | 1145 | } | 
|  | 1146 |  | 
|  | 1147 | status_t IPCThreadState::executeCommand(int32_t cmd) | 
|  | 1148 | { | 
|  | 1149 | BBinder* obj; | 
|  | 1150 | RefBase::weakref_type* refs; | 
|  | 1151 | status_t result = NO_ERROR; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1152 |  | 
| Bernhard RosenkrƤnzer | 74debb0 | 2014-11-25 21:55:33 +0100 | [diff] [blame] | 1153 | switch ((uint32_t)cmd) { | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1154 | case BR_ERROR: | 
|  | 1155 | result = mIn.readInt32(); | 
|  | 1156 | break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1157 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1158 | case BR_OK: | 
|  | 1159 | break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1160 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1161 | case BR_ACQUIRE: | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1162 | refs = (RefBase::weakref_type*)mIn.readPointer(); | 
|  | 1163 | obj = (BBinder*)mIn.readPointer(); | 
| Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1164 | ALOG_ASSERT(refs->refBase() == obj, | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1165 | "BR_ACQUIRE: object %p does not match cookie %p (expected %p)", | 
|  | 1166 | refs, obj, refs->refBase()); | 
|  | 1167 | obj->incStrong(mProcess.get()); | 
|  | 1168 | IF_LOG_REMOTEREFS() { | 
|  | 1169 | LOG_REMOTEREFS("BR_ACQUIRE from driver on %p", obj); | 
|  | 1170 | obj->printRefs(); | 
|  | 1171 | } | 
|  | 1172 | mOut.writeInt32(BC_ACQUIRE_DONE); | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1173 | mOut.writePointer((uintptr_t)refs); | 
|  | 1174 | mOut.writePointer((uintptr_t)obj); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1175 | break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1176 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1177 | case BR_RELEASE: | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1178 | refs = (RefBase::weakref_type*)mIn.readPointer(); | 
|  | 1179 | obj = (BBinder*)mIn.readPointer(); | 
| Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1180 | ALOG_ASSERT(refs->refBase() == obj, | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1181 | "BR_RELEASE: object %p does not match cookie %p (expected %p)", | 
|  | 1182 | refs, obj, refs->refBase()); | 
|  | 1183 | IF_LOG_REMOTEREFS() { | 
|  | 1184 | LOG_REMOTEREFS("BR_RELEASE from driver on %p", obj); | 
|  | 1185 | obj->printRefs(); | 
|  | 1186 | } | 
|  | 1187 | mPendingStrongDerefs.push(obj); | 
|  | 1188 | break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1189 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1190 | case BR_INCREFS: | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1191 | refs = (RefBase::weakref_type*)mIn.readPointer(); | 
|  | 1192 | obj = (BBinder*)mIn.readPointer(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1193 | refs->incWeak(mProcess.get()); | 
|  | 1194 | mOut.writeInt32(BC_INCREFS_DONE); | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1195 | mOut.writePointer((uintptr_t)refs); | 
|  | 1196 | mOut.writePointer((uintptr_t)obj); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1197 | break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1198 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1199 | case BR_DECREFS: | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1200 | refs = (RefBase::weakref_type*)mIn.readPointer(); | 
| Jiyong Park | 5970d0a | 2022-03-08 16:56:13 +0900 | [diff] [blame] | 1201 | // NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores) | 
|  | 1202 | obj = (BBinder*)mIn.readPointer(); // consume | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1203 | // NOTE: This assertion is not valid, because the object may no | 
|  | 1204 | // longer exist (thus the (BBinder*)cast above resulting in a different | 
|  | 1205 | // memory address). | 
| Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1206 | //ALOG_ASSERT(refs->refBase() == obj, | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1207 | //           "BR_DECREFS: object %p does not match cookie %p (expected %p)", | 
|  | 1208 | //           refs, obj, refs->refBase()); | 
|  | 1209 | mPendingWeakDerefs.push(refs); | 
|  | 1210 | break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1211 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1212 | case BR_ATTEMPT_ACQUIRE: | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1213 | refs = (RefBase::weakref_type*)mIn.readPointer(); | 
|  | 1214 | obj = (BBinder*)mIn.readPointer(); | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1215 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1216 | { | 
|  | 1217 | const bool success = refs->attemptIncStrong(mProcess.get()); | 
| Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1218 | ALOG_ASSERT(success && refs->refBase() == obj, | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1219 | "BR_ATTEMPT_ACQUIRE: object %p does not match cookie %p (expected %p)", | 
|  | 1220 | refs, obj, refs->refBase()); | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1221 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1222 | mOut.writeInt32(BC_ACQUIRE_RESULT); | 
|  | 1223 | mOut.writeInt32((int32_t)success); | 
|  | 1224 | } | 
|  | 1225 | break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1226 |  | 
| Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 1227 | case BR_TRANSACTION_SEC_CTX: | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1228 | case BR_TRANSACTION: | 
|  | 1229 | { | 
| Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 1230 | binder_transaction_data_secctx tr_secctx; | 
|  | 1231 | binder_transaction_data& tr = tr_secctx.transaction_data; | 
|  | 1232 |  | 
|  | 1233 | if (cmd == (int) BR_TRANSACTION_SEC_CTX) { | 
|  | 1234 | result = mIn.read(&tr_secctx, sizeof(tr_secctx)); | 
|  | 1235 | } else { | 
|  | 1236 | result = mIn.read(&tr, sizeof(tr)); | 
|  | 1237 | tr_secctx.secctx = 0; | 
|  | 1238 | } | 
|  | 1239 |  | 
| Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1240 | ALOG_ASSERT(result == NO_ERROR, | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1241 | "Not enough command data for brTRANSACTION"); | 
|  | 1242 | if (result != NO_ERROR) break; | 
| 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 | Parcel buffer; | 
|  | 1245 | buffer.ipcSetDataReference( | 
|  | 1246 | reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer), | 
|  | 1247 | tr.data_size, | 
|  | 1248 | reinterpret_cast<const binder_size_t*>(tr.data.ptr.offsets), | 
| Steven Moreland | 161fe12 | 2020-11-12 23:16:47 +0000 | [diff] [blame] | 1249 | tr.offsets_size/sizeof(binder_size_t), freeBuffer); | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1250 |  | 
| Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 1251 | const void* origServingStackPointer = mServingStackPointer; | 
| Steven Moreland | 3562665 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 1252 | mServingStackPointer = __builtin_frame_address(0); | 
| Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 1253 |  | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1254 | const pid_t origPid = mCallingPid; | 
| Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 1255 | const char* origSid = mCallingSid; | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1256 | const uid_t origUid = mCallingUid; | 
|  | 1257 | const int32_t origStrictModePolicy = mStrictModePolicy; | 
|  | 1258 | const int32_t origTransactionBinderFlags = mLastTransactionBinderFlags; | 
| Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame] | 1259 | const int32_t origWorkSource = mWorkSource; | 
|  | 1260 | const bool origPropagateWorkSet = mPropagateWorkSource; | 
|  | 1261 | // Calling work source will be set by Parcel#enforceInterface. Parcel#enforceInterface | 
|  | 1262 | // is only guaranteed to be called for AIDL-generated stubs so we reset the work source | 
|  | 1263 | // here to never propagate it. | 
|  | 1264 | clearCallingWorkSource(); | 
|  | 1265 | clearPropagateWorkSource(); | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1266 |  | 
|  | 1267 | mCallingPid = tr.sender_pid; | 
| Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 1268 | mCallingSid = reinterpret_cast<const char*>(tr_secctx.secctx); | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1269 | mCallingUid = tr.sender_euid; | 
|  | 1270 | mLastTransactionBinderFlags = tr.flags; | 
|  | 1271 |  | 
| Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 1272 | // ALOGI(">>>> TRANSACT from pid %d sid %s uid %d\n", mCallingPid, | 
|  | 1273 | //    (mCallingSid ? mCallingSid : "<N/A>"), mCallingUid); | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1274 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1275 | Parcel reply; | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1276 | status_t error; | 
|  | 1277 | IF_LOG_TRANSACTIONS() { | 
|  | 1278 | TextOutput::Bundle _b(alog); | 
|  | 1279 | alog << "BR_TRANSACTION thr " << (void*)pthread_self() | 
|  | 1280 | << " / obj " << tr.target.ptr << " / code " | 
|  | 1281 | << TypeCode(tr.code) << ": " << indent << buffer | 
|  | 1282 | << dedent << endl | 
|  | 1283 | << "Data addr = " | 
|  | 1284 | << reinterpret_cast<const uint8_t*>(tr.data.ptr.buffer) | 
|  | 1285 | << ", offsets addr=" | 
|  | 1286 | << reinterpret_cast<const size_t*>(tr.data.ptr.offsets) << endl; | 
|  | 1287 | } | 
|  | 1288 | if (tr.target.ptr) { | 
|  | 1289 | // We only have a weak reference on the target object, so we must first try to | 
|  | 1290 | // safely acquire a strong reference before doing anything else with it. | 
|  | 1291 | if (reinterpret_cast<RefBase::weakref_type*>( | 
|  | 1292 | tr.target.ptr)->attemptIncStrong(this)) { | 
|  | 1293 | error = reinterpret_cast<BBinder*>(tr.cookie)->transact(tr.code, buffer, | 
|  | 1294 | &reply, tr.flags); | 
|  | 1295 | reinterpret_cast<BBinder*>(tr.cookie)->decStrong(this); | 
| Dianne Hackborn | c111461 | 2016-03-21 10:36:54 -0700 | [diff] [blame] | 1296 | } else { | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1297 | error = UNKNOWN_TRANSACTION; | 
| Dianne Hackborn | c111461 | 2016-03-21 10:36:54 -0700 | [diff] [blame] | 1298 | } | 
| Brad Fitzpatrick | 5273603 | 2010-08-30 16:01:16 -0700 | [diff] [blame] | 1299 |  | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1300 | } else { | 
|  | 1301 | 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] | 1302 | } | 
| Dianne Hackborn | 5ee2c9d | 2014-09-30 11:30:03 -0700 | [diff] [blame] | 1303 |  | 
| Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 1304 | //ALOGI("<<<< TRANSACT from pid %d restore pid %d sid %s uid %d\n", | 
|  | 1305 | //     mCallingPid, origPid, (origSid ? origSid : "<N/A>"), origUid); | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1306 |  | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1307 | if ((tr.flags & TF_ONE_WAY) == 0) { | 
|  | 1308 | LOG_ONEWAY("Sending reply to %d!", mCallingPid); | 
|  | 1309 | if (error < NO_ERROR) reply.setError(error); | 
| Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 1310 |  | 
|  | 1311 | constexpr uint32_t kForwardReplyFlags = TF_CLEAR_BUF; | 
|  | 1312 | sendReply(reply, (tr.flags & kForwardReplyFlags)); | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1313 | } else { | 
| Steven Moreland | 80844f7 | 2020-12-12 02:06:08 +0000 | [diff] [blame] | 1314 | if (error != OK) { | 
|  | 1315 | alog << "oneway function results for code " << tr.code | 
|  | 1316 | << " on binder at " | 
|  | 1317 | << reinterpret_cast<void*>(tr.target.ptr) | 
|  | 1318 | << " will be dropped but finished with status " | 
|  | 1319 | << statusToString(error); | 
|  | 1320 |  | 
|  | 1321 | // ideally we could log this even when error == OK, but it | 
|  | 1322 | // causes too much logspam because some manually-written | 
|  | 1323 | // interfaces have clients that call methods which always | 
|  | 1324 | // write results, sometimes as oneway methods. | 
|  | 1325 | if (reply.dataSize() != 0) { | 
|  | 1326 | alog << " and reply parcel size " << reply.dataSize(); | 
|  | 1327 | } | 
|  | 1328 |  | 
|  | 1329 | alog << endl; | 
| Steven Moreland | ce66b8a | 2020-02-10 14:43:14 -0800 | [diff] [blame] | 1330 | } | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1331 | LOG_ONEWAY("NOT sending reply to %d!", mCallingPid); | 
|  | 1332 | } | 
|  | 1333 |  | 
| Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 1334 | mServingStackPointer = origServingStackPointer; | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1335 | mCallingPid = origPid; | 
| Steven Moreland | f021200 | 2018-12-26 13:59:23 -0800 | [diff] [blame] | 1336 | mCallingSid = origSid; | 
| Martijn Coenen | ea0090a | 2017-11-02 18:54:40 +0000 | [diff] [blame] | 1337 | mCallingUid = origUid; | 
|  | 1338 | mStrictModePolicy = origStrictModePolicy; | 
|  | 1339 | mLastTransactionBinderFlags = origTransactionBinderFlags; | 
| Olivier Gaillard | 91a0480 | 2018-11-14 17:32:41 +0000 | [diff] [blame] | 1340 | mWorkSource = origWorkSource; | 
|  | 1341 | mPropagateWorkSource = origPropagateWorkSet; | 
| Christopher Tate | 440fd87 | 2010-03-18 17:55:03 -0700 | [diff] [blame] | 1342 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1343 | IF_LOG_TRANSACTIONS() { | 
|  | 1344 | TextOutput::Bundle _b(alog); | 
|  | 1345 | alog << "BC_REPLY thr " << (void*)pthread_self() << " / obj " | 
|  | 1346 | << tr.target.ptr << ": " << indent << reply << dedent << endl; | 
|  | 1347 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1348 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1349 | } | 
|  | 1350 | break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1351 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1352 | case BR_DEAD_BINDER: | 
|  | 1353 | { | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1354 | BpBinder *proxy = (BpBinder*)mIn.readPointer(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1355 | proxy->sendObituary(); | 
|  | 1356 | mOut.writeInt32(BC_DEAD_BINDER_DONE); | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1357 | mOut.writePointer((uintptr_t)proxy); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1358 | } break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1359 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1360 | case BR_CLEAR_DEATH_NOTIFICATION_DONE: | 
|  | 1361 | { | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1362 | BpBinder *proxy = (BpBinder*)mIn.readPointer(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1363 | proxy->getWeakRefs()->decWeak(proxy); | 
|  | 1364 | } break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1365 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1366 | case BR_FINISHED: | 
|  | 1367 | result = TIMED_OUT; | 
|  | 1368 | break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1369 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1370 | case BR_NOOP: | 
|  | 1371 | break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1372 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1373 | case BR_SPAWN_LOOPER: | 
|  | 1374 | mProcess->spawnPooledThread(false); | 
|  | 1375 | break; | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1376 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1377 | default: | 
| liangweikang | a43ee15 | 2016-10-25 16:37:54 +0800 | [diff] [blame] | 1378 | 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] | 1379 | result = UNKNOWN_ERROR; | 
|  | 1380 | break; | 
|  | 1381 | } | 
|  | 1382 |  | 
|  | 1383 | if (result != NO_ERROR) { | 
|  | 1384 | mLastError = result; | 
|  | 1385 | } | 
| Tim Murray | d429f4a | 2017-03-07 09:31:09 -0800 | [diff] [blame] | 1386 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1387 | return result; | 
|  | 1388 | } | 
|  | 1389 |  | 
| Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 1390 | const void* IPCThreadState::getServingStackPointer() const { | 
|  | 1391 | return mServingStackPointer; | 
| Jayant Chowdhary | dac6dc8 | 2018-10-01 22:52:44 +0000 | [diff] [blame] | 1392 | } | 
|  | 1393 |  | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1394 | void IPCThreadState::threadDestructor(void *st) | 
|  | 1395 | { | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 1396 | IPCThreadState* const self = static_cast<IPCThreadState*>(st); | 
|  | 1397 | if (self) { | 
|  | 1398 | self->flushCommands(); | 
| Elliott Hughes | 6071da7 | 2015-08-12 15:27:47 -0700 | [diff] [blame] | 1399 | #if defined(__ANDROID__) | 
| Alexandre Baião | c60c4fc | 2019-07-31 12:29:31 -0200 | [diff] [blame] | 1400 | if (self->mProcess->mDriverFD >= 0) { | 
| Johannes Carlsson | db1597a | 2011-02-17 14:06:53 +0100 | [diff] [blame] | 1401 | ioctl(self->mProcess->mDriverFD, BINDER_THREAD_EXIT, 0); | 
|  | 1402 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1403 | #endif | 
| Todd Poynor | 8d96cab | 2013-06-25 19:12:18 -0700 | [diff] [blame] | 1404 | delete self; | 
|  | 1405 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1406 | } | 
|  | 1407 |  | 
| Li Li | 6f05929 | 2021-09-10 09:59:30 -0700 | [diff] [blame] | 1408 | status_t IPCThreadState::getProcessFreezeInfo(pid_t pid, uint32_t *sync_received, | 
|  | 1409 | uint32_t *async_received) | 
|  | 1410 | { | 
|  | 1411 | int ret = 0; | 
| Jiyong Park | 5970d0a | 2022-03-08 16:56:13 +0900 | [diff] [blame] | 1412 | binder_frozen_status_info info = {}; | 
| Li Li | 6f05929 | 2021-09-10 09:59:30 -0700 | [diff] [blame] | 1413 | info.pid = pid; | 
|  | 1414 |  | 
|  | 1415 | #if defined(__ANDROID__) | 
|  | 1416 | if (ioctl(self()->mProcess->mDriverFD, BINDER_GET_FROZEN_INFO, &info) < 0) | 
|  | 1417 | ret = -errno; | 
|  | 1418 | #endif | 
|  | 1419 | *sync_received = info.sync_recv; | 
|  | 1420 | *async_received = info.async_recv; | 
|  | 1421 |  | 
|  | 1422 | return ret; | 
|  | 1423 | } | 
| Li Li | 6f05929 | 2021-09-10 09:59:30 -0700 | [diff] [blame] | 1424 |  | 
| Marco Ballesio | 7ee1757 | 2020-09-08 10:30:03 -0700 | [diff] [blame] | 1425 | status_t IPCThreadState::freeze(pid_t pid, bool enable, uint32_t timeout_ms) { | 
|  | 1426 | struct binder_freeze_info info; | 
|  | 1427 | int ret = 0; | 
|  | 1428 |  | 
|  | 1429 | info.pid = pid; | 
|  | 1430 | info.enable = enable; | 
|  | 1431 | info.timeout_ms = timeout_ms; | 
|  | 1432 |  | 
|  | 1433 |  | 
|  | 1434 | #if defined(__ANDROID__) | 
|  | 1435 | if (ioctl(self()->mProcess->mDriverFD, BINDER_FREEZE, &info) < 0) | 
|  | 1436 | ret = -errno; | 
|  | 1437 | #endif | 
|  | 1438 |  | 
|  | 1439 | // | 
|  | 1440 | // ret==-EAGAIN indicates that transactions have not drained. | 
|  | 1441 | // Call again to poll for completion. | 
|  | 1442 | // | 
|  | 1443 | return ret; | 
|  | 1444 | } | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1445 |  | 
| Colin Cross | 6f4f3ab | 2014-02-05 17:42:44 -0800 | [diff] [blame] | 1446 | void IPCThreadState::freeBuffer(Parcel* parcel, const uint8_t* data, | 
|  | 1447 | size_t /*dataSize*/, | 
|  | 1448 | const binder_size_t* /*objects*/, | 
| Steven Moreland | 161fe12 | 2020-11-12 23:16:47 +0000 | [diff] [blame] | 1449 | size_t /*objectsSize*/) | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1450 | { | 
| Steve Block | a19954a | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1451 | //ALOGI("Freeing parcel %p", &parcel); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1452 | IF_LOG_COMMANDS() { | 
|  | 1453 | alog << "Writing BC_FREE_BUFFER for " << data << endl; | 
|  | 1454 | } | 
| Steve Block | 6726347 | 2012-01-09 18:35:44 +0000 | [diff] [blame] | 1455 | ALOG_ASSERT(data != NULL, "Called with NULL data"); | 
| Yi Kong | fdd8da9 | 2018-06-07 17:52:27 -0700 | [diff] [blame] | 1456 | if (parcel != nullptr) parcel->closeFileDescriptors(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1457 | IPCThreadState* state = self(); | 
|  | 1458 | state->mOut.writeInt32(BC_FREE_BUFFER); | 
| Serban Constantinescu | f683e01 | 2013-11-05 16:53:55 +0000 | [diff] [blame] | 1459 | state->mOut.writePointer((uintptr_t)data); | 
| Martijn Coenen | 0442a86 | 2017-11-17 10:46:32 +0100 | [diff] [blame] | 1460 | state->flushIfNeeded(); | 
| The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1461 | } | 
|  | 1462 |  | 
| Steven Moreland | 61ff849 | 2019-09-26 16:05:45 -0700 | [diff] [blame] | 1463 | } // namespace android |