Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Wrong ffi_type for unsigned numbers causes bugs on some platforms

未关闭
#1,435 3 条评论 2 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
android, c, java

调研方向

从报告中链接的最小可复现示例开始,重点关注 JNA 的 IntegerType 以及传递给原生 hello 函数的 UINT16 参数。复现优化和未优化 Android 构建之间的差异,然后跟踪 unsigned short 的 FFI 表示;当 hello 在受影响的架构上以预期值返回 true 时,即表示完成。

由索引模型根据 Issue 内容生成。

描述

Provide complete information about the problem
  1. Version of JNA and related jars
    5.11.0@aar
  2. Version and vendor of the java virtual machine
    ART @ Android 11
  3. Operating system
    Android 11
  4. System architecture (CPU type, bitness of the JVM)
    arm64-v8a, x86_64, possibly more
    Output of clang --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).
  5. Complete description of the problem
    Function argument of type unsigned short passed 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.
  6. Steps to reproduce
    Minimal reproducible example available here.
    The hello function in the following code should return true, but returns false.
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
主要语言
Java
星标
8.9k
派生
1.7k
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

java-native-access/jna 的其他 Issue

查看 java-native-access/jna 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。