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

[GR-48909] LibraryFactory can no longer use sun.misc.Unsafe.ensureClassInitialized as it got removed in JDK 22+16 #7510

Merged
merged 1 commit into from
Sep 29, 2023
Merged
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
5 changes: 3 additions & 2 deletions truffle/src/com.oracle.truffle.api.library/snapshot.sigtest
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ meth protected abstract {com.oracle.truffle.api.library.LibraryFactory%0} create
meth protected abstract {com.oracle.truffle.api.library.LibraryFactory%0} createUncachedDispatch()
meth protected final java.util.List<com.oracle.truffle.api.library.LibraryExport<{com.oracle.truffle.api.library.LibraryFactory%0}>> getAOTExports()
meth protected final {com.oracle.truffle.api.library.LibraryFactory%0} createAOT(com.oracle.truffle.api.library.LibraryExport<{com.oracle.truffle.api.library.LibraryFactory%0}>)
meth protected java.lang.invoke.MethodHandles$Lookup getLookup()
meth protected static <%0 extends com.oracle.truffle.api.library.Library> void register(java.lang.Class<{%%0}>,com.oracle.truffle.api.library.LibraryFactory<{%%0}>)
meth protected static <%0 extends com.oracle.truffle.api.library.Library> {%%0} getDelegateLibrary({%%0},java.lang.Object)
meth protected static boolean assertAdopted(com.oracle.truffle.api.nodes.Node)
Expand All @@ -156,7 +157,7 @@ meth public java.lang.String toString()
meth public static <%0 extends com.oracle.truffle.api.library.Library> com.oracle.truffle.api.library.LibraryFactory<{%%0}> resolve(java.lang.Class<{%%0}>)
supr java.lang.Object
hfds EMPTY_DEFAULT_EXPORT_ARRAY,LIBRARIES,afterBuiltinDefaultExports,aot,beforeBuiltinDefaultExports,cachedCache,dispatchLibrary,eagerExportProviders,exportCache,externalDefaultProviders,libraryClass,messages,nameToMessages,proxyExports,uncachedCache,uncachedDispatch
hcls CachedAOTExports,Lazy,ProxyExports,ResolvedDispatch
hcls CachedAOTExports,ProxyExports,ResolvedDispatch

CLSS public abstract com.oracle.truffle.api.library.Message
cons protected !varargs init(java.lang.Class<? extends com.oracle.truffle.api.library.Library>,java.lang.String,int,boolean,java.lang.Class<?>,java.lang.Class<?>[])
Expand Down Expand Up @@ -251,7 +252,7 @@ CLSS public java.lang.Object
cons public init()
meth protected java.lang.Object clone() throws java.lang.CloneNotSupportedException
meth protected void finalize() throws java.lang.Throwable
anno 0 java.lang.Deprecated(boolean forRemoval=false, java.lang.String since="9")
anno 0 java.lang.Deprecated(boolean forRemoval=true, java.lang.String since="9")
meth public boolean equals(java.lang.Object)
meth public final java.lang.Class<?> getClass()
meth public final void notify()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

import static com.oracle.truffle.api.CompilerDirectives.shouldNotReachHere;

import java.lang.reflect.Field;
import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -68,8 +68,6 @@
import com.oracle.truffle.api.library.provider.EagerExportProvider;
import com.oracle.truffle.api.utilities.FinalBitSet;

import sun.misc.Unsafe;

/**
* Library factories allow to create instances of libraries used to call library messages. A library
* factory for a library class can be looked up using the static method {@link #resolve(Class)}.
Expand Down Expand Up @@ -374,15 +372,18 @@ public final T getUncached() {
return dispatch;
}

@SuppressWarnings("deprecation")
private void ensureLibraryInitialized() {
CompilerAsserts.neverPartOfCompilation();
/*
* This is needed to enforce initialization order. This way the library class is always
* initialized before any of the export subclasses. So this method must be invoked before
* any instantiation of a library export.
*/
Lazy.UNSAFE.ensureClassInitialized(libraryClass);
try {
getLookup().ensureInitialized(libraryClass);
} catch (IllegalAccessException e) {
throw CompilerDirectives.shouldNotReachHere(e);
}
}

/**
Expand Down Expand Up @@ -617,6 +618,15 @@ protected T createAssertions(T delegate) {
*/
protected abstract Class<?> getDefaultClass(Object receiver);

/**
* Returns a method handle lookup used to initialize the library class.
*
* @since 24.0
*/
protected MethodHandles.Lookup getLookup() {
throw new UnsupportedOperationException();
}

private Class<?> getDefaultClassImpl(Object receiver) {
for (DefaultExportProvider defaultExport : beforeBuiltinDefaultExports) {
if (defaultExport.getReceiverClass().isInstance(receiver)) {
Expand Down Expand Up @@ -760,38 +770,6 @@ private static <T extends Library> LibraryFactory<T> resolveImpl(Class<T> librar
return (LibraryFactory<T>) lib;
}

/**
* Annotation processors running in the Eclipse JDT compiler that resolve {@link LibraryFactory}
* also try to eagerly resolve the type of all its fields. If {@code jdk.unsupported} is not in
* the module graph of the JDT compile environment, this can result in the Truffle annotation
* processor failing with an internal error that includes the follow message:
*
* <pre>
* The type sun.misc.Unsafe cannot be resolved. It is indirectly referenced from required .class files
* </pre>
*
* Putting the Unsafe field in an inner class works around this (over)eager resolution by JDT.
*/
static class Lazy {
private static final sun.misc.Unsafe UNSAFE;

static {
Unsafe unsafe;
try {
unsafe = Unsafe.getUnsafe();
} catch (SecurityException e) {
try {
Field theUnsafeInstance = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafeInstance.setAccessible(true);
unsafe = (Unsafe) theUnsafeInstance.get(Unsafe.class);
} catch (Exception e2) {
throw new RuntimeException("exception while trying to get Unsafe.theUnsafe via reflection:", e2);
}
}
UNSAFE = unsafe;
}
}

static LibraryFactory<?> loadGeneratedClass(Class<?> libraryClass) {
if (Library.class.isAssignableFrom(libraryClass)) {
String generatedClassName = libraryClass.getPackage().getName() + "." + libraryClass.getSimpleName() + "Gen";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import static javax.lang.model.element.Modifier.PRIVATE;
import static javax.lang.model.element.Modifier.STATIC;

import java.lang.invoke.MethodHandles;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
Expand Down Expand Up @@ -197,6 +198,11 @@ public List<CodeTypeElement> create(ProcessorContext context1, AnnotationProcess
}
genClass.add(getDefault);

CodeExecutableElement getLookup = CodeExecutableElement.clone(ElementUtils.findExecutableElement(types.LibraryFactory, "getLookup"));
builder = getLookup.createBuilder();
builder.startReturn().startStaticCall(context.getType(MethodHandles.class), "lookup").end(2);
genClass.add(getLookup);

// class MessageImpl
final CodeTypeElement messageClass = createClass(model, null, modifiers(PRIVATE, STATIC), "MessageImpl", types.Message);
CodeExecutableElement messageConstructor = new CodeExecutableElement(modifiers(), null, messageClass.getSimpleName().toString());
Expand Down