Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

JNI global references leak when native method registration fails

Đang mở
#1,738 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
66/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
c, java
Lĩnh vực
backend

Hướng nghiên cứu

Bắt đầu trong native/dispatch.c tại Java_com_sun_jna_Native_registerMethod, đặc biệt là đường dẫn dọn dẹp được thực hiện sau khi ffi_prep_cif, ffi_closure_alloc hoặc ffi_prep_closure_loc thất bại. Theo dõi quyền sở hữu của closure_method, from_native, to_native, closure_arg_types, encoding và FFI closure, sau đó xác minh rằng các lần đăng ký thất bại sẽ giải phóng chúng, trong khi các lần đăng ký thành công vẫn chuyển việc dọn dẹp cho Native_unregister.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Java
Star
8.9k
Fork
1.7k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của java-native-access/jna

Tất cả issue của java-native-access/jna

Issue tương tự

Thêm issue về Java

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.