blob: 6995e776671439cd361abdbab75fd6408bf6e777 [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
Steven Moreland71cddc32018-08-30 23:39:22 -070076bool AIBinder::associateClass(const AIBinder_Class* clazz) {
Steven Moreland71cddc32018-08-30 23:39:22 -070077 if (clazz == nullptr) return false;
78 if (mClazz == clazz) return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -070079
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 Moreland71cddc32018-08-30 23:39:22 -070088 } 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 Moreland2e87adc2018-08-20 19:47:00 -070094 }
95
Steven Moreland71cddc32018-08-30 23:39:22 -070096 // always a failure because we know mClazz != clazz
97 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -070098 }
99
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700100 CHECK(asABpBinder() != nullptr); // ABBinder always has a descriptor
Steven Moreland71cddc32018-08-30 23:39:22 -0700101
Steven Moreland2e87adc2018-08-20 19:47:00 -0700102 String8 descriptor(getBinder()->getInterfaceDescriptor());
103 if (descriptor != newDescriptor) {
Steven Moreland640c4092020-07-08 00:19:58 +0000104 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 Moreland71cddc32018-08-30 23:39:22 -0700112 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700113 }
114
Steven Moreland71cddc32018-08-30 23:39:22 -0700115 // if this is a local object, it's not one known to libbinder_ndk
Steven Moreland2e87adc2018-08-20 19:47:00 -0700116 mClazz = clazz;
117
Steven Moreland71cddc32018-08-30 23:39:22 -0700118 return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700119}
120
121ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700122 : AIBinder(clazz), BBinder(), mUserData(userData) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700123 CHECK(clazz != nullptr);
124}
125ABBinder::~ABBinder() {
126 getClass()->onDestroy(mUserData);
127}
128
129const String16& ABBinder::getInterfaceDescriptor() const {
130 return getClass()->getInterfaceDescriptor();
131}
132
Steven Morelanda194c452019-03-04 16:47:07 -0800133status_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 Moreland5d62e442018-09-13 15:01:02 -0700160status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
161 binder_flags_t flags) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700162 if (isUserCommand(code)) {
163 if (!data.checkInterface(this)) {
Steven Moreland9d089af2018-09-18 08:58:09 -0700164 return STATUS_BAD_TYPE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700165 }
166
167 const AParcel in = AParcel::readOnly(this, &data);
168 AParcel out = AParcel(this, reply, false /*owns*/);
169
Steven Moreland5d62e442018-09-13 15:01:02 -0700170 binder_status_t status = getClass()->onTransact(this, code, &in, &out);
171 return PruneStatusT(status);
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800172 } else if (code == SHELL_COMMAND_TRANSACTION) {
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 Moreland2e87adc2018-08-20 19:47:00 -0700211 } else {
212 return BBinder::onTransact(code, data, reply, flags);
213 }
214}
215
Steven Moreland71cddc32018-08-30 23:39:22 -0700216ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700217 : AIBinder(nullptr /*clazz*/), BpRefBase(binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700218 CHECK(binder != nullptr);
219}
220ABpBinder::~ABpBinder() {}
221
Steven Moreland94968952018-09-05 14:42:59 -0700222void 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
240sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700241 if (binder == nullptr) {
242 return nullptr;
243 }
244 if (ABBinderTag::has(binder)) {
245 return static_cast<ABBinder*>(binder.get());
246 }
Steven Moreland94968952018-09-05 14:42:59 -0700247
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 Moreland71cddc32018-08-30 23:39:22 -0700267}
268
Steven Moreland2e87adc2018-08-20 19:47:00 -0700269struct AIBinder_Weak {
270 wp<AIBinder> binder;
271};
272AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700273 if (binder == nullptr) {
274 return nullptr;
275 }
276
Steven Moreland2e87adc2018-08-20 19:47:00 -0700277 return new AIBinder_Weak{wp<AIBinder>(binder)};
278}
Steven Moreland9b80e282018-09-19 13:57:23 -0700279void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
280 delete weakBinder;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700281}
282AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700283 if (weakBinder == nullptr) {
284 return nullptr;
285 }
286
Steven Moreland2e87adc2018-08-20 19:47:00 -0700287 sp<AIBinder> binder = weakBinder->binder.promote();
288 AIBinder_incStrong(binder.get());
289 return binder.get();
290}
291
292AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
293 AIBinder_Class_onDestroy onDestroy,
294 AIBinder_Class_onTransact onTransact)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700295 : onCreate(onCreate),
296 onDestroy(onDestroy),
297 onTransact(onTransact),
298 mInterfaceDescriptor(interfaceDescriptor) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700299
300AIBinder_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 Morelanda194c452019-03-04 16:47:07 -0800312void 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 Rastogicc7a7462020-01-31 14:29:15 -0800319void 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 Moreland901c7452018-09-04 16:17:28 -0700326void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
327 CHECK(who == mWho);
328
329 mOnDied(mCookie);
Steven Moreland64127ca2019-03-13 09:25:44 -0700330
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 Moreland901c7452018-09-04 16:17:28 -0700342 mWho = nullptr;
343}
344
345AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700346 : mOnDied(onDied) {
Steven Moreland901c7452018-09-04 16:17:28 -0700347 CHECK(onDied != nullptr);
348}
349
Steven Moreland64127ca2019-03-13 09:25:44 -0700350void 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
358binder_status_t AIBinder_DeathRecipient::linkToDeath(sp<IBinder> binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700359 CHECK(binder != nullptr);
360
361 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
362
363 sp<TransferDeathRecipient> recipient =
Steven Moreland64127ca2019-03-13 09:25:44 -0700364 new TransferDeathRecipient(binder, cookie, this, mOnDied);
Steven Moreland901c7452018-09-04 16:17:28 -0700365
Steven Moreland64127ca2019-03-13 09:25:44 -0700366 status_t status = binder->linkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700367 if (status != STATUS_OK) {
368 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700369 }
370
371 mDeathRecipients.push_back(recipient);
Steven Moreland64127ca2019-03-13 09:25:44 -0700372
373 pruneDeadTransferEntriesLocked();
Steven Moreland5d62e442018-09-13 15:01:02 -0700374 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700375}
376
Steven Moreland64127ca2019-03-13 09:25:44 -0700377binder_status_t AIBinder_DeathRecipient::unlinkToDeath(sp<IBinder> binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700378 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 Moreland64127ca2019-03-13 09:25:44 -0700385 if (recipient->getCookie() == cookie && recipient->getWho() == binder) {
Steven Moreland901c7452018-09-04 16:17:28 -0700386 mDeathRecipients.erase(it.base() - 1);
387
Steven Moreland64127ca2019-03-13 09:25:44 -0700388 status_t status = binder->unlinkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700389 if (status != ::android::OK) {
Steven Moreland901c7452018-09-04 16:17:28 -0700390 LOG(ERROR) << __func__
391 << ": removed reference to death recipient but unlink failed.";
392 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700393 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700394 }
395 }
396
Steven Moreland5d62e442018-09-13 15:01:02 -0700397 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700398}
399
400// start of C-API methods
401
Steven Moreland2e87adc2018-08-20 19:47:00 -0700402AIBinder* 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 Moreland71cddc32018-08-30 23:39:22 -0700410 sp<AIBinder> ret = new ABBinder(clazz, userData);
411 ABBinderTag::attach(ret->getBinder());
412
413 AIBinder_incStrong(ret.get());
414 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700415}
416
Steven Moreland5ea54da2018-09-04 13:29:55 -0700417bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700418 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800419 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700420 }
421
422 return binder->isRemote();
423}
424
Steven Moreland65867d72018-09-04 14:22:26 -0700425bool AIBinder_isAlive(const AIBinder* binder) {
426 if (binder == nullptr) {
427 return false;
428 }
429
430 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
431}
432
433binder_status_t AIBinder_ping(AIBinder* binder) {
434 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700435 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700436 }
437
Steven Moreland5d62e442018-09-13 15:01:02 -0700438 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700439}
440
Steven Morelanda194c452019-03-04 16:47:07 -0800441binder_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 Moreland901c7452018-09-04 16:17:28 -0700465binder_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 Moreland5d62e442018-09-13 15:01:02 -0700469 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700470 }
471
Steven Moreland5d62e442018-09-13 15:01:02 -0700472 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700473 return recipient->linkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700474}
475
476binder_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 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->unlinkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700485}
486
Steven Morelandf3034b02018-11-12 17:37:46 -0800487uid_t AIBinder_getCallingUid() {
488 return ::android::IPCThreadState::self()->getCallingUid();
489}
490
491pid_t AIBinder_getCallingPid() {
492 return ::android::IPCThreadState::self()->getCallingPid();
493}
494
Steven Moreland2e87adc2018-08-20 19:47:00 -0700495void AIBinder_incStrong(AIBinder* binder) {
496 if (binder == nullptr) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700497 return;
498 }
499
500 binder->incStrong(nullptr);
501}
502void AIBinder_decStrong(AIBinder* binder) {
503 if (binder == nullptr) {
504 LOG(ERROR) << __func__ << ": on null binder";
505 return;
506 }
507
508 binder->decStrong(nullptr);
509}
510int32_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 Moreland71cddc32018-08-30 23:39:22 -0700519bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
520 if (binder == nullptr) {
521 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700522 }
523
Steven Moreland71cddc32018-08-30 23:39:22 -0700524 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700525}
526
527const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
528 if (binder == nullptr) {
529 return nullptr;
530 }
531
532 return binder->getClass();
533}
534
535void* 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
548binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
549 if (binder == nullptr || in == nullptr) {
550 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700551 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700552 }
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 Moreland5d62e442018-09-13 15:01:02 -0700558 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700559 }
560
Steven Moreland3527c2e2018-09-05 17:07:14 -0700561 if (!binder->isRemote()) {
Steven Moreland74521c82018-09-07 14:50:40 -0700562 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 Moreland3527c2e2018-09-05 17:07:14 -0700566 }
567
Steven Moreland2e87adc2018-08-20 19:47:00 -0700568 *in = new AParcel(binder);
Steven Morelandf18615b2018-09-14 11:43:06 -0700569 status_t status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
Steven Moreland5d62e442018-09-13 15:01:02 -0700570 binder_status_t ret = PruneStatusT(status);
571
572 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700573 delete *in;
574 *in = nullptr;
575 }
576
Steven Moreland5d62e442018-09-13 15:01:02 -0700577 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700578}
579
Steven Moreland9b80e282018-09-19 13:57:23 -0700580static void DestroyParcel(AParcel** parcel) {
581 delete *parcel;
582 *parcel = nullptr;
583}
584
Steven Moreland2e87adc2018-08-20 19:47:00 -0700585binder_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 Moreland5d62e442018-09-13 15:01:02 -0700589 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700590 }
591
Steven Morelandcaa776c2018-09-04 13:48:11 -0700592 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700593 // This object is the input to the transaction. This function takes ownership of it and deletes
594 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700595 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700596
597 if (!isUserCommand(code)) {
598 LOG(ERROR) << __func__ << ": Only user-defined transactions can be made from the NDK.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700599 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700600 }
601
Steven Moreland46b5fea2019-10-15 11:22:18 -0700602 constexpr binder_flags_t kAllFlags = FLAG_PRIVATE_VENDOR | FLAG_ONEWAY;
603 if ((flags & ~kAllFlags) != 0) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700604 LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags;
Steven Moreland5d62e442018-09-13 15:01:02 -0700605 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700606 }
607
608 if (binder == nullptr || *in == nullptr || out == nullptr) {
609 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700610 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700611 }
612
613 if ((*in)->getBinder() != binder) {
614 LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
615 << " but called with " << (*in)->getBinder();
Steven Moreland5d62e442018-09-13 15:01:02 -0700616 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700617 }
618
619 *out = new AParcel(binder);
620
Steven Morelandf18615b2018-09-14 11:43:06 -0700621 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700622 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700623
Steven Moreland5d62e442018-09-13 15:01:02 -0700624 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700625 delete *out;
626 *out = nullptr;
627 }
628
Steven Moreland5d62e442018-09-13 15:01:02 -0700629 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700630}
Steven Moreland901c7452018-09-04 16:17:28 -0700631
632AIBinder_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 Moreland64127ca2019-03-13 09:25:44 -0700638 auto ret = new AIBinder_DeathRecipient(onBinderDied);
639 ret->incStrong(nullptr);
640 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700641}
642
Steven Moreland9b80e282018-09-19 13:57:23 -0700643void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
Steven Moreland64127ca2019-03-13 09:25:44 -0700644 if (recipient == nullptr) {
645 return;
646 }
647
648 recipient->decStrong(nullptr);
Steven Moreland901c7452018-09-04 16:17:28 -0700649}
Steven Morelandea14ef22019-08-20 10:50:08 -0700650
651binder_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
674binder_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 Moreland2f405f52020-07-08 22:24:29 +0000687
688// platform methods follow
689
690void 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
699const char* AIBinder_getCallingSid() {
700 return ::android::IPCThreadState::self()->getCallingSid();
701}
Steven Morelandb2de9532020-05-29 21:44:41 +0000702
703android::sp<android::IBinder> AIBinder_toPlatformBinder(AIBinder* binder) {
704 if (binder == nullptr) return nullptr;
705 return binder->getBinder();
706}
707
708AIBinder* 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}