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