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