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