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