|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package com.oracle.truffle.api.impl; |
| 42 | + |
| 43 | +import com.oracle.truffle.api.CompilerDirectives; |
| 44 | +import org.graalvm.home.Version; |
| 45 | +import org.graalvm.nativeimage.hosted.Feature; |
| 46 | + |
| 47 | +import java.io.BufferedReader; |
| 48 | +import java.io.IOException; |
| 49 | +import java.io.InputStream; |
| 50 | +import java.io.InputStreamReader; |
| 51 | +import java.io.PrintStream; |
| 52 | +import java.nio.charset.StandardCharsets; |
| 53 | + |
| 54 | +public final class TruffleAPIFeature implements Feature { |
| 55 | + |
| 56 | + @Override |
| 57 | + public String getURL() { |
| 58 | + return "https://github.com/oracle/graal/tree/master/truffle/src/com.oracle.truffle.api/src/com/oracle/truffle/api/impl/TruffleAPIFeature.java"; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public String getDescription() { |
| 63 | + return "Provides basic support for Truffle"; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void afterRegistration(AfterRegistrationAccess access) { |
| 68 | + String result = doVersionCheck(); |
| 69 | + if (result != null) { |
| 70 | + // GR-67329: Exceptions thrown by features do not include their error messages in the |
| 71 | + // native-image output |
| 72 | + PrintStream out = System.err; |
| 73 | + out.printf("[%s] %s", getClass().getName(), result); |
| 74 | + throw new IllegalStateException(result); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static String doVersionCheck() { |
| 79 | + if (TruffleVersions.isVersionCheckEnabled()) { |
| 80 | + Version truffleAPIVersion = TruffleVersions.TRUFFLE_API_VERSION; |
| 81 | + Version truffleMajorMinorVersion = stripUpdateVersion(truffleAPIVersion); |
| 82 | + Version truffleSVMVersion = getSVMFeatureVersion(); |
| 83 | + Version truffleSVMMajorMinorVersion = stripUpdateVersion(truffleSVMVersion); |
| 84 | + if (truffleSVMVersion.compareTo(TruffleVersions.NEXT_VERSION_UPDATE) >= 0) { |
| 85 | + throw new AssertionError("MIN_COMPILER_VERSION, MIN_JDK_VERSION and MAX_JDK_VERSION must be updated!"); |
| 86 | + } else if (truffleSVMMajorMinorVersion.compareTo(truffleMajorMinorVersion) > 0) { |
| 87 | + // no forward compatibility |
| 88 | + return formatVersionWarningMessage(""" |
| 89 | + Your Java runtime '%s' with native-image feature version '%s' is incompatible with polyglot version '%s'. |
| 90 | + Update the org.graalvm.polyglot versions to at least '%s' to resolve this. |
| 91 | + """, Runtime.version(), truffleSVMVersion, truffleAPIVersion, truffleSVMVersion); |
| 92 | + } else if (Runtime.version().feature() < TruffleVersions.MIN_JDK_VERSION) { |
| 93 | + return formatVersionWarningMessage(""" |
| 94 | + Your Java runtime '%s' with native-image feature version '%s' is incompatible with polyglot version '%s'. |
| 95 | + The Java runtime version must be greater or equal to JDK '%d'. |
| 96 | + Update your Java runtime to resolve this. |
| 97 | + """, Runtime.version(), truffleSVMVersion, truffleAPIVersion, TruffleVersions.MIN_JDK_VERSION); |
| 98 | + } else if (truffleSVMVersion.compareTo(TruffleVersions.MIN_COMPILER_VERSION) < 0) { |
| 99 | + return formatVersionWarningMessage(""" |
| 100 | + Your Java runtime '%s' with compiler version '%s' is incompatible with polyglot version '%s'. |
| 101 | + Update the Java runtime to the latest update release of JDK '%d'. |
| 102 | + """, Runtime.version(), truffleSVMVersion, truffleAPIVersion, Runtime.version().feature()); |
| 103 | + } |
| 104 | + } |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Reads the version of the Truffle feature. |
| 110 | + */ |
| 111 | + private static Version getSVMFeatureVersion() { |
| 112 | + InputStream in = TruffleAPIFeature.class.getClassLoader().getResourceAsStream("META-INF/graalvm/org.graalvm.truffle.runtime.svm/version"); |
| 113 | + if (in == null) { |
| 114 | + throw CompilerDirectives.shouldNotReachHere("Truffle native image feature must have a version file."); |
| 115 | + } |
| 116 | + try (BufferedReader r = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) { |
| 117 | + return Version.parse(r.readLine()); |
| 118 | + } catch (IOException ioe) { |
| 119 | + throw CompilerDirectives.shouldNotReachHere(ioe); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private static Version stripUpdateVersion(Version version) { |
| 124 | + int major = version.getComponent(0); |
| 125 | + int minor = version.getComponent(1); |
| 126 | + if (major == 0 && minor == 0) { |
| 127 | + /* |
| 128 | + * Version represents a pure snapshot version without any numeric component. |
| 129 | + */ |
| 130 | + return version; |
| 131 | + } else { |
| 132 | + return Version.create(major, minor); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + private static String formatVersionWarningMessage(String errorFormat, Object... args) { |
| 137 | + StringBuilder errorMessage = new StringBuilder("Version check failed.\n"); |
| 138 | + errorMessage.append(String.format(errorFormat, args)); |
| 139 | + errorMessage.append(""" |
| 140 | + To disable this version check the '-Dpolyglotimpl.DisableVersionChecks=true' system property can be used. |
| 141 | + It is not recommended to disable version checks. |
| 142 | + """); |
| 143 | + return errorMessage.toString(); |
| 144 | + } |
| 145 | +} |
0 commit comments