blob: a5a83f3f1977f4464e90e54dc78bbe917d8c2be6 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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#define LOG_TAG "JavaBinder"
18//#define LOG_NDEBUG 0
19
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -080020#include "android_os_Parcel.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021#include "android_util_Binder.h"
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -080022
Steven Moreland2279b252017-07-19 09:50:45 -070023#include <nativehelper/JNIHelp.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024
25#include <fcntl.h>
Mark Salyzyncfd91e72014-04-17 15:40:01 -070026#include <inttypes.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027#include <stdio.h>
Brad Fitzpatrickad8fd282010-03-25 02:01:32 -070028#include <sys/stat.h>
Brad Fitzpatrickad8fd282010-03-25 02:01:32 -070029#include <sys/types.h>
Brad Fitzpatrick8f26b322010-03-25 00:25:37 -070030#include <unistd.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031
Andreas Gampe58383ba2017-06-12 10:43:05 -070032#include <android-base/stringprintf.h>
Mathias Agopian07952722009-05-19 19:08:10 -070033#include <binder/IInterface.h>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070034#include <binder/IServiceManager.h>
Mathias Agopian07952722009-05-19 19:08:10 -070035#include <binder/IPCThreadState.h>
Mathias Agopian07952722009-05-19 19:08:10 -070036#include <binder/Parcel.h>
37#include <binder/ProcessState.h>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070038#include <log/log.h>
39#include <utils/Atomic.h>
40#include <utils/KeyedVector.h>
41#include <utils/List.h>
42#include <utils/Log.h>
Jeff Brown0bde66a2011-11-07 12:50:08 -080043#include <utils/String8.h>
Mark Salyzyn52eb4e02016-09-28 16:15:30 -070044#include <utils/SystemClock.h>
45#include <utils/threads.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046
Steven Moreland2279b252017-07-19 09:50:45 -070047#include <nativehelper/ScopedUtfChars.h>
48#include <nativehelper/ScopedLocalRef.h>
Christopher Tateac5e3502011-08-25 15:48:09 -070049
Andreas Gampe987f79f2014-11-18 17:29:46 -080050#include "core_jni_helpers.h"
51
Steve Block71f2cf12011-10-20 11:56:00 +010052//#undef ALOGV
53//#define ALOGV(...) fprintf(stderr, __VA_ARGS__)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054
Christopher Tate79dd31f2011-03-04 17:45:00 -080055#define DEBUG_DEATH 0
56#if DEBUG_DEATH
Steve Block5baa3a62011-12-20 16:23:08 +000057#define LOGDEATH ALOGD
Christopher Tate79dd31f2011-03-04 17:45:00 -080058#else
Steve Block71f2cf12011-10-20 11:56:00 +010059#define LOGDEATH ALOGV
Christopher Tate79dd31f2011-03-04 17:45:00 -080060#endif
61
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062using namespace android;
63
64// ----------------------------------------------------------------------------
65
66static struct bindernative_offsets_t
67{
68 // Class state.
69 jclass mClass;
70 jmethodID mExecTransact;
71
72 // Object state.
73 jfieldID mObject;
74
75} gBinderOffsets;
76
77// ----------------------------------------------------------------------------
78
79static struct binderinternal_offsets_t
80{
81 // Class state.
82 jclass mClass;
83 jmethodID mForceGc;
84
85} gBinderInternalOffsets;
86
87// ----------------------------------------------------------------------------
88
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089static struct error_offsets_t
90{
91 jclass mClass;
92} gErrorOffsets;
93
94// ----------------------------------------------------------------------------
95
96static struct binderproxy_offsets_t
97{
98 // Class state.
99 jclass mClass;
100 jmethodID mConstructor;
101 jmethodID mSendDeathNotice;
102
103 // Object state.
104 jfieldID mObject;
105 jfieldID mSelf;
Christopher Tatebd8b6f22011-03-01 11:55:27 -0800106 jfieldID mOrgue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
108} gBinderProxyOffsets;
109
Christopher Tate0d4a7922011-08-30 12:09:43 -0700110static struct class_offsets_t
111{
112 jmethodID mGetName;
113} gClassOffsets;
114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115// ----------------------------------------------------------------------------
116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117static struct log_offsets_t
118{
119 // Class state.
120 jclass mClass;
121 jmethodID mLogE;
122} gLogOffsets;
123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124static struct parcel_file_descriptor_offsets_t
125{
126 jclass mClass;
127 jmethodID mConstructor;
128} gParcelFileDescriptorOffsets;
129
Brad Fitzpatrick727de402010-07-07 16:06:39 -0700130static struct strict_mode_callback_offsets_t
131{
132 jclass mClass;
133 jmethodID mCallback;
134} gStrictModeCallbackOffsets;
135
Andreas Gampe75a75d62017-09-08 17:44:05 -0700136static struct thread_dispatch_offsets_t
137{
138 // Class state.
139 jclass mClass;
140 jmethodID mDispatchUncaughtException;
141 jmethodID mCurrentThread;
142} gThreadDispatchOffsets;
143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144// ****************************************************************************
145// ****************************************************************************
146// ****************************************************************************
147
148static volatile int32_t gNumRefsCreated = 0;
149static volatile int32_t gNumProxyRefs = 0;
150static volatile int32_t gNumLocalRefs = 0;
151static volatile int32_t gNumDeathRefs = 0;
152
153static void incRefsCreated(JNIEnv* env)
154{
155 int old = android_atomic_inc(&gNumRefsCreated);
156 if (old == 200) {
157 android_atomic_and(0, &gNumRefsCreated);
158 env->CallStaticVoidMethod(gBinderInternalOffsets.mClass,
159 gBinderInternalOffsets.mForceGc);
160 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100161 ALOGV("Now have %d binder ops", old);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 }
163}
164
165static JavaVM* jnienv_to_javavm(JNIEnv* env)
166{
167 JavaVM* vm;
168 return env->GetJavaVM(&vm) >= 0 ? vm : NULL;
169}
170
171static JNIEnv* javavm_to_jnienv(JavaVM* vm)
172{
173 JNIEnv* env;
174 return vm->GetEnv((void **)&env, JNI_VERSION_1_4) >= 0 ? env : NULL;
175}
176
Andreas Gampe75a75d62017-09-08 17:44:05 -0700177// Report a java.lang.Error (or subclass). This may terminate the runtime.
178static void report_java_lang_error(JNIEnv* env, jthrowable error)
179{
180 // Try to run the uncaught exception machinery.
181 jobject thread = env->CallStaticObjectMethod(gThreadDispatchOffsets.mClass,
182 gThreadDispatchOffsets.mCurrentThread);
183 if (thread != nullptr) {
184 env->CallVoidMethod(thread, gThreadDispatchOffsets.mDispatchUncaughtException,
185 error);
186 // Should not return here, unless more errors occured.
187 }
188 // Some error occurred that meant that either dispatchUncaughtException could not be
189 // called or that it had an error itself (as this should be unreachable under normal
190 // conditions). Clear the exception.
191 env->ExceptionClear();
192}
193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194static void report_exception(JNIEnv* env, jthrowable excep, const char* msg)
195{
196 env->ExceptionClear();
197
198 jstring tagstr = env->NewStringUTF(LOG_TAG);
Mathieu Chartiercf6775e2014-08-06 13:39:17 -0700199 jstring msgstr = NULL;
200 if (tagstr != NULL) {
201 msgstr = env->NewStringUTF(msg);
202 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203
204 if ((tagstr == NULL) || (msgstr == NULL)) {
205 env->ExceptionClear(); /* assume exception (OOM?) was thrown */
Steve Block3762c312012-01-06 19:20:56 +0000206 ALOGE("Unable to call Log.e()\n");
207 ALOGE("%s", msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 goto bail;
209 }
210
211 env->CallStaticIntMethod(
212 gLogOffsets.mClass, gLogOffsets.mLogE, tagstr, msgstr, excep);
213 if (env->ExceptionCheck()) {
214 /* attempting to log the failure has failed */
Steve Block8564c8d2012-01-05 23:22:43 +0000215 ALOGW("Failed trying to log exception, msg='%s'\n", msg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 env->ExceptionClear();
217 }
218
219 if (env->IsInstanceOf(excep, gErrorOffsets.mClass)) {
Andreas Gampe75a75d62017-09-08 17:44:05 -0700220 // Try to report the error. This should not return under normal circumstances.
221 report_java_lang_error(env, excep);
222 // The traditional handling: re-raise and abort.
223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 /*
Narayan Kamath83a24af2017-04-12 11:50:10 +0100225 * It's an Error: Reraise the exception and ask the runtime to abort.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 */
Andreas Gampe58383ba2017-06-12 10:43:05 -0700227
228 // Try to get the exception string. Sometimes logcat isn't available,
229 // so try to add it to the abort message.
230 std::string exc_msg = "(Unknown exception message)";
231 {
232 ScopedLocalRef<jclass> exc_class(env, env->GetObjectClass(excep));
233 jmethodID method_id = env->GetMethodID(exc_class.get(),
234 "toString",
235 "()Ljava/lang/String;");
236 ScopedLocalRef<jstring> jstr(
237 env,
238 reinterpret_cast<jstring>(
239 env->CallObjectMethod(excep, method_id)));
240 env->ExceptionClear(); // Just for good measure.
241 if (jstr.get() != nullptr) {
242 ScopedUtfChars jstr_utf(env, jstr.get());
243 exc_msg = jstr_utf.c_str();
244 }
245 }
246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800247 env->Throw(excep);
Narayan Kamathcfd0f9b2017-04-18 17:48:39 +0100248 ALOGE("java.lang.Error thrown during binder transaction (stack trace follows) : ");
249 env->ExceptionDescribe();
Andreas Gampe58383ba2017-06-12 10:43:05 -0700250
251 std::string error_msg = base::StringPrintf(
252 "java.lang.Error thrown during binder transaction: %s",
253 exc_msg.c_str());
254 env->FatalError(error_msg.c_str());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 }
256
257bail:
258 /* discard local refs created for us by VM */
259 env->DeleteLocalRef(tagstr);
260 env->DeleteLocalRef(msgstr);
261}
262
263class JavaBBinderHolder;
264
265class JavaBBinder : public BBinder
266{
267public:
268 JavaBBinder(JNIEnv* env, jobject object)
269 : mVM(jnienv_to_javavm(env)), mObject(env->NewGlobalRef(object))
270 {
Steve Block71f2cf12011-10-20 11:56:00 +0100271 ALOGV("Creating JavaBBinder %p\n", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 android_atomic_inc(&gNumLocalRefs);
273 incRefsCreated(env);
274 }
275
276 bool checkSubclass(const void* subclassID) const
277 {
278 return subclassID == &gBinderOffsets;
279 }
280
281 jobject object() const
282 {
283 return mObject;
284 }
285
286protected:
287 virtual ~JavaBBinder()
288 {
Steve Block71f2cf12011-10-20 11:56:00 +0100289 ALOGV("Destroying JavaBBinder %p\n", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 android_atomic_dec(&gNumLocalRefs);
291 JNIEnv* env = javavm_to_jnienv(mVM);
292 env->DeleteGlobalRef(mObject);
293 }
294
295 virtual status_t onTransact(
296 uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags = 0)
297 {
298 JNIEnv* env = javavm_to_jnienv(mVM);
299
Steve Block71f2cf12011-10-20 11:56:00 +0100300 ALOGV("onTransact() on %p calling object %p in env %p vm %p\n", this, mObject, env, mVM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301
Brad Fitzpatrick727de402010-07-07 16:06:39 -0700302 IPCThreadState* thread_state = IPCThreadState::self();
Dianne Hackbornce92b0d2014-09-30 11:28:18 -0700303 const int32_t strict_policy_before = thread_state->getStrictModePolicy();
Brad Fitzpatrick727de402010-07-07 16:06:39 -0700304
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 //printf("Transact from %p to Java code sending: ", this);
306 //data.print();
307 //printf("\n");
308 jboolean res = env->CallBooleanMethod(mObject, gBinderOffsets.mExecTransact,
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000309 code, reinterpret_cast<jlong>(&data), reinterpret_cast<jlong>(reply), flags);
Brad Fitzpatrick727de402010-07-07 16:06:39 -0700310
Mathieu Chartier98671c32014-08-20 10:04:08 -0700311 if (env->ExceptionCheck()) {
312 jthrowable excep = env->ExceptionOccurred();
Bjorn Bringert9013ccd2011-04-26 19:10:58 +0100313 report_exception(env, excep,
314 "*** Uncaught remote exception! "
315 "(Exceptions are not yet supported across processes.)");
316 res = JNI_FALSE;
317
318 /* clean up JNI local ref -- we don't return to Java code */
319 env->DeleteLocalRef(excep);
320 }
321
Dianne Hackbornce92b0d2014-09-30 11:28:18 -0700322 // Check if the strict mode state changed while processing the
323 // call. The Binder state will be restored by the underlying
324 // Binder system in IPCThreadState, however we need to take care
325 // of the parallel Java state as well.
326 if (thread_state->getStrictModePolicy() != strict_policy_before) {
Brad Fitzpatrick727de402010-07-07 16:06:39 -0700327 set_dalvik_blockguard_policy(env, strict_policy_before);
328 }
329
Mathieu Chartier98671c32014-08-20 10:04:08 -0700330 if (env->ExceptionCheck()) {
331 jthrowable excep = env->ExceptionOccurred();
332 report_exception(env, excep,
Bjorn Bringert9013ccd2011-04-26 19:10:58 +0100333 "*** Uncaught exception in onBinderStrictModePolicyChange");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 /* clean up JNI local ref -- we don't return to Java code */
Mathieu Chartier98671c32014-08-20 10:04:08 -0700335 env->DeleteLocalRef(excep);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 }
337
Dianne Hackborna53de062012-05-08 18:53:51 -0700338 // Need to always call through the native implementation of
339 // SYSPROPS_TRANSACTION.
340 if (code == SYSPROPS_TRANSACTION) {
341 BBinder::onTransact(code, data, reply, flags);
342 }
343
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 //aout << "onTransact to Java code; result=" << res << endl
345 // << "Transact from " << this << " to Java code returning "
346 // << reply << ": " << *reply << endl;
347 return res != JNI_FALSE ? NO_ERROR : UNKNOWN_TRANSACTION;
348 }
349
350 virtual status_t dump(int fd, const Vector<String16>& args)
351 {
352 return 0;
353 }
354
355private:
356 JavaVM* const mVM;
357 jobject const mObject;
358};
359
360// ----------------------------------------------------------------------------
361
362class JavaBBinderHolder : public RefBase
363{
364public:
Christopher Tate0b414482011-02-17 13:00:38 -0800365 sp<JavaBBinder> get(JNIEnv* env, jobject obj)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 {
367 AutoMutex _l(mLock);
368 sp<JavaBBinder> b = mBinder.promote();
369 if (b == NULL) {
Christopher Tate0b414482011-02-17 13:00:38 -0800370 b = new JavaBBinder(env, obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 mBinder = b;
Mark Salyzyncfd91e72014-04-17 15:40:01 -0700372 ALOGV("Creating JavaBinder %p (refs %p) for Object %p, weakCount=%" PRId32 "\n",
Christopher Tate0b414482011-02-17 13:00:38 -0800373 b.get(), b->getWeakRefs(), obj, b->getWeakRefs()->getWeakCount());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 }
375
376 return b;
377 }
378
379 sp<JavaBBinder> getExisting()
380 {
381 AutoMutex _l(mLock);
382 return mBinder.promote();
383 }
384
385private:
386 Mutex mLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 wp<JavaBBinder> mBinder;
388};
389
390// ----------------------------------------------------------------------------
391
Christopher Tate0b414482011-02-17 13:00:38 -0800392// Per-IBinder death recipient bookkeeping. This is how we reconcile local jobject
393// death recipient references passed in through JNI with the permanent corresponding
394// JavaDeathRecipient objects.
395
396class JavaDeathRecipient;
397
398class DeathRecipientList : public RefBase {
399 List< sp<JavaDeathRecipient> > mList;
400 Mutex mLock;
401
402public:
Christopher Tate79dd31f2011-03-04 17:45:00 -0800403 DeathRecipientList();
Christopher Tate0b414482011-02-17 13:00:38 -0800404 ~DeathRecipientList();
405
406 void add(const sp<JavaDeathRecipient>& recipient);
407 void remove(const sp<JavaDeathRecipient>& recipient);
408 sp<JavaDeathRecipient> find(jobject recipient);
Christopher Tate090c08f2015-05-19 18:16:58 -0700409
410 Mutex& lock(); // Use with care; specifically for mutual exclusion during binder death
Christopher Tate0b414482011-02-17 13:00:38 -0800411};
412
413// ----------------------------------------------------------------------------
414
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415class JavaDeathRecipient : public IBinder::DeathRecipient
416{
417public:
Christopher Tatebd8b6f22011-03-01 11:55:27 -0800418 JavaDeathRecipient(JNIEnv* env, jobject object, const sp<DeathRecipientList>& list)
Christopher Tate86284c62011-08-17 15:19:29 -0700419 : mVM(jnienv_to_javavm(env)), mObject(env->NewGlobalRef(object)),
420 mObjectWeak(NULL), mList(list)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 {
Christopher Tate0b414482011-02-17 13:00:38 -0800422 // These objects manage their own lifetimes so are responsible for final bookkeeping.
423 // The list holds a strong reference to this object.
Christopher Tate79dd31f2011-03-04 17:45:00 -0800424 LOGDEATH("Adding JDR %p to DRL %p", this, list.get());
Christopher Tatebd8b6f22011-03-01 11:55:27 -0800425 list->add(this);
Christopher Tate0b414482011-02-17 13:00:38 -0800426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 android_atomic_inc(&gNumDeathRefs);
428 incRefsCreated(env);
429 }
430
431 void binderDied(const wp<IBinder>& who)
432 {
Christopher Tate79dd31f2011-03-04 17:45:00 -0800433 LOGDEATH("Receiving binderDied() on JavaDeathRecipient %p\n", this);
Christopher Tate86284c62011-08-17 15:19:29 -0700434 if (mObject != NULL) {
435 JNIEnv* env = javavm_to_jnienv(mVM);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436
Christopher Tate86284c62011-08-17 15:19:29 -0700437 env->CallStaticVoidMethod(gBinderProxyOffsets.mClass,
438 gBinderProxyOffsets.mSendDeathNotice, mObject);
Mathieu Chartier98671c32014-08-20 10:04:08 -0700439 if (env->ExceptionCheck()) {
440 jthrowable excep = env->ExceptionOccurred();
Christopher Tate86284c62011-08-17 15:19:29 -0700441 report_exception(env, excep,
442 "*** Uncaught exception returned from death notification!");
443 }
444
Christopher Tate090c08f2015-05-19 18:16:58 -0700445 // Serialize with our containing DeathRecipientList so that we can't
446 // delete the global ref on mObject while the list is being iterated.
447 sp<DeathRecipientList> list = mList.promote();
448 if (list != NULL) {
449 AutoMutex _l(list->lock());
450
451 // Demote from strong ref to weak after binderDied() has been delivered,
452 // to allow the DeathRecipient and BinderProxy to be GC'd if no longer needed.
453 mObjectWeak = env->NewWeakGlobalRef(mObject);
454 env->DeleteGlobalRef(mObject);
455 mObject = NULL;
456 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 }
459
460 void clearReference()
461 {
Christopher Tatebd8b6f22011-03-01 11:55:27 -0800462 sp<DeathRecipientList> list = mList.promote();
463 if (list != NULL) {
Christopher Tate79dd31f2011-03-04 17:45:00 -0800464 LOGDEATH("Removing JDR %p from DRL %p", this, list.get());
Christopher Tatebd8b6f22011-03-01 11:55:27 -0800465 list->remove(this);
Christopher Tate79dd31f2011-03-04 17:45:00 -0800466 } else {
467 LOGDEATH("clearReference() on JDR %p but DRL wp purged", this);
Christopher Tatebd8b6f22011-03-01 11:55:27 -0800468 }
Christopher Tate0b414482011-02-17 13:00:38 -0800469 }
470
471 bool matches(jobject obj) {
Christopher Tate86284c62011-08-17 15:19:29 -0700472 bool result;
Christopher Tate0b414482011-02-17 13:00:38 -0800473 JNIEnv* env = javavm_to_jnienv(mVM);
Christopher Tate86284c62011-08-17 15:19:29 -0700474
475 if (mObject != NULL) {
476 result = env->IsSameObject(obj, mObject);
477 } else {
478 jobject me = env->NewLocalRef(mObjectWeak);
479 result = env->IsSameObject(obj, me);
480 env->DeleteLocalRef(me);
481 }
482 return result;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 }
484
Christopher Tateac5e3502011-08-25 15:48:09 -0700485 void warnIfStillLive() {
486 if (mObject != NULL) {
487 // Okay, something is wrong -- we have a hard reference to a live death
488 // recipient on the VM side, but the list is being torn down.
489 JNIEnv* env = javavm_to_jnienv(mVM);
Christopher Tate0d4a7922011-08-30 12:09:43 -0700490 ScopedLocalRef<jclass> objClassRef(env, env->GetObjectClass(mObject));
491 ScopedLocalRef<jstring> nameRef(env,
492 (jstring) env->CallObjectMethod(objClassRef.get(), gClassOffsets.mGetName));
493 ScopedUtfChars nameUtf(env, nameRef.get());
494 if (nameUtf.c_str() != NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000495 ALOGW("BinderProxy is being destroyed but the application did not call "
Christopher Tate0d4a7922011-08-30 12:09:43 -0700496 "unlinkToDeath to unlink all of its death recipients beforehand. "
497 "Releasing leaked death recipient: %s", nameUtf.c_str());
498 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000499 ALOGW("BinderProxy being destroyed; unable to get DR object name");
Christopher Tate0d4a7922011-08-30 12:09:43 -0700500 env->ExceptionClear();
501 }
Christopher Tateac5e3502011-08-25 15:48:09 -0700502 }
503 }
504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505protected:
506 virtual ~JavaDeathRecipient()
507 {
Steve Block6215d3f2012-01-04 20:05:49 +0000508 //ALOGI("Removing death ref: recipient=%p\n", mObject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 android_atomic_dec(&gNumDeathRefs);
510 JNIEnv* env = javavm_to_jnienv(mVM);
Christopher Tate86284c62011-08-17 15:19:29 -0700511 if (mObject != NULL) {
512 env->DeleteGlobalRef(mObject);
513 } else {
514 env->DeleteWeakGlobalRef(mObjectWeak);
515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 }
517
518private:
Christopher Tate86284c62011-08-17 15:19:29 -0700519 JavaVM* const mVM;
Hans Boehmeb6d62c2017-09-20 15:59:12 -0700520 jobject mObject; // Initial strong ref to Java-side DeathRecipient. Cleared on binderDied().
521 jweak mObjectWeak; // weak ref to the same Java-side DeathRecipient after binderDied().
Christopher Tatebd8b6f22011-03-01 11:55:27 -0800522 wp<DeathRecipientList> mList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523};
524
525// ----------------------------------------------------------------------------
526
Christopher Tate79dd31f2011-03-04 17:45:00 -0800527DeathRecipientList::DeathRecipientList() {
528 LOGDEATH("New DRL @ %p", this);
529}
530
Christopher Tate0b414482011-02-17 13:00:38 -0800531DeathRecipientList::~DeathRecipientList() {
Christopher Tate79dd31f2011-03-04 17:45:00 -0800532 LOGDEATH("Destroy DRL @ %p", this);
Christopher Tate0b414482011-02-17 13:00:38 -0800533 AutoMutex _l(mLock);
534
535 // Should never happen -- the JavaDeathRecipient objects that have added themselves
536 // to the list are holding references on the list object. Only when they are torn
537 // down can the list header be destroyed.
538 if (mList.size() > 0) {
Christopher Tateac5e3502011-08-25 15:48:09 -0700539 List< sp<JavaDeathRecipient> >::iterator iter;
540 for (iter = mList.begin(); iter != mList.end(); iter++) {
541 (*iter)->warnIfStillLive();
542 }
Christopher Tate0b414482011-02-17 13:00:38 -0800543 }
544}
545
546void DeathRecipientList::add(const sp<JavaDeathRecipient>& recipient) {
547 AutoMutex _l(mLock);
548
Christopher Tate79dd31f2011-03-04 17:45:00 -0800549 LOGDEATH("DRL @ %p : add JDR %p", this, recipient.get());
Christopher Tate0b414482011-02-17 13:00:38 -0800550 mList.push_back(recipient);
551}
552
553void DeathRecipientList::remove(const sp<JavaDeathRecipient>& recipient) {
554 AutoMutex _l(mLock);
555
556 List< sp<JavaDeathRecipient> >::iterator iter;
557 for (iter = mList.begin(); iter != mList.end(); iter++) {
558 if (*iter == recipient) {
Christopher Tate79dd31f2011-03-04 17:45:00 -0800559 LOGDEATH("DRL @ %p : remove JDR %p", this, recipient.get());
Christopher Tate0b414482011-02-17 13:00:38 -0800560 mList.erase(iter);
561 return;
562 }
563 }
564}
565
566sp<JavaDeathRecipient> DeathRecipientList::find(jobject recipient) {
567 AutoMutex _l(mLock);
568
569 List< sp<JavaDeathRecipient> >::iterator iter;
570 for (iter = mList.begin(); iter != mList.end(); iter++) {
571 if ((*iter)->matches(recipient)) {
572 return *iter;
573 }
574 }
575 return NULL;
576}
577
Christopher Tate090c08f2015-05-19 18:16:58 -0700578Mutex& DeathRecipientList::lock() {
579 return mLock;
580}
581
Christopher Tate0b414482011-02-17 13:00:38 -0800582// ----------------------------------------------------------------------------
583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584namespace android {
585
586static void proxy_cleanup(const void* id, void* obj, void* cleanupCookie)
587{
588 android_atomic_dec(&gNumProxyRefs);
589 JNIEnv* env = javavm_to_jnienv((JavaVM*)cleanupCookie);
590 env->DeleteGlobalRef((jobject)obj);
591}
592
Hans Boehmeb6d62c2017-09-20 15:59:12 -0700593static Mutex gProxyLock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594
595jobject javaObjectForIBinder(JNIEnv* env, const sp<IBinder>& val)
596{
597 if (val == NULL) return NULL;
598
599 if (val->checkSubclass(&gBinderOffsets)) {
600 // One of our own!
601 jobject object = static_cast<JavaBBinder*>(val.get())->object();
Christopher Tate86284c62011-08-17 15:19:29 -0700602 LOGDEATH("objectForBinder %p: it's our own %p!\n", val.get(), object);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800603 return object;
604 }
605
606 // For the rest of the function we will hold this lock, to serialize
Christopher Tate10c3a282016-02-26 17:48:08 -0800607 // looking/creation/destruction of Java proxies for native Binder proxies.
Hans Boehmeb6d62c2017-09-20 15:59:12 -0700608 AutoMutex _l(gProxyLock);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609
610 // Someone else's... do we know about it?
611 jobject object = (jobject)val->findObject(&gBinderProxyOffsets);
612 if (object != NULL) {
Jeff Browna4ca8ea2013-04-02 18:01:38 -0700613 jobject res = jniGetReferent(env, object);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 if (res != NULL) {
Steve Block71f2cf12011-10-20 11:56:00 +0100615 ALOGV("objectForBinder %p: found existing %p!\n", val.get(), res);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 return res;
617 }
Christopher Tate86284c62011-08-17 15:19:29 -0700618 LOGDEATH("Proxy object %p of IBinder %p no longer in working set!!!", object, val.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 android_atomic_dec(&gNumProxyRefs);
620 val->detachObject(&gBinderProxyOffsets);
621 env->DeleteGlobalRef(object);
622 }
623
624 object = env->NewObject(gBinderProxyOffsets.mClass, gBinderProxyOffsets.mConstructor);
625 if (object != NULL) {
Christopher Tate79dd31f2011-03-04 17:45:00 -0800626 LOGDEATH("objectForBinder %p: created new proxy %p !\n", val.get(), object);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 // The proxy holds a reference to the native object.
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000628 env->SetLongField(object, gBinderProxyOffsets.mObject, (jlong)val.get());
Mathias Agopianb1d90c82013-03-06 17:45:42 -0800629 val->incStrong((void*)javaObjectForIBinder);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630
631 // The native object needs to hold a weak reference back to the
632 // proxy, so we can retrieve the same proxy if it is still active.
633 jobject refObject = env->NewGlobalRef(
634 env->GetObjectField(object, gBinderProxyOffsets.mSelf));
635 val->attachObject(&gBinderProxyOffsets, refObject,
636 jnienv_to_javavm(env), proxy_cleanup);
637
Christopher Tatebd8b6f22011-03-01 11:55:27 -0800638 // Also remember the death recipients registered on this proxy
639 sp<DeathRecipientList> drl = new DeathRecipientList;
640 drl->incStrong((void*)javaObjectForIBinder);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000641 env->SetLongField(object, gBinderProxyOffsets.mOrgue, reinterpret_cast<jlong>(drl.get()));
Christopher Tatebd8b6f22011-03-01 11:55:27 -0800642
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643 // Note that a new object reference has been created.
644 android_atomic_inc(&gNumProxyRefs);
645 incRefsCreated(env);
646 }
647
648 return object;
649}
650
651sp<IBinder> ibinderForJavaObject(JNIEnv* env, jobject obj)
652{
653 if (obj == NULL) return NULL;
654
655 if (env->IsInstanceOf(obj, gBinderOffsets.mClass)) {
656 JavaBBinderHolder* jbh = (JavaBBinderHolder*)
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000657 env->GetLongField(obj, gBinderOffsets.mObject);
Christopher Tate0b414482011-02-17 13:00:38 -0800658 return jbh != NULL ? jbh->get(env, obj) : NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659 }
660
661 if (env->IsInstanceOf(obj, gBinderProxyOffsets.mClass)) {
662 return (IBinder*)
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000663 env->GetLongField(obj, gBinderProxyOffsets.mObject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800664 }
665
Steve Block8564c8d2012-01-05 23:22:43 +0000666 ALOGW("ibinderForJavaObject: %p is not a Binder object", obj);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667 return NULL;
668}
669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670jobject newParcelFileDescriptor(JNIEnv* env, jobject fileDesc)
671{
672 return env->NewObject(
673 gParcelFileDescriptorOffsets.mClass, gParcelFileDescriptorOffsets.mConstructor, fileDesc);
674}
675
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -0800676void set_dalvik_blockguard_policy(JNIEnv* env, jint strict_policy)
677{
678 // Call back into android.os.StrictMode#onBinderStrictModePolicyChange
679 // to sync our state back to it. See the comments in StrictMode.java.
680 env->CallStaticVoidMethod(gStrictModeCallbackOffsets.mClass,
681 gStrictModeCallbackOffsets.mCallback,
682 strict_policy);
683}
684
685void signalExceptionForError(JNIEnv* env, jobject obj, status_t err,
Dianne Hackborne5c42622015-05-19 16:04:04 -0700686 bool canThrowRemoteException, int parcelSize)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687{
688 switch (err) {
689 case UNKNOWN_ERROR:
690 jniThrowException(env, "java/lang/RuntimeException", "Unknown error");
691 break;
692 case NO_MEMORY:
693 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
694 break;
695 case INVALID_OPERATION:
696 jniThrowException(env, "java/lang/UnsupportedOperationException", NULL);
697 break;
698 case BAD_VALUE:
699 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
700 break;
701 case BAD_INDEX:
702 jniThrowException(env, "java/lang/IndexOutOfBoundsException", NULL);
703 break;
704 case BAD_TYPE:
705 jniThrowException(env, "java/lang/IllegalArgumentException", NULL);
706 break;
707 case NAME_NOT_FOUND:
708 jniThrowException(env, "java/util/NoSuchElementException", NULL);
709 break;
710 case PERMISSION_DENIED:
711 jniThrowException(env, "java/lang/SecurityException", NULL);
712 break;
713 case NOT_ENOUGH_DATA:
714 jniThrowException(env, "android/os/ParcelFormatException", "Not enough data");
715 break;
716 case NO_INIT:
717 jniThrowException(env, "java/lang/RuntimeException", "Not initialized");
718 break;
719 case ALREADY_EXISTS:
720 jniThrowException(env, "java/lang/RuntimeException", "Item already exists");
721 break;
722 case DEAD_OBJECT:
Jeff Brown0bde66a2011-11-07 12:50:08 -0800723 // DeadObjectException is a checked exception, only throw from certain methods.
724 jniThrowException(env, canThrowRemoteException
725 ? "android/os/DeadObjectException"
726 : "java/lang/RuntimeException", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 break;
728 case UNKNOWN_TRANSACTION:
729 jniThrowException(env, "java/lang/RuntimeException", "Unknown transaction code");
730 break;
Dianne Hackborne5c42622015-05-19 16:04:04 -0700731 case FAILED_TRANSACTION: {
732 ALOGE("!!! FAILED BINDER TRANSACTION !!! (parcel size = %d)", parcelSize);
Christopher Tate02ca7a72015-06-24 18:16:42 -0700733 const char* exceptionToThrow;
Dianne Hackborne5c42622015-05-19 16:04:04 -0700734 char msg[128];
Jeff Brown0bde66a2011-11-07 12:50:08 -0800735 // TransactionTooLargeException is a checked exception, only throw from certain methods.
736 // FIXME: Transaction too large is the most common reason for FAILED_TRANSACTION
737 // but it is not the only one. The Binder driver can return BR_FAILED_REPLY
738 // for other reasons also, such as if the transaction is malformed or
739 // refers to an FD that has been closed. We should change the driver
740 // to enable us to distinguish these cases in the future.
Christopher Tate02ca7a72015-06-24 18:16:42 -0700741 if (canThrowRemoteException && parcelSize > 200*1024) {
742 // bona fide large payload
743 exceptionToThrow = "android/os/TransactionTooLargeException";
744 snprintf(msg, sizeof(msg)-1, "data parcel size %d bytes", parcelSize);
745 } else {
746 // Heuristic: a payload smaller than this threshold "shouldn't" be too
747 // big, so it's probably some other, more subtle problem. In practice
Christopher Tateffd58642015-06-29 11:00:15 -0700748 // it seems to always mean that the remote process died while the binder
Christopher Tate02ca7a72015-06-24 18:16:42 -0700749 // transaction was already in flight.
Christopher Tateffd58642015-06-29 11:00:15 -0700750 exceptionToThrow = (canThrowRemoteException)
751 ? "android/os/DeadObjectException"
752 : "java/lang/RuntimeException";
Christopher Tate02ca7a72015-06-24 18:16:42 -0700753 snprintf(msg, sizeof(msg)-1,
754 "Transaction failed on small parcel; remote process probably died");
755 }
756 jniThrowException(env, exceptionToThrow, msg);
Dianne Hackborne5c42622015-05-19 16:04:04 -0700757 } break;
Dianne Hackborn9ecebbf2011-09-28 23:19:47 -0400758 case FDS_NOT_ALLOWED:
759 jniThrowException(env, "java/lang/RuntimeException",
760 "Not allowed to write file descriptors here");
761 break;
Christopher Wileya94fc522015-11-22 14:21:25 -0800762 case UNEXPECTED_NULL:
763 jniThrowNullPointerException(env, NULL);
764 break;
Dianne Hackborncbefd8d2014-05-14 11:42:00 -0700765 case -EBADF:
766 jniThrowException(env, "java/lang/RuntimeException",
767 "Bad file descriptor");
768 break;
769 case -ENFILE:
770 jniThrowException(env, "java/lang/RuntimeException",
771 "File table overflow");
772 break;
773 case -EMFILE:
774 jniThrowException(env, "java/lang/RuntimeException",
775 "Too many open files");
776 break;
777 case -EFBIG:
778 jniThrowException(env, "java/lang/RuntimeException",
779 "File too large");
780 break;
781 case -ENOSPC:
782 jniThrowException(env, "java/lang/RuntimeException",
783 "No space left on device");
784 break;
785 case -ESPIPE:
786 jniThrowException(env, "java/lang/RuntimeException",
787 "Illegal seek");
788 break;
789 case -EROFS:
790 jniThrowException(env, "java/lang/RuntimeException",
791 "Read-only file system");
792 break;
793 case -EMLINK:
794 jniThrowException(env, "java/lang/RuntimeException",
795 "Too many links");
796 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 default:
Mark Salyzyncfd91e72014-04-17 15:40:01 -0700798 ALOGE("Unknown binder error code. 0x%" PRIx32, err);
Jeff Brown0bde66a2011-11-07 12:50:08 -0800799 String8 msg;
Mark Salyzyncfd91e72014-04-17 15:40:01 -0700800 msg.appendFormat("Unknown binder error code. 0x%" PRIx32, err);
Jeff Brown0bde66a2011-11-07 12:50:08 -0800801 // RemoteException is a checked exception, only throw from certain methods.
802 jniThrowException(env, canThrowRemoteException
803 ? "android/os/RemoteException" : "java/lang/RuntimeException", msg.string());
804 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 }
806}
807
808}
809
810// ----------------------------------------------------------------------------
811
812static jint android_os_Binder_getCallingPid(JNIEnv* env, jobject clazz)
813{
814 return IPCThreadState::self()->getCallingPid();
815}
816
817static jint android_os_Binder_getCallingUid(JNIEnv* env, jobject clazz)
818{
819 return IPCThreadState::self()->getCallingUid();
820}
821
822static jlong android_os_Binder_clearCallingIdentity(JNIEnv* env, jobject clazz)
823{
824 return IPCThreadState::self()->clearCallingIdentity();
825}
826
827static void android_os_Binder_restoreCallingIdentity(JNIEnv* env, jobject clazz, jlong token)
828{
Dianne Hackborncf3004a2011-03-14 14:24:04 -0700829 // XXX temporary sanity check to debug crashes.
830 int uid = (int)(token>>32);
831 if (uid > 0 && uid < 999) {
832 // In Android currently there are no uids in this range.
833 char buf[128];
Mark Salyzyncfd91e72014-04-17 15:40:01 -0700834 sprintf(buf, "Restoring bad calling ident: 0x%" PRIx64, token);
Dianne Hackborncf3004a2011-03-14 14:24:04 -0700835 jniThrowException(env, "java/lang/IllegalStateException", buf);
836 return;
837 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 IPCThreadState::self()->restoreCallingIdentity(token);
839}
840
Brad Fitzpatrick727de402010-07-07 16:06:39 -0700841static void android_os_Binder_setThreadStrictModePolicy(JNIEnv* env, jobject clazz, jint policyMask)
842{
843 IPCThreadState::self()->setStrictModePolicy(policyMask);
844}
845
846static jint android_os_Binder_getThreadStrictModePolicy(JNIEnv* env, jobject clazz)
847{
848 return IPCThreadState::self()->getStrictModePolicy();
849}
850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851static void android_os_Binder_flushPendingCommands(JNIEnv* env, jobject clazz)
852{
853 IPCThreadState::self()->flushCommands();
854}
855
Christopher Tate0b414482011-02-17 13:00:38 -0800856static void android_os_Binder_init(JNIEnv* env, jobject obj)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857{
Christopher Tate0b414482011-02-17 13:00:38 -0800858 JavaBBinderHolder* jbh = new JavaBBinderHolder();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 if (jbh == NULL) {
860 jniThrowException(env, "java/lang/OutOfMemoryError", NULL);
861 return;
862 }
Steve Block71f2cf12011-10-20 11:56:00 +0100863 ALOGV("Java Binder %p: acquiring first ref on holder %p", obj, jbh);
Christopher Tate0b414482011-02-17 13:00:38 -0800864 jbh->incStrong((void*)android_os_Binder_init);
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000865 env->SetLongField(obj, gBinderOffsets.mObject, (jlong)jbh);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866}
867
Vladimir Markob25a5322017-06-21 14:40:12 +0100868static void android_os_Binder_destroyBinder(JNIEnv* env, jobject obj)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869{
870 JavaBBinderHolder* jbh = (JavaBBinderHolder*)
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000871 env->GetLongField(obj, gBinderOffsets.mObject);
Jeff Brown582763a2010-03-24 18:56:57 -0700872 if (jbh != NULL) {
Ashok Bhat8ab665d2014-01-22 16:00:20 +0000873 env->SetLongField(obj, gBinderOffsets.mObject, 0);
Steve Block71f2cf12011-10-20 11:56:00 +0100874 ALOGV("Java Binder %p: removing ref on holder %p", obj, jbh);
Christopher Tate0b414482011-02-17 13:00:38 -0800875 jbh->decStrong((void*)android_os_Binder_init);
Jeff Brown582763a2010-03-24 18:56:57 -0700876 } else {
877 // Encountering an uninitialized binder is harmless. All it means is that
878 // the Binder was only partially initialized when its finalizer ran and called
Vladimir Markob25a5322017-06-21 14:40:12 +0100879 // destroyBinder(). The Binder could be partially initialized for several reasons.
Jeff Brown582763a2010-03-24 18:56:57 -0700880 // For example, a Binder subclass constructor might have thrown an exception before
881 // it could delegate to its superclass's constructor. Consequently init() would
882 // not have been called and the holder pointer would remain NULL.
Steve Block71f2cf12011-10-20 11:56:00 +0100883 ALOGV("Java Binder %p: ignoring uninitialized binder", obj);
Jeff Brown582763a2010-03-24 18:56:57 -0700884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885}
886
Wale Ogunwaled7fdd022015-04-13 16:22:38 -0700887static void android_os_Binder_blockUntilThreadAvailable(JNIEnv* env, jobject clazz)
888{
889 return IPCThreadState::self()->blockUntilThreadAvailable();
890}
891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892// ----------------------------------------------------------------------------
893
894static const JNINativeMethod gBinderMethods[] = {
895 /* name, signature, funcPtr */
896 { "getCallingPid", "()I", (void*)android_os_Binder_getCallingPid },
897 { "getCallingUid", "()I", (void*)android_os_Binder_getCallingUid },
898 { "clearCallingIdentity", "()J", (void*)android_os_Binder_clearCallingIdentity },
899 { "restoreCallingIdentity", "(J)V", (void*)android_os_Binder_restoreCallingIdentity },
Brad Fitzpatrick727de402010-07-07 16:06:39 -0700900 { "setThreadStrictModePolicy", "(I)V", (void*)android_os_Binder_setThreadStrictModePolicy },
901 { "getThreadStrictModePolicy", "()I", (void*)android_os_Binder_getThreadStrictModePolicy },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 { "flushPendingCommands", "()V", (void*)android_os_Binder_flushPendingCommands },
903 { "init", "()V", (void*)android_os_Binder_init },
Vladimir Markob25a5322017-06-21 14:40:12 +0100904 { "destroyBinder", "()V", (void*)android_os_Binder_destroyBinder },
Wale Ogunwaled7fdd022015-04-13 16:22:38 -0700905 { "blockUntilThreadAvailable", "()V", (void*)android_os_Binder_blockUntilThreadAvailable }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906};
907
908const char* const kBinderPathName = "android/os/Binder";
909
910static int int_register_android_os_Binder(JNIEnv* env)
911{
Andreas Gampe987f79f2014-11-18 17:29:46 -0800912 jclass clazz = FindClassOrDie(env, kBinderPathName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913
Andreas Gampe987f79f2014-11-18 17:29:46 -0800914 gBinderOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
915 gBinderOffsets.mExecTransact = GetMethodIDOrDie(env, clazz, "execTransact", "(IJJI)Z");
916 gBinderOffsets.mObject = GetFieldIDOrDie(env, clazz, "mObject", "J");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800918 return RegisterMethodsOrDie(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 env, kBinderPathName,
920 gBinderMethods, NELEM(gBinderMethods));
921}
922
923// ****************************************************************************
924// ****************************************************************************
925// ****************************************************************************
926
927namespace android {
928
929jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz)
930{
931 return gNumLocalRefs;
932}
933
934jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz)
935{
936 return gNumProxyRefs;
937}
938
939jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz)
940{
941 return gNumDeathRefs;
942}
943
944}
945
946// ****************************************************************************
947// ****************************************************************************
948// ****************************************************************************
949
950static jobject android_os_BinderInternal_getContextObject(JNIEnv* env, jobject clazz)
951{
952 sp<IBinder> b = ProcessState::self()->getContextObject(NULL);
953 return javaObjectForIBinder(env, b);
954}
955
956static void android_os_BinderInternal_joinThreadPool(JNIEnv* env, jobject clazz)
957{
958 sp<IBinder> b = ProcessState::self()->getContextObject(NULL);
959 android::IPCThreadState::self()->joinThreadPool();
960}
961
Dianne Hackborn887f3552009-12-07 17:59:37 -0800962static void android_os_BinderInternal_disableBackgroundScheduling(JNIEnv* env,
963 jobject clazz, jboolean disable)
964{
965 IPCThreadState::disableBackgroundScheduling(disable ? true : false);
966}
967
Tim Murrayeef4a3d2016-04-19 14:14:20 -0700968static void android_os_BinderInternal_setMaxThreads(JNIEnv* env,
969 jobject clazz, jint maxThreads)
970{
971 ProcessState::self()->setThreadPoolMaxThreadCount(maxThreads);
972}
973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800974static void android_os_BinderInternal_handleGc(JNIEnv* env, jobject clazz)
975{
Steve Block71f2cf12011-10-20 11:56:00 +0100976 ALOGV("Gc has executed, clearing binder ops");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800977 android_atomic_and(0, &gNumRefsCreated);
978}
979
980// ----------------------------------------------------------------------------
981
982static const JNINativeMethod gBinderInternalMethods[] = {
983 /* name, signature, funcPtr */
984 { "getContextObject", "()Landroid/os/IBinder;", (void*)android_os_BinderInternal_getContextObject },
985 { "joinThreadPool", "()V", (void*)android_os_BinderInternal_joinThreadPool },
Dianne Hackborn887f3552009-12-07 17:59:37 -0800986 { "disableBackgroundScheduling", "(Z)V", (void*)android_os_BinderInternal_disableBackgroundScheduling },
Tim Murrayeef4a3d2016-04-19 14:14:20 -0700987 { "setMaxThreads", "(I)V", (void*)android_os_BinderInternal_setMaxThreads },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800988 { "handleGc", "()V", (void*)android_os_BinderInternal_handleGc }
989};
990
991const char* const kBinderInternalPathName = "com/android/internal/os/BinderInternal";
992
993static int int_register_android_os_BinderInternal(JNIEnv* env)
994{
Andreas Gampe987f79f2014-11-18 17:29:46 -0800995 jclass clazz = FindClassOrDie(env, kBinderInternalPathName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996
Andreas Gampe987f79f2014-11-18 17:29:46 -0800997 gBinderInternalOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
998 gBinderInternalOffsets.mForceGc = GetStaticMethodIDOrDie(env, clazz, "forceBinderGc", "()V");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001000 return RegisterMethodsOrDie(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 env, kBinderInternalPathName,
1002 gBinderInternalMethods, NELEM(gBinderInternalMethods));
1003}
1004
1005// ****************************************************************************
1006// ****************************************************************************
1007// ****************************************************************************
1008
1009static jboolean android_os_BinderProxy_pingBinder(JNIEnv* env, jobject obj)
1010{
1011 IBinder* target = (IBinder*)
Ashok Bhat8ab665d2014-01-22 16:00:20 +00001012 env->GetLongField(obj, gBinderProxyOffsets.mObject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 if (target == NULL) {
1014 return JNI_FALSE;
1015 }
1016 status_t err = target->pingBinder();
1017 return err == NO_ERROR ? JNI_TRUE : JNI_FALSE;
1018}
1019
1020static jstring android_os_BinderProxy_getInterfaceDescriptor(JNIEnv* env, jobject obj)
1021{
Ashok Bhat8ab665d2014-01-22 16:00:20 +00001022 IBinder* target = (IBinder*) env->GetLongField(obj, gBinderProxyOffsets.mObject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 if (target != NULL) {
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -07001024 const String16& desc = target->getInterfaceDescriptor();
Dan Albert66987492014-11-20 11:41:21 -08001025 return env->NewString(reinterpret_cast<const jchar*>(desc.string()),
1026 desc.size());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 }
1028 jniThrowException(env, "java/lang/RuntimeException",
1029 "No binder found for object");
1030 return NULL;
1031}
1032
1033static jboolean android_os_BinderProxy_isBinderAlive(JNIEnv* env, jobject obj)
1034{
1035 IBinder* target = (IBinder*)
Ashok Bhat8ab665d2014-01-22 16:00:20 +00001036 env->GetLongField(obj, gBinderProxyOffsets.mObject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 if (target == NULL) {
1038 return JNI_FALSE;
1039 }
1040 bool alive = target->isBinderAlive();
1041 return alive ? JNI_TRUE : JNI_FALSE;
1042}
1043
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -07001044static int getprocname(pid_t pid, char *buf, size_t len) {
Sungmin Choiec3d44c2012-12-21 14:24:33 +09001045 char filename[32];
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -07001046 FILE *f;
1047
Sungmin Choiec3d44c2012-12-21 14:24:33 +09001048 snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid);
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -07001049 f = fopen(filename, "r");
Sungmin Choiec3d44c2012-12-21 14:24:33 +09001050 if (!f) {
1051 *buf = '\0';
1052 return 1;
1053 }
1054 if (!fgets(buf, len, f)) {
1055 *buf = '\0';
1056 fclose(f);
1057 return 2;
1058 }
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -07001059 fclose(f);
1060 return 0;
1061}
1062
1063static bool push_eventlog_string(char** pos, const char* end, const char* str) {
1064 jint len = strlen(str);
1065 int space_needed = 1 + sizeof(len) + len;
1066 if (end - *pos < space_needed) {
Mark Salyzyn5b6da1a2014-04-17 17:25:36 -07001067 ALOGW("not enough space for string. remain=%" PRIdPTR "; needed=%d",
Mark Salyzyncfd91e72014-04-17 15:40:01 -07001068 end - *pos, space_needed);
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -07001069 return false;
1070 }
1071 **pos = EVENT_TYPE_STRING;
1072 (*pos)++;
1073 memcpy(*pos, &len, sizeof(len));
1074 *pos += sizeof(len);
1075 memcpy(*pos, str, len);
1076 *pos += len;
1077 return true;
1078}
1079
1080static bool push_eventlog_int(char** pos, const char* end, jint val) {
1081 int space_needed = 1 + sizeof(val);
1082 if (end - *pos < space_needed) {
Mark Salyzyn5b6da1a2014-04-17 17:25:36 -07001083 ALOGW("not enough space for int. remain=%" PRIdPTR "; needed=%d",
Mark Salyzyncfd91e72014-04-17 15:40:01 -07001084 end - *pos, space_needed);
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -07001085 return false;
1086 }
1087 **pos = EVENT_TYPE_INT;
1088 (*pos)++;
1089 memcpy(*pos, &val, sizeof(val));
1090 *pos += sizeof(val);
1091 return true;
1092}
1093
1094// From frameworks/base/core/java/android/content/EventLogTags.logtags:
Andreas Gampe0f0b4912014-11-12 08:03:48 -08001095
1096static const bool kEnableBinderSample = false;
1097
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -07001098#define LOGTAG_BINDER_OPERATION 52004
1099
1100static void conditionally_log_binder_call(int64_t start_millis,
1101 IBinder* target, jint code) {
1102 int duration_ms = static_cast<int>(uptimeMillis() - start_millis);
1103
1104 int sample_percent;
1105 if (duration_ms >= 500) {
1106 sample_percent = 100;
1107 } else {
1108 sample_percent = 100 * duration_ms / 500;
1109 if (sample_percent == 0) {
1110 return;
1111 }
1112 if (sample_percent < (random() % 100 + 1)) {
1113 return;
1114 }
1115 }
1116
1117 char process_name[40];
1118 getprocname(getpid(), process_name, sizeof(process_name));
1119 String8 desc(target->getInterfaceDescriptor());
1120
1121 char buf[LOGGER_ENTRY_MAX_PAYLOAD];
1122 buf[0] = EVENT_TYPE_LIST;
1123 buf[1] = 5;
1124 char* pos = &buf[2];
1125 char* end = &buf[LOGGER_ENTRY_MAX_PAYLOAD - 1]; // leave room for final \n
1126 if (!push_eventlog_string(&pos, end, desc.string())) return;
1127 if (!push_eventlog_int(&pos, end, code)) return;
1128 if (!push_eventlog_int(&pos, end, duration_ms)) return;
1129 if (!push_eventlog_string(&pos, end, process_name)) return;
1130 if (!push_eventlog_int(&pos, end, sample_percent)) return;
1131 *(pos++) = '\n'; // conventional with EVENT_TYPE_LIST apparently.
1132 android_bWriteLog(LOGTAG_BINDER_OPERATION, buf, pos - buf);
1133}
1134
Brad Fitzpatrickad8fd282010-03-25 02:01:32 -07001135// We only measure binder call durations to potentially log them if
Elliott Hughes06451fe2014-08-18 10:26:52 -07001136// we're on the main thread.
Brad Fitzpatrickad8fd282010-03-25 02:01:32 -07001137static bool should_time_binder_calls() {
Elliott Hughes06451fe2014-08-18 10:26:52 -07001138 return (getpid() == gettid());
Brad Fitzpatrickad8fd282010-03-25 02:01:32 -07001139}
1140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141static jboolean android_os_BinderProxy_transact(JNIEnv* env, jobject obj,
Jeff Brown0bde66a2011-11-07 12:50:08 -08001142 jint code, jobject dataObj, jobject replyObj, jint flags) // throws RemoteException
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143{
1144 if (dataObj == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -07001145 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 return JNI_FALSE;
1147 }
1148
1149 Parcel* data = parcelForJavaObject(env, dataObj);
1150 if (data == NULL) {
1151 return JNI_FALSE;
1152 }
1153 Parcel* reply = parcelForJavaObject(env, replyObj);
1154 if (reply == NULL && replyObj != NULL) {
1155 return JNI_FALSE;
1156 }
1157
1158 IBinder* target = (IBinder*)
Ashok Bhat8ab665d2014-01-22 16:00:20 +00001159 env->GetLongField(obj, gBinderProxyOffsets.mObject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 if (target == NULL) {
1161 jniThrowException(env, "java/lang/IllegalStateException", "Binder has been finalized!");
1162 return JNI_FALSE;
1163 }
1164
Mark Salyzyncfd91e72014-04-17 15:40:01 -07001165 ALOGV("Java code calling transact on %p in Java object %p with code %" PRId32 "\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001166 target, obj, code);
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -07001167
Brad Fitzpatrickad8fd282010-03-25 02:01:32 -07001168
Andreas Gampe0f0b4912014-11-12 08:03:48 -08001169 bool time_binder_calls;
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -07001170 int64_t start_millis;
Andreas Gampe0f0b4912014-11-12 08:03:48 -08001171 if (kEnableBinderSample) {
1172 // Only log the binder call duration for things on the Java-level main thread.
1173 // But if we don't
1174 time_binder_calls = should_time_binder_calls();
1175
1176 if (time_binder_calls) {
1177 start_millis = uptimeMillis();
1178 }
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -07001179 }
Andreas Gampe0f0b4912014-11-12 08:03:48 -08001180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 //printf("Transact from Java code to %p sending: ", target); data->print();
1182 status_t err = target->transact(code, *data, reply, flags);
1183 //if (reply) printf("Transact from Java code to %p received: ", target); reply->print();
Andreas Gampe0f0b4912014-11-12 08:03:48 -08001184
1185 if (kEnableBinderSample) {
1186 if (time_binder_calls) {
1187 conditionally_log_binder_call(start_millis, target, code);
1188 }
Brad Fitzpatrick2c5da312010-03-24 16:14:09 -07001189 }
1190
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 if (err == NO_ERROR) {
1192 return JNI_TRUE;
1193 } else if (err == UNKNOWN_TRANSACTION) {
1194 return JNI_FALSE;
1195 }
1196
Dianne Hackborne5c42622015-05-19 16:04:04 -07001197 signalExceptionForError(env, obj, err, true /*canThrowRemoteException*/, data->dataSize());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 return JNI_FALSE;
1199}
1200
1201static void android_os_BinderProxy_linkToDeath(JNIEnv* env, jobject obj,
Jeff Brown0bde66a2011-11-07 12:50:08 -08001202 jobject recipient, jint flags) // throws RemoteException
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203{
1204 if (recipient == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -07001205 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 return;
1207 }
1208
1209 IBinder* target = (IBinder*)
Ashok Bhat8ab665d2014-01-22 16:00:20 +00001210 env->GetLongField(obj, gBinderProxyOffsets.mObject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 if (target == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00001212 ALOGW("Binder has been finalized when calling linkToDeath() with recip=%p)\n", recipient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 assert(false);
1214 }
1215
Christopher Tate79dd31f2011-03-04 17:45:00 -08001216 LOGDEATH("linkToDeath: binder=%p recipient=%p\n", target, recipient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001217
1218 if (!target->localBinder()) {
Christopher Tatebd8b6f22011-03-01 11:55:27 -08001219 DeathRecipientList* list = (DeathRecipientList*)
Ashok Bhat8ab665d2014-01-22 16:00:20 +00001220 env->GetLongField(obj, gBinderProxyOffsets.mOrgue);
Christopher Tatebd8b6f22011-03-01 11:55:27 -08001221 sp<JavaDeathRecipient> jdr = new JavaDeathRecipient(env, recipient, list);
Christopher Tate0b414482011-02-17 13:00:38 -08001222 status_t err = target->linkToDeath(jdr, NULL, flags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 if (err != NO_ERROR) {
1224 // Failure adding the death recipient, so clear its reference
1225 // now.
1226 jdr->clearReference();
Jeff Brown0bde66a2011-11-07 12:50:08 -08001227 signalExceptionForError(env, obj, err, true /*canThrowRemoteException*/);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 }
1229 }
1230}
1231
1232static jboolean android_os_BinderProxy_unlinkToDeath(JNIEnv* env, jobject obj,
1233 jobject recipient, jint flags)
1234{
1235 jboolean res = JNI_FALSE;
1236 if (recipient == NULL) {
Elliott Hughes69a017b2011-04-08 14:10:28 -07001237 jniThrowNullPointerException(env, NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 return res;
1239 }
1240
1241 IBinder* target = (IBinder*)
Ashok Bhat8ab665d2014-01-22 16:00:20 +00001242 env->GetLongField(obj, gBinderProxyOffsets.mObject);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 if (target == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00001244 ALOGW("Binder has been finalized when calling linkToDeath() with recip=%p)\n", recipient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 return JNI_FALSE;
1246 }
1247
Christopher Tate79dd31f2011-03-04 17:45:00 -08001248 LOGDEATH("unlinkToDeath: binder=%p recipient=%p\n", target, recipient);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249
1250 if (!target->localBinder()) {
Christopher Tate0b414482011-02-17 13:00:38 -08001251 status_t err = NAME_NOT_FOUND;
Christopher Tatebd8b6f22011-03-01 11:55:27 -08001252
1253 // If we find the matching recipient, proceed to unlink using that
1254 DeathRecipientList* list = (DeathRecipientList*)
Ashok Bhat8ab665d2014-01-22 16:00:20 +00001255 env->GetLongField(obj, gBinderProxyOffsets.mOrgue);
Christopher Tatebd8b6f22011-03-01 11:55:27 -08001256 sp<JavaDeathRecipient> origJDR = list->find(recipient);
Christopher Tate79dd31f2011-03-04 17:45:00 -08001257 LOGDEATH(" unlink found list %p and JDR %p", list, origJDR.get());
Christopher Tate0b414482011-02-17 13:00:38 -08001258 if (origJDR != NULL) {
1259 wp<IBinder::DeathRecipient> dr;
1260 err = target->unlinkToDeath(origJDR, NULL, flags, &dr);
1261 if (err == NO_ERROR && dr != NULL) {
1262 sp<IBinder::DeathRecipient> sdr = dr.promote();
1263 JavaDeathRecipient* jdr = static_cast<JavaDeathRecipient*>(sdr.get());
1264 if (jdr != NULL) {
1265 jdr->clearReference();
1266 }
1267 }
1268 }
1269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 if (err == NO_ERROR || err == DEAD_OBJECT) {
1271 res = JNI_TRUE;
1272 } else {
1273 jniThrowException(env, "java/util/NoSuchElementException",
1274 "Death link does not exist");
1275 }
1276 }
1277
1278 return res;
1279}
1280
1281static void android_os_BinderProxy_destroy(JNIEnv* env, jobject obj)
1282{
Christopher Tate10c3a282016-02-26 17:48:08 -08001283 // Don't race with construction/initialization
Hans Boehmeb6d62c2017-09-20 15:59:12 -07001284 AutoMutex _l(gProxyLock);
Christopher Tate10c3a282016-02-26 17:48:08 -08001285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 IBinder* b = (IBinder*)
Ashok Bhat8ab665d2014-01-22 16:00:20 +00001287 env->GetLongField(obj, gBinderProxyOffsets.mObject);
Christopher Tatebd8b6f22011-03-01 11:55:27 -08001288 DeathRecipientList* drl = (DeathRecipientList*)
Ashok Bhat8ab665d2014-01-22 16:00:20 +00001289 env->GetLongField(obj, gBinderProxyOffsets.mOrgue);
Christopher Tate0b414482011-02-17 13:00:38 -08001290
Christopher Tate79dd31f2011-03-04 17:45:00 -08001291 LOGDEATH("Destroying BinderProxy %p: binder=%p drl=%p\n", obj, b, drl);
Christopher Tate10c3a282016-02-26 17:48:08 -08001292 if (b != nullptr) {
1293 env->SetLongField(obj, gBinderProxyOffsets.mObject, 0);
1294 env->SetLongField(obj, gBinderProxyOffsets.mOrgue, 0);
1295 drl->decStrong((void*)javaObjectForIBinder);
1296 b->decStrong((void*)javaObjectForIBinder);
1297 }
Christopher Tatebd8b6f22011-03-01 11:55:27 -08001298
1299 IPCThreadState::self()->flushCommands();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300}
1301
1302// ----------------------------------------------------------------------------
1303
1304static const JNINativeMethod gBinderProxyMethods[] = {
1305 /* name, signature, funcPtr */
1306 {"pingBinder", "()Z", (void*)android_os_BinderProxy_pingBinder},
1307 {"isBinderAlive", "()Z", (void*)android_os_BinderProxy_isBinderAlive},
1308 {"getInterfaceDescriptor", "()Ljava/lang/String;", (void*)android_os_BinderProxy_getInterfaceDescriptor},
Dianne Hackborn017c6a22014-09-25 17:41:34 -07001309 {"transactNative", "(ILandroid/os/Parcel;Landroid/os/Parcel;I)Z", (void*)android_os_BinderProxy_transact},
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 {"linkToDeath", "(Landroid/os/IBinder$DeathRecipient;I)V", (void*)android_os_BinderProxy_linkToDeath},
1311 {"unlinkToDeath", "(Landroid/os/IBinder$DeathRecipient;I)Z", (void*)android_os_BinderProxy_unlinkToDeath},
1312 {"destroy", "()V", (void*)android_os_BinderProxy_destroy},
1313};
1314
1315const char* const kBinderProxyPathName = "android/os/BinderProxy";
1316
1317static int int_register_android_os_BinderProxy(JNIEnv* env)
1318{
Andreas Gampe987f79f2014-11-18 17:29:46 -08001319 jclass clazz = FindClassOrDie(env, "java/lang/Error");
1320 gErrorOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321
Andreas Gampe987f79f2014-11-18 17:29:46 -08001322 clazz = FindClassOrDie(env, kBinderProxyPathName);
1323 gBinderProxyOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
1324 gBinderProxyOffsets.mConstructor = GetMethodIDOrDie(env, clazz, "<init>", "()V");
1325 gBinderProxyOffsets.mSendDeathNotice = GetStaticMethodIDOrDie(env, clazz, "sendDeathNotice",
1326 "(Landroid/os/IBinder$DeathRecipient;)V");
Elliott Hughes69a017b2011-04-08 14:10:28 -07001327
Andreas Gampe987f79f2014-11-18 17:29:46 -08001328 gBinderProxyOffsets.mObject = GetFieldIDOrDie(env, clazz, "mObject", "J");
1329 gBinderProxyOffsets.mSelf = GetFieldIDOrDie(env, clazz, "mSelf",
1330 "Ljava/lang/ref/WeakReference;");
1331 gBinderProxyOffsets.mOrgue = GetFieldIDOrDie(env, clazz, "mOrgue", "J");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332
Andreas Gampe987f79f2014-11-18 17:29:46 -08001333 clazz = FindClassOrDie(env, "java/lang/Class");
1334 gClassOffsets.mGetName = GetMethodIDOrDie(env, clazz, "getName", "()Ljava/lang/String;");
Christopher Tate0d4a7922011-08-30 12:09:43 -07001335
Andreas Gampeed6b9df2014-11-20 22:02:20 -08001336 return RegisterMethodsOrDie(
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 env, kBinderProxyPathName,
1338 gBinderProxyMethods, NELEM(gBinderProxyMethods));
1339}
1340
1341// ****************************************************************************
1342// ****************************************************************************
1343// ****************************************************************************
1344
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -08001345int register_android_os_Binder(JNIEnv* env)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001346{
Jeff Sharkeyd84e1ce2012-03-06 18:26:19 -08001347 if (int_register_android_os_Binder(env) < 0)
1348 return -1;
1349 if (int_register_android_os_BinderInternal(env) < 0)
1350 return -1;
1351 if (int_register_android_os_BinderProxy(env) < 0)
1352 return -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353
Andreas Gampe987f79f2014-11-18 17:29:46 -08001354 jclass clazz = FindClassOrDie(env, "android/util/Log");
1355 gLogOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
1356 gLogOffsets.mLogE = GetStaticMethodIDOrDie(env, clazz, "e",
1357 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/Throwable;)I");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358
Andreas Gampe987f79f2014-11-18 17:29:46 -08001359 clazz = FindClassOrDie(env, "android/os/ParcelFileDescriptor");
1360 gParcelFileDescriptorOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
1361 gParcelFileDescriptorOffsets.mConstructor = GetMethodIDOrDie(env, clazz, "<init>",
1362 "(Ljava/io/FileDescriptor;)V");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001363
Andreas Gampe987f79f2014-11-18 17:29:46 -08001364 clazz = FindClassOrDie(env, "android/os/StrictMode");
1365 gStrictModeCallbackOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
1366 gStrictModeCallbackOffsets.mCallback = GetStaticMethodIDOrDie(env, clazz,
1367 "onBinderStrictModePolicyChange", "(I)V");
Brad Fitzpatrick727de402010-07-07 16:06:39 -07001368
Andreas Gampe75a75d62017-09-08 17:44:05 -07001369 clazz = FindClassOrDie(env, "java/lang/Thread");
1370 gThreadDispatchOffsets.mClass = MakeGlobalRefOrDie(env, clazz);
1371 gThreadDispatchOffsets.mDispatchUncaughtException = GetMethodIDOrDie(env, clazz,
1372 "dispatchUncaughtException", "(Ljava/lang/Throwable;)V");
1373 gThreadDispatchOffsets.mCurrentThread = GetStaticMethodIDOrDie(env, clazz, "currentThread",
1374 "()Ljava/lang/Thread;");
1375
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 return 0;
1377}