blob: ee1d48a8224562df97b93d5305bc28a9d206084a [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>
18#include "AIBinder_internal.h"
19
20#include <android/binder_status.h>
21#include "AParcel_internal.h"
22
23#include <android-base/logging.h>
24
Steven Moreland901c7452018-09-04 16:17:28 -070025using DeathRecipient = ::android::IBinder::DeathRecipient;
26
Steven Moreland2e87adc2018-08-20 19:47:00 -070027using ::android::IBinder;
28using ::android::Parcel;
29using ::android::sp;
30using ::android::String16;
31using ::android::wp;
32
Steven Moreland71cddc32018-08-30 23:39:22 -070033namespace ABBinderTag {
34
35static const void* kId = "ABBinder";
36static void* kValue = static_cast<void*>(new bool{true});
37void cleanId(const void* /*id*/, void* /*obj*/, void* /*cookie*/){/* do nothing */};
38
39static void attach(const sp<IBinder>& binder) {
40 binder->attachObject(kId, kValue, nullptr /*cookie*/, cleanId);
41}
42static bool has(const sp<IBinder>& binder) {
43 return binder != nullptr && binder->findObject(kId) == kValue;
44}
45
46} // namespace ABBinderTag
47
Steven Moreland2e87adc2018-08-20 19:47:00 -070048AIBinder::AIBinder(const AIBinder_Class* clazz) : mClazz(clazz) {}
49AIBinder::~AIBinder() {}
50
Steven Moreland71cddc32018-08-30 23:39:22 -070051bool AIBinder::associateClass(const AIBinder_Class* clazz) {
Steven Moreland2e87adc2018-08-20 19:47:00 -070052 using ::android::String8;
53
Steven Moreland71cddc32018-08-30 23:39:22 -070054 if (clazz == nullptr) return false;
55 if (mClazz == clazz) return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -070056
57 String8 newDescriptor(clazz->getInterfaceDescriptor());
58
59 if (mClazz != nullptr) {
60 String8 currentDescriptor(mClazz->getInterfaceDescriptor());
61 if (newDescriptor == currentDescriptor) {
62 LOG(ERROR) << __func__ << ": Class descriptors '" << currentDescriptor
63 << "' match during associateClass, but they are different class objects. "
64 "Class descriptor collision?";
Steven Moreland71cddc32018-08-30 23:39:22 -070065 } else {
66 LOG(ERROR) << __func__
67 << ": Class cannot be associated on object which already has a class. "
68 "Trying to associate to '"
69 << newDescriptor.c_str() << "' but already set to '"
70 << currentDescriptor.c_str() << "'.";
Steven Moreland2e87adc2018-08-20 19:47:00 -070071 }
72
Steven Moreland71cddc32018-08-30 23:39:22 -070073 // always a failure because we know mClazz != clazz
74 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -070075 }
76
Steven Moreland71cddc32018-08-30 23:39:22 -070077 CHECK(asABpBinder() != nullptr); // ABBinder always has a descriptor
78
Steven Moreland2e87adc2018-08-20 19:47:00 -070079 String8 descriptor(getBinder()->getInterfaceDescriptor());
80 if (descriptor != newDescriptor) {
81 LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor.c_str()
82 << "' but descriptor is actually '" << descriptor.c_str() << "'.";
Steven Moreland71cddc32018-08-30 23:39:22 -070083 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -070084 }
85
Steven Moreland71cddc32018-08-30 23:39:22 -070086 // if this is a local object, it's not one known to libbinder_ndk
Steven Moreland2e87adc2018-08-20 19:47:00 -070087 mClazz = clazz;
88
Steven Moreland71cddc32018-08-30 23:39:22 -070089 return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -070090}
91
92ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
93 : AIBinder(clazz), BBinder(), mUserData(userData) {
94 CHECK(clazz != nullptr);
95}
96ABBinder::~ABBinder() {
97 getClass()->onDestroy(mUserData);
98}
99
100const String16& ABBinder::getInterfaceDescriptor() const {
101 return getClass()->getInterfaceDescriptor();
102}
103
104binder_status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
105 binder_flags_t flags) {
106 if (isUserCommand(code)) {
107 if (!data.checkInterface(this)) {
108 return EX_ILLEGAL_STATE;
109 }
110
111 const AParcel in = AParcel::readOnly(this, &data);
112 AParcel out = AParcel(this, reply, false /*owns*/);
113
114 return getClass()->onTransact(this, code, &in, &out);
115 } else {
116 return BBinder::onTransact(code, data, reply, flags);
117 }
118}
119
Steven Moreland71cddc32018-08-30 23:39:22 -0700120ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland2e87adc2018-08-20 19:47:00 -0700121 : AIBinder(nullptr /*clazz*/), BpRefBase(binder) {
122 CHECK(binder != nullptr);
123}
124ABpBinder::~ABpBinder() {}
125
Steven Moreland71cddc32018-08-30 23:39:22 -0700126sp<AIBinder> ABpBinder::fromBinder(const ::android::sp<::android::IBinder>& binder) {
127 if (binder == nullptr) {
128 return nullptr;
129 }
130 if (ABBinderTag::has(binder)) {
131 return static_cast<ABBinder*>(binder.get());
132 }
133 return new ABpBinder(binder);
134}
135
Steven Moreland2e87adc2018-08-20 19:47:00 -0700136struct AIBinder_Weak {
137 wp<AIBinder> binder;
138};
139AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700140 if (binder == nullptr) {
141 return nullptr;
142 }
143
Steven Moreland2e87adc2018-08-20 19:47:00 -0700144 return new AIBinder_Weak{wp<AIBinder>(binder)};
145}
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700146void AIBinder_Weak_delete(AIBinder_Weak** weakBinder) {
147 if (weakBinder == nullptr) {
148 return;
149 }
150
151 delete *weakBinder;
152 *weakBinder = nullptr;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700153}
154AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700155 if (weakBinder == nullptr) {
156 return nullptr;
157 }
158
Steven Moreland2e87adc2018-08-20 19:47:00 -0700159 sp<AIBinder> binder = weakBinder->binder.promote();
160 AIBinder_incStrong(binder.get());
161 return binder.get();
162}
163
164AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
165 AIBinder_Class_onDestroy onDestroy,
166 AIBinder_Class_onTransact onTransact)
167 : onCreate(onCreate),
168 onDestroy(onDestroy),
169 onTransact(onTransact),
170 mInterfaceDescriptor(interfaceDescriptor) {}
171
172AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
173 AIBinder_Class_onCreate onCreate,
174 AIBinder_Class_onDestroy onDestroy,
175 AIBinder_Class_onTransact onTransact) {
176 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
177 onTransact == nullptr) {
178 return nullptr;
179 }
180
181 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
182}
183
Steven Moreland901c7452018-09-04 16:17:28 -0700184void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
185 CHECK(who == mWho);
186
187 mOnDied(mCookie);
188 mWho = nullptr;
189}
190
191AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
192 : mOnDied(onDied) {
193 CHECK(onDied != nullptr);
194}
195
196binder_status_t AIBinder_DeathRecipient::linkToDeath(AIBinder* binder, void* cookie) {
197 CHECK(binder != nullptr);
198
199 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
200
201 sp<TransferDeathRecipient> recipient =
202 new TransferDeathRecipient(binder->getBinder(), cookie, mOnDied);
203
204 binder_status_t status = binder->getBinder()->linkToDeath(recipient, cookie, 0 /*flags*/);
205 if (status != EX_NONE) {
206 return status;
207 }
208
209 mDeathRecipients.push_back(recipient);
210 return EX_NONE;
211}
212
213binder_status_t AIBinder_DeathRecipient::unlinkToDeath(AIBinder* binder, void* cookie) {
214 CHECK(binder != nullptr);
215
216 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
217
218 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
219 sp<TransferDeathRecipient> recipient = *it;
220
221 if (recipient->getCookie() == cookie &&
222
223 recipient->getWho() == binder->getBinder()) {
224 mDeathRecipients.erase(it.base() - 1);
225
226 binder_status_t status =
227 binder->getBinder()->unlinkToDeath(recipient, cookie, 0 /*flags*/);
228 if (status != EX_NONE) {
229 LOG(ERROR) << __func__
230 << ": removed reference to death recipient but unlink failed.";
231 }
232 return status;
233 }
234 }
235
236 return -ENOENT;
237}
238
239// start of C-API methods
240
Steven Moreland2e87adc2018-08-20 19:47:00 -0700241AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
242 if (clazz == nullptr) {
243 LOG(ERROR) << __func__ << ": Must provide class to construct local binder.";
244 return nullptr;
245 }
246
247 void* userData = clazz->onCreate(args);
248
Steven Moreland71cddc32018-08-30 23:39:22 -0700249 sp<AIBinder> ret = new ABBinder(clazz, userData);
250 ABBinderTag::attach(ret->getBinder());
251
252 AIBinder_incStrong(ret.get());
253 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700254}
255
Steven Moreland5ea54da2018-09-04 13:29:55 -0700256bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700257 if (binder == nullptr) {
258 return true;
259 }
260
261 return binder->isRemote();
262}
263
Steven Moreland65867d72018-09-04 14:22:26 -0700264bool AIBinder_isAlive(const AIBinder* binder) {
265 if (binder == nullptr) {
266 return false;
267 }
268
269 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
270}
271
272binder_status_t AIBinder_ping(AIBinder* binder) {
273 if (binder == nullptr) {
274 return EX_NULL_POINTER;
275 }
276
277 return binder->getBinder()->pingBinder();
278}
279
Steven Moreland901c7452018-09-04 16:17:28 -0700280binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
281 void* cookie) {
282 if (binder == nullptr || recipient == nullptr) {
283 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
284 return EX_NULL_POINTER;
285 }
286
287 return recipient->linkToDeath(binder, cookie);
288}
289
290binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
291 void* cookie) {
292 if (binder == nullptr || recipient == nullptr) {
293 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
294 return EX_NULL_POINTER;
295 }
296
297 return recipient->unlinkToDeath(binder, cookie);
298}
299
Steven Moreland2e87adc2018-08-20 19:47:00 -0700300void AIBinder_incStrong(AIBinder* binder) {
301 if (binder == nullptr) {
302 LOG(ERROR) << __func__ << ": on null binder";
303 return;
304 }
305
306 binder->incStrong(nullptr);
307}
308void AIBinder_decStrong(AIBinder* binder) {
309 if (binder == nullptr) {
310 LOG(ERROR) << __func__ << ": on null binder";
311 return;
312 }
313
314 binder->decStrong(nullptr);
315}
316int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
317 if (binder == nullptr) {
318 LOG(ERROR) << __func__ << ": on null binder";
319 return -1;
320 }
321
322 return binder->getStrongCount();
323}
324
Steven Moreland71cddc32018-08-30 23:39:22 -0700325bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
326 if (binder == nullptr) {
327 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700328 }
329
Steven Moreland71cddc32018-08-30 23:39:22 -0700330 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700331}
332
333const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
334 if (binder == nullptr) {
335 return nullptr;
336 }
337
338 return binder->getClass();
339}
340
341void* AIBinder_getUserData(AIBinder* binder) {
342 if (binder == nullptr) {
343 return nullptr;
344 }
345
346 ABBinder* bBinder = binder->asABBinder();
347 if (bBinder == nullptr) {
348 return nullptr;
349 }
350
351 return bBinder->getUserData();
352}
353
354binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
355 if (binder == nullptr || in == nullptr) {
356 LOG(ERROR) << __func__ << ": requires non-null parameters.";
357 return EX_NULL_POINTER;
358 }
359 const AIBinder_Class* clazz = binder->getClass();
360 if (clazz == nullptr) {
361 LOG(ERROR) << __func__
362 << ": Class must be defined for a remote binder transaction. See "
363 "AIBinder_associateClass.";
364 return EX_ILLEGAL_STATE;
365 }
366
Steven Moreland3527c2e2018-09-05 17:07:14 -0700367 if (!binder->isRemote()) {
Steven Moreland74521c82018-09-07 14:50:40 -0700368 LOG(WARNING) << "A binder object at " << binder
369 << " is being transacted on, however, this object is in the same process as "
370 "its proxy. Transacting with this binder is expensive compared to just "
371 "calling the corresponding functionality in the same process.";
Steven Moreland3527c2e2018-09-05 17:07:14 -0700372 }
373
Steven Moreland2e87adc2018-08-20 19:47:00 -0700374 *in = new AParcel(binder);
375 binder_status_t status = (**in)->writeInterfaceToken(clazz->getInterfaceDescriptor());
376 if (status != EX_NONE) {
377 delete *in;
378 *in = nullptr;
379 }
380
381 return status;
382}
383
Steven Moreland2e87adc2018-08-20 19:47:00 -0700384binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
385 AParcel** out, binder_flags_t flags) {
386 if (in == nullptr) {
387 LOG(ERROR) << __func__ << ": requires non-null in parameter";
388 return EX_NULL_POINTER;
389 }
390
Steven Morelandcaa776c2018-09-04 13:48:11 -0700391 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700392 // This object is the input to the transaction. This function takes ownership of it and deletes
393 // it.
Steven Morelandcaa776c2018-09-04 13:48:11 -0700394 AutoParcelDestroyer forIn(in, AParcel_delete);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700395
396 if (!isUserCommand(code)) {
397 LOG(ERROR) << __func__ << ": Only user-defined transactions can be made from the NDK.";
398 return EX_UNSUPPORTED_OPERATION;
399 }
400
401 if ((flags & ~FLAG_ONEWAY) != 0) {
402 LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags;
403 return EX_ILLEGAL_ARGUMENT;
404 }
405
406 if (binder == nullptr || *in == nullptr || out == nullptr) {
407 LOG(ERROR) << __func__ << ": requires non-null parameters.";
408 return EX_NULL_POINTER;
409 }
410
411 if ((*in)->getBinder() != binder) {
412 LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
413 << " but called with " << (*in)->getBinder();
414 return EX_ILLEGAL_STATE;
415 }
416
417 *out = new AParcel(binder);
418
419 binder_status_t parcelStatus =
420 binder->getBinder()->transact(code, *(*in)->operator->(), (*out)->operator->(), flags);
421
422 if (parcelStatus != EX_NONE) {
423 delete *out;
424 *out = nullptr;
425 }
426
427 return parcelStatus;
428}
Steven Moreland901c7452018-09-04 16:17:28 -0700429
430AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
431 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
432 if (onBinderDied == nullptr) {
433 LOG(ERROR) << __func__ << ": requires non-null onBinderDied parameter.";
434 return nullptr;
435 }
436 return new AIBinder_DeathRecipient(onBinderDied);
437}
438
439void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient** recipient) {
440 if (recipient == nullptr) {
441 return;
442 }
443
444 delete *recipient;
445 *recipient = nullptr;
446}