java-native-access / java-native-access/jna
Wrong ffi_type for unsigned numbers causes bugs on some platforms
Open
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 8.9k
- Forks
- 1.7k
- PR merge metrics
- No merged PRs in 30d
Description
Provide complete information about the problem
- Version of JNA and related jars
5.11.0@aar - Version and vendor of the java virtual machine
ART @ Android 11 - Operating system
Android 11 - System architecture (CPU type, bitness of the JVM)
arm64-v8a, x86_64, possibly more
Output ofclang --version:
Compiler Android (7019983 based on r365631c3) clang version 9.0.9 (https://android.googlesource.com/toolchain/llvm-project a2a1e703c0edb03ba29944e529ccbf457742737b) (based on LLVM 9.0.9svn). - Complete description of the problem
Function argument of typeunsigned shortpassed by JNA to native code does not behave correctly when the code is compiled with optimizations enabled. The is caused by the fact that LLVM is counting on the argument being stored in straight format (as it should be) instead of two's complement. - Steps to reproduce
Minimal reproducible example available here.
Thehellofunction in the following code should returntrue, but returnsfalse.
const unsigned short CONSTANT = 0x8810u;
bool hello(unsigned short argument) {
__android_log_print(ANDROID_LOG_WARN, TAG, "argument value as %%#010x=%#010x %%d=%d, as %%u=%u, as %%hu=%hu", argument, argument, argument, argument);
__android_log_print(ANDROID_LOG_WARN, TAG, "CONSTANT value as %%#010x=%#010x %%d=%d, as %%u=%u, as %%hu=%hu", CONSTANT, CONSTANT, CONSTANT, CONSTANT);
switch (argument) {
case CONSTANT:
return true;
default:
return false;
}
}
private void logValueFromJna() {
boolean on = lib.hello(new UINT16(0x8810));
Log.w("JNA-BUG", "hello returned correct value? " + on);
}
public static class UINT16 extends IntegerType {
@SuppressWarnings("unused")
public UINT16() {
this(0);
}
public UINT16(int value) {
super(2, value, true);
}
}
interface TestLibrary extends Library {
boolean hello(UINT16 argument);
}
Log output with optimizations disabled:
W/JNA-BUG-NATIVE: argument value as %#010x=0x00008810 %d=34832, as %u=34832, as %hu=34832
W/JNA-BUG-NATIVE: CONSTANT value as %#010x=0x00008810 %d=34832, as %u=34832, as %hu=34832
W/JNA-BUG: hello returned correct value? true
Log output with optimizations enabled:
W/JNA-BUG-NATIVE: argument value as %#010x=0xffff8810 %d=-30704, as %u=4294936592, as %hu=34832
W/JNA-BUG-NATIVE: CONSTANT value as %#010x=0x00008810 %d=34832, as %u=34832, as %hu=34832
W/JNA-BUG: hello returned correct value? false
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the minimal reproducible example linked in the report, focusing on JNA's IntegerType and the UINT16 argument passed to the native hello function. Reproduce the difference between optimized and unoptimized Android builds, then trace the unsigned-short FFI representation; done means hello returns true with the expected value on the affected architectures.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, c, java
- Domain
- devtools, mobile-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100