1 /*
   2  * Copyright (c) 2013, 2015, 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.testlibrary;
  25 import java.util.regex.Pattern;
  26 import java.io.RandomAccessFile;
  27 import java.io.FileNotFoundException;
  28 import java.io.IOException;
  29 
  30 public class Platform {
  31     private static final String osName      = System.getProperty("os.name");
  32     private static final String dataModel   = System.getProperty("sun.arch.data.model");
  33     private static final String vmVersion   = System.getProperty("java.vm.version");
  34     private static final String javaVersion = System.getProperty("java.version");
  35     private static final String osArch      = System.getProperty("os.arch");
  36     private static final String vmName      = System.getProperty("java.vm.name");
  37     private static final String userName    = System.getProperty("user.name");
  38     private static final String compiler    = System.getProperty("sun.management.compiler");
  39 
  40     public static boolean isClient() {
  41         return vmName.endsWith(" Client VM");
  42     }
  43 
  44     public static boolean isServer() {
  45         return vmName.endsWith(" Server VM");
  46     }
  47 
  48     public static boolean isGraal() {
  49         return vmName.endsWith(" Graal VM");
  50     }
  51 
  52     public static boolean isMinimal() {
  53         return vmName.endsWith(" Minimal VM");
  54     }
  55 
  56     public static boolean isEmbedded() {
  57         return vmName.contains("Embedded");
  58     }
  59 
  60     public static boolean isTieredSupported() {
  61         return compiler != null && compiler.contains("Tiered Compilers");
  62     }
  63 
  64 
  65     public static boolean is32bit() {
  66         return dataModel.equals("32");
  67     }
  68 
  69     public static boolean is64bit() {
  70         return dataModel.equals("64");
  71     }
  72 
  73     public static boolean isAix() {
  74         return isOs("aix");
  75     }
  76 
  77     public static boolean isLinux() {
  78         return isOs("linux");
  79     }
  80 
  81     public static boolean isOSX() {
  82         return isOs("mac");
  83     }
  84 
  85     public static boolean isSolaris() {
  86         return isOs("sunos");
  87     }
  88 
  89     public static boolean isWindows() {
  90         return isOs("win");
  91     }
  92 
  93     private static boolean isOs(String osname) {
  94         return osName.toLowerCase().startsWith(osname.toLowerCase());
  95     }
  96 
  97     public static String getOsName() {
  98         return osName;
  99     }
 100 
 101     public static boolean isDebugBuild() {
 102         return (vmVersion.toLowerCase().contains("debug") ||
 103                 javaVersion.toLowerCase().contains("debug"));
 104     }
 105 
 106     public static String getVMVersion() {
 107         return vmVersion;
 108     }
 109 
 110     // Returns true for sparc and sparcv9.
 111     public static boolean isSparc() {
 112         return isArch("sparc.*");
 113     }
 114 
 115     public static boolean isARM() {
 116         return isArch("arm.*");
 117     }
 118 
 119     public static boolean isPPC() {
 120         return isArch("ppc.*");
 121     }
 122 
 123     public static boolean isX86() {
 124         // On Linux it's 'i386', Windows 'x86' without '_64' suffix.
 125         return isArch("(i386)|(x86(?!_64))");
 126     }
 127 
 128     public static boolean isX64() {
 129         // On OSX it's 'x86_64' and on other (Linux, Windows and Solaris) platforms it's 'amd64'
 130         return isArch("(amd64)|(x86_64)");
 131     }
 132 
 133     private static boolean isArch(String archnameRE) {
 134         return Pattern.compile(archnameRE, Pattern.CASE_INSENSITIVE)
 135                 .matcher(osArch)
 136                 .matches();
 137     }
 138 
 139     public static String getOsArch() {
 140         return osArch;
 141     }
 142 
 143     /**
 144      * Return a boolean for whether we expect to be able to attach
 145      * the SA to our own processes on this system.
 146      */
 147     public static boolean shouldSAAttach()
 148                                throws IOException {
 149 
 150         if (isAix()) {
 151             return false;   // SA not implemented.
 152         } else if (isLinux()) {
 153             return canPtraceAttachLinux();
 154         } else if (isOSX()) {
 155             return canAttachOSX();
 156         } else {
 157             // Other platforms expected to work:
 158             return true;
 159         }
 160     }
 161 
 162     /**
 163      * On Linux, first check the SELinux boolean "deny_ptrace" and return false
 164      * as we expect to be denied if that is "1".
 165      */
 166     public static boolean canPtraceAttachLinux()
 167                                throws IOException {
 168 
 169         // SELinux deny_ptrace:
 170         try(RandomAccessFile file = new RandomAccessFile("/sys/fs/selinux/booleans/deny_ptrace", "r")) {
 171             if (file.readByte() != '0') {
 172                 return false;
 173             }
 174         }
 175         catch(FileNotFoundException ex) {
 176             // Ignored
 177         }
 178 
 179         // YAMA enhanced security ptrace_scope:
 180         // 0 - a process can PTRACE_ATTACH to any other process running under the same uid
 181         // 1 - restricted ptrace: a process must be a children of the inferior or user is root
 182         // 2 - only processes with CAP_SYS_PTRACE may use ptrace or user is root
 183         // 3 - no attach: no processes may use ptrace with PTRACE_ATTACH
 184 
 185         try(RandomAccessFile file = new RandomAccessFile("/proc/sys/kernel/yama/ptrace_scope", "r")) {
 186             byte yama_scope = file.readByte();
 187             if (yama_scope == '3') {
 188                 return false;
 189             }
 190 
 191             if (!userName.equals("root") && yama_scope != '0') {
 192                 return false;
 193             }
 194         }
 195         catch(FileNotFoundException ex) {
 196             // Ignored
 197         }
 198 
 199         // Otherwise expect to be permitted:
 200         return true;
 201     }
 202 
 203     /**
 204      * On OSX, expect permission to attach only if we are root.
 205      */
 206     public static boolean canAttachOSX() {
 207         return userName.equals("root");
 208     }
 209 }