-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework sun.misc.Unsafe usages so that we directly refer to sun.misc.U…
…nsafe, and call getObject on it natively instead of through reflection. (Take 2, after exempting its usages internally.) Calling getObject through reflection breaks Azul JVMs, because the staticFieldBase is a funny "not object", but when passed through reflection it tries to become an Object and fails. Retrieval of the unsafe is based on how other google open source projects do it, see for example AbstractFuture, UnsignedBytes, and protobufs. Fixes #1719, and properly fixes #1672. (Note that this change also bumps the Github Action's bazel version from 4.2.2 to 6.1.2, because somewhere along the way from 4.2.2->6.1.2, Bazel fixed the --javacopts="--release 8" option to also allow sun.misc.Unsafe.) PiperOrigin-RevId: 529486761
- Loading branch information
Showing
4 changed files
with
53 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright (C) 2023 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.inject.internal.aop; | ||
|
||
final class UnsafeGetter { | ||
|
||
private UnsafeGetter() {} | ||
|
||
static sun.misc.Unsafe getUnsafe() throws ReflectiveOperationException { | ||
try { | ||
return sun.misc.Unsafe.getUnsafe(); | ||
} catch (SecurityException unusedFallbackToReflection) { | ||
} | ||
// Note that we do not do this in a privileged action because we expect we're already in a | ||
// privileged block (from UnsafeClassDefiner). | ||
Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class; | ||
for (java.lang.reflect.Field f : k.getDeclaredFields()) { | ||
f.setAccessible(true); | ||
Object x = f.get(null); | ||
if (k.isInstance(x)) { | ||
return k.cast(x); | ||
} | ||
} | ||
throw new NoSuchFieldError("the Unsafe"); | ||
} | ||
} |