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 | |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 23 | #include <atomic> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | #include <fcntl.h> |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 25 | #include <inttypes.h> |
Steven Moreland | e52bb7d | 2018-10-10 11:24:58 -0700 | [diff] [blame] | 26 | #include <mutex> |
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> |
Michael Wachenschwanz | 5518246 | 2017-08-14 23:10:13 -0700 | [diff] [blame] | 33 | #include <binder/BpBinder.h> |
Steven Moreland | d587a55 | 2019-11-27 18:15:33 -0800 | [diff] [blame] | 34 | #include <binder/IInterface.h> |
| 35 | #include <binder/IPCThreadState.h> |
| 36 | #include <binder/IServiceManager.h> |
| 37 | #include <binder/Parcel.h> |
Mathias Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 38 | #include <binder/ProcessState.h> |
Steven Moreland | d587a55 | 2019-11-27 18:15:33 -0800 | [diff] [blame] | 39 | #include <binder/Stability.h> |
Steven Moreland | fb7952f | 2018-02-23 14:58:50 -0800 | [diff] [blame] | 40 | #include <cutils/atomic.h> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 41 | #include <log/log.h> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 42 | #include <utils/KeyedVector.h> |
| 43 | #include <utils/List.h> |
| 44 | #include <utils/Log.h> |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 45 | #include <utils/String8.h> |
Mark Salyzyn | 52eb4e0 | 2016-09-28 16:15:30 -0700 | [diff] [blame] | 46 | #include <utils/SystemClock.h> |
| 47 | #include <utils/threads.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 49 | #include <nativehelper/JNIHelp.h> |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 50 | #include <nativehelper/ScopedLocalRef.h> |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 51 | #include <nativehelper/ScopedUtfChars.h> |
Christopher Tate | ac5e350 | 2011-08-25 15:48:09 -0700 | [diff] [blame] | 52 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 53 | #include "core_jni_helpers.h" |
| 54 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 55 | //#undef ALOGV |
| 56 | //#define ALOGV(...) fprintf(stderr, __VA_ARGS__) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 58 | #define DEBUG_DEATH 0 |
| 59 | #if DEBUG_DEATH |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 60 | #define LOGDEATH ALOGD |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 61 | #else |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 62 | #define LOGDEATH ALOGV |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 63 | #endif |
| 64 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | using namespace android; |
| 66 | |
| 67 | // ---------------------------------------------------------------------------- |
| 68 | |
| 69 | static struct bindernative_offsets_t |
| 70 | { |
| 71 | // Class state. |
| 72 | jclass mClass; |
| 73 | jmethodID mExecTransact; |
Steven Moreland | e52bb7d | 2018-10-10 11:24:58 -0700 | [diff] [blame] | 74 | jmethodID mGetInterfaceDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | |
| 76 | // Object state. |
| 77 | jfieldID mObject; |
| 78 | |
| 79 | } gBinderOffsets; |
| 80 | |
| 81 | // ---------------------------------------------------------------------------- |
| 82 | |
| 83 | static struct binderinternal_offsets_t |
| 84 | { |
| 85 | // Class state. |
| 86 | jclass mClass; |
| 87 | jmethodID mForceGc; |
Michael Wachenschwanz | 5518246 | 2017-08-14 23:10:13 -0700 | [diff] [blame] | 88 | jmethodID mProxyLimitCallback; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | |
| 90 | } gBinderInternalOffsets; |
| 91 | |
Michael Wachenschwanz | 5518246 | 2017-08-14 23:10:13 -0700 | [diff] [blame] | 92 | static struct sparseintarray_offsets_t |
| 93 | { |
| 94 | jclass classObject; |
| 95 | jmethodID constructor; |
| 96 | jmethodID put; |
| 97 | } gSparseIntArrayOffsets; |
| 98 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 99 | // ---------------------------------------------------------------------------- |
| 100 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | static struct error_offsets_t |
| 102 | { |
Andreas Gampe | 9ff9c40 | 2019-07-08 08:31:27 -0700 | [diff] [blame] | 103 | jclass mError; |
| 104 | jclass mOutOfMemory; |
| 105 | jclass mStackOverflow; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | } gErrorOffsets; |
| 107 | |
| 108 | // ---------------------------------------------------------------------------- |
| 109 | |
| 110 | static struct binderproxy_offsets_t |
| 111 | { |
| 112 | // Class state. |
| 113 | jclass mClass; |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 114 | jmethodID mGetInstance; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | jmethodID mSendDeathNotice; |
| 116 | |
| 117 | // Object state. |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 118 | jfieldID mNativeData; // Field holds native pointer to BinderProxyNativeData. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | } gBinderProxyOffsets; |
| 120 | |
Christopher Tate | 0d4a792 | 2011-08-30 12:09:43 -0700 | [diff] [blame] | 121 | static struct class_offsets_t |
| 122 | { |
| 123 | jmethodID mGetName; |
| 124 | } gClassOffsets; |
| 125 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | // ---------------------------------------------------------------------------- |
| 127 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | static struct log_offsets_t |
| 129 | { |
| 130 | // Class state. |
| 131 | jclass mClass; |
| 132 | jmethodID mLogE; |
| 133 | } gLogOffsets; |
| 134 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | static struct parcel_file_descriptor_offsets_t |
| 136 | { |
| 137 | jclass mClass; |
| 138 | jmethodID mConstructor; |
| 139 | } gParcelFileDescriptorOffsets; |
| 140 | |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 141 | static struct strict_mode_callback_offsets_t |
| 142 | { |
| 143 | jclass mClass; |
| 144 | jmethodID mCallback; |
| 145 | } gStrictModeCallbackOffsets; |
| 146 | |
Andreas Gampe | 1cd76f5 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 147 | static struct thread_dispatch_offsets_t |
| 148 | { |
| 149 | // Class state. |
| 150 | jclass mClass; |
| 151 | jmethodID mDispatchUncaughtException; |
| 152 | jmethodID mCurrentThread; |
| 153 | } gThreadDispatchOffsets; |
| 154 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | // **************************************************************************** |
| 156 | // **************************************************************************** |
| 157 | // **************************************************************************** |
| 158 | |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 159 | static constexpr int32_t PROXY_WARN_INTERVAL = 5000; |
| 160 | static constexpr uint32_t GC_INTERVAL = 1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | |
Martijn Coenen | d3ef4bf | 2018-07-05 14:58:59 +0200 | [diff] [blame] | 162 | static std::atomic<uint32_t> gNumProxies(0); |
| 163 | static std::atomic<uint32_t> gProxiesWarned(0); |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 164 | |
| 165 | // Number of GlobalRefs held by JavaBBinders. |
| 166 | static std::atomic<uint32_t> gNumLocalRefsCreated(0); |
| 167 | static std::atomic<uint32_t> gNumLocalRefsDeleted(0); |
| 168 | // Number of GlobalRefs held by JavaDeathRecipients. |
| 169 | static std::atomic<uint32_t> gNumDeathRefsCreated(0); |
| 170 | static std::atomic<uint32_t> gNumDeathRefsDeleted(0); |
| 171 | |
| 172 | // We collected after creating this many refs. |
| 173 | static std::atomic<uint32_t> gCollectedAtRefs(0); |
| 174 | |
| 175 | // Garbage collect if we've allocated at least GC_INTERVAL refs since the last time. |
| 176 | // TODO: Consider removing this completely. We should no longer be generating GlobalRefs |
| 177 | // that are reclaimed as a result of GC action. |
Ivan Lozano | 2ea7135 | 2017-11-02 14:10:57 -0700 | [diff] [blame] | 178 | __attribute__((no_sanitize("unsigned-integer-overflow"))) |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 179 | static void gcIfManyNewRefs(JNIEnv* env) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | { |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 181 | uint32_t totalRefs = gNumLocalRefsCreated.load(std::memory_order_relaxed) |
| 182 | + gNumDeathRefsCreated.load(std::memory_order_relaxed); |
| 183 | uint32_t collectedAtRefs = gCollectedAtRefs.load(memory_order_relaxed); |
| 184 | // A bound on the number of threads that can have incremented gNum...RefsCreated before the |
| 185 | // following check is executed. Effectively a bound on #threads. Almost any value will do. |
| 186 | static constexpr uint32_t MAX_RACING = 100000; |
| 187 | |
| 188 | if (totalRefs - (collectedAtRefs + GC_INTERVAL) /* modular arithmetic! */ < MAX_RACING) { |
| 189 | // Recently passed next GC interval. |
| 190 | if (gCollectedAtRefs.compare_exchange_strong(collectedAtRefs, |
| 191 | collectedAtRefs + GC_INTERVAL, std::memory_order_relaxed)) { |
| 192 | ALOGV("Binder forcing GC at %u created refs", totalRefs); |
| 193 | env->CallStaticVoidMethod(gBinderInternalOffsets.mClass, |
| 194 | gBinderInternalOffsets.mForceGc); |
| 195 | } // otherwise somebody else beat us to it. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | } else { |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 197 | ALOGV("Now have %d binder ops", totalRefs - collectedAtRefs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | static JavaVM* jnienv_to_javavm(JNIEnv* env) |
| 202 | { |
| 203 | JavaVM* vm; |
| 204 | return env->GetJavaVM(&vm) >= 0 ? vm : NULL; |
| 205 | } |
| 206 | |
| 207 | static JNIEnv* javavm_to_jnienv(JavaVM* vm) |
| 208 | { |
| 209 | JNIEnv* env; |
| 210 | return vm->GetEnv((void **)&env, JNI_VERSION_1_4) >= 0 ? env : NULL; |
| 211 | } |
| 212 | |
Andreas Gampe | 9ff9c40 | 2019-07-08 08:31:27 -0700 | [diff] [blame] | 213 | static const char* GetErrorTypeName(JNIEnv* env, jthrowable error) { |
| 214 | if (env->IsInstanceOf(error, gErrorOffsets.mOutOfMemory)) { |
| 215 | return "OutOfMemoryError"; |
| 216 | } |
| 217 | if (env->IsInstanceOf(error, gErrorOffsets.mStackOverflow)) { |
| 218 | return "StackOverflowError"; |
| 219 | } |
| 220 | return nullptr; |
| 221 | } |
| 222 | |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 223 | // Report a java.lang.Error (or subclass). This will terminate the runtime by |
| 224 | // calling FatalError with a message derived from the given error. |
| 225 | static void report_java_lang_error_fatal_error(JNIEnv* env, jthrowable error, |
| 226 | const char* msg) |
| 227 | { |
| 228 | // Report an error: reraise the exception and ask the runtime to abort. |
| 229 | |
| 230 | // Try to get the exception string. Sometimes logcat isn't available, |
| 231 | // so try to add it to the abort message. |
Andreas Gampe | 9ff9c40 | 2019-07-08 08:31:27 -0700 | [diff] [blame] | 232 | std::string exc_msg; |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 233 | { |
| 234 | ScopedLocalRef<jclass> exc_class(env, env->GetObjectClass(error)); |
| 235 | jmethodID method_id = env->GetMethodID(exc_class.get(), "toString", |
| 236 | "()Ljava/lang/String;"); |
| 237 | ScopedLocalRef<jstring> jstr( |
| 238 | env, |
| 239 | reinterpret_cast<jstring>( |
| 240 | env->CallObjectMethod(error, method_id))); |
Andreas Gampe | 9ff9c40 | 2019-07-08 08:31:27 -0700 | [diff] [blame] | 241 | ScopedLocalRef<jthrowable> new_error(env, nullptr); |
| 242 | bool got_jstr = false; |
| 243 | if (env->ExceptionCheck()) { |
| 244 | new_error = ScopedLocalRef<jthrowable>(env, env->ExceptionOccurred()); |
| 245 | env->ExceptionClear(); |
| 246 | } |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 247 | if (jstr.get() != nullptr) { |
| 248 | ScopedUtfChars jstr_utf(env, jstr.get()); |
| 249 | if (jstr_utf.c_str() != nullptr) { |
| 250 | exc_msg = jstr_utf.c_str(); |
Andreas Gampe | 9ff9c40 | 2019-07-08 08:31:27 -0700 | [diff] [blame] | 251 | got_jstr = true; |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 252 | } else { |
Andreas Gampe | 9ff9c40 | 2019-07-08 08:31:27 -0700 | [diff] [blame] | 253 | new_error = ScopedLocalRef<jthrowable>(env, env->ExceptionOccurred()); |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 254 | env->ExceptionClear(); |
| 255 | } |
| 256 | } |
Andreas Gampe | 9ff9c40 | 2019-07-08 08:31:27 -0700 | [diff] [blame] | 257 | if (!got_jstr) { |
| 258 | exc_msg = "(Unknown exception message)"; |
| 259 | const char* orig_type = GetErrorTypeName(env, error); |
| 260 | if (orig_type != nullptr) { |
| 261 | exc_msg = base::StringPrintf("%s (Error was %s)", exc_msg.c_str(), orig_type); |
| 262 | } |
| 263 | const char* new_type = |
| 264 | new_error == nullptr ? nullptr : GetErrorTypeName(env, new_error.get()); |
| 265 | if (new_type != nullptr) { |
| 266 | exc_msg = base::StringPrintf("%s (toString() error was %s)", |
| 267 | exc_msg.c_str(), |
| 268 | new_type); |
| 269 | } |
| 270 | } |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | env->Throw(error); |
| 274 | ALOGE("java.lang.Error thrown during binder transaction (stack trace follows) : "); |
| 275 | env->ExceptionDescribe(); |
| 276 | |
| 277 | std::string error_msg = base::StringPrintf( |
| 278 | "java.lang.Error thrown during binder transaction: %s", |
| 279 | exc_msg.c_str()); |
| 280 | env->FatalError(error_msg.c_str()); |
| 281 | } |
| 282 | |
| 283 | // Report a java.lang.Error (or subclass). This will terminate the runtime, either by |
| 284 | // the uncaught exception handler, or explicitly by calling |
| 285 | // report_java_lang_error_fatal_error. |
| 286 | static void report_java_lang_error(JNIEnv* env, jthrowable error, const char* msg) |
Andreas Gampe | 1cd76f5 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 287 | { |
| 288 | // Try to run the uncaught exception machinery. |
| 289 | jobject thread = env->CallStaticObjectMethod(gThreadDispatchOffsets.mClass, |
| 290 | gThreadDispatchOffsets.mCurrentThread); |
| 291 | if (thread != nullptr) { |
| 292 | env->CallVoidMethod(thread, gThreadDispatchOffsets.mDispatchUncaughtException, |
| 293 | error); |
| 294 | // Should not return here, unless more errors occured. |
| 295 | } |
| 296 | // Some error occurred that meant that either dispatchUncaughtException could not be |
| 297 | // called or that it had an error itself (as this should be unreachable under normal |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 298 | // conditions). As the binder code cannot handle Errors, attempt to log the error and |
| 299 | // abort. |
Andreas Gampe | 1cd76f5 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 300 | env->ExceptionClear(); |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 301 | report_java_lang_error_fatal_error(env, error, msg); |
Andreas Gampe | 1cd76f5 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 302 | } |
| 303 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | static void report_exception(JNIEnv* env, jthrowable excep, const char* msg) |
| 305 | { |
| 306 | env->ExceptionClear(); |
| 307 | |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 308 | ScopedLocalRef<jstring> tagstr(env, env->NewStringUTF(LOG_TAG)); |
Andreas Gampe | 8571ec3 | 2017-09-21 10:55:59 -0700 | [diff] [blame] | 309 | ScopedLocalRef<jstring> msgstr(env); |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 310 | if (tagstr != nullptr) { |
| 311 | msgstr.reset(env->NewStringUTF(msg)); |
Mathieu Chartier | cf6775e | 2014-08-06 13:39:17 -0700 | [diff] [blame] | 312 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 314 | if ((tagstr != nullptr) && (msgstr != nullptr)) { |
| 315 | env->CallStaticIntMethod(gLogOffsets.mClass, gLogOffsets.mLogE, |
| 316 | tagstr.get(), msgstr.get(), excep); |
| 317 | if (env->ExceptionCheck()) { |
| 318 | // Attempting to log the failure has failed. |
| 319 | ALOGW("Failed trying to log exception, msg='%s'\n", msg); |
| 320 | env->ExceptionClear(); |
| 321 | } |
| 322 | } else { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | env->ExceptionClear(); /* assume exception (OOM?) was thrown */ |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 324 | ALOGE("Unable to call Log.e()\n"); |
| 325 | ALOGE("%s", msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Andreas Gampe | 9ff9c40 | 2019-07-08 08:31:27 -0700 | [diff] [blame] | 328 | if (env->IsInstanceOf(excep, gErrorOffsets.mError)) { |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 329 | report_java_lang_error(env, excep, msg); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | class JavaBBinderHolder; |
| 334 | |
| 335 | class JavaBBinder : public BBinder |
| 336 | { |
| 337 | public: |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 338 | JavaBBinder(JNIEnv* env, jobject /* Java Binder */ object) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | : mVM(jnienv_to_javavm(env)), mObject(env->NewGlobalRef(object)) |
| 340 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 341 | ALOGV("Creating JavaBBinder %p\n", this); |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 342 | gNumLocalRefsCreated.fetch_add(1, std::memory_order_relaxed); |
| 343 | gcIfManyNewRefs(env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | bool checkSubclass(const void* subclassID) const |
| 347 | { |
| 348 | return subclassID == &gBinderOffsets; |
| 349 | } |
| 350 | |
| 351 | jobject object() const |
| 352 | { |
| 353 | return mObject; |
| 354 | } |
| 355 | |
| 356 | protected: |
| 357 | virtual ~JavaBBinder() |
| 358 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 359 | ALOGV("Destroying JavaBBinder %p\n", this); |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 360 | gNumLocalRefsDeleted.fetch_add(1, memory_order_relaxed); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 361 | JNIEnv* env = javavm_to_jnienv(mVM); |
| 362 | env->DeleteGlobalRef(mObject); |
| 363 | } |
| 364 | |
Steven Moreland | e52bb7d | 2018-10-10 11:24:58 -0700 | [diff] [blame] | 365 | const String16& getInterfaceDescriptor() const override |
| 366 | { |
| 367 | call_once(mPopulateDescriptor, [this] { |
| 368 | JNIEnv* env = javavm_to_jnienv(mVM); |
| 369 | |
| 370 | ALOGV("getInterfaceDescriptor() on %p calling object %p in env %p vm %p\n", this, mObject, env, mVM); |
| 371 | |
| 372 | jstring descriptor = (jstring)env->CallObjectMethod(mObject, gBinderOffsets.mGetInterfaceDescriptor); |
| 373 | |
| 374 | if (descriptor == nullptr) { |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | static_assert(sizeof(jchar) == sizeof(char16_t), ""); |
| 379 | const jchar* descriptorChars = env->GetStringChars(descriptor, nullptr); |
| 380 | const char16_t* rawDescriptor = reinterpret_cast<const char16_t*>(descriptorChars); |
| 381 | jsize rawDescriptorLen = env->GetStringLength(descriptor); |
| 382 | mDescriptor = String16(rawDescriptor, rawDescriptorLen); |
| 383 | env->ReleaseStringChars(descriptor, descriptorChars); |
| 384 | }); |
| 385 | |
| 386 | return mDescriptor; |
| 387 | } |
| 388 | |
| 389 | status_t onTransact( |
| 390 | uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags = 0) override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | { |
| 392 | JNIEnv* env = javavm_to_jnienv(mVM); |
| 393 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 394 | 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] | 395 | |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 396 | IPCThreadState* thread_state = IPCThreadState::self(); |
Dianne Hackborn | ce92b0d | 2014-09-30 11:28:18 -0700 | [diff] [blame] | 397 | const int32_t strict_policy_before = thread_state->getStrictModePolicy(); |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 398 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 399 | //printf("Transact from %p to Java code sending: ", this); |
| 400 | //data.print(); |
| 401 | //printf("\n"); |
| 402 | jboolean res = env->CallBooleanMethod(mObject, gBinderOffsets.mExecTransact, |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 403 | code, reinterpret_cast<jlong>(&data), reinterpret_cast<jlong>(reply), flags); |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 404 | |
Mathieu Chartier | 98671c3 | 2014-08-20 10:04:08 -0700 | [diff] [blame] | 405 | if (env->ExceptionCheck()) { |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 406 | ScopedLocalRef<jthrowable> excep(env, env->ExceptionOccurred()); |
| 407 | report_exception(env, excep.get(), |
Bjorn Bringert | 9013ccd | 2011-04-26 19:10:58 +0100 | [diff] [blame] | 408 | "*** Uncaught remote exception! " |
| 409 | "(Exceptions are not yet supported across processes.)"); |
| 410 | res = JNI_FALSE; |
Bjorn Bringert | 9013ccd | 2011-04-26 19:10:58 +0100 | [diff] [blame] | 411 | } |
| 412 | |
Dianne Hackborn | ce92b0d | 2014-09-30 11:28:18 -0700 | [diff] [blame] | 413 | // Check if the strict mode state changed while processing the |
| 414 | // call. The Binder state will be restored by the underlying |
| 415 | // Binder system in IPCThreadState, however we need to take care |
| 416 | // of the parallel Java state as well. |
| 417 | if (thread_state->getStrictModePolicy() != strict_policy_before) { |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 418 | set_dalvik_blockguard_policy(env, strict_policy_before); |
| 419 | } |
| 420 | |
Mathieu Chartier | 98671c3 | 2014-08-20 10:04:08 -0700 | [diff] [blame] | 421 | if (env->ExceptionCheck()) { |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 422 | ScopedLocalRef<jthrowable> excep(env, env->ExceptionOccurred()); |
| 423 | report_exception(env, excep.get(), |
Bjorn Bringert | 9013ccd | 2011-04-26 19:10:58 +0100 | [diff] [blame] | 424 | "*** Uncaught exception in onBinderStrictModePolicyChange"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 425 | } |
| 426 | |
Dianne Hackborn | a53de06 | 2012-05-08 18:53:51 -0700 | [diff] [blame] | 427 | // Need to always call through the native implementation of |
| 428 | // SYSPROPS_TRANSACTION. |
| 429 | if (code == SYSPROPS_TRANSACTION) { |
| 430 | BBinder::onTransact(code, data, reply, flags); |
| 431 | } |
| 432 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 433 | //aout << "onTransact to Java code; result=" << res << endl |
| 434 | // << "Transact from " << this << " to Java code returning " |
| 435 | // << reply << ": " << *reply << endl; |
| 436 | return res != JNI_FALSE ? NO_ERROR : UNKNOWN_TRANSACTION; |
| 437 | } |
| 438 | |
Steven Moreland | e52bb7d | 2018-10-10 11:24:58 -0700 | [diff] [blame] | 439 | status_t dump(int fd, const Vector<String16>& args) override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | { |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | private: |
| 445 | JavaVM* const mVM; |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 446 | jobject const mObject; // GlobalRef to Java Binder |
Steven Moreland | e52bb7d | 2018-10-10 11:24:58 -0700 | [diff] [blame] | 447 | |
| 448 | mutable std::once_flag mPopulateDescriptor; |
| 449 | mutable String16 mDescriptor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 450 | }; |
| 451 | |
| 452 | // ---------------------------------------------------------------------------- |
| 453 | |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 454 | class JavaBBinderHolder |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 455 | { |
| 456 | public: |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 457 | sp<JavaBBinder> get(JNIEnv* env, jobject obj) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | { |
| 459 | AutoMutex _l(mLock); |
| 460 | sp<JavaBBinder> b = mBinder.promote(); |
| 461 | if (b == NULL) { |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 462 | b = new JavaBBinder(env, obj); |
Steven Moreland | d587a55 | 2019-11-27 18:15:33 -0800 | [diff] [blame] | 463 | if (mVintf) { |
| 464 | ::android::internal::Stability::markVintf(b.get()); |
| 465 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | mBinder = b; |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 467 | ALOGV("Creating JavaBinder %p (refs %p) for Object %p, weakCount=%" PRId32 "\n", |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 468 | b.get(), b->getWeakRefs(), obj, b->getWeakRefs()->getWeakCount()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | return b; |
| 472 | } |
| 473 | |
| 474 | sp<JavaBBinder> getExisting() |
| 475 | { |
| 476 | AutoMutex _l(mLock); |
| 477 | return mBinder.promote(); |
| 478 | } |
| 479 | |
Steven Moreland | d587a55 | 2019-11-27 18:15:33 -0800 | [diff] [blame] | 480 | void markVintf() { |
| 481 | mVintf = true; |
| 482 | } |
| 483 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | private: |
| 485 | Mutex mLock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | wp<JavaBBinder> mBinder; |
Steven Moreland | d587a55 | 2019-11-27 18:15:33 -0800 | [diff] [blame] | 487 | |
| 488 | // in the future, we might condense this into int32_t stability, or if there |
| 489 | // is too much binder state here, we can think about making JavaBBinder an |
| 490 | // sp here (avoid recreating it) |
| 491 | bool mVintf = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | }; |
| 493 | |
| 494 | // ---------------------------------------------------------------------------- |
| 495 | |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 496 | // Per-IBinder death recipient bookkeeping. This is how we reconcile local jobject |
| 497 | // death recipient references passed in through JNI with the permanent corresponding |
| 498 | // JavaDeathRecipient objects. |
| 499 | |
| 500 | class JavaDeathRecipient; |
| 501 | |
| 502 | class DeathRecipientList : public RefBase { |
| 503 | List< sp<JavaDeathRecipient> > mList; |
| 504 | Mutex mLock; |
| 505 | |
| 506 | public: |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 507 | DeathRecipientList(); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 508 | ~DeathRecipientList(); |
| 509 | |
| 510 | void add(const sp<JavaDeathRecipient>& recipient); |
| 511 | void remove(const sp<JavaDeathRecipient>& recipient); |
| 512 | sp<JavaDeathRecipient> find(jobject recipient); |
Christopher Tate | 090c08f | 2015-05-19 18:16:58 -0700 | [diff] [blame] | 513 | |
| 514 | Mutex& lock(); // Use with care; specifically for mutual exclusion during binder death |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 515 | }; |
| 516 | |
| 517 | // ---------------------------------------------------------------------------- |
| 518 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 519 | class JavaDeathRecipient : public IBinder::DeathRecipient |
| 520 | { |
| 521 | public: |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 522 | JavaDeathRecipient(JNIEnv* env, jobject object, const sp<DeathRecipientList>& list) |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 523 | : mVM(jnienv_to_javavm(env)), mObject(env->NewGlobalRef(object)), |
| 524 | mObjectWeak(NULL), mList(list) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 525 | { |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 526 | // These objects manage their own lifetimes so are responsible for final bookkeeping. |
| 527 | // The list holds a strong reference to this object. |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 528 | LOGDEATH("Adding JDR %p to DRL %p", this, list.get()); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 529 | list->add(this); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 530 | |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 531 | gNumDeathRefsCreated.fetch_add(1, std::memory_order_relaxed); |
| 532 | gcIfManyNewRefs(env); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | void binderDied(const wp<IBinder>& who) |
| 536 | { |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 537 | LOGDEATH("Receiving binderDied() on JavaDeathRecipient %p\n", this); |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 538 | if (mObject != NULL) { |
| 539 | JNIEnv* env = javavm_to_jnienv(mVM); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 540 | |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 541 | env->CallStaticVoidMethod(gBinderProxyOffsets.mClass, |
| 542 | gBinderProxyOffsets.mSendDeathNotice, mObject); |
Mathieu Chartier | 98671c3 | 2014-08-20 10:04:08 -0700 | [diff] [blame] | 543 | if (env->ExceptionCheck()) { |
| 544 | jthrowable excep = env->ExceptionOccurred(); |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 545 | report_exception(env, excep, |
| 546 | "*** Uncaught exception returned from death notification!"); |
| 547 | } |
| 548 | |
Christopher Tate | 090c08f | 2015-05-19 18:16:58 -0700 | [diff] [blame] | 549 | // Serialize with our containing DeathRecipientList so that we can't |
| 550 | // delete the global ref on mObject while the list is being iterated. |
| 551 | sp<DeathRecipientList> list = mList.promote(); |
| 552 | if (list != NULL) { |
| 553 | AutoMutex _l(list->lock()); |
| 554 | |
| 555 | // Demote from strong ref to weak after binderDied() has been delivered, |
| 556 | // to allow the DeathRecipient and BinderProxy to be GC'd if no longer needed. |
| 557 | mObjectWeak = env->NewWeakGlobalRef(mObject); |
| 558 | env->DeleteGlobalRef(mObject); |
| 559 | mObject = NULL; |
| 560 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | void clearReference() |
| 565 | { |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 566 | sp<DeathRecipientList> list = mList.promote(); |
| 567 | if (list != NULL) { |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 568 | LOGDEATH("Removing JDR %p from DRL %p", this, list.get()); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 569 | list->remove(this); |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 570 | } else { |
| 571 | LOGDEATH("clearReference() on JDR %p but DRL wp purged", this); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 572 | } |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | bool matches(jobject obj) { |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 576 | bool result; |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 577 | JNIEnv* env = javavm_to_jnienv(mVM); |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 578 | |
| 579 | if (mObject != NULL) { |
| 580 | result = env->IsSameObject(obj, mObject); |
| 581 | } else { |
Andreas Gampe | 625e000 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 582 | ScopedLocalRef<jobject> me(env, env->NewLocalRef(mObjectWeak)); |
| 583 | result = env->IsSameObject(obj, me.get()); |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 584 | } |
| 585 | return result; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 586 | } |
| 587 | |
Christopher Tate | ac5e350 | 2011-08-25 15:48:09 -0700 | [diff] [blame] | 588 | void warnIfStillLive() { |
| 589 | if (mObject != NULL) { |
| 590 | // Okay, something is wrong -- we have a hard reference to a live death |
| 591 | // recipient on the VM side, but the list is being torn down. |
| 592 | JNIEnv* env = javavm_to_jnienv(mVM); |
Christopher Tate | 0d4a792 | 2011-08-30 12:09:43 -0700 | [diff] [blame] | 593 | ScopedLocalRef<jclass> objClassRef(env, env->GetObjectClass(mObject)); |
| 594 | ScopedLocalRef<jstring> nameRef(env, |
| 595 | (jstring) env->CallObjectMethod(objClassRef.get(), gClassOffsets.mGetName)); |
| 596 | ScopedUtfChars nameUtf(env, nameRef.get()); |
| 597 | if (nameUtf.c_str() != NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 598 | ALOGW("BinderProxy is being destroyed but the application did not call " |
Christopher Tate | 0d4a792 | 2011-08-30 12:09:43 -0700 | [diff] [blame] | 599 | "unlinkToDeath to unlink all of its death recipients beforehand. " |
| 600 | "Releasing leaked death recipient: %s", nameUtf.c_str()); |
| 601 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 602 | ALOGW("BinderProxy being destroyed; unable to get DR object name"); |
Christopher Tate | 0d4a792 | 2011-08-30 12:09:43 -0700 | [diff] [blame] | 603 | env->ExceptionClear(); |
| 604 | } |
Christopher Tate | ac5e350 | 2011-08-25 15:48:09 -0700 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 608 | protected: |
| 609 | virtual ~JavaDeathRecipient() |
| 610 | { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 611 | //ALOGI("Removing death ref: recipient=%p\n", mObject); |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 612 | gNumDeathRefsDeleted.fetch_add(1, std::memory_order_relaxed); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 613 | JNIEnv* env = javavm_to_jnienv(mVM); |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 614 | if (mObject != NULL) { |
| 615 | env->DeleteGlobalRef(mObject); |
| 616 | } else { |
| 617 | env->DeleteWeakGlobalRef(mObjectWeak); |
| 618 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | private: |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 622 | JavaVM* const mVM; |
Hans Boehm | eb6d62c | 2017-09-20 15:59:12 -0700 | [diff] [blame] | 623 | jobject mObject; // Initial strong ref to Java-side DeathRecipient. Cleared on binderDied(). |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 624 | jweak mObjectWeak; // Weak ref to the same Java-side DeathRecipient after binderDied(). |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 625 | wp<DeathRecipientList> mList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 626 | }; |
| 627 | |
| 628 | // ---------------------------------------------------------------------------- |
| 629 | |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 630 | DeathRecipientList::DeathRecipientList() { |
| 631 | LOGDEATH("New DRL @ %p", this); |
| 632 | } |
| 633 | |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 634 | DeathRecipientList::~DeathRecipientList() { |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 635 | LOGDEATH("Destroy DRL @ %p", this); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 636 | AutoMutex _l(mLock); |
| 637 | |
| 638 | // Should never happen -- the JavaDeathRecipient objects that have added themselves |
| 639 | // to the list are holding references on the list object. Only when they are torn |
| 640 | // down can the list header be destroyed. |
| 641 | if (mList.size() > 0) { |
Christopher Tate | ac5e350 | 2011-08-25 15:48:09 -0700 | [diff] [blame] | 642 | List< sp<JavaDeathRecipient> >::iterator iter; |
| 643 | for (iter = mList.begin(); iter != mList.end(); iter++) { |
| 644 | (*iter)->warnIfStillLive(); |
| 645 | } |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 646 | } |
| 647 | } |
| 648 | |
| 649 | void DeathRecipientList::add(const sp<JavaDeathRecipient>& recipient) { |
| 650 | AutoMutex _l(mLock); |
| 651 | |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 652 | LOGDEATH("DRL @ %p : add JDR %p", this, recipient.get()); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 653 | mList.push_back(recipient); |
| 654 | } |
| 655 | |
| 656 | void DeathRecipientList::remove(const sp<JavaDeathRecipient>& recipient) { |
| 657 | AutoMutex _l(mLock); |
| 658 | |
| 659 | List< sp<JavaDeathRecipient> >::iterator iter; |
| 660 | for (iter = mList.begin(); iter != mList.end(); iter++) { |
| 661 | if (*iter == recipient) { |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 662 | LOGDEATH("DRL @ %p : remove JDR %p", this, recipient.get()); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 663 | mList.erase(iter); |
| 664 | return; |
| 665 | } |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | sp<JavaDeathRecipient> DeathRecipientList::find(jobject recipient) { |
| 670 | AutoMutex _l(mLock); |
| 671 | |
| 672 | List< sp<JavaDeathRecipient> >::iterator iter; |
| 673 | for (iter = mList.begin(); iter != mList.end(); iter++) { |
| 674 | if ((*iter)->matches(recipient)) { |
| 675 | return *iter; |
| 676 | } |
| 677 | } |
| 678 | return NULL; |
| 679 | } |
| 680 | |
Christopher Tate | 090c08f | 2015-05-19 18:16:58 -0700 | [diff] [blame] | 681 | Mutex& DeathRecipientList::lock() { |
| 682 | return mLock; |
| 683 | } |
| 684 | |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 685 | // ---------------------------------------------------------------------------- |
| 686 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 687 | namespace android { |
| 688 | |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 689 | // We aggregate native pointer fields for BinderProxy in a single object to allow |
| 690 | // management with a single NativeAllocationRegistry, and to reduce the number of JNI |
| 691 | // Java field accesses. This costs us some extra indirections here. |
| 692 | struct BinderProxyNativeData { |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 693 | // Both fields are constant and not null once javaObjectForIBinder returns this as |
| 694 | // part of a BinderProxy. |
| 695 | |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 696 | // The native IBinder proxied by this BinderProxy. |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 697 | sp<IBinder> mObject; |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 698 | |
| 699 | // Death recipients for mObject. Reference counted only because DeathRecipients |
| 700 | // hold a weak reference that can be temporarily promoted. |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 701 | sp<DeathRecipientList> mOrgue; // Death recipients for mObject. |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 702 | }; |
| 703 | |
| 704 | BinderProxyNativeData* getBPNativeData(JNIEnv* env, jobject obj) { |
| 705 | return (BinderProxyNativeData *) env->GetLongField(obj, gBinderProxyOffsets.mNativeData); |
| 706 | } |
| 707 | |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 708 | // If the argument is a JavaBBinder, return the Java object that was used to create it. |
| 709 | // Otherwise return a BinderProxy for the IBinder. If a previous call was passed the |
| 710 | // same IBinder, and the original BinderProxy is still alive, return the same BinderProxy. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 711 | jobject javaObjectForIBinder(JNIEnv* env, const sp<IBinder>& val) |
| 712 | { |
| 713 | if (val == NULL) return NULL; |
| 714 | |
| 715 | if (val->checkSubclass(&gBinderOffsets)) { |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 716 | // It's a JavaBBinder created by ibinderForJavaObject. Already has Java object. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 717 | jobject object = static_cast<JavaBBinder*>(val.get())->object(); |
Christopher Tate | 86284c6 | 2011-08-17 15:19:29 -0700 | [diff] [blame] | 718 | 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] | 719 | return object; |
| 720 | } |
| 721 | |
Martijn Coenen | d3ef4bf | 2018-07-05 14:58:59 +0200 | [diff] [blame] | 722 | BinderProxyNativeData* nativeData = new BinderProxyNativeData(); |
| 723 | nativeData->mOrgue = new DeathRecipientList; |
| 724 | nativeData->mObject = val; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 725 | |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 726 | jobject object = env->CallStaticObjectMethod(gBinderProxyOffsets.mClass, |
| 727 | gBinderProxyOffsets.mGetInstance, (jlong) nativeData, (jlong) val.get()); |
| 728 | if (env->ExceptionCheck()) { |
Hans Boehm | 03477cb | 2018-02-15 16:12:51 -0800 | [diff] [blame] | 729 | // In the exception case, getInstance still took ownership of nativeData. |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 730 | return NULL; |
| 731 | } |
| 732 | BinderProxyNativeData* actualNativeData = getBPNativeData(env, object); |
| 733 | if (actualNativeData == nativeData) { |
Martijn Coenen | d3ef4bf | 2018-07-05 14:58:59 +0200 | [diff] [blame] | 734 | // Created a new Proxy |
| 735 | uint32_t numProxies = gNumProxies.fetch_add(1, std::memory_order_relaxed); |
| 736 | uint32_t numLastWarned = gProxiesWarned.load(std::memory_order_relaxed); |
| 737 | if (numProxies >= numLastWarned + PROXY_WARN_INTERVAL) { |
| 738 | // Multiple threads can get here, make sure only one of them gets to |
| 739 | // update the warn counter. |
| 740 | if (gProxiesWarned.compare_exchange_strong(numLastWarned, |
| 741 | numLastWarned + PROXY_WARN_INTERVAL, std::memory_order_relaxed)) { |
| 742 | ALOGW("Unexpectedly many live BinderProxies: %d\n", numProxies); |
| 743 | } |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 744 | } |
| 745 | } else { |
Martijn Coenen | d3ef4bf | 2018-07-05 14:58:59 +0200 | [diff] [blame] | 746 | delete nativeData; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | return object; |
| 750 | } |
| 751 | |
| 752 | sp<IBinder> ibinderForJavaObject(JNIEnv* env, jobject obj) |
| 753 | { |
| 754 | if (obj == NULL) return NULL; |
| 755 | |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 756 | // Instance of Binder? |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 757 | if (env->IsInstanceOf(obj, gBinderOffsets.mClass)) { |
| 758 | JavaBBinderHolder* jbh = (JavaBBinderHolder*) |
Ashok Bhat | 8ab665d | 2014-01-22 16:00:20 +0000 | [diff] [blame] | 759 | env->GetLongField(obj, gBinderOffsets.mObject); |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 760 | return jbh->get(env, obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 761 | } |
| 762 | |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 763 | // Instance of BinderProxy? |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 764 | if (env->IsInstanceOf(obj, gBinderProxyOffsets.mClass)) { |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 765 | return getBPNativeData(env, obj)->mObject; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 766 | } |
| 767 | |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 768 | ALOGW("ibinderForJavaObject: %p is not a Binder object", obj); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 769 | return NULL; |
| 770 | } |
| 771 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 772 | jobject newParcelFileDescriptor(JNIEnv* env, jobject fileDesc) |
| 773 | { |
| 774 | return env->NewObject( |
| 775 | gParcelFileDescriptorOffsets.mClass, gParcelFileDescriptorOffsets.mConstructor, fileDesc); |
| 776 | } |
| 777 | |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 778 | void set_dalvik_blockguard_policy(JNIEnv* env, jint strict_policy) |
| 779 | { |
| 780 | // Call back into android.os.StrictMode#onBinderStrictModePolicyChange |
| 781 | // to sync our state back to it. See the comments in StrictMode.java. |
| 782 | env->CallStaticVoidMethod(gStrictModeCallbackOffsets.mClass, |
| 783 | gStrictModeCallbackOffsets.mCallback, |
| 784 | strict_policy); |
| 785 | } |
| 786 | |
| 787 | void signalExceptionForError(JNIEnv* env, jobject obj, status_t err, |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 788 | bool canThrowRemoteException, int parcelSize) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | { |
| 790 | switch (err) { |
| 791 | case UNKNOWN_ERROR: |
| 792 | jniThrowException(env, "java/lang/RuntimeException", "Unknown error"); |
| 793 | break; |
| 794 | case NO_MEMORY: |
| 795 | jniThrowException(env, "java/lang/OutOfMemoryError", NULL); |
| 796 | break; |
| 797 | case INVALID_OPERATION: |
| 798 | jniThrowException(env, "java/lang/UnsupportedOperationException", NULL); |
| 799 | break; |
| 800 | case BAD_VALUE: |
| 801 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 802 | break; |
| 803 | case BAD_INDEX: |
| 804 | jniThrowException(env, "java/lang/IndexOutOfBoundsException", NULL); |
| 805 | break; |
| 806 | case BAD_TYPE: |
| 807 | jniThrowException(env, "java/lang/IllegalArgumentException", NULL); |
| 808 | break; |
| 809 | case NAME_NOT_FOUND: |
| 810 | jniThrowException(env, "java/util/NoSuchElementException", NULL); |
| 811 | break; |
| 812 | case PERMISSION_DENIED: |
| 813 | jniThrowException(env, "java/lang/SecurityException", NULL); |
| 814 | break; |
| 815 | case NOT_ENOUGH_DATA: |
| 816 | jniThrowException(env, "android/os/ParcelFormatException", "Not enough data"); |
| 817 | break; |
| 818 | case NO_INIT: |
| 819 | jniThrowException(env, "java/lang/RuntimeException", "Not initialized"); |
| 820 | break; |
| 821 | case ALREADY_EXISTS: |
| 822 | jniThrowException(env, "java/lang/RuntimeException", "Item already exists"); |
| 823 | break; |
| 824 | case DEAD_OBJECT: |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 825 | // DeadObjectException is a checked exception, only throw from certain methods. |
| 826 | jniThrowException(env, canThrowRemoteException |
| 827 | ? "android/os/DeadObjectException" |
| 828 | : "java/lang/RuntimeException", NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 829 | break; |
| 830 | case UNKNOWN_TRANSACTION: |
| 831 | jniThrowException(env, "java/lang/RuntimeException", "Unknown transaction code"); |
| 832 | break; |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 833 | case FAILED_TRANSACTION: { |
| 834 | ALOGE("!!! FAILED BINDER TRANSACTION !!! (parcel size = %d)", parcelSize); |
Christopher Tate | 02ca7a7 | 2015-06-24 18:16:42 -0700 | [diff] [blame] | 835 | const char* exceptionToThrow; |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 836 | char msg[128]; |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 837 | // TransactionTooLargeException is a checked exception, only throw from certain methods. |
| 838 | // FIXME: Transaction too large is the most common reason for FAILED_TRANSACTION |
| 839 | // but it is not the only one. The Binder driver can return BR_FAILED_REPLY |
| 840 | // for other reasons also, such as if the transaction is malformed or |
| 841 | // refers to an FD that has been closed. We should change the driver |
| 842 | // to enable us to distinguish these cases in the future. |
Christopher Tate | 02ca7a7 | 2015-06-24 18:16:42 -0700 | [diff] [blame] | 843 | if (canThrowRemoteException && parcelSize > 200*1024) { |
| 844 | // bona fide large payload |
| 845 | exceptionToThrow = "android/os/TransactionTooLargeException"; |
| 846 | snprintf(msg, sizeof(msg)-1, "data parcel size %d bytes", parcelSize); |
| 847 | } else { |
| 848 | // Heuristic: a payload smaller than this threshold "shouldn't" be too |
| 849 | // big, so it's probably some other, more subtle problem. In practice |
Christopher Tate | ffd5864 | 2015-06-29 11:00:15 -0700 | [diff] [blame] | 850 | // 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] | 851 | // transaction was already in flight. |
Christopher Tate | ffd5864 | 2015-06-29 11:00:15 -0700 | [diff] [blame] | 852 | exceptionToThrow = (canThrowRemoteException) |
| 853 | ? "android/os/DeadObjectException" |
| 854 | : "java/lang/RuntimeException"; |
Christopher Tate | 02ca7a7 | 2015-06-24 18:16:42 -0700 | [diff] [blame] | 855 | snprintf(msg, sizeof(msg)-1, |
| 856 | "Transaction failed on small parcel; remote process probably died"); |
| 857 | } |
| 858 | jniThrowException(env, exceptionToThrow, msg); |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 859 | } break; |
Dianne Hackborn | 9ecebbf | 2011-09-28 23:19:47 -0400 | [diff] [blame] | 860 | case FDS_NOT_ALLOWED: |
| 861 | jniThrowException(env, "java/lang/RuntimeException", |
| 862 | "Not allowed to write file descriptors here"); |
| 863 | break; |
Christopher Wiley | a94fc52 | 2015-11-22 14:21:25 -0800 | [diff] [blame] | 864 | case UNEXPECTED_NULL: |
| 865 | jniThrowNullPointerException(env, NULL); |
| 866 | break; |
Dianne Hackborn | cbefd8d | 2014-05-14 11:42:00 -0700 | [diff] [blame] | 867 | case -EBADF: |
| 868 | jniThrowException(env, "java/lang/RuntimeException", |
| 869 | "Bad file descriptor"); |
| 870 | break; |
| 871 | case -ENFILE: |
| 872 | jniThrowException(env, "java/lang/RuntimeException", |
| 873 | "File table overflow"); |
| 874 | break; |
| 875 | case -EMFILE: |
| 876 | jniThrowException(env, "java/lang/RuntimeException", |
| 877 | "Too many open files"); |
| 878 | break; |
| 879 | case -EFBIG: |
| 880 | jniThrowException(env, "java/lang/RuntimeException", |
| 881 | "File too large"); |
| 882 | break; |
| 883 | case -ENOSPC: |
| 884 | jniThrowException(env, "java/lang/RuntimeException", |
| 885 | "No space left on device"); |
| 886 | break; |
| 887 | case -ESPIPE: |
| 888 | jniThrowException(env, "java/lang/RuntimeException", |
| 889 | "Illegal seek"); |
| 890 | break; |
| 891 | case -EROFS: |
| 892 | jniThrowException(env, "java/lang/RuntimeException", |
| 893 | "Read-only file system"); |
| 894 | break; |
| 895 | case -EMLINK: |
| 896 | jniThrowException(env, "java/lang/RuntimeException", |
| 897 | "Too many links"); |
| 898 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 899 | default: |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 900 | ALOGE("Unknown binder error code. 0x%" PRIx32, err); |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 901 | String8 msg; |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 902 | msg.appendFormat("Unknown binder error code. 0x%" PRIx32, err); |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 903 | // RemoteException is a checked exception, only throw from certain methods. |
| 904 | jniThrowException(env, canThrowRemoteException |
| 905 | ? "android/os/RemoteException" : "java/lang/RuntimeException", msg.string()); |
| 906 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 907 | } |
| 908 | } |
| 909 | |
| 910 | } |
| 911 | |
| 912 | // ---------------------------------------------------------------------------- |
| 913 | |
Olivier Gaillard | d8c3df5 | 2018-10-23 09:58:42 +0100 | [diff] [blame] | 914 | static jint android_os_Binder_getCallingPid() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 915 | { |
| 916 | return IPCThreadState::self()->getCallingPid(); |
| 917 | } |
| 918 | |
Olivier Gaillard | d8c3df5 | 2018-10-23 09:58:42 +0100 | [diff] [blame] | 919 | static jint android_os_Binder_getCallingUid() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 920 | { |
| 921 | return IPCThreadState::self()->getCallingUid(); |
| 922 | } |
| 923 | |
Nikita Ioffe | a929cf0 | 2019-01-03 13:35:22 +0000 | [diff] [blame] | 924 | static jboolean android_os_Binder_isHandlingTransaction() |
| 925 | { |
| 926 | return IPCThreadState::self()->isServingCall(); |
| 927 | } |
| 928 | |
Olivier Gaillard | d8c3df5 | 2018-10-23 09:58:42 +0100 | [diff] [blame] | 929 | static jlong android_os_Binder_clearCallingIdentity() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 930 | { |
| 931 | return IPCThreadState::self()->clearCallingIdentity(); |
| 932 | } |
| 933 | |
| 934 | static void android_os_Binder_restoreCallingIdentity(JNIEnv* env, jobject clazz, jlong token) |
| 935 | { |
Dianne Hackborn | cf3004a | 2011-03-14 14:24:04 -0700 | [diff] [blame] | 936 | // XXX temporary sanity check to debug crashes. |
| 937 | int uid = (int)(token>>32); |
| 938 | if (uid > 0 && uid < 999) { |
| 939 | // In Android currently there are no uids in this range. |
| 940 | char buf[128]; |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 941 | sprintf(buf, "Restoring bad calling ident: 0x%" PRIx64, token); |
Dianne Hackborn | cf3004a | 2011-03-14 14:24:04 -0700 | [diff] [blame] | 942 | jniThrowException(env, "java/lang/IllegalStateException", buf); |
| 943 | return; |
| 944 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | IPCThreadState::self()->restoreCallingIdentity(token); |
| 946 | } |
| 947 | |
Olivier Gaillard | d8c3df5 | 2018-10-23 09:58:42 +0100 | [diff] [blame] | 948 | static void android_os_Binder_setThreadStrictModePolicy(jint policyMask) |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 949 | { |
| 950 | IPCThreadState::self()->setStrictModePolicy(policyMask); |
| 951 | } |
| 952 | |
Olivier Gaillard | d8c3df5 | 2018-10-23 09:58:42 +0100 | [diff] [blame] | 953 | static jint android_os_Binder_getThreadStrictModePolicy() |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 954 | { |
| 955 | return IPCThreadState::self()->getStrictModePolicy(); |
| 956 | } |
| 957 | |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 958 | static jlong android_os_Binder_setCallingWorkSourceUid(jint workSource) |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 959 | { |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 960 | return IPCThreadState::self()->setCallingWorkSourceUid(workSource); |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 961 | } |
| 962 | |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 963 | static jlong android_os_Binder_getCallingWorkSourceUid() |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 964 | { |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 965 | return IPCThreadState::self()->getCallingWorkSourceUid(); |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 966 | } |
| 967 | |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 968 | static jlong android_os_Binder_clearCallingWorkSource() |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 969 | { |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 970 | return IPCThreadState::self()->clearCallingWorkSource(); |
| 971 | } |
| 972 | |
Olivier Gaillard | a16b83c | 2018-12-11 23:09:05 +0000 | [diff] [blame] | 973 | static void android_os_Binder_restoreCallingWorkSource(jlong token) |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 974 | { |
| 975 | IPCThreadState::self()->restoreCallingWorkSource(token); |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 976 | } |
| 977 | |
Steven Moreland | d587a55 | 2019-11-27 18:15:33 -0800 | [diff] [blame] | 978 | static void android_os_Binder_markVintfStability(JNIEnv* env, jobject clazz) { |
| 979 | JavaBBinderHolder* jbh = |
| 980 | (JavaBBinderHolder*) env->GetLongField(clazz, gBinderOffsets.mObject); |
| 981 | jbh->markVintf(); |
| 982 | } |
| 983 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 984 | static void android_os_Binder_flushPendingCommands(JNIEnv* env, jobject clazz) |
| 985 | { |
| 986 | IPCThreadState::self()->flushCommands(); |
| 987 | } |
| 988 | |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 989 | static jlong android_os_Binder_getNativeBBinderHolder(JNIEnv* env, jobject clazz) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 990 | { |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 991 | JavaBBinderHolder* jbh = new JavaBBinderHolder(); |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 992 | return (jlong) jbh; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 993 | } |
| 994 | |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 995 | static void Binder_destroy(void* rawJbh) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 996 | { |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 997 | JavaBBinderHolder* jbh = (JavaBBinderHolder*) rawJbh; |
| 998 | ALOGV("Java Binder: deleting holder %p", jbh); |
| 999 | delete jbh; |
| 1000 | } |
| 1001 | |
| 1002 | JNIEXPORT jlong JNICALL android_os_Binder_getNativeFinalizer(JNIEnv*, jclass) { |
| 1003 | return (jlong) Binder_destroy; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1004 | } |
| 1005 | |
Wale Ogunwale | d7fdd02 | 2015-04-13 16:22:38 -0700 | [diff] [blame] | 1006 | static void android_os_Binder_blockUntilThreadAvailable(JNIEnv* env, jobject clazz) |
| 1007 | { |
| 1008 | return IPCThreadState::self()->blockUntilThreadAvailable(); |
| 1009 | } |
| 1010 | |
Jon Spivack | 9e45fde | 2019-10-09 17:23:00 -0700 | [diff] [blame] | 1011 | static jobject android_os_Binder_waitForService( |
| 1012 | JNIEnv *env, |
| 1013 | jclass /* clazzObj */, |
| 1014 | jstring serviceNameObj) { |
| 1015 | |
| 1016 | const jchar* serviceName = env->GetStringCritical(serviceNameObj, nullptr); |
| 1017 | if (!serviceName) { |
| 1018 | signalExceptionForError(env, nullptr, BAD_VALUE, true /*canThrowRemoteException*/); |
| 1019 | return nullptr; |
| 1020 | } |
| 1021 | String16 nameCopy = String16(reinterpret_cast<const char16_t *>(serviceName), |
| 1022 | env->GetStringLength(serviceNameObj)); |
| 1023 | env->ReleaseStringCritical(serviceNameObj, serviceName); |
| 1024 | |
| 1025 | auto sm = android::defaultServiceManager(); |
| 1026 | sp<IBinder> service = sm->waitForService(nameCopy); |
| 1027 | |
| 1028 | if (!service) { |
| 1029 | signalExceptionForError(env, nullptr, NAME_NOT_FOUND, true /*canThrowRemoteException*/); |
| 1030 | return nullptr; |
| 1031 | } |
| 1032 | |
| 1033 | return javaObjectForIBinder(env, service); |
| 1034 | } |
| 1035 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1036 | // ---------------------------------------------------------------------------- |
| 1037 | |
| 1038 | static const JNINativeMethod gBinderMethods[] = { |
| 1039 | /* name, signature, funcPtr */ |
Olivier Gaillard | d8c3df5 | 2018-10-23 09:58:42 +0100 | [diff] [blame] | 1040 | // @CriticalNative |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1041 | { "getCallingPid", "()I", (void*)android_os_Binder_getCallingPid }, |
Olivier Gaillard | d8c3df5 | 2018-10-23 09:58:42 +0100 | [diff] [blame] | 1042 | // @CriticalNative |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1043 | { "getCallingUid", "()I", (void*)android_os_Binder_getCallingUid }, |
Olivier Gaillard | d8c3df5 | 2018-10-23 09:58:42 +0100 | [diff] [blame] | 1044 | // @CriticalNative |
Nikita Ioffe | a929cf0 | 2019-01-03 13:35:22 +0000 | [diff] [blame] | 1045 | { "isHandlingTransaction", "()Z", (void*)android_os_Binder_isHandlingTransaction }, |
| 1046 | // @CriticalNative |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1047 | { "clearCallingIdentity", "()J", (void*)android_os_Binder_clearCallingIdentity }, |
| 1048 | { "restoreCallingIdentity", "(J)V", (void*)android_os_Binder_restoreCallingIdentity }, |
Olivier Gaillard | d8c3df5 | 2018-10-23 09:58:42 +0100 | [diff] [blame] | 1049 | // @CriticalNative |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 1050 | { "setThreadStrictModePolicy", "(I)V", (void*)android_os_Binder_setThreadStrictModePolicy }, |
Olivier Gaillard | d8c3df5 | 2018-10-23 09:58:42 +0100 | [diff] [blame] | 1051 | // @CriticalNative |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 1052 | { "getThreadStrictModePolicy", "()I", (void*)android_os_Binder_getThreadStrictModePolicy }, |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 1053 | // @CriticalNative |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 1054 | { "setCallingWorkSourceUid", "(I)J", (void*)android_os_Binder_setCallingWorkSourceUid }, |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 1055 | // @CriticalNative |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 1056 | { "getCallingWorkSourceUid", "()I", (void*)android_os_Binder_getCallingWorkSourceUid }, |
Olivier Gaillard | e4ff397 | 2018-08-16 14:01:58 +0100 | [diff] [blame] | 1057 | // @CriticalNative |
Olivier Gaillard | d542b1c | 2018-11-14 15:24:35 +0000 | [diff] [blame] | 1058 | { "clearCallingWorkSource", "()J", (void*)android_os_Binder_clearCallingWorkSource }, |
| 1059 | { "restoreCallingWorkSource", "(J)V", (void*)android_os_Binder_restoreCallingWorkSource }, |
Steven Moreland | d587a55 | 2019-11-27 18:15:33 -0800 | [diff] [blame] | 1060 | { "markVintfStability", "()V", (void*)android_os_Binder_markVintfStability}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1061 | { "flushPendingCommands", "()V", (void*)android_os_Binder_flushPendingCommands }, |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 1062 | { "getNativeBBinderHolder", "()J", (void*)android_os_Binder_getNativeBBinderHolder }, |
| 1063 | { "getNativeFinalizer", "()J", (void*)android_os_Binder_getNativeFinalizer }, |
Jon Spivack | 9e45fde | 2019-10-09 17:23:00 -0700 | [diff] [blame] | 1064 | { "blockUntilThreadAvailable", "()V", (void*)android_os_Binder_blockUntilThreadAvailable }, |
| 1065 | { "waitForService", "(Ljava/lang/String;)Landroid/os/IBinder;", (void*)android_os_Binder_waitForService } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1066 | }; |
| 1067 | |
| 1068 | const char* const kBinderPathName = "android/os/Binder"; |
| 1069 | |
| 1070 | static int int_register_android_os_Binder(JNIEnv* env) |
| 1071 | { |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1072 | jclass clazz = FindClassOrDie(env, kBinderPathName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1073 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1074 | gBinderOffsets.mClass = MakeGlobalRefOrDie(env, clazz); |
| 1075 | gBinderOffsets.mExecTransact = GetMethodIDOrDie(env, clazz, "execTransact", "(IJJI)Z"); |
Steven Moreland | e52bb7d | 2018-10-10 11:24:58 -0700 | [diff] [blame] | 1076 | gBinderOffsets.mGetInterfaceDescriptor = GetMethodIDOrDie(env, clazz, "getInterfaceDescriptor", |
| 1077 | "()Ljava/lang/String;"); |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1078 | gBinderOffsets.mObject = GetFieldIDOrDie(env, clazz, "mObject", "J"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1079 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1080 | return RegisterMethodsOrDie( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1081 | env, kBinderPathName, |
| 1082 | gBinderMethods, NELEM(gBinderMethods)); |
| 1083 | } |
| 1084 | |
| 1085 | // **************************************************************************** |
| 1086 | // **************************************************************************** |
| 1087 | // **************************************************************************** |
| 1088 | |
| 1089 | namespace android { |
| 1090 | |
| 1091 | jint android_os_Debug_getLocalObjectCount(JNIEnv* env, jobject clazz) |
| 1092 | { |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 1093 | return gNumLocalRefsCreated - gNumLocalRefsDeleted; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | jint android_os_Debug_getProxyObjectCount(JNIEnv* env, jobject clazz) |
| 1097 | { |
Martijn Coenen | d3ef4bf | 2018-07-05 14:58:59 +0200 | [diff] [blame] | 1098 | return gNumProxies.load(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | jint android_os_Debug_getDeathObjectCount(JNIEnv* env, jobject clazz) |
| 1102 | { |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 1103 | return gNumDeathRefsCreated - gNumDeathRefsDeleted; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | } |
| 1107 | |
| 1108 | // **************************************************************************** |
| 1109 | // **************************************************************************** |
| 1110 | // **************************************************************************** |
| 1111 | |
| 1112 | static jobject android_os_BinderInternal_getContextObject(JNIEnv* env, jobject clazz) |
| 1113 | { |
| 1114 | sp<IBinder> b = ProcessState::self()->getContextObject(NULL); |
| 1115 | return javaObjectForIBinder(env, b); |
| 1116 | } |
| 1117 | |
| 1118 | static void android_os_BinderInternal_joinThreadPool(JNIEnv* env, jobject clazz) |
| 1119 | { |
| 1120 | sp<IBinder> b = ProcessState::self()->getContextObject(NULL); |
| 1121 | android::IPCThreadState::self()->joinThreadPool(); |
| 1122 | } |
| 1123 | |
Dianne Hackborn | 887f355 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 1124 | static void android_os_BinderInternal_disableBackgroundScheduling(JNIEnv* env, |
| 1125 | jobject clazz, jboolean disable) |
| 1126 | { |
| 1127 | IPCThreadState::disableBackgroundScheduling(disable ? true : false); |
| 1128 | } |
| 1129 | |
Tim Murray | eef4a3d | 2016-04-19 14:14:20 -0700 | [diff] [blame] | 1130 | static void android_os_BinderInternal_setMaxThreads(JNIEnv* env, |
| 1131 | jobject clazz, jint maxThreads) |
| 1132 | { |
| 1133 | ProcessState::self()->setThreadPoolMaxThreadCount(maxThreads); |
| 1134 | } |
| 1135 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1136 | static void android_os_BinderInternal_handleGc(JNIEnv* env, jobject clazz) |
| 1137 | { |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 1138 | ALOGV("Gc has executed, updating Refs count at GC"); |
| 1139 | gCollectedAtRefs = gNumLocalRefsCreated + gNumDeathRefsCreated; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1140 | } |
| 1141 | |
Michael Wachenschwanz | 5518246 | 2017-08-14 23:10:13 -0700 | [diff] [blame] | 1142 | static void android_os_BinderInternal_proxyLimitcallback(int uid) |
| 1143 | { |
| 1144 | JNIEnv *env = AndroidRuntime::getJNIEnv(); |
| 1145 | env->CallStaticVoidMethod(gBinderInternalOffsets.mClass, |
| 1146 | gBinderInternalOffsets.mProxyLimitCallback, |
| 1147 | uid); |
Martijn Coenen | dfa390e | 2018-06-05 11:02:23 +0200 | [diff] [blame] | 1148 | |
| 1149 | if (env->ExceptionCheck()) { |
| 1150 | ScopedLocalRef<jthrowable> excep(env, env->ExceptionOccurred()); |
| 1151 | report_exception(env, excep.get(), |
| 1152 | "*** Uncaught exception in binderProxyLimitCallbackFromNative"); |
| 1153 | } |
Michael Wachenschwanz | 5518246 | 2017-08-14 23:10:13 -0700 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | static void android_os_BinderInternal_setBinderProxyCountEnabled(JNIEnv* env, jobject clazz, |
| 1157 | jboolean enable) |
| 1158 | { |
| 1159 | BpBinder::setCountByUidEnabled((bool) enable); |
| 1160 | } |
| 1161 | |
| 1162 | static jobject android_os_BinderInternal_getBinderProxyPerUidCounts(JNIEnv* env, jclass clazz) |
| 1163 | { |
| 1164 | Vector<uint32_t> uids, counts; |
| 1165 | BpBinder::getCountByUid(uids, counts); |
| 1166 | jobject sparseIntArray = env->NewObject(gSparseIntArrayOffsets.classObject, |
| 1167 | gSparseIntArrayOffsets.constructor); |
| 1168 | for (size_t i = 0; i < uids.size(); i++) { |
| 1169 | env->CallVoidMethod(sparseIntArray, gSparseIntArrayOffsets.put, |
| 1170 | static_cast<jint>(uids[i]), static_cast<jint>(counts[i])); |
| 1171 | } |
| 1172 | return sparseIntArray; |
| 1173 | } |
| 1174 | |
| 1175 | static jint android_os_BinderInternal_getBinderProxyCount(JNIEnv* env, jobject clazz, jint uid) { |
| 1176 | return static_cast<jint>(BpBinder::getBinderProxyCount(static_cast<uint32_t>(uid))); |
| 1177 | } |
| 1178 | |
| 1179 | static void android_os_BinderInternal_setBinderProxyCountWatermarks(JNIEnv* env, jobject clazz, |
| 1180 | jint high, jint low) |
| 1181 | { |
| 1182 | BpBinder::setBinderProxyCountWatermarks(high, low); |
| 1183 | } |
| 1184 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1185 | // ---------------------------------------------------------------------------- |
| 1186 | |
| 1187 | static const JNINativeMethod gBinderInternalMethods[] = { |
| 1188 | /* name, signature, funcPtr */ |
| 1189 | { "getContextObject", "()Landroid/os/IBinder;", (void*)android_os_BinderInternal_getContextObject }, |
| 1190 | { "joinThreadPool", "()V", (void*)android_os_BinderInternal_joinThreadPool }, |
Dianne Hackborn | 887f355 | 2009-12-07 17:59:37 -0800 | [diff] [blame] | 1191 | { "disableBackgroundScheduling", "(Z)V", (void*)android_os_BinderInternal_disableBackgroundScheduling }, |
Tim Murray | eef4a3d | 2016-04-19 14:14:20 -0700 | [diff] [blame] | 1192 | { "setMaxThreads", "(I)V", (void*)android_os_BinderInternal_setMaxThreads }, |
Michael Wachenschwanz | 5518246 | 2017-08-14 23:10:13 -0700 | [diff] [blame] | 1193 | { "handleGc", "()V", (void*)android_os_BinderInternal_handleGc }, |
| 1194 | { "nSetBinderProxyCountEnabled", "(Z)V", (void*)android_os_BinderInternal_setBinderProxyCountEnabled }, |
| 1195 | { "nGetBinderProxyPerUidCounts", "()Landroid/util/SparseIntArray;", (void*)android_os_BinderInternal_getBinderProxyPerUidCounts }, |
| 1196 | { "nGetBinderProxyCount", "(I)I", (void*)android_os_BinderInternal_getBinderProxyCount }, |
| 1197 | { "nSetBinderProxyCountWatermarks", "(II)V", (void*)android_os_BinderInternal_setBinderProxyCountWatermarks} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1198 | }; |
| 1199 | |
| 1200 | const char* const kBinderInternalPathName = "com/android/internal/os/BinderInternal"; |
| 1201 | |
| 1202 | static int int_register_android_os_BinderInternal(JNIEnv* env) |
| 1203 | { |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1204 | jclass clazz = FindClassOrDie(env, kBinderInternalPathName); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1205 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1206 | gBinderInternalOffsets.mClass = MakeGlobalRefOrDie(env, clazz); |
| 1207 | gBinderInternalOffsets.mForceGc = GetStaticMethodIDOrDie(env, clazz, "forceBinderGc", "()V"); |
Michael Wachenschwanz | 5518246 | 2017-08-14 23:10:13 -0700 | [diff] [blame] | 1208 | gBinderInternalOffsets.mProxyLimitCallback = GetStaticMethodIDOrDie(env, clazz, "binderProxyLimitCallbackFromNative", "(I)V"); |
| 1209 | |
| 1210 | jclass SparseIntArrayClass = FindClassOrDie(env, "android/util/SparseIntArray"); |
| 1211 | gSparseIntArrayOffsets.classObject = MakeGlobalRefOrDie(env, SparseIntArrayClass); |
| 1212 | gSparseIntArrayOffsets.constructor = GetMethodIDOrDie(env, gSparseIntArrayOffsets.classObject, |
| 1213 | "<init>", "()V"); |
| 1214 | gSparseIntArrayOffsets.put = GetMethodIDOrDie(env, gSparseIntArrayOffsets.classObject, "put", |
| 1215 | "(II)V"); |
| 1216 | |
| 1217 | BpBinder::setLimitCallback(android_os_BinderInternal_proxyLimitcallback); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1218 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1219 | return RegisterMethodsOrDie( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1220 | env, kBinderInternalPathName, |
| 1221 | gBinderInternalMethods, NELEM(gBinderInternalMethods)); |
| 1222 | } |
| 1223 | |
| 1224 | // **************************************************************************** |
| 1225 | // **************************************************************************** |
| 1226 | // **************************************************************************** |
| 1227 | |
| 1228 | static jboolean android_os_BinderProxy_pingBinder(JNIEnv* env, jobject obj) |
| 1229 | { |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 1230 | IBinder* target = getBPNativeData(env, obj)->mObject.get(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1231 | if (target == NULL) { |
| 1232 | return JNI_FALSE; |
| 1233 | } |
| 1234 | status_t err = target->pingBinder(); |
| 1235 | return err == NO_ERROR ? JNI_TRUE : JNI_FALSE; |
| 1236 | } |
| 1237 | |
| 1238 | static jstring android_os_BinderProxy_getInterfaceDescriptor(JNIEnv* env, jobject obj) |
| 1239 | { |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 1240 | IBinder* target = getBPNativeData(env, obj)->mObject.get(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1241 | if (target != NULL) { |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1242 | const String16& desc = target->getInterfaceDescriptor(); |
Dan Albert | 6698749 | 2014-11-20 11:41:21 -0800 | [diff] [blame] | 1243 | return env->NewString(reinterpret_cast<const jchar*>(desc.string()), |
| 1244 | desc.size()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1245 | } |
| 1246 | jniThrowException(env, "java/lang/RuntimeException", |
| 1247 | "No binder found for object"); |
| 1248 | return NULL; |
| 1249 | } |
| 1250 | |
| 1251 | static jboolean android_os_BinderProxy_isBinderAlive(JNIEnv* env, jobject obj) |
| 1252 | { |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 1253 | IBinder* target = getBPNativeData(env, obj)->mObject.get(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1254 | if (target == NULL) { |
| 1255 | return JNI_FALSE; |
| 1256 | } |
| 1257 | bool alive = target->isBinderAlive(); |
| 1258 | return alive ? JNI_TRUE : JNI_FALSE; |
| 1259 | } |
| 1260 | |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1261 | static int getprocname(pid_t pid, char *buf, size_t len) { |
Sungmin Choi | ec3d44c | 2012-12-21 14:24:33 +0900 | [diff] [blame] | 1262 | char filename[32]; |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1263 | FILE *f; |
| 1264 | |
Sungmin Choi | ec3d44c | 2012-12-21 14:24:33 +0900 | [diff] [blame] | 1265 | snprintf(filename, sizeof(filename), "/proc/%d/cmdline", pid); |
Nick Kralevich | 4b3a08c | 2019-01-28 10:39:10 -0800 | [diff] [blame] | 1266 | f = fopen(filename, "re"); |
Sungmin Choi | ec3d44c | 2012-12-21 14:24:33 +0900 | [diff] [blame] | 1267 | if (!f) { |
| 1268 | *buf = '\0'; |
| 1269 | return 1; |
| 1270 | } |
| 1271 | if (!fgets(buf, len, f)) { |
| 1272 | *buf = '\0'; |
| 1273 | fclose(f); |
| 1274 | return 2; |
| 1275 | } |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1276 | fclose(f); |
| 1277 | return 0; |
| 1278 | } |
| 1279 | |
| 1280 | static bool push_eventlog_string(char** pos, const char* end, const char* str) { |
| 1281 | jint len = strlen(str); |
| 1282 | int space_needed = 1 + sizeof(len) + len; |
| 1283 | if (end - *pos < space_needed) { |
Mark Salyzyn | 5b6da1a | 2014-04-17 17:25:36 -0700 | [diff] [blame] | 1284 | ALOGW("not enough space for string. remain=%" PRIdPTR "; needed=%d", |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 1285 | end - *pos, space_needed); |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1286 | return false; |
| 1287 | } |
| 1288 | **pos = EVENT_TYPE_STRING; |
| 1289 | (*pos)++; |
| 1290 | memcpy(*pos, &len, sizeof(len)); |
| 1291 | *pos += sizeof(len); |
| 1292 | memcpy(*pos, str, len); |
| 1293 | *pos += len; |
| 1294 | return true; |
| 1295 | } |
| 1296 | |
| 1297 | static bool push_eventlog_int(char** pos, const char* end, jint val) { |
| 1298 | int space_needed = 1 + sizeof(val); |
| 1299 | if (end - *pos < space_needed) { |
Mark Salyzyn | 5b6da1a | 2014-04-17 17:25:36 -0700 | [diff] [blame] | 1300 | ALOGW("not enough space for int. remain=%" PRIdPTR "; needed=%d", |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 1301 | end - *pos, space_needed); |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1302 | return false; |
| 1303 | } |
| 1304 | **pos = EVENT_TYPE_INT; |
| 1305 | (*pos)++; |
| 1306 | memcpy(*pos, &val, sizeof(val)); |
| 1307 | *pos += sizeof(val); |
| 1308 | return true; |
| 1309 | } |
| 1310 | |
| 1311 | // From frameworks/base/core/java/android/content/EventLogTags.logtags: |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1312 | |
| 1313 | static const bool kEnableBinderSample = false; |
| 1314 | |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1315 | #define LOGTAG_BINDER_OPERATION 52004 |
| 1316 | |
| 1317 | static void conditionally_log_binder_call(int64_t start_millis, |
| 1318 | IBinder* target, jint code) { |
| 1319 | int duration_ms = static_cast<int>(uptimeMillis() - start_millis); |
| 1320 | |
| 1321 | int sample_percent; |
| 1322 | if (duration_ms >= 500) { |
| 1323 | sample_percent = 100; |
| 1324 | } else { |
| 1325 | sample_percent = 100 * duration_ms / 500; |
| 1326 | if (sample_percent == 0) { |
| 1327 | return; |
| 1328 | } |
| 1329 | if (sample_percent < (random() % 100 + 1)) { |
| 1330 | return; |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | char process_name[40]; |
| 1335 | getprocname(getpid(), process_name, sizeof(process_name)); |
| 1336 | String8 desc(target->getInterfaceDescriptor()); |
| 1337 | |
| 1338 | char buf[LOGGER_ENTRY_MAX_PAYLOAD]; |
| 1339 | buf[0] = EVENT_TYPE_LIST; |
| 1340 | buf[1] = 5; |
| 1341 | char* pos = &buf[2]; |
| 1342 | char* end = &buf[LOGGER_ENTRY_MAX_PAYLOAD - 1]; // leave room for final \n |
| 1343 | if (!push_eventlog_string(&pos, end, desc.string())) return; |
| 1344 | if (!push_eventlog_int(&pos, end, code)) return; |
| 1345 | if (!push_eventlog_int(&pos, end, duration_ms)) return; |
| 1346 | if (!push_eventlog_string(&pos, end, process_name)) return; |
| 1347 | if (!push_eventlog_int(&pos, end, sample_percent)) return; |
| 1348 | *(pos++) = '\n'; // conventional with EVENT_TYPE_LIST apparently. |
| 1349 | android_bWriteLog(LOGTAG_BINDER_OPERATION, buf, pos - buf); |
| 1350 | } |
| 1351 | |
Brad Fitzpatrick | ad8fd28 | 2010-03-25 02:01:32 -0700 | [diff] [blame] | 1352 | // We only measure binder call durations to potentially log them if |
Elliott Hughes | 06451fe | 2014-08-18 10:26:52 -0700 | [diff] [blame] | 1353 | // we're on the main thread. |
Brad Fitzpatrick | ad8fd28 | 2010-03-25 02:01:32 -0700 | [diff] [blame] | 1354 | static bool should_time_binder_calls() { |
Elliott Hughes | 06451fe | 2014-08-18 10:26:52 -0700 | [diff] [blame] | 1355 | return (getpid() == gettid()); |
Brad Fitzpatrick | ad8fd28 | 2010-03-25 02:01:32 -0700 | [diff] [blame] | 1356 | } |
| 1357 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1358 | static jboolean android_os_BinderProxy_transact(JNIEnv* env, jobject obj, |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 1359 | 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] | 1360 | { |
| 1361 | if (dataObj == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1362 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1363 | return JNI_FALSE; |
| 1364 | } |
| 1365 | |
| 1366 | Parcel* data = parcelForJavaObject(env, dataObj); |
| 1367 | if (data == NULL) { |
| 1368 | return JNI_FALSE; |
| 1369 | } |
| 1370 | Parcel* reply = parcelForJavaObject(env, replyObj); |
| 1371 | if (reply == NULL && replyObj != NULL) { |
| 1372 | return JNI_FALSE; |
| 1373 | } |
| 1374 | |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 1375 | IBinder* target = getBPNativeData(env, obj)->mObject.get(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1376 | if (target == NULL) { |
| 1377 | jniThrowException(env, "java/lang/IllegalStateException", "Binder has been finalized!"); |
| 1378 | return JNI_FALSE; |
| 1379 | } |
| 1380 | |
Mark Salyzyn | cfd91e7 | 2014-04-17 15:40:01 -0700 | [diff] [blame] | 1381 | 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] | 1382 | target, obj, code); |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1383 | |
Brad Fitzpatrick | ad8fd28 | 2010-03-25 02:01:32 -0700 | [diff] [blame] | 1384 | |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1385 | bool time_binder_calls; |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1386 | int64_t start_millis; |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1387 | if (kEnableBinderSample) { |
| 1388 | // Only log the binder call duration for things on the Java-level main thread. |
| 1389 | // But if we don't |
| 1390 | time_binder_calls = should_time_binder_calls(); |
| 1391 | |
| 1392 | if (time_binder_calls) { |
| 1393 | start_millis = uptimeMillis(); |
| 1394 | } |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1395 | } |
Andreas Gampe | 0f0b491 | 2014-11-12 08:03:48 -0800 | [diff] [blame] | 1396 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1397 | //printf("Transact from Java code to %p sending: ", target); data->print(); |
| 1398 | status_t err = target->transact(code, *data, reply, flags); |
| 1399 | //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] | 1400 | |
| 1401 | if (kEnableBinderSample) { |
| 1402 | if (time_binder_calls) { |
| 1403 | conditionally_log_binder_call(start_millis, target, code); |
| 1404 | } |
Brad Fitzpatrick | 2c5da31 | 2010-03-24 16:14:09 -0700 | [diff] [blame] | 1405 | } |
| 1406 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1407 | if (err == NO_ERROR) { |
| 1408 | return JNI_TRUE; |
| 1409 | } else if (err == UNKNOWN_TRANSACTION) { |
| 1410 | return JNI_FALSE; |
| 1411 | } |
| 1412 | |
Dianne Hackborn | e5c4262 | 2015-05-19 16:04:04 -0700 | [diff] [blame] | 1413 | signalExceptionForError(env, obj, err, true /*canThrowRemoteException*/, data->dataSize()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1414 | return JNI_FALSE; |
| 1415 | } |
| 1416 | |
| 1417 | static void android_os_BinderProxy_linkToDeath(JNIEnv* env, jobject obj, |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 1418 | jobject recipient, jint flags) // throws RemoteException |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1419 | { |
| 1420 | if (recipient == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1421 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1422 | return; |
| 1423 | } |
| 1424 | |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 1425 | BinderProxyNativeData *nd = getBPNativeData(env, obj); |
| 1426 | IBinder* target = nd->mObject.get(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1427 | |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 1428 | LOGDEATH("linkToDeath: binder=%p recipient=%p\n", target, recipient); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1429 | |
| 1430 | if (!target->localBinder()) { |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 1431 | DeathRecipientList* list = nd->mOrgue.get(); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 1432 | sp<JavaDeathRecipient> jdr = new JavaDeathRecipient(env, recipient, list); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 1433 | status_t err = target->linkToDeath(jdr, NULL, flags); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1434 | if (err != NO_ERROR) { |
| 1435 | // Failure adding the death recipient, so clear its reference |
| 1436 | // now. |
| 1437 | jdr->clearReference(); |
Jeff Brown | 0bde66a | 2011-11-07 12:50:08 -0800 | [diff] [blame] | 1438 | signalExceptionForError(env, obj, err, true /*canThrowRemoteException*/); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1439 | } |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | static jboolean android_os_BinderProxy_unlinkToDeath(JNIEnv* env, jobject obj, |
| 1444 | jobject recipient, jint flags) |
| 1445 | { |
| 1446 | jboolean res = JNI_FALSE; |
| 1447 | if (recipient == NULL) { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 1448 | jniThrowNullPointerException(env, NULL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1449 | return res; |
| 1450 | } |
| 1451 | |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 1452 | BinderProxyNativeData* nd = getBPNativeData(env, obj); |
| 1453 | IBinder* target = nd->mObject.get(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1454 | if (target == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1455 | 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] | 1456 | return JNI_FALSE; |
| 1457 | } |
| 1458 | |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 1459 | LOGDEATH("unlinkToDeath: binder=%p recipient=%p\n", target, recipient); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1460 | |
| 1461 | if (!target->localBinder()) { |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 1462 | status_t err = NAME_NOT_FOUND; |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 1463 | |
| 1464 | // If we find the matching recipient, proceed to unlink using that |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 1465 | DeathRecipientList* list = nd->mOrgue.get(); |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 1466 | sp<JavaDeathRecipient> origJDR = list->find(recipient); |
Christopher Tate | 79dd31f | 2011-03-04 17:45:00 -0800 | [diff] [blame] | 1467 | LOGDEATH(" unlink found list %p and JDR %p", list, origJDR.get()); |
Christopher Tate | 0b41448 | 2011-02-17 13:00:38 -0800 | [diff] [blame] | 1468 | if (origJDR != NULL) { |
| 1469 | wp<IBinder::DeathRecipient> dr; |
| 1470 | err = target->unlinkToDeath(origJDR, NULL, flags, &dr); |
| 1471 | if (err == NO_ERROR && dr != NULL) { |
| 1472 | sp<IBinder::DeathRecipient> sdr = dr.promote(); |
| 1473 | JavaDeathRecipient* jdr = static_cast<JavaDeathRecipient*>(sdr.get()); |
| 1474 | if (jdr != NULL) { |
| 1475 | jdr->clearReference(); |
| 1476 | } |
| 1477 | } |
| 1478 | } |
| 1479 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1480 | if (err == NO_ERROR || err == DEAD_OBJECT) { |
| 1481 | res = JNI_TRUE; |
| 1482 | } else { |
| 1483 | jniThrowException(env, "java/util/NoSuchElementException", |
| 1484 | "Death link does not exist"); |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | return res; |
| 1489 | } |
| 1490 | |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 1491 | static void BinderProxy_destroy(void* rawNativeData) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1492 | { |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 1493 | BinderProxyNativeData * nativeData = (BinderProxyNativeData *) rawNativeData; |
| 1494 | LOGDEATH("Destroying BinderProxy: binder=%p drl=%p\n", |
| 1495 | nativeData->mObject.get(), nativeData->mOrgue.get()); |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 1496 | delete nativeData; |
Christopher Tate | bd8b6f2 | 2011-03-01 11:55:27 -0800 | [diff] [blame] | 1497 | IPCThreadState::self()->flushCommands(); |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 1498 | --gNumProxies; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1499 | } |
| 1500 | |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 1501 | JNIEXPORT jlong JNICALL android_os_BinderProxy_getNativeFinalizer(JNIEnv*, jclass) { |
| 1502 | return (jlong) BinderProxy_destroy; |
| 1503 | } |
| 1504 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1505 | // ---------------------------------------------------------------------------- |
| 1506 | |
| 1507 | static const JNINativeMethod gBinderProxyMethods[] = { |
| 1508 | /* name, signature, funcPtr */ |
| 1509 | {"pingBinder", "()Z", (void*)android_os_BinderProxy_pingBinder}, |
| 1510 | {"isBinderAlive", "()Z", (void*)android_os_BinderProxy_isBinderAlive}, |
| 1511 | {"getInterfaceDescriptor", "()Ljava/lang/String;", (void*)android_os_BinderProxy_getInterfaceDescriptor}, |
Dianne Hackborn | 017c6a2 | 2014-09-25 17:41:34 -0700 | [diff] [blame] | 1512 | {"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] | 1513 | {"linkToDeath", "(Landroid/os/IBinder$DeathRecipient;I)V", (void*)android_os_BinderProxy_linkToDeath}, |
| 1514 | {"unlinkToDeath", "(Landroid/os/IBinder$DeathRecipient;I)Z", (void*)android_os_BinderProxy_unlinkToDeath}, |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 1515 | {"getNativeFinalizer", "()J", (void*)android_os_BinderProxy_getNativeFinalizer}, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1516 | }; |
| 1517 | |
| 1518 | const char* const kBinderProxyPathName = "android/os/BinderProxy"; |
| 1519 | |
| 1520 | static int int_register_android_os_BinderProxy(JNIEnv* env) |
| 1521 | { |
Andreas Gampe | 9ff9c40 | 2019-07-08 08:31:27 -0700 | [diff] [blame] | 1522 | gErrorOffsets.mError = MakeGlobalRefOrDie(env, FindClassOrDie(env, "java/lang/Error")); |
| 1523 | gErrorOffsets.mOutOfMemory = |
| 1524 | MakeGlobalRefOrDie(env, FindClassOrDie(env, "java/lang/OutOfMemoryError")); |
| 1525 | gErrorOffsets.mStackOverflow = |
| 1526 | MakeGlobalRefOrDie(env, FindClassOrDie(env, "java/lang/StackOverflowError")); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1527 | |
Andreas Gampe | 9ff9c40 | 2019-07-08 08:31:27 -0700 | [diff] [blame] | 1528 | jclass clazz = FindClassOrDie(env, kBinderProxyPathName); |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1529 | gBinderProxyOffsets.mClass = MakeGlobalRefOrDie(env, clazz); |
Hans Boehm | 29f388f | 2017-10-03 18:01:20 -0700 | [diff] [blame] | 1530 | gBinderProxyOffsets.mGetInstance = GetStaticMethodIDOrDie(env, clazz, "getInstance", |
| 1531 | "(JJ)Landroid/os/BinderProxy;"); |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1532 | gBinderProxyOffsets.mSendDeathNotice = GetStaticMethodIDOrDie(env, clazz, "sendDeathNotice", |
| 1533 | "(Landroid/os/IBinder$DeathRecipient;)V"); |
Hans Boehm | 5e5b13f | 2017-09-28 18:16:50 -0700 | [diff] [blame] | 1534 | gBinderProxyOffsets.mNativeData = GetFieldIDOrDie(env, clazz, "mNativeData", "J"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1535 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1536 | clazz = FindClassOrDie(env, "java/lang/Class"); |
| 1537 | gClassOffsets.mGetName = GetMethodIDOrDie(env, clazz, "getName", "()Ljava/lang/String;"); |
Christopher Tate | 0d4a792 | 2011-08-30 12:09:43 -0700 | [diff] [blame] | 1538 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 1539 | return RegisterMethodsOrDie( |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1540 | env, kBinderProxyPathName, |
| 1541 | gBinderProxyMethods, NELEM(gBinderProxyMethods)); |
| 1542 | } |
| 1543 | |
| 1544 | // **************************************************************************** |
| 1545 | // **************************************************************************** |
| 1546 | // **************************************************************************** |
| 1547 | |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 1548 | int register_android_os_Binder(JNIEnv* env) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1549 | { |
Jeff Sharkey | d84e1ce | 2012-03-06 18:26:19 -0800 | [diff] [blame] | 1550 | if (int_register_android_os_Binder(env) < 0) |
| 1551 | return -1; |
| 1552 | if (int_register_android_os_BinderInternal(env) < 0) |
| 1553 | return -1; |
| 1554 | if (int_register_android_os_BinderProxy(env) < 0) |
| 1555 | return -1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1556 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1557 | jclass clazz = FindClassOrDie(env, "android/util/Log"); |
| 1558 | gLogOffsets.mClass = MakeGlobalRefOrDie(env, clazz); |
| 1559 | gLogOffsets.mLogE = GetStaticMethodIDOrDie(env, clazz, "e", |
| 1560 | "(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] | 1561 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1562 | clazz = FindClassOrDie(env, "android/os/ParcelFileDescriptor"); |
| 1563 | gParcelFileDescriptorOffsets.mClass = MakeGlobalRefOrDie(env, clazz); |
| 1564 | gParcelFileDescriptorOffsets.mConstructor = GetMethodIDOrDie(env, clazz, "<init>", |
| 1565 | "(Ljava/io/FileDescriptor;)V"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1566 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 1567 | clazz = FindClassOrDie(env, "android/os/StrictMode"); |
| 1568 | gStrictModeCallbackOffsets.mClass = MakeGlobalRefOrDie(env, clazz); |
| 1569 | gStrictModeCallbackOffsets.mCallback = GetStaticMethodIDOrDie(env, clazz, |
| 1570 | "onBinderStrictModePolicyChange", "(I)V"); |
Brad Fitzpatrick | 727de40 | 2010-07-07 16:06:39 -0700 | [diff] [blame] | 1571 | |
Andreas Gampe | 1cd76f5 | 2017-09-08 17:44:05 -0700 | [diff] [blame] | 1572 | clazz = FindClassOrDie(env, "java/lang/Thread"); |
| 1573 | gThreadDispatchOffsets.mClass = MakeGlobalRefOrDie(env, clazz); |
| 1574 | gThreadDispatchOffsets.mDispatchUncaughtException = GetMethodIDOrDie(env, clazz, |
| 1575 | "dispatchUncaughtException", "(Ljava/lang/Throwable;)V"); |
| 1576 | gThreadDispatchOffsets.mCurrentThread = GetStaticMethodIDOrDie(env, clazz, "currentThread", |
| 1577 | "()Ljava/lang/Thread;"); |
| 1578 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1579 | return 0; |
| 1580 | } |