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