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