1 /*
   2  * Copyright (c) 2013, 2016, 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.util.regex.Pattern;
  27 
  28 /**
  29  * @deprecated This class is deprecated. Use the one from
  30  *             {@code <root>/test/lib/share/classes/jdk/test/lib}
  31  */
  32 @Deprecated
  33 public class Platform {
  34     public  static final String vmName      = System.getProperty("java.vm.name");
  35     public  static final String vmInfo      = System.getProperty("java.vm.info");
  36     private static final String osName      = System.getProperty("os.name");
  37     private static final String dataModel   = System.getProperty("sun.arch.data.model");
  38     private static final String vmVersion   = System.getProperty("java.vm.version");
  39     private static final String jdkDebug    = System.getProperty("jdk.debug");
  40     private static final String osArch      = System.getProperty("os.arch");
  41     private static final String userName    = System.getProperty("user.name");
  42     private static final String compiler    = System.getProperty("sun.management.compiler");
  43 
  44     public static boolean isClient() {
  45         return vmName.endsWith(" Client VM");
  46     }
  47 
  48     public static boolean isServer() {
  49         return vmName.endsWith(" Server VM");
  50     }
  51 
  52     public static boolean isGraal() {
  53         return vmName.endsWith(" Graal VM");
  54     }
  55 
  56     public static boolean isZero() {
  57         return vmName.endsWith(" Zero VM");
  58     }
  59 
  60     public static boolean isMinimal() {
  61         return vmName.endsWith(" Minimal VM");
  62     }
  63 
  64     public static boolean isTieredSupported() {
  65         return compiler.contains("Tiered Compilers");
  66     }
  67 
  68     public static boolean isInt() {
  69         return vmInfo.contains("interpreted");
  70     }
  71 
  72     public static boolean isMixed() {
  73         return vmInfo.contains("mixed");
  74     }
  75 
  76     public static boolean isComp() {
  77         return vmInfo.contains("compiled");
  78     }
  79 
  80     public static boolean is32bit() {
  81         return dataModel.equals("32");
  82     }
  83 
  84     public static boolean is64bit() {
  85         return dataModel.equals("64");
  86     }
  87 
  88     public static boolean isAix() {
  89         return isOs("aix");
  90     }
  91 
  92     public static boolean isLinux() {
  93         return isOs("linux");
  94     }
  95 
  96     public static boolean isOSX() {
  97         return isOs("mac");
  98     }
  99 
 100     public static boolean isSolaris() {
 101         return isOs("sunos");
 102     }
 103 
 104     public static boolean isWindows() {
 105         return isOs("win");
 106     }
 107 
 108     private static boolean isOs(String osname) {
 109         return osName.toLowerCase().startsWith(osname.toLowerCase());
 110     }
 111 
 112     public static String getOsName() {
 113         return osName;
 114     }
 115 
 116     public static boolean isDebugBuild() {
 117         return (jdkDebug.toLowerCase().contains("debug"));
 118     }
 119 
 120     public static String getVMVersion() {
 121         return vmVersion;
 122     }
 123 
 124     // Returns true for sparc and sparcv9.
 125     public static boolean isSparc() {
 126         return isArch("sparc.*");
 127     }
 128 
 129     public static boolean isARM() {
 130         return isArch("arm.*");
 131     }
 132 
 133     public static boolean isPPC() {
 134         return isArch("ppc.*");
 135     }
 136 
 137     public static boolean isX86() {
 138         // On Linux it's 'i386', Windows 'x86' without '_64' suffix.
 139         return isArch("(i386)|(x86(?!_64))");
 140     }
 141 
 142     public static boolean isX64() {
 143         // On OSX it's 'x86_64' and on other (Linux, Windows and Solaris) platforms it's 'amd64'
 144         return isArch("(amd64)|(x86_64)");
 145     }
 146 
 147     public static boolean isAArch64() {
 148         return isArch("aarch64");
 149     }
 150 
 151     private static boolean isArch(String archnameRE) {
 152         return Pattern.compile(archnameRE, Pattern.CASE_INSENSITIVE)
 153                 .matcher(osArch)
 154                 .matches();
 155     }
 156 
 157     public static String getOsArch() {
 158         return osArch;
 159     }
 160 
 161     /**
 162      * Return a boolean for whether we expect to be able to attach
 163      * the SA to our own processes on this system.
 164      */
 165     public static boolean shouldSAAttach() throws Exception {
 166 
 167         if (isAix()) {
 168             return false;   // SA not implemented.
 169         } else if (isLinux()) {
 170             return canPtraceAttachLinux();
 171         } else if (isOSX()) {
 172             return canAttachOSX();
 173         } else {
 174             // Other platforms expected to work:
 175             return true;
 176         }
 177     }
 178 
 179     /**
 180      * On Linux, first check the SELinux boolean "deny_ptrace" and return false
 181      * as we expect to be denied if that is "1".  Then expect permission to attach
 182      * if we are root, so return true.  Then return false for an expected denial
 183      * if "ptrace_scope" is 1, and true otherwise.
 184      */
 185     public static boolean canPtraceAttachLinux() throws Exception {
 186 
 187         // SELinux deny_ptrace:
 188         String deny_ptrace = Utils.fileAsString("/sys/fs/selinux/booleans/deny_ptrace");
 189         if (deny_ptrace != null && deny_ptrace.contains("1")) {
 190             // ptrace will be denied:
 191             return false;
 192         }
 193 
 194         if (userName.equals("root")) {
 195             return true;
 196         }
 197 
 198         // ptrace_scope:
 199         String ptrace_scope = Utils.fileAsString("/proc/sys/kernel/yama/ptrace_scope");
 200         if (ptrace_scope != null && ptrace_scope.contains("1")) {
 201             // ptrace will be denied:
 202             return false;
 203         }
 204 
 205         // Otherwise expect to be permitted:
 206         return true;
 207     }
 208 
 209     /**
 210      * On OSX, expect permission to attach only if we are root.
 211      */
 212     public static boolean canAttachOSX() throws Exception {
 213         return userName.equals("root");
 214     }
 215 
 216     /**
 217      * return path to library inside jdk tree
 218      */
 219     public static String jdkLibPath() {
 220         if (isWindows()) {
 221             return "bin";
 222         }
 223         if (isOSX()) {
 224             return "lib";
 225         }
 226 
 227         return "lib/" + getOsArch();
 228     }
 229 
 230     /**
 231      * Build name of shared object according to platform rules
 232      */
 233     public static String sharedObjectName(String name) {
 234         if (isWindows()) {
 235             return name + ".dll";
 236         }
 237         if (isOSX()) {
 238             return "lib" + name + ".dylib";
 239         }
 240         return "lib" + name + ".so";
 241     }
 242 }