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