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 public final 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     private final String javaSpecVersion;
  77     private final String vmVersion;
  78     private int cursor;
  79     private final Map<String, String> props;
  80 
  81     private JVMCIVersionCheck(Map<String, String> props, String javaSpecVersion, String vmVersion) {
  82         this.props = props;
  83         this.javaSpecVersion = javaSpecVersion;
  84         this.vmVersion = vmVersion;
  85     }
  86 
  87     static void check(Map<String, String> props, boolean exitOnFailure) {
  88         JVMCIVersionCheck checker = new JVMCIVersionCheck(props, props.get("java.specification.version"), props.get("java.vm.version"));
  89         checker.run(exitOnFailure, JVMCI8_MIN_MAJOR_VERSION, JVMCI8_MIN_MINOR_VERSION);
  90     }
  91 
  92     /**
  93      * Entry point for testing.
  94      */
  95     public static void check(Map<String, String> props,
  96                     int jvmci8MinMajorVersion,
  97                     int jvmci8MinMinorVersion,
  98                     String javaSpecVersion,
  99                     String javaVmVersion,
 100                     boolean exitOnFailure) {
 101         JVMCIVersionCheck checker = new JVMCIVersionCheck(props, javaSpecVersion, javaVmVersion);
 102         checker.run(exitOnFailure, jvmci8MinMajorVersion, jvmci8MinMinorVersion);
 103     }
 104 
 105     /**
 106      * Parses a positive decimal number at {@link #cursor}.
 107      *
 108      * @return -1 if there is no positive decimal number at {@link #cursor}
 109      */
 110     private int parseNumber() {
 111         int result = -1;
 112         while (cursor < vmVersion.length()) {
 113             int digit = vmVersion.charAt(cursor) - '0';
 114             if (digit >= 0 && digit <= 9) {
 115                 if (result == -1) {
 116                     result = digit;
 117                 } else {
 118                     long r = (long) result * (long) 10;
 119                     if ((int) r != r) {
 120                         // Overflow
 121                         return -1;
 122                     }
 123                     result = (int) r + digit;
 124                 }
 125                 cursor++;
 126             } else {
 127                 break;
 128             }
 129         }
 130         return result;
 131     }
 132 
 133     /**
 134      * Parse {@code "."} or {@code "-b"} at {@link #cursor}.
 135      *
 136      * @return {@code true} iff there was an expected separator at {@link #cursor}
 137      */
 138     private boolean parseSeparator() {
 139         if (cursor < vmVersion.length()) {
 140             char ch = vmVersion.charAt(cursor);
 141             if (ch == '.') {
 142                 cursor++;
 143                 return true;
 144             }
 145             if (ch == '-') {
 146                 cursor++;
 147                 if (cursor < vmVersion.length()) {
 148                     if (vmVersion.charAt(cursor) == 'b') {
 149                         cursor++;
 150                         return true;
 151                     }
 152                 }
 153                 return false;
 154             }
 155         }
 156         return false;
 157     }
 158 
 159     private void run(boolean exitOnFailure, int jvmci8MinMajorVersion, int jvmci8MinMinorVersion) {
 160         // Don't use regular expressions to minimize Graal startup time
 161         if (javaSpecVersion.compareTo("1.9") < 0) {
 162             cursor = vmVersion.indexOf("-jvmci-");
 163             if (cursor >= 0) {
 164                 cursor += "-jvmci-".length();
 165                 int major = parseNumber();
 166                 if (major == -1) {
 167                     failVersionCheck(props, exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal.%n" +
 168                                     "Cannot read JVMCI major version from java.vm.version property: %s.%n", vmVersion);
 169                     return;
 170                 }
 171 
 172                 if (parseSeparator()) {
 173                     int minor = parseNumber();
 174                     if (minor == -1) {
 175                         failVersionCheck(props, exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal.%n" +
 176                                         "Cannot read JVMCI minor version from java.vm.version property: %s.%n", vmVersion);
 177                         return;
 178                     }
 179 
 180                     if (major > jvmci8MinMajorVersion || (major >= jvmci8MinMajorVersion && minor >= jvmci8MinMinorVersion)) {
 181                         return;
 182                     }
 183                     failVersionCheck(props, exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal: %d.%d < %d.%d.%n",
 184                                     major, minor, jvmci8MinMajorVersion, jvmci8MinMinorVersion);
 185                     return;
 186                 }
 187             }
 188             failVersionCheck(props, exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal.%n" +
 189                             "Cannot read JVMCI version from java.vm.version property: %s.%n", vmVersion);
 190         } else if (javaSpecVersion.compareTo("11") < 0) {
 191             failVersionCheck(props, exitOnFailure, "Graal is not compatible with the JVMCI API in JDK 9 and 10.%n");
 192         } else {
 193             if (vmVersion.contains("SNAPSHOT")) {
 194                 return;
 195             }
 196             if (vmVersion.contains("internal")) {
 197                 // Allow local builds
 198                 return;
 199             }
 200             if (vmVersion.startsWith("11-ea+")) {
 201                 String buildString = vmVersion.substring("11-ea+".length());
 202                 try {
 203                     int build = Integer.parseInt(buildString);
 204                     if (build < 20) {
 205                         failVersionCheck(props, exitOnFailure, "Graal requires build 20 or later of JDK 11 early access binary, got build %d.%n", build);
 206                         return;
 207                     }
 208                 } catch (NumberFormatException e) {
 209                     failVersionCheck(props, exitOnFailure, "Could not parse the JDK 11 early access build number from java.vm.version property: %s.%n", vmVersion);
 210                     return;
 211                 }
 212             } else {
 213                 // Graal is compatible with all JDK versions as of 11 GA.
 214             }
 215         }
 216     }
 217 
 218     /**
 219      * Command line interface for performing the check.
 220      */
 221     public static void main(String[] args) {
 222         Properties sprops = System.getProperties();
 223         Map<String, String> props = new HashMap<>(sprops.size());
 224         for (String name : sprops.stringPropertyNames()) {
 225             props.put(name, sprops.getProperty(name));
 226         }
 227         check(props, true);
 228     }
 229 }