Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [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 | |
Elliott Hughes | 2fc9d0a | 2014-08-18 19:50:01 -0700 | [diff] [blame] | 17 | #if defined(__ANDROID__) |
| 18 | /* libnativehelper is built by NDK 19 in one variant, which doesn't yet have the GNU strerror_r. */ |
| 19 | #undef _GNU_SOURCE |
Elliott Hughes | 1ed11b4 | 2016-06-11 16:58:34 -0700 | [diff] [blame] | 20 | /* ...but this code uses asprintf, which is a BSD/GNU extension. */ |
| 21 | #define _BSD_SOURCE |
Elliott Hughes | 2fc9d0a | 2014-08-18 19:50:01 -0700 | [diff] [blame] | 22 | #endif |
| 23 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 24 | #define LOG_TAG "JNIHelp" |
| 25 | |
Steven Moreland | 3544a93 | 2017-07-19 10:26:05 -0700 | [diff] [blame] | 26 | #include <nativehelper/JniConstants.h> |
| 27 | #include <nativehelper/JNIHelp.h> |
Ruben Brunk | a77f3a2 | 2013-09-09 02:21:31 -0700 | [diff] [blame] | 28 | #include "ALog-priv.h" |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 29 | |
Ruben Brunk | a77f3a2 | 2013-09-09 02:21:31 -0700 | [diff] [blame] | 30 | #include <stdio.h> |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 31 | #include <stdlib.h> |
| 32 | #include <string.h> |
| 33 | #include <assert.h> |
| 34 | |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 35 | #include <string> |
| 36 | |
Logan Chien | 63e4917 | 2016-06-08 11:33:36 +0800 | [diff] [blame] | 37 | namespace { |
| 38 | |
| 39 | // java.io.FileDescriptor.descriptor. |
| 40 | jfieldID fileDescriptorDescriptorField = nullptr; |
| 41 | |
| 42 | // void java.io.FileDescriptor.<init>(). |
| 43 | jmethodID fileDescriptorInitMethod = nullptr; |
| 44 | // Object java.lang.ref.Reference.get() |
| 45 | jmethodID referenceGetMethod = nullptr; |
| 46 | |
| 47 | jfieldID FindField(JNIEnv* env, jclass klass, const char* name, const char* desc) { |
| 48 | jfieldID result = env->GetFieldID(klass, name, desc); |
| 49 | if (result == NULL) { |
| 50 | ALOGV("failed to find field '%s:%s'", name, desc); |
| 51 | abort(); |
| 52 | } |
| 53 | return result; |
| 54 | } |
| 55 | |
| 56 | jmethodID FindMethod(JNIEnv* env, jclass klass, const char* name, const char* signature) { |
| 57 | jmethodID result = env->GetMethodID(klass, name, signature); |
| 58 | if (result == NULL) { |
| 59 | ALOGV("failed to find method '%s%s'", name, signature); |
| 60 | abort(); |
| 61 | } |
| 62 | return result; |
| 63 | } |
| 64 | |
| 65 | void InitFieldsAndMethods(JNIEnv* env) { |
| 66 | JniConstants::init(env); // Ensure that classes are cached. |
| 67 | fileDescriptorDescriptorField = FindField(env, JniConstants::fileDescriptorClass, "descriptor", |
| 68 | "I"); |
| 69 | fileDescriptorInitMethod = FindMethod(env, JniConstants::fileDescriptorClass, "<init>", "()V"); |
| 70 | referenceGetMethod = FindMethod(env, JniConstants::referenceClass, "get", |
| 71 | "()Ljava/lang/Object;"); |
| 72 | } |
| 73 | |
| 74 | } |
| 75 | |
| 76 | namespace android { |
| 77 | |
| 78 | void ClearJNIHelpLocalCache() { |
| 79 | fileDescriptorDescriptorField = nullptr; |
| 80 | fileDescriptorInitMethod = nullptr; |
| 81 | referenceGetMethod = nullptr; |
| 82 | } |
| 83 | |
| 84 | } |
| 85 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 86 | /** |
| 87 | * Equivalent to ScopedLocalRef, but for C_JNIEnv instead. (And slightly more powerful.) |
| 88 | */ |
| 89 | template<typename T> |
| 90 | class scoped_local_ref { |
| 91 | public: |
Chih-Hung Hsieh | 0133214 | 2016-05-02 11:47:43 -0700 | [diff] [blame] | 92 | explicit scoped_local_ref(C_JNIEnv* env, T localRef = NULL) |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 93 | : mEnv(env), mLocalRef(localRef) |
| 94 | { |
| 95 | } |
| 96 | |
| 97 | ~scoped_local_ref() { |
| 98 | reset(); |
| 99 | } |
| 100 | |
| 101 | void reset(T localRef = NULL) { |
| 102 | if (mLocalRef != NULL) { |
| 103 | (*mEnv)->DeleteLocalRef(reinterpret_cast<JNIEnv*>(mEnv), mLocalRef); |
| 104 | mLocalRef = localRef; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | T get() const { |
| 109 | return mLocalRef; |
| 110 | } |
| 111 | |
| 112 | private: |
Ian Rogers | 8288dde | 2014-11-04 11:42:02 -0800 | [diff] [blame] | 113 | C_JNIEnv* const mEnv; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 114 | T mLocalRef; |
| 115 | |
Ian Rogers | 8288dde | 2014-11-04 11:42:02 -0800 | [diff] [blame] | 116 | DISALLOW_COPY_AND_ASSIGN(scoped_local_ref); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | static jclass findClass(C_JNIEnv* env, const char* className) { |
| 120 | JNIEnv* e = reinterpret_cast<JNIEnv*>(env); |
| 121 | return (*env)->FindClass(e, className); |
| 122 | } |
| 123 | |
| 124 | extern "C" int jniRegisterNativeMethods(C_JNIEnv* env, const char* className, |
| 125 | const JNINativeMethod* gMethods, int numMethods) |
| 126 | { |
| 127 | JNIEnv* e = reinterpret_cast<JNIEnv*>(env); |
| 128 | |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 129 | ALOGV("Registering %s's %d native methods...", className, numMethods); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 130 | |
| 131 | scoped_local_ref<jclass> c(env, findClass(env, className)); |
| 132 | if (c.get() == NULL) { |
Andreas Gampe | 6c5be19 | 2015-08-03 10:31:52 -0700 | [diff] [blame] | 133 | char* tmp; |
| 134 | const char* msg; |
| 135 | if (asprintf(&tmp, |
| 136 | "Native registration unable to find class '%s'; aborting...", |
| 137 | className) == -1) { |
| 138 | // Allocation failed, print default warning. |
| 139 | msg = "Native registration unable to find class; aborting..."; |
| 140 | } else { |
| 141 | msg = tmp; |
| 142 | } |
Elliott Hughes | a3b5700 | 2013-01-22 09:35:09 -0800 | [diff] [blame] | 143 | e->FatalError(msg); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | if ((*env)->RegisterNatives(e, c.get(), gMethods, numMethods) < 0) { |
Andreas Gampe | 6c5be19 | 2015-08-03 10:31:52 -0700 | [diff] [blame] | 147 | char* tmp; |
| 148 | const char* msg; |
| 149 | if (asprintf(&tmp, "RegisterNatives failed for '%s'; aborting...", className) == -1) { |
| 150 | // Allocation failed, print default warning. |
| 151 | msg = "RegisterNatives failed; aborting..."; |
| 152 | } else { |
| 153 | msg = tmp; |
| 154 | } |
Elliott Hughes | a3b5700 | 2013-01-22 09:35:09 -0800 | [diff] [blame] | 155 | e->FatalError(msg); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * Returns a human-readable summary of an exception object. The buffer will |
| 163 | * be populated with the "binary" class name and, if present, the |
| 164 | * exception message. |
| 165 | */ |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 166 | static bool getExceptionSummary(C_JNIEnv* env, jthrowable exception, std::string& result) { |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 167 | JNIEnv* e = reinterpret_cast<JNIEnv*>(env); |
| 168 | |
| 169 | /* get the name of the exception's class */ |
| 170 | scoped_local_ref<jclass> exceptionClass(env, (*env)->GetObjectClass(e, exception)); // can't fail |
| 171 | scoped_local_ref<jclass> classClass(env, |
| 172 | (*env)->GetObjectClass(e, exceptionClass.get())); // java.lang.Class, can't fail |
| 173 | jmethodID classGetNameMethod = |
| 174 | (*env)->GetMethodID(e, classClass.get(), "getName", "()Ljava/lang/String;"); |
| 175 | scoped_local_ref<jstring> classNameStr(env, |
| 176 | (jstring) (*env)->CallObjectMethod(e, exceptionClass.get(), classGetNameMethod)); |
| 177 | if (classNameStr.get() == NULL) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 178 | (*env)->ExceptionClear(e); |
| 179 | result = "<error getting class name>"; |
| 180 | return false; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 181 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 182 | const char* classNameChars = (*env)->GetStringUTFChars(e, classNameStr.get(), NULL); |
| 183 | if (classNameChars == NULL) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 184 | (*env)->ExceptionClear(e); |
| 185 | result = "<error getting class name UTF-8>"; |
| 186 | return false; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 187 | } |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 188 | result += classNameChars; |
| 189 | (*env)->ReleaseStringUTFChars(e, classNameStr.get(), classNameChars); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 190 | |
| 191 | /* if the exception has a detail message, get that */ |
| 192 | jmethodID getMessage = |
| 193 | (*env)->GetMethodID(e, exceptionClass.get(), "getMessage", "()Ljava/lang/String;"); |
| 194 | scoped_local_ref<jstring> messageStr(env, |
| 195 | (jstring) (*env)->CallObjectMethod(e, exception, getMessage)); |
| 196 | if (messageStr.get() == NULL) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 197 | return true; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 200 | result += ": "; |
| 201 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 202 | const char* messageChars = (*env)->GetStringUTFChars(e, messageStr.get(), NULL); |
| 203 | if (messageChars != NULL) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 204 | result += messageChars; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 205 | (*env)->ReleaseStringUTFChars(e, messageStr.get(), messageChars); |
| 206 | } else { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 207 | result += "<error getting message>"; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 208 | (*env)->ExceptionClear(e); // clear OOM |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 211 | return true; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* |
| 215 | * Returns an exception (with stack trace) as a string. |
| 216 | */ |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 217 | static bool getStackTrace(C_JNIEnv* env, jthrowable exception, std::string& result) { |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 218 | JNIEnv* e = reinterpret_cast<JNIEnv*>(env); |
| 219 | |
| 220 | scoped_local_ref<jclass> stringWriterClass(env, findClass(env, "java/io/StringWriter")); |
| 221 | if (stringWriterClass.get() == NULL) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 222 | return false; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | jmethodID stringWriterCtor = (*env)->GetMethodID(e, stringWriterClass.get(), "<init>", "()V"); |
| 226 | jmethodID stringWriterToStringMethod = |
| 227 | (*env)->GetMethodID(e, stringWriterClass.get(), "toString", "()Ljava/lang/String;"); |
| 228 | |
| 229 | scoped_local_ref<jclass> printWriterClass(env, findClass(env, "java/io/PrintWriter")); |
| 230 | if (printWriterClass.get() == NULL) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 231 | return false; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | jmethodID printWriterCtor = |
| 235 | (*env)->GetMethodID(e, printWriterClass.get(), "<init>", "(Ljava/io/Writer;)V"); |
| 236 | |
| 237 | scoped_local_ref<jobject> stringWriter(env, |
| 238 | (*env)->NewObject(e, stringWriterClass.get(), stringWriterCtor)); |
| 239 | if (stringWriter.get() == NULL) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 240 | return false; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Huanqi Chen | d47c325 | 2014-06-24 13:31:31 +0200 | [diff] [blame] | 243 | scoped_local_ref<jobject> printWriter(env, |
| 244 | (*env)->NewObject(e, printWriterClass.get(), printWriterCtor, stringWriter.get())); |
| 245 | if (printWriter.get() == NULL) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 246 | return false; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | scoped_local_ref<jclass> exceptionClass(env, (*env)->GetObjectClass(e, exception)); // can't fail |
| 250 | jmethodID printStackTraceMethod = |
| 251 | (*env)->GetMethodID(e, exceptionClass.get(), "printStackTrace", "(Ljava/io/PrintWriter;)V"); |
Huanqi Chen | d47c325 | 2014-06-24 13:31:31 +0200 | [diff] [blame] | 252 | (*env)->CallVoidMethod(e, exception, printStackTraceMethod, printWriter.get()); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 253 | |
| 254 | if ((*env)->ExceptionCheck(e)) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 255 | return false; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | scoped_local_ref<jstring> messageStr(env, |
| 259 | (jstring) (*env)->CallObjectMethod(e, stringWriter.get(), stringWriterToStringMethod)); |
| 260 | if (messageStr.get() == NULL) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 261 | return false; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | const char* utfChars = (*env)->GetStringUTFChars(e, messageStr.get(), NULL); |
| 265 | if (utfChars == NULL) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 266 | return false; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 269 | result = utfChars; |
| 270 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 271 | (*env)->ReleaseStringUTFChars(e, messageStr.get(), utfChars); |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 272 | return true; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | extern "C" int jniThrowException(C_JNIEnv* env, const char* className, const char* msg) { |
| 276 | JNIEnv* e = reinterpret_cast<JNIEnv*>(env); |
| 277 | |
| 278 | if ((*env)->ExceptionCheck(e)) { |
| 279 | /* TODO: consider creating the new exception with this as "cause" */ |
| 280 | scoped_local_ref<jthrowable> exception(env, (*env)->ExceptionOccurred(e)); |
| 281 | (*env)->ExceptionClear(e); |
| 282 | |
| 283 | if (exception.get() != NULL) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 284 | std::string text; |
| 285 | getExceptionSummary(env, exception.get(), text); |
| 286 | ALOGW("Discarding pending exception (%s) to throw %s", text.c_str(), className); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
| 290 | scoped_local_ref<jclass> exceptionClass(env, findClass(env, className)); |
| 291 | if (exceptionClass.get() == NULL) { |
Steve Block | 79a0d9c | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 292 | ALOGE("Unable to find exception class %s", className); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 293 | /* ClassNotFoundException now pending */ |
| 294 | return -1; |
| 295 | } |
| 296 | |
| 297 | if ((*env)->ThrowNew(e, exceptionClass.get(), msg) != JNI_OK) { |
Steve Block | 79a0d9c | 2012-01-06 19:16:58 +0000 | [diff] [blame] | 298 | ALOGE("Failed throwing '%s' '%s'", className, msg); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 299 | /* an exception, most likely OOM, will now be pending */ |
| 300 | return -1; |
| 301 | } |
| 302 | |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | int jniThrowExceptionFmt(C_JNIEnv* env, const char* className, const char* fmt, va_list args) { |
| 307 | char msgBuf[512]; |
| 308 | vsnprintf(msgBuf, sizeof(msgBuf), fmt, args); |
| 309 | return jniThrowException(env, className, msgBuf); |
| 310 | } |
| 311 | |
| 312 | int jniThrowNullPointerException(C_JNIEnv* env, const char* msg) { |
| 313 | return jniThrowException(env, "java/lang/NullPointerException", msg); |
| 314 | } |
| 315 | |
| 316 | int jniThrowRuntimeException(C_JNIEnv* env, const char* msg) { |
| 317 | return jniThrowException(env, "java/lang/RuntimeException", msg); |
| 318 | } |
| 319 | |
| 320 | int jniThrowIOException(C_JNIEnv* env, int errnum) { |
| 321 | char buffer[80]; |
| 322 | const char* message = jniStrError(errnum, buffer, sizeof(buffer)); |
| 323 | return jniThrowException(env, "java/io/IOException", message); |
| 324 | } |
| 325 | |
Elliott Hughes | a4e4e74 | 2013-09-03 17:47:18 -0700 | [diff] [blame] | 326 | static std::string jniGetStackTrace(C_JNIEnv* env, jthrowable exception) { |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 327 | JNIEnv* e = reinterpret_cast<JNIEnv*>(env); |
| 328 | |
Jeff Brown | e2b11e7 | 2012-04-10 20:34:39 -0700 | [diff] [blame] | 329 | scoped_local_ref<jthrowable> currentException(env, (*env)->ExceptionOccurred(e)); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 330 | if (exception == NULL) { |
Jeff Brown | e2b11e7 | 2012-04-10 20:34:39 -0700 | [diff] [blame] | 331 | exception = currentException.get(); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 332 | if (exception == NULL) { |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 333 | return "<no pending exception>"; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 334 | } |
Jeff Brown | e2b11e7 | 2012-04-10 20:34:39 -0700 | [diff] [blame] | 335 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 336 | |
Jeff Brown | e2b11e7 | 2012-04-10 20:34:39 -0700 | [diff] [blame] | 337 | if (currentException.get() != NULL) { |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 338 | (*env)->ExceptionClear(e); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 339 | } |
| 340 | |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 341 | std::string trace; |
| 342 | if (!getStackTrace(env, exception, trace)) { |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 343 | (*env)->ExceptionClear(e); |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 344 | getExceptionSummary(env, exception, trace); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 347 | if (currentException.get() != NULL) { |
Jeff Brown | e2b11e7 | 2012-04-10 20:34:39 -0700 | [diff] [blame] | 348 | (*env)->Throw(e, currentException.get()); // rethrow |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 349 | } |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 350 | |
| 351 | return trace; |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 352 | } |
| 353 | |
Elliott Hughes | a4e4e74 | 2013-09-03 17:47:18 -0700 | [diff] [blame] | 354 | void jniLogException(C_JNIEnv* env, int priority, const char* tag, jthrowable exception) { |
| 355 | std::string trace(jniGetStackTrace(env, exception)); |
| 356 | __android_log_write(priority, tag, trace.c_str()); |
| 357 | } |
| 358 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 359 | const char* jniStrError(int errnum, char* buf, size_t buflen) { |
Elliott Hughes | d60512c | 2013-10-08 14:15:18 -0700 | [diff] [blame] | 360 | #if __GLIBC__ |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 361 | // Note: glibc has a nonstandard strerror_r that returns char* rather than POSIX's int. |
| 362 | // char *strerror_r(int errnum, char *buf, size_t n); |
Elliott Hughes | d60512c | 2013-10-08 14:15:18 -0700 | [diff] [blame] | 363 | return strerror_r(errnum, buf, buflen); |
| 364 | #else |
| 365 | int rc = strerror_r(errnum, buf, buflen); |
| 366 | if (rc != 0) { |
| 367 | // (POSIX only guarantees a value other than 0. The safest |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 368 | // way to implement this function is to use C++ and overload on the |
Elliott Hughes | d60512c | 2013-10-08 14:15:18 -0700 | [diff] [blame] | 369 | // type of strerror_r to accurately distinguish GNU from POSIX.) |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 370 | snprintf(buf, buflen, "errno %d", errnum); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 371 | } |
Elliott Hughes | d60512c | 2013-10-08 14:15:18 -0700 | [diff] [blame] | 372 | return buf; |
| 373 | #endif |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 376 | jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd) { |
| 377 | JNIEnv* e = reinterpret_cast<JNIEnv*>(env); |
Logan Chien | 63e4917 | 2016-06-08 11:33:36 +0800 | [diff] [blame] | 378 | if (fileDescriptorInitMethod == nullptr) { |
| 379 | InitFieldsAndMethods(e); |
| 380 | } |
| 381 | jobject fileDescriptor = (*env)->NewObject(e, JniConstants::fileDescriptorClass, |
| 382 | fileDescriptorInitMethod); |
Narayan Kamath | e29dbf5 | 2013-10-31 17:17:10 +0000 | [diff] [blame] | 383 | // NOTE: NewObject ensures that an OutOfMemoryError will be seen by the Java |
| 384 | // caller if the alloc fails, so we just return NULL when that happens. |
| 385 | if (fileDescriptor != NULL) { |
| 386 | jniSetFileDescriptorOfFD(env, fileDescriptor, fd); |
| 387 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 388 | return fileDescriptor; |
| 389 | } |
| 390 | |
| 391 | int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor) { |
| 392 | JNIEnv* e = reinterpret_cast<JNIEnv*>(env); |
Logan Chien | 63e4917 | 2016-06-08 11:33:36 +0800 | [diff] [blame] | 393 | if (fileDescriptorDescriptorField == nullptr) { |
| 394 | InitFieldsAndMethods(e); |
| 395 | } |
Narayan Kamath | e29dbf5 | 2013-10-31 17:17:10 +0000 | [diff] [blame] | 396 | if (fileDescriptor != NULL) { |
Logan Chien | 63e4917 | 2016-06-08 11:33:36 +0800 | [diff] [blame] | 397 | return (*env)->GetIntField(e, fileDescriptor, |
| 398 | fileDescriptorDescriptorField); |
Narayan Kamath | e29dbf5 | 2013-10-31 17:17:10 +0000 | [diff] [blame] | 399 | } else { |
| 400 | return -1; |
| 401 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | void jniSetFileDescriptorOfFD(C_JNIEnv* env, jobject fileDescriptor, int value) { |
| 405 | JNIEnv* e = reinterpret_cast<JNIEnv*>(env); |
Logan Chien | 63e4917 | 2016-06-08 11:33:36 +0800 | [diff] [blame] | 406 | if (fileDescriptorDescriptorField == nullptr) { |
| 407 | InitFieldsAndMethods(e); |
| 408 | } |
| 409 | (*env)->SetIntField(e, fileDescriptor, fileDescriptorDescriptorField, value); |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 410 | } |
| 411 | |
Jeff Brown | dc176c5 | 2013-04-02 18:09:29 -0700 | [diff] [blame] | 412 | jobject jniGetReferent(C_JNIEnv* env, jobject ref) { |
| 413 | JNIEnv* e = reinterpret_cast<JNIEnv*>(env); |
Logan Chien | 63e4917 | 2016-06-08 11:33:36 +0800 | [diff] [blame] | 414 | if (referenceGetMethod == nullptr) { |
| 415 | InitFieldsAndMethods(e); |
| 416 | } |
| 417 | return (*env)->CallObjectMethod(e, ref, referenceGetMethod); |
Jeff Brown | dc176c5 | 2013-04-02 18:09:29 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Fredrik Roubert | 2e31280 | 2017-07-04 21:53:08 +0200 | [diff] [blame] | 420 | jstring jniCreateString(C_JNIEnv* env, const jchar* unicodeChars, jsize len) { |
| 421 | JNIEnv* e = reinterpret_cast<JNIEnv*>(env); |
| 422 | return (*env)->NewString(e, unicodeChars, len); |
| 423 | } |