1 /*
   2  * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 package org.graalvm.compiler.hotspot;
  26 
  27 import java.util.Formatter;
  28 import java.util.HashMap;
  29 import java.util.Map;
  30 import java.util.Properties;
  31 
  32 /**
  33  * Mechanism for checking that the current Java runtime environment supports the minimum JVMCI API
  34  * required by Graal. The {@code JVMCI_VERSION_CHECK} environment variable can be used to ignore a
  35  * failed check ({@code JVMCI_VERSION_CHECK=ignore}) or print a warning (
  36  * {@code JVMCI_VERSION_CHECK=warn}) and continue. Otherwise, a failed check results in an
  37  * {@link InternalError} being raised or, if called from {@link #main(String[])}, the VM exiting
  38  * with a result code of {@code -1}
  39  *
  40  * This class only depends on the JDK so that it can be used without building Graal.
  41  */
  42 class JVMCIVersionCheck {
  43 
  44     // 0.57 introduces HotSpotJVMCIRuntime.excludeFromJVMCICompilation
  45     private static final int JVMCI8_MIN_MAJOR_VERSION = 0;
  46     private static final int JVMCI8_MIN_MINOR_VERSION = 57;
  47 
  48     private static void failVersionCheck(Map<String, String> props, boolean exit, String reason, Object... args) {
  49         Formatter errorMessage = new Formatter().format(reason, args);
  50         String javaHome = props.get("java.home");
  51         String vmName = props.get("java.vm.name");
  52         errorMessage.format("Set the JVMCI_VERSION_CHECK environment variable to \"ignore\" to suppress ");
  53         errorMessage.format("this error or to \"warn\" to emit a warning and continue execution.%n");
  54         errorMessage.format("Currently used Java home directory is %s.%n", javaHome);
  55         errorMessage.format("Currently used VM configuration is: %s%n", vmName);
  56         if (props.get("java.specification.version").compareTo("1.9") < 0) {
  57             errorMessage.format("Download the latest JVMCI JDK 8 from " +
  58                             "http://www.oracle.com/technetwork/oracle-labs/program-languages/downloads/index.html or " +
  59                             "https://github.com/graalvm/openjdk8-jvmci-builder/releases");
  60         } else {
  61             errorMessage.format("Download JDK 11 or later.");
  62         }
  63         String value = System.getenv("JVMCI_VERSION_CHECK");
  64         if ("warn".equals(value)) {
  65             System.err.println(errorMessage.toString());
  66         } else if ("ignore".equals(value)) {
  67             return;
  68         } else if (exit) {
  69             System.err.println(errorMessage.toString());
  70             System.exit(-1);
  71         } else {
  72             throw new InternalError(errorMessage.toString());
  73         }
  74     }
  75 
  76     static void check(Map<String, String> props, boolean exitOnFailure) {
  77         // Don't use regular expressions to minimize Graal startup time
  78         String javaSpecVersion = props.get("java.specification.version");
  79         String vmVersion = props.get("java.vm.version");
  80         if (javaSpecVersion.compareTo("1.9") < 0) {
  81             int start = vmVersion.indexOf("-jvmci-");
  82             if (start >= 0) {
  83                 start += "-jvmci-".length();
  84                 int end = vmVersion.indexOf('.', start);
  85                 if (end > 0) {
  86                     int major;
  87                     try {
  88                         major = Integer.parseInt(vmVersion.substring(start, end));
  89                     } catch (NumberFormatException e) {
  90                         failVersionCheck(props, exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal.%n" +
  91                                         "Cannot read JVMCI major version from java.vm.version property: %s.%n", vmVersion);
  92                         return;
  93                     }
  94                     start = end + 1;
  95                     end = start;
  96                     while (end < vmVersion.length() && Character.isDigit(vmVersion.charAt(end))) {
  97                         end++;
  98                     }
  99                     int minor;
 100                     try {
 101                         minor = Integer.parseInt(vmVersion.substring(start, end));
 102                     } catch (NumberFormatException e) {
 103                         failVersionCheck(props, exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal.%n" +
 104                                         "Cannot read JVMCI minor version from java.vm.version property: %s.%n", vmVersion);
 105                         return;
 106                     }
 107                     if (major >= JVMCI8_MIN_MAJOR_VERSION && minor >= JVMCI8_MIN_MINOR_VERSION) {
 108                         return;
 109                     }
 110                     failVersionCheck(props, exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal: %d.%d < %d.%d.%n",
 111                                     major, minor, JVMCI8_MIN_MAJOR_VERSION, JVMCI8_MIN_MINOR_VERSION);
 112                     return;
 113                 }
 114             }
 115             failVersionCheck(props, exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal.%n" +
 116                             "Cannot read JVMCI version from java.vm.version property: %s.%n", vmVersion);
 117         } else if (javaSpecVersion.compareTo("11") < 0) {
 118             failVersionCheck(props, exitOnFailure, "Graal is not compatible with the JVMCI API in JDK 9 and 10.%n");
 119         } else {
 120             if (vmVersion.contains("SNAPSHOT")) {
 121                 return;
 122             }
 123             if (vmVersion.contains("internal")) {
 124                 // Allow local builds
 125                 return;
 126             }
 127             if (vmVersion.startsWith("11-ea+")) {
 128                 String buildString = vmVersion.substring("11-ea+".length());
 129                 try {
 130                     int build = Integer.parseInt(buildString);
 131                     if (build < 20) {
 132                         failVersionCheck(props, exitOnFailure, "Graal requires build 20 or later of JDK 11 early access binary, got build %d.%n", build);
 133                         return;
 134                     }
 135                 } catch (NumberFormatException e) {
 136                     failVersionCheck(props, exitOnFailure, "Could not parse the JDK 11 early access build number from java.vm.version property: %s.%n", vmVersion);
 137                     return;
 138                 }
 139             } else {
 140                 // Graal is compatible with all JDK versions as of 11 GA.
 141             }
 142         }
 143     }
 144 
 145     /**
 146      * Command line interface for performing the check.
 147      */
 148     public static void main(String[] args) {
 149         Properties sprops = System.getProperties();
 150         Map<String, String> props = new HashMap<>(sprops.size());
 151         for (String name : sprops.stringPropertyNames()) {
 152             props.put(name, sprops.getProperty(name));
 153         }
 154         check(props, true);
 155     }
 156 }