changeset 24547:bd32be47ba3e

restrict service lookup via Services to only providers on the JVMCI class path
author Doug Simon <doug.simon@oracle.com>
date Tue, 21 Nov 2017 17:19:01 +0100
parents 56f70a206b8c
children 92aafe53cdb8
files jvmci/jdk.vm.ci.services/src/jdk/vm/ci/services/Services.java
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	Tue Nov 21 17:19:01 2017 +0100
@@ -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;