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

JNI global references leak when native method registration fails

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

还没有人认领这个 Issue。

评估

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

调研方向

从 native/dispatch.c 中的 Java_com_sun_jna_Native_registerMethod 开始,重点检查 ffi_prep_cif、ffi_closure_alloc 或 ffi_prep_closure_loc 失败后进入的清理路径。跟踪 closure_method、from_native、to_native、closure_arg_types、encoding 以及 FFI closure 的所有权,然后确认失败的注册会释放它们,而成功的注册仍会将清理工作转交给 Native_unregister。

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

描述

Summary

Java_com_sun_jna_Native_registerMethod creates persistent JNI references before several fallible FFI initialization steps.

Affected code:
https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/native/dispatch.c#L3553-L3587

If ffi_prep_cif, ffi_closure_alloc, or ffi_prep_closure_loc fails, execution jumps to cleanup. That path frees data without calling DeleteGlobalRef for data->closure_method.

Because registration did not complete, Native_unregister cannot release this reference later.

Impact

Each failed registration permanently retains the reflected Java Method and potentially its class-loader object graph until JVM shutdown. Repeated failures may cause persistent memory growth.

The same cleanup path also omits some related resources, including weak global references, closure_arg_types, encoding, and an allocated FFI closure.

Suggested fix

Before freeing data on failure:

  • Delete closure_method with DeleteGlobalRef.
  • Delete any from_native and to_native weak global references.
  • Free all allocated arrays, encoding data, and FFI closure memory.

Successful registrations should continue to transfer ownership to Native_unregister.

主要语言
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 摘要。