Platform.java revision 2475:d660c00d91bf
1/*
2 * Copyright (c) 2013, 2016, 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
24package jdk.test.lib;
25
26import java.util.regex.Pattern;
27
28public class Platform {
29    public  static final String vmName      = System.getProperty("java.vm.name");
30    public  static final String vmInfo      = System.getProperty("java.vm.info");
31    private static final String osVersion   = System.getProperty("os.version");
32    private static       int osVersionMajor = -1;
33    private static       int osVersionMinor = -1;
34    private static final String osName      = System.getProperty("os.name");
35    private static final String dataModel   = System.getProperty("sun.arch.data.model");
36    private static final String vmVersion   = System.getProperty("java.vm.version");
37    private static final String jdkDebug    = System.getProperty("jdk.debug");
38    private static final String osArch      = System.getProperty("os.arch");
39    private static final String userName    = System.getProperty("user.name");
40    private static final String compiler    = System.getProperty("sun.management.compiler");
41
42    public static boolean isClient() {
43        return vmName.endsWith(" Client VM");
44    }
45
46    public static boolean isServer() {
47        return vmName.endsWith(" Server VM");
48    }
49
50    public static boolean isGraal() {
51        return vmName.endsWith(" Graal VM");
52    }
53
54    public static boolean isZero() {
55        return vmName.endsWith(" Zero VM");
56    }
57
58    public static boolean isMinimal() {
59        return vmName.endsWith(" Minimal VM");
60    }
61
62    public static boolean isEmbedded() {
63        return vmName.contains("Embedded");
64    }
65
66    public static boolean isEmulatedClient() {
67        return vmInfo.contains(" emulated-client");
68    }
69
70    public static boolean isTieredSupported() {
71        return compiler.contains("Tiered Compilers");
72    }
73
74    public static boolean isInt() {
75        return vmInfo.contains("interpreted");
76    }
77
78    public static boolean isMixed() {
79        return vmInfo.contains("mixed");
80    }
81
82    public static boolean isComp() {
83        return vmInfo.contains("compiled");
84    }
85
86    public static boolean is32bit() {
87        return dataModel.equals("32");
88    }
89
90    public static boolean is64bit() {
91        return dataModel.equals("64");
92    }
93
94    public static boolean isAix() {
95        return isOs("aix");
96    }
97
98    public static boolean isLinux() {
99        return isOs("linux");
100    }
101
102    public static boolean isOSX() {
103        return isOs("mac");
104    }
105
106    public static boolean isSolaris() {
107        return isOs("sunos");
108    }
109
110    public static boolean isWindows() {
111        return isOs("win");
112    }
113
114    private static boolean isOs(String osname) {
115        return osName.toLowerCase().startsWith(osname.toLowerCase());
116    }
117
118    public static String getOsName() {
119        return osName;
120    }
121
122    // Os version support.
123    private static void init_version() {
124        try {
125            final String[] tokens = osVersion.split("\\.");
126            if (tokens.length > 0) {
127                osVersionMajor = Integer.parseInt(tokens[0]);
128                if (tokens.length > 1) {
129                    osVersionMinor = Integer.parseInt(tokens[1]);
130                }
131            }
132        } catch (NumberFormatException e) {
133            osVersionMajor = osVersionMinor = 0;
134        }
135    }
136
137    // Returns major version number from os.version system property.
138    // E.g. 5 on Solaris 10 and 3 on SLES 11.3 (for the linux kernel version).
139    public static int getOsVersionMajor() {
140        if (osVersionMajor == -1) init_version();
141        return osVersionMajor;
142    }
143
144    // Returns minor version number from os.version system property.
145    // E.g. 10 on Solaris 10 and 0 on SLES 11.3 (for the linux kernel version).
146    public static int getOsVersionMinor() {
147        if (osVersionMinor == -1) init_version();
148        return osVersionMinor;
149    }
150
151    public static boolean isDebugBuild() {
152        return (jdkDebug.toLowerCase().contains("debug"));
153    }
154
155    public static boolean isSlowDebugBuild() {
156        return (jdkDebug.toLowerCase().equals("slowdebug"));
157    }
158
159    public static boolean isFastDebugBuild() {
160        return (jdkDebug.toLowerCase().equals("fastdebug"));
161    }
162
163    public static String getVMVersion() {
164        return vmVersion;
165    }
166
167    public static boolean isAArch64() {
168        return isArch("aarch64");
169    }
170
171    public static boolean isARM() {
172        return isArch("arm.*");
173    }
174
175    public static boolean isPPC() {
176        return isArch("ppc.*");
177    }
178
179    // Returns true for IBM z System running linux.
180    public static boolean isS390x() {
181        return isArch("s390.*") || isArch("s/390.*") || isArch("zArch_64");
182    }
183
184    // Returns true for sparc and sparcv9.
185    public static boolean isSparc() {
186        return isArch("sparc.*");
187    }
188
189    public static boolean isX64() {
190        // On OSX it's 'x86_64' and on other (Linux, Windows and Solaris) platforms it's 'amd64'
191        return isArch("(amd64)|(x86_64)");
192    }
193
194    public static boolean isX86() {
195        // On Linux it's 'i386', Windows 'x86' without '_64' suffix.
196        return isArch("(i386)|(x86(?!_64))");
197    }
198
199    public static String getOsArch() {
200        return osArch;
201    }
202
203    /**
204     * Return a boolean for whether we expect to be able to attach
205     * the SA to our own processes on this system.
206     */
207    public static boolean shouldSAAttach() throws Exception {
208
209        if (isAix()) {
210            return false;   // SA not implemented.
211        } else if (isLinux()) {
212            if (isS390x()) { return false; }   // SA not implemented.
213            return canPtraceAttachLinux();
214        } else if (isOSX()) {
215            return canAttachOSX();
216        } else {
217            // Other platforms expected to work:
218            return true;
219        }
220    }
221
222    /**
223     * On Linux, first check the SELinux boolean "deny_ptrace" and return false
224     * as we expect to be denied if that is "1".  Then expect permission to attach
225     * if we are root, so return true.  Then return false for an expected denial
226     * if "ptrace_scope" is 1, and true otherwise.
227     */
228    public static boolean canPtraceAttachLinux() throws Exception {
229
230        // SELinux deny_ptrace:
231        String deny_ptrace = Utils.fileAsString("/sys/fs/selinux/booleans/deny_ptrace");
232        if (deny_ptrace != null && deny_ptrace.contains("1")) {
233            // ptrace will be denied:
234            return false;
235        }
236
237        // YAMA enhanced security ptrace_scope:
238        // 0 - a process can PTRACE_ATTACH to any other process running under the same uid
239        // 1 - restricted ptrace: a process must be a children of the inferior or user is root
240        // 2 - only processes with CAP_SYS_PTRACE may use ptrace or user is root
241        // 3 - no attach: no processes may use ptrace with PTRACE_ATTACH
242        String ptrace_scope = Utils.fileAsString("/proc/sys/kernel/yama/ptrace_scope");
243        if (ptrace_scope != null) {
244            if (ptrace_scope.startsWith("3")) {
245                return false;
246            }
247            if (!userName.equals("root") && !ptrace_scope.startsWith("0")) {
248                // ptrace will be denied:
249                return false;
250            }
251        }
252        // Otherwise expect to be permitted:
253        return true;
254    }
255
256    /**
257     * On OSX, expect permission to attach only if we are root.
258     */
259    public static boolean canAttachOSX() throws Exception {
260        return userName.equals("root");
261    }
262
263    private static boolean isArch(String archnameRE) {
264        return Pattern.compile(archnameRE, Pattern.CASE_INSENSITIVE)
265                      .matcher(osArch)
266                      .matches();
267    }
268}
269