Platform.java revision 2269:88c6c6fbc171
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 boolean isSlowDebugBuild() {
152        return (jdkDebug.toLowerCase().equals("slowdebug"));
153    }
154
155    public static boolean isFastDebugBuild() {
156        return (jdkDebug.toLowerCase().equals("fastdebug"));
157    }
158
159    public static String getVMVersion() {
160        return vmVersion;
161    }
162
163    // Returns true for sparc and sparcv9.
164    public static boolean isSparc() {
165        return isArch("sparc.*");
166    }
167
168    public static boolean isARM() {
169        return isArch("arm.*");
170    }
171
172    public static boolean isPPC() {
173        return isArch("ppc.*");
174    }
175
176    public static boolean isX86() {
177        // On Linux it's 'i386', Windows 'x86' without '_64' suffix.
178        return isArch("(i386)|(x86(?!_64))");
179    }
180
181    public static boolean isX64() {
182        // On OSX it's 'x86_64' and on other (Linux, Windows and Solaris) platforms it's 'amd64'
183        return isArch("(amd64)|(x86_64)");
184    }
185
186    public static boolean isAArch64() {
187        return isArch("aarch64");
188    }
189
190    public static String getOsArch() {
191        return osArch;
192    }
193
194    /**
195     * Return a boolean for whether we expect to be able to attach
196     * the SA to our own processes on this system.
197     */
198    public static boolean shouldSAAttach() throws Exception {
199
200        if (isAix()) {
201            return false;   // SA not implemented.
202        } else if (isLinux()) {
203            return canPtraceAttachLinux();
204        } else if (isOSX()) {
205            return canAttachOSX();
206        } else {
207            // Other platforms expected to work:
208            return true;
209        }
210    }
211
212    /**
213     * On Linux, first check the SELinux boolean "deny_ptrace" and return false
214     * as we expect to be denied if that is "1".  Then expect permission to attach
215     * if we are root, so return true.  Then return false for an expected denial
216     * if "ptrace_scope" is 1, and true otherwise.
217     */
218    public static boolean canPtraceAttachLinux() throws Exception {
219
220        // SELinux deny_ptrace:
221        String deny_ptrace = Utils.fileAsString("/sys/fs/selinux/booleans/deny_ptrace");
222        if (deny_ptrace != null && deny_ptrace.contains("1")) {
223            // ptrace will be denied:
224            return false;
225        }
226
227        // YAMA enhanced security ptrace_scope:
228        // 0 - a process can PTRACE_ATTACH to any other process running under the same uid
229        // 1 - restricted ptrace: a process must be a children of the inferior or user is root
230        // 2 - only processes with CAP_SYS_PTRACE may use ptrace or user is root
231        // 3 - no attach: no processes may use ptrace with PTRACE_ATTACH
232        String ptrace_scope = Utils.fileAsString("/proc/sys/kernel/yama/ptrace_scope");
233        if (ptrace_scope != null) {
234            if (ptrace_scope.startsWith("3")) {
235                return false;
236            }
237            if (!userName.equals("root") && !ptrace_scope.startsWith("0")) {
238                // ptrace will be denied:
239                return false;
240            }
241        }
242        // Otherwise expect to be permitted:
243        return true;
244    }
245
246    /**
247     * On OSX, expect permission to attach only if we are root.
248     */
249    public static boolean canAttachOSX() throws Exception {
250        return userName.equals("root");
251    }
252
253    private static boolean isArch(String archnameRE) {
254        return Pattern.compile(archnameRE, Pattern.CASE_INSENSITIVE)
255                      .matcher(osArch)
256                      .matches();
257    }
258}
259