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 com.oracle.java.testlibrary; 25 26 import java.util.regex.Pattern; 27 28 import com.oracle.java.testlibrary.Utils; 29 30 public class Platform { 31 private static final String osName = System.getProperty("os.name"); 32 private static final String dataModel = System.getProperty("sun.arch.data.model"); 33 private static final String vmVersion = System.getProperty("java.vm.version"); 34 private static final String osArch = System.getProperty("os.arch"); 35 public static final String vmName = System.getProperty("java.vm.name"); 36 private static final String userName = System.getProperty("user.name"); 37 38 public static boolean isClient() { 39 return vmName.endsWith(" Client VM"); 40 } 41 42 public static boolean isServer() { 43 return vmName.endsWith(" Server VM"); 44 } 45 46 public static boolean isGraal() { 47 return vmName.endsWith(" Graal VM"); 48 } 49 50 public static boolean isMinimal() { 51 return vmName.endsWith(" Minimal VM"); 52 } 53 54 public static boolean isEmbedded() { 55 return vmName.contains("Embedded"); 56 } 57 58 public static boolean is32bit() { 59 return dataModel.equals("32"); 60 } 61 62 public static boolean is64bit() { 63 return dataModel.equals("64"); 64 } 65 66 public static boolean isAix() { 67 return isOs("aix"); 68 } 69 70 public static boolean isLinux() { 71 return isOs("linux"); 72 } 73 74 public static boolean isOSX() { 75 return isOs("mac"); 76 } 77 78 public static boolean isSolaris() { 79 return isOs("sunos"); 80 } 81 82 public static boolean isWindows() { 83 return isOs("win"); 84 } 85 86 private static boolean isOs(String osname) { 87 return osName.toLowerCase().startsWith(osname.toLowerCase()); 88 } 89 90 public static String getOsName() { 91 return osName; 92 } 93 94 public static boolean isDebugBuild() { 95 return vmVersion.toLowerCase().contains("debug"); 96 } 97 98 public static String getVMVersion() { 99 return vmVersion; 100 } 101 102 // Returns true for sparc and sparcv9. 103 public static boolean isSparc() { 104 return isArch("sparc.*"); 105 } 106 107 public static boolean isARM() { 108 return isArch("arm.*"); 109 } 110 111 public static boolean isPPC() { 112 return isArch("ppc.*"); 113 } 114 115 public static boolean isX86() { 116 // On Linux it's 'i386', Windows 'x86' without '_64' suffix. 117 return isArch("(i386)|(x86(?!_64))"); 118 } 119 120 public static boolean isX64() { 121 // On OSX it's 'x86_64' and on other (Linux, Windows and Solaris) platforms it's 'amd64' 122 return isArch("(amd64)|(x86_64)"); 123 } 124 125 public static boolean isAArch64() { 126 return isArch("aarch64"); 127 } 128 129 private static boolean isArch(String archnameRE) { 130 return Pattern.compile(archnameRE, Pattern.CASE_INSENSITIVE) 131 .matcher(osArch) 132 .matches(); 133 } 134 135 public static String getOsArch() { 136 return osArch; 137 } 138 139 /** 140 * Return a boolean for whether we expect to be able to attach 141 * the SA to our own processes on this system. 142 */ 143 public static boolean shouldSAAttach() throws Exception { 144 145 if (isAix()) { 146 return false; // SA not implemented. 147 } else if (isLinux()) { 148 return canPtraceAttachLinux(); 149 } else if (isOSX()) { 150 return canAttachOSX(); 151 } else { 152 // Other platforms expected to work: 153 return true; 154 } 155 } 156 157 /** 158 * On Linux, first check the SELinux boolean "deny_ptrace" and return false 159 * as we expect to be denied if that is "1". Then expect permission to attach 160 * if we are root, so return true. Then return false for an expected denial 161 * if "ptrace_scope" is 1, and true otherwise. 162 */ 163 public static boolean canPtraceAttachLinux() throws Exception { 164 165 // SELinux deny_ptrace: 166 String deny_ptrace = Utils.fileAsString("/sys/fs/selinux/booleans/deny_ptrace"); 167 if (deny_ptrace != null && deny_ptrace.contains("1")) { 168 // ptrace will be denied: 169 return false; 170 } 171 172 if (userName.equals("root")) { 173 return true; 174 } 175 176 // ptrace_scope: 177 String ptrace_scope = Utils.fileAsString("/proc/sys/kernel/yama/ptrace_scope"); 178 if (ptrace_scope != null && ptrace_scope.contains("1")) { 179 // ptrace will be denied: 180 return false; 181 } 182 183 // Otherwise expect to be permitted: 184 return true; 185 } 186 187 /** 188 * On OSX, expect permission to attach only if we are root. 189 */ 190 public static boolean canAttachOSX() throws Exception { 191 return userName.equals("root"); 192 } 193 }