1 /*
   2  * Copyright (c) 2013, 2020, 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 package jdk.test.lib;
  25 
  26 import java.io.File;
  27 import java.io.FileNotFoundException;
  28 import java.io.IOException;
  29 import java.nio.file.Path;
  30 import java.nio.file.Paths;
  31 import java.security.AccessController;
  32 import java.security.PrivilegedAction;
  33 import java.util.concurrent.TimeUnit;
  34 import java.util.regex.Pattern;
  35 
  36 public class Platform {
  37     public  static final String vmName      = privilegedGetProperty("java.vm.name");
  38     public  static final String vmInfo      = privilegedGetProperty("java.vm.info");
  39     private static final String osVersion   = privilegedGetProperty("os.version");
  40     private static       int osVersionMajor = -1;
  41     private static       int osVersionMinor = -1;
  42     private static final String osName      = privilegedGetProperty("os.name");
  43     private static final String dataModel   = privilegedGetProperty("sun.arch.data.model");
  44     private static final String vmVersion   = privilegedGetProperty("java.vm.version");
  45     private static final String jdkDebug    = privilegedGetProperty("jdk.debug");
  46     private static final String osArch      = privilegedGetProperty("os.arch");
  47     private static final String userName    = privilegedGetProperty("user.name");
  48     private static final String compiler    = privilegedGetProperty("sun.management.compiler");
  49     private static final String testJdk     = privilegedGetProperty("test.jdk");
  50 
  51     private static String privilegedGetProperty(String key) {
  52         return AccessController.doPrivileged((
  53                 PrivilegedAction<String>) () -> System.getProperty(key));
  54     }
  55 
  56     public static boolean isClient() {
  57         return vmName.endsWith(" Client VM");
  58     }
  59 
  60     public static boolean isServer() {
  61         return vmName.endsWith(" Server VM");
  62     }
  63 
  64     public static boolean isZero() {
  65         return vmName.endsWith(" Zero VM");
  66     }
  67 
  68     public static boolean isMinimal() {
  69         return vmName.endsWith(" Minimal VM");
  70     }
  71 
  72     public static boolean isEmbedded() {
  73         return vmName.contains("Embedded");
  74     }
  75 
  76     public static boolean isEmulatedClient() {
  77         return vmInfo.contains(" emulated-client");
  78     }
  79 
  80     public static boolean isTieredSupported() {
  81         return compiler.contains("Tiered Compilers");
  82     }
  83 
  84     public static boolean isInt() {
  85         return vmInfo.contains("interpreted");
  86     }
  87 
  88     public static boolean isMixed() {
  89         return vmInfo.contains("mixed");
  90     }
  91 
  92     public static boolean isComp() {
  93         return vmInfo.contains("compiled");
  94     }
  95 
  96     public static boolean is32bit() {
  97         return dataModel.equals("32");
  98     }
  99 
 100     public static boolean is64bit() {
 101         return dataModel.equals("64");
 102     }
 103 
 104     public static boolean isAix() {
 105         return isOs("aix");
 106     }
 107 
 108     public static boolean isLinux() {
 109         return isOs("linux");
 110     }
 111 
 112     public static boolean isOSX() {
 113         return isOs("mac");
 114     }
 115 
 116     public static boolean isSolaris() {
 117         return isOs("sunos");
 118     }
 119 
 120     public static boolean isWindows() {
 121         return isOs("win");
 122     }
 123 
 124     private static boolean isOs(String osname) {
 125         return osName.toLowerCase().startsWith(osname.toLowerCase());
 126     }
 127 
 128     public static String getOsName() {
 129         return osName;
 130     }
 131 
 132     // Os version support.
 133     private static void init_version() {
 134         String[] osVersionTokens = osVersion.split("\\.");
 135         try {
 136             if (osVersionTokens.length > 0) {
 137                 osVersionMajor = Integer.parseInt(osVersionTokens[0]);
 138                 if (osVersionTokens.length > 1) {
 139                     osVersionMinor = Integer.parseInt(osVersionTokens[1]);
 140                 }
 141             }
 142         } catch (NumberFormatException e) {
 143             osVersionMajor = osVersionMinor = 0;
 144         }
 145     }
 146 
 147     public static String getOsVersion() {
 148         return osVersion;
 149     }
 150 
 151     // Returns major version number from os.version system property.
 152     // E.g. 5 on Solaris 10 and 3 on SLES 11.3 (for the linux kernel version).
 153     public static int getOsVersionMajor() {
 154         if (osVersionMajor == -1) init_version();
 155         return osVersionMajor;
 156     }
 157 
 158     // Returns minor version number from os.version system property.
 159     // E.g. 10 on Solaris 10 and 0 on SLES 11.3 (for the linux kernel version).
 160     public static int getOsVersionMinor() {
 161         if (osVersionMinor == -1) init_version();
 162         return osVersionMinor;
 163     }
 164 
 165     public static boolean isDebugBuild() {
 166         return (jdkDebug.toLowerCase().contains("debug"));
 167     }
 168 
 169     public static boolean isSlowDebugBuild() {
 170         return (jdkDebug.toLowerCase().equals("slowdebug"));
 171     }
 172 
 173     public static boolean isFastDebugBuild() {
 174         return (jdkDebug.toLowerCase().equals("fastdebug"));
 175     }
 176 
 177     public static String getVMVersion() {
 178         return vmVersion;
 179     }
 180 
 181     public static boolean isAArch64() {
 182         return isArch("aarch64");
 183     }
 184 
 185     public static boolean isARM() {
 186         return isArch("arm.*");
 187     }
 188 
 189     public static boolean isPPC() {
 190         return isArch("ppc.*");
 191     }
 192 
 193     // Returns true for IBM z System running linux.
 194     public static boolean isS390x() {
 195         return isArch("s390.*") || isArch("s/390.*") || isArch("zArch_64");
 196     }
 197 
 198     // Returns true for sparc and sparcv9.
 199     public static boolean isSparc() {
 200         return isArch("sparc.*");
 201     }
 202 
 203     public static boolean isX64() {
 204         // On OSX it's 'x86_64' and on other (Linux, Windows and Solaris) platforms it's 'amd64'
 205         return isArch("(amd64)|(x86_64)");
 206     }
 207 
 208     public static boolean isX86() {
 209         // On Linux it's 'i386', Windows 'x86' without '_64' suffix.
 210         return isArch("(i386)|(x86(?!_64))");
 211     }
 212 
 213     public static String getOsArch() {
 214         return osArch;
 215     }
 216 
 217     public static boolean isRoot() {
 218         return userName.equals("root");
 219     }
 220 
 221     /**
 222      * Return a boolean for whether SA and jhsdb are ported/available
 223      * on this platform.
 224      */
 225     public static boolean hasSA() {
 226         if (isAix()) {
 227             return false; // SA not implemented.
 228         } else if (isSolaris()) {
 229             return false; // Testing disabled due to JDK-8193639.
 230         } else if (isLinux()) {
 231             if (isS390x() || isARM()) {
 232                 return false; // SA not implemented.
 233             }
 234         }
 235         // Other platforms expected to work:
 236         return true;
 237     }
 238 
 239     /**
 240      * Return true if the test JDK is signed, otherwise false. Only valid on OSX.
 241      */
 242     public static boolean isSignedOSX() throws IOException {
 243         // We only care about signed binaries for 10.14 and later (actually 10.14.5, but
 244         // for simplicity we'll also include earlier 10.14 versions).
 245         if (getOsVersionMajor() == 10 && getOsVersionMinor() < 14) {
 246             return false; // assume not signed
 247         }
 248 
 249         // Find the path to the java binary.
 250         String jdkPath = System.getProperty("java.home");
 251         Path javaPath = Paths.get(jdkPath + "/bin/java");
 252         String javaFileName = javaPath.toAbsolutePath().toString();
 253         if (!javaPath.toFile().exists()) {
 254             throw new FileNotFoundException("Could not find file " + javaFileName);
 255         }
 256 
 257         // Run codesign on the java binary.
 258         ProcessBuilder pb = new ProcessBuilder("codesign", "-d", "-v", javaFileName);
 259         pb.redirectError(ProcessBuilder.Redirect.DISCARD);
 260         pb.redirectOutput(ProcessBuilder.Redirect.DISCARD);
 261         Process codesignProcess = pb.start();
 262         try {
 263             if (codesignProcess.waitFor(10, TimeUnit.SECONDS) == false) {
 264                 System.err.println("Timed out waiting for the codesign process to complete. Assuming not signed.");
 265                 codesignProcess.destroyForcibly();
 266                 return false; // assume not signed
 267             }
 268         } catch (InterruptedException e) {
 269             throw new RuntimeException(e);
 270         }
 271 
 272         // Check codesign result to see if java binary is signed. Here are the
 273         // exit code meanings:
 274         //    0: signed
 275         //    1: not signed
 276         //    2: invalid arguments
 277         //    3: only has meaning with the -R argument.
 278         // So we should always get 0 or 1 as an exit value.
 279         if (codesignProcess.exitValue() == 0) {
 280             System.out.println("Target JDK is signed. Some tests may be skipped.");
 281             return true; // signed
 282         } else if (codesignProcess.exitValue() == 1) {
 283             System.out.println("Target JDK is not signed.");
 284             return false; // not signed
 285         } else {
 286             System.err.println("Executing codesign failed. Assuming unsigned: " +
 287                                codesignProcess.exitValue());
 288             return false; // not signed
 289         }
 290     }
 291 
 292     private static boolean isArch(String archnameRE) {
 293         return Pattern.compile(archnameRE, Pattern.CASE_INSENSITIVE)
 294                       .matcher(osArch)
 295                       .matches();
 296     }
 297 
 298     /**
 299      * Returns file extension of shared library, e.g. "so" on linux, "dll" on windows.
 300      * @return file extension
 301      */
 302     public static String sharedLibraryExt() {
 303         if (isWindows()) {
 304             return "dll";
 305         } else if (isOSX()) {
 306             return "dylib";
 307         } else {
 308             return "so";
 309         }
 310     }
 311 
 312     /*
 313      * Returns name of system variable containing paths to shared native libraries.
 314      */
 315     public static String sharedLibraryPathVariableName() {
 316         if (isWindows()) {
 317             return "PATH";
 318         } else if (isOSX()) {
 319             return "DYLD_LIBRARY_PATH";
 320         } else if (isAix()) {
 321             return "LIBPATH";
 322         } else {
 323             return "LD_LIBRARY_PATH";
 324         }
 325     }
 326 
 327     /**
 328      * Returns absolute path to directory containing shared libraries in the tested JDK.
 329      */
 330     public static Path libDir() {
 331         Path dir = Paths.get(testJdk);
 332         if (Platform.isWindows()) {
 333             return dir.resolve("bin").toAbsolutePath();
 334         } else {
 335             return dir.resolve("lib").toAbsolutePath();
 336         }
 337     }
 338 
 339     /**
 340      * Returns absolute path to directory containing JVM shared library.
 341      */
 342     public static Path jvmLibDir() {
 343         return libDir().resolve(variant());
 344     }
 345 
 346     private static String variant() {
 347         if (Platform.isServer()) {
 348             return "server";
 349         } else if (Platform.isClient()) {
 350             return "client";
 351         } else if (Platform.isMinimal()) {
 352             return "minimal";
 353         } else {
 354             throw new Error("TESTBUG: unsupported vm variant");
 355         }
 356     }
 357 
 358 
 359     public static boolean isDefaultCDSArchiveSupported() {
 360         return (is64bit()  &&
 361                 isServer() &&
 362                 (isLinux()   ||
 363                  isOSX()     ||
 364                  isSolaris() ||
 365                  isWindows()) &&
 366                 !isZero()    &&
 367                 !isMinimal() &&
 368                 !isAArch64() &&
 369                 !isARM());
 370     }
 371 
 372     /*
 373      * This should match the #if condition in ClassListParser::load_class_from_source().
 374      */
 375     public static boolean areCustomLoadersSupportedForCDS() {
 376         return (is64bit() && (isLinux() || isSolaris() || isOSX()));
 377     }
 378 }