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