The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 20 | #include "android_os_Parcel.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | #include "android_util_Binder.h" |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 22 | |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 23 | #include <nativehelper/JNIHelp.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
| 25 | #include <fcntl.h> |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 26 | #include <inttypes.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | #include <stdio.h> |
Brad Fitzpatrick | ad8fd28 | 2010-03-25 02:01:32 -0700 | [diff] [blame] | 28 | #include <sys/stat.h> |
Brad Fitzpatrick | ad8fd28 | 2010-03-25 02:01:32 -0700 | [diff] [blame] | 29 | #include <sys/types.h> |
Brad Fitzpatrick | 8f26b32 | 2010-03-25 00:25:37 -0700 | [diff] [blame] | 30 | #include <unistd.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | |
Andreas Gampe | 58383ba | 2017-06-12 10:43:05 -0700 | [diff] [blame] | 32 | #include <android-base/stringprintf.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 33 | #include <binder/IInterface.h> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 34 | #include <binder/IServiceManager.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 35 | #include <binder/IPCThreadState.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 36 | #include <binder/Parcel.h> |
| 37 | #include <binder/ProcessState.h> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 38 | #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 Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 43 | #include <utils/String8.h> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 44 | #include <utils/SystemClock.h> |
| 45 | #include <utils/threads.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 47 | #include <nativehelper/ScopedUtfChars.h> |
| 48 | #include <nativehelper/ScopedLocalRef.h> |
Christopher Tate | ac5e350 | 2011-08-25 15:48:09 -0700 | [diff] [blame] | 49 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 50 | #include "core_jni_helpers.h" |
| 51 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 52 | //#undef ALOGV |
| 53 | //#define ALOGV(...) fprintf(stderr, __VA_ARGS__) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 55 | #define DEBUG_DEATH 0 |
| 56 | #if DEBUG_DEATH |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 57 | #define LOGDEATH ALOGD |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 58 | #else |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 59 | #define LOGDEATH ALOGV |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 60 | #endif |
| 61 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | using namespace android; |
| 63 | |
| 64 | // ---------------------------------------------------------------------------- |
| 65 | |
| 66 | static 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 | |
| 79 | static struct binderinternal_offsets_t |
| 80 | { |
| 81 | // Class state. |
| 82 | jclass mClass; |
| 83 | jmethodID mForceGc; |
| 84 | |
| 85 | } gBinderInternalOffsets; |
| 86 | |
| 87 | // ---------------------------------------------------------------------------- |
| 88 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | static struct error_offsets_t |
| 90 | { |
| 91 | jclass mClass; |
| 92 | } gErrorOffsets; |
| 93 | |
| 94 | // ---------------------------------------------------------------------------- |
| 95 | |
| 96 | static 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 Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 106 | jfieldID mOrgue; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | |
| 108 | } gBinderProxyOffsets; |
| 109 | |
Christopher Tate | 0d4a792 | 2011-08-30 12:09:43 -0700 | [diff] [blame] | 110 | static struct class_offsets_t |
| 111 | { |
| 112 | jmethodID mGetName; |
| 113 | } gClassOffsets; |
| 114 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | // ---------------------------------------------------------------------------- |
| 116 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | static struct log_offsets_t |
| 118 | { |
| 119 | // Class state. |
| 120 | jclass mClass; |
| 121 | jmethodID mLogE; |
| 122 | } gLogOffsets; |
| 123 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | static struct parcel_file_descriptor_offsets_t |
| 125 | { |
| 126 | jclass mClass; |
| 127 | jmethodID mConstructor; |
| 128 | } gParcelFileDescriptorOffsets; |
| 129 | |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 130 | static struct strict_mode_callback_offsets_t |
| 131 | { |
| 132 | jclass mClass; |
| 133 | jmethodID mCallback; |
| 134 | } gStrictModeCallbackOffsets; |
| 135 | |
Andreas Gampe | 75a75d6 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 136 | static 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | // **************************************************************************** |
| 145 | // **************************************************************************** |
| 146 | // **************************************************************************** |
| 147 | |
| 148 | static volatile int32_t gNumRefsCreated = 0; |
| 149 | static volatile int32_t gNumProxyRefs = 0; |
| 150 | static volatile int32_t gNumLocalRefs = 0; |
| 151 | static volatile int32_t gNumDeathRefs = 0; |
| 152 | |
| 153 | static 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 Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 161 | ALOGV("Now have %d binder ops", old); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | static JavaVM* jnienv_to_javavm(JNIEnv* env) |
| 166 | { |
| 167 | JavaVM* vm; |
| 168 | return env->GetJavaVM(&vm) >= 0 ? vm : NULL; |
| 169 | } |
| 170 | |
| 171 | static 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 Gampe | 75a75d6 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 177 | // Report a java.lang.Error (or subclass). This may terminate the runtime. |
| 178 | static 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | static void report_exception(JNIEnv* env, jthrowable excep, const char* msg) |
| 195 | { |
| 196 | env->ExceptionClear(); |
| 197 | |
| 198 | jstring tagstr = env->NewStringUTF(LOG_TAG); |
Mathieu Chartier | cf6775e | 2014-08-06 13:39:17 -0700 | [diff] [blame] | 199 | jstring msgstr = NULL; |
| 200 | if (tagstr != NULL) { |
| 201 | msgstr = env->NewStringUTF(msg); |
| 202 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | |
| 204 | if ((tagstr == NULL) || (msgstr == NULL)) { |
| 205 | env->ExceptionClear(); /* assume exception (OOM?) was thrown */ |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 206 | ALOGE("Unable to call Log.e()\n"); |
| 207 | ALOGE("%s", msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | 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 Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 215 | ALOGW("Failed trying to log exception, msg='%s'\n", msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | env->ExceptionClear(); |
| 217 | } |
| 218 | |
| 219 | if (env->IsInstanceOf(excep, gErrorOffsets.mClass)) { |
Andreas Gampe | 75a75d6 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 220 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | /* |
Narayan Kamath | 83a24af | 2017-04-12 11:50:10 +0100 | [diff] [blame] | 225 | * It's an Error: Reraise the exception and ask the runtime to abort. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | */ |
Andreas Gampe | 58383ba | 2017-06-12 10:43:05 -0700 | [diff] [blame] | 227 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | env->Throw(excep); |
Narayan Kamath | cfd0f9b | 2017-04-18 17:48:39 +0100 | [diff] [blame] | 248 | ALOGE("java.lang.Error thrown during binder transaction (stack trace follows) : "); |
| 249 | env->ExceptionDescribe(); |
Andreas Gampe | 58383ba | 2017-06-12 10:43:05 -0700 | [diff] [blame] | 250 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | bail: |
| 258 | /* discard local refs created for us by VM */ |
| 259 | env->DeleteLocalRef(tagstr); |
| 260 | env->DeleteLocalRef(msgstr); |
| 261 | } |
| 262 | |
| 263 | class JavaBBinderHolder; |
| 264 | |
| 265 | class JavaBBinder : public BBinder |
| 266 | { |
| 267 | public: |
| 268 | JavaBBinder(JNIEnv* env, jobject object) |
| 269 | : mVM(jnienv_to_javavm(env)), mObject(env->NewGlobalRef(object)) |
| 270 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 271 | ALOGV("Creating JavaBBinder %p\n", this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | 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 | |
| 286 | protected: |
| 287 | virtual ~JavaBBinder() |
| 288 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 289 | ALOGV("Destroying JavaBBinder %p\n", this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | 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 Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 300 | ALOGV("onTransact() on %p calling object %p in env %p vm %p\n", this, mObject, env, mVM); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 302 | IPCThreadState* thread_state = IPCThreadState::self(); |
Dianne Hackborn | ce92b0d | 2014-09-30 11:28:18 -0700 | [diff] [blame] | 303 | const int32_t strict_policy_before = thread_state->getStrictModePolicy(); |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 304 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 305 | //printf("Transact from %p to Java code sending: ", this); |
| 306 | //data.print(); |
| 307 | //printf("\n"); |
| 308 | jboolean res = env->CallBooleanMethod(mObject, gBinderOffsets.mExecTransact, |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 309 | code, reinterpret_cast<jlong>(&data), reinterpret_cast<jlong>(reply), flags); |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 310 | |
Mathieu Chartier | 98671c3 | 2014-08-20 10:04:08 -0700 | [diff] [blame] | 311 | if (env->ExceptionCheck()) { |
| 312 | jthrowable excep = env->ExceptionOccurred(); |
Bjorn Bringert | 9013ccd | 2011-04-26 19:10:58 +0100 | [diff] [blame] | 313 | 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 Hackborn | ce92b0d | 2014-09-30 11:28:18 -0700 | [diff] [blame] | 322 | // 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 Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 327 | set_dalvik_blockguard_policy(env, strict_policy_before); |
| 328 | } |
| 329 | |
Mathieu Chartier | 98671c3 | 2014-08-20 10:04:08 -0700 | [diff] [blame] | 330 | if (env->ExceptionCheck()) { |
| 331 | jthrowable excep = env->ExceptionOccurred(); |
| 332 | report_exception(env, excep, |
Bjorn Bringert | 9013ccd | 2011-04-26 19:10:58 +0100 | [diff] [blame] | 333 | "*** Uncaught exception in onBinderStrictModePolicyChange"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 334 | /* clean up JNI local ref -- we don't return to Java code */ |
Mathieu Chartier | 98671c3 | 2014-08-20 10:04:08 -0700 | [diff] [blame] | 335 | env->DeleteLocalRef(excep); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | } |
| 337 | |
Dianne Hackborn | a53de06 | 2012-05-08 18:53:51 -0700 | [diff] [blame] | 338 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | //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 | |
| 355 | private: |
| 356 | JavaVM* const mVM; |
| 357 | jobject const mObject; |
| 358 | }; |
| 359 | |
| 360 | // ---------------------------------------------------------------------------- |
| 361 | |
| 362 | class JavaBBinderHolder : public RefBase |
| 363 | { |
| 364 | public: |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 365 | sp<JavaBBinder> get(JNIEnv* env, jobject obj) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 366 | { |
| 367 | AutoMutex _l(mLock); |
| 368 | sp<JavaBBinder> b = mBinder.promote(); |
| 369 | if (b == NULL) { |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 370 | b = new JavaBBinder(env, obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 371 | mBinder = b; |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 372 | ALOGV("Creating JavaBinder %p (refs %p) for Object %p, weakCount=%" PRId32 "\n", |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 373 | b.get(), b->getWeakRefs(), obj, b->getWeakRefs()->getWeakCount()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | return b; |
| 377 | } |
| 378 | |
| 379 | sp<JavaBBinder> getExisting() |
| 380 | { |
| 381 | AutoMutex _l(mLock); |
| 382 | return mBinder.promote(); |
| 383 | } |
| 384 | |
| 385 | private: |
| 386 | Mutex mLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | wp<JavaBBinder> mBinder; |
| 388 | }; |
| 389 | |
| 390 | // ---------------------------------------------------------------------------- |
| 391 | |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 392 | // 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 | |
| 396 | class JavaDeathRecipient; |
| 397 | |
| 398 | class DeathRecipientList : public RefBase { |
| 399 | List< sp<JavaDeathRecipient> > mList; |
| 400 | Mutex mLock; |
| 401 | |
| 402 | public: |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 403 | DeathRecipientList(); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 404 | ~DeathRecipientList(); |
| 405 | |
| 406 | void add(const sp<JavaDeathRecipient>& recipient); |
| 407 | void remove(const sp<JavaDeathRecipient>& recipient); |
| 408 | sp<JavaDeathRecipient> find(jobject recipient); |
Christopher Tate | 090c08f | 2015-05-19 18:16:58 -0700 | [diff] [blame] | 409 | |
| 410 | Mutex& lock(); // Use with care; specifically for mutual exclusion during binder death |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 411 | }; |
| 412 | |
| 413 | // ---------------------------------------------------------------------------- |
| 414 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 415 | class JavaDeathRecipient : public IBinder::DeathRecipient |
| 416 | { |
| 417 | public: |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 418 | JavaDeathRecipient(JNIEnv* env, jobject object, const sp<DeathRecipientList>& list) |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 419 | : mVM(jnienv_to_javavm(env)), mObject(env->NewGlobalRef(object)), |
| 420 | mObjectWeak(NULL), mList(list) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 421 | { |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 422 | // These objects manage their own lifetimes so are responsible for final bookkeeping. |
| 423 | // The list holds a strong reference to this object. |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 424 | LOGDEATH("Adding JDR %p to DRL %p", this, list.get()); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 425 | list->add(this); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 426 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 427 | android_atomic_inc(&gNumDeathRefs); |
| 428 | incRefsCreated(env); |
| 429 | } |
| 430 | |
| 431 | void binderDied(const wp<IBinder>& who) |
| 432 | { |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 433 | LOGDEATH("Receiving binderDied() on JavaDeathRecipient %p\n", this); |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 434 | if (mObject != NULL) { |
| 435 | JNIEnv* env = javavm_to_jnienv(mVM); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 436 | |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 437 | env->CallStaticVoidMethod(gBinderProxyOffsets.mClass, |
| 438 | gBinderProxyOffsets.mSendDeathNotice, mObject); |
Mathieu Chartier | 98671c3 | 2014-08-20 10:04:08 -0700 | [diff] [blame] | 439 | if (env->ExceptionCheck()) { |
| 440 | jthrowable excep = env->ExceptionOccurred(); |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 441 | report_exception(env, excep, |
| 442 | "*** Uncaught exception returned from death notification!"); |
| 443 | } |
| 444 | |
Christopher Tate | 090c08f | 2015-05-19 18:16:58 -0700 | [diff] [blame] | 445 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 457 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | void clearReference() |
| 461 | { |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 462 | sp<DeathRecipientList> list = mList.promote(); |
| 463 | if (list != NULL) { |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 464 | LOGDEATH("Removing JDR %p from DRL %p", this, list.get()); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 465 | list->remove(this); |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 466 | } else { |
| 467 | LOGDEATH("clearReference() on JDR %p but DRL wp purged", this); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 468 | } |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | bool matches(jobject obj) { |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 472 | bool result; |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 473 | JNIEnv* env = javavm_to_jnienv(mVM); |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 474 | |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 483 | } |
| 484 | |
Christopher Tate | ac5e350 | 2011-08-25 15:48:09 -0700 | [diff] [blame] | 485 | 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 Tate | 0d4a792 | 2011-08-30 12:09:43 -0700 | [diff] [blame] | 490 | 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 Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 495 | ALOGW("BinderProxy is being destroyed but the application did not call " |
Christopher Tate | 0d4a792 | 2011-08-30 12:09:43 -0700 | [diff] [blame] | 496 | "unlinkToDeath to unlink all of its death recipients beforehand. " |
| 497 | "Releasing leaked death recipient: %s", nameUtf.c_str()); |
| 498 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 499 | ALOGW("BinderProxy being destroyed; unable to get DR object name"); |
Christopher Tate | 0d4a792 | 2011-08-30 12:09:43 -0700 | [diff] [blame] | 500 | env->ExceptionClear(); |
| 501 | } |
Christopher Tate | ac5e350 | 2011-08-25 15:48:09 -0700 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | protected: |
| 506 | virtual ~JavaDeathRecipient() |
| 507 | { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 508 | //ALOGI("Removing death ref: recipient=%p\n", mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | android_atomic_dec(&gNumDeathRefs); |
| 510 | JNIEnv* env = javavm_to_jnienv(mVM); |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 511 | if (mObject != NULL) { |
| 512 | env->DeleteGlobalRef(mObject); |
| 513 | } else { |
| 514 | env->DeleteWeakGlobalRef(mObjectWeak); |
| 515 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | private: |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 519 | JavaVM* const mVM; |
Hans Boehm | eb6d62c | 2017-09-20 15:59:12 -0700 | [diff] [blame] | 520 | 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 Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 522 | wp<DeathRecipientList> mList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 523 | }; |
| 524 | |
| 525 | // ---------------------------------------------------------------------------- |
| 526 | |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 527 | DeathRecipientList::DeathRecipientList() { |
| 528 | LOGDEATH("New DRL @ %p", this); |
| 529 | } |
| 530 | |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 531 | DeathRecipientList::~DeathRecipientList() { |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 532 | LOGDEATH("Destroy DRL @ %p", this); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 533 | 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 Tate | ac5e350 | 2011-08-25 15:48:09 -0700 | [diff] [blame] | 539 | List< sp<JavaDeathRecipient> >::iterator iter; |
| 540 | for (iter = mList.begin(); iter != mList.end(); iter++) { |
| 541 | (*iter)->warnIfStillLive(); |
| 542 | } |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
| 546 | void DeathRecipientList::add(const sp<JavaDeathRecipient>& recipient) { |
| 547 | AutoMutex _l(mLock); |
| 548 | |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 549 | LOGDEATH("DRL @ %p : add JDR %p", this, recipient.get()); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 550 | mList.push_back(recipient); |
| 551 | } |
| 552 | |
| 553 | void 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 Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 559 | LOGDEATH("DRL @ %p : remove JDR %p", this, recipient.get()); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 560 | mList.erase(iter); |
| 561 | return; |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | sp<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 Tate | 090c08f | 2015-05-19 18:16:58 -0700 | [diff] [blame] | 578 | Mutex& DeathRecipientList::lock() { |
| 579 | return mLock; |
| 580 | } |
| 581 | |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 582 | // ---------------------------------------------------------------------------- |
| 583 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 584 | namespace android { |
| 585 | |
| 586 | static 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 Boehm | eb6d62c | 2017-09-20 15:59:12 -0700 | [diff] [blame] | 593 | static Mutex gProxyLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 594 | |
| 595 | jobject 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 Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 602 | LOGDEATH("objectForBinder %p: it's our own %p!\n", val.get(), object); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 603 | return object; |
| 604 | } |
| 605 | |
| 606 | // For the rest of the function we will hold this lock, to serialize |
Christopher Tate | 10c3a28 | 2016-02-26 17:48:08 -0800 | [diff] [blame] | 607 | // looking/creation/destruction of Java proxies for native Binder proxies. |
Hans Boehm | eb6d62c | 2017-09-20 15:59:12 -0700 | [diff] [blame] | 608 | AutoMutex _l(gProxyLock); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 609 | |
| 610 | // Someone else's... do we know about it? |
| 611 | jobject object = (jobject)val->findObject(&gBinderProxyOffsets); |
| 612 | if (object != NULL) { |
Jeff Brown | a4ca8ea | 2013-04-02 18:01:38 -0700 | [diff] [blame] | 613 | jobject res = jniGetReferent(env, object); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | if (res != NULL) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 615 | ALOGV("objectForBinder %p: found existing %p!\n", val.get(), res); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 616 | return res; |
| 617 | } |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 618 | LOGDEATH("Proxy object %p of IBinder %p no longer in working set!!!", object, val.get()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | 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 Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 626 | LOGDEATH("objectForBinder %p: created new proxy %p !\n", val.get(), object); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 627 | // The proxy holds a reference to the native object. |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 628 | env->SetLongField(object, gBinderProxyOffsets.mObject, (jlong)val.get()); |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 629 | val->incStrong((void*)javaObjectForIBinder); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 630 | |
| 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 Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 638 | // Also remember the death recipients registered on this proxy |
| 639 | sp<DeathRecipientList> drl = new DeathRecipientList; |
| 640 | drl->incStrong((void*)javaObjectForIBinder); |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 641 | env->SetLongField(object, gBinderProxyOffsets.mOrgue, reinterpret_cast<jlong>(drl.get())); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 642 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 643 | // Note that a new object reference has been created. |
| 644 | android_atomic_inc(&gNumProxyRefs); |
| 645 | incRefsCreated(env); |
| 646 | } |
| 647 | |
| 648 | return object; |
| 649 | } |
| 650 | |
| 651 | sp<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 Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 657 | env->GetLongField(obj, gBinderOffsets.mObject); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 658 | return jbh != NULL ? jbh->get(env, obj) : NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | if (env->IsInstanceOf(obj, gBinderProxyOffsets.mClass)) { |
| 662 | return (IBinder*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 663 | env->GetLongField(obj, gBinderProxyOffsets.mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | } |
| 665 | |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 666 | ALOGW("ibinderForJavaObject: %p is not a Binder object", obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 667 | return NULL; |
| 668 | } |
| 669 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 670 | jobject newParcelFileDescriptor(JNIEnv* env, jobject fileDesc) |
| 671 | { |
| 672 | return env->NewObject( |
| 673 | gParcelFileDescriptorOffsets.mClass, gParcelFileDescriptorOffsets.mConstructor, fileDesc); |
| 674 | } |
| 675 | |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 676 | void 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 | |
| 685 | void signalExceptionForError(JNIEnv* env, jobject obj, status_t err, |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 686 | bool canThrowRemoteException, int parcelSize) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 687 | { |
| 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 Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 723 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 727 | break; |
| 728 | case UNKNOWN_TRANSACTION: |
| 729 | jniThrowException(env, "java/lang/RuntimeException", "Unknown transaction code"); |
| 730 | break; |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 731 | case FAILED_TRANSACTION: { |
| 732 | ALOGE("!!! FAILED BINDER TRANSACTION !!! (parcel size = %d)", parcelSize); |
Christopher Tate | 02ca7a7 | 2015-06-24 18:16:42 -0700 | [diff] [blame] | 733 | const char* exceptionToThrow; |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 734 | char msg[128]; |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 735 | // 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 Tate | 02ca7a7 | 2015-06-24 18:16:42 -0700 | [diff] [blame] | 741 | 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 Tate | ffd5864 | 2015-06-29 11:00:15 -0700 | [diff] [blame] | 748 | // it seems to always mean that the remote process died while the binder |
Christopher Tate | 02ca7a7 | 2015-06-24 18:16:42 -0700 | [diff] [blame] | 749 | // transaction was already in flight. |
Christopher Tate | ffd5864 | 2015-06-29 11:00:15 -0700 | [diff] [blame] | 750 | exceptionToThrow = (canThrowRemoteException) |
| 751 | ? "android/os/DeadObjectException" |
| 752 | : "java/lang/RuntimeException"; |
Christopher Tate | 02ca7a7 | 2015-06-24 18:16:42 -0700 | [diff] [blame] | 753 | snprintf(msg, sizeof(msg)-1, |
| 754 | "Transaction failed on small parcel; remote process probably died"); |
| 755 | } |
| 756 | jniThrowException(env, exceptionToThrow, msg); |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 757 | } break; |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 758 | case FDS_NOT_ALLOWED: |
| 759 | jniThrowException(env, "java/lang/RuntimeException", |
| 760 | "Not allowed to write file descriptors here"); |
| 761 | break; |
Christopher Wiley | a94fc52 | 2015-11-22 14:21:25 -0800 | [diff] [blame] | 762 | case UNEXPECTED_NULL: |
| 763 | jniThrowNullPointerException(env, NULL); |
| 764 | break; |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 765 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 797 | default: |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 798 | ALOGE("Unknown binder error code. 0x%" PRIx32, err); |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 799 | String8 msg; |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 800 | msg.appendFormat("Unknown binder error code. 0x%" PRIx32, err); |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 801 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 805 | } |
| 806 | } |
| 807 | |
| 808 | } |
| 809 | |
| 810 | // ---------------------------------------------------------------------------- |
| 811 | |
| 812 | static jint android_os_Binder_getCallingPid(JNIEnv* env, jobject clazz) |
| 813 | { |
| 814 | return IPCThreadState::self()->getCallingPid(); |
| 815 | } |
| 816 | |
| 817 | static jint android_os_Binder_getCallingUid(JNIEnv* env, jobject clazz) |
| 818 | { |
| 819 | return IPCThreadState::self()->getCallingUid(); |
| 820 | } |
| 821 | |
| 822 | static jlong android_os_Binder_clearCallingIdentity(JNIEnv* env, jobject clazz) |
| 823 | { |
| 824 | return IPCThreadState::self()->clearCallingIdentity(); |
| 825 | } |
| 826 | |
| 827 | static void android_os_Binder_restoreCallingIdentity(JNIEnv* env, jobject clazz, jlong token) |
| 828 | { |
Dianne Hackborn | cf3004a | 2011-03-14 14:24:04 -0700 | [diff] [blame] | 829 | // 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 Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 834 | sprintf(buf, "Restoring bad calling ident: 0x%" PRIx64, token); |
Dianne Hackborn | cf3004a | 2011-03-14 14:24:04 -0700 | [diff] [blame] | 835 | jniThrowException(env, "java/lang/IllegalStateException", buf); |
| 836 | return; |
| 837 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 838 | IPCThreadState::self()->restoreCallingIdentity(token); |
| 839 | } |
| 840 | |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 841 | static void android_os_Binder_setThreadStrictModePolicy(JNIEnv* env, jobject clazz, jint policyMask) |
| 842 | { |
| 843 | IPCThreadState::self()->setStrictModePolicy(policyMask); |
| 844 | } |
| 845 | |
| 846 | static jint android_os_Binder_getThreadStrictModePolicy(JNIEnv* env, jobject clazz) |
| 847 | { |
| 848 | return IPCThreadState::self()->getStrictModePolicy(); |
| 849 | } |
| 850 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 851 | static void android_os_Binder_flushPendingCommands(JNIEnv* env, jobject clazz) |
| 852 | { |
| 853 | IPCThreadState::self()->flushCommands(); |
| 854 | } |
| 855 | |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 856 | static void android_os_Binder_init(JNIEnv* env, jobject obj) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 857 | { |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 858 | JavaBBinderHolder* jbh = new JavaBBinderHolder(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 859 | if (jbh == NULL) { |
| 860 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 861 | return; |
| 862 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 863 | ALOGV("Java Binder %p: acquiring first ref on holder %p", obj, jbh); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 864 | jbh->incStrong((void*)android_os_Binder_init); |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 865 | env->SetLongField(obj, gBinderOffsets.mObject, (jlong)jbh); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 866 | } |
| 867 | |
Vladimir Marko | b25a532 | 2017-06-21 14:40:12 +0100 | [diff] [blame] | 868 | static void android_os_Binder_destroyBinder(JNIEnv* env, jobject obj) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 869 | { |
| 870 | JavaBBinderHolder* jbh = (JavaBBinderHolder*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 871 | env->GetLongField(obj, gBinderOffsets.mObject); |
Jeff Brown | 582763a | 2010-03-24 18:56:57 -0700 | [diff] [blame] | 872 | if (jbh != NULL) { |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 873 | env->SetLongField(obj, gBinderOffsets.mObject, 0); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 874 | ALOGV("Java Binder %p: removing ref on holder %p", obj, jbh); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 875 | jbh->decStrong((void*)android_os_Binder_init); |
Jeff Brown | 582763a | 2010-03-24 18:56:57 -0700 | [diff] [blame] | 876 | } 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 Marko | b25a532 | 2017-06-21 14:40:12 +0100 | [diff] [blame] | 879 | // destroyBinder(). The Binder could be partially initialized for several reasons. |
Jeff Brown | 582763a | 2010-03-24 18:56:57 -0700 | [diff] [blame] | 880 | // 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 Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 883 | ALOGV("Java Binder %p: ignoring uninitialized binder", obj); |
Jeff Brown | 582763a | 2010-03-24 18:56:57 -0700 | [diff] [blame] | 884 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 885 | } |
| 886 | |
Wale Ogunwale | d7fdd02 | 2015-04-13 16:22:38 -0700 | [diff] [blame] | 887 | static void android_os_Binder_blockUntilThreadAvailable(JNIEnv* env, jobject clazz) |
| 888 | { |
| 889 | return IPCThreadState::self()->blockUntilThreadAvailable(); |
| 890 | } |
| 891 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 892 | // ---------------------------------------------------------------------------- |
| 893 | |
| 894 | static 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 Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 900 | { "setThreadStrictModePolicy", "(I)V", (void*)android_os_Binder_setThreadStrictModePolicy }, |
| 901 | { "getThreadStrictModePolicy", "()I", (void*)android_os_Binder_getThreadStrictModePolicy }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 902 | { "flushPendingCommands", "()V", (void*)android_os_Binder_flushPendingCommands }, |
| 903 | { "init", "()V", (void*)android_os_Binder_init }, |
Vladimir Marko | b25a532 | 2017-06-21 14:40:12 +0100 | [diff] [blame] | 904 | { "destroyBinder", "()V", (void*)android_os_Binder_destroyBinder }, |
Wale Ogunwale | d7fdd02 | 2015-04-13 16:22:38 -0700 | [diff] [blame] | 905 | { "blockUntilThreadAvailable", "()V", (void*)android_os_Binder_blockUntilThreadAvailable } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 906 | }; |
| 907 | |
| 908 | const char* const kBinderPathName = "android/os/Binder"; |
| 909 | |
| 910 | static int int_register_android_os_Binder(JNIEnv* env) |
| 911 | { |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 912 | jclass clazz = FindClassOrDie(env, kBinderPathName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 913 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 914 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 917 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 918 | return RegisterMethodsOrDie( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 919 | env, kBinderPathName, |
| 920 | gBinderMethods, NELEM(gBinderMethods)); |
| 921 | } |
| 922 | |
| 923 | // **************************************************************************** |
| 924 | // **************************************************************************** |
| 925 | // **************************************************************************** |
| 926 | |
| 927 | namespace android { |
| 928 | |
| 929 | jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz) |
| 930 | { |
| 931 | return gNumLocalRefs; |
| 932 | } |
| 933 | |
| 934 | jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz) |
| 935 | { |
| 936 | return gNumProxyRefs; |
| 937 | } |
| 938 | |
| 939 | jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz) |
| 940 | { |
| 941 | return gNumDeathRefs; |
| 942 | } |
| 943 | |
| 944 | } |
| 945 | |
| 946 | // **************************************************************************** |
| 947 | // **************************************************************************** |
| 948 | // **************************************************************************** |
| 949 | |
| 950 | static 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 | |
| 956 | static 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 Hackborn | 887f355 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 962 | static void android_os_BinderInternal_disableBackgroundScheduling(JNIEnv* env, |
| 963 | jobject clazz, jboolean disable) |
| 964 | { |
| 965 | IPCThreadState::disableBackgroundScheduling(disable ? true : false); |
| 966 | } |
| 967 | |
Tim Murray | eef4a3d | 2016-04-19 14:14:20 -0700 | [diff] [blame] | 968 | static void android_os_BinderInternal_setMaxThreads(JNIEnv* env, |
| 969 | jobject clazz, jint maxThreads) |
| 970 | { |
| 971 | ProcessState::self()->setThreadPoolMaxThreadCount(maxThreads); |
| 972 | } |
| 973 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 974 | static void android_os_BinderInternal_handleGc(JNIEnv* env, jobject clazz) |
| 975 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 976 | ALOGV("Gc has executed, clearing binder ops"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 977 | android_atomic_and(0, &gNumRefsCreated); |
| 978 | } |
| 979 | |
| 980 | // ---------------------------------------------------------------------------- |
| 981 | |
| 982 | static 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 Hackborn | 887f355 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 986 | { "disableBackgroundScheduling", "(Z)V", (void*)android_os_BinderInternal_disableBackgroundScheduling }, |
Tim Murray | eef4a3d | 2016-04-19 14:14:20 -0700 | [diff] [blame] | 987 | { "setMaxThreads", "(I)V", (void*)android_os_BinderInternal_setMaxThreads }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 988 | { "handleGc", "()V", (void*)android_os_BinderInternal_handleGc } |
| 989 | }; |
| 990 | |
| 991 | const char* const kBinderInternalPathName = "com/android/internal/os/BinderInternal"; |
| 992 | |
| 993 | static int int_register_android_os_BinderInternal(JNIEnv* env) |
| 994 | { |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 995 | jclass clazz = FindClassOrDie(env, kBinderInternalPathName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 996 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 997 | gBinderInternalOffsets.mClass = MakeGlobalRefOrDie(env, clazz); |
| 998 | gBinderInternalOffsets.mForceGc = GetStaticMethodIDOrDie(env, clazz, "forceBinderGc", "()V"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 999 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1000 | return RegisterMethodsOrDie( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1001 | env, kBinderInternalPathName, |
| 1002 | gBinderInternalMethods, NELEM(gBinderInternalMethods)); |
| 1003 | } |
| 1004 | |
| 1005 | // **************************************************************************** |
| 1006 | // **************************************************************************** |
| 1007 | // **************************************************************************** |
| 1008 | |
| 1009 | static jboolean android_os_BinderProxy_pingBinder(JNIEnv* env, jobject obj) |
| 1010 | { |
| 1011 | IBinder* target = (IBinder*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1012 | env->GetLongField(obj, gBinderProxyOffsets.mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1013 | 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 | |
| 1020 | static jstring android_os_BinderProxy_getInterfaceDescriptor(JNIEnv* env, jobject obj) |
| 1021 | { |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1022 | IBinder* target = (IBinder*) env->GetLongField(obj, gBinderProxyOffsets.mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1023 | if (target != NULL) { |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1024 | const String16& desc = target->getInterfaceDescriptor(); |
Dan Albert | 6698749 | 2014-11-20 11:41:21 -0800 | [diff] [blame] | 1025 | return env->NewString(reinterpret_cast<const jchar*>(desc.string()), |
| 1026 | desc.size()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1027 | } |
| 1028 | jniThrowException(env, "java/lang/RuntimeException", |
| 1029 | "No binder found for object"); |
| 1030 | return NULL; |
| 1031 | } |
| 1032 | |
| 1033 | static jboolean android_os_BinderProxy_isBinderAlive(JNIEnv* env, jobject obj) |
| 1034 | { |
| 1035 | IBinder* target = (IBinder*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1036 | env->GetLongField(obj, gBinderProxyOffsets.mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1037 | if (target == NULL) { |
| 1038 | return JNI_FALSE; |
| 1039 | } |
| 1040 | bool alive = target->isBinderAlive(); |
| 1041 | return alive ? JNI_TRUE : JNI_FALSE; |
| 1042 | } |
| 1043 | |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1044 | static int getprocname(pid_t pid, char *buf, size_t len) { |
Sungmin Choi | ec3d44c | 2012-12-21 14:24:33 +0900 | [diff] [blame] | 1045 | char filename[32]; |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1046 | FILE *f; |
| 1047 | |
Sungmin Choi | ec3d44c | 2012-12-21 14:24:33 +0900 | [diff] [blame] | 1048 | snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid); |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1049 | f = fopen(filename, "r"); |
Sungmin Choi | ec3d44c | 2012-12-21 14:24:33 +0900 | [diff] [blame] | 1050 | 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 Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1059 | fclose(f); |
| 1060 | return 0; |
| 1061 | } |
| 1062 | |
| 1063 | static 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 Salyzyn | 5b6da1a | 2014-04-17 17:25:36 -0700 | [diff] [blame] | 1067 | ALOGW("not enough space for string. remain=%" PRIdPTR "; needed=%d", |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 1068 | end - *pos, space_needed); |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1069 | 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 | |
| 1080 | static 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 Salyzyn | 5b6da1a | 2014-04-17 17:25:36 -0700 | [diff] [blame] | 1083 | ALOGW("not enough space for int. remain=%" PRIdPTR "; needed=%d", |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 1084 | end - *pos, space_needed); |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1085 | 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 Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1095 | |
| 1096 | static const bool kEnableBinderSample = false; |
| 1097 | |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1098 | #define LOGTAG_BINDER_OPERATION 52004 |
| 1099 | |
| 1100 | static 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 Fitzpatrick | ad8fd28 | 2010-03-25 02:01:32 -0700 | [diff] [blame] | 1135 | // We only measure binder call durations to potentially log them if |
Elliott Hughes | 06451fe | 2014-08-18 10:26:52 -0700 | [diff] [blame] | 1136 | // we're on the main thread. |
Brad Fitzpatrick | ad8fd28 | 2010-03-25 02:01:32 -0700 | [diff] [blame] | 1137 | static bool should_time_binder_calls() { |
Elliott Hughes | 06451fe | 2014-08-18 10:26:52 -0700 | [diff] [blame] | 1138 | return (getpid() == gettid()); |
Brad Fitzpatrick | ad8fd28 | 2010-03-25 02:01:32 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1141 | static jboolean android_os_BinderProxy_transact(JNIEnv* env, jobject obj, |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 1142 | jint code, jobject dataObj, jobject replyObj, jint flags) // throws RemoteException |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1143 | { |
| 1144 | if (dataObj == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1145 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1146 | 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 Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1159 | env->GetLongField(obj, gBinderProxyOffsets.mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1160 | if (target == NULL) { |
| 1161 | jniThrowException(env, "java/lang/IllegalStateException", "Binder has been finalized!"); |
| 1162 | return JNI_FALSE; |
| 1163 | } |
| 1164 | |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 1165 | ALOGV("Java code calling transact on %p in Java object %p with code %" PRId32 "\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1166 | target, obj, code); |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1167 | |
Brad Fitzpatrick | ad8fd28 | 2010-03-25 02:01:32 -0700 | [diff] [blame] | 1168 | |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1169 | bool time_binder_calls; |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1170 | int64_t start_millis; |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1171 | 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 Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1179 | } |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1180 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1181 | //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 Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1184 | |
| 1185 | if (kEnableBinderSample) { |
| 1186 | if (time_binder_calls) { |
| 1187 | conditionally_log_binder_call(start_millis, target, code); |
| 1188 | } |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1189 | } |
| 1190 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1191 | if (err == NO_ERROR) { |
| 1192 | return JNI_TRUE; |
| 1193 | } else if (err == UNKNOWN_TRANSACTION) { |
| 1194 | return JNI_FALSE; |
| 1195 | } |
| 1196 | |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1197 | signalExceptionForError(env, obj, err, true /*canThrowRemoteException*/, data->dataSize()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1198 | return JNI_FALSE; |
| 1199 | } |
| 1200 | |
| 1201 | static void android_os_BinderProxy_linkToDeath(JNIEnv* env, jobject obj, |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 1202 | jobject recipient, jint flags) // throws RemoteException |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1203 | { |
| 1204 | if (recipient == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1205 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1206 | return; |
| 1207 | } |
| 1208 | |
| 1209 | IBinder* target = (IBinder*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1210 | env->GetLongField(obj, gBinderProxyOffsets.mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1211 | if (target == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1212 | ALOGW("Binder has been finalized when calling linkToDeath() with recip=%p)\n", recipient); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1213 | assert(false); |
| 1214 | } |
| 1215 | |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 1216 | LOGDEATH("linkToDeath: binder=%p recipient=%p\n", target, recipient); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1217 | |
| 1218 | if (!target->localBinder()) { |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 1219 | DeathRecipientList* list = (DeathRecipientList*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1220 | env->GetLongField(obj, gBinderProxyOffsets.mOrgue); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 1221 | sp<JavaDeathRecipient> jdr = new JavaDeathRecipient(env, recipient, list); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 1222 | status_t err = target->linkToDeath(jdr, NULL, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1223 | if (err != NO_ERROR) { |
| 1224 | // Failure adding the death recipient, so clear its reference |
| 1225 | // now. |
| 1226 | jdr->clearReference(); |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 1227 | signalExceptionForError(env, obj, err, true /*canThrowRemoteException*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1228 | } |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | static 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 Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1237 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1238 | return res; |
| 1239 | } |
| 1240 | |
| 1241 | IBinder* target = (IBinder*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1242 | env->GetLongField(obj, gBinderProxyOffsets.mObject); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1243 | if (target == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1244 | ALOGW("Binder has been finalized when calling linkToDeath() with recip=%p)\n", recipient); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1245 | return JNI_FALSE; |
| 1246 | } |
| 1247 | |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 1248 | LOGDEATH("unlinkToDeath: binder=%p recipient=%p\n", target, recipient); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1249 | |
| 1250 | if (!target->localBinder()) { |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 1251 | status_t err = NAME_NOT_FOUND; |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 1252 | |
| 1253 | // If we find the matching recipient, proceed to unlink using that |
| 1254 | DeathRecipientList* list = (DeathRecipientList*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1255 | env->GetLongField(obj, gBinderProxyOffsets.mOrgue); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 1256 | sp<JavaDeathRecipient> origJDR = list->find(recipient); |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 1257 | LOGDEATH(" unlink found list %p and JDR %p", list, origJDR.get()); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 1258 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1270 | 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 | |
| 1281 | static void android_os_BinderProxy_destroy(JNIEnv* env, jobject obj) |
| 1282 | { |
Christopher Tate | 10c3a28 | 2016-02-26 17:48:08 -0800 | [diff] [blame] | 1283 | // Don't race with construction/initialization |
Hans Boehm | eb6d62c | 2017-09-20 15:59:12 -0700 | [diff] [blame] | 1284 | AutoMutex _l(gProxyLock); |
Christopher Tate | 10c3a28 | 2016-02-26 17:48:08 -0800 | [diff] [blame] | 1285 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1286 | IBinder* b = (IBinder*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1287 | env->GetLongField(obj, gBinderProxyOffsets.mObject); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 1288 | DeathRecipientList* drl = (DeathRecipientList*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 1289 | env->GetLongField(obj, gBinderProxyOffsets.mOrgue); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 1290 | |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 1291 | LOGDEATH("Destroying BinderProxy %p: binder=%p drl=%p\n", obj, b, drl); |
Christopher Tate | 10c3a28 | 2016-02-26 17:48:08 -0800 | [diff] [blame] | 1292 | 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 Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 1298 | |
| 1299 | IPCThreadState::self()->flushCommands(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | // ---------------------------------------------------------------------------- |
| 1303 | |
| 1304 | static 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 Hackborn | 017c6a2 | 2014-09-25 17:41:34 -0700 | [diff] [blame] | 1309 | {"transactNative", "(ILandroid/os/Parcel;Landroid/os/Parcel;I)Z", (void*)android_os_BinderProxy_transact}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1310 | {"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 | |
| 1315 | const char* const kBinderProxyPathName = "android/os/BinderProxy"; |
| 1316 | |
| 1317 | static int int_register_android_os_BinderProxy(JNIEnv* env) |
| 1318 | { |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1319 | jclass clazz = FindClassOrDie(env, "java/lang/Error"); |
| 1320 | gErrorOffsets.mClass = MakeGlobalRefOrDie(env, clazz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1321 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1322 | 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 Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1327 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1328 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1332 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1333 | clazz = FindClassOrDie(env, "java/lang/Class"); |
| 1334 | gClassOffsets.mGetName = GetMethodIDOrDie(env, clazz, "getName", "()Ljava/lang/String;"); |
Christopher Tate | 0d4a792 | 2011-08-30 12:09:43 -0700 | [diff] [blame] | 1335 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1336 | return RegisterMethodsOrDie( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1337 | env, kBinderProxyPathName, |
| 1338 | gBinderProxyMethods, NELEM(gBinderProxyMethods)); |
| 1339 | } |
| 1340 | |
| 1341 | // **************************************************************************** |
| 1342 | // **************************************************************************** |
| 1343 | // **************************************************************************** |
| 1344 | |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 1345 | int register_android_os_Binder(JNIEnv* env) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1346 | { |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 1347 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1353 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1354 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1358 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1359 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1363 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1364 | clazz = FindClassOrDie(env, "android/os/StrictMode"); |
| 1365 | gStrictModeCallbackOffsets.mClass = MakeGlobalRefOrDie(env, clazz); |
| 1366 | gStrictModeCallbackOffsets.mCallback = GetStaticMethodIDOrDie(env, clazz, |
| 1367 | "onBinderStrictModePolicyChange", "(I)V"); |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 1368 | |
Andreas Gampe | 75a75d6 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 1369 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1376 | return 0; |
| 1377 | } |