Skip to content

Commit

Permalink
fix get static field by unsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
chaokunyang committed Feb 20, 2024
1 parent dbbd096 commit ab44771
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
@SuppressWarnings("rawtypes")
public class SingletonCollectionSerializer extends AbstractCollectionSerializer {
private final Field field;
private Object base = null;
private long offset = -1;

public SingletonCollectionSerializer(Fury fury, Class cls) {
Expand All @@ -61,8 +62,9 @@ public Object read(MemoryBuffer buffer) {
if (offset == -1) {
Preconditions.checkArgument(!GraalvmSupport.isGraalBuildtime());
offset = this.offset = Platform.UNSAFE.staticFieldOffset(field);
base = Platform.UNSAFE.staticFieldBase(field);
}
return Platform.getObject(type, offset);
return Platform.getObject(base, offset);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
@SuppressWarnings("rawtypes")
public class SingletonMapSerializer extends AbstractMapSerializer {
private final Field field;
private Object base = null;
private long offset = -1;

public SingletonMapSerializer(Fury fury, Class cls) {
Expand All @@ -61,8 +62,9 @@ public Object read(MemoryBuffer buffer) {
if (offset == -1) {
Preconditions.checkArgument(!GraalvmSupport.isGraalBuildtime());
offset = this.offset = Platform.UNSAFE.staticFieldOffset(field);
base = Platform.UNSAFE.staticFieldBase(field);
}
return Platform.getObject(type, offset);
return Platform.getObject(base, offset);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
@SuppressWarnings("rawtypes")
public class SingletonObjectSerializer extends Serializer {
private final Field field;
private Object base = null;
private long offset = -1;

public SingletonObjectSerializer(Fury fury, Class type) {
Expand All @@ -54,7 +55,8 @@ public Object read(MemoryBuffer buffer) {
if (offset == -1) {
Preconditions.checkArgument(!GraalvmSupport.isGraalBuildtime());
offset = this.offset = Platform.UNSAFE.staticFieldOffset(field);
base = Platform.UNSAFE.staticFieldBase(field);
}
return Platform.getObject(type, offset);
return Platform.getObject(base, offset);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class _Lookup {
try {
Field implLookup = Lookup.class.getDeclaredField("IMPL_LOOKUP");
long fieldOffset = _JDKAccess.UNSAFE.staticFieldOffset(implLookup);
trustedLookup = (Lookup) _JDKAccess.UNSAFE.getObject(Lookup.class, fieldOffset);
Object fieldBase = _JDKAccess.UNSAFE.staticFieldBase(implLookup);
trustedLookup = (Lookup) _JDKAccess.UNSAFE.getObject(fieldBase, fieldOffset);
} catch (Throwable ignored) {
// ignored
}
Expand Down

0 comments on commit ab44771

Please sign in to comment.