changeset 24548:92aafe53cdb8

[GR-7046] Restrict lookup via Services to only providers on the JVMCI class path.
author Doug Simon <doug.simon@oracle.com>
date Thu, 30 Nov 2017 09:03:06 -0800
parents 56f70a206b8c bd32be47ba3e
children 5d5f20c8290a 65570408a42e
files
diffstat 1 files changed, 16 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/jvmci/jdk.vm.ci.services/src/jdk/vm/ci/services/Services.java	Fri Nov 17 03:59:19 2017 -0800
+++ b/jvmci/jdk.vm.ci.services/src/jdk/vm/ci/services/Services.java	Thu Nov 30 09:03:06 2017 -0800
@@ -79,11 +79,27 @@
 
     private static boolean jvmciEnabled = true;
 
+    /**
+     * When {@code -XX:-UseJVMCIClassLoader} is in use, JVMCI classes are loaded via the boot class
+     * loader. When {@code null} is the second argument to
+     * {@link ServiceLoader#load(Class, ClassLoader)}, service lookup will use the system class
+     * loader and thus find application classes which violates the API of {@link #load} and
+     * {@link #loadSingle}. To avoid this, a class loader that simply delegates to the boot class
+     * loader is used.
+     */
+    static class LazyBootClassPath {
+        static final ClassLoader bootClassPath = new ClassLoader(null) {
+        };
+    }
+
     private static <S> Iterable<S> load0(Class<S> service) {
         if (jvmciEnabled) {
             ClassLoader cl = null;
             try {
                 cl = getJVMCIClassLoader();
+                if (cl == null) {
+                    cl = LazyBootClassPath.bootClassPath;
+                }
                 return ServiceLoader.load(service, cl);
             } catch (UnsatisfiedLinkError e) {
                 jvmciEnabled = false;