1 /*
   2  * Copyright (c) 2009, 2013, 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 /*
  25  * @test
  26  * @bug 4780570 4731671 6354700 6367077 6670965 4882974
  27  * @summary Checks for LD_LIBRARY_PATH and execution  on *nixes
  28  * @compile -XDignore.symbol.file ExecutionEnvironment.java
  29  * @run main/othervm ExecutionEnvironment
  30  */
  31 
  32 /*
  33  * This tests for various things as follows:
  34  * Ensures that:
  35  *   1. uneccessary execs do not occur
  36  *   2. the environment is pristine,  users environment variable wrt.
  37  *      LD_LIBRARY_PATH if set are not modified in any way.
  38  *   3. the correct vm is chosen with -server and -client options
  39  *   4. the VM on Solaris correctly interprets the LD_LIBRARY_PATH32
  40  *      and LD_LIBRARY_PATH64 variables if set by the user, ie.
  41  *      i. on 32 bit systems:
  42  *         a. if LD_LIBRARY_PATH32 is set it will override LD_LIBRARY_PATH
  43  *         b. LD_LIBRARY_PATH64 is ignored if set
  44  *      ii. on 64 bit systems:
  45  *            a. if LD_LIBRARY_PATH64 is set it will override LD_LIBRARY_PATH
  46  *            b. LD_LIBRARY_PATH32 is ignored if set
  47  *   5. no extra symlink exists on Solaris ie.
  48  *      jre/lib/$arch/libjvm.so -> client/libjvm.so
  49  *   6. Since 32-bit Solaris is no longer supported we continue to ensure that
  50  *      the appropriate paths are ignored or used, additionally we also test to
  51  *      ensure the 64-bit isadir exists and contains appropriate links.
  52  * TODO:
  53  *      a. perhaps we need to add a test to audit all environment variables are
  54  *         in pristine condition after the launch, there may be a few that the
  55  *         launcher may add as implementation details.
  56  *      b. add a pldd for solaris to ensure only one libjvm.so is linked
  57  */
  58 import java.io.File;
  59 import java.io.FileNotFoundException;
  60 import java.io.IOException;
  61 import java.nio.file.DirectoryStream;
  62 import java.nio.file.Files;
  63 import java.nio.file.Path;
  64 import java.util.ArrayList;
  65 import java.util.HashMap;
  66 import java.util.List;
  67 import java.util.Map;
  68 import static java.nio.file.LinkOption.*;
  69 import java.util.regex.Pattern;
  70 
  71 
  72 public class ExecutionEnvironment extends TestHelper {
  73     static final String LD_LIBRARY_PATH    = TestHelper.isMacOSX
  74             ? "DYLD_LIBRARY_PATH"
  75             : "LD_LIBRARY_PATH";
  76     static final String LD_LIBRARY_PATH_32 = LD_LIBRARY_PATH + "_32";
  77     static final String LD_LIBRARY_PATH_64 = LD_LIBRARY_PATH + "_64";
  78 
  79     // Note: these paths need not exist on the filesytem
  80     static final String LD_LIBRARY_PATH_VALUE    = "/Bridge/On/The/River/Kwai";
  81     static final String LD_LIBRARY_PATH_32_VALUE = "/Lawrence/Of/Arabia";
  82     static final String LD_LIBRARY_PATH_64_VALUE = "/A/Passage/To/India";
  83 
  84     static final String[] LD_PATH_STRINGS = {
  85         LD_LIBRARY_PATH + "=" + LD_LIBRARY_PATH_VALUE,
  86         LD_LIBRARY_PATH_32 + "=" + LD_LIBRARY_PATH_32_VALUE,
  87         LD_LIBRARY_PATH_64 + "=" + LD_LIBRARY_PATH_64_VALUE
  88     };
  89 
  90     static final File testJarFile = new File("EcoFriendly.jar");
  91 
  92     public ExecutionEnvironment() {
  93         createTestJar();
  94     }
  95 
  96     static void createTestJar() {
  97         try {
  98             List<String> codeList = new ArrayList<>();
  99             codeList.add("static void printValue(String name, boolean property) {\n");
 100             codeList.add("    String value = (property) ? System.getProperty(name) : System.getenv(name);\n");
 101             codeList.add("    System.out.println(name + \"=\" + value);\n");
 102             codeList.add("}\n");
 103             codeList.add("public static void main(String... args) {\n");
 104             codeList.add("    System.out.println(\"Execute test:\");\n");
 105             codeList.add("    printValue(\"os.name\", true);\n");
 106             codeList.add("    printValue(\"os.arch\", true);\n");
 107             codeList.add("    printValue(\"os.version\", true);\n");
 108             codeList.add("    printValue(\"sun.arch.data.model\", true);\n");
 109             codeList.add("    printValue(\"java.library.path\", true);\n");
 110             codeList.add("    printValue(\"" + LD_LIBRARY_PATH + "\", false);\n");
 111             codeList.add("    printValue(\"" + LD_LIBRARY_PATH_32 + "\", false);\n");
 112             codeList.add("    printValue(\"" + LD_LIBRARY_PATH_64 + "\", false);\n");
 113             codeList.add("}\n");
 114             String[] clist = new String[codeList.size()];
 115             createJar(testJarFile, codeList.toArray(clist));
 116         } catch (FileNotFoundException fnfe) {
 117             throw new RuntimeException(fnfe);
 118         }
 119     }
 120     private void flagError(TestResult tr, String message) {
 121         System.err.println(tr);
 122         throw new RuntimeException(message);
 123     }
 124     /*
 125      * tests if the launcher pollutes the LD_LIBRARY_PATH variables ie. there
 126      * should not be any new variables or pollution/mutations of any kind, the
 127      * environment should be pristine.
 128      */
 129     @Test
 130     void testEcoFriendly() {
 131         TestResult tr = null;
 132 
 133         Map<String, String> env = new HashMap<>();
 134         for (String x : LD_PATH_STRINGS) {
 135             String pairs[] = x.split("=");
 136             env.put(pairs[0], pairs[1]);
 137         }
 138 
 139         tr = doExec(env, javaCmd, "-jar", testJarFile.getAbsolutePath());
 140 
 141         if (!tr.isNotZeroOutput()) {
 142             flagError(tr, "Error: No output at all. Did the test execute ?");
 143         }
 144 
 145         for (String x : LD_PATH_STRINGS) {
 146             if (!tr.contains(x)) {
 147                 flagError(tr, "FAIL: did not get <" + x + ">");
 148             }
 149         }
 150     }
 151 
 152     /*
 153      * ensures that there are no execs as long as we are in the same
 154      * data model
 155      */
 156     @Test
 157     void testNoExec() {
 158         Map<String, String> env = new HashMap<>();
 159         env.put(JLDEBUG_KEY, "true");
 160         TestResult tr = doExec(env, javaCmd, "-version");
 161         if (tr.testOutput.contains(EXPECTED_MARKER)) {
 162             flagError(tr, "testNoExec: found  warning <" + EXPECTED_MARKER +
 163                     "> the process execing ?");
 164         }
 165     }
 166 
 167     /*
 168      * This test ensures that LD_LIBRARY_PATH* values are interpreted by the VM
 169      * and the expected java.library.path behaviour.
 170      * For Generic platforms (All *nixes):
 171      *    * All LD_LIBRARY_PATH variable should be on java.library.path
 172      * For Solaris 32-bit
 173      *    * The LD_LIBRARY_PATH_32 should override LD_LIBRARY_PATH if specified
 174      * For Solaris 64-bit
 175      *    * The LD_LIBRARY_PATH_64 should override LD_LIBRARY_PATH if specified
 176      */
 177     @Test
 178     void testJavaLibraryPath() {
 179         TestResult tr = null;
 180 
 181         Map<String, String> env = new HashMap<>();
 182 
 183         if (TestHelper.isLinux || TestHelper.isMacOSX) {
 184             for (String x : LD_PATH_STRINGS) {
 185                 String pairs[] = x.split("=");
 186                 env.put(pairs[0], pairs[1]);
 187             }
 188 
 189             tr = doExec(env, javaCmd, "-jar", testJarFile.getAbsolutePath());
 190             verifyJavaLibraryPathGeneric(tr);
 191         } else { // Solaris
 192             // no override
 193             env.clear();
 194             env.put(LD_LIBRARY_PATH, LD_LIBRARY_PATH_VALUE);
 195             tr = doExec(env, javaCmd, "-jar", testJarFile.getAbsolutePath());
 196             verifyJavaLibraryPathGeneric(tr);
 197 
 198             env.clear();
 199             for (String x : LD_PATH_STRINGS) {
 200                 String pairs[] = x.split("=");
 201                 env.put(pairs[0], pairs[1]);
 202             }
 203 
 204             // verify the override occurs for 64-bit system
 205             tr = doExec(env, javaCmd, "-jar", testJarFile.getAbsolutePath());
 206             verifyJavaLibraryPathOverride(tr, false);
 207         }
 208     }
 209 
 210     private void verifyJavaLibraryPathGeneric(TestResult tr) {
 211         if (!tr.matches("java.library.path=.*" + LD_LIBRARY_PATH_VALUE + ".*")) {
 212             flagError(tr, "testJavaLibraryPath: java.library.path does not contain " +
 213                     LD_LIBRARY_PATH_VALUE);
 214         }
 215     }
 216 
 217     private void verifyJavaLibraryPathOverride(TestResult tr,
 218             boolean is32Bit) {
 219         // make sure the 32/64 bit value exists
 220         if (!tr.matches("java.library.path=.*" +
 221                 (is32Bit ? LD_LIBRARY_PATH_32_VALUE : LD_LIBRARY_PATH_64_VALUE) + ".*")) {
 222             flagError(tr, "verifyJavaLibraryPathOverride: " +
 223                 " java.library.path does not contain " +
 224                     (is32Bit ? LD_LIBRARY_PATH_32_VALUE : LD_LIBRARY_PATH_64_VALUE));
 225 
 226         }
 227         // make sure the generic value is absent
 228         if (!tr.notMatches("java.library.path=.*" + LD_LIBRARY_PATH_VALUE + ".*")) {
 229             flagError(tr, "verifyJavaLibraryPathOverride: " +
 230                     " java.library.path contains " + LD_LIBRARY_PATH_VALUE);
 231         }
 232     }
 233 
 234     /*
 235      * ensures we have indeed exec'ed the correct vm of choice if it exists
 236      */
 237     @Test
 238     void testVmSelection() {
 239 
 240         TestResult tr = null;
 241 
 242         if (haveClientVM) {
 243             tr = doExec(javaCmd, "-client", "-version");
 244             if (!tr.matches(".*Client VM.*")) {
 245                 flagError(tr, "the expected vm -client did not launch");
 246             }
 247         }
 248         if (haveServerVM) {
 249             tr = doExec(javaCmd, "-server", "-version");
 250             if (!tr.matches(".*Server VM.*")) {
 251                 flagError(tr, "the expected vm -server did not launch");
 252             }
 253         }
 254     }
 255 
 256     /*
 257      * checks to see there is no extra libjvm.so than needed
 258      */
 259     @Test
 260     void testNoSymLink() {
 261         if (is64Bit) {
 262             return;
 263         }
 264 
 265         File symLink = null;
 266         String libPathPrefix = isSDK ? "jre/lib" : "/lib";
 267         symLink = new File(JAVAHOME, libPathPrefix +
 268                 getJreArch() + "/" + LIBJVM);
 269         if (symLink.exists()) {
 270             throw new RuntimeException("symlink exists " + symLink.getAbsolutePath());
 271         }
 272     }
 273 
 274     /*
 275      * verify if all the symlinks in the images are created correctly,
 276      * only on solaris, this test works only on images.
 277      */
 278     @Test
 279     void testSymLinks() throws Exception {
 280         if (!isSolaris)
 281             return;
 282         verifySymLinks(JAVA_BIN);
 283         verifySymLinks(JAVA_JRE_BIN);
 284     }
 285     // exclude non-consequential binaries or scripts co-packaged in other
 286     // build phases
 287     private final String excludeRE =
 288             ".*jvisualvm.*" +
 289             "|.*javaws.*" +
 290             "|.*ControlPanel.*" +
 291             "|.*java-rmi.cgi" +
 292             "|.*jcontrol.*";
 293     private final Pattern symlinkExcludes = Pattern.compile(excludeRE);
 294 
 295     private void verifySymLinks(String bindir) throws IOException {
 296         File binDir = new File(bindir);
 297         System.err.println("verifying links in: " + bindir);
 298         File isaDir = new File(binDir, getArch()).getAbsoluteFile();
 299         if (!isaDir.exists()) {
 300             throw new RuntimeException("dir: " + isaDir + " does not exist");
 301         }
 302         try (DirectoryStream<Path> ds = Files.newDirectoryStream(binDir.toPath())) {
 303             for (Path p : ds) {
 304                 if (symlinkExcludes.matcher(p.toString()).matches() ||
 305                         Files.isDirectory(p, NOFOLLOW_LINKS)) {
 306                     continue;
 307                 }
 308                 Path link = new File(isaDir, p.getFileName().toString()).toPath();
 309                 if (Files.isSymbolicLink(link)) {
 310                     Path target = Files.readSymbolicLink(link);
 311                     if (target.startsWith("..") && p.endsWith(target.getFileName())) {
 312                         // System.out.println(target + " OK");
 313                         continue;
 314                     }
 315                     System.err.println("target:" + target);
 316                     System.err.println("file:" + p);
 317                 }
 318                 throw new RuntimeException("could not find link to " + p);
 319             }
 320         }
 321 
 322     }
 323     public static void main(String... args) throws Exception {
 324         if (isWindows) {
 325             System.err.println("Warning: test not applicable to windows");
 326             return;
 327         }
 328         ExecutionEnvironment ee = new ExecutionEnvironment();
 329         ee.run(args);
 330     }
 331 }