blob: 23db59e238dd1d211a617b2f99410765838110cb [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 Moreland9234c432021-06-29 23:01:10 +000050 // can only attach once
51 CHECK_EQ(nullptr, binder->attachObject(kId, kValue, nullptr /*cookie*/, clean));
Steven Moreland71cddc32018-08-30 23:39:22 -070052}
53static bool has(const sp<IBinder>& binder) {
54 return binder != nullptr && binder->findObject(kId) == kValue;
55}
56
Steven Moreland6cf66ac2018-11-02 18:14:54 -070057} // namespace ABBinderTag
Steven Moreland71cddc32018-08-30 23:39:22 -070058
Steven Moreland94968952018-09-05 14:42:59 -070059namespace ABpBinderTag {
60
Steven Moreland94968952018-09-05 14:42:59 -070061static 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
Steven Moreland4d196972021-08-12 15:49:44 -070085 << "' match during associateClass, but they are different class objects ("
86 << clazz << " vs " << mClazz << "). 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)) {
Steven Morelandf3ed8062021-08-27 14:31:14 -0700175 if (getClass()->writeHeader && !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);
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800184 } else if (code == SHELL_COMMAND_TRANSACTION) {
185 int in = data.readFileDescriptor();
186 int out = data.readFileDescriptor();
187 int err = data.readFileDescriptor();
188
189 int argc = data.readInt32();
190 std::vector<String8> utf8Args; // owns memory of utf8s
191 std::vector<const char*> utf8Pointers; // what can be passed over NDK API
192 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
193 utf8Args.push_back(String8(data.readString16()));
194 utf8Pointers.push_back(utf8Args[i].c_str());
195 }
196
197 data.readStrongBinder(); // skip over the IShellCallback
198 sp<IResultReceiver> resultReceiver = IResultReceiver::asInterface(data.readStrongBinder());
199
200 // Shell commands should only be callable by ADB.
201 uid_t uid = AIBinder_getCallingUid();
202 if (uid != AID_ROOT && uid != AID_SHELL) {
203 if (resultReceiver != nullptr) {
204 resultReceiver->send(-1);
205 }
206 return STATUS_PERMISSION_DENIED;
207 }
208
209 // Check that the file descriptors are valid.
210 if (in == STATUS_BAD_TYPE || out == STATUS_BAD_TYPE || err == STATUS_BAD_TYPE) {
211 if (resultReceiver != nullptr) {
212 resultReceiver->send(-1);
213 }
214 return STATUS_BAD_VALUE;
215 }
216
217 binder_status_t status = getClass()->handleShellCommand(
218 this, in, out, err, utf8Pointers.data(), utf8Pointers.size());
219 if (resultReceiver != nullptr) {
220 resultReceiver->send(status);
221 }
222 return status;
Steven 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) {
Steven Moreland2505ec42021-06-25 02:08:49 +0000235 // Since ABpBinder is OBJECT_LIFETIME_WEAK, we must remove this weak reference in order for
Steven Moreland91a96b62021-06-30 23:07:13 +0000236 // the ABpBinder to be deleted. Even though we have no more references on the ABpBinder
237 // (BpRefBase), the remote object may still exist (for instance, if we
238 // receive it from another process, before the ABpBinder is attached).
Steven Moreland94968952018-09-05 14:42:59 -0700239
Steven Moreland2505ec42021-06-25 02:08:49 +0000240 ABpBinderTag::Value* value =
Steven Moreland91a96b62021-06-30 23:07:13 +0000241 static_cast<ABpBinderTag::Value*>(remote()->findObject(ABpBinderTag::kId));
242 CHECK_NE(nullptr, value) << "ABpBinder must always be attached";
243
244 remote()->withLock([&]() { value->binder = nullptr; });
Steven Moreland94968952018-09-05 14:42:59 -0700245
246 BpRefBase::onLastStrongRef(id);
247}
248
249sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700250 if (binder == nullptr) {
251 return nullptr;
252 }
253 if (ABBinderTag::has(binder)) {
254 return static_cast<ABBinder*>(binder.get());
255 }
Steven Moreland94968952018-09-05 14:42:59 -0700256
Steven Moreland91a96b62021-06-30 23:07:13 +0000257 // The following code ensures that for a given binder object (remote or local), if it is not an
258 // ABBinder then at most one ABpBinder object exists in a given process representing it.
259
Steven Moreland1f1bed62021-06-25 21:50:50 +0000260 auto* value = static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId));
Steven Moreland94968952018-09-05 14:42:59 -0700261 if (value == nullptr) {
262 value = new ABpBinderTag::Value;
Steven Moreland1f1bed62021-06-25 21:50:50 +0000263 auto oldValue = static_cast<ABpBinderTag::Value*>(
264 binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value),
265 nullptr /*cookie*/, ABpBinderTag::clean));
266
267 // allocated by another thread
268 if (oldValue) {
269 delete value;
270 value = oldValue;
271 }
Steven Moreland94968952018-09-05 14:42:59 -0700272 }
273
Steven Moreland1f1bed62021-06-25 21:50:50 +0000274 sp<ABpBinder> ret;
275 binder->withLock([&]() {
276 ret = value->binder.promote();
277 if (ret == nullptr) {
278 ret = sp<ABpBinder>::make(binder);
279 value->binder = ret;
280 }
281 });
Steven Moreland94968952018-09-05 14:42:59 -0700282
283 return ret;
Steven Moreland71cddc32018-08-30 23:39:22 -0700284}
285
Steven Moreland2e87adc2018-08-20 19:47:00 -0700286struct AIBinder_Weak {
287 wp<AIBinder> binder;
288};
289AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700290 if (binder == nullptr) {
291 return nullptr;
292 }
293
Steven Moreland2e87adc2018-08-20 19:47:00 -0700294 return new AIBinder_Weak{wp<AIBinder>(binder)};
295}
Steven Moreland9b80e282018-09-19 13:57:23 -0700296void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
297 delete weakBinder;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700298}
299AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700300 if (weakBinder == nullptr) {
301 return nullptr;
302 }
303
Steven Moreland2e87adc2018-08-20 19:47:00 -0700304 sp<AIBinder> binder = weakBinder->binder.promote();
305 AIBinder_incStrong(binder.get());
306 return binder.get();
307}
308
Steven Morelandc70e0122021-01-08 02:44:53 +0000309AIBinder_Weak* AIBinder_Weak_clone(const AIBinder_Weak* weak) {
310 if (weak == nullptr) {
311 return nullptr;
312 }
313
314 return new AIBinder_Weak{weak->binder};
315}
316
317bool AIBinder_lt(const AIBinder* lhs, const AIBinder* rhs) {
318 if (lhs == nullptr || rhs == nullptr) return lhs < rhs;
319
320 return const_cast<AIBinder*>(lhs)->getBinder() < const_cast<AIBinder*>(rhs)->getBinder();
321}
322
323bool AIBinder_Weak_lt(const AIBinder_Weak* lhs, const AIBinder_Weak* rhs) {
324 if (lhs == nullptr || rhs == nullptr) return lhs < rhs;
325
326 return lhs->binder < rhs->binder;
327}
328
Steven Moreland2e87adc2018-08-20 19:47:00 -0700329AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
330 AIBinder_Class_onDestroy onDestroy,
331 AIBinder_Class_onTransact onTransact)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700332 : onCreate(onCreate),
333 onDestroy(onDestroy),
334 onTransact(onTransact),
Stephen Crane8fde87f2020-11-17 15:06:11 -0800335 mInterfaceDescriptor(interfaceDescriptor),
336 mWideInterfaceDescriptor(interfaceDescriptor) {}
Steven Moreland2e87adc2018-08-20 19:47:00 -0700337
338AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
339 AIBinder_Class_onCreate onCreate,
340 AIBinder_Class_onDestroy onDestroy,
341 AIBinder_Class_onTransact onTransact) {
342 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
343 onTransact == nullptr) {
344 return nullptr;
345 }
346
347 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
348}
349
Steven Morelanda194c452019-03-04 16:47:07 -0800350void AIBinder_Class_setOnDump(AIBinder_Class* clazz, AIBinder_onDump onDump) {
351 CHECK(clazz != nullptr) << "setOnDump requires non-null clazz";
352
353 // this is required to be called before instances are instantiated
354 clazz->onDump = onDump;
355}
356
Steven Morelandf3ed8062021-08-27 14:31:14 -0700357void AIBinder_Class_disableInterfaceTokenHeader(AIBinder_Class* clazz) {
358 CHECK(clazz != nullptr) << "disableInterfaceTokenHeader requires non-null clazz";
359
360 clazz->writeHeader = false;
361}
362
Ruchir Rastogicc7a7462020-01-31 14:29:15 -0800363void AIBinder_Class_setHandleShellCommand(AIBinder_Class* clazz,
364 AIBinder_handleShellCommand handleShellCommand) {
365 CHECK(clazz != nullptr) << "setHandleShellCommand requires non-null clazz";
366
367 clazz->handleShellCommand = handleShellCommand;
368}
369
Stephen Crane8fde87f2020-11-17 15:06:11 -0800370const char* AIBinder_Class_getDescriptor(const AIBinder_Class* clazz) {
371 CHECK(clazz != nullptr) << "getDescriptor requires non-null clazz";
372
373 return clazz->getInterfaceDescriptorUtf8();
374}
375
Steven Moreland901c7452018-09-04 16:17:28 -0700376void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
Steven Moreland8bcb5032021-04-07 23:06:43 +0000377 CHECK(who == mWho) << who.unsafe_get() << "(" << who.get_refs() << ") vs " << mWho.unsafe_get()
378 << " (" << mWho.get_refs() << ")";
Steven Moreland901c7452018-09-04 16:17:28 -0700379
380 mOnDied(mCookie);
Steven Moreland64127ca2019-03-13 09:25:44 -0700381
382 sp<AIBinder_DeathRecipient> recipient = mParentRecipient.promote();
383 sp<IBinder> strongWho = who.promote();
384
385 // otherwise this will be cleaned up later with pruneDeadTransferEntriesLocked
386 if (recipient != nullptr && strongWho != nullptr) {
387 status_t result = recipient->unlinkToDeath(strongWho, mCookie);
388 if (result != ::android::DEAD_OBJECT) {
389 LOG(WARNING) << "Unlinking to dead binder resulted in: " << result;
390 }
391 }
392
Steven Moreland901c7452018-09-04 16:17:28 -0700393 mWho = nullptr;
394}
395
396AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700397 : mOnDied(onDied) {
Steven Moreland901c7452018-09-04 16:17:28 -0700398 CHECK(onDied != nullptr);
399}
400
Steven Moreland64127ca2019-03-13 09:25:44 -0700401void AIBinder_DeathRecipient::pruneDeadTransferEntriesLocked() {
402 mDeathRecipients.erase(std::remove_if(mDeathRecipients.begin(), mDeathRecipients.end(),
403 [](const sp<TransferDeathRecipient>& tdr) {
404 return tdr->getWho() == nullptr;
405 }),
406 mDeathRecipients.end());
407}
408
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900409binder_status_t AIBinder_DeathRecipient::linkToDeath(const sp<IBinder>& binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700410 CHECK(binder != nullptr);
411
412 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
413
414 sp<TransferDeathRecipient> recipient =
Steven Moreland64127ca2019-03-13 09:25:44 -0700415 new TransferDeathRecipient(binder, cookie, this, mOnDied);
Steven Moreland901c7452018-09-04 16:17:28 -0700416
Steven Moreland64127ca2019-03-13 09:25:44 -0700417 status_t status = binder->linkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700418 if (status != STATUS_OK) {
419 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700420 }
421
422 mDeathRecipients.push_back(recipient);
Steven Moreland64127ca2019-03-13 09:25:44 -0700423
424 pruneDeadTransferEntriesLocked();
Steven Moreland5d62e442018-09-13 15:01:02 -0700425 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700426}
427
Jiyong Park4f97a8c2020-11-16 18:11:14 +0900428binder_status_t AIBinder_DeathRecipient::unlinkToDeath(const sp<IBinder>& binder, void* cookie) {
Steven Moreland901c7452018-09-04 16:17:28 -0700429 CHECK(binder != nullptr);
430
431 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
432
433 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
434 sp<TransferDeathRecipient> recipient = *it;
435
Steven Moreland64127ca2019-03-13 09:25:44 -0700436 if (recipient->getCookie() == cookie && recipient->getWho() == binder) {
Steven Moreland901c7452018-09-04 16:17:28 -0700437 mDeathRecipients.erase(it.base() - 1);
438
Steven Moreland64127ca2019-03-13 09:25:44 -0700439 status_t status = binder->unlinkToDeath(recipient, cookie, 0 /*flags*/);
Steven Moreland5d62e442018-09-13 15:01:02 -0700440 if (status != ::android::OK) {
Steven Moreland901c7452018-09-04 16:17:28 -0700441 LOG(ERROR) << __func__
442 << ": removed reference to death recipient but unlink failed.";
443 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700444 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700445 }
446 }
447
Steven Moreland5d62e442018-09-13 15:01:02 -0700448 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700449}
450
451// start of C-API methods
452
Steven Moreland2e87adc2018-08-20 19:47:00 -0700453AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
454 if (clazz == nullptr) {
455 LOG(ERROR) << __func__ << ": Must provide class to construct local binder.";
456 return nullptr;
457 }
458
459 void* userData = clazz->onCreate(args);
460
Steven Moreland71cddc32018-08-30 23:39:22 -0700461 sp<AIBinder> ret = new ABBinder(clazz, userData);
462 ABBinderTag::attach(ret->getBinder());
463
464 AIBinder_incStrong(ret.get());
465 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700466}
467
Steven Moreland5ea54da2018-09-04 13:29:55 -0700468bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700469 if (binder == nullptr) {
Steven Moreland56f752a2018-11-05 17:23:37 -0800470 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700471 }
472
473 return binder->isRemote();
474}
475
Steven Moreland65867d72018-09-04 14:22:26 -0700476bool AIBinder_isAlive(const AIBinder* binder) {
477 if (binder == nullptr) {
478 return false;
479 }
480
481 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
482}
483
484binder_status_t AIBinder_ping(AIBinder* binder) {
485 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700486 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700487 }
488
Steven Moreland5d62e442018-09-13 15:01:02 -0700489 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700490}
491
Steven Morelanda194c452019-03-04 16:47:07 -0800492binder_status_t AIBinder_dump(AIBinder* binder, int fd, const char** args, uint32_t numArgs) {
493 if (binder == nullptr) {
494 return STATUS_UNEXPECTED_NULL;
495 }
496
497 ABBinder* bBinder = binder->asABBinder();
498 if (bBinder != nullptr) {
499 AIBinder_onDump onDump = binder->getClass()->onDump;
500 if (onDump == nullptr) {
501 return STATUS_OK;
502 }
503 return PruneStatusT(onDump(bBinder, fd, args, numArgs));
504 }
505
506 ::android::Vector<String16> utf16Args;
507 utf16Args.setCapacity(numArgs);
508 for (uint32_t i = 0; i < numArgs; i++) {
509 utf16Args.push(String16(String8(args[i])));
510 }
511
512 status_t status = binder->getBinder()->dump(fd, utf16Args);
513 return PruneStatusT(status);
514}
515
Steven Moreland901c7452018-09-04 16:17:28 -0700516binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
517 void* cookie) {
518 if (binder == nullptr || recipient == nullptr) {
519 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700520 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700521 }
522
Steven Moreland5d62e442018-09-13 15:01:02 -0700523 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700524 return recipient->linkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700525}
526
527binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
528 void* cookie) {
529 if (binder == nullptr || recipient == nullptr) {
530 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700531 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700532 }
533
Steven Moreland5d62e442018-09-13 15:01:02 -0700534 // returns binder_status_t
Steven Moreland64127ca2019-03-13 09:25:44 -0700535 return recipient->unlinkToDeath(binder->getBinder(), cookie);
Steven Moreland901c7452018-09-04 16:17:28 -0700536}
537
Steven Morelandf3034b02018-11-12 17:37:46 -0800538uid_t AIBinder_getCallingUid() {
539 return ::android::IPCThreadState::self()->getCallingUid();
540}
541
542pid_t AIBinder_getCallingPid() {
543 return ::android::IPCThreadState::self()->getCallingPid();
544}
545
Steven Moreland2e87adc2018-08-20 19:47:00 -0700546void AIBinder_incStrong(AIBinder* binder) {
547 if (binder == nullptr) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700548 return;
549 }
550
551 binder->incStrong(nullptr);
552}
553void AIBinder_decStrong(AIBinder* binder) {
554 if (binder == nullptr) {
555 LOG(ERROR) << __func__ << ": on null binder";
556 return;
557 }
558
559 binder->decStrong(nullptr);
560}
561int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
562 if (binder == nullptr) {
563 LOG(ERROR) << __func__ << ": on null binder";
564 return -1;
565 }
566
567 return binder->getStrongCount();
568}
569
Steven Moreland71cddc32018-08-30 23:39:22 -0700570bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
571 if (binder == nullptr) {
572 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700573 }
574
Steven Moreland71cddc32018-08-30 23:39:22 -0700575 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700576}
577
578const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
579 if (binder == nullptr) {
580 return nullptr;
581 }
582
583 return binder->getClass();
584}
585
586void* AIBinder_getUserData(AIBinder* binder) {
587 if (binder == nullptr) {
588 return nullptr;
589 }
590
591 ABBinder* bBinder = binder->asABBinder();
592 if (bBinder == nullptr) {
593 return nullptr;
594 }
595
596 return bBinder->getUserData();
597}
598
599binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
600 if (binder == nullptr || in == nullptr) {
601 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700602 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700603 }
604 const AIBinder_Class* clazz = binder->getClass();
605 if (clazz == nullptr) {
606 LOG(ERROR) << __func__
607 << ": Class must be defined for a remote binder transaction. See "
608 "AIBinder_associateClass.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700609 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700610 }
611
612 *in = new AParcel(binder);
Steven Moreland1fda67b2021-04-02 18:35:50 +0000613 (*in)->get()->markForBinder(binder->getBinder());
614
Steven Morelandf3ed8062021-08-27 14:31:14 -0700615 status_t status = android::OK;
616 if (clazz->writeHeader) {
617 status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
618 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700619 binder_status_t ret = PruneStatusT(status);
620
621 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700622 delete *in;
623 *in = nullptr;
624 }
625
Steven Moreland5d62e442018-09-13 15:01:02 -0700626 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700627}
628
Steven Moreland9b80e282018-09-19 13:57:23 -0700629static void DestroyParcel(AParcel** parcel) {
630 delete *parcel;
631 *parcel = nullptr;
632}
633
Steven Moreland2e87adc2018-08-20 19:47:00 -0700634binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
635 AParcel** out, binder_flags_t flags) {
636 if (in == nullptr) {
637 LOG(ERROR) << __func__ << ": requires non-null in parameter";
Steven Moreland5d62e442018-09-13 15:01:02 -0700638 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700639 }
640
Steven Morelandcaa776c2018-09-04 13:48:11 -0700641 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700642 // This object is the input to the transaction. This function takes ownership of it and deletes
643 // it.
Steven Moreland9b80e282018-09-19 13:57:23 -0700644 AutoParcelDestroyer forIn(in, DestroyParcel);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700645
646 if (!isUserCommand(code)) {
647 LOG(ERROR) << __func__ << ": Only user-defined transactions can be made from the NDK.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700648 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700649 }
650
Steven Morelandf183fdd2020-10-27 00:12:12 +0000651 constexpr binder_flags_t kAllFlags = FLAG_PRIVATE_VENDOR | FLAG_ONEWAY | FLAG_CLEAR_BUF;
Steven Moreland46b5fea2019-10-15 11:22:18 -0700652 if ((flags & ~kAllFlags) != 0) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700653 LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags;
Steven Moreland5d62e442018-09-13 15:01:02 -0700654 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700655 }
656
657 if (binder == nullptr || *in == nullptr || out == nullptr) {
658 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700659 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700660 }
661
662 if ((*in)->getBinder() != binder) {
663 LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
664 << " but called with " << (*in)->getBinder();
Steven Moreland5d62e442018-09-13 15:01:02 -0700665 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700666 }
667
668 *out = new AParcel(binder);
669
Steven Morelandf18615b2018-09-14 11:43:06 -0700670 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700671 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700672
Steven Moreland5d62e442018-09-13 15:01:02 -0700673 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700674 delete *out;
675 *out = nullptr;
676 }
677
Steven Moreland5d62e442018-09-13 15:01:02 -0700678 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700679}
Steven Moreland901c7452018-09-04 16:17:28 -0700680
681AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
682 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
683 if (onBinderDied == nullptr) {
684 LOG(ERROR) << __func__ << ": requires non-null onBinderDied parameter.";
685 return nullptr;
686 }
Steven Moreland64127ca2019-03-13 09:25:44 -0700687 auto ret = new AIBinder_DeathRecipient(onBinderDied);
688 ret->incStrong(nullptr);
689 return ret;
Steven Moreland901c7452018-09-04 16:17:28 -0700690}
691
Steven Moreland9b80e282018-09-19 13:57:23 -0700692void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient* recipient) {
Steven Moreland64127ca2019-03-13 09:25:44 -0700693 if (recipient == nullptr) {
694 return;
695 }
696
697 recipient->decStrong(nullptr);
Steven Moreland901c7452018-09-04 16:17:28 -0700698}
Steven Morelandea14ef22019-08-20 10:50:08 -0700699
700binder_status_t AIBinder_getExtension(AIBinder* binder, AIBinder** outExt) {
701 if (binder == nullptr || outExt == nullptr) {
702 if (outExt != nullptr) {
703 *outExt = nullptr;
704 }
705 return STATUS_UNEXPECTED_NULL;
706 }
707
708 sp<IBinder> ext;
709 status_t res = binder->getBinder()->getExtension(&ext);
710
711 if (res != android::OK) {
712 *outExt = nullptr;
713 return PruneStatusT(res);
714 }
715
716 sp<AIBinder> ret = ABpBinder::lookupOrCreateFromBinder(ext);
717 if (ret != nullptr) ret->incStrong(binder);
718
719 *outExt = ret.get();
720 return STATUS_OK;
721}
722
723binder_status_t AIBinder_setExtension(AIBinder* binder, AIBinder* ext) {
724 if (binder == nullptr || ext == nullptr) {
725 return STATUS_UNEXPECTED_NULL;
726 }
727
728 ABBinder* rawBinder = binder->asABBinder();
729 if (rawBinder == nullptr) {
730 return STATUS_INVALID_OPERATION;
731 }
732
733 rawBinder->setExtension(ext->getBinder());
734 return STATUS_OK;
735}
Steven Moreland2f405f52020-07-08 22:24:29 +0000736
737// platform methods follow
738
739void AIBinder_setRequestingSid(AIBinder* binder, bool requestingSid) {
740 ABBinder* localBinder = binder->asABBinder();
741 if (localBinder == nullptr) {
742 LOG(FATAL) << "AIBinder_setRequestingSid must be called on a local binder";
743 }
744
745 localBinder->setRequestingSid(requestingSid);
746}
747
748const char* AIBinder_getCallingSid() {
749 return ::android::IPCThreadState::self()->getCallingSid();
750}
Steven Morelandb2de9532020-05-29 21:44:41 +0000751
752android::sp<android::IBinder> AIBinder_toPlatformBinder(AIBinder* binder) {
753 if (binder == nullptr) return nullptr;
754 return binder->getBinder();
755}
756
757AIBinder* AIBinder_fromPlatformBinder(const android::sp<android::IBinder>& binder) {
758 sp<AIBinder> ndkBinder = ABpBinder::lookupOrCreateFromBinder(binder);
759 AIBinder_incStrong(ndkBinder.get());
760 return ndkBinder.get();
761}