blob: fe07914c046dda9e5650a5fd075d128cca412393 [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
25using ::android::IBinder;
26using ::android::Parcel;
27using ::android::sp;
28using ::android::String16;
29using ::android::wp;
30
Steven Moreland71cddc32018-08-30 23:39:22 -070031namespace ABBinderTag {
32
33static const void* kId = "ABBinder";
34static void* kValue = static_cast<void*>(new bool{true});
35void cleanId(const void* /*id*/, void* /*obj*/, void* /*cookie*/){/* do nothing */};
36
37static void attach(const sp<IBinder>& binder) {
38 binder->attachObject(kId, kValue, nullptr /*cookie*/, cleanId);
39}
40static bool has(const sp<IBinder>& binder) {
41 return binder != nullptr && binder->findObject(kId) == kValue;
42}
43
44} // namespace ABBinderTag
45
Steven Moreland2e87adc2018-08-20 19:47:00 -070046AIBinder::AIBinder(const AIBinder_Class* clazz) : mClazz(clazz) {}
47AIBinder::~AIBinder() {}
48
Steven Moreland71cddc32018-08-30 23:39:22 -070049bool AIBinder::associateClass(const AIBinder_Class* clazz) {
Steven Moreland2e87adc2018-08-20 19:47:00 -070050 using ::android::String8;
51
Steven Moreland71cddc32018-08-30 23:39:22 -070052 if (clazz == nullptr) return false;
53 if (mClazz == clazz) return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -070054
55 String8 newDescriptor(clazz->getInterfaceDescriptor());
56
57 if (mClazz != nullptr) {
58 String8 currentDescriptor(mClazz->getInterfaceDescriptor());
59 if (newDescriptor == currentDescriptor) {
60 LOG(ERROR) << __func__ << ": Class descriptors '" << currentDescriptor
61 << "' match during associateClass, but they are different class objects. "
62 "Class descriptor collision?";
Steven Moreland71cddc32018-08-30 23:39:22 -070063 } else {
64 LOG(ERROR) << __func__
65 << ": Class cannot be associated on object which already has a class. "
66 "Trying to associate to '"
67 << newDescriptor.c_str() << "' but already set to '"
68 << currentDescriptor.c_str() << "'.";
Steven Moreland2e87adc2018-08-20 19:47:00 -070069 }
70
Steven Moreland71cddc32018-08-30 23:39:22 -070071 // always a failure because we know mClazz != clazz
72 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -070073 }
74
Steven Moreland71cddc32018-08-30 23:39:22 -070075 CHECK(asABpBinder() != nullptr); // ABBinder always has a descriptor
76
Steven Moreland2e87adc2018-08-20 19:47:00 -070077 String8 descriptor(getBinder()->getInterfaceDescriptor());
78 if (descriptor != newDescriptor) {
79 LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor.c_str()
80 << "' but descriptor is actually '" << descriptor.c_str() << "'.";
Steven Moreland71cddc32018-08-30 23:39:22 -070081 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -070082 }
83
Steven Moreland71cddc32018-08-30 23:39:22 -070084 // if this is a local object, it's not one known to libbinder_ndk
Steven Moreland2e87adc2018-08-20 19:47:00 -070085 mClazz = clazz;
86
Steven Moreland71cddc32018-08-30 23:39:22 -070087 return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -070088}
89
90ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
91 : AIBinder(clazz), BBinder(), mUserData(userData) {
92 CHECK(clazz != nullptr);
93}
94ABBinder::~ABBinder() {
95 getClass()->onDestroy(mUserData);
96}
97
98const String16& ABBinder::getInterfaceDescriptor() const {
99 return getClass()->getInterfaceDescriptor();
100}
101
102binder_status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
103 binder_flags_t flags) {
104 if (isUserCommand(code)) {
105 if (!data.checkInterface(this)) {
106 return EX_ILLEGAL_STATE;
107 }
108
109 const AParcel in = AParcel::readOnly(this, &data);
110 AParcel out = AParcel(this, reply, false /*owns*/);
111
112 return getClass()->onTransact(this, code, &in, &out);
113 } else {
114 return BBinder::onTransact(code, data, reply, flags);
115 }
116}
117
Steven Moreland71cddc32018-08-30 23:39:22 -0700118ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland2e87adc2018-08-20 19:47:00 -0700119 : AIBinder(nullptr /*clazz*/), BpRefBase(binder) {
120 CHECK(binder != nullptr);
121}
122ABpBinder::~ABpBinder() {}
123
Steven Moreland71cddc32018-08-30 23:39:22 -0700124sp<AIBinder> ABpBinder::fromBinder(const ::android::sp<::android::IBinder>& binder) {
125 if (binder == nullptr) {
126 return nullptr;
127 }
128 if (ABBinderTag::has(binder)) {
129 return static_cast<ABBinder*>(binder.get());
130 }
131 return new ABpBinder(binder);
132}
133
Steven Moreland2e87adc2018-08-20 19:47:00 -0700134struct AIBinder_Weak {
135 wp<AIBinder> binder;
136};
137AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
138 if (binder == nullptr) return nullptr;
139 return new AIBinder_Weak{wp<AIBinder>(binder)};
140}
141void AIBinder_Weak_delete(AIBinder_Weak* weakBinder) {
142 delete weakBinder;
143}
144AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
145 if (weakBinder == nullptr) return nullptr;
146 sp<AIBinder> binder = weakBinder->binder.promote();
147 AIBinder_incStrong(binder.get());
148 return binder.get();
149}
150
151AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
152 AIBinder_Class_onDestroy onDestroy,
153 AIBinder_Class_onTransact onTransact)
154 : onCreate(onCreate),
155 onDestroy(onDestroy),
156 onTransact(onTransact),
157 mInterfaceDescriptor(interfaceDescriptor) {}
158
159AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
160 AIBinder_Class_onCreate onCreate,
161 AIBinder_Class_onDestroy onDestroy,
162 AIBinder_Class_onTransact onTransact) {
163 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
164 onTransact == nullptr) {
165 return nullptr;
166 }
167
168 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
169}
170
171AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
172 if (clazz == nullptr) {
173 LOG(ERROR) << __func__ << ": Must provide class to construct local binder.";
174 return nullptr;
175 }
176
177 void* userData = clazz->onCreate(args);
178
Steven Moreland71cddc32018-08-30 23:39:22 -0700179 sp<AIBinder> ret = new ABBinder(clazz, userData);
180 ABBinderTag::attach(ret->getBinder());
181
182 AIBinder_incStrong(ret.get());
183 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700184}
185
186bool AIBinder_isRemote(AIBinder* binder) {
187 if (binder == nullptr) {
188 return true;
189 }
190
191 return binder->isRemote();
192}
193
194void AIBinder_incStrong(AIBinder* binder) {
195 if (binder == nullptr) {
196 LOG(ERROR) << __func__ << ": on null binder";
197 return;
198 }
199
200 binder->incStrong(nullptr);
201}
202void AIBinder_decStrong(AIBinder* binder) {
203 if (binder == nullptr) {
204 LOG(ERROR) << __func__ << ": on null binder";
205 return;
206 }
207
208 binder->decStrong(nullptr);
209}
210int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
211 if (binder == nullptr) {
212 LOG(ERROR) << __func__ << ": on null binder";
213 return -1;
214 }
215
216 return binder->getStrongCount();
217}
218
Steven Moreland71cddc32018-08-30 23:39:22 -0700219bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
220 if (binder == nullptr) {
221 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700222 }
223
Steven Moreland71cddc32018-08-30 23:39:22 -0700224 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700225}
226
227const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
228 if (binder == nullptr) {
229 return nullptr;
230 }
231
232 return binder->getClass();
233}
234
235void* AIBinder_getUserData(AIBinder* binder) {
236 if (binder == nullptr) {
237 return nullptr;
238 }
239
240 ABBinder* bBinder = binder->asABBinder();
241 if (bBinder == nullptr) {
242 return nullptr;
243 }
244
245 return bBinder->getUserData();
246}
247
248binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
249 if (binder == nullptr || in == nullptr) {
250 LOG(ERROR) << __func__ << ": requires non-null parameters.";
251 return EX_NULL_POINTER;
252 }
253 const AIBinder_Class* clazz = binder->getClass();
254 if (clazz == nullptr) {
255 LOG(ERROR) << __func__
256 << ": Class must be defined for a remote binder transaction. See "
257 "AIBinder_associateClass.";
258 return EX_ILLEGAL_STATE;
259 }
260
261 *in = new AParcel(binder);
262 binder_status_t status = (**in)->writeInterfaceToken(clazz->getInterfaceDescriptor());
263 if (status != EX_NONE) {
264 delete *in;
265 *in = nullptr;
266 }
267
268 return status;
269}
270
271using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
272static void destroy_parcel(AParcel** parcel) {
273 delete *parcel;
274 *parcel = nullptr;
275}
276
277binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
278 AParcel** out, binder_flags_t flags) {
279 if (in == nullptr) {
280 LOG(ERROR) << __func__ << ": requires non-null in parameter";
281 return EX_NULL_POINTER;
282 }
283
284 // This object is the input to the transaction. This function takes ownership of it and deletes
285 // it.
286 AutoParcelDestroyer forIn(in, destroy_parcel);
287
288 if (!isUserCommand(code)) {
289 LOG(ERROR) << __func__ << ": Only user-defined transactions can be made from the NDK.";
290 return EX_UNSUPPORTED_OPERATION;
291 }
292
293 if ((flags & ~FLAG_ONEWAY) != 0) {
294 LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags;
295 return EX_ILLEGAL_ARGUMENT;
296 }
297
298 if (binder == nullptr || *in == nullptr || out == nullptr) {
299 LOG(ERROR) << __func__ << ": requires non-null parameters.";
300 return EX_NULL_POINTER;
301 }
302
303 if ((*in)->getBinder() != binder) {
304 LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
305 << " but called with " << (*in)->getBinder();
306 return EX_ILLEGAL_STATE;
307 }
308
309 *out = new AParcel(binder);
310
311 binder_status_t parcelStatus =
312 binder->getBinder()->transact(code, *(*in)->operator->(), (*out)->operator->(), flags);
313
314 if (parcelStatus != EX_NONE) {
315 delete *out;
316 *out = nullptr;
317 }
318
319 return parcelStatus;
320}
321
322binder_status_t AIBinder_finalizeTransaction(AIBinder* binder, AParcel** out) {
323 if (out == nullptr) {
324 LOG(ERROR) << __func__ << ": requires non-null out parameter";
325 return EX_NULL_POINTER;
326 }
327
328 // This object is the input to the transaction. This function takes ownership of it and deletes
329 // it.
330 AutoParcelDestroyer forOut(out, destroy_parcel);
331
332 if (binder == nullptr || *out == nullptr) {
333 LOG(ERROR) << __func__ << ": requires non-null parameters.";
334 return EX_NULL_POINTER;
335 }
336
337 if ((*out)->getBinder() != binder) {
338 LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
339 << " but called with " << (*out)->getBinder();
340 return EX_ILLEGAL_STATE;
341 }
342
343 if ((**out)->dataAvail() != 0) {
344 LOG(ERROR) << __func__
345 << ": Only part of this transaction was read. There is remaining data left.";
346 return EX_ILLEGAL_STATE;
347 }
348
349 return EX_NONE;
350}