Skip to content

Commit f81be75

Browse files
committed
Use Unsafe API in more correct way
* pass result of Unsafe.staticFieldBase() instead of Lookup.class to conform with specification for Unsafe.getObject() requirements. * avoid calling Unsafe.getObject() via reflection Fixes google#1672
1 parent a99b87a commit f81be75

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

core/src/com/google/inject/internal/aop/HiddenClassDefiner.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
final class HiddenClassDefiner implements ClassDefiner {
3030

3131
private static final Object THE_UNSAFE;
32+
private static final Object TRUSTED_LOOKUP_BASE;
3233
private static final Object TRUSTED_LOOKUP_OFFSET;
3334
private static final Method GET_OBJECT_METHOD;
3435
private static final Object HIDDEN_CLASS_OPTIONS;
@@ -41,6 +42,8 @@ final class HiddenClassDefiner implements ClassDefiner {
4142
theUnsafeField.setAccessible(true);
4243
THE_UNSAFE = theUnsafeField.get(null);
4344
Field trustedLookupField = Lookup.class.getDeclaredField("IMPL_LOOKUP");
45+
Method baseMethod = unsafeType.getMethod("staticFieldBase", Field.class);
46+
TRUSTED_LOOKUP_BASE = baseMethod.invoke(THE_UNSAFE, trustedLookupField);
4447
Method offsetMethod = unsafeType.getMethod("staticFieldOffset", Field.class);
4548
TRUSTED_LOOKUP_OFFSET = offsetMethod.invoke(THE_UNSAFE, trustedLookupField);
4649
GET_OBJECT_METHOD = unsafeType.getMethod("getObject", Object.class, long.class);
@@ -56,7 +59,8 @@ final class HiddenClassDefiner implements ClassDefiner {
5659
@Override
5760
public Class<?> define(Class<?> hostClass, byte[] bytecode) throws Exception {
5861
Lookup trustedLookup =
59-
(Lookup) GET_OBJECT_METHOD.invoke(THE_UNSAFE, Lookup.class, TRUSTED_LOOKUP_OFFSET);
62+
(Lookup) ((sun.misc.Unsafe)THE_UNSAFE)
63+
.getObject(TRUSTED_LOOKUP_BASE, ((Number)TRUSTED_LOOKUP_OFFSET).longValue());
6064
Lookup definedLookup =
6165
(Lookup)
6266
HIDDEN_DEFINE_METHOD.invoke(

0 commit comments

Comments
 (0)