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.testlibrary; 25 import java.util.regex.Pattern; 26 import java.io.RandomAccessFile; 27 import java.io.FileNotFoundException; 28 import java.io.IOException; 29 30 /** 31 * @deprecated This class is deprecated. Use the one from 32 * {@code <root>/test/lib/share/classes/jdk/test/lib} 33 */ 34 @Deprecated 35 public class Platform { 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 vmName = System.getProperty("java.vm.name"); 42 private static final String userName = System.getProperty("user.name"); 43 private static final String compiler = System.getProperty("sun.management.compiler"); 44 45 public static boolean isClient() { 46 return vmName.endsWith(" Client VM"); 47 } 48 49 public static boolean isServer() { 50 return vmName.endsWith(" Server VM"); 51 } 52 53 public static boolean isGraal() { 54 return vmName.endsWith(" Graal VM"); 55 } 56 57 public static boolean isMinimal() { 58 return vmName.endsWith(" Minimal VM"); 59 } 60 61 public static boolean isTieredSupported() { 62 return compiler.contains("Tiered Compilers"); 63 } 64 65 66 public static boolean is32bit() { 67 return dataModel.equals("32"); 68 } 69 70 public static boolean is64bit() { 71 return dataModel.equals("64"); 72 } 73 74 public static boolean isAix() { 75 return isOs("aix"); 76 } 77 78 public static boolean isLinux() { 79 return isOs("linux"); 80 } 81 82 public static boolean isOSX() { 83 return isOs("mac"); 84 } 85 86 public static boolean isSolaris() { 87 return isOs("sunos"); 88 } 89 90 public static boolean isWindows() { 91 return isOs("win"); 92 } 93 94 private static boolean isOs(String osname) { 95 return osName.toLowerCase().startsWith(osname.toLowerCase()); 96 } 97 98 public static String getOsName() { 99 return osName; 100 } 101 102 public static boolean isDebugBuild() { 103 return (jdkDebug.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 }