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