Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
| 17 | #include <android/binder_ibinder.h> |
Steven Moreland | 2f405f5 | 2020-07-08 22:24:29 +0000 | [diff] [blame] | 18 | #include <android/binder_ibinder_platform.h> |
Steven Moreland | 46b5fea | 2019-10-15 11:22:18 -0700 | [diff] [blame] | 19 | #include <android/binder_stability.h> |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 20 | #include <android/binder_status.h> |
Steven Moreland | f3034b0 | 2018-11-12 17:37:46 -0800 | [diff] [blame] | 21 | #include <binder/IPCThreadState.h> |
Ruchir Rastogi | cc7a746 | 2020-01-31 14:29:15 -0800 | [diff] [blame] | 22 | #include <binder/IResultReceiver.h> |
Tomasz Wasilczyk | b8ebcca | 2023-10-09 19:54:20 +0000 | [diff] [blame] | 23 | #if __has_include(<private/android_filesystem_config.h>) |
Ruchir Rastogi | cc7a746 | 2020-01-31 14:29:15 -0800 | [diff] [blame] | 24 | #include <private/android_filesystem_config.h> |
Tomasz Wasilczyk | b8ebcca | 2023-10-09 19:54:20 +0000 | [diff] [blame] | 25 | #endif |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 26 | |
Andrei Homescu | 38eafb7 | 2024-03-22 22:38:08 +0000 | [diff] [blame] | 27 | #include "../BuildFlags.h" |
Steven Moreland | 39fdec9 | 2022-08-27 00:27:37 +0000 | [diff] [blame] | 28 | #include "ibinder_internal.h" |
| 29 | #include "parcel_internal.h" |
| 30 | #include "status_internal.h" |
| 31 | |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 32 | using DeathRecipient = ::android::IBinder::DeathRecipient; |
| 33 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 34 | using ::android::IBinder; |
Ruchir Rastogi | cc7a746 | 2020-01-31 14:29:15 -0800 | [diff] [blame] | 35 | using ::android::IResultReceiver; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 36 | using ::android::Parcel; |
| 37 | using ::android::sp; |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 38 | using ::android::status_t; |
Steven Moreland | 009fc1a | 2022-06-10 17:24:25 +0000 | [diff] [blame] | 39 | using ::android::statusToString; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 40 | using ::android::String16; |
Steven Moreland | a194c45 | 2019-03-04 16:47:07 -0800 | [diff] [blame] | 41 | using ::android::String8; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 42 | using ::android::wp; |
| 43 | |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 44 | namespace ABBinderTag { |
| 45 | |
| 46 | static const void* kId = "ABBinder"; |
| 47 | static void* kValue = static_cast<void*>(new bool{true}); |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 48 | void clean(const void* /*id*/, void* /*obj*/, void* /*cookie*/){/* do nothing */}; |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 49 | |
| 50 | static void attach(const sp<IBinder>& binder) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 51 | auto alreadyAttached = binder->attachObject(kId, kValue, nullptr /*cookie*/, clean); |
| 52 | LOG_ALWAYS_FATAL_IF(alreadyAttached != nullptr, "can only attach once"); |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 53 | } |
| 54 | static bool has(const sp<IBinder>& binder) { |
| 55 | return binder != nullptr && binder->findObject(kId) == kValue; |
| 56 | } |
| 57 | |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 58 | } // namespace ABBinderTag |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 59 | |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 60 | namespace ABpBinderTag { |
| 61 | |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 62 | static const void* kId = "ABpBinder"; |
| 63 | struct Value { |
| 64 | wp<ABpBinder> binder; |
| 65 | }; |
| 66 | void clean(const void* id, void* obj, void* cookie) { |
Steven Moreland | 3acd490 | 2022-07-26 18:29:58 +0000 | [diff] [blame] | 67 | // be weary of leaks! |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 68 | // ALOGI("Deleting an ABpBinder"); |
Steven Moreland | 3acd490 | 2022-07-26 18:29:58 +0000 | [diff] [blame] | 69 | |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 70 | LOG_ALWAYS_FATAL_IF(id != kId, "%p %p %p", id, obj, cookie); |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 71 | |
| 72 | delete static_cast<Value*>(obj); |
| 73 | }; |
| 74 | |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 75 | } // namespace ABpBinderTag |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 76 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 77 | AIBinder::AIBinder(const AIBinder_Class* clazz) : mClazz(clazz) {} |
| 78 | AIBinder::~AIBinder() {} |
| 79 | |
Steven Moreland | 94a7679 | 2022-12-21 01:20:06 +0000 | [diff] [blame] | 80 | // b/175635923 libcxx causes "implicit-conversion" with a string with invalid char |
| 81 | static std::string SanitizeString(const String16& str) { |
| 82 | std::string sanitized{String8(str)}; |
| 83 | for (auto& c : sanitized) { |
| 84 | if (!isprint(c)) { |
| 85 | c = '?'; |
| 86 | } |
| 87 | } |
| 88 | return sanitized; |
| 89 | } |
| 90 | |
| 91 | bool AIBinder::associateClass(const AIBinder_Class* clazz) { |
| 92 | if (clazz == nullptr) return false; |
| 93 | |
| 94 | // If mClazz is non-null, this must have been called and cached |
| 95 | // already. So, we can safely call this first. Due to the implementation |
| 96 | // of getInterfaceDescriptor (at time of writing), two simultaneous calls |
| 97 | // may lead to extra binder transactions, but this is expected to be |
| 98 | // exceedingly rare. Once we have a binder, when we get it again later, |
| 99 | // we won't make another binder transaction here. |
| 100 | const String16& descriptor = getBinder()->getInterfaceDescriptor(); |
| 101 | const String16& newDescriptor = clazz->getInterfaceDescriptor(); |
| 102 | |
Andrei Homescu | 9556bb1 | 2020-09-21 16:59:45 -0700 | [diff] [blame] | 103 | std::lock_guard<std::mutex> lock(mClazzMutex); |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 104 | if (mClazz == clazz) return true; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 105 | |
Steven Moreland | faf25a4 | 2022-12-21 02:21:36 +0000 | [diff] [blame] | 106 | // If this is an ABpBinder, the first class object becomes the canonical one. The implication |
| 107 | // of this is that no API can require a proxy information to get information on how to behave. |
| 108 | // from the class itself - which should only store the interface descriptor. The functionality |
| 109 | // should be implemented by adding AIBinder_* APIs to set values on binders themselves, by |
| 110 | // setting things on AIBinder_Class which get transferred along with the binder, so that they |
| 111 | // can be read along with the BpBinder, or by modifying APIs directly (e.g. an option in |
| 112 | // onTransact). |
| 113 | // |
| 114 | // While this check is required to support linkernamespaces, one downside of it is that |
| 115 | // you may parcel code to communicate between things in the same process. However, comms |
| 116 | // between linkernamespaces like this already happen for cross-language calls like Java<->C++ |
| 117 | // or Rust<->Java, and there are good stability guarantees here. This interacts with |
| 118 | // binder Stability checks exactly like any other in-process call. The stability is known |
| 119 | // to the IBinder object, so that it doesn't matter if a class object comes from |
| 120 | // a different stability level. |
| 121 | if (mClazz != nullptr && !asABpBinder()) { |
Steven Moreland | 63872b5 | 2020-10-02 19:28:23 +0000 | [diff] [blame] | 122 | const String16& currentDescriptor = mClazz->getInterfaceDescriptor(); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 123 | if (newDescriptor == currentDescriptor) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 124 | ALOGE("Class descriptors '%s' match during associateClass, but they are different class" |
| 125 | " objects (%p vs %p). Class descriptor collision?", |
| 126 | String8(currentDescriptor).c_str(), clazz, mClazz); |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 127 | } else { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 128 | ALOGE("%s: Class cannot be associated on object which already has a class. " |
| 129 | "Trying to associate to '%s' but already set to '%s'.", |
| 130 | __func__, String8(newDescriptor).c_str(), String8(currentDescriptor).c_str()); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 133 | // always a failure because we know mClazz != clazz |
| 134 | return false; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Steven Moreland | 94a7679 | 2022-12-21 01:20:06 +0000 | [diff] [blame] | 137 | // This will always be an O(n) comparison, but it's expected to be extremely rare. |
| 138 | // since it's an error condition. Do the comparison after we take the lock and |
| 139 | // check the pointer equality fast path. By always taking the lock, it's also |
| 140 | // more flake-proof. However, the check is not dependent on the lock. |
Steven Moreland | 418914a | 2023-06-28 21:47:14 +0000 | [diff] [blame] | 141 | if (descriptor != newDescriptor && !(asABpBinder() && asABpBinder()->isServiceFuzzing())) { |
Steven Moreland | 640c409 | 2020-07-08 00:19:58 +0000 | [diff] [blame] | 142 | if (getBinder()->isBinderAlive()) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 143 | ALOGE("%s: Expecting binder to have class '%s' but descriptor is actually '%s'.", |
| 144 | __func__, String8(newDescriptor).c_str(), SanitizeString(descriptor).c_str()); |
Steven Moreland | 640c409 | 2020-07-08 00:19:58 +0000 | [diff] [blame] | 145 | } else { |
| 146 | // b/155793159 |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 147 | ALOGE("%s: Cannot associate class '%s' to dead binder with cached descriptor '%s'.", |
| 148 | __func__, String8(newDescriptor).c_str(), SanitizeString(descriptor).c_str()); |
Steven Moreland | 640c409 | 2020-07-08 00:19:58 +0000 | [diff] [blame] | 149 | } |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 150 | return false; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Steven Moreland | faf25a4 | 2022-12-21 02:21:36 +0000 | [diff] [blame] | 153 | // A local binder being set for the first time OR |
| 154 | // ignoring a proxy binder which is set multiple time, by considering the first |
| 155 | // associated class as the canonical one. |
| 156 | if (mClazz == nullptr) { |
| 157 | mClazz = clazz; |
| 158 | } |
| 159 | |
Steven Moreland | 94a7679 | 2022-12-21 01:20:06 +0000 | [diff] [blame] | 160 | return true; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData) |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 164 | : AIBinder(clazz), BBinder(), mUserData(userData) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 165 | LOG_ALWAYS_FATAL_IF(clazz == nullptr, "clazz == nullptr"); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 166 | } |
| 167 | ABBinder::~ABBinder() { |
| 168 | getClass()->onDestroy(mUserData); |
| 169 | } |
| 170 | |
| 171 | const String16& ABBinder::getInterfaceDescriptor() const { |
| 172 | return getClass()->getInterfaceDescriptor(); |
| 173 | } |
| 174 | |
Steven Moreland | a194c45 | 2019-03-04 16:47:07 -0800 | [diff] [blame] | 175 | status_t ABBinder::dump(int fd, const ::android::Vector<String16>& args) { |
| 176 | AIBinder_onDump onDump = getClass()->onDump; |
| 177 | |
| 178 | if (onDump == nullptr) { |
| 179 | return STATUS_OK; |
| 180 | } |
| 181 | |
| 182 | // technically UINT32_MAX would be okay here, but INT32_MAX is expected since this may be |
| 183 | // null in Java |
| 184 | if (args.size() > INT32_MAX) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 185 | ALOGE("ABBinder::dump received too many arguments: %zu", args.size()); |
Steven Moreland | a194c45 | 2019-03-04 16:47:07 -0800 | [diff] [blame] | 186 | return STATUS_BAD_VALUE; |
| 187 | } |
| 188 | |
| 189 | std::vector<String8> utf8Args; // owns memory of utf8s |
| 190 | utf8Args.reserve(args.size()); |
| 191 | std::vector<const char*> utf8Pointers; // what can be passed over NDK API |
| 192 | utf8Pointers.reserve(args.size()); |
| 193 | |
| 194 | for (size_t i = 0; i < args.size(); i++) { |
| 195 | utf8Args.push_back(String8(args[i])); |
| 196 | utf8Pointers.push_back(utf8Args[i].c_str()); |
| 197 | } |
| 198 | |
| 199 | return onDump(this, fd, utf8Pointers.data(), utf8Pointers.size()); |
| 200 | } |
| 201 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 202 | status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply, |
| 203 | binder_flags_t flags) { |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 204 | if (isUserCommand(code)) { |
Steven Moreland | f3ed806 | 2021-08-27 14:31:14 -0700 | [diff] [blame] | 205 | if (getClass()->writeHeader && !data.checkInterface(this)) { |
Steven Moreland | 9d089af | 2018-09-18 08:58:09 -0700 | [diff] [blame] | 206 | return STATUS_BAD_TYPE; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | const AParcel in = AParcel::readOnly(this, &data); |
| 210 | AParcel out = AParcel(this, reply, false /*owns*/); |
| 211 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 212 | binder_status_t status = getClass()->onTransact(this, code, &in, &out); |
| 213 | return PruneStatusT(status); |
Steven Moreland | 8823407 | 2020-08-06 19:32:45 +0000 | [diff] [blame] | 214 | } else if (code == SHELL_COMMAND_TRANSACTION && getClass()->handleShellCommand != nullptr) { |
Andrei Homescu | 38eafb7 | 2024-03-22 22:38:08 +0000 | [diff] [blame] | 215 | if constexpr (!android::kEnableKernelIpc) { |
| 216 | // Non-IPC builds do not have getCallingUid(), |
| 217 | // so we have no way of authenticating the caller |
| 218 | return STATUS_PERMISSION_DENIED; |
| 219 | } |
| 220 | |
Ruchir Rastogi | cc7a746 | 2020-01-31 14:29:15 -0800 | [diff] [blame] | 221 | int in = data.readFileDescriptor(); |
| 222 | int out = data.readFileDescriptor(); |
| 223 | int err = data.readFileDescriptor(); |
| 224 | |
| 225 | int argc = data.readInt32(); |
| 226 | std::vector<String8> utf8Args; // owns memory of utf8s |
| 227 | std::vector<const char*> utf8Pointers; // what can be passed over NDK API |
| 228 | for (int i = 0; i < argc && data.dataAvail() > 0; i++) { |
| 229 | utf8Args.push_back(String8(data.readString16())); |
| 230 | utf8Pointers.push_back(utf8Args[i].c_str()); |
| 231 | } |
| 232 | |
| 233 | data.readStrongBinder(); // skip over the IShellCallback |
| 234 | sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(data.readStrongBinder()); |
| 235 | |
| 236 | // Shell commands should only be callable by ADB. |
| 237 | uid_t uid = AIBinder_getCallingUid(); |
Tomasz Wasilczyk | b8ebcca | 2023-10-09 19:54:20 +0000 | [diff] [blame] | 238 | if (uid != 0 /* root */ |
| 239 | #ifdef AID_SHELL |
| 240 | && uid != AID_SHELL |
| 241 | #endif |
| 242 | ) { |
Ruchir Rastogi | cc7a746 | 2020-01-31 14:29:15 -0800 | [diff] [blame] | 243 | if (resultReceiver != nullptr) { |
| 244 | resultReceiver->send(-1); |
| 245 | } |
| 246 | return STATUS_PERMISSION_DENIED; |
| 247 | } |
| 248 | |
| 249 | // Check that the file descriptors are valid. |
| 250 | if (in == STATUS_BAD_TYPE || out == STATUS_BAD_TYPE || err == STATUS_BAD_TYPE) { |
| 251 | if (resultReceiver != nullptr) { |
| 252 | resultReceiver->send(-1); |
| 253 | } |
| 254 | return STATUS_BAD_VALUE; |
| 255 | } |
| 256 | |
| 257 | binder_status_t status = getClass()->handleShellCommand( |
| 258 | this, in, out, err, utf8Pointers.data(), utf8Pointers.size()); |
| 259 | if (resultReceiver != nullptr) { |
| 260 | resultReceiver->send(status); |
| 261 | } |
| 262 | return status; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 263 | } else { |
| 264 | return BBinder::onTransact(code, data, reply, flags); |
| 265 | } |
| 266 | } |
| 267 | |
Devin Moore | 9f9d0e1 | 2024-02-29 22:07:12 +0000 | [diff] [blame] | 268 | void ABBinder::addDeathRecipient(const ::android::sp<AIBinder_DeathRecipient>& /* recipient */, |
| 269 | void* /* cookie */) { |
| 270 | LOG_ALWAYS_FATAL("Should not reach this. Can't linkToDeath local binders."); |
| 271 | } |
| 272 | |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 273 | ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder) |
Steven Moreland | 3acd490 | 2022-07-26 18:29:58 +0000 | [diff] [blame] | 274 | : AIBinder(nullptr /*clazz*/), mRemote(binder) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 275 | LOG_ALWAYS_FATAL_IF(binder == nullptr, "binder == nullptr"); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 276 | } |
Devin Moore | 9f9d0e1 | 2024-02-29 22:07:12 +0000 | [diff] [blame] | 277 | |
| 278 | ABpBinder::~ABpBinder() { |
| 279 | for (auto& recip : mDeathRecipients) { |
| 280 | sp<AIBinder_DeathRecipient> strongRecip = recip.recipient.promote(); |
| 281 | if (strongRecip) { |
| 282 | strongRecip->pruneThisTransferEntry(getBinder(), recip.cookie); |
| 283 | } |
| 284 | } |
| 285 | } |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 286 | |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 287 | sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) { |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 288 | if (binder == nullptr) { |
| 289 | return nullptr; |
| 290 | } |
| 291 | if (ABBinderTag::has(binder)) { |
| 292 | return static_cast<ABBinder*>(binder.get()); |
| 293 | } |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 294 | |
Steven Moreland | 91a96b6 | 2021-06-30 23:07:13 +0000 | [diff] [blame] | 295 | // The following code ensures that for a given binder object (remote or local), if it is not an |
| 296 | // ABBinder then at most one ABpBinder object exists in a given process representing it. |
| 297 | |
Steven Moreland | 1f1bed6 | 2021-06-25 21:50:50 +0000 | [diff] [blame] | 298 | auto* value = static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId)); |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 299 | if (value == nullptr) { |
| 300 | value = new ABpBinderTag::Value; |
Steven Moreland | 1f1bed6 | 2021-06-25 21:50:50 +0000 | [diff] [blame] | 301 | auto oldValue = static_cast<ABpBinderTag::Value*>( |
| 302 | binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value), |
| 303 | nullptr /*cookie*/, ABpBinderTag::clean)); |
| 304 | |
| 305 | // allocated by another thread |
| 306 | if (oldValue) { |
| 307 | delete value; |
| 308 | value = oldValue; |
| 309 | } |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Steven Moreland | 1f1bed6 | 2021-06-25 21:50:50 +0000 | [diff] [blame] | 312 | sp<ABpBinder> ret; |
| 313 | binder->withLock([&]() { |
| 314 | ret = value->binder.promote(); |
| 315 | if (ret == nullptr) { |
| 316 | ret = sp<ABpBinder>::make(binder); |
| 317 | value->binder = ret; |
| 318 | } |
| 319 | }); |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 320 | |
| 321 | return ret; |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Devin Moore | 9f9d0e1 | 2024-02-29 22:07:12 +0000 | [diff] [blame] | 324 | void ABpBinder::addDeathRecipient(const ::android::sp<AIBinder_DeathRecipient>& recipient, |
| 325 | void* cookie) { |
| 326 | std::lock_guard<std::mutex> l(mDeathRecipientsMutex); |
| 327 | mDeathRecipients.emplace_back(recipient, cookie); |
| 328 | } |
| 329 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 330 | struct AIBinder_Weak { |
| 331 | wp<AIBinder> binder; |
| 332 | }; |
| 333 | AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) { |
Steven Moreland | 5ccb70f | 2018-09-04 16:30:21 -0700 | [diff] [blame] | 334 | if (binder == nullptr) { |
| 335 | return nullptr; |
| 336 | } |
| 337 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 338 | return new AIBinder_Weak{wp<AIBinder>(binder)}; |
| 339 | } |
Steven Moreland | 9b80e28 | 2018-09-19 13:57:23 -0700 | [diff] [blame] | 340 | void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) { |
| 341 | delete weakBinder; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 342 | } |
| 343 | AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) { |
Steven Moreland | 5ccb70f | 2018-09-04 16:30:21 -0700 | [diff] [blame] | 344 | if (weakBinder == nullptr) { |
| 345 | return nullptr; |
| 346 | } |
| 347 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 348 | sp<AIBinder> binder = weakBinder->binder.promote(); |
| 349 | AIBinder_incStrong(binder.get()); |
| 350 | return binder.get(); |
| 351 | } |
| 352 | |
Steven Moreland | c70e012 | 2021-01-08 02:44:53 +0000 | [diff] [blame] | 353 | AIBinder_Weak* AIBinder_Weak_clone(const AIBinder_Weak* weak) { |
| 354 | if (weak == nullptr) { |
| 355 | return nullptr; |
| 356 | } |
| 357 | |
| 358 | return new AIBinder_Weak{weak->binder}; |
| 359 | } |
| 360 | |
| 361 | bool AIBinder_lt(const AIBinder* lhs, const AIBinder* rhs) { |
| 362 | if (lhs == nullptr || rhs == nullptr) return lhs < rhs; |
| 363 | |
| 364 | return const_cast<AIBinder*>(lhs)->getBinder() < const_cast<AIBinder*>(rhs)->getBinder(); |
| 365 | } |
| 366 | |
| 367 | bool AIBinder_Weak_lt(const AIBinder_Weak* lhs, const AIBinder_Weak* rhs) { |
| 368 | if (lhs == nullptr || rhs == nullptr) return lhs < rhs; |
| 369 | |
| 370 | return lhs->binder < rhs->binder; |
| 371 | } |
| 372 | |
Steven Moreland | faf25a4 | 2022-12-21 02:21:36 +0000 | [diff] [blame] | 373 | // WARNING: When multiple classes exist with the same interface descriptor in different |
| 374 | // linkernamespaces, the first one to be associated with mClazz becomes the canonical one |
| 375 | // and the only requirement on this is that the interface descriptors match. If this |
| 376 | // is an ABpBinder, no other state can be referenced from mClazz. |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 377 | AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate, |
| 378 | AIBinder_Class_onDestroy onDestroy, |
| 379 | AIBinder_Class_onTransact onTransact) |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 380 | : onCreate(onCreate), |
| 381 | onDestroy(onDestroy), |
| 382 | onTransact(onTransact), |
Stephen Crane | 8fde87f | 2020-11-17 15:06:11 -0800 | [diff] [blame] | 383 | mInterfaceDescriptor(interfaceDescriptor), |
| 384 | mWideInterfaceDescriptor(interfaceDescriptor) {} |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 385 | |
| 386 | AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor, |
| 387 | AIBinder_Class_onCreate onCreate, |
| 388 | AIBinder_Class_onDestroy onDestroy, |
| 389 | AIBinder_Class_onTransact onTransact) { |
| 390 | if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr || |
| 391 | onTransact == nullptr) { |
| 392 | return nullptr; |
| 393 | } |
| 394 | |
| 395 | return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact); |
| 396 | } |
| 397 | |
Steven Moreland | a194c45 | 2019-03-04 16:47:07 -0800 | [diff] [blame] | 398 | void AIBinder_Class_setOnDump(AIBinder_Class* clazz, AIBinder_onDump onDump) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 399 | LOG_ALWAYS_FATAL_IF(clazz == nullptr, "setOnDump requires non-null clazz"); |
Steven Moreland | a194c45 | 2019-03-04 16:47:07 -0800 | [diff] [blame] | 400 | |
| 401 | // this is required to be called before instances are instantiated |
| 402 | clazz->onDump = onDump; |
| 403 | } |
| 404 | |
Steven Moreland | f3ed806 | 2021-08-27 14:31:14 -0700 | [diff] [blame] | 405 | void AIBinder_Class_disableInterfaceTokenHeader(AIBinder_Class* clazz) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 406 | LOG_ALWAYS_FATAL_IF(clazz == nullptr, "disableInterfaceTokenHeader requires non-null clazz"); |
Steven Moreland | f3ed806 | 2021-08-27 14:31:14 -0700 | [diff] [blame] | 407 | |
| 408 | clazz->writeHeader = false; |
| 409 | } |
| 410 | |
Ruchir Rastogi | cc7a746 | 2020-01-31 14:29:15 -0800 | [diff] [blame] | 411 | void AIBinder_Class_setHandleShellCommand(AIBinder_Class* clazz, |
| 412 | AIBinder_handleShellCommand handleShellCommand) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 413 | LOG_ALWAYS_FATAL_IF(clazz == nullptr, "setHandleShellCommand requires non-null clazz"); |
Ruchir Rastogi | cc7a746 | 2020-01-31 14:29:15 -0800 | [diff] [blame] | 414 | |
| 415 | clazz->handleShellCommand = handleShellCommand; |
| 416 | } |
| 417 | |
Stephen Crane | 8fde87f | 2020-11-17 15:06:11 -0800 | [diff] [blame] | 418 | const char* AIBinder_Class_getDescriptor(const AIBinder_Class* clazz) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 419 | LOG_ALWAYS_FATAL_IF(clazz == nullptr, "getDescriptor requires non-null clazz"); |
Stephen Crane | 8fde87f | 2020-11-17 15:06:11 -0800 | [diff] [blame] | 420 | |
| 421 | return clazz->getInterfaceDescriptorUtf8(); |
| 422 | } |
| 423 | |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 424 | AIBinder_DeathRecipient::TransferDeathRecipient::~TransferDeathRecipient() { |
| 425 | if (mOnUnlinked != nullptr) { |
| 426 | mOnUnlinked(mCookie); |
| 427 | } |
| 428 | } |
| 429 | |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 430 | void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 431 | LOG_ALWAYS_FATAL_IF(who != mWho, "%p (%p) vs %p (%p)", who.unsafe_get(), who.get_refs(), |
| 432 | mWho.unsafe_get(), mWho.get_refs()); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 433 | |
| 434 | mOnDied(mCookie); |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 435 | |
| 436 | sp<AIBinder_DeathRecipient> recipient = mParentRecipient.promote(); |
| 437 | sp<IBinder> strongWho = who.promote(); |
| 438 | |
| 439 | // otherwise this will be cleaned up later with pruneDeadTransferEntriesLocked |
| 440 | if (recipient != nullptr && strongWho != nullptr) { |
| 441 | status_t result = recipient->unlinkToDeath(strongWho, mCookie); |
| 442 | if (result != ::android::DEAD_OBJECT) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 443 | ALOGW("Unlinking to dead binder resulted in: %d", result); |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 444 | } |
| 445 | } |
| 446 | |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 447 | mWho = nullptr; |
| 448 | } |
| 449 | |
| 450 | AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied) |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 451 | : mOnDied(onDied), mOnUnlinked(nullptr) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 452 | LOG_ALWAYS_FATAL_IF(onDied == nullptr, "onDied == nullptr"); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 453 | } |
| 454 | |
Devin Moore | 9f9d0e1 | 2024-02-29 22:07:12 +0000 | [diff] [blame] | 455 | void AIBinder_DeathRecipient::pruneThisTransferEntry(const sp<IBinder>& who, void* cookie) { |
| 456 | std::lock_guard<std::mutex> l(mDeathRecipientsMutex); |
| 457 | mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(), |
| 458 | [&](const sp<TransferDeathRecipient>& tdr) { |
| 459 | auto tdrWho = tdr->getWho(); |
| 460 | return tdrWho != nullptr && tdrWho.promote() == who && |
| 461 | cookie == tdr->getCookie(); |
| 462 | }), |
| 463 | mDeathRecipients.end()); |
| 464 | } |
| 465 | |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 466 | void AIBinder_DeathRecipient::pruneDeadTransferEntriesLocked() { |
| 467 | mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(), |
| 468 | [](const sp<TransferDeathRecipient>& tdr) { |
| 469 | return tdr->getWho() == nullptr; |
| 470 | }), |
| 471 | mDeathRecipients.end()); |
| 472 | } |
| 473 | |
Jiyong Park | 4f97a8c | 2020-11-16 18:11:14 +0900 | [diff] [blame] | 474 | binder_status_t AIBinder_DeathRecipient::linkToDeath(const sp<IBinder>& binder, void* cookie) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 475 | LOG_ALWAYS_FATAL_IF(binder == nullptr, "binder == nullptr"); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 476 | |
| 477 | std::lock_guard<std::mutex> l(mDeathRecipientsMutex); |
| 478 | |
| 479 | sp<TransferDeathRecipient> recipient = |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 480 | new TransferDeathRecipient(binder, cookie, this, mOnDied, mOnUnlinked); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 481 | |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 482 | status_t status = binder->linkToDeath(recipient, cookie, 0 /*flags*/); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 483 | if (status != STATUS_OK) { |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 484 | // When we failed to link, the destructor of TransferDeathRecipient runs here, which |
| 485 | // ensures that mOnUnlinked is called before we return with an error from this method. |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 486 | return PruneStatusT(status); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | mDeathRecipients.push_back(recipient); |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 490 | |
| 491 | pruneDeadTransferEntriesLocked(); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 492 | return STATUS_OK; |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 493 | } |
| 494 | |
Jiyong Park | 4f97a8c | 2020-11-16 18:11:14 +0900 | [diff] [blame] | 495 | binder_status_t AIBinder_DeathRecipient::unlinkToDeath(const sp<IBinder>& binder, void* cookie) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 496 | LOG_ALWAYS_FATAL_IF(binder == nullptr, "binder == nullptr"); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 497 | |
| 498 | std::lock_guard<std::mutex> l(mDeathRecipientsMutex); |
| 499 | |
| 500 | for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) { |
| 501 | sp<TransferDeathRecipient> recipient = *it; |
| 502 | |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 503 | if (recipient->getCookie() == cookie && recipient->getWho() == binder) { |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 504 | mDeathRecipients.erase(it.base() - 1); |
| 505 | |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 506 | status_t status = binder->unlinkToDeath(recipient, cookie, 0 /*flags*/); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 507 | if (status != ::android::OK) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 508 | ALOGE("%s: removed reference to death recipient but unlink failed: %s", __func__, |
| 509 | statusToString(status).c_str()); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 510 | } |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 511 | return PruneStatusT(status); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 515 | return STATUS_NAME_NOT_FOUND; |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 518 | void AIBinder_DeathRecipient::setOnUnlinked(AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) { |
| 519 | mOnUnlinked = onUnlinked; |
| 520 | } |
| 521 | |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 522 | // start of C-API methods |
| 523 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 524 | AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) { |
| 525 | if (clazz == nullptr) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 526 | ALOGE("%s: Must provide class to construct local binder.", __func__); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 527 | return nullptr; |
| 528 | } |
| 529 | |
| 530 | void* userData = clazz->onCreate(args); |
| 531 | |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 532 | sp<AIBinder> ret = new ABBinder(clazz, userData); |
| 533 | ABBinderTag::attach(ret->getBinder()); |
| 534 | |
| 535 | AIBinder_incStrong(ret.get()); |
| 536 | return ret.get(); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Steven Moreland | 5ea54da | 2018-09-04 13:29:55 -0700 | [diff] [blame] | 539 | bool AIBinder_isRemote(const AIBinder* binder) { |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 540 | if (binder == nullptr) { |
Steven Moreland | 56f752a | 2018-11-05 17:23:37 -0800 | [diff] [blame] | 541 | return false; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | return binder->isRemote(); |
| 545 | } |
| 546 | |
Steven Moreland | 65867d7 | 2018-09-04 14:22:26 -0700 | [diff] [blame] | 547 | bool AIBinder_isAlive(const AIBinder* binder) { |
| 548 | if (binder == nullptr) { |
| 549 | return false; |
| 550 | } |
| 551 | |
| 552 | return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive(); |
| 553 | } |
| 554 | |
| 555 | binder_status_t AIBinder_ping(AIBinder* binder) { |
| 556 | if (binder == nullptr) { |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 557 | return STATUS_UNEXPECTED_NULL; |
Steven Moreland | 65867d7 | 2018-09-04 14:22:26 -0700 | [diff] [blame] | 558 | } |
| 559 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 560 | return PruneStatusT(binder->getBinder()->pingBinder()); |
Steven Moreland | 65867d7 | 2018-09-04 14:22:26 -0700 | [diff] [blame] | 561 | } |
| 562 | |
Steven Moreland | a194c45 | 2019-03-04 16:47:07 -0800 | [diff] [blame] | 563 | binder_status_t AIBinder_dump(AIBinder* binder, int fd, const char** args, uint32_t numArgs) { |
| 564 | if (binder == nullptr) { |
| 565 | return STATUS_UNEXPECTED_NULL; |
| 566 | } |
| 567 | |
| 568 | ABBinder* bBinder = binder->asABBinder(); |
| 569 | if (bBinder != nullptr) { |
| 570 | AIBinder_onDump onDump = binder->getClass()->onDump; |
| 571 | if (onDump == nullptr) { |
| 572 | return STATUS_OK; |
| 573 | } |
| 574 | return PruneStatusT(onDump(bBinder, fd, args, numArgs)); |
| 575 | } |
| 576 | |
| 577 | ::android::Vector<String16> utf16Args; |
| 578 | utf16Args.setCapacity(numArgs); |
| 579 | for (uint32_t i = 0; i < numArgs; i++) { |
| 580 | utf16Args.push(String16(String8(args[i]))); |
| 581 | } |
| 582 | |
| 583 | status_t status = binder->getBinder()->dump(fd, utf16Args); |
| 584 | return PruneStatusT(status); |
| 585 | } |
| 586 | |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 587 | binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient, |
| 588 | void* cookie) { |
| 589 | if (binder == nullptr || recipient == nullptr) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 590 | ALOGE("%s: Must provide binder (%p) and recipient (%p)", __func__, binder, recipient); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 591 | return STATUS_UNEXPECTED_NULL; |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Devin Moore | 9f9d0e1 | 2024-02-29 22:07:12 +0000 | [diff] [blame] | 594 | binder_status_t ret = recipient->linkToDeath(binder->getBinder(), cookie); |
| 595 | if (ret == STATUS_OK) { |
| 596 | binder->addDeathRecipient(recipient, cookie); |
| 597 | } |
| 598 | return ret; |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient, |
| 602 | void* cookie) { |
| 603 | if (binder == nullptr || recipient == nullptr) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 604 | ALOGE("%s: Must provide binder (%p) and recipient (%p)", __func__, binder, recipient); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 605 | return STATUS_UNEXPECTED_NULL; |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 608 | // returns binder_status_t |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 609 | return recipient->unlinkToDeath(binder->getBinder(), cookie); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 610 | } |
| 611 | |
Steven Moreland | f3034b0 | 2018-11-12 17:37:46 -0800 | [diff] [blame] | 612 | uid_t AIBinder_getCallingUid() { |
| 613 | return ::android::IPCThreadState::self()->getCallingUid(); |
| 614 | } |
| 615 | |
| 616 | pid_t AIBinder_getCallingPid() { |
| 617 | return ::android::IPCThreadState::self()->getCallingPid(); |
| 618 | } |
| 619 | |
Alice Ryhl | 59aeae8 | 2021-08-25 10:21:26 +0000 | [diff] [blame] | 620 | bool AIBinder_isHandlingTransaction() { |
| 621 | return ::android::IPCThreadState::self()->getServingStackPointer() != nullptr; |
| 622 | } |
| 623 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 624 | void AIBinder_incStrong(AIBinder* binder) { |
| 625 | if (binder == nullptr) { |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 626 | return; |
| 627 | } |
| 628 | |
| 629 | binder->incStrong(nullptr); |
| 630 | } |
| 631 | void AIBinder_decStrong(AIBinder* binder) { |
| 632 | if (binder == nullptr) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 633 | ALOGE("%s: on null binder", __func__); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 634 | return; |
| 635 | } |
| 636 | |
| 637 | binder->decStrong(nullptr); |
| 638 | } |
| 639 | int32_t AIBinder_debugGetRefCount(AIBinder* binder) { |
| 640 | if (binder == nullptr) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 641 | ALOGE("%s: on null binder", __func__); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 642 | return -1; |
| 643 | } |
| 644 | |
| 645 | return binder->getStrongCount(); |
| 646 | } |
| 647 | |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 648 | bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) { |
| 649 | if (binder == nullptr) { |
| 650 | return false; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 651 | } |
| 652 | |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 653 | return binder->associateClass(clazz); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | const AIBinder_Class* AIBinder_getClass(AIBinder* binder) { |
| 657 | if (binder == nullptr) { |
| 658 | return nullptr; |
| 659 | } |
| 660 | |
| 661 | return binder->getClass(); |
| 662 | } |
| 663 | |
| 664 | void* AIBinder_getUserData(AIBinder* binder) { |
| 665 | if (binder == nullptr) { |
| 666 | return nullptr; |
| 667 | } |
| 668 | |
| 669 | ABBinder* bBinder = binder->asABBinder(); |
| 670 | if (bBinder == nullptr) { |
| 671 | return nullptr; |
| 672 | } |
| 673 | |
| 674 | return bBinder->getUserData(); |
| 675 | } |
| 676 | |
| 677 | binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) { |
| 678 | if (binder == nullptr || in == nullptr) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 679 | ALOGE("%s: requires non-null parameters binder (%p) and in (%p).", __func__, binder, in); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 680 | return STATUS_UNEXPECTED_NULL; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 681 | } |
| 682 | const AIBinder_Class* clazz = binder->getClass(); |
| 683 | if (clazz == nullptr) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 684 | ALOGE("%s: Class must be defined for a remote binder transaction. See " |
| 685 | "AIBinder_associateClass.", |
| 686 | __func__); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 687 | return STATUS_INVALID_OPERATION; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | *in = new AParcel(binder); |
Steven Moreland | 1fda67b | 2021-04-02 18:35:50 +0000 | [diff] [blame] | 691 | (*in)->get()->markForBinder(binder->getBinder()); |
| 692 | |
Steven Moreland | f3ed806 | 2021-08-27 14:31:14 -0700 | [diff] [blame] | 693 | status_t status = android::OK; |
Steven Moreland | faf25a4 | 2022-12-21 02:21:36 +0000 | [diff] [blame] | 694 | |
| 695 | // note - this is the only read of a value in clazz, and it comes with a warning |
| 696 | // on the API itself. Do not copy this design. Instead, attach data in a new |
| 697 | // version of the prepareTransaction function. |
Steven Moreland | f3ed806 | 2021-08-27 14:31:14 -0700 | [diff] [blame] | 698 | if (clazz->writeHeader) { |
| 699 | status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor()); |
| 700 | } |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 701 | binder_status_t ret = PruneStatusT(status); |
| 702 | |
| 703 | if (ret != STATUS_OK) { |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 704 | delete *in; |
| 705 | *in = nullptr; |
| 706 | } |
| 707 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 708 | return ret; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 709 | } |
| 710 | |
Steven Moreland | 9b80e28 | 2018-09-19 13:57:23 -0700 | [diff] [blame] | 711 | static void DestroyParcel(AParcel** parcel) { |
| 712 | delete *parcel; |
| 713 | *parcel = nullptr; |
| 714 | } |
| 715 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 716 | binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in, |
| 717 | AParcel** out, binder_flags_t flags) { |
| 718 | if (in == nullptr) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 719 | ALOGE("%s: requires non-null in parameter", __func__); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 720 | return STATUS_UNEXPECTED_NULL; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 721 | } |
| 722 | |
Steven Moreland | caa776c | 2018-09-04 13:48:11 -0700 | [diff] [blame] | 723 | using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 724 | // This object is the input to the transaction. This function takes ownership of it and deletes |
| 725 | // it. |
Steven Moreland | 9b80e28 | 2018-09-19 13:57:23 -0700 | [diff] [blame] | 726 | AutoParcelDestroyer forIn(in, DestroyParcel); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 727 | |
| 728 | if (!isUserCommand(code)) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 729 | ALOGE("%s: Only user-defined transactions can be made from the NDK, but requested: %d", |
| 730 | __func__, code); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 731 | return STATUS_UNKNOWN_TRANSACTION; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 734 | constexpr binder_flags_t kAllFlags = FLAG_PRIVATE_VENDOR | FLAG_ONEWAY | FLAG_CLEAR_BUF; |
Steven Moreland | 46b5fea | 2019-10-15 11:22:18 -0700 | [diff] [blame] | 735 | if ((flags & ~kAllFlags) != 0) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 736 | ALOGE("%s: Unrecognized flags sent: %d", __func__, flags); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 737 | return STATUS_BAD_VALUE; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | if (binder == nullptr || *in == nullptr || out == nullptr) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 741 | ALOGE("%s: requires non-null parameters binder (%p), in (%p), and out (%p).", __func__, |
| 742 | binder, in, out); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 743 | return STATUS_UNEXPECTED_NULL; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | if ((*in)->getBinder() != binder) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 747 | ALOGE("%s: parcel is associated with binder object %p but called with %p", __func__, binder, |
| 748 | (*in)->getBinder()); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 749 | return STATUS_BAD_VALUE; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | *out = new AParcel(binder); |
| 753 | |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 754 | status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 755 | binder_status_t ret = PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 756 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 757 | if (ret != STATUS_OK) { |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 758 | delete *out; |
| 759 | *out = nullptr; |
| 760 | } |
| 761 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 762 | return ret; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 763 | } |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 764 | |
| 765 | AIBinder_DeathRecipient* AIBinder_DeathRecipient_new( |
| 766 | AIBinder_DeathRecipient_onBinderDied onBinderDied) { |
| 767 | if (onBinderDied == nullptr) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 768 | ALOGE("%s: requires non-null onBinderDied parameter.", __func__); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 769 | return nullptr; |
| 770 | } |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 771 | auto ret = new AIBinder_DeathRecipient(onBinderDied); |
| 772 | ret->incStrong(nullptr); |
| 773 | return ret; |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 774 | } |
| 775 | |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 776 | void AIBinder_DeathRecipient_setOnUnlinked(AIBinder_DeathRecipient* recipient, |
| 777 | AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) { |
| 778 | if (recipient == nullptr) { |
| 779 | return; |
| 780 | } |
| 781 | |
| 782 | recipient->setOnUnlinked(onUnlinked); |
| 783 | } |
| 784 | |
Steven Moreland | 9b80e28 | 2018-09-19 13:57:23 -0700 | [diff] [blame] | 785 | void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) { |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 786 | if (recipient == nullptr) { |
| 787 | return; |
| 788 | } |
| 789 | |
| 790 | recipient->decStrong(nullptr); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 791 | } |
Steven Moreland | ea14ef2 | 2019-08-20 10:50:08 -0700 | [diff] [blame] | 792 | |
| 793 | binder_status_t AIBinder_getExtension(AIBinder* binder, AIBinder** outExt) { |
| 794 | if (binder == nullptr || outExt == nullptr) { |
| 795 | if (outExt != nullptr) { |
| 796 | *outExt = nullptr; |
| 797 | } |
| 798 | return STATUS_UNEXPECTED_NULL; |
| 799 | } |
| 800 | |
| 801 | sp<IBinder> ext; |
| 802 | status_t res = binder->getBinder()->getExtension(&ext); |
| 803 | |
| 804 | if (res != android::OK) { |
| 805 | *outExt = nullptr; |
| 806 | return PruneStatusT(res); |
| 807 | } |
| 808 | |
| 809 | sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(ext); |
| 810 | if (ret != nullptr) ret->incStrong(binder); |
| 811 | |
| 812 | *outExt = ret.get(); |
| 813 | return STATUS_OK; |
| 814 | } |
| 815 | |
| 816 | binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) { |
| 817 | if (binder == nullptr || ext == nullptr) { |
| 818 | return STATUS_UNEXPECTED_NULL; |
| 819 | } |
| 820 | |
| 821 | ABBinder* rawBinder = binder->asABBinder(); |
| 822 | if (rawBinder == nullptr) { |
| 823 | return STATUS_INVALID_OPERATION; |
| 824 | } |
| 825 | |
| 826 | rawBinder->setExtension(ext->getBinder()); |
| 827 | return STATUS_OK; |
| 828 | } |
Steven Moreland | 2f405f5 | 2020-07-08 22:24:29 +0000 | [diff] [blame] | 829 | |
| 830 | // platform methods follow |
| 831 | |
| 832 | void AIBinder_setRequestingSid(AIBinder* binder, bool requestingSid) { |
| 833 | ABBinder* localBinder = binder->asABBinder(); |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 834 | LOG_ALWAYS_FATAL_IF(localBinder == nullptr, |
| 835 | "AIBinder_setRequestingSid must be called on a local binder"); |
Steven Moreland | 2f405f5 | 2020-07-08 22:24:29 +0000 | [diff] [blame] | 836 | |
| 837 | localBinder->setRequestingSid(requestingSid); |
| 838 | } |
| 839 | |
| 840 | const char* AIBinder_getCallingSid() { |
| 841 | return ::android::IPCThreadState::self()->getCallingSid(); |
| 842 | } |
Steven Moreland | b2de953 | 2020-05-29 21:44:41 +0000 | [diff] [blame] | 843 | |
Ady Abraham | 96174ac | 2021-10-15 11:02:03 -0700 | [diff] [blame] | 844 | void AIBinder_setMinSchedulerPolicy(AIBinder* binder, int policy, int priority) { |
| 845 | binder->asABBinder()->setMinSchedulerPolicy(policy, priority); |
| 846 | } |
Steven Moreland | 454a26a | 2021-12-14 01:26:21 +0000 | [diff] [blame] | 847 | |
| 848 | void AIBinder_setInheritRt(AIBinder* binder, bool inheritRt) { |
| 849 | ABBinder* localBinder = binder->asABBinder(); |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 850 | LOG_ALWAYS_FATAL_IF(localBinder == nullptr, |
| 851 | "AIBinder_setInheritRt must be called on a local binder"); |
Steven Moreland | 454a26a | 2021-12-14 01:26:21 +0000 | [diff] [blame] | 852 | |
| 853 | localBinder->setInheritRt(inheritRt); |
| 854 | } |