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