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