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