Platform.java revision 2268:a85b22ed4fc8
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 isTieredSupported() {
67        return compiler.contains("Tiered Compilers");
68    }
69
70    public static boolean isInt() {
71        return vmInfo.contains("interpreted");
72    }
73
74    public static boolean isMixed() {
75        return vmInfo.contains("mixed");
76    }
77
78    public static boolean isComp() {
79        return vmInfo.contains("compiled");
80    }
81
82    public static boolean is32bit() {
83        return dataModel.equals("32");
84    }
85
86    public static boolean is64bit() {
87        return dataModel.equals("64");
88    }
89
90    public static boolean isAix() {
91        return isOs("aix");
92    }
93
94    public static boolean isLinux() {
95        return isOs("linux");
96    }
97
98    public static boolean isOSX() {
99        return isOs("mac");
100    }
101
102    public static boolean isSolaris() {
103        return isOs("sunos");
104    }
105
106    public static boolean isWindows() {
107        return isOs("win");
108    }
109
110    private static boolean isOs(String osname) {
111        return osName.toLowerCase().startsWith(osname.toLowerCase());
112    }
113
114    public static String getOsName() {
115        return osName;
116    }
117
118    // Os version support.
119    private static void init_version() {
120        try {
121            final String[] tokens = osVersion.split("\\.");
122            if (tokens.length > 0) {
123                osVersionMajor = Integer.parseInt(tokens[0]);
124                if (tokens.length > 1) {
125                    osVersionMinor = Integer.parseInt(tokens[1]);
126                }
127            }
128        } catch (NumberFormatException e) {
129            osVersionMajor = osVersionMinor = 0;
130        }
131    }
132
133    // Returns major version number from os.version system property.
134    // E.g. 5 on Solaris 10 and 3 on SLES 11.3 (for the linux kernel version).
135    public static int getOsVersionMajor() {
136        if (osVersionMajor == -1) init_version();
137        return osVersionMajor;
138    }
139
140    // Returns minor version number from os.version system property.
141    // E.g. 10 on Solaris 10 and 0 on SLES 11.3 (for the linux kernel version).
142    public static int getOsVersionMinor() {
143        if (osVersionMinor == -1) init_version();
144        return osVersionMinor;
145    }
146
147    public static boolean isDebugBuild() {
148        return (jdkDebug.toLowerCase().contains("debug"));
149    }
150
151    public static String getVMVersion() {
152        return vmVersion;
153    }
154
155    // Returns true for sparc and sparcv9.
156    public static boolean isSparc() {
157        return isArch("sparc.*");
158    }
159
160    public static boolean isARM() {
161        return isArch("arm.*");
162    }
163
164    public static boolean isPPC() {
165        return isArch("ppc.*");
166    }
167
168    public static boolean isX86() {
169        // On Linux it's 'i386', Windows 'x86' without '_64' suffix.
170        return isArch("(i386)|(x86(?!_64))");
171    }
172
173    public static boolean isX64() {
174        // On OSX it's 'x86_64' and on other (Linux, Windows and Solaris) platforms it's 'amd64'
175        return isArch("(amd64)|(x86_64)");
176    }
177
178    public static boolean isAArch64() {
179        return isArch("aarch64");
180    }
181
182    public static String getOsArch() {
183        return osArch;
184    }
185
186    /**
187     * Return a boolean for whether we expect to be able to attach
188     * the SA to our own processes on this system.
189     */
190    public static boolean shouldSAAttach() throws Exception {
191
192        if (isAix()) {
193            return false;   // SA not implemented.
194        } else if (isLinux()) {
195            return canPtraceAttachLinux();
196        } else if (isOSX()) {
197            return canAttachOSX();
198        } else {
199            // Other platforms expected to work:
200            return true;
201        }
202    }
203
204    /**
205     * On Linux, first check the SELinux boolean "deny_ptrace" and return false
206     * as we expect to be denied if that is "1".  Then expect permission to attach
207     * if we are root, so return true.  Then return false for an expected denial
208     * if "ptrace_scope" is 1, and true otherwise.
209     */
210    public static boolean canPtraceAttachLinux() throws Exception {
211
212        // SELinux deny_ptrace:
213        String deny_ptrace = Utils.fileAsString("/sys/fs/selinux/booleans/deny_ptrace");
214        if (deny_ptrace != null && deny_ptrace.contains("1")) {
215            // ptrace will be denied:
216            return false;
217        }
218
219        // YAMA enhanced security ptrace_scope:
220        // 0 - a process can PTRACE_ATTACH to any other process running under the same uid
221        // 1 - restricted ptrace: a process must be a children of the inferior or user is root
222        // 2 - only processes with CAP_SYS_PTRACE may use ptrace or user is root
223        // 3 - no attach: no processes may use ptrace with PTRACE_ATTACH
224        String ptrace_scope = Utils.fileAsString("/proc/sys/kernel/yama/ptrace_scope");
225        if (ptrace_scope != null) {
226            if (ptrace_scope.startsWith("3")) {
227                return false;
228            }
229            if (!userName.equals("root") && !ptrace_scope.startsWith("0")) {
230                // ptrace will be denied:
231                return false;
232            }
233        }
234        // Otherwise expect to be permitted:
235        return true;
236    }
237
238    /**
239     * On OSX, expect permission to attach only if we are root.
240     */
241    public static boolean canAttachOSX() throws Exception {
242        return userName.equals("root");
243    }
244
245    private static boolean isArch(String archnameRE) {
246        return Pattern.compile(archnameRE, Pattern.CASE_INSENSITIVE)
247                      .matcher(osArch)
248                      .matches();
249    }
250}
251