You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
class sun.misc.Unsafe is a jdk internal class. package sun.misc is jdk internal after jdk 8. In jdk16, --illegal-access=deny was default. This option is ignored in jdk 17. usage context: run method declaration (return type)
Can you provide some information about what problem you're seeing? We have included fallback implementations for cases in which Unsafe is not available—except, it appears, perhaps not for Striped64. I would expect for those normally to automatically kick in. Are you using some kind of static analyzer that flags the Unsafe usages?
Yes we are using a static analyzer to see what are the packages that java 17 doesn't like to expose usage of CLASS sun.misc.Unsafe and it is identifying
Striped64
LittleEndianByteArray
UnsignedBytes
AbstractFuture
As some classes in guava which are using sun.misc.Unsafe and this needs to be avoided
The analyzer is complaining that
class sun.misc.Unsafe is a jdk internal class. package sun.misc is jdk internal after jdk 8. In jdk16, --illegal-access=deny was default. This option is ignored in jdk 17. usage context: sun.misc.Unsafe.getUnsafe call
Is there something that we can put on our code to suppress the static analyzer for the classes that have a fallback implementation? If we can do that without adding any dependencies, then we'd probably do it.
We probably should look at Striped64. (Note that it's part of common.cache, and as always, we encourage people to use Caffeine for caching nowadays.)
The ViewCVS frontend for the jsr166 repo has been offline for a while now, I believe. But the command-line instructions still work. The repo includes a copy of Striped64 that uses MethodHandle+VarHandle, so we could use that if we don't want to figure things out ourselves.
Or perhaps we just want to use LongAdder? As noted, that might cause trouble for GWT/J2CL, but we could use supersource. As also hinted there, it would cause trouble for Android, too, if it were the only approach (since the class isn't available there until API Level 24). But it would be fine with a fallback to Unsafe.
JDK 24-ea+26 or later now issues this warning by default (see MNG-8399 for all of them):
WARNING: A terminally deprecated method in sun.misc.Unsafe has been called
WARNING: sun.misc.Unsafe::objectFieldOffset has been called by com.google.common.util.concurrent.AbstractFuture$UnsafeAtomicHelper (file:/home/marc/.asdf/installs/maven/3.9.9/lib/guava-33.2.1-jre.jar)
WARNING: Please consider reporting this to the maintainers of class com.google.common.util.concurrent.AbstractFuture$UnsafeAtomicHelper
WARNING: sun.misc.Unsafe::objectFieldOffset will be removed in a future release
For now, it can be suppressed using --sun-misc-unsafe-memory-access=allow but according to JEP 498 that's going to change in "JDK 26 or later".
Thanks. Our current JDK@head project is SecurityManager, which you've probably seen produce similar warnings :) I imagine that this one will be in our sights soon. We'll compare performance of the various AtomicHelper implementations under various JDKs and then see if we should reorder them, introduce a new one (VarHandle-based?), or what.
It's possible that we'll want to do something in the Android flavor for users who use it under newer JVMs: Currently, the code would still try to use Unsafe there, and I don't think it would successfully fall back when Unsafe starts throwing exceptions. [edit: I subsequently verified that is does successfully fall back.] (And even when Unsafe "only" logs warnings, that's annoying, too.)
We of course recommend using the JRE flavor when running under a JVM. But it can be easy for the Android flavor to sneak in through cross-platform libraries that use it as a dependency. I don't expect it to be especially common, but it will come up.
This might actually be a case for a multi-release jar. But we'd want to see how that would affect Android users. (And I would expect to still find the occasional tool that doesn't understand multi-release jars, perhaps especially in the Android ecosystem, where they are presumably less used.) It's possible that the simpler course of action would be to perform a runtime version check. (We could also catch the exception that Unsafe will throw, but that wouldn't deal with the logging issue.)
Currently, the code would still try to use Unsafe there, and I don't think it would successfully fall back when Unsafe starts throwing exceptions.
My mistake: It does fall back successfully. I had been confused with a separate problem, in which an internal utility was trying to preinitialize essentially all the classes in Guava, including UnsafeAtomicHelper, even in environments in which it won't work. We might still change Guava to accommodate that, or we may change the utility.
OK, it's still worth noting that, if you use guava-android on a JRE (even after the upcoming release to avoid using Unsafe in guava-jre), recent JVMs would still show you warnings about using Unsafe, as already noted above. So maybe we'll want to do something more for guava-android, whether it involves multi-release jars or a runtime version check.
...or maybe including the VarHandle version in guava-android, too, while making sure not to use it from Android. (As I understand things, we'd want to avoid using it even if VarHandle is present: I don't think that VarHandle is actually usable unless the minimum(?) SDK set at build time is high enough to support it—or something like that. And (because of precisely this problem :)) we also haven't evaluated the performance of VarHandle under Android. So if we were to include the VarHandle code in guava-android, it would be (for now) purely for the benefit of JVM users.)
(This is still solved for guava-jre as of the forthcoming Guava 33.4.5. The following is only about guava-android.)
Actually, I've been focused mostly on AbstractFuture. Our otherUnsafe-using classes don't necessarily have fallbacks (Striped64?), or, if they do, those fallbacks might not trigger properly (LittleEndianByteArray)? UnsignedBytes, like AbstractFuture, is probably fine. But that would be good to verify.
Hmm, I'd run some kind of test of what happens when fallback is off, but maybe I'd ignored the failures in the backport? Or, more likely, they got lost among some other failures from other tools that we use. I do see at least one failure related to Striped64 now, along with what appears to be the expected successful fallback in UnsignedBytes (but our tests don't expect fallback at all at the moment). I'm not sure what's up with LittleEndianByteArray, for which I'd expect to see a failure but seemingly don't. [edit: See #7742 (comment).]
Anyway, I will still come back to the guava-android question.
Activity
cpovirk commentedon Oct 25, 2023
Can you provide some information about what problem you're seeing? We have included fallback implementations for cases in which
Unsafeis not available—except, it appears, perhaps not forStriped64. I would expect for those normally to automatically kick in. Are you using some kind of static analyzer that flags theUnsafeusages?koundinyarvs commentedon Oct 27, 2023
Hi @cpovirk
Yes we are using a static analyzer to see what are the packages that java 17 doesn't like to expose usage of CLASS sun.misc.Unsafe and it is identifying
Striped64
LittleEndianByteArray
UnsignedBytes
AbstractFuture
As some classes in guava which are using sun.misc.Unsafe and this needs to be avoided
The analyzer is complaining that
class sun.misc.Unsafe is a jdk internal class. package sun.misc is jdk internal after jdk 8. In jdk16, --illegal-access=deny was default. This option is ignored in jdk 17. usage context: sun.misc.Unsafe.getUnsafe call
cpovirk commentedon Oct 28, 2023
Is there something that we can put on our code to suppress the static analyzer for the classes that have a fallback implementation? If we can do that without adding any dependencies, then we'd probably do it.
We probably should look at
Striped64. (Note that it's part of common.cache, and as always, we encourage people to use Caffeine for caching nowadays.)cpovirk commentedon Nov 21, 2023
On the
Striped64front:The ViewCVS frontend for the jsr166 repo has been offline for a while now, I believe. But the command-line instructions still work. The repo includes a copy of
Striped64that usesMethodHandle+VarHandle, so we could use that if we don't want to figure things out ourselves.Or perhaps we just want to use
LongAdder? As noted, that might cause trouble for GWT/J2CL, but we could use supersource. As also hinted there, it would cause trouble for Android, too, if it were the only approach (since the class isn't available there until API Level 24). But it would be fine with a fallback toUnsafe.marcphilipp commentedon Dec 3, 2024
JDK 24-ea+26 or later now issues this warning by default (see MNG-8399 for all of them):
For now, it can be suppressed using
--sun-misc-unsafe-memory-access=allowbut according to JEP 498 that's going to change in "JDK 26 or later".cpovirk commentedon Dec 4, 2024
Thanks. Our current JDK@head project is
SecurityManager, which you've probably seen produce similar warnings :) I imagine that this one will be in our sights soon. We'll compare performance of the variousAtomicHelperimplementations under various JDKs and then see if we should reorder them, introduce a new one (VarHandle-based?), or what.37 remaining items
cpovirk commentedon Feb 5, 2025
It's possible that we'll want to do something in the Android flavor for users who use it under newer JVMs: Currently, the code would still try to use
Unsafethere, andI don't think it would successfully fall back when. [edit: I subsequently verified that is does successfully fall back.] (And even whenUnsafestarts throwing exceptionsUnsafe"only" logs warnings, that's annoying, too.)We of course recommend using the JRE flavor when running under a JVM. But it can be easy for the Android flavor to sneak in through cross-platform libraries that use it as a dependency. I don't expect it to be especially common, but it will come up.
This might actually be a case for a multi-release jar. But we'd want to see how that would affect Android users. (And I would expect to still find the occasional tool that doesn't understand multi-release jars, perhaps especially in the Android ecosystem, where they are presumably less used.) It's possible that the simpler course of action would be to perform a runtime version check. (We could also catch the exception that
Unsafewill throw, but that wouldn't deal with the logging issue.)cpovirk commentedon Feb 21, 2025
My mistake: It does fall back successfully. I had been confused with a separate problem, in which an internal utility was trying to preinitialize essentially all the classes in Guava, including
UnsafeAtomicHelper, even in environments in which it won't work. We might still change Guava to accommodate that, or we may change the utility.cpovirk commentedon Feb 21, 2025
Well... :)
OK, it's still worth noting that, if you use
guava-androidon a JRE (even after the upcoming release to avoid usingUnsafeinguava-jre), recent JVMs would still show you warnings about usingUnsafe, as already noted above. So maybe we'll want to do something more forguava-android, whether it involves multi-release jars or a runtime version check.cpovirk commentedon Feb 21, 2025
...or maybe including the
VarHandleversion inguava-android, too, while making sure not to use it from Android. (As I understand things, we'd want to avoid using it even ifVarHandleis present: I don't think thatVarHandleis actually usable unless the minimum(?) SDK set at build time is high enough to support it—or something like that. And (because of precisely this problem :)) we also haven't evaluated the performance ofVarHandleunder Android. So if we were to include theVarHandlecode inguava-android, it would be (for now) purely for the benefit of JVM users.)cpovirk commentedon Mar 19, 2025
(This is still solved for
guava-jreas of the forthcoming Guava 33.4.5. The following is only aboutguava-android.)Actually, I've been focused mostly on
AbstractFuture. Our otherUnsafe-using classes don't necessarily have fallbacks (Striped64?), or, if they do, those fallbacks might not trigger properly (LittleEndianByteArray)?UnsignedBytes, likeAbstractFuture, is probably fine. But that would be good to verify.Hmm, I'd run some kind of test of what happens when fallback is off, but maybe I'd ignored the failures in the backport? Or, more likely, they got lost among some other failures from other tools that we use. I do see at least one failure related to
Striped64now, along with what appears to be the expected successful fallback inUnsignedBytes(but our tests don't expect fallback at all at the moment). I'm not sure what's up withLittleEndianByteArray, for which I'd expect to see a failure but seemingly don't. [edit: See #7742 (comment).]Anyway, I will still come back to the
guava-androidquestion.TWiStErRob commentedon Mar 24, 2025
Is there a way to track -android Unsafe usage migration? (Considering this issue is in closed state.)
[-]Migrate off Unsafe[/-][+]Migrate off Unsafe (guava-jre)[/+]cpovirk commentedon Mar 24, 2025
Thanks, I should really have filed a new issue. I've now done that: #7742
Make the `guava-android` copy of `AbstractFuture` try `VarHandle` whe…
Make the `guava-android` copy of `AbstractFuture` try `VarHandle` whe…
Make the `guava-android` copy of `AbstractFuture` try `VarHandle` whe…