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