aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOrion Hodson <oth@google.com>2021-05-19 14:17:40 +0100
committerOrion Hodson <oth@google.com>2021-05-20 13:39:02 +0100
commitabaf7cbf92e9f783b3be0890d58a4ee04cd3aaf2 (patch)
tree63bb557903ad8f738087f529d0077cf89d466c94
parenta3d2b6360d33762203b86d9417e2f02f7a6509d2 (diff)
downloadlibnativehelper-abaf7cbf92e9f783b3be0890d58a4ee04cd3aaf2.tar.gz
libnativehelper: Retire legacy FileDescriptor methods
Implements the following methods: * jniCreateFileDescriptor * jniGetFDFromFileDescriptor * jniSetFileDescriptorOfFD in terms of NDK AFileDescriptor API. Bug: 188067586 Test: m Change-Id: Ifbcb63982c24cca63ed2f23444c4ada0b315fd42
-rw-r--r--Android.bp7
-rw-r--r--JNIPlatformHelp.c30
-rw-r--r--include/android/file_descriptor_jni.h4
-rw-r--r--include_platform/nativehelper/JNIPlatformHelp.h36
-rw-r--r--libnativehelper.map.txt3
-rw-r--r--libnativehelper_lazy.c21
-rw-r--r--tests/libnativehelper_lazy_test.cpp3
7 files changed, 22 insertions, 82 deletions
diff --git a/Android.bp b/Android.bp
index 0c7c6fe..0bcd9f0 100644
--- a/Android.bp
+++ b/Android.bp
@@ -110,6 +110,7 @@ cc_library_shared {
"JNIPlatformHelp.c",
"JniConstants.c",
"JniInvocation.c",
+ "file_descriptor_jni.c",
],
export_include_dirs: [
"header_only_include",
@@ -123,12 +124,6 @@ cc_library_shared {
symbol_file: "libnativehelper.map.txt",
versions: ["S"],
},
- target: {
- android: {
- // FileDescriptor NDK API is Android-only.
- srcs: ["file_descriptor_jni.c"],
- },
- },
// Only distributed in the ART Module.
apex_available: [
"com.android.art",
diff --git a/JNIPlatformHelp.c b/JNIPlatformHelp.c
index d80b048..464e964 100644
--- a/JNIPlatformHelp.c
+++ b/JNIPlatformHelp.c
@@ -32,36 +32,6 @@ static int GetBufferElementSizeShift(JNIEnv* env, jobject nioBuffer) {
return(*env)->GetIntField(env, nioBuffer, JniConstants_NioBuffer__elementSizeShift(env));
}
-jobject jniCreateFileDescriptor(JNIEnv* env, int fd) {
- jobject fileDescriptor = (*env)->NewObject(env,
- JniConstants_FileDescriptorClass(env),
- JniConstants_FileDescriptor_init(env));
- // NOTE: NewObject ensures that an OutOfMemoryError will be seen by the Java
- // caller if the alloc fails, so we just return nullptr when that happens.
- if (fileDescriptor != NULL) {
- jniSetFileDescriptorOfFD(env, fileDescriptor, fd);
- }
- return fileDescriptor;
-}
-
-int jniGetFDFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) {
- if (fileDescriptor != NULL) {
- return (*env)->GetIntField(env, fileDescriptor,
- JniConstants_FileDescriptor_descriptor(env));
- } else {
- return -1;
- }
-}
-
-void jniSetFileDescriptorOfFD(JNIEnv* env, jobject fileDescriptor, int value) {
- if (fileDescriptor == NULL) {
- jniThrowNullPointerException(env, "null FileDescriptor");
- } else {
- (*env)->SetIntField(env,
- fileDescriptor, JniConstants_FileDescriptor_descriptor(env), value);
- }
-}
-
jarray jniGetNioBufferBaseArray(JNIEnv* env, jobject nioBuffer) {
jclass nioAccessClass = JniConstants_NIOAccessClass(env);
jmethodID getBaseArrayMethod = JniConstants_NIOAccess_getBaseArray(env);
diff --git a/include/android/file_descriptor_jni.h b/include/android/file_descriptor_jni.h
index fbdfef5..26529b9 100644
--- a/include/android/file_descriptor_jni.h
+++ b/include/android/file_descriptor_jni.h
@@ -29,6 +29,10 @@
#include <jni.h>
+#if !defined(__BIONIC__) && !defined(__INTRODUCED_IN)
+#define __INTRODUCED_IN(x)
+#endif
+
__BEGIN_DECLS
/**
diff --git a/include_platform/nativehelper/JNIPlatformHelp.h b/include_platform/nativehelper/JNIPlatformHelp.h
index 466032b..70f6136 100644
--- a/include_platform/nativehelper/JNIPlatformHelp.h
+++ b/include_platform/nativehelper/JNIPlatformHelp.h
@@ -31,22 +31,6 @@
__BEGIN_DECLS
/*
- * 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);
-
-/*
* Gets the managed heap array backing a java.nio.Buffer instance.
*
* Returns nullptr if there is no array backing.
@@ -100,18 +84,32 @@ __END_DECLS
* For C++ code, we provide inlines that map to the C functions. g++ always
* inlines these, even on non-optimized builds.
*/
+
#if defined(__cplusplus)
+#include <android/file_descriptor_jni.h>
+
inline jobject jniCreateFileDescriptor(JNIEnv* env, int fd) {
- return jniCreateFileDescriptor(&env->functions, fd);
+ jobject fileDescriptor = AFileDescriptor_create(env);
+ if (fileDescriptor != nullptr) {
+ AFileDescriptor_setFd(env, fileDescriptor, fd);
+ }
+ return fileDescriptor;
}
inline int jniGetFDFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) {
- return jniGetFDFromFileDescriptor(&env->functions, fileDescriptor);
+ if (fileDescriptor == nullptr) {
+ return -1;
+ }
+ return AFileDescriptor_getFd(env, fileDescriptor);
}
inline void jniSetFileDescriptorOfFD(JNIEnv* env, jobject fileDescriptor, int value) {
- jniSetFileDescriptorOfFD(&env->functions, fileDescriptor, value);
+ if (fileDescriptor == nullptr) {
+ jniThrowNullPointerException(env, "fileDescriptor is null");
+ return;
+ }
+ AFileDescriptor_setFd(env, fileDescriptor, value);
}
inline jarray jniGetNioBufferBaseArray(JNIEnv* env, jobject nioBuffer) {
diff --git a/libnativehelper.map.txt b/libnativehelper.map.txt
index c23ddc2..0dd0781 100644
--- a/libnativehelper.map.txt
+++ b/libnativehelper.map.txt
@@ -11,9 +11,6 @@ LIBNATIVEHELPER_S { # introduced=S
JNI_GetCreatedJavaVMs;
# API for platform and modules only.
- jniCreateFileDescriptor; # apex
- jniGetFDFromFileDescriptor; # apex
- jniSetFileDescriptorOfFD; # apex
jniGetNioBufferBaseArray; # apex
jniGetNioBufferBaseArrayOffset; # apex
jniGetNioBufferPointer; # apex
diff --git a/libnativehelper_lazy.c b/libnativehelper_lazy.c
index 5885fe1..2a1c4df 100644
--- a/libnativehelper_lazy.c
+++ b/libnativehelper_lazy.c
@@ -48,13 +48,10 @@ enum MethodIndex {
k_JNI_GetDefaultJavaVMInitArgs,
// Methods in JNIPlatformHelp.h.
- k_jniCreateFileDescriptor,
- k_jniGetFDFromFileDescriptor,
k_jniGetNioBufferBaseArray,
k_jniGetNioBufferBaseArrayOffset,
k_jniGetNioBufferFields,
k_jniGetNioBufferPointer,
- k_jniSetFileDescriptorOfFD,
k_jniUninitializeConstants,
// Methods in JniInvocation.h.
@@ -120,13 +117,10 @@ static void InitializeOnce() {
BIND_SYMBOL(JNI_GetDefaultJavaVMInitArgs);
// Methods in JNIPlatformHelp.h.
- BIND_SYMBOL(jniCreateFileDescriptor);
- BIND_SYMBOL(jniGetFDFromFileDescriptor);
BIND_SYMBOL(jniGetNioBufferBaseArray);
BIND_SYMBOL(jniGetNioBufferBaseArrayOffset);
BIND_SYMBOL(jniGetNioBufferFields);
BIND_SYMBOL(jniGetNioBufferPointer);
- BIND_SYMBOL(jniSetFileDescriptorOfFD);
BIND_SYMBOL(jniUninitializeConstants);
// Methods in JniInvocation.h.
@@ -229,16 +223,6 @@ jint JNI_GetCreatedJavaVMs(JavaVM** p_vm, jsize vm_max, jsize* p_vm_count) {
// Forwarding for methods in JNIPlatformHelp.h.
//
-jobject jniCreateFileDescriptor(JNIEnv* env, int fd) {
- typedef jobject (*M)(JNIEnv*, int);
- INVOKE_METHOD(jniCreateFileDescriptor, M, env, fd);
-}
-
-int jniGetFDFromFileDescriptor(JNIEnv* env, jobject fileDescriptor) {
- typedef int (*M)(JNIEnv*, jobject);
- INVOKE_METHOD(jniGetFDFromFileDescriptor, M, env, fileDescriptor);
-}
-
jarray jniGetNioBufferBaseArray(JNIEnv* env, jobject nioBuffer) {
typedef jarray (*M)(JNIEnv*, jobject);
INVOKE_METHOD(jniGetNioBufferBaseArray, M, env, nioBuffer);
@@ -261,11 +245,6 @@ jlong jniGetNioBufferPointer(JNIEnv* env, jobject nioBuffer) {
INVOKE_METHOD(jniGetNioBufferPointer, M, env, nioBuffer);
}
-void jniSetFileDescriptorOfFD(JNIEnv* env, jobject fileDescriptor, int value) {
- typedef void (*M)(JNIEnv*, jobject, int);
- INVOKE_VOID_METHOD(jniSetFileDescriptorOfFD, M, env, fileDescriptor, value);
-}
-
void jniUninitializeConstants() {
typedef void (*M)();
INVOKE_VOID_METHOD(jniUninitializeConstants, M);
diff --git a/tests/libnativehelper_lazy_test.cpp b/tests/libnativehelper_lazy_test.cpp
index f7668f2..fd7c0f2 100644
--- a/tests/libnativehelper_lazy_test.cpp
+++ b/tests/libnativehelper_lazy_test.cpp
@@ -37,13 +37,10 @@ static const char* kLoadFailed = "Failed to load libnativehelper.so";
TEST_F(LibnativehelperLazyTest, NoLibnativehelperIsForJNIPlatformHelp) {
C_JNIEnv* env = NULL;
- EXPECT_DEATH(jniCreateFileDescriptor(env, 0), kLoadFailed);
- EXPECT_DEATH(jniGetFDFromFileDescriptor(env, NULL), kLoadFailed);
EXPECT_DEATH(jniGetNioBufferBaseArray(env, NULL), kLoadFailed);
EXPECT_DEATH(jniGetNioBufferBaseArrayOffset(env, NULL), kLoadFailed);
EXPECT_DEATH(jniGetNioBufferFields(env, NULL, NULL, NULL, NULL), kLoadFailed);
EXPECT_DEATH(jniGetNioBufferPointer(env, NULL), kLoadFailed);
- EXPECT_DEATH(jniSetFileDescriptorOfFD(env, NULL, 1), kLoadFailed);
EXPECT_DEATH(jniUninitializeConstants(), kLoadFailed);
}