Skip to content

Commit

Permalink
8340131: Refactor internal makeHiddenClassDefiner to take option mask…
Browse files Browse the repository at this point in the history
… instead of Set<ClassOption>

Reviewed-by: liach, jvernee
  • Loading branch information
cl4es committed Sep 16, 2024
1 parent 05b9d47 commit e1ebeef
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
import java.lang.classfile.constantpool.ConstantPoolBuilder;
import java.lang.classfile.constantpool.MethodRefEntry;
import static java.lang.constant.ConstantDescs.*;
import static java.lang.invoke.MethodHandleNatives.Constants.NESTMATE_CLASS;
import static java.lang.invoke.MethodHandleNatives.Constants.STRONG_LOADER_LINK;
import static java.lang.invoke.MethodHandles.Lookup.ClassOption.NESTMATE;
import static java.lang.invoke.MethodHandles.Lookup.ClassOption.STRONG;
import static java.lang.invoke.MethodType.methodType;
Expand Down Expand Up @@ -348,7 +350,7 @@ else if (finalAccidentallySerializable)
try {
// this class is linked at the indy callsite; so define a hidden nestmate
var classdata = useImplMethodHandle? implementation : null;
return caller.makeHiddenClassDefiner(lambdaClassName, classBytes, Set.of(NESTMATE, STRONG), lambdaProxyClassFileDumper)
return caller.makeHiddenClassDefiner(lambdaClassName, classBytes, lambdaProxyClassFileDumper, NESTMATE_CLASS | STRONG_LOADER_LINK)
.defineClass(!disableEagerInitialization, classdata);

} catch (Throwable t) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Stream;
import jdk.internal.constant.MethodTypeDescImpl;
Expand Down Expand Up @@ -224,7 +223,7 @@ FieldRefEntry classData(ClassFileBuilder<?, ?> cfb, Object arg, ClassDesc desc)
* Extract the MemberName of a newly-defined method.
*/
private MemberName loadMethod(byte[] classFile) {
Class<?> invokerClass = LOOKUP.makeHiddenClassDefiner(className, classFile, Set.of(), dumper())
Class<?> invokerClass = LOOKUP.makeHiddenClassDefiner(className, classFile, dumper())
.defineClass(true, classDataValues());
return resolveInvokerMember(invokerClass, invokerName, invokerType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import static java.lang.invoke.LambdaForm.*;
import static java.lang.invoke.MethodHandleNatives.Constants.MN_CALLER_SENSITIVE;
import static java.lang.invoke.MethodHandleNatives.Constants.MN_HIDDEN_MEMBER;
import static java.lang.invoke.MethodHandleNatives.Constants.NESTMATE_CLASS;
import static java.lang.invoke.MethodHandleStatics.*;
import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
import static java.lang.invoke.MethodHandles.Lookup.ClassOption.NESTMATE;
Expand Down Expand Up @@ -1111,7 +1112,7 @@ private static Class<?> makeInjectedInvoker(Class<?> targetClass) {
}
name = name.replace('.', '/');
Class<?> invokerClass = new Lookup(targetClass)
.makeHiddenClassDefiner(name, INJECTED_INVOKER_TEMPLATE, Set.of(NESTMATE), dumper())
.makeHiddenClassDefiner(name, INJECTED_INVOKER_TEMPLATE, dumper(), NESTMATE_CLASS)
.defineClass(true, targetClass);
assert checkInjectedInvoker(targetClass, invokerClass);
return invokerClass;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ private static Class<?> newProxyClass(Class<?> intfc) {
byte[] template = createTemplate(loader, binaryNameToDesc(className),
referenceClassDesc(intfc), uniqueName, methods);
// define the dynamic module to the class loader of the interface
var definer = new Lookup(intfc).makeHiddenClassDefiner(className, template, Set.of(), DUMPER);
var definer = new Lookup(intfc).makeHiddenClassDefiner(className, template, DUMPER);

@SuppressWarnings("removal")
var sm = System.getSecurityManager();
Expand Down
54 changes: 36 additions & 18 deletions src/java.base/share/classes/java/lang/invoke/MethodHandles.java
Original file line number Diff line number Diff line change
Expand Up @@ -1906,9 +1906,12 @@ public enum ClassOption {
this.flag = flag;
}

static int optionsToFlag(Set<ClassOption> options) {
static int optionsToFlag(ClassOption[] options) {
int flags = 0;
for (ClassOption cp : options) {
if ((flags & cp.flag) != 0) {
throw new IllegalArgumentException("Duplicate ClassOption " + cp);
}
flags |= cp.flag;
}
return flags;
Expand Down Expand Up @@ -2126,14 +2129,13 @@ public Lookup defineHiddenClass(byte[] bytes, boolean initialize, ClassOption...
throws IllegalAccessException
{
Objects.requireNonNull(bytes);
Objects.requireNonNull(options);

int flags = ClassOption.optionsToFlag(options);
ensureDefineClassPermission();
if (!hasFullPrivilegeAccess()) {
throw new IllegalAccessException(this + " does not have full privilege access");
}

return makeHiddenClassDefiner(bytes.clone(), Set.of(options), false).defineClassAsLookup(initialize);
return makeHiddenClassDefiner(bytes.clone(), false, flags).defineClassAsLookup(initialize);
}

/**
Expand Down Expand Up @@ -2213,14 +2215,15 @@ public Lookup defineHiddenClassWithClassData(byte[] bytes, Object classData, boo
{
Objects.requireNonNull(bytes);
Objects.requireNonNull(classData);
Objects.requireNonNull(options);

int flags = ClassOption.optionsToFlag(options);

ensureDefineClassPermission();
if (!hasFullPrivilegeAccess()) {
throw new IllegalAccessException(this + " does not have full privilege access");
}

return makeHiddenClassDefiner(bytes.clone(), Set.of(options), false)
return makeHiddenClassDefiner(bytes.clone(), false, flags)
.defineClassAsLookup(initialize, classData);
}

Expand Down Expand Up @@ -2366,7 +2369,7 @@ ClassDefiner makeClassDefiner(String name, byte[] bytes, ClassFileDumper dumper)
*/
ClassDefiner makeHiddenClassDefiner(byte[] bytes, ClassFileDumper dumper) {
ClassFile cf = ClassFile.newInstance(bytes, lookupClass().getPackageName());
return makeHiddenClassDefiner(cf, Set.of(), false, dumper);
return makeHiddenClassDefiner(cf, false, dumper, 0);
}

/**
Expand All @@ -2378,18 +2381,33 @@ ClassDefiner makeHiddenClassDefiner(byte[] bytes, ClassFileDumper dumper) {
* before calling this factory method.
*
* @param bytes class bytes
* @param options class options
* @param flags class option flag mask
* @param accessVmAnnotations true to give the hidden class access to VM annotations
* @return ClassDefiner that defines a hidden class of the given bytes and options
*
* @throws IllegalArgumentException if {@code bytes} is not a class or interface or
* {@code bytes} denotes a class in a different package than the lookup class
*/
private ClassDefiner makeHiddenClassDefiner(byte[] bytes,
Set<ClassOption> options,
boolean accessVmAnnotations) {
boolean accessVmAnnotations,
int flags) {
ClassFile cf = ClassFile.newInstance(bytes, lookupClass().getPackageName());
return makeHiddenClassDefiner(cf, options, accessVmAnnotations, defaultDumper());
return makeHiddenClassDefiner(cf, accessVmAnnotations, defaultDumper(), flags);
}

/**
* Returns a ClassDefiner that creates a {@code Class} object of a hidden class
* from the given bytes and the given options. No package name check on the given bytes.
*
* @param name internal name that specifies the prefix of the hidden class
* @param bytes class bytes
* @param dumper dumper to write the given bytes to the dumper's output directory
* @return ClassDefiner that defines a hidden class of the given bytes and options.
*/
ClassDefiner makeHiddenClassDefiner(String name, byte[] bytes, ClassFileDumper dumper) {
Objects.requireNonNull(dumper);
// skip name and access flags validation
return makeHiddenClassDefiner(ClassFile.newInstanceNoCheck(name, bytes), false, dumper, 0);
}

/**
Expand All @@ -2398,30 +2416,30 @@ private ClassDefiner makeHiddenClassDefiner(byte[] bytes,
*
* @param name internal name that specifies the prefix of the hidden class
* @param bytes class bytes
* @param options class options
* @param flags class options flag mask
* @param dumper dumper to write the given bytes to the dumper's output directory
* @return ClassDefiner that defines a hidden class of the given bytes and options.
*/
ClassDefiner makeHiddenClassDefiner(String name, byte[] bytes, Set<ClassOption> options, ClassFileDumper dumper) {
ClassDefiner makeHiddenClassDefiner(String name, byte[] bytes, ClassFileDumper dumper, int flags) {
Objects.requireNonNull(dumper);
// skip name and access flags validation
return makeHiddenClassDefiner(ClassFile.newInstanceNoCheck(name, bytes), options, false, dumper);
return makeHiddenClassDefiner(ClassFile.newInstanceNoCheck(name, bytes), false, dumper, flags);
}

/**
* Returns a ClassDefiner that creates a {@code Class} object of a hidden class
* from the given class file and options.
*
* @param cf ClassFile
* @param options class options
* @param flags class option flag mask
* @param accessVmAnnotations true to give the hidden class access to VM annotations
* @param dumper dumper to write the given bytes to the dumper's output directory
*/
private ClassDefiner makeHiddenClassDefiner(ClassFile cf,
Set<ClassOption> options,
boolean accessVmAnnotations,
ClassFileDumper dumper) {
int flags = HIDDEN_CLASS | ClassOption.optionsToFlag(options);
ClassFileDumper dumper,
int flags) {
flags |= HIDDEN_CLASS;
if (accessVmAnnotations | VM.isSystemDomainLoader(lookupClass.getClassLoader())) {
// jdk.internal.vm.annotations are permitted for classes
// defined to boot loader and platform loader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import java.lang.ref.SoftReference;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.function.Supplier;
Expand Down Expand Up @@ -1353,7 +1352,7 @@ public void accept(MethodBuilder mb) {
}
}});
try {
var hiddenClass = lookup.makeHiddenClassDefiner(CLASS_NAME, classBytes, Set.of(), DUMPER)
var hiddenClass = lookup.makeHiddenClassDefiner(CLASS_NAME, classBytes, DUMPER)
.defineClass(true, null);

if (staticConcat) {
Expand Down

0 comments on commit e1ebeef

Please sign in to comment.