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