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