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