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 35 public class Platform { 36 public static final String vmName = System.getProperty("java.vm.name"); 37 public static final String vmInfo = System.getProperty("java.vm.info"); 38 private static final String osVersion = System.getProperty("os.version"); 39 private static String[] osVersionTokens; 40 private static int osVersionMajor = -1; 41 private static int osVersionMinor = -1; 42 private static final String osName = System.getProperty("os.name"); 43 private static final String dataModel = System.getProperty("sun.arch.data.model"); 44 private static final String vmVersion = System.getProperty("java.vm.version"); 45 private static final String jdkDebug = System.getProperty("jdk.debug"); 46 private static final String osArch = System.getProperty("os.arch"); 47 private static final String userName = System.getProperty("user.name"); 48 private static final String compiler = System.getProperty("sun.management.compiler"); 49 50 public static boolean isClient() { 51 return vmName.endsWith(" Client VM"); 52 } 53 54 public static boolean isServer() { 55 return vmName.endsWith(" Server VM"); 56 } 57 58 public static boolean isGraal() { 59 return vmName.endsWith(" Graal 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 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 /** 164 * Compares the platform version with the supplied version. The 165 * version must be of the form a[.b[.c[.d...]]] where a, b, c, d, ... 166 * are decimal integers. 167 * 168 * @throws NullPointerException if the parameter is null 169 * @throws NumberFormatException if there is an error parsing either 170 * version as split into component strings 171 * @return -1, 0, or 1 according to whether the platform version is 172 * less than, equal to, or greater than the supplied version 173 */ 174 public static int compareOsVersion(String version) { 175 if (osVersionTokens == null) init_version(); 176 177 Objects.requireNonNull(version); 178 179 List<Integer> s1 = Arrays 180 .stream(osVersionTokens) 181 .map(Integer::valueOf) 182 .collect(Collectors.toList()); 183 List<Integer> s2 = Arrays 184 .stream(version.split("\\.")) 185 .map(Integer::valueOf) 186 .collect(Collectors.toList()); 187 188 int count = Math.max(s1.size(), s2.size()); 189 for (int i = 0; i < count; i++) { 190 int i1 = i < s1.size() ? s1.get(i) : 0; 191 int i2 = i < s2.size() ? s2.get(i) : 0; 192 if (i1 > i2) { 193 return 1; 194 } else if (i2 > i1) { 195 return -1; 196 } 197 } 198 199 return 0; 200 } 201 202 public static boolean isDebugBuild() { 203 return (jdkDebug.toLowerCase().contains("debug")); 204 } 205 206 public static boolean isSlowDebugBuild() { 207 return (jdkDebug.toLowerCase().equals("slowdebug")); 208 } 209 210 public static boolean isFastDebugBuild() { 211 return (jdkDebug.toLowerCase().equals("fastdebug")); 212 } 213 214 public static String getVMVersion() { 215 return vmVersion; 216 } 217 218 public static boolean isAArch64() { 219 return isArch("aarch64"); 220 } 221 222 public static boolean isARM() { 223 return isArch("arm.*"); 224 } 225 226 public static boolean isPPC() { 227 return isArch("ppc.*"); 228 } 229 230 // Returns true for IBM z System running linux. 231 public static boolean isS390x() { 232 return isArch("s390.*") || isArch("s/390.*") || isArch("zArch_64"); 233 } 234 235 // Returns true for sparc and sparcv9. 236 public static boolean isSparc() { 237 return isArch("sparc.*"); 238 } 239 240 public static boolean isX64() { 241 // On OSX it's 'x86_64' and on other (Linux, Windows and Solaris) platforms it's 'amd64' 242 return isArch("(amd64)|(x86_64)"); 243 } 244 245 public static boolean isX86() { 246 // On Linux it's 'i386', Windows 'x86' without '_64' suffix. 247 return isArch("(i386)|(x86(?!_64))"); 248 } 249 250 public static String getOsArch() { 251 return osArch; 252 } 253 254 /** 255 * Return a boolean for whether SA and jhsdb are ported/available 256 * on this platform. 257 */ 258 public static boolean hasSA() { 259 if (isAix()) { 260 return false; // SA not implemented. 261 } else if (isLinux()) { 262 if (isS390x()) { 263 return false; // SA not implemented. 264 } 265 } 266 // Other platforms expected to work: 267 return true; 268 } 269 270 /** 271 * Return a boolean for whether we expect to be able to attach 272 * the SA to our own processes on this system. This requires 273 * that SA is ported/available on this platform. 274 */ 275 public static boolean shouldSAAttach() throws IOException { 276 if (!hasSA()) return false; 277 if (isLinux()) { 278 return canPtraceAttachLinux(); 279 } else if (isOSX()) { 280 return canAttachOSX(); 281 } else { 282 // Other platforms expected to work: 283 return true; 284 } 285 } 286 287 /** 288 * On Linux, first check the SELinux boolean "deny_ptrace" and return false 289 * as we expect to be denied if that is "1". Then expect permission to attach 290 * if we are root, so return true. Then return false for an expected denial 291 * if "ptrace_scope" is 1, and true otherwise. 292 */ 293 private static boolean canPtraceAttachLinux() throws IOException { 294 // SELinux deny_ptrace: 295 File deny_ptrace = new File("/sys/fs/selinux/booleans/deny_ptrace"); 296 if (deny_ptrace.exists()) { 297 try (RandomAccessFile file = new RandomAccessFile(deny_ptrace, "r")) { 298 if (file.readByte() != '0') { 299 return false; 300 } 301 } 302 } 303 304 // YAMA enhanced security ptrace_scope: 305 // 0 - a process can PTRACE_ATTACH to any other process running under the same uid 306 // 1 - restricted ptrace: a process must be a children of the inferior or user is root 307 // 2 - only processes with CAP_SYS_PTRACE may use ptrace or user is root 308 // 3 - no attach: no processes may use ptrace with PTRACE_ATTACH 309 File ptrace_scope = new File("/proc/sys/kernel/yama/ptrace_scope"); 310 if (ptrace_scope.exists()) { 311 try (RandomAccessFile file = new RandomAccessFile(ptrace_scope, "r")) { 312 byte yama_scope = file.readByte(); 313 if (yama_scope == '3') { 314 return false; 315 } 316 317 if (!userName.equals("root") && yama_scope != '0') { 318 return false; 319 } 320 } 321 } 322 // Otherwise expect to be permitted: 323 return true; 324 } 325 326 /** 327 * On OSX, expect permission to attach only if we are root. 328 */ 329 private static boolean canAttachOSX() { 330 return userName.equals("root"); 331 } 332 333 private static boolean isArch(String archnameRE) { 334 return Pattern.compile(archnameRE, Pattern.CASE_INSENSITIVE) 335 .matcher(osArch) 336 .matches(); 337 } 338 339 /** 340 * Returns file extension of shared library, e.g. "so" on linux, "dll" on windows. 341 * @return file extension 342 */ 343 public static String sharedLibraryExt() { 344 if (isWindows()) { 345 return "dll"; 346 } else if (isOSX()) { 347 return "dylib"; 348 } else { 349 return "so"; 350 } 351 } 352 353 /* 354 * This should match the #if condition in ClassListParser::load_class_from_source(). 355 */ 356 public static boolean areCustomLoadersSupportedForCDS() { 357 boolean isLinux = Platform.isLinux(); 358 boolean is64 = Platform.is64bit(); 359 boolean isSolaris = Platform.isSolaris(); 360 361 return (is64 && (isLinux || isSolaris)); 362 } 363 }