1 /*
   2  * Copyright (c) 2016, 2018, 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 
  29 /**
  30  * Mechanism for checking that the current Java runtime environment supports the minimum JVMCI API
  31  * required by Graal. The {@code JVMCI_VERSION_CHECK} environment variable can be used to ignore a
  32  * failed check ({@code JVMCI_VERSION_CHECK=ignore}) or print a warning (
  33  * {@code JVMCI_VERSION_CHECK=warn}) and continue. Otherwise, a failed check results in an
  34  * {@link InternalError} being raised or, if called from {@link #main(String[])}, the VM exiting
  35  * with a result code of {@code -1}
  36  *
  37  * This class only depends on the JDK so that it can be used without building Graal.
  38  */
  39 class JVMCIVersionCheck {
  40 
  41     private static final int JVMCI8_MIN_MAJOR_VERSION = 0;
  42     private static final int JVMCI8_MIN_MINOR_VERSION = 46;
  43 
  44     private static void failVersionCheck(boolean exit, String reason, Object... args) {
  45         Formatter errorMessage = new Formatter().format(reason, args);
  46         String javaHome = System.getProperty("java.home");
  47         String vmName = System.getProperty("java.vm.name");
  48         errorMessage.format("Set the JVMCI_VERSION_CHECK environment variable to \"ignore\" to suppress ");
  49         errorMessage.format("this error or to \"warn\" to emit a warning and continue execution.%n");
  50         errorMessage.format("Currently used Java home directory is %s.%n", javaHome);
  51         errorMessage.format("Currently used VM configuration is: %s%n", vmName);
  52         if (System.getProperty("java.specification.version").compareTo("1.9") < 0) {
  53             errorMessage.format("Download the latest JVMCI JDK 8 from http://www.oracle.com/technetwork/oracle-labs/program-languages/downloads/index.html");
  54         } else {
  55             errorMessage.format("Download JDK 11 or later.");
  56         }
  57         String value = System.getenv("JVMCI_VERSION_CHECK");
  58         if ("warn".equals(value)) {
  59             System.err.println(errorMessage.toString());
  60         } else if ("ignore".equals(value)) {
  61             return;
  62         } else if (exit) {
  63             System.err.println(errorMessage.toString());
  64             System.exit(-1);
  65         } else {
  66             throw new InternalError(errorMessage.toString());
  67         }
  68     }
  69 
  70     static void check(boolean exitOnFailure) {
  71         // Don't use regular expressions to minimize Graal startup time
  72         String javaSpecVersion = System.getProperty("java.specification.version");
  73         String vmVersion = System.getProperty("java.vm.version");
  74         if (javaSpecVersion.compareTo("1.9") < 0) {
  75             int start = vmVersion.indexOf("-jvmci-");
  76             if (start >= 0) {
  77                 start += "-jvmci-".length();
  78                 int end = vmVersion.indexOf('.', start);
  79                 if (end > 0) {
  80                     int major;
  81                     try {
  82                         major = Integer.parseInt(vmVersion.substring(start, end));
  83                     } catch (NumberFormatException e) {
  84                         failVersionCheck(exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal.%n" +
  85                                         "Cannot read JVMCI major version from java.vm.version property: %s.%n", vmVersion);
  86                         return;
  87                     }
  88                     start = end + 1;
  89                     end = start;
  90                     while (end < vmVersion.length() && Character.isDigit(vmVersion.charAt(end))) {
  91                         end++;
  92                     }
  93                     int minor;
  94                     try {
  95                         minor = Integer.parseInt(vmVersion.substring(start, end));
  96                     } catch (NumberFormatException e) {
  97                         failVersionCheck(exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal.%n" +
  98                                         "Cannot read JVMCI minor version from java.vm.version property: %s.%n", vmVersion);
  99                         return;
 100                     }
 101                     if (major >= JVMCI8_MIN_MAJOR_VERSION && minor >= JVMCI8_MIN_MINOR_VERSION) {
 102                         return;
 103                     }
 104                     failVersionCheck(exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal: %d.%d < %d.%d.%n",
 105                                     major, minor, JVMCI8_MIN_MAJOR_VERSION, JVMCI8_MIN_MINOR_VERSION);
 106                     return;
 107                 }
 108             }
 109             failVersionCheck(exitOnFailure, "The VM does not support the minimum JVMCI API version required by Graal.%n" +
 110                             "Cannot read JVMCI version from java.vm.version property: %s.%n", vmVersion);
 111         } else if (javaSpecVersion.compareTo("11") < 0) {
 112             failVersionCheck(exitOnFailure, "Graal is not compatible with the JVMCI API in JDK 9 and 10.%n");
 113         } else {
 114             if (vmVersion.contains("SNAPSHOT")) {
 115                 return;
 116             }
 117             if (vmVersion.contains("internal")) {
 118                 // Allow local builds
 119                 return;
 120             }
 121             if (vmVersion.startsWith("11-ea+")) {
 122                 String buildString = vmVersion.substring("11-ea+".length());
 123                 try {
 124                     int build = Integer.parseInt(buildString);
 125                     if (build < 20) {
 126                         failVersionCheck(exitOnFailure, "Graal requires build 20 or later of JDK 11 early access binary, got build %d.%n", build);
 127                         return;
 128                     }
 129                 } catch (NumberFormatException e) {
 130                     failVersionCheck(exitOnFailure, "Could not parse the JDK 11 early access build number from java.vm.version property: %s.%n", vmVersion);
 131                     return;
 132                 }
 133             } else {
 134                 // Graal is compatible with all JDK versions as of 11 GA.
 135             }
 136         }
 137     }
 138 
 139     /**
 140      * Command line interface for performing the check.
 141      */
 142     public static void main(String[] args) {
 143         check(true);
 144     }
 145 }