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