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