JNI global references leak when native method registration fails
还没有人认领这个 Issue。
评估
调研方向
从 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_methodwithDeleteGlobalRef. - Delete any
from_nativeandto_nativeweak 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,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
java-native-access/jna 的其他 Issue
-
Incorrect mapping of c.s.j.platform.unix.solaris.LibKstat.KstatCtl可能已有人在做 @dbwiddis 于 1 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 88/100
java-native-access/jna#1740 · 1 条评论 ·
-
难度 4/5 3-5 天 新手友好度 45/100
java-native-access/jna#1736 ·
-
难度 3/5 1-2 天 新手友好度 65/100
java-native-access/jna#1717 ·
-
Windows32Exception - The parameter is incorrect可能已有人在做 @marktech0813 于 331 天前认领。 未关闭
难度 4/5 3-5 天 新手友好度 35/100
java-native-access/jna#1700 · 11 条评论 ·
-
feature request
难度 4/5 3-5 天 新手友好度 35/100
java-native-access/jna#1698 · 1 条评论 ·
查看 java-native-access/jna 的全部 Issue
相似的 Issue
-
backend
难度 2/5 1-3 小时 新手友好度 76/100
bcgov/nr-forest-client#2524 ·
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 85/100
Sinytra/ForgifiedFabricAPI#298 ·
-
难度 2/5 1-3 小时 新手友好度 67/100
维护者通常 1 天内回复
-
难度 1/5 1 小时以内 新手友好度 74/100
维护者通常 1 天内回复
-
team:Lumberjack
难度 2/5 1-3 小时 新手友好度 76/100
OpenLiberty/open-liberty#35998 ·
维护者通常 1 天内回复