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