Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion java/core/src/main/java/com/google/protobuf/UnsafeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,30 @@ public sun.misc.Unsafe run() throws Exception {
// Catching Throwable here due to the fact that Google AppEngine raises NoClassDefFoundError
// for Unsafe.
}
return unsafe;

if (unsafe == null) {
return null;
}

// Unsafe is terminally deprecated and will be removed in a future version. Our runtime is
// anyway compatible with environments where Unsafe is not available, but as part of the removal
// some environments will have Unsafe available but throw on use, which would break us. To check
// if the JDK is in this state, simply try using a method and see if it does throw.
try {
int unused = unsafe.arrayBaseOffset(byte[].class);
return unsafe;
} catch (Exception unused) {
Logger.getLogger(UnsafeUtil.class.getName())
.log(
Level.WARNING,
"As part of the planned removal, sun.misc.Unsafe is available in the current"
+ " environment but configured to throw on use. Protobuf will continue without"
+ " using it, but with slightly reduced performance."
+ " --sun-misc-unsafe-memory-access=allow is likely available to opt back in if"
+ " desired. A later Protobuf version release will stop using sun.misc.Unsafe"
+ " entirely.");
return null;
}
}

/** Get a {@link MemoryAccessor} appropriate for the platform, or null if not supported. */
Expand Down
Loading