Skip to content

Commit

Permalink
[GR-40265] Set proper class loader to a proxy class
Browse files Browse the repository at this point in the history
PullRequest: graal/12423
  • Loading branch information
sstanoje committed Sep 16, 2024
2 parents 7a2b91d + e21b5e7 commit 7de5eb5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;

import org.graalvm.collections.EconomicMap;
import org.graalvm.nativeimage.ImageSingletons;
Expand Down Expand Up @@ -67,6 +68,8 @@ public static final class Options {

public static final String ENABLE_BYTECODES_OPTION = SubstrateOptionsParser.commandArgument(Options.SupportPredefinedClasses, "+");

@Platforms(Platform.HOSTED_ONLY.class) private Consumer<Class<?>> validator = null;

@Fold
public static boolean supportsBytecodes() {
return Options.SupportPredefinedClasses.getValue();
Expand Down Expand Up @@ -111,8 +114,16 @@ static PredefinedClassesSupport singleton() {
/** Predefined classes which have already been loaded, by name. */
private final EconomicMap<String, Class<?>> loadedClassesByName = EconomicMap.create();

@Platforms(Platform.HOSTED_ONLY.class)
public void setRegistrationValidator(Consumer<Class<?>> consumer) {
validator = consumer;
}

@Platforms(Platform.HOSTED_ONLY.class)
public static void registerClass(String hash, Class<?> clazz) {
if (singleton().validator != null) {
singleton().validator.accept(clazz);
}
Class<?> existing = singleton().predefinedClassesByHash.putIfAbsent(hash, clazz);
if (existing != clazz) {
VMError.guarantee(existing == null, "Can define only one class per hash");
Expand Down Expand Up @@ -167,6 +178,9 @@ private static void registerLambdaForReflection(Class<?> lambdaClass) {
*/
@Platforms(Platform.HOSTED_ONLY.class)
public static void registerClass(Class<?> clazz) {
if (singleton().validator != null) {
singleton().validator.accept(clazz);
}
singleton().predefinedClasses.add(clazz);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.hosted.Feature;
import org.graalvm.nativeimage.hosted.RuntimeClassInitialization;

import com.oracle.graal.pointsto.meta.AnalysisType;
import com.oracle.svm.core.configure.ConfigurationFile;
import com.oracle.svm.core.configure.ConfigurationFiles;
import com.oracle.svm.core.configure.PredefinedClassesConfigurationParser;
Expand Down Expand Up @@ -100,6 +102,14 @@ public void afterRegistration(AfterRegistrationAccess arg) {

@Override
public void beforeAnalysis(BeforeAnalysisAccess access) {
FeatureImpl.BeforeAnalysisAccessImpl impl = (FeatureImpl.BeforeAnalysisAccessImpl) access;
PredefinedClassesSupport support = ImageSingletons.lookup(PredefinedClassesSupport.class);
support.setRegistrationValidator(clazz -> {
Optional<AnalysisType> analysisType = impl.getMetaAccess().optionalLookupJavaType(clazz);
analysisType.map(impl.getHostVM()::dynamicHub).ifPresent(
hub -> VMError.guarantee(hub.isLoaded(), "Classes that should be predefined must not have a class loader."));
});

sealed = true;

List<String> skipped = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

package com.oracle.svm.test.proxy;

import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand All @@ -39,10 +41,6 @@
import java.util.List;
import java.util.Set;

/**
* FIXME: Temporary workaround for GR-40265. Test will be re-enabled as soon as the solution for
* this problem is found.
*/
public class ProxyClassSerializationTest {

private static void serialize(ByteArrayOutputStream byteArrayOutputStream, Object proxyObject) throws IOException {
Expand All @@ -57,6 +55,7 @@ private static Object deserialize(ByteArrayOutputStream byteArrayOutputStream) t
return objectInputStream.readObject();
}

@Test
public void testProxyClassSerialization() throws Exception {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

Expand Down

0 comments on commit 7de5eb5

Please sign in to comment.