1 /* 2 * Copyright (c) 2013, 2020, 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.nio.file.Path; 30 import java.nio.file.Paths; 31 import java.util.regex.Pattern; 32 import java.security.AccessController; 33 import java.security.PrivilegedAction; 34 import java.security.PrivilegedActionException; 35 import java.security.PrivilegedExceptionAction; 36 37 public class Platform { 38 public static final String vmName = privilegedGetProperty("java.vm.name"); 39 public static final String vmInfo = privilegedGetProperty("java.vm.info"); 40 private static final String osVersion = privilegedGetProperty("os.version"); 41 private static int osVersionMajor = -1; 42 private static int osVersionMinor = -1; 43 private static final String osName = privilegedGetProperty("os.name"); 44 private static final String dataModel = privilegedGetProperty("sun.arch.data.model"); 45 private static final String vmVersion = privilegedGetProperty("java.vm.version"); 46 private static final String jdkDebug = privilegedGetProperty("jdk.debug"); 47 private static final String osArch = privilegedGetProperty("os.arch"); 48 private static final String userName = privilegedGetProperty("user.name"); 49 private static final String compiler = privilegedGetProperty("sun.management.compiler"); 50 private static final String testJdk = privilegedGetProperty("test.jdk"); 51 52 private static String privilegedGetProperty(String key) { 53 return AccessController.doPrivileged(( 54 PrivilegedAction<String>) () -> System.getProperty(key)); 55 } 56 57 public static boolean isClient() { 58 return vmName.endsWith(" Client VM"); 59 } 60 61 public static boolean isServer() { 62 return vmName.endsWith(" Server 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() || isARM()) { 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 IOException t = (IOException) e.getException(); 268 throw t; 269 } 270 } 271 272 // YAMA enhanced security ptrace_scope: 273 // 0 - a process can PTRACE_ATTACH to any other process running under the same uid 274 // 1 - restricted ptrace: a process must be a children of the inferior or user is root 275 // 2 - only processes with CAP_SYS_PTRACE may use ptrace or user is root 276 // 3 - no attach: no processes may use ptrace with PTRACE_ATTACH 277 File ptrace_scope = new File("/proc/sys/kernel/yama/ptrace_scope"); 278 if (ptrace_scope.exists()) { 279 try (RandomAccessFile file = AccessController.doPrivileged( 280 (PrivilegedExceptionAction<RandomAccessFile>) () -> new RandomAccessFile(ptrace_scope, "r"))) { 281 byte yama_scope = file.readByte(); 282 if (yama_scope == '3') { 283 return false; 284 } 285 286 if (!userName.equals("root") && yama_scope != '0') { 287 return false; 288 } 289 } catch (PrivilegedActionException e) { 290 IOException t = (IOException) e.getException(); 291 throw t; 292 } 293 } 294 // Otherwise expect to be permitted: 295 return true; 296 } 297 298 /** 299 * On OSX, expect permission to attach only if we are root. 300 */ 301 private static boolean canAttachOSX() { 302 return userName.equals("root"); 303 } 304 305 private static boolean isArch(String archnameRE) { 306 return Pattern.compile(archnameRE, Pattern.CASE_INSENSITIVE) 307 .matcher(osArch) 308 .matches(); 309 } 310 311 /** 312 * Returns file extension of shared library, e.g. "so" on linux, "dll" on windows. 313 * @return file extension 314 */ 315 public static String sharedLibraryExt() { 316 if (isWindows()) { 317 return "dll"; 318 } else if (isOSX()) { 319 return "dylib"; 320 } else { 321 return "so"; 322 } 323 } 324 325 /* 326 * Returns name of system variable containing paths to shared native libraries. 327 */ 328 public static String sharedLibraryPathVariableName() { 329 if (isWindows()) { 330 return "PATH"; 331 } else if (isOSX()) { 332 return "DYLD_LIBRARY_PATH"; 333 } else if (isAix()) { 334 return "LIBPATH"; 335 } else { 336 return "LD_LIBRARY_PATH"; 337 } 338 } 339 340 /** 341 * Returns absolute path to directory containing JVM shared library. 342 */ 343 public static Path jvmLibDir() { 344 Path dir = Paths.get(testJdk); 345 if (Platform.isWindows()) { 346 return dir.resolve("bin") 347 .resolve(variant()) 348 .toAbsolutePath(); 349 } else { 350 return dir.resolve("lib") 351 .resolve(variant()) 352 .toAbsolutePath(); 353 } 354 } 355 356 private static String variant() { 357 if (Platform.isServer()) { 358 return "server"; 359 } else if (Platform.isClient()) { 360 return "client"; 361 } else if (Platform.isMinimal()) { 362 return "minimal"; 363 } else { 364 throw new Error("TESTBUG: unsupported vm variant"); 365 } 366 } 367 368 369 public static boolean isDefaultCDSArchiveSupported() { 370 return (is64bit() && 371 isServer() && 372 (isLinux() || 373 isOSX() || 374 isSolaris() || 375 isWindows()) && 376 !isZero() && 377 !isMinimal() && 378 !isAArch64() && 379 !isARM()); 380 } 381 382 /* 383 * This should match the #if condition in ClassListParser::load_class_from_source(). 384 */ 385 public static boolean areCustomLoadersSupportedForCDS() { 386 return (is64bit() && (isLinux() || isSolaris() || isOSX())); 387 } 388 }