Skip to content

Replace deprecated Unsafe API #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Horcrux7 opened this issue Mar 3, 2025 · 5 comments
Open

Replace deprecated Unsafe API #165

Horcrux7 opened this issue Mar 3, 2025 · 5 comments
Milestone

Comments

@Horcrux7
Copy link

Horcrux7 commented Mar 3, 2025

I get the follow on the console with Java 23:

WARNING: A terminally deprecated method in sun.misc.Unsafe has been called
WARNING: sun.misc.Unsafe::getLong has been called by com.kenai.jffi.UnsafeMemoryIO$UnsafeMemoryIO64 (file:.../jffi.jar)
WARNING: Please consider reporting this to the maintainers of class com.kenai.jffi.UnsafeMemoryIO$UnsafeMemoryIO64
WARNING: sun.misc.Unsafe::getLong will be removed in a future release

If I look into the source of Unsafe you should use the class VarHandle which exists since Java 9.

@headius
Copy link
Member

headius commented Mar 3, 2025

Thanks for the issue! Yes, we know we should move to Java 9 VarHandle, but since this library still supports 8 we need to be able to work both ways. Perhaps you can help with a VarHandle patch?

@headius headius added this to the 1.3.14 milestone Mar 3, 2025
@Horcrux7
Copy link
Author

Horcrux7 commented Mar 6, 2025

@headius Thanks for the very fast feedback. If you want support the out dated Java 8 in the future then a possible solution for Java 8 support and the use of VarHandle in newer Java versions can be a multi-release jar file.

Perhaps you can help with a VarHandle patch?

No, I have self never use Unsafe or VarHandle. I know that it exists. This is all. This is not a good base to write a patch.

@headius
Copy link
Member

headius commented Mar 13, 2025

Warnings show up for JRuby users so we'll need a fix sooner than later: jruby/jruby#8696

@headius
Copy link
Member

headius commented Mar 13, 2025

I started looking into the code and it's not actually something we can use VarHandle for. The code in question is actually for doing IO to and from native memory given a pointer value. We will need to replace it with Project Panama's foreign memory API conditionally.

We do, however, do have a property to disable Unsafe use at least for this case: jffi.unsafe.disabled. If you set that to false it will switch to a slower (by how much I don't know) JNI-based version of MemoryIO and it won't produce the warnings.

@headius
Copy link
Member

headius commented Mar 24, 2025

A second option is this JVM flag you can use to disable the warning: --sun-misc-unsafe-memory-access=allow.

We are debating whether we fix this or just push forward with the FFM-based jnr-ffi in https://github.com/JornVernee/jnr-ffi/tree/panama-22-unsquashed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants