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