Skip to content

Commit

Permalink
Use Unsafe API in more correct way
Browse files Browse the repository at this point in the history
 * pass result of Unsafe.staticFieldBase() instead of Lookup.class
to conform with specification for Unsafe.getObject() requirements.
 * avoid calling Unsafe.getObject() via reflection

Fixes #1672
  • Loading branch information
alsemenov committed May 2, 2023
1 parent a99b87a commit 860ceb3
Showing 1 changed file with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
final class HiddenClassDefiner implements ClassDefiner {

private static final Object THE_UNSAFE;
private static final Object TRUSTED_LOOKUP_BASE;
private static final Object TRUSTED_LOOKUP_OFFSET;
private static final Method GET_OBJECT_METHOD;
private static final Object HIDDEN_CLASS_OPTIONS;
Expand All @@ -41,6 +42,8 @@ final class HiddenClassDefiner implements ClassDefiner {
theUnsafeField.setAccessible(true);
THE_UNSAFE = theUnsafeField.get(null);
Field trustedLookupField = Lookup.class.getDeclaredField("IMPL_LOOKUP");
Method baseMethod = unsafeType.getMethod("staticFieldBase", Field.class);
TRUSTED_LOOKUP_BASE = baseMethod.invoke(THE_UNSAFE, trustedLookupField);
Method offsetMethod = unsafeType.getMethod("staticFieldOffset", Field.class);
TRUSTED_LOOKUP_OFFSET = offsetMethod.invoke(THE_UNSAFE, trustedLookupField);
GET_OBJECT_METHOD = unsafeType.getMethod("getObject", Object.class, long.class);
Expand All @@ -56,7 +59,8 @@ final class HiddenClassDefiner implements ClassDefiner {
@Override
public Class<?> define(Class<?> hostClass, byte[] bytecode) throws Exception {
Lookup trustedLookup =
(Lookup) GET_OBJECT_METHOD.invoke(THE_UNSAFE, Lookup.class, TRUSTED_LOOKUP_OFFSET);
(Lookup) ((sun.misc.Unsafe)THE_UNSAFE)
.getObject(TRUSTED_LOOKUP_BASE, ((Number)TRUSTED_LOOKUP_OFFSET).longValue());
Lookup definedLookup =
(Lookup)
HIDDEN_DEFINE_METHOD.invoke(
Expand Down

0 comments on commit 860ceb3

Please sign in to comment.