blob: 54868be0310f9d43ddbae71ea513ce683942501b [file] [log] [blame]
Steven Moreland2e87adc2018-08-20 19:47:00 -07001/*
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 Danisevskis798a09a2020-08-18 08:35:38 -070017#include <android/binder_context.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070018#include <android/binder_ibinder.h>
Steven Moreland2f405f52020-07-08 22:24:29 +000019#include <android/binder_ibinder_platform.h>
Steven Moreland4d5ad492018-09-13 12:49:16 -070020#include "ibinder_internal.h"
Steven Moreland2e87adc2018-08-20 19:47:00 -070021
Steven Moreland46b5fea2019-10-15 11:22:18 -070022#include <android/binder_stability.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070023#include <android/binder_status.h>
Steven Moreland4d5ad492018-09-13 12:49:16 -070024#include "parcel_internal.h"
Steven Moreland5d62e442018-09-13 15:01:02 -070025#include "status_internal.h"
Steven Moreland2e87adc2018-08-20 19:47:00 -070026
27#include <android-base/logging.h>
Steven Morelandf3034b02018-11-12 17:37:46 -080028#include <binder/IPCThreadState.h>
Ruchir Rastogicc7a7462020-01-31 14:29:15 -080029#include <binder/IResultReceiver.h>
30#include <private/android_filesystem_config.h>
Steven Moreland2e87adc2018-08-20 19:47:00 -070031
Steven Moreland901c7452018-09-04 16:17:28 -070032using DeathRecipient = ::android::IBinder::DeathRecipient;
33
Steven Moreland2e87adc2018-08-20 19:47:00 -070034using ::android::IBinder;
Ruchir Rastogicc7a7462020-01-31 14:29:15 -080035using ::android::IResultReceiver;
Steven Moreland2e87adc2018-08-20 19:47:00 -070036using ::android::Parcel;
37using ::android::sp;
Steven Moreland5d62e442018-09-13 15:01:02 -070038using ::android::status_t;
Steven Moreland2e87adc2018-08-20 19:47:00 -070039using ::android::String16;
Steven Morelanda194c452019-03-04 16:47:07 -080040using ::android::String8;
Steven Moreland2e87adc2018-08-20 19:47:00 -070041using ::android::wp;
42
Steven Moreland71cddc32018-08-30 23:39:22 -070043namespace ABBinderTag {
44
45static const void* kId = "ABBinder";
46static void* kValue = static_cast<void*>(new bool{true});
Steven Moreland94968952018-09-05 14:42:59 -070047void clean(const void* /*id*/, void* /*obj*/, void* /*cookie*/){/* do nothing */};
Steven Moreland71cddc32018-08-30 23:39:22 -070048
49static void attach(const sp<IBinder>& binder) {
Steven Moreland94968952018-09-05 14:42:59 -070050 binder->attachObject(kId, kValue, nullptr /*cookie*/, clean);
Steven Moreland71cddc32018-08-30 23:39:22 -070051}
52static bool has(const sp<IBinder>& binder) {
53 return binder != nullptr && binder->findObject(kId) == kValue;
54}
55
Steven Moreland6cf66ac2018-11-02 18:14:54 -070056} // namespace ABBinderTag
Steven Moreland71cddc32018-08-30 23:39:22 -070057
Steven Moreland94968952018-09-05 14:42:59 -070058namespace ABpBinderTag {
59
60static std::mutex gLock;
61static const void* kId = "ABpBinder";
62struct Value {
63 wp<ABpBinder> binder;
64};
65void 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 Moreland6cf66ac2018-11-02 18:14:54 -070071} // namespace ABpBinderTag
Steven Moreland94968952018-09-05 14:42:59 -070072
Steven Moreland2e87adc2018-08-20 19:47:00 -070073AIBinder::AIBinder(const AIBinder_Class* clazz) : mClazz(clazz) {}
74AIBinder::~AIBinder() {}
75
Andrei Homescu9556bb12020-09-21 16:59:45 -070076std::optional<bool> AIBinder::associateClassInternal(const AIBinder_Class* clazz,
Steven Moreland63872b52020-10-02 19:28:23 +000077 const String16& newDescriptor, bool set) {
Andrei Homescu9556bb12020-09-21 16:59:45 -070078 std::lock_guard<std::mutex> lock(mClazzMutex);
Steven Moreland71cddc32018-08-30 23:39:22 -070079 if (mClazz == clazz) return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -070080
Steven Moreland2e87adc2018-08-20 19:47:00 -070081 if (mClazz != nullptr) {
Steven Moreland63872b52020-10-02 19:28:23 +000082 const String16& currentDescriptor = mClazz->getInterfaceDescriptor();
Steven Moreland2e87adc2018-08-20 19:47:00 -070083 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 Moreland71cddc32018-08-30 23:39:22 -070087 } else {
88 LOG(ERROR) << __func__
89 << ": Class cannot be associated on object which already has a class. "
90 "Trying to associate to '"
Steven Moreland63872b52020-10-02 19:28:23 +000091 << newDescriptor << "' but already set to '" << currentDescriptor << "'.";
Steven Moreland2e87adc2018-08-20 19:47:00 -070092 }
93
Steven Moreland71cddc32018-08-30 23:39:22 -070094 // always a failure because we know mClazz != clazz
95 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -070096 }
97
Andrei Homescu9556bb12020-09-21 16:59:45 -070098 if (set) {
99 // if this is a local object, it's not one known to libbinder_ndk
100 mClazz = clazz;
101 }
102
103 return {};
104}
105
106bool AIBinder::associateClass(const AIBinder_Class* clazz) {
107 if (clazz == nullptr) return false;
108
Steven Moreland63872b52020-10-02 19:28:23 +0000109 const String16& newDescriptor = clazz->getInterfaceDescriptor();
Andrei Homescu9556bb12020-09-21 16:59:45 -0700110
111 auto result = associateClassInternal(clazz, newDescriptor, false);
112 if (result.has_value()) return *result;
113
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700114 CHECK(asABpBinder() != nullptr); // ABBinder always has a descriptor
Steven Moreland71cddc32018-08-30 23:39:22 -0700115
Steven Moreland63872b52020-10-02 19:28:23 +0000116 const String16& descriptor = getBinder()->getInterfaceDescriptor();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700117 if (descriptor != newDescriptor) {
Steven Moreland640c4092020-07-08 00:19:58 +0000118 if (getBinder()->isBinderAlive()) {
Steven Moreland63872b52020-10-02 19:28:23 +0000119 LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor
120 << "' but descriptor is actually '" << descriptor << "'.";
Steven Moreland640c4092020-07-08 00:19:58 +0000121 } else {
122 // b/155793159
Steven Moreland63872b52020-10-02 19:28:23 +0000123 LOG(ERROR) << __func__ << ": Cannot associate class '" << newDescriptor
Steven Moreland640c4092020-07-08 00:19:58 +0000124 << "' to dead binder.";
125 }
Steven Moreland71cddc32018-08-30 23:39:22 -0700126 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700127 }
128
Andrei Homescu9556bb12020-09-21 16:59:45 -0700129 return associateClassInternal(clazz, newDescriptor, true).value_or(true);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700130}
131
132ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700133 : AIBinder(clazz), BBinder(), mUserData(userData) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700134 CHECK(clazz != nullptr);
135}
136ABBinder::~ABBinder() {
137 getClass()->onDestroy(mUserData);
138}
139
140const String16& ABBinder::getInterfaceDescriptor() const {
141 return getClass()->getInterfaceDescriptor();
142}
143
Steven Morelanda194c452019-03-04 16:47:07 -0800144status_t ABBinder::dump(int fd, const ::android::Vector<String16>& args) {
145 AIBinder_onDump onDump = getClass()->onDump;
146
147 if (onDump == nullptr) {
148 return STATUS_OK;
149 }
150
151 // technically UINT32_MAX would be okay here, but INT32_MAX is expected since this may be
152 // null in Java
153 if (args.size() > INT32_MAX) {
154 LOG(ERROR) << "ABBinder::dump received too many arguments: " << args.size();
155 return STATUS_BAD_VALUE;
156 }
157
158 std::vector<String8> utf8Args; // owns memory of utf8s
159 utf8Args.reserve(args.size());
160 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
161 utf8Pointers.reserve(args.size());
162
163 for (size_t i = 0; i < args.size(); i++) {
164 utf8Args.push_back(String8(args[i]));
165 utf8Pointers.push_back(utf8Args[i].c_str());
166 }
167
168 return onDump(this, fd, utf8Pointers.data(), utf8Pointers.size());
169}
170
Steven Moreland5d62e442018-09-13 15:01:02 -0700171status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
172 binder_flags_t flags) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700173 if (isUserCommand(code)) {
174 if (!data.checkInterface(this)) {
Steven Moreland9d089af2018-09-18 08:58:09 -0700175 return STATUS_BAD_TYPE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700176 }
177
178 const AParcel in = AParcel::readOnly(this, &data);
179 AParcel out = AParcel(this, reply, false /*owns*/);
180
Steven Moreland5d62e442018-09-13 15:01:02 -0700181 binder_status_t status = getClass()->onTransact(this, code, &in, &out);
182 return PruneStatusT(status);
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800183 } else if (code == SHELL_COMMAND_TRANSACTION) {
184 int in = data.readFileDescriptor();
185 int out = data.readFileDescriptor();
186 int err = data.readFileDescriptor();
187
188 int argc = data.readInt32();
189 std::vector<String8> utf8Args; // owns memory of utf8s
190 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
191 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
192 utf8Args.push_back(String8(data.readString16()));
193 utf8Pointers.push_back(utf8Args[i].c_str());
194 }
195
196 data.readStrongBinder(); // skip over the IShellCallback
197 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(data.readStrongBinder());
198
199 // Shell commands should only be callable by ADB.
200 uid_t uid = AIBinder_getCallingUid();
201 if (uid != AID_ROOT && uid != AID_SHELL) {
202 if (resultReceiver != nullptr) {
203 resultReceiver->send(-1);
204 }
205 return STATUS_PERMISSION_DENIED;
206 }
207
208 // Check that the file descriptors are valid.
209 if (in == STATUS_BAD_TYPE || out == STATUS_BAD_TYPE || err == STATUS_BAD_TYPE) {
210 if (resultReceiver != nullptr) {
211 resultReceiver->send(-1);
212 }
213 return STATUS_BAD_VALUE;
214 }
215
216 binder_status_t status = getClass()->handleShellCommand(
217 this, in, out, err, utf8Pointers.data(), utf8Pointers.size());
218 if (resultReceiver != nullptr) {
219 resultReceiver->send(status);
220 }
221 return status;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700222 } else {
223 return BBinder::onTransact(code, data, reply, flags);
224 }
225}
226
Steven Moreland71cddc32018-08-30 23:39:22 -0700227ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700228 : AIBinder(nullptr /*clazz*/), BpRefBase(binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700229 CHECK(binder != nullptr);
230}
231ABpBinder::~ABpBinder() {}
232
Steven Moreland94968952018-09-05 14:42:59 -0700233void ABpBinder::onLastStrongRef(const void* id) {
234 {
235 std::lock_guard<std::mutex> lock(ABpBinderTag::gLock);
236 // Since ABpBinder is OBJECT_LIFETIME_WEAK, we must remove this weak reference in order for
237 // the ABpBinder to be deleted. Since a strong reference to this ABpBinder object should no
238 // longer be able to exist at the time of this method call, there is no longer a need to
239 // recover it.
240
241 ABpBinderTag::Value* value =
242 static_cast<ABpBinderTag::Value*>(remote()->findObject(ABpBinderTag::kId));
243 if (value != nullptr) {
244 value->binder = nullptr;
245 }
246 }
247
248 BpRefBase::onLastStrongRef(id);
249}
250
251sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700252 if (binder == nullptr) {
253 return nullptr;
254 }
255 if (ABBinderTag::has(binder)) {
256 return static_cast<ABBinder*>(binder.get());
257 }
Steven Moreland94968952018-09-05 14:42:59 -0700258
259 // The following code ensures that for a given binder object (remote or local), if it is not an
260 // ABBinder then at most one ABpBinder object exists in a given process representing it.
261 std::lock_guard<std::mutex> lock(ABpBinderTag::gLock);
262
263 ABpBinderTag::Value* value =
264 static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId));
265 if (value == nullptr) {
266 value = new ABpBinderTag::Value;
267 binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value), nullptr /*cookie*/,
268 ABpBinderTag::clean);
269 }
270
271 sp<ABpBinder> ret = value->binder.promote();
272 if (ret == nullptr) {
273 ret = new ABpBinder(binder);
274 value->binder = ret;
275 }
276
277 return ret;
Steven Moreland71cddc32018-08-30 23:39:22 -0700278}
279
Steven Moreland2e87adc2018-08-20 19:47:00 -0700280struct AIBinder_Weak {
281 wp<AIBinder> binder;
282};
283AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700284 if (binder == nullptr) {
285 return nullptr;
286 }
287
Steven Moreland2e87adc2018-08-20 19:47:00 -0700288 return new AIBinder_Weak{wp<AIBinder>(binder)};
289}
Steven Moreland9b80e282018-09-19 13:57:23 -0700290void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
291 delete weakBinder;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700292}
293AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700294 if (weakBinder == nullptr) {
295 return nullptr;
296 }
297
Steven Moreland2e87adc2018-08-20 19:47:00 -0700298 sp<AIBinder> binder = weakBinder->binder.promote();
299 AIBinder_incStrong(binder.get());
300 return binder.get();
301}
302
303AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
304 AIBinder_Class_onDestroy onDestroy,
305 AIBinder_Class_onTransact onTransact)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700306 : onCreate(onCreate),
307 onDestroy(onDestroy),
308 onTransact(onTransact),
309 mInterfaceDescriptor(interfaceDescriptor) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700310
311AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
312 AIBinder_Class_onCreate onCreate,
313 AIBinder_Class_onDestroy onDestroy,
314 AIBinder_Class_onTransact onTransact) {
315 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
316 onTransact == nullptr) {
317 return nullptr;
318 }
319
320 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
321}
322
Steven Morelanda194c452019-03-04 16:47:07 -0800323void AIBinder_Class_setOnDump(AIBinder_Class* clazz, AIBinder_onDump onDump) {
324 CHECK(clazz != nullptr) << "setOnDump requires non-null clazz";
325
326 // this is required to be called before instances are instantiated
327 clazz->onDump = onDump;
328}
329
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800330void AIBinder_Class_setHandleShellCommand(AIBinder_Class* clazz,
331 AIBinder_handleShellCommand handleShellCommand) {
332 CHECK(clazz != nullptr) << "setHandleShellCommand requires non-null clazz";
333
334 clazz->handleShellCommand = handleShellCommand;
335}
336
Steven Moreland901c7452018-09-04 16:17:28 -0700337void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
338 CHECK(who == mWho);
339
340 mOnDied(mCookie);
Steven Moreland64127ca2019-03-13 09:25:44 -0700341
342 sp<AIBinder_DeathRecipient> recipient = mParentRecipient.promote();
343 sp<IBinder> strongWho = who.promote();
344
345 // otherwise this will be cleaned up later with pruneDeadTransferEntriesLocked
346 if (recipient != nullptr && strongWho != nullptr) {
347 status_t result = recipient->unlinkToDeath(strongWho, mCookie);
348 if (result != ::android::DEAD_OBJECT) {
349 LOG(WARNING) << "Unlinking to dead binder resulted in: " << result;
350 }
351 }
352
Steven Moreland901c7452018-09-04 16:17:28 -0700353 mWho = nullptr;
354}
355
356AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700357 : mOnDied(onDied) {
Steven Moreland901c7452018-09-04 16:17:28 -0700358 CHECK(onDied != nullptr);
359}
360
Steven Moreland64127ca2019-03-13 09:25:44 -0700361void AIBinder_DeathRecipient::pruneDeadTransferEntriesLocked() {
362 mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(),
363 [](const sp<TransferDeathRecipient>& tdr) {
364 return tdr->getWho() == nullptr;
365 }),
366 mDeathRecipients.end());
367}
368
369binder_status_t AIBinder_DeathRecipient::linkToDeath(sp<IBinder> binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700370 CHECK(binder != nullptr);
371
372 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
373
374 sp<TransferDeathRecipient> recipient =
Steven Moreland64127ca2019-03-13 09:25:44 -0700375 new TransferDeathRecipient(binder, cookie, this, mOnDied);
Steven Moreland901c7452018-09-04 16:17:28 -0700376
Steven Moreland64127ca2019-03-13 09:25:44 -0700377 status_t status = binder->linkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700378 if (status != STATUS_OK) {
379 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700380 }
381
382 mDeathRecipients.push_back(recipient);
Steven Moreland64127ca2019-03-13 09:25:44 -0700383
384 pruneDeadTransferEntriesLocked();
Steven Moreland5d62e442018-09-13 15:01:02 -0700385 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700386}
387
Steven Moreland64127ca2019-03-13 09:25:44 -0700388binder_status_t AIBinder_DeathRecipient::unlinkToDeath(sp<IBinder> binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700389 CHECK(binder != nullptr);
390
391 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
392
393 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
394 sp<TransferDeathRecipient> recipient = *it;
395
Steven Moreland64127ca2019-03-13 09:25:44 -0700396 if (recipient->getCookie() == cookie && recipient->getWho() == binder) {
Steven Moreland901c7452018-09-04 16:17:28 -0700397 mDeathRecipients.erase(it.base() - 1);
398
Steven Moreland64127ca2019-03-13 09:25:44 -0700399 status_t status = binder->unlinkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700400 if (status != ::android::OK) {
Steven Moreland901c7452018-09-04 16:17:28 -0700401 LOG(ERROR) << __func__
402 << ": removed reference to death recipient but unlink failed.";
403 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700404 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700405 }
406 }
407
Steven Moreland5d62e442018-09-13 15:01:02 -0700408 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700409}
410
411// start of C-API methods
412
Steven Moreland2e87adc2018-08-20 19:47:00 -0700413AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
414 if (clazz == nullptr) {
415 LOG(ERROR) << __func__ << ": Must provide class to construct local binder.";
416 return nullptr;
417 }
418
419 void* userData = clazz->onCreate(args);
420
Steven Moreland71cddc32018-08-30 23:39:22 -0700421 sp<AIBinder> ret = new ABBinder(clazz, userData);
422 ABBinderTag::attach(ret->getBinder());
423
424 AIBinder_incStrong(ret.get());
425 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700426}
427
Steven Moreland5ea54da2018-09-04 13:29:55 -0700428bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700429 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800430 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700431 }
432
433 return binder->isRemote();
434}
435
Steven Moreland65867d72018-09-04 14:22:26 -0700436bool AIBinder_isAlive(const AIBinder* binder) {
437 if (binder == nullptr) {
438 return false;
439 }
440
441 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
442}
443
444binder_status_t AIBinder_ping(AIBinder* binder) {
445 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700446 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700447 }
448
Steven Moreland5d62e442018-09-13 15:01:02 -0700449 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700450}
451
Steven Morelanda194c452019-03-04 16:47:07 -0800452binder_status_t AIBinder_dump(AIBinder* binder, int fd, const char** args, uint32_t numArgs) {
453 if (binder == nullptr) {
454 return STATUS_UNEXPECTED_NULL;
455 }
456
457 ABBinder* bBinder = binder->asABBinder();
458 if (bBinder != nullptr) {
459 AIBinder_onDump onDump = binder->getClass()->onDump;
460 if (onDump == nullptr) {
461 return STATUS_OK;
462 }
463 return PruneStatusT(onDump(bBinder, fd, args, numArgs));
464 }
465
466 ::android::Vector<String16> utf16Args;
467 utf16Args.setCapacity(numArgs);
468 for (uint32_t i = 0; i < numArgs; i++) {
469 utf16Args.push(String16(String8(args[i])));
470 }
471
472 status_t status = binder->getBinder()->dump(fd, utf16Args);
473 return PruneStatusT(status);
474}
475
Steven Moreland901c7452018-09-04 16:17:28 -0700476binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
477 void* cookie) {
478 if (binder == nullptr || recipient == nullptr) {
479 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700480 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700481 }
482
Steven Moreland5d62e442018-09-13 15:01:02 -0700483 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700484 return recipient->linkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700485}
486
487binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
488 void* cookie) {
489 if (binder == nullptr || recipient == nullptr) {
490 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700491 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700492 }
493
Steven Moreland5d62e442018-09-13 15:01:02 -0700494 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700495 return recipient->unlinkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700496}
497
Steven Morelandf3034b02018-11-12 17:37:46 -0800498uid_t AIBinder_getCallingUid() {
499 return ::android::IPCThreadState::self()->getCallingUid();
500}
501
502pid_t AIBinder_getCallingPid() {
503 return ::android::IPCThreadState::self()->getCallingPid();
504}
505
Steven Moreland2e87adc2018-08-20 19:47:00 -0700506void AIBinder_incStrong(AIBinder* binder) {
507 if (binder == nullptr) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700508 return;
509 }
510
511 binder->incStrong(nullptr);
512}
513void AIBinder_decStrong(AIBinder* binder) {
514 if (binder == nullptr) {
515 LOG(ERROR) << __func__ << ": on null binder";
516 return;
517 }
518
519 binder->decStrong(nullptr);
520}
521int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
522 if (binder == nullptr) {
523 LOG(ERROR) << __func__ << ": on null binder";
524 return -1;
525 }
526
527 return binder->getStrongCount();
528}
529
Steven Moreland71cddc32018-08-30 23:39:22 -0700530bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
531 if (binder == nullptr) {
532 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700533 }
534
Steven Moreland71cddc32018-08-30 23:39:22 -0700535 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700536}
537
538const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
539 if (binder == nullptr) {
540 return nullptr;
541 }
542
543 return binder->getClass();
544}
545
546void* AIBinder_getUserData(AIBinder* binder) {
547 if (binder == nullptr) {
548 return nullptr;
549 }
550
551 ABBinder* bBinder = binder->asABBinder();
552 if (bBinder == nullptr) {
553 return nullptr;
554 }
555
556 return bBinder->getUserData();
557}
558
559binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
560 if (binder == nullptr || in == nullptr) {
561 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700562 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700563 }
564 const AIBinder_Class* clazz = binder->getClass();
565 if (clazz == nullptr) {
566 LOG(ERROR) << __func__
567 << ": Class must be defined for a remote binder transaction. See "
568 "AIBinder_associateClass.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700569 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700570 }
571
Steven Moreland3527c2e2018-09-05 17:07:14 -0700572 if (!binder->isRemote()) {
Steven Moreland74521c82018-09-07 14:50:40 -0700573 LOG(WARNING) << "A binder object at " << binder
574 << " is being transacted on, however, this object is in the same process as "
575 "its proxy. Transacting with this binder is expensive compared to just "
576 "calling the corresponding functionality in the same process.";
Steven Moreland3527c2e2018-09-05 17:07:14 -0700577 }
578
Steven Moreland2e87adc2018-08-20 19:47:00 -0700579 *in = new AParcel(binder);
Steven Morelandf18615b2018-09-14 11:43:06 -0700580 status_t status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
Steven Moreland5d62e442018-09-13 15:01:02 -0700581 binder_status_t ret = PruneStatusT(status);
582
583 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700584 delete *in;
585 *in = nullptr;
586 }
587
Steven Moreland5d62e442018-09-13 15:01:02 -0700588 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700589}
590
Steven Moreland9b80e282018-09-19 13:57:23 -0700591static void DestroyParcel(AParcel** parcel) {
592 delete *parcel;
593 *parcel = nullptr;
594}
595
Steven Moreland2e87adc2018-08-20 19:47:00 -0700596binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
597 AParcel** out, binder_flags_t flags) {
598 if (in == nullptr) {
599 LOG(ERROR) << __func__ << ": requires non-null in parameter";
Steven Moreland5d62e442018-09-13 15:01:02 -0700600 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700601 }
602
Steven Morelandcaa776c2018-09-04 13:48:11 -0700603 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700604 // This object is the input to the transaction. This function takes ownership of it and deletes
605 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700606 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700607
608 if (!isUserCommand(code)) {
609 LOG(ERROR) << __func__ << ": Only user-defined transactions can be made from the NDK.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700610 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700611 }
612
Steven Moreland46b5fea2019-10-15 11:22:18 -0700613 constexpr binder_flags_t kAllFlags = FLAG_PRIVATE_VENDOR | FLAG_ONEWAY;
614 if ((flags & ~kAllFlags) != 0) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700615 LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags;
Steven Moreland5d62e442018-09-13 15:01:02 -0700616 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700617 }
618
619 if (binder == nullptr || *in == nullptr || out == nullptr) {
620 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700621 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700622 }
623
624 if ((*in)->getBinder() != binder) {
625 LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
626 << " but called with " << (*in)->getBinder();
Steven Moreland5d62e442018-09-13 15:01:02 -0700627 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700628 }
629
630 *out = new AParcel(binder);
631
Steven Morelandf18615b2018-09-14 11:43:06 -0700632 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700633 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700634
Steven Moreland5d62e442018-09-13 15:01:02 -0700635 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700636 delete *out;
637 *out = nullptr;
638 }
639
Steven Moreland5d62e442018-09-13 15:01:02 -0700640 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700641}
Steven Moreland901c7452018-09-04 16:17:28 -0700642
643AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
644 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
645 if (onBinderDied == nullptr) {
646 LOG(ERROR) << __func__ << ": requires non-null onBinderDied parameter.";
647 return nullptr;
648 }
Steven Moreland64127ca2019-03-13 09:25:44 -0700649 auto ret = new AIBinder_DeathRecipient(onBinderDied);
650 ret->incStrong(nullptr);
651 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700652}
653
Steven Moreland9b80e282018-09-19 13:57:23 -0700654void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
Steven Moreland64127ca2019-03-13 09:25:44 -0700655 if (recipient == nullptr) {
656 return;
657 }
658
659 recipient->decStrong(nullptr);
Steven Moreland901c7452018-09-04 16:17:28 -0700660}
Steven Morelandea14ef22019-08-20 10:50:08 -0700661
662binder_status_t AIBinder_getExtension(AIBinder* binder, AIBinder** outExt) {
663 if (binder == nullptr || outExt == nullptr) {
664 if (outExt != nullptr) {
665 *outExt = nullptr;
666 }
667 return STATUS_UNEXPECTED_NULL;
668 }
669
670 sp<IBinder> ext;
671 status_t res = binder->getBinder()->getExtension(&ext);
672
673 if (res != android::OK) {
674 *outExt = nullptr;
675 return PruneStatusT(res);
676 }
677
678 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(ext);
679 if (ret != nullptr) ret->incStrong(binder);
680
681 *outExt = ret.get();
682 return STATUS_OK;
683}
684
685binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) {
686 if (binder == nullptr || ext == nullptr) {
687 return STATUS_UNEXPECTED_NULL;
688 }
689
690 ABBinder* rawBinder = binder->asABBinder();
691 if (rawBinder == nullptr) {
692 return STATUS_INVALID_OPERATION;
693 }
694
695 rawBinder->setExtension(ext->getBinder());
696 return STATUS_OK;
697}
Steven Moreland2f405f52020-07-08 22:24:29 +0000698
699// platform methods follow
700
701void AIBinder_setRequestingSid(AIBinder* binder, bool requestingSid) {
702 ABBinder* localBinder = binder->asABBinder();
703 if (localBinder == nullptr) {
704 LOG(FATAL) << "AIBinder_setRequestingSid must be called on a local binder";
705 }
706
707 localBinder->setRequestingSid(requestingSid);
708}
709
710const char* AIBinder_getCallingSid() {
711 return ::android::IPCThreadState::self()->getCallingSid();
712}
Steven Morelandb2de9532020-05-29 21:44:41 +0000713
714android::sp<android::IBinder> AIBinder_toPlatformBinder(AIBinder* binder) {
715 if (binder == nullptr) return nullptr;
716 return binder->getBinder();
717}
718
719AIBinder* AIBinder_fromPlatformBinder(const android::sp<android::IBinder>& binder) {
720 sp<AIBinder> ndkBinder = ABpBinder::lookupOrCreateFromBinder(binder);
721 AIBinder_incStrong(ndkBinder.get());
722 return ndkBinder.get();
723}