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 | |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 261 | ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder) |
Steven Moreland | 3acd490 | 2022-07-26 18:29:58 +0000 | [diff] [blame] | 262 | : AIBinder(nullptr /*clazz*/), mRemote(binder) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 263 | LOG_ALWAYS_FATAL_IF(binder == nullptr, "binder == nullptr"); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 264 | } |
| 265 | ABpBinder::~ABpBinder() {} |
| 266 | |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 267 | sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) { |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 268 | if (binder == nullptr) { |
| 269 | return nullptr; |
| 270 | } |
| 271 | if (ABBinderTag::has(binder)) { |
| 272 | return static_cast<ABBinder*>(binder.get()); |
| 273 | } |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 274 | |
Steven Moreland | 91a96b6 | 2021-06-30 23:07:13 +0000 | [diff] [blame] | 275 | // The following code ensures that for a given binder object (remote or local), if it is not an |
| 276 | // ABBinder then at most one ABpBinder object exists in a given process representing it. |
| 277 | |
Steven Moreland | 1f1bed6 | 2021-06-25 21:50:50 +0000 | [diff] [blame] | 278 | auto* value = static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId)); |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 279 | if (value == nullptr) { |
| 280 | value = new ABpBinderTag::Value; |
Steven Moreland | 1f1bed6 | 2021-06-25 21:50:50 +0000 | [diff] [blame] | 281 | auto oldValue = static_cast<ABpBinderTag::Value*>( |
| 282 | binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value), |
| 283 | nullptr /*cookie*/, ABpBinderTag::clean)); |
| 284 | |
| 285 | // allocated by another thread |
| 286 | if (oldValue) { |
| 287 | delete value; |
| 288 | value = oldValue; |
| 289 | } |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Steven Moreland | 1f1bed6 | 2021-06-25 21:50:50 +0000 | [diff] [blame] | 292 | sp<ABpBinder> ret; |
| 293 | binder->withLock([&]() { |
| 294 | ret = value->binder.promote(); |
| 295 | if (ret == nullptr) { |
| 296 | ret = sp<ABpBinder>::make(binder); |
| 297 | value->binder = ret; |
| 298 | } |
| 299 | }); |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 300 | |
| 301 | return ret; |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 304 | struct AIBinder_Weak { |
| 305 | wp<AIBinder> binder; |
| 306 | }; |
| 307 | AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) { |
Steven Moreland | 5ccb70f | 2018-09-04 16:30:21 -0700 | [diff] [blame] | 308 | if (binder == nullptr) { |
| 309 | return nullptr; |
| 310 | } |
| 311 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 312 | return new AIBinder_Weak{wp<AIBinder>(binder)}; |
| 313 | } |
Steven Moreland | 9b80e28 | 2018-09-19 13:57:23 -0700 | [diff] [blame] | 314 | void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) { |
| 315 | delete weakBinder; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 316 | } |
| 317 | AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) { |
Steven Moreland | 5ccb70f | 2018-09-04 16:30:21 -0700 | [diff] [blame] | 318 | if (weakBinder == nullptr) { |
| 319 | return nullptr; |
| 320 | } |
| 321 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 322 | sp<AIBinder> binder = weakBinder->binder.promote(); |
| 323 | AIBinder_incStrong(binder.get()); |
| 324 | return binder.get(); |
| 325 | } |
| 326 | |
Steven Moreland | c70e012 | 2021-01-08 02:44:53 +0000 | [diff] [blame] | 327 | AIBinder_Weak* AIBinder_Weak_clone(const AIBinder_Weak* weak) { |
| 328 | if (weak == nullptr) { |
| 329 | return nullptr; |
| 330 | } |
| 331 | |
| 332 | return new AIBinder_Weak{weak->binder}; |
| 333 | } |
| 334 | |
| 335 | bool AIBinder_lt(const AIBinder* lhs, const AIBinder* rhs) { |
| 336 | if (lhs == nullptr || rhs == nullptr) return lhs < rhs; |
| 337 | |
| 338 | return const_cast<AIBinder*>(lhs)->getBinder() < const_cast<AIBinder*>(rhs)->getBinder(); |
| 339 | } |
| 340 | |
| 341 | bool AIBinder_Weak_lt(const AIBinder_Weak* lhs, const AIBinder_Weak* rhs) { |
| 342 | if (lhs == nullptr || rhs == nullptr) return lhs < rhs; |
| 343 | |
| 344 | return lhs->binder < rhs->binder; |
| 345 | } |
| 346 | |
Steven Moreland | faf25a4 | 2022-12-21 02:21:36 +0000 | [diff] [blame] | 347 | // WARNING: When multiple classes exist with the same interface descriptor in different |
| 348 | // linkernamespaces, the first one to be associated with mClazz becomes the canonical one |
| 349 | // and the only requirement on this is that the interface descriptors match. If this |
| 350 | // is an ABpBinder, no other state can be referenced from mClazz. |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 351 | AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate, |
| 352 | AIBinder_Class_onDestroy onDestroy, |
| 353 | AIBinder_Class_onTransact onTransact) |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 354 | : onCreate(onCreate), |
| 355 | onDestroy(onDestroy), |
| 356 | onTransact(onTransact), |
Stephen Crane | 8fde87f | 2020-11-17 15:06:11 -0800 | [diff] [blame] | 357 | mInterfaceDescriptor(interfaceDescriptor), |
| 358 | mWideInterfaceDescriptor(interfaceDescriptor) {} |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 359 | |
| 360 | AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor, |
| 361 | AIBinder_Class_onCreate onCreate, |
| 362 | AIBinder_Class_onDestroy onDestroy, |
| 363 | AIBinder_Class_onTransact onTransact) { |
| 364 | if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr || |
| 365 | onTransact == nullptr) { |
| 366 | return nullptr; |
| 367 | } |
| 368 | |
| 369 | return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact); |
| 370 | } |
| 371 | |
Steven Moreland | a194c45 | 2019-03-04 16:47:07 -0800 | [diff] [blame] | 372 | void AIBinder_Class_setOnDump(AIBinder_Class* clazz, AIBinder_onDump onDump) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 373 | LOG_ALWAYS_FATAL_IF(clazz == nullptr, "setOnDump requires non-null clazz"); |
Steven Moreland | a194c45 | 2019-03-04 16:47:07 -0800 | [diff] [blame] | 374 | |
| 375 | // this is required to be called before instances are instantiated |
| 376 | clazz->onDump = onDump; |
| 377 | } |
| 378 | |
Steven Moreland | f3ed806 | 2021-08-27 14:31:14 -0700 | [diff] [blame] | 379 | void AIBinder_Class_disableInterfaceTokenHeader(AIBinder_Class* clazz) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 380 | LOG_ALWAYS_FATAL_IF(clazz == nullptr, "disableInterfaceTokenHeader requires non-null clazz"); |
Steven Moreland | f3ed806 | 2021-08-27 14:31:14 -0700 | [diff] [blame] | 381 | |
| 382 | clazz->writeHeader = false; |
| 383 | } |
| 384 | |
Ruchir Rastogi | cc7a746 | 2020-01-31 14:29:15 -0800 | [diff] [blame] | 385 | void AIBinder_Class_setHandleShellCommand(AIBinder_Class* clazz, |
| 386 | AIBinder_handleShellCommand handleShellCommand) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 387 | LOG_ALWAYS_FATAL_IF(clazz == nullptr, "setHandleShellCommand requires non-null clazz"); |
Ruchir Rastogi | cc7a746 | 2020-01-31 14:29:15 -0800 | [diff] [blame] | 388 | |
| 389 | clazz->handleShellCommand = handleShellCommand; |
| 390 | } |
| 391 | |
Stephen Crane | 8fde87f | 2020-11-17 15:06:11 -0800 | [diff] [blame] | 392 | const char* AIBinder_Class_getDescriptor(const AIBinder_Class* clazz) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 393 | LOG_ALWAYS_FATAL_IF(clazz == nullptr, "getDescriptor requires non-null clazz"); |
Stephen Crane | 8fde87f | 2020-11-17 15:06:11 -0800 | [diff] [blame] | 394 | |
| 395 | return clazz->getInterfaceDescriptorUtf8(); |
| 396 | } |
| 397 | |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 398 | AIBinder_DeathRecipient::TransferDeathRecipient::~TransferDeathRecipient() { |
| 399 | if (mOnUnlinked != nullptr) { |
| 400 | mOnUnlinked(mCookie); |
| 401 | } |
| 402 | } |
| 403 | |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 404 | void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 405 | LOG_ALWAYS_FATAL_IF(who != mWho, "%p (%p) vs %p (%p)", who.unsafe_get(), who.get_refs(), |
| 406 | mWho.unsafe_get(), mWho.get_refs()); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 407 | |
| 408 | mOnDied(mCookie); |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 409 | |
| 410 | sp<AIBinder_DeathRecipient> recipient = mParentRecipient.promote(); |
| 411 | sp<IBinder> strongWho = who.promote(); |
| 412 | |
| 413 | // otherwise this will be cleaned up later with pruneDeadTransferEntriesLocked |
| 414 | if (recipient != nullptr && strongWho != nullptr) { |
| 415 | status_t result = recipient->unlinkToDeath(strongWho, mCookie); |
| 416 | if (result != ::android::DEAD_OBJECT) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 417 | ALOGW("Unlinking to dead binder resulted in: %d", result); |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 421 | mWho = nullptr; |
| 422 | } |
| 423 | |
| 424 | AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied) |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 425 | : mOnDied(onDied), mOnUnlinked(nullptr) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 426 | LOG_ALWAYS_FATAL_IF(onDied == nullptr, "onDied == nullptr"); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 429 | void AIBinder_DeathRecipient::pruneDeadTransferEntriesLocked() { |
| 430 | mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(), |
| 431 | [](const sp<TransferDeathRecipient>& tdr) { |
| 432 | return tdr->getWho() == nullptr; |
| 433 | }), |
| 434 | mDeathRecipients.end()); |
| 435 | } |
| 436 | |
Jiyong Park | 4f97a8c | 2020-11-16 18:11:14 +0900 | [diff] [blame] | 437 | binder_status_t AIBinder_DeathRecipient::linkToDeath(const sp<IBinder>& binder, void* cookie) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 438 | LOG_ALWAYS_FATAL_IF(binder == nullptr, "binder == nullptr"); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 439 | |
| 440 | std::lock_guard<std::mutex> l(mDeathRecipientsMutex); |
| 441 | |
| 442 | sp<TransferDeathRecipient> recipient = |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 443 | new TransferDeathRecipient(binder, cookie, this, mOnDied, mOnUnlinked); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 444 | |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 445 | status_t status = binder->linkToDeath(recipient, cookie, 0 /*flags*/); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 446 | if (status != STATUS_OK) { |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 447 | // When we failed to link, the destructor of TransferDeathRecipient runs here, which |
| 448 | // 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] | 449 | return PruneStatusT(status); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | mDeathRecipients.push_back(recipient); |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 453 | |
| 454 | pruneDeadTransferEntriesLocked(); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 455 | return STATUS_OK; |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 456 | } |
| 457 | |
Jiyong Park | 4f97a8c | 2020-11-16 18:11:14 +0900 | [diff] [blame] | 458 | binder_status_t AIBinder_DeathRecipient::unlinkToDeath(const sp<IBinder>& binder, void* cookie) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 459 | LOG_ALWAYS_FATAL_IF(binder == nullptr, "binder == nullptr"); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 460 | |
| 461 | std::lock_guard<std::mutex> l(mDeathRecipientsMutex); |
| 462 | |
| 463 | for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) { |
| 464 | sp<TransferDeathRecipient> recipient = *it; |
| 465 | |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 466 | if (recipient->getCookie() == cookie && recipient->getWho() == binder) { |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 467 | mDeathRecipients.erase(it.base() - 1); |
| 468 | |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 469 | status_t status = binder->unlinkToDeath(recipient, cookie, 0 /*flags*/); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 470 | if (status != ::android::OK) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 471 | ALOGE("%s: removed reference to death recipient but unlink failed: %s", __func__, |
| 472 | statusToString(status).c_str()); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 473 | } |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 474 | return PruneStatusT(status); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 478 | return STATUS_NAME_NOT_FOUND; |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 481 | void AIBinder_DeathRecipient::setOnUnlinked(AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) { |
| 482 | mOnUnlinked = onUnlinked; |
| 483 | } |
| 484 | |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 485 | // start of C-API methods |
| 486 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 487 | AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) { |
| 488 | if (clazz == nullptr) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 489 | ALOGE("%s: Must provide class to construct local binder.", __func__); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 490 | return nullptr; |
| 491 | } |
| 492 | |
| 493 | void* userData = clazz->onCreate(args); |
| 494 | |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 495 | sp<AIBinder> ret = new ABBinder(clazz, userData); |
| 496 | ABBinderTag::attach(ret->getBinder()); |
| 497 | |
| 498 | AIBinder_incStrong(ret.get()); |
| 499 | return ret.get(); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Steven Moreland | 5ea54da | 2018-09-04 13:29:55 -0700 | [diff] [blame] | 502 | bool AIBinder_isRemote(const AIBinder* binder) { |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 503 | if (binder == nullptr) { |
Steven Moreland | 56f752a | 2018-11-05 17:23:37 -0800 | [diff] [blame] | 504 | return false; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | return binder->isRemote(); |
| 508 | } |
| 509 | |
Steven Moreland | 65867d7 | 2018-09-04 14:22:26 -0700 | [diff] [blame] | 510 | bool AIBinder_isAlive(const AIBinder* binder) { |
| 511 | if (binder == nullptr) { |
| 512 | return false; |
| 513 | } |
| 514 | |
| 515 | return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive(); |
| 516 | } |
| 517 | |
| 518 | binder_status_t AIBinder_ping(AIBinder* binder) { |
| 519 | if (binder == nullptr) { |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 520 | return STATUS_UNEXPECTED_NULL; |
Steven Moreland | 65867d7 | 2018-09-04 14:22:26 -0700 | [diff] [blame] | 521 | } |
| 522 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 523 | return PruneStatusT(binder->getBinder()->pingBinder()); |
Steven Moreland | 65867d7 | 2018-09-04 14:22:26 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Steven Moreland | a194c45 | 2019-03-04 16:47:07 -0800 | [diff] [blame] | 526 | binder_status_t AIBinder_dump(AIBinder* binder, int fd, const char** args, uint32_t numArgs) { |
| 527 | if (binder == nullptr) { |
| 528 | return STATUS_UNEXPECTED_NULL; |
| 529 | } |
| 530 | |
| 531 | ABBinder* bBinder = binder->asABBinder(); |
| 532 | if (bBinder != nullptr) { |
| 533 | AIBinder_onDump onDump = binder->getClass()->onDump; |
| 534 | if (onDump == nullptr) { |
| 535 | return STATUS_OK; |
| 536 | } |
| 537 | return PruneStatusT(onDump(bBinder, fd, args, numArgs)); |
| 538 | } |
| 539 | |
| 540 | ::android::Vector<String16> utf16Args; |
| 541 | utf16Args.setCapacity(numArgs); |
| 542 | for (uint32_t i = 0; i < numArgs; i++) { |
| 543 | utf16Args.push(String16(String8(args[i]))); |
| 544 | } |
| 545 | |
| 546 | status_t status = binder->getBinder()->dump(fd, utf16Args); |
| 547 | return PruneStatusT(status); |
| 548 | } |
| 549 | |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 550 | binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient, |
| 551 | void* cookie) { |
| 552 | if (binder == nullptr || recipient == nullptr) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 553 | ALOGE("%s: Must provide binder (%p) and recipient (%p)", __func__, binder, recipient); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 554 | return STATUS_UNEXPECTED_NULL; |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 555 | } |
| 556 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 557 | // returns binder_status_t |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 558 | return recipient->linkToDeath(binder->getBinder(), cookie); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient, |
| 562 | void* cookie) { |
| 563 | if (binder == nullptr || recipient == nullptr) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 564 | ALOGE("%s: Must provide binder (%p) and recipient (%p)", __func__, binder, recipient); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 565 | return STATUS_UNEXPECTED_NULL; |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 568 | // returns binder_status_t |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 569 | return recipient->unlinkToDeath(binder->getBinder(), cookie); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Steven Moreland | f3034b0 | 2018-11-12 17:37:46 -0800 | [diff] [blame] | 572 | uid_t AIBinder_getCallingUid() { |
| 573 | return ::android::IPCThreadState::self()->getCallingUid(); |
| 574 | } |
| 575 | |
| 576 | pid_t AIBinder_getCallingPid() { |
| 577 | return ::android::IPCThreadState::self()->getCallingPid(); |
| 578 | } |
| 579 | |
Alice Ryhl | 59aeae8 | 2021-08-25 10:21:26 +0000 | [diff] [blame] | 580 | bool AIBinder_isHandlingTransaction() { |
| 581 | return ::android::IPCThreadState::self()->getServingStackPointer() != nullptr; |
| 582 | } |
| 583 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 584 | void AIBinder_incStrong(AIBinder* binder) { |
| 585 | if (binder == nullptr) { |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 586 | return; |
| 587 | } |
| 588 | |
| 589 | binder->incStrong(nullptr); |
| 590 | } |
| 591 | void AIBinder_decStrong(AIBinder* binder) { |
| 592 | if (binder == nullptr) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 593 | ALOGE("%s: on null binder", __func__); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 594 | return; |
| 595 | } |
| 596 | |
| 597 | binder->decStrong(nullptr); |
| 598 | } |
| 599 | int32_t AIBinder_debugGetRefCount(AIBinder* binder) { |
| 600 | if (binder == nullptr) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 601 | ALOGE("%s: on null binder", __func__); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 602 | return -1; |
| 603 | } |
| 604 | |
| 605 | return binder->getStrongCount(); |
| 606 | } |
| 607 | |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 608 | bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) { |
| 609 | if (binder == nullptr) { |
| 610 | return false; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 613 | return binder->associateClass(clazz); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | const AIBinder_Class* AIBinder_getClass(AIBinder* binder) { |
| 617 | if (binder == nullptr) { |
| 618 | return nullptr; |
| 619 | } |
| 620 | |
| 621 | return binder->getClass(); |
| 622 | } |
| 623 | |
| 624 | void* AIBinder_getUserData(AIBinder* binder) { |
| 625 | if (binder == nullptr) { |
| 626 | return nullptr; |
| 627 | } |
| 628 | |
| 629 | ABBinder* bBinder = binder->asABBinder(); |
| 630 | if (bBinder == nullptr) { |
| 631 | return nullptr; |
| 632 | } |
| 633 | |
| 634 | return bBinder->getUserData(); |
| 635 | } |
| 636 | |
| 637 | binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) { |
| 638 | if (binder == nullptr || in == nullptr) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 639 | 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] | 640 | return STATUS_UNEXPECTED_NULL; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 641 | } |
| 642 | const AIBinder_Class* clazz = binder->getClass(); |
| 643 | if (clazz == nullptr) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 644 | ALOGE("%s: Class must be defined for a remote binder transaction. See " |
| 645 | "AIBinder_associateClass.", |
| 646 | __func__); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 647 | return STATUS_INVALID_OPERATION; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | *in = new AParcel(binder); |
Steven Moreland | 1fda67b | 2021-04-02 18:35:50 +0000 | [diff] [blame] | 651 | (*in)->get()->markForBinder(binder->getBinder()); |
| 652 | |
Steven Moreland | f3ed806 | 2021-08-27 14:31:14 -0700 | [diff] [blame] | 653 | status_t status = android::OK; |
Steven Moreland | faf25a4 | 2022-12-21 02:21:36 +0000 | [diff] [blame] | 654 | |
| 655 | // note - this is the only read of a value in clazz, and it comes with a warning |
| 656 | // on the API itself. Do not copy this design. Instead, attach data in a new |
| 657 | // version of the prepareTransaction function. |
Steven Moreland | f3ed806 | 2021-08-27 14:31:14 -0700 | [diff] [blame] | 658 | if (clazz->writeHeader) { |
| 659 | status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor()); |
| 660 | } |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 661 | binder_status_t ret = PruneStatusT(status); |
| 662 | |
| 663 | if (ret != STATUS_OK) { |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 664 | delete *in; |
| 665 | *in = nullptr; |
| 666 | } |
| 667 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 668 | return ret; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 669 | } |
| 670 | |
Steven Moreland | 9b80e28 | 2018-09-19 13:57:23 -0700 | [diff] [blame] | 671 | static void DestroyParcel(AParcel** parcel) { |
| 672 | delete *parcel; |
| 673 | *parcel = nullptr; |
| 674 | } |
| 675 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 676 | binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in, |
| 677 | AParcel** out, binder_flags_t flags) { |
| 678 | if (in == nullptr) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 679 | ALOGE("%s: requires non-null in parameter", __func__); |
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 | |
Steven Moreland | caa776c | 2018-09-04 13:48:11 -0700 | [diff] [blame] | 683 | using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 684 | // This object is the input to the transaction. This function takes ownership of it and deletes |
| 685 | // it. |
Steven Moreland | 9b80e28 | 2018-09-19 13:57:23 -0700 | [diff] [blame] | 686 | AutoParcelDestroyer forIn(in, DestroyParcel); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 687 | |
| 688 | if (!isUserCommand(code)) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 689 | ALOGE("%s: Only user-defined transactions can be made from the NDK, but requested: %d", |
| 690 | __func__, code); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 691 | return STATUS_UNKNOWN_TRANSACTION; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Steven Moreland | f183fdd | 2020-10-27 00:12:12 +0000 | [diff] [blame] | 694 | 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] | 695 | if ((flags & ~kAllFlags) != 0) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 696 | ALOGE("%s: Unrecognized flags sent: %d", __func__, flags); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 697 | return STATUS_BAD_VALUE; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | if (binder == nullptr || *in == nullptr || out == nullptr) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 701 | ALOGE("%s: requires non-null parameters binder (%p), in (%p), and out (%p).", __func__, |
| 702 | binder, in, out); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 703 | return STATUS_UNEXPECTED_NULL; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | if ((*in)->getBinder() != binder) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 707 | ALOGE("%s: parcel is associated with binder object %p but called with %p", __func__, binder, |
| 708 | (*in)->getBinder()); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 709 | return STATUS_BAD_VALUE; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | *out = new AParcel(binder); |
| 713 | |
Steven Moreland | f18615b | 2018-09-14 11:43:06 -0700 | [diff] [blame] | 714 | status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags); |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 715 | binder_status_t ret = PruneStatusT(status); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 716 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 717 | if (ret != STATUS_OK) { |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 718 | delete *out; |
| 719 | *out = nullptr; |
| 720 | } |
| 721 | |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 722 | return ret; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 723 | } |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 724 | |
| 725 | AIBinder_DeathRecipient* AIBinder_DeathRecipient_new( |
| 726 | AIBinder_DeathRecipient_onBinderDied onBinderDied) { |
| 727 | if (onBinderDied == nullptr) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 728 | ALOGE("%s: requires non-null onBinderDied parameter.", __func__); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 729 | return nullptr; |
| 730 | } |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 731 | auto ret = new AIBinder_DeathRecipient(onBinderDied); |
| 732 | ret->incStrong(nullptr); |
| 733 | return ret; |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 734 | } |
| 735 | |
Alice Ryhl | ea9d9d2 | 2021-08-27 07:51:30 +0000 | [diff] [blame] | 736 | void AIBinder_DeathRecipient_setOnUnlinked(AIBinder_DeathRecipient* recipient, |
| 737 | AIBinder_DeathRecipient_onBinderUnlinked onUnlinked) { |
| 738 | if (recipient == nullptr) { |
| 739 | return; |
| 740 | } |
| 741 | |
| 742 | recipient->setOnUnlinked(onUnlinked); |
| 743 | } |
| 744 | |
Steven Moreland | 9b80e28 | 2018-09-19 13:57:23 -0700 | [diff] [blame] | 745 | void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) { |
Steven Moreland | 64127ca | 2019-03-13 09:25:44 -0700 | [diff] [blame] | 746 | if (recipient == nullptr) { |
| 747 | return; |
| 748 | } |
| 749 | |
| 750 | recipient->decStrong(nullptr); |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 751 | } |
Steven Moreland | ea14ef2 | 2019-08-20 10:50:08 -0700 | [diff] [blame] | 752 | |
| 753 | binder_status_t AIBinder_getExtension(AIBinder* binder, AIBinder** outExt) { |
| 754 | if (binder == nullptr || outExt == nullptr) { |
| 755 | if (outExt != nullptr) { |
| 756 | *outExt = nullptr; |
| 757 | } |
| 758 | return STATUS_UNEXPECTED_NULL; |
| 759 | } |
| 760 | |
| 761 | sp<IBinder> ext; |
| 762 | status_t res = binder->getBinder()->getExtension(&ext); |
| 763 | |
| 764 | if (res != android::OK) { |
| 765 | *outExt = nullptr; |
| 766 | return PruneStatusT(res); |
| 767 | } |
| 768 | |
| 769 | sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(ext); |
| 770 | if (ret != nullptr) ret->incStrong(binder); |
| 771 | |
| 772 | *outExt = ret.get(); |
| 773 | return STATUS_OK; |
| 774 | } |
| 775 | |
| 776 | binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) { |
| 777 | if (binder == nullptr || ext == nullptr) { |
| 778 | return STATUS_UNEXPECTED_NULL; |
| 779 | } |
| 780 | |
| 781 | ABBinder* rawBinder = binder->asABBinder(); |
| 782 | if (rawBinder == nullptr) { |
| 783 | return STATUS_INVALID_OPERATION; |
| 784 | } |
| 785 | |
| 786 | rawBinder->setExtension(ext->getBinder()); |
| 787 | return STATUS_OK; |
| 788 | } |
Steven Moreland | 2f405f5 | 2020-07-08 22:24:29 +0000 | [diff] [blame] | 789 | |
| 790 | // platform methods follow |
| 791 | |
| 792 | void AIBinder_setRequestingSid(AIBinder* binder, bool requestingSid) { |
| 793 | ABBinder* localBinder = binder->asABBinder(); |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 794 | LOG_ALWAYS_FATAL_IF(localBinder == nullptr, |
| 795 | "AIBinder_setRequestingSid must be called on a local binder"); |
Steven Moreland | 2f405f5 | 2020-07-08 22:24:29 +0000 | [diff] [blame] | 796 | |
| 797 | localBinder->setRequestingSid(requestingSid); |
| 798 | } |
| 799 | |
| 800 | const char* AIBinder_getCallingSid() { |
| 801 | return ::android::IPCThreadState::self()->getCallingSid(); |
| 802 | } |
Steven Moreland | b2de953 | 2020-05-29 21:44:41 +0000 | [diff] [blame] | 803 | |
Ady Abraham | 96174ac | 2021-10-15 11:02:03 -0700 | [diff] [blame] | 804 | void AIBinder_setMinSchedulerPolicy(AIBinder* binder, int policy, int priority) { |
| 805 | binder->asABBinder()->setMinSchedulerPolicy(policy, priority); |
| 806 | } |
Steven Moreland | 454a26a | 2021-12-14 01:26:21 +0000 | [diff] [blame] | 807 | |
| 808 | void AIBinder_setInheritRt(AIBinder* binder, bool inheritRt) { |
| 809 | ABBinder* localBinder = binder->asABBinder(); |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame^] | 810 | LOG_ALWAYS_FATAL_IF(localBinder == nullptr, |
| 811 | "AIBinder_setInheritRt must be called on a local binder"); |
Steven Moreland | 454a26a | 2021-12-14 01:26:21 +0000 | [diff] [blame] | 812 | |
| 813 | localBinder->setInheritRt(inheritRt); |
| 814 | } |