From 5c434282fb2a6261dbf6ba068f2016efbabd0043 Mon Sep 17 00:00:00 2001 From: Sam Gammon Date: Wed, 12 Jun 2024 20:43:17 -0700 Subject: [PATCH] Avoid polymorphic dispatch over JNI When linked statically on GraalVM, JNI symbols declared in the overloaded form cannot be resolved. Luckily, all of `Native`'s callsites are in `Pointer` or itself, and all `native` methods of `Native` are non-public. This PR adjusts the JNA C API to avoid using overloaded `read`, `write`, or `getDirectByteBuffer`. Callsites are amended in `Pointer` accordingly. Signed-off-by: Sam Gammon Signed-off-by: Dario Valdespino --- common.xml | 2 +- native/dispatch.c | 58 ++++++++++++++++++------------------ src/com/sun/jna/Native.java | 28 ++++++++--------- src/com/sun/jna/Pointer.java | 28 ++++++++--------- 4 files changed, 58 insertions(+), 58 deletions(-) diff --git a/common.xml b/common.xml index bc1c63b32..eada46b5d 100644 --- a/common.xml +++ b/common.xml @@ -27,7 +27,7 @@ - + diff --git a/native/dispatch.c b/native/dispatch.c index 29f944944..69e2f1a8e 100644 --- a/native/dispatch.c +++ b/native/dispatch.c @@ -2334,10 +2334,10 @@ Java_com_sun_jna_Native_findSymbol(JNIEnv *env, jclass UNUSED(cls), /* * Class: com_sun_jna_Native - * Method: write + * Method: writeBytes * Signature: (Lcom/sun/jna/Pointer;JJ[BII)V */ -JNIEXPORT void JNICALL Java_com_sun_jna_Native_write__Lcom_sun_jna_Pointer_2JJ_3BII +JNIEXPORT void JNICALL Java_com_sun_jna_Native_writeBytes (JNIEnv *env, jclass UNUSED(cls), jobject UNUSED(pointer), jlong addr, jlong offset, jbyteArray arr, jint off, jint n) { PSTART(); @@ -2347,10 +2347,10 @@ JNIEXPORT void JNICALL Java_com_sun_jna_Native_write__Lcom_sun_jna_Pointer_2JJ_3 /* * Class: com_sun_jna_Native - * Method: write + * Method: writeChars * Signature: (Lcom/sun/jna/Pointer;JJ[CII)V */ -JNIEXPORT void JNICALL Java_com_sun_jna_Native_write__Lcom_sun_jna_Pointer_2JJ_3CII +JNIEXPORT void JNICALL Java_com_sun_jna_Native_writeChars (JNIEnv *env, jclass UNUSED(cls), jobject UNUSED(pointer), jlong addr, jlong offset, jcharArray arr, jint off, jint n) { getChars(env, (wchar_t*)L2A(addr + offset), arr, off, n); @@ -2358,10 +2358,10 @@ JNIEXPORT void JNICALL Java_com_sun_jna_Native_write__Lcom_sun_jna_Pointer_2JJ_3 /* * Class: com_sun_jna_Native - * Method: write + * Method: writeDoubles * Signature: (Lcom/sun/jna/Pointer;JJ[DII)V */ -JNIEXPORT void JNICALL Java_com_sun_jna_Native_write__Lcom_sun_jna_Pointer_2JJ_3DII +JNIEXPORT void JNICALL Java_com_sun_jna_Native_writeDoubles (JNIEnv *env, jclass UNUSED(cls), jobject UNUSED(pointer), jlong addr, jlong offset, jdoubleArray arr, jint off, jint n) { PSTART(); @@ -2371,10 +2371,10 @@ JNIEXPORT void JNICALL Java_com_sun_jna_Native_write__Lcom_sun_jna_Pointer_2JJ_3 /* * Class: com_sun_jna_Native - * Method: write + * Method: writeFloats * Signature: (Lcom/sun/jna/Pointer;JJ[FII)V */ -JNIEXPORT void JNICALL Java_com_sun_jna_Native_write__Lcom_sun_jna_Pointer_2JJ_3FII +JNIEXPORT void JNICALL Java_com_sun_jna_Native_writeFloats (JNIEnv *env, jclass UNUSED(cls), jobject UNUSED(pointer), jlong addr, jlong offset, jfloatArray arr, jint off, jint n) { PSTART(); @@ -2384,10 +2384,10 @@ JNIEXPORT void JNICALL Java_com_sun_jna_Native_write__Lcom_sun_jna_Pointer_2JJ_3 /* * Class: com_sun_jna_Native - * Method: write + * Method: writeInts * Signature: (Lcom/sun/jna/Pointer;JJ[III)V */ -JNIEXPORT void JNICALL Java_com_sun_jna_Native_write__Lcom_sun_jna_Pointer_2JJ_3III +JNIEXPORT void JNICALL Java_com_sun_jna_Native_writeInts (JNIEnv *env, jclass UNUSED(cls), jobject UNUSED(pointer), jlong addr, jlong offset, jintArray arr, jint off, jint n) { PSTART(); @@ -2397,10 +2397,10 @@ JNIEXPORT void JNICALL Java_com_sun_jna_Native_write__Lcom_sun_jna_Pointer_2JJ_3 /* * Class: com_sun_jna_Native - * Method: write + * Method: writeLongs * Signature: (Lcom/sun/jna/Pointer;JJ[JII)V */ -JNIEXPORT void JNICALL Java_com_sun_jna_Native_write__Lcom_sun_jna_Pointer_2JJ_3JII +JNIEXPORT void JNICALL Java_com_sun_jna_Native_writeLongs (JNIEnv *env, jclass UNUSED(cls), jobject UNUSED(pointer), jlong addr, jlong offset, jlongArray arr, jint off, jint n) { PSTART(); @@ -2410,10 +2410,10 @@ JNIEXPORT void JNICALL Java_com_sun_jna_Native_write__Lcom_sun_jna_Pointer_2JJ_3 /* * Class: com_sun_jna_Native - * Method: write + * Method: writeShorts * Signature: (Lcom/sun/jna/Pointer;JJ[SII)V */ -JNIEXPORT void JNICALL Java_com_sun_jna_Native_write__Lcom_sun_jna_Pointer_2JJ_3SII +JNIEXPORT void JNICALL Java_com_sun_jna_Native_writeShorts (JNIEnv *env, jclass UNUSED(cls), jobject UNUSED(pointer), jlong addr, jlong offset, jshortArray arr, jint off, jint n) { PSTART(); @@ -2445,10 +2445,10 @@ JNIEXPORT jlong JNICALL Java_com_sun_jna_Native_indexOf /* * Class: com_sun_jna_Native - * Method: read + * Method: readBytes * Signature: (Lcom/sun/jna/Pointer;JJ[BII)V */ -JNIEXPORT void JNICALL Java_com_sun_jna_Native_read__Lcom_sun_jna_Pointer_2JJ_3BII +JNIEXPORT void JNICALL Java_com_sun_jna_Native_readBytes (JNIEnv *env, jclass UNUSED(cls), jobject UNUSED(pointer), jlong addr, jlong offset, jbyteArray arr, jint off, jint n) { PSTART(); @@ -2458,10 +2458,10 @@ JNIEXPORT void JNICALL Java_com_sun_jna_Native_read__Lcom_sun_jna_Pointer_2JJ_3B /* * Class: com_sun_jna_Native - * Method: read + * Method: readChars * Signature: (Lcom/sun/jna/Pointer;JJ[CII)V */ -JNIEXPORT void JNICALL Java_com_sun_jna_Native_read__Lcom_sun_jna_Pointer_2JJ_3CII +JNIEXPORT void JNICALL Java_com_sun_jna_Native_readChars (JNIEnv *env, jclass UNUSED(cls), jobject UNUSED(pointer), jlong addr, jlong offset, jcharArray arr, jint off, jint n) { setChars(env, (wchar_t*)L2A(addr + offset), arr, off, n); @@ -2469,10 +2469,10 @@ JNIEXPORT void JNICALL Java_com_sun_jna_Native_read__Lcom_sun_jna_Pointer_2JJ_3C /* * Class: com_sun_jna_Native - * Method: read + * Method: readDoubles * Signature: (Lcom/sun/jna/Pointer;JJ[DII)V */ -JNIEXPORT void JNICALL Java_com_sun_jna_Native_read__Lcom_sun_jna_Pointer_2JJ_3DII +JNIEXPORT void JNICALL Java_com_sun_jna_Native_readDoubles (JNIEnv *env, jclass UNUSED(cls), jobject UNUSED(pointer), jlong addr, jlong offset, jdoubleArray arr, jint off, jint n) { PSTART(); @@ -2482,10 +2482,10 @@ JNIEXPORT void JNICALL Java_com_sun_jna_Native_read__Lcom_sun_jna_Pointer_2JJ_3D /* * Class: com_sun_jna_Native - * Method: read + * Method: readFloats * Signature: (Lcom/sun/jna/Pointer;JJ[FII)V */ -JNIEXPORT void JNICALL Java_com_sun_jna_Native_read__Lcom_sun_jna_Pointer_2JJ_3FII +JNIEXPORT void JNICALL Java_com_sun_jna_Native_readFloats (JNIEnv *env, jclass UNUSED(cls), jobject UNUSED(pointer), jlong addr, jlong offset, jfloatArray arr, jint off, jint n) { PSTART(); @@ -2495,10 +2495,10 @@ JNIEXPORT void JNICALL Java_com_sun_jna_Native_read__Lcom_sun_jna_Pointer_2JJ_3F /* * Class: com_sun_jna_Native - * Method: read + * Method: readInts * Signature: (Lcom/sun/jna/Pointer;JJ[III)V */ -JNIEXPORT void JNICALL Java_com_sun_jna_Native_read__Lcom_sun_jna_Pointer_2JJ_3III +JNIEXPORT void JNICALL Java_com_sun_jna_Native_readInts (JNIEnv *env, jclass UNUSED(cls), jobject UNUSED(pointer), jlong addr, jlong offset, jintArray arr, jint off, jint n) { PSTART(); @@ -2508,10 +2508,10 @@ JNIEXPORT void JNICALL Java_com_sun_jna_Native_read__Lcom_sun_jna_Pointer_2JJ_3I /* * Class: com_sun_jna_Native - * Method: read + * Method: readLongs * Signature: (Lcom/sun/jna/Pointer;JJ[JII)V */ -JNIEXPORT void JNICALL Java_com_sun_jna_Native_read__Lcom_sun_jna_Pointer_2JJ_3JII +JNIEXPORT void JNICALL Java_com_sun_jna_Native_readLongs (JNIEnv *env, jclass UNUSED(cls), jobject UNUSED(pointer), jlong addr, jlong offset, jlongArray arr, jint off, jint n) { PSTART(); @@ -2521,10 +2521,10 @@ JNIEXPORT void JNICALL Java_com_sun_jna_Native_read__Lcom_sun_jna_Pointer_2JJ_3J /* * Class: com_sun_jna_Native - * Method: read + * Method: readShorts * Signature: (Lcom/sun/jna/Pointer;JJ[SII)V */ -JNIEXPORT void JNICALL Java_com_sun_jna_Native_read__Lcom_sun_jna_Pointer_2JJ_3SII +JNIEXPORT void JNICALL Java_com_sun_jna_Native_readShorts (JNIEnv *env, jclass UNUSED(cls), jobject UNUSED(pointer), jlong addr, jlong offset, jshortArray arr, jint off, jint n) { PSTART(); @@ -2576,7 +2576,7 @@ JNIEXPORT jlong JNICALL Java_com_sun_jna_Native__1getPointer * Method: getDirectByteBuffer * Signature: (Lcom/sun/jna/Pointer;JJJ)Ljava/nio/ByteBuffer; */ -JNIEXPORT jobject JNICALL Java_com_sun_jna_Native_getDirectByteBuffer__Lcom_sun_jna_Pointer_2JJJ +JNIEXPORT jobject JNICALL Java_com_sun_jna_Native_getDirectByteBuffer (JNIEnv *env, jclass UNUSED(cls), jobject UNUSED(pointer), jlong addr, jlong offset, jlong length) { #ifdef NO_NIO_BUFFERS diff --git a/src/com/sun/jna/Native.java b/src/com/sun/jna/Native.java index 66624a13d..3948746c2 100644 --- a/src/com/sun/jna/Native.java +++ b/src/com/sun/jna/Native.java @@ -2230,33 +2230,33 @@ static long open(String name) { */ static native long indexOf(Pointer pointer, long baseaddr, long offset, byte value); - static native void read(Pointer pointer, long baseaddr, long offset, byte[] buf, int index, int length); + static native void readBytes(Pointer pointer, long baseaddr, long offset, byte[] buf, int index, int length); - static native void read(Pointer pointer, long baseaddr, long offset, short[] buf, int index, int length); + static native void readShorts(Pointer pointer, long baseaddr, long offset, short[] buf, int index, int length); - static native void read(Pointer pointer, long baseaddr, long offset, char[] buf, int index, int length); + static native void readChars(Pointer pointer, long baseaddr, long offset, char[] buf, int index, int length); - static native void read(Pointer pointer, long baseaddr, long offset, int[] buf, int index, int length); + static native void readInts(Pointer pointer, long baseaddr, long offset, int[] buf, int index, int length); - static native void read(Pointer pointer, long baseaddr, long offset, long[] buf, int index, int length); + static native void readLongs(Pointer pointer, long baseaddr, long offset, long[] buf, int index, int length); - static native void read(Pointer pointer, long baseaddr, long offset, float[] buf, int index, int length); + static native void readFloats(Pointer pointer, long baseaddr, long offset, float[] buf, int index, int length); - static native void read(Pointer pointer, long baseaddr, long offset, double[] buf, int index, int length); + static native void readDoubles(Pointer pointer, long baseaddr, long offset, double[] buf, int index, int length); - static native void write(Pointer pointer, long baseaddr, long offset, byte[] buf, int index, int length); + static native void writeBytes(Pointer pointer, long baseaddr, long offset, byte[] buf, int index, int length); - static native void write(Pointer pointer, long baseaddr, long offset, short[] buf, int index, int length); + static native void writeShorts(Pointer pointer, long baseaddr, long offset, short[] buf, int index, int length); - static native void write(Pointer pointer, long baseaddr, long offset, char[] buf, int index, int length); + static native void writeChars(Pointer pointer, long baseaddr, long offset, char[] buf, int index, int length); - static native void write(Pointer pointer, long baseaddr, long offset, int[] buf, int index, int length); + static native void writeInts(Pointer pointer, long baseaddr, long offset, int[] buf, int index, int length); - static native void write(Pointer pointer, long baseaddr, long offset, long[] buf, int index, int length); + static native void writeLongs(Pointer pointer, long baseaddr, long offset, long[] buf, int index, int length); - static native void write(Pointer pointer, long baseaddr, long offset, float[] buf, int index, int length); + static native void writeFloats(Pointer pointer, long baseaddr, long offset, float[] buf, int index, int length); - static native void write(Pointer pointer, long baseaddr, long offset, double[] buf, int index, int length); + static native void writeDoubles(Pointer pointer, long baseaddr, long offset, double[] buf, int index, int length); static native byte getByte(Pointer pointer, long baseaddr, long offset); diff --git a/src/com/sun/jna/Pointer.java b/src/com/sun/jna/Pointer.java index 444035f51..3dad56909 100644 --- a/src/com/sun/jna/Pointer.java +++ b/src/com/sun/jna/Pointer.java @@ -137,7 +137,7 @@ public long indexOf(long offset, byte value) { * @param length number of elements from native pointer that must be copied */ public void read(long offset, byte[] buf, int index, int length) { - Native.read(this, this.peer, offset, buf, index, length); + Native.readBytes(this, this.peer, offset, buf, index, length); } /** @@ -150,7 +150,7 @@ public void read(long offset, byte[] buf, int index, int length) { * @param length number of elements from native pointer that must be copied */ public void read(long offset, short[] buf, int index, int length) { - Native.read(this, this.peer, offset, buf, index, length); + Native.readShorts(this, this.peer, offset, buf, index, length); } /** @@ -163,7 +163,7 @@ public void read(long offset, short[] buf, int index, int length) { * @param length number of elements from native pointer that must be copied */ public void read(long offset, char[] buf, int index, int length) { - Native.read(this, this.peer, offset, buf, index, length); + Native.readChars(this, this.peer, offset, buf, index, length); } /** @@ -176,7 +176,7 @@ public void read(long offset, char[] buf, int index, int length) { * @param length number of elements from native pointer that must be copied */ public void read(long offset, int[] buf, int index, int length) { - Native.read(this, this.peer, offset, buf, index, length); + Native.readInts(this, this.peer, offset, buf, index, length); } /** @@ -189,7 +189,7 @@ public void read(long offset, int[] buf, int index, int length) { * @param length number of elements from native pointer that must be copied */ public void read(long offset, long[] buf, int index, int length) { - Native.read(this, this.peer, offset, buf, index, length); + Native.readLongs(this, this.peer, offset, buf, index, length); } /** @@ -202,7 +202,7 @@ public void read(long offset, long[] buf, int index, int length) { * @param length number of elements from native pointer that must be copied */ public void read(long offset, float[] buf, int index, int length) { - Native.read(this, this.peer, offset, buf, index, length); + Native.readFloats(this, this.peer, offset, buf, index, length); } /** @@ -215,7 +215,7 @@ public void read(long offset, float[] buf, int index, int length) { * @param length number of elements from native pointer that must be copied */ public void read(long offset, double[] buf, int index, int length) { - Native.read(this, this.peer, offset, buf, index, length); + Native.readDoubles(this, this.peer, offset, buf, index, length); } /** @@ -254,7 +254,7 @@ public void read(long offset, Pointer[] buf, int index, int length) { * copied */ public void write(long offset, byte[] buf, int index, int length) { - Native.write(this, this.peer, offset, buf, index, length); + Native.writeBytes(this, this.peer, offset, buf, index, length); } /** @@ -268,7 +268,7 @@ public void write(long offset, byte[] buf, int index, int length) { * copied */ public void write(long offset, short[] buf, int index, int length) { - Native.write(this, this.peer, offset, buf, index, length); + Native.writeShorts(this, this.peer, offset, buf, index, length); } /** @@ -282,7 +282,7 @@ public void write(long offset, short[] buf, int index, int length) { * copied */ public void write(long offset, char[] buf, int index, int length) { - Native.write(this, this.peer, offset, buf, index, length); + Native.writeChars(this, this.peer, offset, buf, index, length); } /** @@ -296,7 +296,7 @@ public void write(long offset, char[] buf, int index, int length) { * copied */ public void write(long offset, int[] buf, int index, int length) { - Native.write(this, this.peer, offset, buf, index, length); + Native.writeInts(this, this.peer, offset, buf, index, length); } /** @@ -310,7 +310,7 @@ public void write(long offset, int[] buf, int index, int length) { * copied */ public void write(long offset, long[] buf, int index, int length) { - Native.write(this, this.peer, offset, buf, index, length); + Native.writeLongs(this, this.peer, offset, buf, index, length); } /** @@ -324,7 +324,7 @@ public void write(long offset, long[] buf, int index, int length) { * copied */ public void write(long offset, float[] buf, int index, int length) { - Native.write(this, this.peer, offset, buf, index, length); + Native.writeFloats(this, this.peer, offset, buf, index, length); } /** @@ -338,7 +338,7 @@ public void write(long offset, float[] buf, int index, int length) { * copied */ public void write(long offset, double[] buf, int index, int length) { - Native.write(this, this.peer, offset, buf, index, length); + Native.writeDoubles(this, this.peer, offset, buf, index, length); } /** Write the given array of Pointer to native memory.