blob: d35debc88cfedfaf45b80d999dadc60ec1c9e2cb [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
17#include <android/binder_ibinder.h>
Steven Moreland2f405f52020-07-08 22:24:29 +000018#include <android/binder_ibinder_platform.h>
Steven Moreland8afad292020-11-06 22:26:51 +000019#include <android/binder_libbinder.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;
Steven Moreland9facfce2020-10-02 17:26:56 +0000101 return true;
Andrei Homescu9556bb12020-09-21 16:59:45 -0700102 }
103
104 return {};
105}
106
107bool AIBinder::associateClass(const AIBinder_Class* clazz) {
108 if (clazz == nullptr) return false;
109
Steven Moreland63872b52020-10-02 19:28:23 +0000110 const String16& newDescriptor = clazz->getInterfaceDescriptor();
Andrei Homescu9556bb12020-09-21 16:59:45 -0700111
112 auto result = associateClassInternal(clazz, newDescriptor, false);
113 if (result.has_value()) return *result;
114
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700115 CHECK(asABpBinder() != nullptr); // ABBinder always has a descriptor
Steven Moreland71cddc32018-08-30 23:39:22 -0700116
Steven Moreland63872b52020-10-02 19:28:23 +0000117 const String16& descriptor = getBinder()->getInterfaceDescriptor();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700118 if (descriptor != newDescriptor) {
Steven Moreland640c4092020-07-08 00:19:58 +0000119 if (getBinder()->isBinderAlive()) {
Steven Moreland63872b52020-10-02 19:28:23 +0000120 LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor
121 << "' but descriptor is actually '" << descriptor << "'.";
Steven Moreland640c4092020-07-08 00:19:58 +0000122 } else {
123 // b/155793159
Steven Moreland63872b52020-10-02 19:28:23 +0000124 LOG(ERROR) << __func__ << ": Cannot associate class '" << newDescriptor
Steven Moreland640c4092020-07-08 00:19:58 +0000125 << "' to dead binder.";
126 }
Steven Moreland71cddc32018-08-30 23:39:22 -0700127 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700128 }
129
Steven Moreland9facfce2020-10-02 17:26:56 +0000130 return associateClassInternal(clazz, newDescriptor, true).value();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700131}
132
133ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700134 : AIBinder(clazz), BBinder(), mUserData(userData) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700135 CHECK(clazz != nullptr);
136}
137ABBinder::~ABBinder() {
138 getClass()->onDestroy(mUserData);
139}
140
141const String16& ABBinder::getInterfaceDescriptor() const {
142 return getClass()->getInterfaceDescriptor();
143}
144
Steven Morelanda194c452019-03-04 16:47:07 -0800145status_t ABBinder::dump(int fd, const ::android::Vector<String16>& args) {
146 AIBinder_onDump onDump = getClass()->onDump;
147
148 if (onDump == nullptr) {
149 return STATUS_OK;
150 }
151
152 // technically UINT32_MAX would be okay here, but INT32_MAX is expected since this may be
153 // null in Java
154 if (args.size() > INT32_MAX) {
155 LOG(ERROR) << "ABBinder::dump received too many arguments: " << args.size();
156 return STATUS_BAD_VALUE;
157 }
158
159 std::vector<String8> utf8Args; // owns memory of utf8s
160 utf8Args.reserve(args.size());
161 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
162 utf8Pointers.reserve(args.size());
163
164 for (size_t i = 0; i < args.size(); i++) {
165 utf8Args.push_back(String8(args[i]));
166 utf8Pointers.push_back(utf8Args[i].c_str());
167 }
168
169 return onDump(this, fd, utf8Pointers.data(), utf8Pointers.size());
170}
171
Steven Moreland5d62e442018-09-13 15:01:02 -0700172status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
173 binder_flags_t flags) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700174 if (isUserCommand(code)) {
175 if (!data.checkInterface(this)) {
Steven Moreland9d089af2018-09-18 08:58:09 -0700176 return STATUS_BAD_TYPE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700177 }
178
179 const AParcel in = AParcel::readOnly(this, &data);
180 AParcel out = AParcel(this, reply, false /*owns*/);
181
Steven Moreland5d62e442018-09-13 15:01:02 -0700182 binder_status_t status = getClass()->onTransact(this, code, &in, &out);
183 return PruneStatusT(status);
Steven Moreland10b19f82020-08-06 19:32:45 +0000184 } else if (code == SHELL_COMMAND_TRANSACTION && getClass()->handleShellCommand != nullptr) {
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800185 int in = data.readFileDescriptor();
186 int out = data.readFileDescriptor();
187 int err = data.readFileDescriptor();
188
189 int argc = data.readInt32();
190 std::vector<String8> utf8Args; // owns memory of utf8s
191 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
192 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
193 utf8Args.push_back(String8(data.readString16()));
194 utf8Pointers.push_back(utf8Args[i].c_str());
195 }
196
197 data.readStrongBinder(); // skip over the IShellCallback
198 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(data.readStrongBinder());
199
200 // Shell commands should only be callable by ADB.
201 uid_t uid = AIBinder_getCallingUid();
202 if (uid != AID_ROOT && uid != AID_SHELL) {
203 if (resultReceiver != nullptr) {
204 resultReceiver->send(-1);
205 }
206 return STATUS_PERMISSION_DENIED;
207 }
208
209 // Check that the file descriptors are valid.
210 if (in == STATUS_BAD_TYPE || out == STATUS_BAD_TYPE || err == STATUS_BAD_TYPE) {
211 if (resultReceiver != nullptr) {
212 resultReceiver->send(-1);
213 }
214 return STATUS_BAD_VALUE;
215 }
216
217 binder_status_t status = getClass()->handleShellCommand(
218 this, in, out, err, utf8Pointers.data(), utf8Pointers.size());
219 if (resultReceiver != nullptr) {
220 resultReceiver->send(status);
221 }
222 return status;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700223 } else {
224 return BBinder::onTransact(code, data, reply, flags);
225 }
226}
227
Steven Moreland71cddc32018-08-30 23:39:22 -0700228ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700229 : AIBinder(nullptr /*clazz*/), BpRefBase(binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700230 CHECK(binder != nullptr);
231}
232ABpBinder::~ABpBinder() {}
233
Steven Moreland94968952018-09-05 14:42:59 -0700234void ABpBinder::onLastStrongRef(const void* id) {
235 {
236 std::lock_guard<std::mutex> lock(ABpBinderTag::gLock);
237 // Since ABpBinder is OBJECT_LIFETIME_WEAK, we must remove this weak reference in order for
238 // the ABpBinder to be deleted. Since a strong reference to this ABpBinder object should no
239 // longer be able to exist at the time of this method call, there is no longer a need to
240 // recover it.
241
242 ABpBinderTag::Value* value =
243 static_cast<ABpBinderTag::Value*>(remote()->findObject(ABpBinderTag::kId));
244 if (value != nullptr) {
245 value->binder = nullptr;
246 }
247 }
248
249 BpRefBase::onLastStrongRef(id);
250}
251
252sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700253 if (binder == nullptr) {
254 return nullptr;
255 }
256 if (ABBinderTag::has(binder)) {
257 return static_cast<ABBinder*>(binder.get());
258 }
Steven Moreland94968952018-09-05 14:42:59 -0700259
260 // The following code ensures that for a given binder object (remote or local), if it is not an
261 // ABBinder then at most one ABpBinder object exists in a given process representing it.
262 std::lock_guard<std::mutex> lock(ABpBinderTag::gLock);
263
264 ABpBinderTag::Value* value =
265 static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId));
266 if (value == nullptr) {
267 value = new ABpBinderTag::Value;
268 binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value), nullptr /*cookie*/,
269 ABpBinderTag::clean);
270 }
271
272 sp<ABpBinder> ret = value->binder.promote();
273 if (ret == nullptr) {
274 ret = new ABpBinder(binder);
275 value->binder = ret;
276 }
277
278 return ret;
Steven Moreland71cddc32018-08-30 23:39:22 -0700279}
280
Steven Moreland2e87adc2018-08-20 19:47:00 -0700281struct AIBinder_Weak {
282 wp<AIBinder> binder;
283};
284AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700285 if (binder == nullptr) {
286 return nullptr;
287 }
288
Steven Moreland2e87adc2018-08-20 19:47:00 -0700289 return new AIBinder_Weak{wp<AIBinder>(binder)};
290}
Steven Moreland9b80e282018-09-19 13:57:23 -0700291void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
292 delete weakBinder;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700293}
294AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700295 if (weakBinder == nullptr) {
296 return nullptr;
297 }
298
Steven Moreland2e87adc2018-08-20 19:47:00 -0700299 sp<AIBinder> binder = weakBinder->binder.promote();
300 AIBinder_incStrong(binder.get());
301 return binder.get();
302}
303
304AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
305 AIBinder_Class_onDestroy onDestroy,
306 AIBinder_Class_onTransact onTransact)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700307 : onCreate(onCreate),
308 onDestroy(onDestroy),
309 onTransact(onTransact),
310 mInterfaceDescriptor(interfaceDescriptor) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700311
312AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
313 AIBinder_Class_onCreate onCreate,
314 AIBinder_Class_onDestroy onDestroy,
315 AIBinder_Class_onTransact onTransact) {
316 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
317 onTransact == nullptr) {
318 return nullptr;
319 }
320
321 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
322}
323
Steven Morelanda194c452019-03-04 16:47:07 -0800324void AIBinder_Class_setOnDump(AIBinder_Class* clazz, AIBinder_onDump onDump) {
325 CHECK(clazz != nullptr) << "setOnDump requires non-null clazz";
326
327 // this is required to be called before instances are instantiated
328 clazz->onDump = onDump;
329}
330
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800331void AIBinder_Class_setHandleShellCommand(AIBinder_Class* clazz,
332 AIBinder_handleShellCommand handleShellCommand) {
333 CHECK(clazz != nullptr) << "setHandleShellCommand requires non-null clazz";
334
335 clazz->handleShellCommand = handleShellCommand;
336}
337
Steven Moreland901c7452018-09-04 16:17:28 -0700338void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
339 CHECK(who == mWho);
340
341 mOnDied(mCookie);
Steven Moreland64127ca2019-03-13 09:25:44 -0700342
343 sp<AIBinder_DeathRecipient> recipient = mParentRecipient.promote();
344 sp<IBinder> strongWho = who.promote();
345
346 // otherwise this will be cleaned up later with pruneDeadTransferEntriesLocked
347 if (recipient != nullptr && strongWho != nullptr) {
348 status_t result = recipient->unlinkToDeath(strongWho, mCookie);
349 if (result != ::android::DEAD_OBJECT) {
350 LOG(WARNING) << "Unlinking to dead binder resulted in: " << result;
351 }
352 }
353
Steven Moreland901c7452018-09-04 16:17:28 -0700354 mWho = nullptr;
355}
356
357AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700358 : mOnDied(onDied) {
Steven Moreland901c7452018-09-04 16:17:28 -0700359 CHECK(onDied != nullptr);
360}
361
Steven Moreland64127ca2019-03-13 09:25:44 -0700362void AIBinder_DeathRecipient::pruneDeadTransferEntriesLocked() {
363 mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(),
364 [](const sp<TransferDeathRecipient>& tdr) {
365 return tdr->getWho() == nullptr;
366 }),
367 mDeathRecipients.end());
368}
369
370binder_status_t AIBinder_DeathRecipient::linkToDeath(sp<IBinder> binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700371 CHECK(binder != nullptr);
372
373 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
374
375 sp<TransferDeathRecipient> recipient =
Steven Moreland64127ca2019-03-13 09:25:44 -0700376 new TransferDeathRecipient(binder, cookie, this, mOnDied);
Steven Moreland901c7452018-09-04 16:17:28 -0700377
Steven Moreland64127ca2019-03-13 09:25:44 -0700378 status_t status = binder->linkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700379 if (status != STATUS_OK) {
380 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700381 }
382
383 mDeathRecipients.push_back(recipient);
Steven Moreland64127ca2019-03-13 09:25:44 -0700384
385 pruneDeadTransferEntriesLocked();
Steven Moreland5d62e442018-09-13 15:01:02 -0700386 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700387}
388
Steven Moreland64127ca2019-03-13 09:25:44 -0700389binder_status_t AIBinder_DeathRecipient::unlinkToDeath(sp<IBinder> binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700390 CHECK(binder != nullptr);
391
392 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
393
394 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
395 sp<TransferDeathRecipient> recipient = *it;
396
Steven Moreland64127ca2019-03-13 09:25:44 -0700397 if (recipient->getCookie() == cookie && recipient->getWho() == binder) {
Steven Moreland901c7452018-09-04 16:17:28 -0700398 mDeathRecipients.erase(it.base() - 1);
399
Steven Moreland64127ca2019-03-13 09:25:44 -0700400 status_t status = binder->unlinkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700401 if (status != ::android::OK) {
Steven Moreland901c7452018-09-04 16:17:28 -0700402 LOG(ERROR) << __func__
403 << ": removed reference to death recipient but unlink failed.";
404 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700405 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700406 }
407 }
408
Steven Moreland5d62e442018-09-13 15:01:02 -0700409 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700410}
411
412// start of C-API methods
413
Steven Moreland2e87adc2018-08-20 19:47:00 -0700414AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
415 if (clazz == nullptr) {
416 LOG(ERROR) << __func__ << ": Must provide class to construct local binder.";
417 return nullptr;
418 }
419
420 void* userData = clazz->onCreate(args);
421
Steven Moreland71cddc32018-08-30 23:39:22 -0700422 sp<AIBinder> ret = new ABBinder(clazz, userData);
423 ABBinderTag::attach(ret->getBinder());
424
425 AIBinder_incStrong(ret.get());
426 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700427}
428
Steven Moreland5ea54da2018-09-04 13:29:55 -0700429bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700430 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800431 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700432 }
433
434 return binder->isRemote();
435}
436
Steven Moreland65867d72018-09-04 14:22:26 -0700437bool AIBinder_isAlive(const AIBinder* binder) {
438 if (binder == nullptr) {
439 return false;
440 }
441
442 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
443}
444
445binder_status_t AIBinder_ping(AIBinder* binder) {
446 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700447 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700448 }
449
Steven Moreland5d62e442018-09-13 15:01:02 -0700450 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700451}
452
Steven Morelanda194c452019-03-04 16:47:07 -0800453binder_status_t AIBinder_dump(AIBinder* binder, int fd, const char** args, uint32_t numArgs) {
454 if (binder == nullptr) {
455 return STATUS_UNEXPECTED_NULL;
456 }
457
458 ABBinder* bBinder = binder->asABBinder();
459 if (bBinder != nullptr) {
460 AIBinder_onDump onDump = binder->getClass()->onDump;
461 if (onDump == nullptr) {
462 return STATUS_OK;
463 }
464 return PruneStatusT(onDump(bBinder, fd, args, numArgs));
465 }
466
467 ::android::Vector<String16> utf16Args;
468 utf16Args.setCapacity(numArgs);
469 for (uint32_t i = 0; i < numArgs; i++) {
470 utf16Args.push(String16(String8(args[i])));
471 }
472
473 status_t status = binder->getBinder()->dump(fd, utf16Args);
474 return PruneStatusT(status);
475}
476
Steven Moreland901c7452018-09-04 16:17:28 -0700477binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
478 void* cookie) {
479 if (binder == nullptr || recipient == nullptr) {
480 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700481 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700482 }
483
Steven Moreland5d62e442018-09-13 15:01:02 -0700484 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700485 return recipient->linkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700486}
487
488binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
489 void* cookie) {
490 if (binder == nullptr || recipient == nullptr) {
491 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700492 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700493 }
494
Steven Moreland5d62e442018-09-13 15:01:02 -0700495 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700496 return recipient->unlinkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700497}
498
Steven Morelandf3034b02018-11-12 17:37:46 -0800499uid_t AIBinder_getCallingUid() {
500 return ::android::IPCThreadState::self()->getCallingUid();
501}
502
503pid_t AIBinder_getCallingPid() {
504 return ::android::IPCThreadState::self()->getCallingPid();
505}
506
Steven Moreland2e87adc2018-08-20 19:47:00 -0700507void AIBinder_incStrong(AIBinder* binder) {
508 if (binder == nullptr) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700509 return;
510 }
511
512 binder->incStrong(nullptr);
513}
514void AIBinder_decStrong(AIBinder* binder) {
515 if (binder == nullptr) {
516 LOG(ERROR) << __func__ << ": on null binder";
517 return;
518 }
519
520 binder->decStrong(nullptr);
521}
522int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
523 if (binder == nullptr) {
524 LOG(ERROR) << __func__ << ": on null binder";
525 return -1;
526 }
527
528 return binder->getStrongCount();
529}
530
Steven Moreland71cddc32018-08-30 23:39:22 -0700531bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
532 if (binder == nullptr) {
533 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700534 }
535
Steven Moreland71cddc32018-08-30 23:39:22 -0700536 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700537}
538
539const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
540 if (binder == nullptr) {
541 return nullptr;
542 }
543
544 return binder->getClass();
545}
546
547void* AIBinder_getUserData(AIBinder* binder) {
548 if (binder == nullptr) {
549 return nullptr;
550 }
551
552 ABBinder* bBinder = binder->asABBinder();
553 if (bBinder == nullptr) {
554 return nullptr;
555 }
556
557 return bBinder->getUserData();
558}
559
560binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
561 if (binder == nullptr || in == nullptr) {
562 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700563 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700564 }
565 const AIBinder_Class* clazz = binder->getClass();
566 if (clazz == nullptr) {
567 LOG(ERROR) << __func__
568 << ": Class must be defined for a remote binder transaction. See "
569 "AIBinder_associateClass.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700570 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700571 }
572
Steven Moreland3527c2e2018-09-05 17:07:14 -0700573 if (!binder->isRemote()) {
Steven Moreland74521c82018-09-07 14:50:40 -0700574 LOG(WARNING) << "A binder object at " << binder
575 << " is being transacted on, however, this object is in the same process as "
576 "its proxy. Transacting with this binder is expensive compared to just "
577 "calling the corresponding functionality in the same process.";
Steven Moreland3527c2e2018-09-05 17:07:14 -0700578 }
579
Steven Moreland2e87adc2018-08-20 19:47:00 -0700580 *in = new AParcel(binder);
Steven Morelandf18615b2018-09-14 11:43:06 -0700581 status_t status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
Steven Moreland5d62e442018-09-13 15:01:02 -0700582 binder_status_t ret = PruneStatusT(status);
583
584 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700585 delete *in;
586 *in = nullptr;
587 }
588
Steven Moreland5d62e442018-09-13 15:01:02 -0700589 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700590}
591
Steven Moreland9b80e282018-09-19 13:57:23 -0700592static void DestroyParcel(AParcel** parcel) {
593 delete *parcel;
594 *parcel = nullptr;
595}
596
Steven Moreland2e87adc2018-08-20 19:47:00 -0700597binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
598 AParcel** out, binder_flags_t flags) {
599 if (in == nullptr) {
600 LOG(ERROR) << __func__ << ": requires non-null in parameter";
Steven Moreland5d62e442018-09-13 15:01:02 -0700601 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700602 }
603
Steven Morelandcaa776c2018-09-04 13:48:11 -0700604 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700605 // This object is the input to the transaction. This function takes ownership of it and deletes
606 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700607 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700608
609 if (!isUserCommand(code)) {
610 LOG(ERROR) << __func__ << ": Only user-defined transactions can be made from the NDK.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700611 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700612 }
613
Steven Moreland46b5fea2019-10-15 11:22:18 -0700614 constexpr binder_flags_t kAllFlags = FLAG_PRIVATE_VENDOR | FLAG_ONEWAY;
615 if ((flags & ~kAllFlags) != 0) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700616 LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags;
Steven Moreland5d62e442018-09-13 15:01:02 -0700617 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700618 }
619
620 if (binder == nullptr || *in == nullptr || out == nullptr) {
621 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700622 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700623 }
624
625 if ((*in)->getBinder() != binder) {
626 LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
627 << " but called with " << (*in)->getBinder();
Steven Moreland5d62e442018-09-13 15:01:02 -0700628 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700629 }
630
631 *out = new AParcel(binder);
632
Steven Morelandf18615b2018-09-14 11:43:06 -0700633 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700634 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700635
Steven Moreland5d62e442018-09-13 15:01:02 -0700636 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700637 delete *out;
638 *out = nullptr;
639 }
640
Steven Moreland5d62e442018-09-13 15:01:02 -0700641 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700642}
Steven Moreland901c7452018-09-04 16:17:28 -0700643
644AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
645 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
646 if (onBinderDied == nullptr) {
647 LOG(ERROR) << __func__ << ": requires non-null onBinderDied parameter.";
648 return nullptr;
649 }
Steven Moreland64127ca2019-03-13 09:25:44 -0700650 auto ret = new AIBinder_DeathRecipient(onBinderDied);
651 ret->incStrong(nullptr);
652 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700653}
654
Steven Moreland9b80e282018-09-19 13:57:23 -0700655void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
Steven Moreland64127ca2019-03-13 09:25:44 -0700656 if (recipient == nullptr) {
657 return;
658 }
659
660 recipient->decStrong(nullptr);
Steven Moreland901c7452018-09-04 16:17:28 -0700661}
Steven Morelandea14ef22019-08-20 10:50:08 -0700662
663binder_status_t AIBinder_getExtension(AIBinder* binder, AIBinder** outExt) {
664 if (binder == nullptr || outExt == nullptr) {
665 if (outExt != nullptr) {
666 *outExt = nullptr;
667 }
668 return STATUS_UNEXPECTED_NULL;
669 }
670
671 sp<IBinder> ext;
672 status_t res = binder->getBinder()->getExtension(&ext);
673
674 if (res != android::OK) {
675 *outExt = nullptr;
676 return PruneStatusT(res);
677 }
678
679 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(ext);
680 if (ret != nullptr) ret->incStrong(binder);
681
682 *outExt = ret.get();
683 return STATUS_OK;
684}
685
686binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) {
687 if (binder == nullptr || ext == nullptr) {
688 return STATUS_UNEXPECTED_NULL;
689 }
690
691 ABBinder* rawBinder = binder->asABBinder();
692 if (rawBinder == nullptr) {
693 return STATUS_INVALID_OPERATION;
694 }
695
696 rawBinder->setExtension(ext->getBinder());
697 return STATUS_OK;
698}
Steven Moreland2f405f52020-07-08 22:24:29 +0000699
700// platform methods follow
701
702void AIBinder_setRequestingSid(AIBinder* binder, bool requestingSid) {
703 ABBinder* localBinder = binder->asABBinder();
704 if (localBinder == nullptr) {
705 LOG(FATAL) << "AIBinder_setRequestingSid must be called on a local binder";
706 }
707
708 localBinder->setRequestingSid(requestingSid);
709}
710
711const char* AIBinder_getCallingSid() {
712 return ::android::IPCThreadState::self()->getCallingSid();
713}
Steven Morelandb2de9532020-05-29 21:44:41 +0000714
715android::sp<android::IBinder> AIBinder_toPlatformBinder(AIBinder* binder) {
716 if (binder == nullptr) return nullptr;
717 return binder->getBinder();
718}
719
720AIBinder* AIBinder_fromPlatformBinder(const android::sp<android::IBinder>& binder) {
721 sp<AIBinder> ndkBinder = ABpBinder::lookupOrCreateFromBinder(binder);
722 AIBinder_incStrong(ndkBinder.get());
723 return ndkBinder.get();
724}