Skip to content

Commit

Permalink
Merge pull request #1504 from andrew-nowak/expand-max-fixedargs
Browse files Browse the repository at this point in the history
Increase maximum supported fixed args on varargs calls from 3 to 255
  • Loading branch information
matthiasblaesing authored Feb 23, 2023
2 parents 54fc662 + 767662a commit c9f062a
Show file tree
Hide file tree
Showing 39 changed files with 61 additions and 13 deletions.
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ Features
Bug Fixes
---------
* [#1501](https://github.com/java-native-access/jna/pull/1501): `Library.OPTION_STRING_ENCODING` is ignore for string arguments function calls - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#1504](https://github.com/java-native-access/jna/pull/1504): Increase maximum supported fixed args on varargs calls from 3 to 255 - [@andrew-nowak](https://github.com/andrew-nowak).

Important Changes
-----------------
* The interfaces between Java and native code have changed, so `libjnidispatch`
must be rebuilt to be compatible with this release.

Release (5.13.0)
================
Expand Down
14 changes: 10 additions & 4 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@
<property name="jna.version" value="${jna.major}.${jna.minor}.${jna.revision}${version.suffix}"/>
<property name="osgi.version" value="${jna.major}.${jna.minor}.${jna.revision}"/>
<!-- jnidispatch library release version -->
<property name="jni.major" value="6"/>
<property name="jni.minor" value="1"/>
<property name="jni.revision" value="6"/>
<property name="jni.major" value="7"/>
<property name="jni.minor" value="0"/>
<property name="jni.revision" value="0"/>
<property name="jni.build" value="0"/> <!--${build.number}-->
<property name="jni.version" value="${jni.major}.${jni.minor}.${jni.revision}"/>
<property name="jni.md5" value="147a998f0cbc89681a1ae6c0dd121629"/>
<property name="jni.md5" value="5fb98531302accd485c534c452dd952a"/>
<property name="spec.title" value="Java Native Access (JNA)"/>
<property name="spec.vendor" value="${vendor}"/>
<property name="spec.version" value="${jna.major}"/>
Expand Down Expand Up @@ -818,6 +818,12 @@ osname=macosx;processor=aarch64
<arg value="JNIEXPORT"/>
<fileset dir="${build.headers}" includes="*.h"/>
</apply>
<apply dir="${build.headers}" executable="${grep}" parallel="true"
failonerror="${grep.required}" relative="true" output="${md5.file}"
error="${md5.file}.error" append="true">
<arg value="#define"/>
<fileset dir="${build.headers}" includes="*.h"/>
</apply>
<!-- Clean up gcj javah output to match that of Sun's javah -->
<fixcrlf file="${md5.file}" eol="unix"/>
<replaceregexp match="^((.*\.h):JNIEXPORT[^(]+)( +\(.*)"
Expand Down
Binary file modified lib/native/aix-ppc.jar
Binary file not shown.
Binary file modified lib/native/aix-ppc64.jar
Binary file not shown.
Binary file modified lib/native/android-aarch64.jar
Binary file not shown.
Binary file modified lib/native/android-arm.jar
Binary file not shown.
Binary file modified lib/native/android-armv7.jar
Binary file not shown.
Binary file modified lib/native/android-mips.jar
Binary file not shown.
Binary file modified lib/native/android-mips64.jar
Binary file not shown.
Binary file modified lib/native/android-x86-64.jar
Binary file not shown.
Binary file modified lib/native/android-x86.jar
Binary file not shown.
Binary file modified lib/native/darwin-aarch64.jar
Binary file not shown.
Binary file modified lib/native/darwin-x86-64.jar
Binary file not shown.
Binary file modified lib/native/freebsd-x86-64.jar
Binary file not shown.
Binary file modified lib/native/freebsd-x86.jar
Binary file not shown.
Binary file modified lib/native/linux-aarch64.jar
Binary file not shown.
Binary file modified lib/native/linux-arm.jar
Binary file not shown.
Binary file modified lib/native/linux-armel.jar
Binary file not shown.
Binary file modified lib/native/linux-loongarch64.jar
Binary file not shown.
Binary file modified lib/native/linux-mips64el.jar
Binary file not shown.
Binary file modified lib/native/linux-ppc.jar
Binary file not shown.
Binary file modified lib/native/linux-ppc64le.jar
Binary file not shown.
Binary file modified lib/native/linux-riscv64.jar
Binary file not shown.
Binary file modified lib/native/linux-s390x.jar
Binary file not shown.
Binary file modified lib/native/linux-x86-64.jar
Binary file not shown.
Binary file modified lib/native/linux-x86.jar
Binary file not shown.
Binary file modified lib/native/openbsd-x86-64.jar
Binary file not shown.
Binary file modified lib/native/openbsd-x86.jar
Binary file not shown.
Binary file modified lib/native/sunos-sparc.jar
Binary file not shown.
Binary file modified lib/native/sunos-sparcv9.jar
Binary file not shown.
Binary file modified lib/native/sunos-x86-64.jar
Binary file not shown.
Binary file modified lib/native/sunos-x86.jar
Binary file not shown.
Binary file modified lib/native/win32-aarch64.jar
Binary file not shown.
Binary file modified lib/native/win32-x86-64.jar
Binary file not shown.
Binary file modified lib/native/win32-x86.jar
Binary file not shown.
11 changes: 6 additions & 5 deletions native/dispatch.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
#define DEFAULT_LOAD_OPTS (RTLD_LAZY|RTLD_GLOBAL)
#define LOAD_LIBRARY(NAME,OPTS) dlopen(NAME, OPTS)
static inline char * LOAD_ERROR() {
char* message = dlerror();
const char* message = dlerror();
char* buf = (char*) malloc(strlen(message) + 1 /* null */);
strcpy(buf, message);
return buf;
Expand Down Expand Up @@ -144,9 +144,10 @@ extern "C" {
PSTART(); memset(D,C,L); PEND(ENV); \
} while(0)

#define MASK_CC com_sun_jna_Function_MASK_CC
#define THROW_LAST_ERROR com_sun_jna_Function_THROW_LAST_ERROR
#define USE_VARARGS com_sun_jna_Function_USE_VARARGS
#define MASK_CC com_sun_jna_Function_MASK_CC
#define THROW_LAST_ERROR com_sun_jna_Function_THROW_LAST_ERROR
#define USE_VARARGS com_sun_jna_Function_USE_VARARGS
#define USE_VARARGS_SHIFT com_sun_jna_Function_USE_VARARGS_SHIFT

/* Cached class, field and method IDs */
static jclass classObject;
Expand Down Expand Up @@ -480,7 +481,7 @@ dispatch(JNIEnv *env, void* func, jint flags, jobjectArray args,
callconv_t callconv = flags & MASK_CC;
const char* volatile throw_type = NULL;
const char* volatile throw_msg = NULL;
int fixed_args = (flags & USE_VARARGS) >> 7;
int fixed_args = (flags >> USE_VARARGS_SHIFT) & USE_VARARGS;

nargs = (*env)->GetArrayLength(env, args);

Expand Down
17 changes: 16 additions & 1 deletion native/testlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,20 @@ addVarArgs(const char *fmt, ...) {
return sum;
}

EXPORT int32_t
addSeveralFixedArgsAndVarArgs(int a, int b, int c, int d, int n_varargs, ...) {
va_list ap;
int i;
int32_t sum = a + b + c + d;
va_start(ap, n_varargs);

for (i = 0; i < n_varargs; i++) {
sum += va_arg(ap, int32_t);
}
va_end(ap);
return sum;
}

EXPORT void
modifyStructureVarArgs(const char* fmt, ...) {
struct _ss {
Expand Down Expand Up @@ -1095,8 +1109,9 @@ EXPORT size_t copyString(char* input, char* output) {
* responsible to allocate a correctly sized buffer.
*/
EXPORT size_t copyStringArray(char** input, char* output) {
int i;
size_t len = 0;
for(int i = 0;; i++) {
for(i = 0;; i++) {
char* currInput = input[i];
if(currInput == NULL) {
break;
Expand Down
9 changes: 6 additions & 3 deletions src/com/sun/jna/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,12 @@ public interface PostCallRead {
/** Whether to throw an exception if last error is non-zero after call. */
@java.lang.annotation.Native
public static final int THROW_LAST_ERROR = 0x40;
/** Mask for number of fixed args (1-3) for varargs calls. */
/** Mask for number of fixed args (max 255) for varargs calls. */
@java.lang.annotation.Native
public static final int USE_VARARGS = 0x180;
public static final int USE_VARARGS = 0xFF;
/** Offset of USE_VARARGS in call flags */
@java.lang.annotation.Native
public static final int USE_VARARGS_SHIFT = 7;

static final Integer INTEGER_TRUE = Integer.valueOf(-1);
static final Integer INTEGER_FALSE = Integer.valueOf(0);
Expand Down Expand Up @@ -410,7 +413,7 @@ Object invoke(Object[] args, Class<?> returnType, boolean allowObjects) {
/* @see NativeLibrary#NativeLibrary(String,String,long,Map) implementation */
Object invoke(Object[] args, Class<?> returnType, boolean allowObjects, int fixedArgs) {
Object result = null;
int callFlags = this.callFlags | ((fixedArgs & 0x3) << 7);
int callFlags = this.callFlags | ((fixedArgs & USE_VARARGS) << USE_VARARGS_SHIFT);
if (returnType == null || returnType==void.class || returnType==Void.class) {
Native.invokeVoid(this, this.peer, callFlags, args);
result = null;
Expand Down
17 changes: 17 additions & 0 deletions test/com/sun/jna/VarArgsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected List<String> getFieldOrder() {
}
}
public int addVarArgs(String fmt, Number... args);
public int addSeveralFixedArgsAndVarArgs(int a, int b, int c, int d, int nArgs, Integer... args);
public String returnStringVarArgs(String fmt, Object... args);
public void modifyStructureVarArgs(String fmt, Object... args);
public String returnStringVarArgs2(String fmt, String... args);
Expand Down Expand Up @@ -102,6 +103,22 @@ public void testStringVarArgsFull() {
lib.returnStringVarArgs2("", "Test"));
}

public void testSeveralFixedAndVarArgs() {
int fixedarg1 = 1;
int fixedarg2 = 2;
int fixedarg3 = 3;
int fixedarg4 = 4;
int vararg1 = 100;
int vararg2 = 200;

int expected = fixedarg1 + fixedarg2 + fixedarg3 + fixedarg4 + vararg1 + vararg2;
int result = lib.addSeveralFixedArgsAndVarArgs(fixedarg1, fixedarg2, fixedarg3, fixedarg4,
2, vararg1, vararg2);

assertEquals("varargs not passed correctly with multiple fixed args",
expected, result);
}

public void testAppendNullToVarargs() {
Number[] args = new Number[] { Integer.valueOf(1) };
assertEquals("No trailing NULL was appended to varargs list",
Expand Down

0 comments on commit c9f062a

Please sign in to comment.