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