JDK-8154447 : Exempt classes under java.util.concurrent from MH.Lookup restrictions
  • Type: Sub-task
  • Component: core-libs
  • Sub-Component: java.lang.invoke
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • Submitted: 2016-04-18
  • Updated: 2016-05-09
  • Resolved: 2016-04-27
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9 b117Fixed
Related Reports
Relates :  
Relates :  
Description
MethodHandle.Lookup has the following code:

        private static void checkUnprivilegedlookupClass(Class<?> lookupClass, int allowedModes) {
            String name = lookupClass.getName();
            if (name.startsWith("java.lang.invoke."))
                throw newIllegalArgumentException("illegal lookupClass: "+lookupClass);

            // For caller-sensitive MethodHandles.lookup()
            // disallow lookup more restricted packages
            if (allowedModes == ALL_MODES && lookupClass.getClassLoader() == null) {
                if (name.startsWith("java.") ||
                        (name.startsWith("sun.") && !name.startsWith("sun.invoke."))) {
                    throw newIllegalArgumentException("illegal lookupClass: " + lookupClass);
                }
            }
        }

Which is called from the constructor:

        Lookup(Class<?> lookupClass) {
            this(lookupClass, ALL_MODES);
            // make sure we haven't accidentally picked up a privileged class:
            checkUnprivilegedlookupClass(lookupClass, ALL_MODES);
        }

and at the end of the Lookup.in method:

            checkUnprivilegedlookupClass(requestedLookupClass, newModes);
            return new Lookup(requestedLookupClass, newModes);
        }

Thus Lookup instances cannot be created for caller class or "teleporting" class that is in the package

  java.lang.invoke.* for any mode
  java.* and sun.* (except for sun.invoke.*) for ALL_MODES

This means that classes under say java.util.concurrent.* cannot create a Lookup instance to produce a VarHandle.

The "sun.invoke.*" package is an exception since the class ValueConversions looks up methods on itself an on Class (for the public method "cast").

As an interim solution the "java.util.concurrent" package should also be added as an exception.