1 /*
   2  * Copyright (c) 2013, 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 package jdk.test.lib;
  25 
  26 import java.io.File;
  27 import java.io.IOException;
  28 import java.io.RandomAccessFile;
  29 import java.util.regex.Pattern;
  30 import java.security.AccessController;
  31 import java.security.PrivilegedAction;
  32 import java.security.PrivilegedActionException;
  33 import java.security.PrivilegedExceptionAction;
  34 
  35 public class Platform {
  36     public  static final String vmName      = privilegedGetProperty("java.vm.name");
  37     public  static final String vmInfo      = privilegedGetProperty("java.vm.info");
  38     private static final String osVersion   = privilegedGetProperty("os.version");
  39     private static       int osVersionMajor = -1;
  40     private static       int osVersionMinor = -1;
  41     private static final String osName      = privilegedGetProperty("os.name");
  42     private static final String dataModel   = privilegedGetProperty("sun.arch.data.model");
  43     private static final String vmVersion   = privilegedGetProperty("java.vm.version");
  44     private static final String jdkDebug    = privilegedGetProperty("jdk.debug");
  45     private static final String osArch      = privilegedGetProperty("os.arch");
  46     private static final String userName    = privilegedGetProperty("user.name");
  47     private static final String compiler    = privilegedGetProperty("sun.management.compiler");
  48 
  49     private static String privilegedGetProperty(String key) {
  50         return AccessController.doPrivileged((
  51                 PrivilegedAction<String>) () -> System.getProperty(key));
  52     }
  53 
  54     public static boolean isClient() {
  55         return vmName.endsWith(" Client VM");
  56     }
  57 
  58     public static boolean isServer() {
  59         return vmName.endsWith(" Server VM");
  60     }
  61 
  62     public static boolean isGraal() {
  63         return vmName.endsWith(" Graal VM");
  64     }
  65 
  66     public static boolean isZero() {
  67         return vmName.endsWith(" Zero VM");
  68     }
  69 
  70     public static boolean isMinimal() {
  71         return vmName.endsWith(" Minimal VM");
  72     }
  73 
  74     public static boolean isEmbedded() {
  75         return vmName.contains("Embedded");
  76     }
  77 
  78     public static boolean isEmulatedClient() {
  79         return vmInfo.contains(" emulated-client");
  80     }
  81 
  82     public static boolean isTieredSupported() {
  83         return compiler.contains("Tiered Compilers");
  84     }
  85 
  86     public static boolean isInt() {
  87         return vmInfo.contains("interpreted");
  88     }
  89 
  90     public static boolean isMixed() {
  91         return vmInfo.contains("mixed");
  92     }
  93 
  94     public static boolean isComp() {
  95         return vmInfo.contains("compiled");
  96     }
  97 
  98     public static boolean is32bit() {
  99         return dataModel.equals("32");
 100     }
 101 
 102     public static boolean is64bit() {
 103         return dataModel.equals("64");
 104     }
 105 
 106     public static boolean isAix() {
 107         return isOs("aix");
 108     }
 109 
 110     public static boolean isLinux() {
 111         return isOs("linux");
 112     }
 113 
 114     public static boolean isOSX() {
 115         return isOs("mac");
 116     }
 117 
 118     public static boolean isSolaris() {
 119         return isOs("sunos");
 120     }
 121 
 122     public static boolean isWindows() {
 123         return isOs("win");
 124     }
 125 
 126     private static boolean isOs(String osname) {
 127         return osName.toLowerCase().startsWith(osname.toLowerCase());
 128     }
 129 
 130     public static String getOsName() {
 131         return osName;
 132     }
 133 
 134     // Os version support.
 135     private static void init_version() {
 136         String[] osVersionTokens = osVersion.split("\\.");
 137         try {
 138             if (osVersionTokens.length > 0) {
 139                 osVersionMajor = Integer.parseInt(osVersionTokens[0]);
 140                 if (osVersionTokens.length > 1) {
 141                     osVersionMinor = Integer.parseInt(osVersionTokens[1]);
 142                 }
 143             }
 144         } catch (NumberFormatException e) {
 145             osVersionMajor = osVersionMinor = 0;
 146         }
 147     }
 148 
 149     public static String getOsVersion() {
 150         return osVersion;
 151     }
 152 
 153     // Returns major version number from os.version system property.
 154     // E.g. 5 on Solaris 10 and 3 on SLES 11.3 (for the linux kernel version).
 155     public static int getOsVersionMajor() {
 156         if (osVersionMajor == -1) init_version();
 157         return osVersionMajor;
 158     }
 159 
 160     // Returns minor version number from os.version system property.
 161     // E.g. 10 on Solaris 10 and 0 on SLES 11.3 (for the linux kernel version).
 162     public static int getOsVersionMinor() {
 163         if (osVersionMinor == -1) init_version();
 164         return osVersionMinor;
 165     }
 166 
 167     public static boolean isDebugBuild() {
 168         return (jdkDebug.toLowerCase().contains("debug"));
 169     }
 170 
 171     public static boolean isSlowDebugBuild() {
 172         return (jdkDebug.toLowerCase().equals("slowdebug"));
 173     }
 174 
 175     public static boolean isFastDebugBuild() {
 176         return (jdkDebug.toLowerCase().equals("fastdebug"));
 177     }
 178 
 179     public static String getVMVersion() {
 180         return vmVersion;
 181     }
 182 
 183     public static boolean isAArch64() {
 184         return isArch("aarch64");
 185     }
 186 
 187     public static boolean isARM() {
 188         return isArch("arm.*");
 189     }
 190 
 191     public static boolean isPPC() {
 192         return isArch("ppc.*");
 193     }
 194 
 195     // Returns true for IBM z System running linux.
 196     public static boolean isS390x() {
 197         return isArch("s390.*") || isArch("s/390.*") || isArch("zArch_64");
 198     }
 199 
 200     // Returns true for sparc and sparcv9.
 201     public static boolean isSparc() {
 202         return isArch("sparc.*");
 203     }
 204 
 205     public static boolean isX64() {
 206         // On OSX it's 'x86_64' and on other (Linux, Windows and Solaris) platforms it's 'amd64'
 207         return isArch("(amd64)|(x86_64)");
 208     }
 209 
 210     public static boolean isX86() {
 211         // On Linux it's 'i386', Windows 'x86' without '_64' suffix.
 212         return isArch("(i386)|(x86(?!_64))");
 213     }
 214 
 215     public static String getOsArch() {
 216         return osArch;
 217     }
 218 
 219     /**
 220      * Return a boolean for whether SA and jhsdb are ported/available
 221      * on this platform.
 222      */
 223     public static boolean hasSA() {
 224         if (isAix()) {
 225             return false; // SA not implemented.
 226         } else if (isLinux()) {
 227             if (isS390x()) {
 228                 return false; // SA not implemented.
 229             }
 230         }
 231         // Other platforms expected to work:
 232         return true;
 233     }
 234 
 235     /**
 236      * Return a boolean for whether we expect to be able to attach
 237      * the SA to our own processes on this system.  This requires
 238      * that SA is ported/available on this platform.
 239      */
 240     public static boolean shouldSAAttach() throws IOException {
 241         if (!hasSA()) return false;
 242         if (isLinux()) {
 243             return canPtraceAttachLinux();
 244         } else if (isOSX()) {
 245             return canAttachOSX();
 246         } else {
 247             // Other platforms expected to work:
 248             return true;
 249         }
 250     }
 251 
 252     /**
 253      * On Linux, first check the SELinux boolean "deny_ptrace" and return false
 254      * as we expect to be denied if that is "1".  Then expect permission to attach
 255      * if we are root, so return true.  Then return false for an expected denial
 256      * if "ptrace_scope" is 1, and true otherwise.
 257      */
 258     private static boolean canPtraceAttachLinux() throws IOException {
 259         // SELinux deny_ptrace:
 260         File deny_ptrace = new File("/sys/fs/selinux/booleans/deny_ptrace");
 261         if (deny_ptrace.exists()) {
 262             try (RandomAccessFile file = AccessController.doPrivileged(
 263                     (PrivilegedExceptionAction<RandomAccessFile>) () -> new RandomAccessFile(deny_ptrace, "r"))) {
 264                 if (file.readByte() != '0') {
 265                     return false;
 266                 }
 267             } catch (PrivilegedActionException e) {
 268                 @SuppressWarnings("unchecked")
 269                 IOException t = (IOException) e.getException();
 270                 throw t;
 271             }
 272         }
 273 
 274         // YAMA enhanced security ptrace_scope:
 275         // 0 - a process can PTRACE_ATTACH to any other process running under the same uid
 276         // 1 - restricted ptrace: a process must be a children of the inferior or user is root
 277         // 2 - only processes with CAP_SYS_PTRACE may use ptrace or user is root
 278         // 3 - no attach: no processes may use ptrace with PTRACE_ATTACH
 279         File ptrace_scope = new File("/proc/sys/kernel/yama/ptrace_scope");
 280         if (ptrace_scope.exists()) {
 281             try (RandomAccessFile file = AccessController.doPrivileged(
 282                     (PrivilegedExceptionAction<RandomAccessFile>) () -> new RandomAccessFile(ptrace_scope, "r"))) {
 283                 byte yama_scope = file.readByte();
 284                 if (yama_scope == '3') {
 285                     return false;
 286                 }
 287 
 288                 if (!userName.equals("root") && yama_scope != '0') {
 289                     return false;
 290                 }
 291             } catch (PrivilegedActionException e) {
 292                 @SuppressWarnings("unchecked")
 293                 IOException t = (IOException) e.getException();
 294                 throw t;
 295             }
 296         }
 297         // Otherwise expect to be permitted:
 298         return true;
 299     }
 300 
 301     /**
 302      * On OSX, expect permission to attach only if we are root.
 303      */
 304     private static boolean canAttachOSX() {
 305         return userName.equals("root");
 306     }
 307 
 308     private static boolean isArch(String archnameRE) {
 309         return Pattern.compile(archnameRE, Pattern.CASE_INSENSITIVE)
 310                       .matcher(osArch)
 311                       .matches();
 312     }
 313 
 314     /**
 315      * Returns file extension of shared library, e.g. "so" on linux, "dll" on windows.
 316      * @return file extension
 317      */
 318     public static String sharedLibraryExt() {
 319         if (isWindows()) {
 320             return "dll";
 321         } else if (isOSX()) {
 322             return "dylib";
 323         } else {
 324             return "so";
 325         }
 326     }
 327 
 328     public static boolean isDefaultCDSArchiveSupported() {
 329         return (is64bit()  &&
 330                 isServer() &&
 331                 (isLinux()   ||
 332                  isOSX()     ||
 333                  isSolaris() ||
 334                  isWindows()) &&
 335                 !isZero()    &&
 336                 !isMinimal() &&
 337                 !isAArch64() &&
 338                 !isARM());
 339     }
 340 
 341     /*
 342      * This should match the #if condition in ClassListParser::load_class_from_source().
 343      */
 344     public static boolean areCustomLoadersSupportedForCDS() {
 345         return (is64bit() && (isLinux() || isSolaris() || isOSX()));
 346     }
 347 }