Skip to content

Commit

Permalink
Avoid polymorphic dispatch over JNI
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Dario Valdespino <[email protected]>
  • Loading branch information
sgammon committed Jun 13, 2024
1 parent a311e52 commit cf08aa6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 58 deletions.
2 changes: 1 addition & 1 deletion common.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<property name="jni.revision" value="2"/>
<property name="jni.build" value="0"/> <!--${build.number}-->
<property name="jni.version" value="${jni.major}.${jni.minor}.${jni.revision}"/>
<property name="jni.md5" value="eea709d4f4531903eb04ab51ff3840ce"/>
<property name="jni.md5" value="2758dad3ef270c95b39b70ca30de3941"/>
<property name="spec.title" value="Java Native Access (JNA)"/>
<property name="spec.vendor" value="${vendor}"/>
<property name="spec.version" value="${jna.major}"/>
Expand Down
58 changes: 29 additions & 29 deletions native/dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -2347,21 +2347,21 @@ 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);
}

/*
* 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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -2458,21 +2458,21 @@ 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);
}

/*
* 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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions src/com/sun/jna/Native.java
Original file line number Diff line number Diff line change
Expand Up @@ -2229,33 +2229,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);

Expand Down
28 changes: 14 additions & 14 deletions src/com/sun/jna/Pointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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.
Expand Down

0 comments on commit cf08aa6

Please sign in to comment.