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 isWindows() {
 117         return isOs("win");
 118     }
 119 
 120     private static boolean isOs(String osname) {
 121         return osName.toLowerCase().startsWith(osname.toLowerCase());
 122     }
 123 
 124     public static String getOsName() {
 125         return osName;
 126     }
 127 
 128     // Os version support.
 129     private static void init_version() {
 130         String[] osVersionTokens = osVersion.split("\\.");
 131         try {
 132             if (osVersionTokens.length > 0) {
 133                 osVersionMajor = Integer.parseInt(osVersionTokens[0]);
 134                 if (osVersionTokens.length > 1) {
 135                     osVersionMinor = Integer.parseInt(osVersionTokens[1]);
 136                 }
 137             }
 138         } catch (NumberFormatException e) {
 139             osVersionMajor = osVersionMinor = 0;
 140         }
 141     }
 142 
 143     public static String getOsVersion() {
 144         return osVersion;
 145     }
 146 
 147     // Returns major version number from os.version system property.
 148     // E.g. 3 on SLES 11.3 (for the linux kernel version).
 149     public static int getOsVersionMajor() {
 150         if (osVersionMajor == -1) init_version();
 151         return osVersionMajor;
 152     }
 153 
 154     // Returns minor version number from os.version system property.
 155     // E.g. 0 on SLES 11.3 (for the linux kernel version).
 156     public static int getOsVersionMinor() {
 157         if (osVersionMinor == -1) init_version();
 158         return osVersionMinor;
 159     }
 160 
 161     public static boolean isDebugBuild() {
 162         return (jdkDebug.toLowerCase().contains("debug"));
 163     }
 164 
 165     public static boolean isSlowDebugBuild() {
 166         return (jdkDebug.toLowerCase().equals("slowdebug"));
 167     }
 168 
 169     public static boolean isFastDebugBuild() {
 170         return (jdkDebug.toLowerCase().equals("fastdebug"));
 171     }
 172 
 173     public static String getVMVersion() {
 174         return vmVersion;
 175     }
 176 
 177     public static boolean isAArch64() {
 178         return isArch("aarch64");
 179     }
 180 
 181     public static boolean isARM() {
 182         return isArch("arm.*");
 183     }
 184 
 185     public static boolean isPPC() {
 186         return isArch("ppc.*");
 187     }
 188 
 189     // Returns true for IBM z System running linux.
 190     public static boolean isS390x() {
 191         return isArch("s390.*") || isArch("s/390.*") || isArch("zArch_64");
 192     }
 193 
 194     public static boolean isX64() {
 195         // On OSX it's 'x86_64' and on other (Linux and Windows) platforms it's 'amd64'
 196         return isArch("(amd64)|(x86_64)");
 197     }
 198 
 199     public static boolean isX86() {
 200         // On Linux it's 'i386', Windows 'x86' without '_64' suffix.
 201         return isArch("(i386)|(x86(?!_64))");
 202     }
 203 
 204     public static String getOsArch() {
 205         return osArch;
 206     }
 207 
 208     public static boolean isRoot() {
 209         return userName.equals("root");
 210     }
 211 
 212     /**
 213      * Return a boolean for whether SA and jhsdb are ported/available
 214      * on this platform.
 215      */
 216     public static boolean hasSA() {
 217         if (isAix()) {
 218             return false; // SA not implemented.
 219         } else if (isLinux()) {
 220             if (isS390x() || isARM()) {
 221                 return false; // SA not implemented.
 222             }
 223         }
 224         // Other platforms expected to work:
 225         return true;
 226     }
 227 
 228     /**
 229      * Return true if the test JDK is signed, otherwise false. Only valid on OSX.
 230      */
 231     public static boolean isSignedOSX() throws IOException {
 232         // We only care about signed binaries for 10.14 and later (actually 10.14.5, but
 233         // for simplicity we'll also include earlier 10.14 versions).
 234         if (getOsVersionMajor() == 10 && getOsVersionMinor() < 14) {
 235             return false; // assume not signed
 236         }
 237 
 238         // Find the path to the java binary.
 239         String jdkPath = System.getProperty("java.home");
 240         Path javaPath = Paths.get(jdkPath + "/bin/java");
 241         String javaFileName = javaPath.toAbsolutePath().toString();
 242         if (!javaPath.toFile().exists()) {
 243             throw new FileNotFoundException("Could not find file " + javaFileName);
 244         }
 245 
 246         // Run codesign on the java binary.
 247         ProcessBuilder pb = new ProcessBuilder("codesign", "-d", "-v", javaFileName);
 248         pb.redirectError(ProcessBuilder.Redirect.DISCARD);
 249         pb.redirectOutput(ProcessBuilder.Redirect.DISCARD);
 250         Process codesignProcess = pb.start();
 251         try {
 252             if (codesignProcess.waitFor(10, TimeUnit.SECONDS) == false) {
 253                 System.err.println("Timed out waiting for the codesign process to complete. Assuming not signed.");
 254                 codesignProcess.destroyForcibly();
 255                 return false; // assume not signed
 256             }
 257         } catch (InterruptedException e) {
 258             throw new RuntimeException(e);
 259         }
 260 
 261         // Check codesign result to see if java binary is signed. Here are the
 262         // exit code meanings:
 263         //    0: signed
 264         //    1: not signed
 265         //    2: invalid arguments
 266         //    3: only has meaning with the -R argument.
 267         // So we should always get 0 or 1 as an exit value.
 268         if (codesignProcess.exitValue() == 0) {
 269             System.out.println("Target JDK is signed. Some tests may be skipped.");
 270             return true; // signed
 271         } else if (codesignProcess.exitValue() == 1) {
 272             System.out.println("Target JDK is not signed.");
 273             return false; // not signed
 274         } else {
 275             System.err.println("Executing codesign failed. Assuming unsigned: " +
 276                                codesignProcess.exitValue());
 277             return false; // not signed
 278         }
 279     }
 280 
 281     private static boolean isArch(String archnameRE) {
 282         return Pattern.compile(archnameRE, Pattern.CASE_INSENSITIVE)
 283                       .matcher(osArch)
 284                       .matches();
 285     }
 286 
 287     /**
 288      * Returns file extension of shared library, e.g. "so" on linux, "dll" on windows.
 289      * @return file extension
 290      */
 291     public static String sharedLibraryExt() {
 292         if (isWindows()) {
 293             return "dll";
 294         } else if (isOSX()) {
 295             return "dylib";
 296         } else {
 297             return "so";
 298         }
 299     }
 300 
 301     /*
 302      * Returns name of system variable containing paths to shared native libraries.
 303      */
 304     public static String sharedLibraryPathVariableName() {
 305         if (isWindows()) {
 306             return "PATH";
 307         } else if (isOSX()) {
 308             return "DYLD_LIBRARY_PATH";
 309         } else if (isAix()) {
 310             return "LIBPATH";
 311         } else {
 312             return "LD_LIBRARY_PATH";
 313         }
 314     }
 315 
 316     /**
 317      * Returns absolute path to directory containing shared libraries in the tested JDK.
 318      */
 319     public static Path libDir() {
 320         Path dir = Paths.get(testJdk);
 321         if (Platform.isWindows()) {
 322             return dir.resolve("bin").toAbsolutePath();
 323         } else {
 324             return dir.resolve("lib").toAbsolutePath();
 325         }
 326     }
 327 
 328     /**
 329      * Returns absolute path to directory containing JVM shared library.
 330      */
 331     public static Path jvmLibDir() {
 332         return libDir().resolve(variant());
 333     }
 334 
 335     private static String variant() {
 336         if (Platform.isServer()) {
 337             return "server";
 338         } else if (Platform.isClient()) {
 339             return "client";
 340         } else if (Platform.isMinimal()) {
 341             return "minimal";
 342         } else {
 343             throw new Error("TESTBUG: unsupported vm variant");
 344         }
 345     }
 346 
 347 
 348     public static boolean isDefaultCDSArchiveSupported() {
 349         return (is64bit()  &&
 350                 isServer() &&
 351                 (isLinux()   ||
 352                  isOSX()     ||
 353                  isWindows()) &&
 354                 !isZero()    &&
 355                 !isMinimal() &&
 356                 !isAArch64() &&
 357                 !isARM());
 358     }
 359 
 360     /*
 361      * This should match the #if condition in ClassListParser::load_class_from_source().
 362      */
 363     public static boolean areCustomLoadersSupportedForCDS() {
 364         return (is64bit() && (isLinux() || isOSX()));
 365     }
 366 }