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