Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement static field base and offset substitutions for Unsafe #3284

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.security.ProtectionDomain;
import java.util.function.Function;

import com.oracle.svm.core.StaticFieldsSupport;
import org.graalvm.compiler.nodes.extended.MembarNode;
import org.graalvm.compiler.serviceprovider.JavaVersionUtil;
import org.graalvm.nativeimage.Platform;
Expand Down Expand Up @@ -206,14 +207,13 @@ public void ensureClassInitialized(Class<?> c) {
DynamicHub.fromClass(c).ensureInitialized();
}

@Substitute
private long staticFieldOffset(Field f) {
throw VMError.unsupportedFeature("Unsupported method of Unsafe");
}

@Substitute
private Object staticFieldBase(Field f) {
throw VMError.unsupportedFeature("Unsupported method of Unsafe");
if (f.getType().isPrimitive()) {
return StaticFieldsSupport.getStaticPrimitiveFields();
} else {
return StaticFieldsSupport.getStaticObjectFields();
}
}

@Substitute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,7 @@ public final class Target_jdk_internal_misc_Unsafe_Reflection {

@Substitute
public long objectFieldOffset(Target_java_lang_reflect_Field field) {
int offset = field.root == null ? field.offset : field.root.offset;
if (offset > 0) {
return offset;
}
throw VMError.unsupportedFeature("The offset of " + field + " is accessed without the field being first registered as unsafe accessed. " +
"Please register the field as unsafe accessed. You can do so with a reflection configuration that " +
"contains an entry for the field with the attribute \"allowUnsafeAccess\": true. Such a configuration " +
"file can be generated for you. Read BuildConfiguration.md and Reflection.md for details.");
return FieldUtils.getFieldOffset(field);
}

@Substitute
Expand All @@ -67,4 +60,27 @@ public long objectFieldOffset(Class<?> c, String name) {
throw new InternalError();
}
}

@Substitute
public long staticFieldOffset(Target_java_lang_reflect_Field field) {
/*
* Since we store the offset in the `offset` field, which is computed through
* `FieldOffsetComputer#compute`, the implementation for this is the same as
* `objectFieldOffset` method
*/
return FieldUtils.getFieldOffset(field);
}

private static class FieldUtils {
private static long getFieldOffset(Target_java_lang_reflect_Field field) {
int offset = field.root == null ? field.offset : field.root.offset;
if (offset > 0) {
return offset;
}
throw VMError.unsupportedFeature("The offset of " + field + " is accessed without the field being first registered as unsafe accessed. " +
"Please register the field as unsafe accessed. You can do so with a reflection configuration that " +
"contains an entry for the field with the attribute \"allowUnsafeAccess\": true. Such a configuration " +
"file can be generated for you. Read BuildConfiguration.md and Reflection.md for details.");
}
}
}