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