The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | /* |
| 18 | * JNI helper functions. |
| 19 | * |
| 20 | * This file may be included by C or C++ code, which is trouble because jni.h |
| 21 | * uses different typedefs for JNIEnv in each language. |
| 22 | */ |
Carl Shapiro | 1ff7a0d | 2011-06-14 20:31:24 -0700 | [diff] [blame] | 23 | #ifndef NATIVEHELPER_JNIHELP_H_ |
| 24 | #define NATIVEHELPER_JNIHELP_H_ |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 25 | |
Dan Albert | 099d521 | 2014-05-21 13:25:01 -0700 | [diff] [blame] | 26 | #include <errno.h> |
Dan Bornstein | 6edaa47 | 2009-10-26 13:33:22 -0700 | [diff] [blame] | 27 | #include <unistd.h> |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 28 | |
Orion Hodson | b01e7fe | 2018-11-07 06:07:50 +0000 | [diff] [blame^] | 29 | #include <jni.h> |
| 30 | #include "module_api.h" |
| 31 | |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 32 | #ifndef NELEM |
| 33 | # define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) |
| 34 | #endif |
| 35 | |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 36 | /* |
| 37 | * Register one or more native methods with a particular class. |
Elliott Hughes | 73d3c2e | 2012-05-03 17:06:04 -0700 | [diff] [blame] | 38 | * "className" looks like "java/lang/String". Aborts on failure. |
| 39 | * TODO: fix all callers and change the return type to void. |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 40 | */ |
Orion Hodson | b01e7fe | 2018-11-07 06:07:50 +0000 | [diff] [blame^] | 41 | MODULE_API int jniRegisterNativeMethods(C_JNIEnv* env, |
| 42 | const char* className, |
| 43 | const JNINativeMethod* gMethods, |
| 44 | int numMethods); |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 45 | |
| 46 | /* |
| 47 | * Throw an exception with the specified class and an optional message. |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 48 | * |
Andy McFadden | 0b9d26c | 2009-06-04 14:34:14 -0700 | [diff] [blame] | 49 | * The "className" argument will be passed directly to FindClass, which |
| 50 | * takes strings with slashes (e.g. "java/lang/Object"). |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 51 | * |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 52 | * If an exception is currently pending, we log a warning message and |
| 53 | * clear it. |
| 54 | * |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 55 | * Returns 0 on success, nonzero if something failed (e.g. the exception |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 56 | * class couldn't be found, so *an* exception will still be pending). |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 57 | * |
| 58 | * Currently aborts the VM if it can't throw the exception. |
| 59 | */ |
Orion Hodson | b01e7fe | 2018-11-07 06:07:50 +0000 | [diff] [blame^] | 60 | MODULE_API int jniThrowException(C_JNIEnv* env, const char* className, const char* msg); |
| 61 | |
| 62 | /* |
| 63 | * Throw an exception with the specified class and formatted error message. |
| 64 | * |
| 65 | * The "className" argument will be passed directly to FindClass, which |
| 66 | * takes strings with slashes (e.g. "java/lang/Object"). |
| 67 | * |
| 68 | * If an exception is currently pending, we log a warning message and |
| 69 | * clear it. |
| 70 | * |
| 71 | * Returns 0 on success, nonzero if something failed (e.g. the exception |
| 72 | * class couldn't be found, so *an* exception will still be pending). |
| 73 | * |
| 74 | * Currently aborts the VM if it can't throw the exception. |
| 75 | */ |
| 76 | MODULE_API int jniThrowExceptionFmt(C_JNIEnv* env, const char* className, const char* fmt, va_list args); |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 77 | |
| 78 | /* |
Elliott Hughes | 9f6e1d3 | 2010-01-28 13:43:39 -0800 | [diff] [blame] | 79 | * Throw a java.lang.NullPointerException, with an optional message. |
| 80 | */ |
Orion Hodson | b01e7fe | 2018-11-07 06:07:50 +0000 | [diff] [blame^] | 81 | MODULE_API int jniThrowNullPointerException(C_JNIEnv* env, const char* msg); |
Elliott Hughes | 9f6e1d3 | 2010-01-28 13:43:39 -0800 | [diff] [blame] | 82 | |
| 83 | /* |
Dan Bornstein | f84f4c6 | 2009-12-07 15:46:23 -0800 | [diff] [blame] | 84 | * Throw a java.lang.RuntimeException, with an optional message. |
| 85 | */ |
Orion Hodson | b01e7fe | 2018-11-07 06:07:50 +0000 | [diff] [blame^] | 86 | MODULE_API int jniThrowRuntimeException(C_JNIEnv* env, const char* msg); |
Dan Bornstein | f84f4c6 | 2009-12-07 15:46:23 -0800 | [diff] [blame] | 87 | |
| 88 | /* |
| 89 | * Throw a java.io.IOException, generating the message from errno. |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 90 | */ |
Orion Hodson | b01e7fe | 2018-11-07 06:07:50 +0000 | [diff] [blame^] | 91 | MODULE_API int jniThrowIOException(C_JNIEnv* env, int errnum); |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 92 | |
| 93 | /* |
Elliott Hughes | e4239ae | 2009-10-20 16:59:01 -0700 | [diff] [blame] | 94 | * Return a pointer to a locale-dependent error string explaining errno |
| 95 | * value 'errnum'. The returned pointer may or may not be equal to 'buf'. |
| 96 | * This function is thread-safe (unlike strerror) and portable (unlike |
| 97 | * strerror_r). |
| 98 | */ |
Orion Hodson | b01e7fe | 2018-11-07 06:07:50 +0000 | [diff] [blame^] | 99 | MODULE_API const char* jniStrError(int errnum, char* buf, size_t buflen); |
Elliott Hughes | e4239ae | 2009-10-20 16:59:01 -0700 | [diff] [blame] | 100 | |
| 101 | /* |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 102 | * Returns a new java.io.FileDescriptor for the given int fd. |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 103 | */ |
Orion Hodson | b01e7fe | 2018-11-07 06:07:50 +0000 | [diff] [blame^] | 104 | MODULE_API jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd); |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 105 | |
Carl Shapiro | abb4eff | 2010-06-08 16:37:12 -0700 | [diff] [blame] | 106 | /* |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 107 | * Returns the int fd from a java.io.FileDescriptor. |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 108 | */ |
Orion Hodson | b01e7fe | 2018-11-07 06:07:50 +0000 | [diff] [blame^] | 109 | MODULE_API int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor); |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 110 | |
| 111 | /* |
Pete Bentley | 0505b22 | 2018-07-20 18:18:44 +0100 | [diff] [blame] | 112 | * Sets the int fd in a java.io.FileDescriptor. Throws java.lang.NullPointerException |
| 113 | * if fileDescriptor is null. |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 114 | */ |
Orion Hodson | b01e7fe | 2018-11-07 06:07:50 +0000 | [diff] [blame^] | 115 | MODULE_API void jniSetFileDescriptorOfFD(C_JNIEnv* env, |
| 116 | jobject fileDescriptor, |
| 117 | int value); |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 118 | |
Jeff Brown | 2c3fc7a | 2010-06-01 21:07:08 -0700 | [diff] [blame] | 119 | /* |
Josh Gao | 669bc9e | 2018-06-25 16:22:11 -0700 | [diff] [blame] | 120 | * Returns the long ownerId from a java.io.FileDescriptor. |
| 121 | */ |
Orion Hodson | b01e7fe | 2018-11-07 06:07:50 +0000 | [diff] [blame^] | 122 | MODULE_API jlong jniGetOwnerIdFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor); |
Josh Gao | 669bc9e | 2018-06-25 16:22:11 -0700 | [diff] [blame] | 123 | |
| 124 | /* |
Jeff Brown | a7de678 | 2013-04-02 18:09:29 -0700 | [diff] [blame] | 125 | * Returns the reference from a java.lang.ref.Reference. |
| 126 | */ |
Orion Hodson | b01e7fe | 2018-11-07 06:07:50 +0000 | [diff] [blame^] | 127 | MODULE_API jobject jniGetReferent(C_JNIEnv* env, jobject ref); |
Jeff Brown | a7de678 | 2013-04-02 18:09:29 -0700 | [diff] [blame] | 128 | |
| 129 | /* |
Fredrik Roubert | 2e31280 | 2017-07-04 21:53:08 +0200 | [diff] [blame] | 130 | * Returns a Java String object created from UTF-16 data either from jchar or, |
| 131 | * if called from C++11, char16_t (a bitwise identical distinct type). |
| 132 | */ |
Orion Hodson | b01e7fe | 2018-11-07 06:07:50 +0000 | [diff] [blame^] | 133 | MODULE_API jstring jniCreateString(C_JNIEnv* env, const jchar* unicodeChars, jsize len); |
Fredrik Roubert | 2e31280 | 2017-07-04 21:53:08 +0200 | [diff] [blame] | 134 | |
| 135 | /* |
Jeff Brown | 2c3fc7a | 2010-06-01 21:07:08 -0700 | [diff] [blame] | 136 | * Log a message and an exception. |
| 137 | * If exception is NULL, logs the current exception in the JNI environment. |
| 138 | */ |
Orion Hodson | b01e7fe | 2018-11-07 06:07:50 +0000 | [diff] [blame^] | 139 | MODULE_API void jniLogException(C_JNIEnv* env, int priority, const char* tag, jthrowable exception); |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 140 | |
| 141 | /* |
| 142 | * For C++ code, we provide inlines that map to the C functions. g++ always |
| 143 | * inlines these, even on non-optimized builds. |
| 144 | */ |
Andy McFadden | b53ec15 | 2011-02-08 16:12:33 -0800 | [diff] [blame] | 145 | #if defined(__cplusplus) |
Orion Hodson | b01e7fe | 2018-11-07 06:07:50 +0000 | [diff] [blame^] | 146 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 147 | inline int jniRegisterNativeMethods(JNIEnv* env, const char* className, const JNINativeMethod* gMethods, int numMethods) { |
| 148 | return jniRegisterNativeMethods(&env->functions, className, gMethods, numMethods); |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 149 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 150 | |
| 151 | inline int jniThrowException(JNIEnv* env, const char* className, const char* msg) { |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 152 | return jniThrowException(&env->functions, className, msg); |
| 153 | } |
Elliott Hughes | 2f82af1 | 2011-04-08 17:15:16 -0700 | [diff] [blame] | 154 | |
Elliott Hughes | 2f82af1 | 2011-04-08 17:15:16 -0700 | [diff] [blame] | 155 | /* |
| 156 | * Equivalent to jniThrowException but with a printf-like format string and |
| 157 | * variable-length argument list. This is only available in C++. |
| 158 | */ |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 159 | inline int jniThrowExceptionFmt(JNIEnv* env, const char* className, const char* fmt, ...) { |
Elliott Hughes | 2f82af1 | 2011-04-08 17:15:16 -0700 | [diff] [blame] | 160 | va_list args; |
| 161 | va_start(args, fmt); |
| 162 | return jniThrowExceptionFmt(&env->functions, className, fmt, args); |
| 163 | va_end(args); |
| 164 | } |
| 165 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 166 | inline int jniThrowNullPointerException(JNIEnv* env, const char* msg) { |
Elliott Hughes | 9f6e1d3 | 2010-01-28 13:43:39 -0800 | [diff] [blame] | 167 | return jniThrowNullPointerException(&env->functions, msg); |
| 168 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 169 | |
| 170 | inline int jniThrowRuntimeException(JNIEnv* env, const char* msg) { |
Elliott Hughes | 9f6e1d3 | 2010-01-28 13:43:39 -0800 | [diff] [blame] | 171 | return jniThrowRuntimeException(&env->functions, msg); |
| 172 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 173 | |
| 174 | inline int jniThrowIOException(JNIEnv* env, int errnum) { |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 175 | return jniThrowIOException(&env->functions, errnum); |
| 176 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 177 | |
| 178 | inline jobject jniCreateFileDescriptor(JNIEnv* env, int fd) { |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 179 | return jniCreateFileDescriptor(&env->functions, fd); |
| 180 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 181 | |
| 182 | inline int jniGetFDFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) { |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 183 | return jniGetFDFromFileDescriptor(&env->functions, fileDescriptor); |
| 184 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 185 | |
| 186 | inline void jniSetFileDescriptorOfFD(JNIEnv* env, jobject fileDescriptor, int value) { |
Jeff Brown | 2c3fc7a | 2010-06-01 21:07:08 -0700 | [diff] [blame] | 187 | jniSetFileDescriptorOfFD(&env->functions, fileDescriptor, value); |
| 188 | } |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 189 | |
Josh Gao | 669bc9e | 2018-06-25 16:22:11 -0700 | [diff] [blame] | 190 | inline jlong jniGetOwnerIdFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) { |
| 191 | return jniGetOwnerIdFromFileDescriptor(&env->functions, fileDescriptor); |
| 192 | } |
| 193 | |
Jeff Brown | a7de678 | 2013-04-02 18:09:29 -0700 | [diff] [blame] | 194 | inline jobject jniGetReferent(JNIEnv* env, jobject ref) { |
| 195 | return jniGetReferent(&env->functions, ref); |
| 196 | } |
| 197 | |
Fredrik Roubert | 2e31280 | 2017-07-04 21:53:08 +0200 | [diff] [blame] | 198 | inline jstring jniCreateString(JNIEnv* env, const jchar* unicodeChars, jsize len) { |
| 199 | return jniCreateString(&env->functions, unicodeChars, len); |
| 200 | } |
| 201 | |
Fredrik Roubert | 2e31280 | 2017-07-04 21:53:08 +0200 | [diff] [blame] | 202 | inline jstring jniCreateString(JNIEnv* env, const char16_t* unicodeChars, jsize len) { |
| 203 | return jniCreateString(&env->functions, reinterpret_cast<const jchar*>(unicodeChars), len); |
| 204 | } |
Fredrik Roubert | 2e31280 | 2017-07-04 21:53:08 +0200 | [diff] [blame] | 205 | |
Elliott Hughes | 87f62a8 | 2011-04-22 19:22:54 -0700 | [diff] [blame] | 206 | inline void jniLogException(JNIEnv* env, int priority, const char* tag, jthrowable exception = NULL) { |
Jeff Brown | 2c3fc7a | 2010-06-01 21:07:08 -0700 | [diff] [blame] | 207 | jniLogException(&env->functions, priority, tag, exception); |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 208 | } |
Brian Carlstrom | dd8af23 | 2012-05-13 23:56:07 -0700 | [diff] [blame] | 209 | |
Ian Rogers | 8288dde | 2014-11-04 11:42:02 -0800 | [diff] [blame] | 210 | #if !defined(DISALLOW_COPY_AND_ASSIGN) |
| 211 | // DISALLOW_COPY_AND_ASSIGN disallows the copy and operator= functions. It goes in the private: |
| 212 | // declarations in a class. |
Ian Rogers | 8288dde | 2014-11-04 11:42:02 -0800 | [diff] [blame] | 213 | #define DISALLOW_COPY_AND_ASSIGN(TypeName) \ |
| 214 | TypeName(const TypeName&) = delete; \ |
| 215 | void operator=(const TypeName&) = delete |
Ian Rogers | 8288dde | 2014-11-04 11:42:02 -0800 | [diff] [blame] | 216 | #endif // !defined(DISALLOW_COPY_AND_ASSIGN) |
| 217 | |
Orion Hodson | b01e7fe | 2018-11-07 06:07:50 +0000 | [diff] [blame^] | 218 | #endif // defined(__cplusplus) |
The Android Open Source Project | 7f844dd | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 219 | |
Dan Bornstein | 6edaa47 | 2009-10-26 13:33:22 -0700 | [diff] [blame] | 220 | /* |
| 221 | * TEMP_FAILURE_RETRY is defined by some, but not all, versions of |
| 222 | * <unistd.h>. (Alas, it is not as standard as we'd hoped!) So, if it's |
| 223 | * not already defined, then define it here. |
| 224 | */ |
| 225 | #ifndef TEMP_FAILURE_RETRY |
| 226 | /* Used to retry syscalls that can return EINTR. */ |
| 227 | #define TEMP_FAILURE_RETRY(exp) ({ \ |
| 228 | typeof (exp) _rc; \ |
| 229 | do { \ |
| 230 | _rc = (exp); \ |
| 231 | } while (_rc == -1 && errno == EINTR); \ |
| 232 | _rc; }) |
| 233 | #endif |
| 234 | |
Carl Shapiro | 1ff7a0d | 2011-06-14 20:31:24 -0700 | [diff] [blame] | 235 | #endif /* NATIVEHELPER_JNIHELP_H_ */ |