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