diff options
| author | android-build-team Robot <android-build-team-robot@google.com> | 2019-11-05 12:33:11 +0000 |
|---|---|---|
| committer | android-build-team Robot <android-build-team-robot@google.com> | 2019-11-05 12:33:11 +0000 |
| commit | 8386fa6d12887b5b70b6d48aa8c9d19901a04e1f (patch) | |
| tree | 0e1feaa1d877c9f624d1dea63127295f53b34e8a | |
| parent | 721695a8babcce4bdad0fd7134a3f6ab0c059ada (diff) | |
| parent | fcb38946a79ccfde12654834e936f0dee2c73350 (diff) | |
| download | platform_libnativehelper-android10-mainline-networking-release.tar.gz platform_libnativehelper-android10-mainline-networking-release.tar.bz2 platform_libnativehelper-android10-mainline-networking-release.zip | |
Snap for 5988121 from fcb38946a79ccfde12654834e936f0dee2c73350 to qt-aml-networking-releaseandroid-mainline-10.0.0_r6android10-mainline-networking-release
Change-Id: Icd43ef086589f74e4986d0037428a4a4be9390b3
27 files changed, 465 insertions, 387 deletions
diff --git a/ALog-priv.h b/ALog-priv.h index 5e24fc8..ca132d9 100644 --- a/ALog-priv.h +++ b/ALog-priv.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef NATIVEHELPER_ALOGPRIV_H_ -#define NATIVEHELPER_ALOGPRIV_H_ +#ifndef LIBNATIVEHELPER_ALOG_PRIV_H_ +#define LIBNATIVEHELPER_ALOG_PRIV_H_ #include <android/log.h> @@ -73,4 +73,4 @@ if (cond) __android_log_assert(#cond, LOG_TAG, __VA_ARGS__) #endif -#endif // NATIVEHELPER_ALOGPRIV_H_ +#endif // LIBNATIVEHELPER_ALOG_PRIV_H_ diff --git a/JNIHelp.cpp b/JNIHelp.cpp index 8432f7a..b57ec50 100644 --- a/JNIHelp.cpp +++ b/JNIHelp.cpp @@ -14,22 +14,23 @@ * limitations under the License. */ -#define LOG_TAG "JNIHelp" - #include "nativehelper/JNIHelp.h" -#include "ALog-priv.h" - #include <string> +#define LOG_TAG "JNIHelp" +#include "ALog-priv.h" + +#include "jni.h" #include "JniConstants.h" -#include "nativehelper/ScopedLocalRef.h" + +namespace { /** * Equivalent to ScopedLocalRef, but for C_JNIEnv instead. (And slightly more powerful.) */ template<typename T> -class scoped_local_ref { +class scoped_local_ref final { public: explicit scoped_local_ref(C_JNIEnv* env, T localRef = NULL) : mEnv(env), mLocalRef(localRef) @@ -52,42 +53,28 @@ public: } private: + // scoped_local_ref does not support copy or move semantics. + scoped_local_ref(const scoped_local_ref&) = delete; + scoped_local_ref(scoped_local_ref&&) = delete; + scoped_local_ref& operator=(const scoped_local_ref&) = delete; + scoped_local_ref& operator=(scoped_local_ref&&) = delete; + +private: C_JNIEnv* const mEnv; T mLocalRef; - - DISALLOW_COPY_AND_ASSIGN(scoped_local_ref); }; -static jclass findClass(C_JNIEnv* env, const char* className) { +jclass findClass(C_JNIEnv* env, const char* className) { JNIEnv* e = reinterpret_cast<JNIEnv*>(env); return (*env)->FindClass(e, className); } -MODULE_API int jniRegisterNativeMethods(C_JNIEnv* env, const char* className, - const JNINativeMethod* gMethods, int numMethods) -{ - JNIEnv* e = reinterpret_cast<JNIEnv*>(env); - - ALOGV("Registering %s's %d native methods...", className, numMethods); - - scoped_local_ref<jclass> c(env, findClass(env, className)); - ALOG_ALWAYS_FATAL_IF(c.get() == NULL, - "Native registration unable to find class '%s'; aborting...", - className); - - int result = e->RegisterNatives(c.get(), gMethods, numMethods); - ALOG_ALWAYS_FATAL_IF(result < 0, "RegisterNatives failed for '%s'; aborting...", - className); - - return 0; -} - /* * Returns a human-readable summary of an exception object. The buffer will * be populated with the "binary" class name and, if present, the * exception message. */ -static bool getExceptionSummary(C_JNIEnv* env, jthrowable exception, std::string& result) { +bool getExceptionSummary(C_JNIEnv* env, jthrowable exception, std::string& result) { JNIEnv* e = reinterpret_cast<JNIEnv*>(env); /* get the name of the exception's class */ @@ -138,7 +125,7 @@ static bool getExceptionSummary(C_JNIEnv* env, jthrowable exception, std::string /* * Returns an exception (with stack trace) as a string. */ -static bool getStackTrace(C_JNIEnv* env, jthrowable exception, std::string& result) { +bool getStackTrace(C_JNIEnv* env, jthrowable exception, std::string& result) { JNIEnv* e = reinterpret_cast<JNIEnv*>(env); scoped_local_ref<jclass> stringWriterClass(env, findClass(env, "java/io/StringWriter")); @@ -196,58 +183,7 @@ static bool getStackTrace(C_JNIEnv* env, jthrowable exception, std::string& resu return true; } -MODULE_API int jniThrowException(C_JNIEnv* env, const char* className, const char* msg) { - JNIEnv* e = reinterpret_cast<JNIEnv*>(env); - - if ((*env)->ExceptionCheck(e)) { - /* TODO: consider creating the new exception with this as "cause" */ - scoped_local_ref<jthrowable> exception(env, (*env)->ExceptionOccurred(e)); - (*env)->ExceptionClear(e); - - if (exception.get() != NULL) { - std::string text; - getExceptionSummary(env, exception.get(), text); - ALOGW("Discarding pending exception (%s) to throw %s", text.c_str(), className); - } - } - - scoped_local_ref<jclass> exceptionClass(env, findClass(env, className)); - if (exceptionClass.get() == NULL) { - ALOGE("Unable to find exception class %s", className); - /* ClassNotFoundException now pending */ - return -1; - } - - if ((*env)->ThrowNew(e, exceptionClass.get(), msg) != JNI_OK) { - ALOGE("Failed throwing '%s' '%s'", className, msg); - /* an exception, most likely OOM, will now be pending */ - return -1; - } - - return 0; -} - -MODULE_API int jniThrowExceptionFmt(C_JNIEnv* env, const char* className, const char* fmt, va_list args) { - char msgBuf[512]; - vsnprintf(msgBuf, sizeof(msgBuf), fmt, args); - return jniThrowException(env, className, msgBuf); -} - -MODULE_API int jniThrowNullPointerException(C_JNIEnv* env, const char* msg) { - return jniThrowException(env, "java/lang/NullPointerException", msg); -} - -MODULE_API int jniThrowRuntimeException(C_JNIEnv* env, const char* msg) { - return jniThrowException(env, "java/lang/RuntimeException", msg); -} - -MODULE_API int jniThrowIOException(C_JNIEnv* env, int errnum) { - char buffer[80]; - const char* message = jniStrError(errnum, buffer, sizeof(buffer)); - return jniThrowException(env, "java/io/IOException", message); -} - -static std::string jniGetStackTrace(C_JNIEnv* env, jthrowable exception) { +std::string jniGetStackTrace(C_JNIEnv* env, jthrowable exception) { JNIEnv* e = reinterpret_cast<JNIEnv*>(env); scoped_local_ref<jthrowable> currentException(env, (*env)->ExceptionOccurred(e)); @@ -275,18 +211,12 @@ static std::string jniGetStackTrace(C_JNIEnv* env, jthrowable exception) { return trace; } -MODULE_API void jniLogException(C_JNIEnv* env, int priority, const char* tag, jthrowable exception) { - std::string trace(jniGetStackTrace(env, exception)); - __android_log_write(priority, tag, trace.c_str()); -} - // Note: glibc has a nonstandard strerror_r that returns char* rather than POSIX's int. // char *strerror_r(int errnum, char *buf, size_t n); // // Some versions of bionic support the glibc style call. Since the set of defines that determine // which version is used is byzantine in its complexity we will just use this C++ template hack to // select the correct jniStrError implementation based on the libc being used. -namespace impl { using GNUStrError = char* (*)(int,char*,size_t); using POSIXStrError = int (*)(int,char*,size_t); @@ -306,20 +236,95 @@ inline const char* realJniStrError(POSIXStrError func, int errnum, char* buf, si return buf; } -} // namespace impl +} // namespace + +int jniRegisterNativeMethods(C_JNIEnv* env, const char* className, + const JNINativeMethod* gMethods, int numMethods) +{ + JNIEnv* e = reinterpret_cast<JNIEnv*>(env); + + ALOGV("Registering %s's %d native methods...", className, numMethods); + + scoped_local_ref<jclass> c(env, findClass(env, className)); + ALOG_ALWAYS_FATAL_IF(c.get() == NULL, + "Native registration unable to find class '%s'; aborting...", + className); + + int result = e->RegisterNatives(c.get(), gMethods, numMethods); + ALOG_ALWAYS_FATAL_IF(result < 0, "RegisterNatives failed for '%s'; aborting...", + className); + + return 0; +} + +int jniThrowException(C_JNIEnv* env, const char* className, const char* msg) { + JNIEnv* e = reinterpret_cast<JNIEnv*>(env); + + if ((*env)->ExceptionCheck(e)) { + /* TODO: consider creating the new exception with this as "cause" */ + scoped_local_ref<jthrowable> exception(env, (*env)->ExceptionOccurred(e)); + (*env)->ExceptionClear(e); + + if (exception.get() != NULL) { + std::string text; + getExceptionSummary(env, exception.get(), text); + ALOGW("Discarding pending exception (%s) to throw %s", text.c_str(), className); + } + } + + scoped_local_ref<jclass> exceptionClass(env, findClass(env, className)); + if (exceptionClass.get() == NULL) { + ALOGE("Unable to find exception class %s", className); + /* ClassNotFoundException now pending */ + return -1; + } + + if ((*env)->ThrowNew(e, exceptionClass.get(), msg) != JNI_OK) { + ALOGE("Failed throwing '%s' '%s'", className, msg); + /* an exception, most likely OOM, will now be pending */ + return -1; + } + + return 0; +} + +int jniThrowExceptionFmt(C_JNIEnv* env, const char* className, const char* fmt, va_list args) { + char msgBuf[512]; + vsnprintf(msgBuf, sizeof(msgBuf), fmt, args); + return jniThrowException(env, className, msgBuf); +} + +int jniThrowNullPointerException(C_JNIEnv* env, const char* msg) { + return jniThrowException(env, "java/lang/NullPointerException", msg); +} + +int jniThrowRuntimeException(C_JNIEnv* env, const char* msg) { + return jniThrowException(env, "java/lang/RuntimeException", msg); +} + +int jniThrowIOException(C_JNIEnv* env, int errnum) { + char buffer[80]; + const char* message = jniStrError(errnum, buffer, sizeof(buffer)); + return jniThrowException(env, "java/io/IOException", message); +} + +void jniLogException(C_JNIEnv* env, int priority, const char* tag, jthrowable exception) { + std::string trace(jniGetStackTrace(env, exception)); + __android_log_write(priority, tag, trace.c_str()); +} -MODULE_API const char* jniStrError(int errnum, char* buf, size_t buflen) { +const char* jniStrError(int errnum, char* buf, size_t buflen) { #ifdef _WIN32 strerror_s(buf, buflen, errnum); return buf; #else // The magic of C++ overloading selects the correct implementation based on the declared type of // strerror_r. The inline will ensure that we don't have any indirect calls. - return impl::realJniStrError(strerror_r, errnum, buf, buflen); + return realJniStrError(strerror_r, errnum, buf, buflen); #endif } -MODULE_API jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd) { +jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd) { JNIEnv* e = reinterpret_cast<JNIEnv*>(env); jobject fileDescriptor = e->NewObject(JniConstants::GetFileDescriptorClass(e), JniConstants::GetFileDescriptorInitMethod(e)); @@ -331,7 +336,7 @@ MODULE_API jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd) { return fileDescriptor; } -MODULE_API int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor) { +int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor) { JNIEnv* e = reinterpret_cast<JNIEnv*>(env); if (fileDescriptor != nullptr) { return e->GetIntField(fileDescriptor, @@ -341,7 +346,7 @@ MODULE_API int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor) } } -MODULE_API void jniSetFileDescriptorOfFD(C_JNIEnv* env, jobject fileDescriptor, int value) { +void jniSetFileDescriptorOfFD(C_JNIEnv* env, jobject fileDescriptor, int value) { JNIEnv* e = reinterpret_cast<JNIEnv*>(env); if (fileDescriptor == nullptr) { jniThrowNullPointerException(e, "null FileDescriptor"); @@ -350,12 +355,12 @@ MODULE_API void jniSetFileDescriptorOfFD(C_JNIEnv* env, jobject fileDescriptor, } } -MODULE_API jlong jniGetOwnerIdFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor) { +jlong jniGetOwnerIdFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor) { JNIEnv* e = reinterpret_cast<JNIEnv*>(env); return e->GetLongField(fileDescriptor, JniConstants::GetFileDescriptorOwnerIdField(e)); } -MODULE_API jarray jniGetNioBufferBaseArray(C_JNIEnv* env, jobject nioBuffer) { +jarray jniGetNioBufferBaseArray(C_JNIEnv* env, jobject nioBuffer) { JNIEnv* e = reinterpret_cast<JNIEnv*>(env); jclass nioAccessClass = JniConstants::GetNioAccessClass(e); jmethodID getBaseArrayMethod = JniConstants::GetNioAccessGetBaseArrayMethod(e); @@ -363,14 +368,14 @@ MODULE_API jarray jniGetNioBufferBaseArray(C_JNIEnv* env, jobject nioBuffer) { return static_cast<jarray>(object); } -MODULE_API int jniGetNioBufferBaseArrayOffset(C_JNIEnv* env, jobject nioBuffer) { +int jniGetNioBufferBaseArrayOffset(C_JNIEnv* env, jobject nioBuffer) { JNIEnv* e = reinterpret_cast<JNIEnv*>(env); jclass nioAccessClass = JniConstants::GetNioAccessClass(e); jmethodID getBaseArrayOffsetMethod = JniConstants::GetNioAccessGetBaseArrayOffsetMethod(e); return e->CallStaticIntMethod(nioAccessClass, getBaseArrayOffsetMethod, nioBuffer); } -MODULE_API jlong jniGetNioBufferPointer(C_JNIEnv* env, jobject nioBuffer) { +jlong jniGetNioBufferPointer(C_JNIEnv* env, jobject nioBuffer) { JNIEnv* e = reinterpret_cast<JNIEnv*>(env); jlong baseAddress = e->GetLongField(nioBuffer, JniConstants::GetNioBufferAddressField(e)); if (baseAddress != 0) { @@ -382,8 +387,8 @@ MODULE_API jlong jniGetNioBufferPointer(C_JNIEnv* env, jobject nioBuffer) { return baseAddress; } -MODULE_API jlong jniGetNioBufferFields(C_JNIEnv* env, jobject nioBuffer, - jint* position, jint* limit, jint* elementSizeShift) { +jlong jniGetNioBufferFields(C_JNIEnv* env, jobject nioBuffer, + jint* position, jint* limit, jint* elementSizeShift) { JNIEnv* e = reinterpret_cast<JNIEnv*>(env); *position = e->GetIntField(nioBuffer, JniConstants::GetNioBufferPositionField(e)); *limit = e->GetIntField(nioBuffer, JniConstants::GetNioBufferLimitField(e)); @@ -392,16 +397,16 @@ MODULE_API jlong jniGetNioBufferFields(C_JNIEnv* env, jobject nioBuffer, return e->GetLongField(nioBuffer, JniConstants::GetNioBufferAddressField(e)); } -MODULE_API jobject jniGetReferent(C_JNIEnv* env, jobject ref) { +jobject jniGetReferent(C_JNIEnv* env, jobject ref) { JNIEnv* e = reinterpret_cast<JNIEnv*>(env); return e->CallObjectMethod(ref, JniConstants::GetReferenceGetMethod(e)); } -MODULE_API jstring jniCreateString(C_JNIEnv* env, const jchar* unicodeChars, jsize len) { +jstring jniCreateString(C_JNIEnv* env, const jchar* unicodeChars, jsize len) { JNIEnv* e = reinterpret_cast<JNIEnv*>(env); return e->NewString(unicodeChars, len); } -MODULE_API void jniUninitializeConstants() { +void jniUninitializeConstants() { JniConstants::Uninitialize(); } diff --git a/JniConstants.h b/JniConstants.h index b2d03be..2a637a1 100644 --- a/JniConstants.h +++ b/JniConstants.h @@ -14,7 +14,8 @@ * limitations under the License. */ -#pragma once +#ifndef LIBNATIVEHELPER_JNICONSTANTS_H_ +#define LIBNATIVEHELPER_JNICONSTANTS_H_ #include "jni.h" @@ -81,3 +82,5 @@ struct JniConstants { // one reason why there is a limit of VM instance per process. static void Uninitialize(); }; + +#endif // LIBNATIVEHELPER_JNICONSTANTS_H_ diff --git a/JniInvocation.cpp b/JniInvocation.cpp index 6d992e6..dc55d81 100644 --- a/JniInvocation.cpp +++ b/JniInvocation.cpp @@ -112,8 +112,6 @@ struct JniInvocationImpl final { bool Init(const char* library); - // static const char* GetLibrary(const char* library, char* buffer); - static const char* GetLibrary(const char* library, char* buffer, bool (*is_debuggable)() = IsDebuggable, @@ -281,11 +279,11 @@ JniInvocationImpl& JniInvocationImpl::GetJniInvocation() { return *jni_invocation_; } -MODULE_API jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) { +jint JNI_GetDefaultJavaVMInitArgs(void* vm_args) { return JniInvocationImpl::GetJniInvocation().JNI_GetDefaultJavaVMInitArgs(vm_args); } -MODULE_API jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) { +jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) { // Ensure any cached heap objects from previous VM instances are // invalidated. There is no notification here that a VM is destroyed. These // cached objects limit us to one VM instance per process. @@ -293,29 +291,29 @@ MODULE_API jint JNI_CreateJavaVM(JavaVM** p_vm, JNIEnv** p_env, void* vm_args) { return JniInvocationImpl::GetJniInvocation().JNI_CreateJavaVM(p_vm, p_env, vm_args); } -MODULE_API jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize size, jsize* vm_count) { +jint JNI_GetCreatedJavaVMs(JavaVM** vms, jsize size, jsize* vm_count) { return JniInvocationImpl::GetJniInvocation().JNI_GetCreatedJavaVMs(vms, size, vm_count); } -MODULE_API JniInvocationImpl* JniInvocationCreate() { +const char* JniInvocation::GetLibrary(const char* library, + char* buffer, + bool (*is_debuggable)(), + int (*get_library_system_property)(char* buffer)) { + return JniInvocationImpl::GetLibrary(library, buffer, is_debuggable, get_library_system_property); +} + +JniInvocationImpl* JniInvocationCreate() { return new JniInvocationImpl(); } -MODULE_API void JniInvocationDestroy(JniInvocationImpl* instance) { +void JniInvocationDestroy(JniInvocationImpl* instance) { delete instance; } -MODULE_API int JniInvocationInit(JniInvocationImpl* instance, const char* library) { +int JniInvocationInit(JniInvocationImpl* instance, const char* library) { return instance->Init(library) ? 1 : 0; } -MODULE_API const char* JniInvocationGetLibrary(const char* library, char* buffer) { +const char* JniInvocationGetLibrary(const char* library, char* buffer) { return JniInvocationImpl::GetLibrary(library, buffer); } - -MODULE_API const char* JniInvocation::GetLibrary(const char* library, - char* buffer, - bool (*is_debuggable)(), - int (*get_library_system_property)(char* buffer)) { - return JniInvocationImpl::GetLibrary(library, buffer, is_debuggable, get_library_system_property); -} diff --git a/header_only_include/nativehelper/nativehelper_utils.h b/header_only_include/nativehelper/nativehelper_utils.h index d7289f9..2cf7b96 100644 --- a/header_only_include/nativehelper/nativehelper_utils.h +++ b/header_only_include/nativehelper/nativehelper_utils.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef NATIVEHELPER_MACROS_H_ -#define NATIVEHELPER_MACROS_H_ +#ifndef LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_NATIVEHELPER_UTILS_H_ +#define LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_NATIVEHELPER_UTILS_H_ #if defined(__cplusplus) @@ -27,9 +27,8 @@ void operator=(const TypeName&) = delete #endif // !defined(DISALLOW_COPY_AND_ASSIGN) -#ifndef NATIVEHELPER_JNIHELP_H_ // This seems a header-only include. Provide NPE throwing. -static inline int jniThrowNullPointerException(JNIEnv* env, const char* msg) { +static inline int jniThrowNullPointerException(JNIEnv* env) { if (env->ExceptionCheck()) { // Drop any pending exception. env->ExceptionClear(); @@ -40,7 +39,7 @@ static inline int jniThrowNullPointerException(JNIEnv* env, const char* msg) { return -1; } - if (env->ThrowNew(e_class, msg) != JNI_OK) { + if (env->ThrowNew(e_class, nullptr) != JNI_OK) { env->DeleteLocalRef(e_class); return -1; } @@ -48,8 +47,7 @@ static inline int jniThrowNullPointerException(JNIEnv* env, const char* msg) { env->DeleteLocalRef(e_class); return 0; } -#endif // NATIVEHELPER_JNIHELP_H_ #endif // defined(__cplusplus) -#endif // NATIVEHELPER_MACROS_H_ +#endif // LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_NATIVEHELPER_UTILS_H_ diff --git a/header_only_include/nativehelper/scoped_bytes.h b/header_only_include/nativehelper/scoped_bytes.h index f53931e..1806d37 100644 --- a/header_only_include/nativehelper/scoped_bytes.h +++ b/header_only_include/nativehelper/scoped_bytes.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef SCOPED_BYTES_H_ -#define SCOPED_BYTES_H_ +#ifndef LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_SCOPED_BYTES_H_ +#define LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_SCOPED_BYTES_H_ #include "jni.h" #include "nativehelper_utils.h" @@ -33,7 +33,7 @@ public: : mEnv(env), mObject(object), mByteArray(NULL), mPtr(NULL) { if (mObject == NULL) { - jniThrowNullPointerException(mEnv, NULL); + jniThrowNullPointerException(mEnv); } else { jclass byteArrayClass = env->FindClass("[B"); if (mEnv->IsInstanceOf(mObject, byteArrayClass)) { @@ -80,4 +80,4 @@ public: } }; -#endif // SCOPED_BYTES_H_ +#endif // LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_SCOPED_BYTES_H_ diff --git a/header_only_include/nativehelper/scoped_local_frame.h b/header_only_include/nativehelper/scoped_local_frame.h index 91180fe..0222014 100644 --- a/header_only_include/nativehelper/scoped_local_frame.h +++ b/header_only_include/nativehelper/scoped_local_frame.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef SCOPED_LOCAL_FRAME_H_ -#define SCOPED_LOCAL_FRAME_H_ +#ifndef LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_SCOPED_LOCAL_FRAME_H_ +#define LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_SCOPED_LOCAL_FRAME_H_ #include "jni.h" #include "nativehelper_utils.h" @@ -36,4 +36,4 @@ private: DISALLOW_COPY_AND_ASSIGN(ScopedLocalFrame); }; -#endif // SCOPED_LOCAL_FRAME_H_ +#endif // LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_SCOPED_LOCAL_FRAME_H_ diff --git a/header_only_include/nativehelper/scoped_local_ref.h b/header_only_include/nativehelper/scoped_local_ref.h index 3eb21d9..11e1043 100644 --- a/header_only_include/nativehelper/scoped_local_ref.h +++ b/header_only_include/nativehelper/scoped_local_ref.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef SCOPED_LOCAL_REF_H_ -#define SCOPED_LOCAL_REF_H_ +#ifndef LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_SCOPED_LOCAL_REF_H_ +#define LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_SCOPED_LOCAL_REF_H_ #include <cstddef> @@ -88,4 +88,4 @@ private: DISALLOW_COPY_AND_ASSIGN(ScopedLocalRef); }; -#endif // SCOPED_LOCAL_REF_H_ +#endif // LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_SCOPED_LOCAL_REF_H_ diff --git a/header_only_include/nativehelper/scoped_primitive_array.h b/header_only_include/nativehelper/scoped_primitive_array.h index d6840c2..185177a 100644 --- a/header_only_include/nativehelper/scoped_primitive_array.h +++ b/header_only_include/nativehelper/scoped_primitive_array.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef SCOPED_PRIMITIVE_ARRAY_H_ -#define SCOPED_PRIMITIVE_ARRAY_H_ +#ifndef LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_SCOPED_PRIMITIVE_ARRAY_H_ +#define LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_SCOPED_PRIMITIVE_ARRAY_H_ #include "jni.h" #include "nativehelper_utils.h" @@ -47,7 +47,7 @@ mJavaArray = NULL; \ mSize = 0; \ mRawArray = NULL; \ - jniThrowNullPointerException(mEnv, NULL); \ + jniThrowNullPointerException(mEnv); \ } else { \ reset(javaArray); \ } \ @@ -104,7 +104,7 @@ INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RO(jshort, Short); Scoped ## NAME ## ArrayRW(JNIEnv* env, PRIMITIVE_TYPE ## Array javaArray) \ : mEnv(env), mJavaArray(javaArray), mRawArray(NULL) { \ if (mJavaArray == NULL) { \ - jniThrowNullPointerException(mEnv, NULL); \ + jniThrowNullPointerException(mEnv); \ } else { \ mRawArray = mEnv->Get ## NAME ## ArrayElements(mJavaArray, NULL); \ } \ @@ -144,4 +144,4 @@ INSTANTIATE_SCOPED_PRIMITIVE_ARRAY_RW(jshort, Short); #undef POINTER_TYPE #undef REFERENCE_TYPE -#endif // SCOPED_PRIMITIVE_ARRAY_H_ +#endif // LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_SCOPED_PRIMITIVE_ARRAY_H_ diff --git a/header_only_include/nativehelper/scoped_string_chars.h b/header_only_include/nativehelper/scoped_string_chars.h index 4debb2a..6c9f05c 100644 --- a/header_only_include/nativehelper/scoped_string_chars.h +++ b/header_only_include/nativehelper/scoped_string_chars.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef SCOPED_STRING_CHARS_H_ -#define SCOPED_STRING_CHARS_H_ +#ifndef LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_SCOPED_STRING_CHARS_H_ +#define LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_SCOPED_STRING_CHARS_H_ #include "jni.h" #include "nativehelper_utils.h" @@ -34,7 +34,7 @@ class ScopedStringChars { ScopedStringChars(JNIEnv* env, jstring s) : env_(env), string_(s), size_(0) { if (s == NULL) { chars_ = NULL; - jniThrowNullPointerException(env, NULL); + jniThrowNullPointerException(env); } else { chars_ = env->GetStringChars(string_, NULL); if (chars_ != NULL) { @@ -70,4 +70,4 @@ class ScopedStringChars { DISALLOW_COPY_AND_ASSIGN(ScopedStringChars); }; -#endif // SCOPED_STRING_CHARS_H_ +#endif // LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_SCOPED_STRING_CHARS_H_ diff --git a/header_only_include/nativehelper/scoped_utf_chars.h b/header_only_include/nativehelper/scoped_utf_chars.h index bab7cb7..87df2c4 100644 --- a/header_only_include/nativehelper/scoped_utf_chars.h +++ b/header_only_include/nativehelper/scoped_utf_chars.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef SCOPED_UTF_CHARS_H_ -#define SCOPED_UTF_CHARS_H_ +#ifndef LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_SCOPED_UTF_CHARS_H_ +#define LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_SCOPED_UTF_CHARS_H_ #include <string.h> @@ -36,7 +36,7 @@ class ScopedUtfChars { ScopedUtfChars(JNIEnv* env, jstring s) : env_(env), string_(s) { if (s == nullptr) { utf_chars_ = nullptr; - jniThrowNullPointerException(env, nullptr); + jniThrowNullPointerException(env); } else { utf_chars_ = env->GetStringUTFChars(s, nullptr); } @@ -91,4 +91,4 @@ class ScopedUtfChars { DISALLOW_COPY_AND_ASSIGN(ScopedUtfChars); }; -#endif // SCOPED_UTF_CHARS_H_ +#endif // LIBNATIVEHELPER_HEADER_ONLY_INCLUDE_NATIVEHELPER_SCOPED_UTF_CHARS_H_ diff --git a/include/nativehelper/JNIHelp.h b/include/nativehelper/JNIHelp.h index 92a3882..858d625 100644 --- a/include/nativehelper/JNIHelp.h +++ b/include/nativehelper/JNIHelp.h @@ -20,172 +20,19 @@ * This file may be included by C or C++ code, which is trouble because jni.h * uses different typedefs for JNIEnv in each language. */ -#ifndef NATIVEHELPER_JNIHELP_H_ -#define NATIVEHELPER_JNIHELP_H_ +#ifndef LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_JNIHELP_H_ +#define LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_JNIHELP_H_ #include <errno.h> #include <unistd.h> -#include <jni.h> -#include "module_api.h" +#include "libnativehelper_api.h" #ifndef NELEM -# define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) +#define NELEM(x) ((int) (sizeof(x) / sizeof((x)[0]))) #endif /* - * Register one or more native methods with a particular class. - * "className" looks like "java/lang/String". Aborts on failure. - * TODO: fix all callers and change the return type to void. - */ -MODULE_API int jniRegisterNativeMethods(C_JNIEnv* env, - const char* className, - const JNINativeMethod* gMethods, - int numMethods); - -/* - * Throw an exception with the specified class and an optional message. - * - * The "className" argument will be passed directly to FindClass, which - * takes strings with slashes (e.g. "java/lang/Object"). - * - * If an exception is currently pending, we log a warning message and - * clear it. - * - * Returns 0 on success, nonzero if something failed (e.g. the exception - * class couldn't be found, so *an* exception will still be pending). - * - * Currently aborts the VM if it can't throw the exception. - */ -MODULE_API int jniThrowException(C_JNIEnv* env, const char* className, const char* msg); - -/* - * Throw an exception with the specified class and formatted error message. - * - * The "className" argument will be passed directly to FindClass, which - * takes strings with slashes (e.g. "java/lang/Object"). - * - * If an exception is currently pending, we log a warning message and - * clear it. - * - * Returns 0 on success, nonzero if something failed (e.g. the exception - * class couldn't be found, so *an* exception will still be pending). - * - * Currently aborts the VM if it can't throw the exception. - */ -MODULE_API int jniThrowExceptionFmt(C_JNIEnv* env, const char* className, const char* fmt, va_list args); - -/* - * Throw a java.lang.NullPointerException, with an optional message. - */ -MODULE_API int jniThrowNullPointerException(C_JNIEnv* env, const char* msg); - -/* - * Throw a java.lang.RuntimeException, with an optional message. - */ -MODULE_API int jniThrowRuntimeException(C_JNIEnv* env, const char* msg); - -/* - * Throw a java.io.IOException, generating the message from errno. - */ -MODULE_API int jniThrowIOException(C_JNIEnv* env, int errnum); - -/* - * Return a pointer to a locale-dependent error string explaining errno - * value 'errnum'. The returned pointer may or may not be equal to 'buf'. - * This function is thread-safe (unlike strerror) and portable (unlike - * strerror_r). - */ -MODULE_API const char* jniStrError(int errnum, char* buf, size_t buflen); - -/* - * Returns a new java.io.FileDescriptor for the given int fd. - */ -MODULE_API jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd); - -/* - * Returns the int fd from a java.io.FileDescriptor. - */ -MODULE_API int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor); - -/* - * Sets the int fd in a java.io.FileDescriptor. Throws java.lang.NullPointerException - * if fileDescriptor is null. - */ -MODULE_API void jniSetFileDescriptorOfFD(C_JNIEnv* env, - jobject fileDescriptor, - int value); - -/* - * Returns the long ownerId from a java.io.FileDescriptor. - */ -MODULE_API jlong jniGetOwnerIdFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor); - -/* - * Gets the managed heap array backing a java.nio.Buffer instance. - * - * Returns nullptr if there is no array backing. - * - * This method performs a JNI call to java.nio.NIOAccess.getBaseArray(). - */ -MODULE_API jarray jniGetNioBufferBaseArray(C_JNIEnv* env, jobject nioBuffer); - -/* - * Gets the offset in bytes from the start of the managed heap array backing the buffer. - * - * Returns 0 if there is no array backing. - * - * This method performs a JNI call to java.nio.NIOAccess.getBaseArrayOffset(). - */ -MODULE_API jint jniGetNioBufferBaseArrayOffset(C_JNIEnv* env, jobject nioBuffer); - -/* - * Gets field information from a java.nio.Buffer instance. - * - * Reads the |position|, |limit|, and |elementSizeShift| fields from the buffer instance. - * - * Returns the |address| field of the java.nio.Buffer instance which is only valid (non-zero) when - * the buffer is backed by a direct buffer. - */ -MODULE_API jlong jniGetNioBufferFields(C_JNIEnv* env, - jobject nioBuffer, - /*out*/jint* position, - /*out*/jint* limit, - /*out*/jint* elementSizeShift); - -/* - * Gets the current position from a java.nio.Buffer as a pointer to memory in a fixed buffer. - * - * Returns 0 if |nioBuffer| is not backed by a direct buffer. - * - * This method reads the |address|, |position|, and |elementSizeShift| fields from the - * java.nio.Buffer instance to calculate the pointer address for the current position. - */ -MODULE_API jlong jniGetNioBufferPointer(C_JNIEnv* env, jobject nioBuffer); - -/* - * Returns the reference from a java.lang.ref.Reference. - */ -MODULE_API jobject jniGetReferent(C_JNIEnv* env, jobject ref); - -/* - * Returns a Java String object created from UTF-16 data either from jchar or, - * if called from C++11, char16_t (a bitwise identical distinct type). - */ -MODULE_API jstring jniCreateString(C_JNIEnv* env, const jchar* unicodeChars, jsize len); - -/* - * Log a message and an exception. - * If exception is NULL, logs the current exception in the JNI environment. - */ -MODULE_API void jniLogException(C_JNIEnv* env, int priority, const char* tag, jthrowable exception); - -/* - * Clear the cache of constants libnativehelper is using. - */ -MODULE_API void jniUninitializeConstants(); - -/* * For C++ code, we provide inlines that map to the C functions. g++ always * inlines these, even on non-optimized builds. */ @@ -274,4 +121,4 @@ inline void jniLogException(JNIEnv* env, int priority, const char* tag, jthrowab #endif // defined(__cplusplus) -#endif /* NATIVEHELPER_JNIHELP_H_ */ +#endif // LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_JNIHELP_H_ diff --git a/include/nativehelper/JniInvocation.h b/include/nativehelper/JniInvocation.h index 0d87aa9..9b7d510 100644 --- a/include/nativehelper/JniInvocation.h +++ b/include/nativehelper/JniInvocation.h @@ -14,18 +14,10 @@ * limitations under the License. */ -#ifndef JNI_INVOCATION_H_included -#define JNI_INVOCATION_H_included +#ifndef LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_JNIINVOCATION_H_ +#define LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_JNIINVOCATION_H_ -#include <jni.h> -#include "module_api.h" - -struct JniInvocationImpl; - -MODULE_API struct JniInvocationImpl* JniInvocationCreate(); -MODULE_API void JniInvocationDestroy(struct JniInvocationImpl* instance); -MODULE_API int JniInvocationInit(struct JniInvocationImpl* instance, const char* library); -MODULE_API const char* JniInvocationGetLibrary(const char* library, char* buffer); +#include "libnativehelper_api.h" #ifdef __cplusplus @@ -77,4 +69,4 @@ class JniInvocation final { #endif // __cplusplus -#endif // JNI_INVOCATION_H_included +#endif // LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_JNIINVOCATION_H_ diff --git a/include/nativehelper/ScopedBytes.h b/include/nativehelper/ScopedBytes.h index 7cb2ad0..371b1bb 100644 --- a/include/nativehelper/ScopedBytes.h +++ b/include/nativehelper/ScopedBytes.h @@ -14,10 +14,10 @@ * limitations under the License. */ -#ifndef SCOPED_BYTES_H_included -#define SCOPED_BYTES_H_included +#ifndef LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_SCOPEDBYTES_H_ +#define LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_SCOPEDBYTES_H_ #include "JNIHelp.h" #include <nativehelper/scoped_bytes.h> -#endif // SCOPED_BYTES_H_included +#endif // LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_SCOPEDBYTES_H_ diff --git a/include/nativehelper/ScopedLocalFrame.h b/include/nativehelper/ScopedLocalFrame.h index 57873f2..4cb3d23 100644 --- a/include/nativehelper/ScopedLocalFrame.h +++ b/include/nativehelper/ScopedLocalFrame.h @@ -14,9 +14,9 @@ * limitations under the License. */ -#ifndef SCOPED_LOCAL_FRAME_H_included -#define SCOPED_LOCAL_FRAME_H_included +#ifndef LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_SCOPEDLOCALFRAME_H_ +#define LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_SCOPEDLOCALFRAME_H_ #include <nativehelper/scoped_local_frame.h> -#endif // SCOPED_LOCAL_FRAME_H_included +#endif // LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_SCOPEDLOCALFRAME_H_ diff --git a/include/nativehelper/ScopedLocalRef.h b/include/nativehelper/ScopedLocalRef.h index 0fb03d7..9960922 100644 --- a/include/nativehelper/ScopedLocalRef.h +++ b/include/nativehelper/ScopedLocalRef.h @@ -14,10 +14,10 @@ * limitations under the License. */ -#ifndef SCOPED_LOCAL_REF_H_included -#define SCOPED_LOCAL_REF_H_included +#ifndef LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_SCOPEDLOCALREF_H_ +#define LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_SCOPEDLOCALREF_H_ #include "JNIHelp.h" #include <nativehelper/scoped_local_ref.h> -#endif // SCOPED_LOCAL_REF_H_included +#endif // LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_SCOPEDLOCALREF_H_ diff --git a/include/nativehelper/ScopedPrimitiveArray.h b/include/nativehelper/ScopedPrimitiveArray.h index 626b64f..830a712 100644 --- a/include/nativehelper/ScopedPrimitiveArray.h +++ b/include/nativehelper/ScopedPrimitiveArray.h @@ -14,10 +14,10 @@ * limitations under the License. */ -#ifndef SCOPED_PRIMITIVE_ARRAY_H_included -#define SCOPED_PRIMITIVE_ARRAY_H_included +#ifndef LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_SCOPEDPRIMITIVEARRAY_H_ +#define LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_SCOPEDPRIMITIVEARRAY_H_ #include "JNIHelp.h" #include <nativehelper/scoped_primitive_array.h> -#endif // SCOPED_PRIMITIVE_ARRAY_H_included +#endif // LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_SCOPEDPRIMITIVEARRAY_H_ diff --git a/include/nativehelper/ScopedStringChars.h b/include/nativehelper/ScopedStringChars.h index 59c405c..f37d05c 100644 --- a/include/nativehelper/ScopedStringChars.h +++ b/include/nativehelper/ScopedStringChars.h @@ -14,10 +14,10 @@ * limitations under the License. */ -#ifndef SCOPED_STRING_CHARS_H_included -#define SCOPED_STRING_CHARS_H_included +#ifndef LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_SCOPEDSTRINGCHARS_H_ +#define LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_SCOPEDSTRINGCHARS_H_ #include "JNIHelp.h" #include <nativehelper/scoped_string_chars.h> -#endif // SCOPED_STRING_CHARS_H_included +#endif // LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_SCOPEDSTRINGCHARS_H_ diff --git a/include/nativehelper/ScopedUtfChars.h b/include/nativehelper/ScopedUtfChars.h index f123115..b782a4e 100644 --- a/include/nativehelper/ScopedUtfChars.h +++ b/include/nativehelper/ScopedUtfChars.h @@ -14,10 +14,10 @@ * limitations under the License. */ -#ifndef SCOPED_UTF_CHARS_H_included -#define SCOPED_UTF_CHARS_H_included +#ifndef LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_SCOPEDUTFCHARS_H_ +#define LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_SCOPEDUTFCHARS_H_ #include "JNIHelp.h" #include <nativehelper/scoped_utf_chars.h> -#endif // SCOPED_UTF_CHARS_H_included +#endif // LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_SCOPEDUTFCHARS_H_ diff --git a/include/nativehelper/libnativehelper_api.h b/include/nativehelper/libnativehelper_api.h new file mode 100644 index 0000000..b9e3570 --- /dev/null +++ b/include/nativehelper/libnativehelper_api.h @@ -0,0 +1,263 @@ +/* + * Copyright (C) 2019 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_LIBNATIVEHELPER_API_H_ +#define LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_LIBNATIVEHELPER_API_H_ + +#include <stddef.h> + +#include "jni.h" + +// This is the stable C API exported by libnativehelper. + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +/* ------------------------------------ C API for JNIHelp.h ------------------------------------- */ + +/* + * Register one or more native methods with a particular class. "className" looks like + * "java/lang/String". Aborts on failure, returns 0 on success. + */ +int jniRegisterNativeMethods(C_JNIEnv* env, + const char* className, + const JNINativeMethod* gMethods, + int numMethods); + +/* + * Throw an exception with the specified class and an optional message. + * + * The "className" argument will be passed directly to FindClass, which + * takes strings with slashes (e.g. "java/lang/Object"). + * + * If an exception is currently pending, we log a warning message and + * clear it. + * + * Returns 0 on success, nonzero if something failed (e.g. the exception + * class couldn't be found, so *an* exception will still be pending). + * + * Currently aborts the VM if it can't throw the exception. + */ +int jniThrowException(C_JNIEnv* env, const char* className, const char* msg); + +/* + * Throw an exception with the specified class and formatted error message. + * + * The "className" argument will be passed directly to FindClass, which + * takes strings with slashes (e.g. "java/lang/Object"). + * + * If an exception is currently pending, we log a warning message and + * clear it. + * + * Returns 0 on success, nonzero if something failed (e.g. the exception + * class couldn't be found, so *an* exception will still be pending). + * + * Currently aborts the VM if it can't throw the exception. + */ +int jniThrowExceptionFmt(C_JNIEnv* env, const char* className, const char* fmt, va_list args); + +/* + * Throw a java.lang.NullPointerException, with an optional message. + */ +int jniThrowNullPointerException(C_JNIEnv* env, const char* msg); + +/* + * Throw a java.lang.RuntimeException, with an optional message. + */ +int jniThrowRuntimeException(C_JNIEnv* env, const char* msg); + +/* + * Throw a java.io.IOException, generating the message from errno. + */ +int jniThrowIOException(C_JNIEnv* env, int errnum); + +/* + * Return a pointer to a locale-dependent error string explaining errno + * value 'errnum'. The returned pointer may or may not be equal to 'buf'. + * This function is thread-safe (unlike strerror) and portable (unlike + * strerror_r). + */ +const char* jniStrError(int errnum, char* buf, size_t buflen); + +/* + * Returns a new java.io.FileDescriptor for the given int fd. + */ +jobject jniCreateFileDescriptor(C_JNIEnv* env, int fd); + +/* + * Returns the int fd from a java.io.FileDescriptor. + */ +int jniGetFDFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor); + +/* + * Sets the int fd in a java.io.FileDescriptor. Throws java.lang.NullPointerException + * if fileDescriptor is null. + */ +void jniSetFileDescriptorOfFD(C_JNIEnv* env, jobject fileDescriptor, int value); + +/* + * Returns the long ownerId from a java.io.FileDescriptor. + */ +jlong jniGetOwnerIdFromFileDescriptor(C_JNIEnv* env, jobject fileDescriptor); + +/* + * Gets the managed heap array backing a java.nio.Buffer instance. + * + * Returns nullptr if there is no array backing. + * + * This method performs a JNI call to java.nio.NIOAccess.getBaseArray(). + */ +jarray jniGetNioBufferBaseArray(C_JNIEnv* env, jobject nioBuffer); + +/* + * Gets the offset in bytes from the start of the managed heap array backing the buffer. + * + * Returns 0 if there is no array backing. + * + * This method performs a JNI call to java.nio.NIOAccess.getBaseArrayOffset(). + */ +jint jniGetNioBufferBaseArrayOffset(C_JNIEnv* env, jobject nioBuffer); + +/* + * Gets field information from a java.nio.Buffer instance. + * + * Reads the |position|, |limit|, and |elementSizeShift| fields from the buffer instance. + * + * Returns the |address| field of the java.nio.Buffer instance which is only valid (non-zero) when + * the buffer is backed by a direct buffer. + */ +jlong jniGetNioBufferFields(C_JNIEnv* env, + jobject nioBuffer, + /*out*/jint* position, + /*out*/jint* limit, + /*out*/jint* elementSizeShift); + +/* + * Gets the current position from a java.nio.Buffer as a pointer to memory in a fixed buffer. + * + * Returns 0 if |nioBuffer| is not backed by a direct buffer. + * + * This method reads the |address|, |position|, and |elementSizeShift| fields from the + * java.nio.Buffer instance to calculate the pointer address for the current position. + */ +jlong jniGetNioBufferPointer(C_JNIEnv* env, jobject nioBuffer); + +/* + * Returns the reference from a java.lang.ref.Reference. + */ +jobject jniGetReferent(C_JNIEnv* env, jobject ref); + +/* + * Returns a Java String object created from UTF-16 data either from jchar or, + * if called from C++11, char16_t (a bitwise identical distinct type). + */ +jstring jniCreateString(C_JNIEnv* env, const jchar* unicodeChars, jsize len); + +/* + * Log a message and an exception. + * If exception is NULL, logs the current exception in the JNI environment. + */ +void jniLogException(C_JNIEnv* env, int priority, const char* tag, jthrowable exception); + +/* + * Clear the cache of constants libnativehelper is using. + */ +void jniUninitializeConstants(); + +/* ---------------------------------- C API for JniInvocation.h --------------------------------- */ + +/* + * The JNI invocation API exists to allow a choice of library responsible for managing virtual + * machines. + */ + +/* + * Opaque structure used to hold JNI invocation internal state. + */ +struct JniInvocationImpl; + +/* + * Creates an instance of a JniInvocationImpl. + */ +struct JniInvocationImpl* JniInvocationCreate(); + +/* + * Associates a library with a JniInvocationImpl instance. The library should export C symbols for + * JNI_GetDefaultJavaVMInitArgs, JNI_CreateJavaVM and JNI_GetDefaultJavaVMInitArgs. + * + * The specified |library| should be the filename of a shared library. The |library| is opened with + * dlopen(3). + * + * If there is an error opening the specified |library|, then function will fallback to the + * default library "libart.so". If the fallback library is successfully used then a warning is + * written to the Android log buffer. Use of the fallback library is not considered an error. + * + * If the fallback library cannot be opened or the expected symbols are not found in the library + * opened, then an error message is written to the Android log buffer and the function returns 0. + * + * Returns 1 on success, 0 otherwise. + */ +int JniInvocationInit(struct JniInvocationImpl* instance, const char* library); + +/* + * Release resources associated with JniInvocationImpl instance. + */ +void JniInvocationDestroy(struct JniInvocationImpl* instance); + +/* + * Gets the default library for JNI invocation. The default library is "libart.so". This value may + * be overridden for debuggable builds using the persist.sys.dalvik.vm.lib.2 system property. + * + * The |library| argument is the preferred library to use on debuggable builds (when + * ro.debuggable=1). If the |library| argument is nullptr, then the system preferred value will be + * queried from persist.sys.dalvik.vm.lib.2 if the caller has provided |buffer| argument. + * + * The |buffer| argument is used for reading system properties in debuggable builds. It is + * optional, but should be provisioned to be PROP_VALUE_MAX bytes if provided to ensure it is + * large enough to hold a system property. + * + * Returns the filename of the invocation library determined from the inputs and system + * properties. The returned value may be |library|, |buffer|, or a pointer to a string constant + * "libart.so". + */ +const char* JniInvocationGetLibrary(const char* library, char* buffer); + +/* ---------------------------------- C API for toStringArray.h --------------------------------- */ + +/* + * Allocates a new array for java/lang/String instances with space for |count| elements. Elements + * are initially null. + * + * Returns a new array on success or nullptr in case of failure. This method raises an + * OutOfMemoryError exception if allocation fails. + */ +jobjectArray newStringArray(JNIEnv* env, size_t count); + +/* + * Converts an array of C strings into a managed array of Java strings. The size of the C array is + * determined by the presence of a final element containing a nullptr. + * + * Returns a new array on success or nullptr in case of failure. This method raises an + * OutOfMemoryError exception if allocation fails. + */ +jobjectArray toStringArray(JNIEnv* env, const char* const* strings); + +#ifdef __cplusplus +} +#endif // __cplusplus + +#endif // LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_LIBNATIVEHELPER_API_H_ diff --git a/include/nativehelper/module_api.h b/include/nativehelper/module_api.h deleted file mode 100644 index 8b109e3..0000000 --- a/include/nativehelper/module_api.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (C) 2019 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#ifdef __cplusplus -#define MODULE_API extern "C" -#else -#define MODULE_API -#endif // __cplusplus diff --git a/include/nativehelper/toStringArray.h b/include/nativehelper/toStringArray.h index 1965d6a..02072b0 100644 --- a/include/nativehelper/toStringArray.h +++ b/include/nativehelper/toStringArray.h @@ -14,17 +14,10 @@ * limitations under the License. */ -#ifndef TO_STRING_ARRAY_H_included -#define TO_STRING_ARRAY_H_included +#ifndef LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_TOSTRINGARRAY_H_ +#define LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_TOSTRINGARRAY_H_ -#include <stddef.h> - -#include <jni.h> -#include "module_api.h" - -// Public API for libnativehelper library. -MODULE_API jobjectArray newStringArray(JNIEnv* env, size_t count); -MODULE_API jobjectArray toStringArray(JNIEnv* env, const char* const* strings); +#include "libnativehelper_api.h" #ifdef __cplusplus @@ -75,4 +68,4 @@ inline jobjectArray toStringArray(JNIEnv* env, const std::vector<std::string>& s #endif // __cplusplus -#endif // TO_STRING_ARRAY_H_included +#endif // LIBNATIVEHELPER_INCLUDE_NATIVEHELPER_TOSTRINGARRAY_H_ diff --git a/include_jni/jni.h b/include_jni/jni.h index 4c34313..f709d67 100644 --- a/include_jni/jni.h +++ b/include_jni/jni.h @@ -21,8 +21,8 @@ * Everything here is expected to be VM-neutral. */ -#ifndef JNI_H_ -#define JNI_H_ +#ifndef LIBNATIVEHELPER_INCLUDE_JNI_JNI_H_ +#define LIBNATIVEHELPER_INCLUDE_JNI_JNI_H_ #include <stdarg.h> #include <stdint.h> @@ -1140,4 +1140,4 @@ JNIEXPORT void JNI_OnUnload(JavaVM* vm, void* reserved); #define JNI_COMMIT 1 /* copy content, do not free buffer */ #define JNI_ABORT 2 /* free buffer w/o copying back */ -#endif /* JNI_H_ */ +#endif // LIBNATIVEHELPER_INCLUDE_JNI_JNI_H_ diff --git a/platform_include/nativehelper/detail/signature_checker.h b/platform_include/nativehelper/detail/signature_checker.h index 7c2a7fc..d22c1e2 100644 --- a/platform_include/nativehelper/detail/signature_checker.h +++ b/platform_include/nativehelper/detail/signature_checker.h @@ -32,7 +32,8 @@ * Everything in this file except ostream<< is constexpr. */ -#pragma once +#ifndef LIBNATIVEHELPER_PLATFORM_INCLUDE_NATIVEHELPER_DETAIL_SIGNATURE_CHECKER_H_ +#define LIBNATIVEHELPER_PLATFORM_INCLUDE_NATIVEHELPER_DETAIL_SIGNATURE_CHECKER_H_ #include <iostream> // std::ostream #include <jni.h> // jni typedefs, JniNativeMethod. @@ -1439,3 +1440,4 @@ struct InferJniDescriptor { } // namespace detail } // namespace nativehelper +#endif // LIBNATIVEHELPER_PLATFORM_INCLUDE_NATIVEHELPER_DETAIL_SIGNATURE_CHECKER_H_ diff --git a/platform_include/nativehelper/jni_macros.h b/platform_include/nativehelper/jni_macros.h index da01e6f..b6f6a4b 100644 --- a/platform_include/nativehelper/jni_macros.h +++ b/platform_include/nativehelper/jni_macros.h @@ -141,8 +141,8 @@ * the stricter approach is taken: the most exact C++ type must be used. */ -#ifndef NATIVEHELPER_JNI_MACROS_H -#define NATIVEHELPER_JNI_MACROS_H +#ifndef LIBNATIVEHELPER_PLATFORM_INCLUDE_NATIVEHELPER_JNI_MACROS_H_ +#define LIBNATIVEHELPER_PLATFORM_INCLUDE_NATIVEHELPER_JNI_MACROS_H_ // The below basic macros do not perform automatic stringification, // invoked e.g. as MAKE_JNI_NATIVE_METHOD("some_name", "()V", void_fn) @@ -282,4 +282,4 @@ (to) #endif -#endif // NATIVEHELPER_JNI_MACROS_H +#endif // LIBNATIVEHELPER_PLATFORM_INCLUDE_NATIVEHELPER_JNI_MACROS_H_ diff --git a/tests/jni_gtest/base/nativehelper/jni_gtest.h b/tests/jni_gtest/base/nativehelper/jni_gtest.h index 975d56c..bed1ae0 100644 --- a/tests/jni_gtest/base/nativehelper/jni_gtest.h +++ b/tests/jni_gtest/base/nativehelper/jni_gtest.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef LIBNATIVEHELPER_TESTS_JNI_GTEST_H -#define LIBNATIVEHELPER_TESTS_JNI_GTEST_H +#ifndef LIBNATIVEHELPER_TESTS_JNI_GTEST_BASE_NATIVEHELPER_JNI_GTEST_H_ +#define LIBNATIVEHELPER_TESTS_JNI_GTEST_BASE_NATIVEHELPER_JNI_GTEST_H_ #include <memory> @@ -123,4 +123,4 @@ protected: } // namespace android -#endif // LIBNATIVEHELPER_TESTS_JNI_GTEST_H +#endif // LIBNATIVEHELPER_TESTS_JNI_GTEST_BASE_NATIVEHELPER_JNI_GTEST_H_ diff --git a/toStringArray.cpp b/toStringArray.cpp index b1f0f42..0c6a1a9 100644 --- a/toStringArray.cpp +++ b/toStringArray.cpp @@ -42,11 +42,11 @@ struct ArrayGetter { } // namespace -MODULE_API jobjectArray newStringArray(JNIEnv* env, size_t count) { +jobjectArray newStringArray(JNIEnv* env, size_t count) { return env->NewObjectArray(count, JniConstants::GetStringClass(env), nullptr); } -MODULE_API jobjectArray toStringArray(JNIEnv* env, const char* const* strings) { +jobjectArray toStringArray(JNIEnv* env, const char* const* strings) { ArrayCounter counter(strings); ArrayGetter getter(strings); return toStringArray(env, &counter, &getter); |
