1 /* 2 * Copyright (c) 2009, 2012, 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 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 * TODO: 50 * a. perhaps we need to add a test to audit all environment variables are 51 * in pristine condition after the launch, there may be a few that the 52 * launcher may add as implementation details. 53 * b. add a pldd for solaris to ensure only one libjvm.so is linked 54 */ 55 import java.io.File; 56 import java.io.FileNotFoundException; 57 import java.util.ArrayList; 58 import java.util.HashMap; 59 import java.util.List; 60 import java.util.Map; 61 62 63 public class ExecutionEnvironment extends TestHelper { 64 static final String LD_LIBRARY_PATH = TestHelper.isMacOSX 65 ? "DYLD_LIBRARY_PATH" 66 : "LD_LIBRARY_PATH"; 67 static final String LD_LIBRARY_PATH_32 = LD_LIBRARY_PATH + "_32"; 68 static final String LD_LIBRARY_PATH_64 = LD_LIBRARY_PATH + "_64"; 69 70 // Note: these paths need not exist on the filesytem 71 static final String LD_LIBRARY_PATH_VALUE = "/Bridge/On/The/River/Kwai"; 72 static final String LD_LIBRARY_PATH_32_VALUE = "/Lawrence/Of/Arabia"; 73 static final String LD_LIBRARY_PATH_64_VALUE = "/A/Passage/To/India"; 74 75 static final String[] LD_PATH_STRINGS = { 76 LD_LIBRARY_PATH + "=" + LD_LIBRARY_PATH_VALUE, 77 LD_LIBRARY_PATH_32 + "=" + LD_LIBRARY_PATH_32_VALUE, 78 LD_LIBRARY_PATH_64 + "=" + LD_LIBRARY_PATH_64_VALUE 79 }; 80 81 static final File testJarFile = new File("EcoFriendly.jar"); 82 83 static int errors = 0; 84 static int passes = 0; 85 86 static final String LIBJVM = TestHelper.isWindows 87 ? "jvm.dll" 88 : "libjvm" + (TestHelper.isMacOSX ? ".dylib" : ".so"); 89 90 static void createTestJar() { 91 try { 92 List<String> codeList = new ArrayList<>(); 93 codeList.add("static void printValue(String name, boolean property) {\n"); 94 codeList.add(" String value = (property) ? System.getProperty(name) : System.getenv(name);\n"); 95 codeList.add(" System.out.println(name + \"=\" + value);\n"); 96 codeList.add("}\n"); 97 codeList.add("public static void main(String... args) {\n"); 98 codeList.add(" System.out.println(\"Execute test:\");\n"); 99 codeList.add(" printValue(\"os.name\", true);\n"); 100 codeList.add(" printValue(\"os.arch\", true);\n"); 101 codeList.add(" printValue(\"os.version\", true);\n"); 102 codeList.add(" printValue(\"sun.arch.data.model\", true);\n"); 103 codeList.add(" printValue(\"java.library.path\", true);\n"); 104 codeList.add(" printValue(\"" + LD_LIBRARY_PATH + "\", false);\n"); 105 codeList.add(" printValue(\"" + LD_LIBRARY_PATH_32 + "\", false);\n"); 106 codeList.add(" printValue(\"" + LD_LIBRARY_PATH_64 + "\", false);\n"); 107 codeList.add("}\n"); 108 String[] clist = new String[codeList.size()]; 109 createJar(testJarFile, codeList.toArray(clist)); 110 } catch (FileNotFoundException fnfe) { 111 throw new RuntimeException(fnfe); 112 } 113 } 114 115 /* 116 * tests if the launcher pollutes the LD_LIBRARY_PATH variables ie. there 117 * should not be any new variables or pollution/mutations of any kind, the 118 * environment should be pristine. 119 */ 120 private static void ensureEcoFriendly() { 121 TestResult tr = null; 122 123 Map<String, String> env = new HashMap<>(); 124 for (String x : LD_PATH_STRINGS) { 125 String pairs[] = x.split("="); 126 env.put(pairs[0], pairs[1]); 127 } 128 129 tr = doExec(env, javaCmd, "-jar", testJarFile.getAbsolutePath()); 130 131 if (!tr.isNotZeroOutput()) { 132 System.out.println(tr); 133 throw new RuntimeException("Error: No output at all. Did the test execute ?"); 134 } 135 136 for (String x : LD_PATH_STRINGS) { 137 if (!tr.contains(x)) { 138 System.out.println("FAIL: did not get <" + x + ">"); 139 System.out.println(tr); 140 errors++; 141 } else { 142 passes++; 143 } 144 } 145 } 146 147 /* 148 * ensures that there are no execs as long as we are in the same 149 * data model 150 */ 151 static void ensureNoExec() { 152 Map<String, String> env = new HashMap<>(); 153 env.put(JLDEBUG_KEY, "true"); 154 TestResult tr = doExec(env, javaCmd, "-version"); 155 if (tr.testOutput.contains(EXPECTED_MARKER)) { 156 System.out.println("FAIL: EnsureNoExecs: found expected warning <" + 157 EXPECTED_MARKER + 158 "> the process execing ?"); 159 errors++; 160 } else { 161 passes++; 162 } 163 return; 164 } 165 166 /* 167 * This test ensures that LD_LIBRARY_PATH* values are interpreted by the VM 168 * and the expected java.library.path behaviour. 169 * For Generic platforms (All *nixes): 170 * * All LD_LIBRARY_PATH variable should be on java.library.path 171 * For Solaris 32-bit 172 * * The LD_LIBRARY_PATH_32 should override LD_LIBRARY_PATH if specified 173 * For Solaris 64-bit 174 * * The LD_LIBRARY_PATH_64 should override LD_LIBRARY_PATH if specified 175 */ 176 177 static void verifyJavaLibraryPath() { 178 TestResult tr = null; 179 180 Map<String, String> env = new HashMap<>(); 181 182 if (TestHelper.isLinux || TestHelper.isMacOSX) { 183 for (String x : LD_PATH_STRINGS) { 184 String pairs[] = x.split("="); 185 env.put(pairs[0], pairs[1]); 186 } 187 188 tr = doExec(env, javaCmd, "-jar", testJarFile.getAbsolutePath()); 189 verifyJavaLibraryPathGeneric(tr); 190 } else { 191 // no override 192 env.clear(); 193 env.put(LD_LIBRARY_PATH, LD_LIBRARY_PATH_VALUE); 194 tr = doExec(env, javaCmd, "-jar", testJarFile.getAbsolutePath()); 195 verifyJavaLibraryPathGeneric(tr); 196 197 env.clear(); 198 for (String x : LD_PATH_STRINGS) { 199 String pairs[] = x.split("="); 200 env.put(pairs[0], pairs[1]); 201 } 202 203 // verify the override occurs, since we know the invocation always 204 // uses by default is 32-bit, therefore we also set the test 205 // expectation to be the same. 206 tr = doExec(env, javaCmd, "-jar", testJarFile.getAbsolutePath()); 207 verifyJavaLibraryPathOverride(tr, true); 208 209 // try changing the model from 32 to 64 bit 210 if (dualModePresent() && is32Bit) { 211 // verify the override occurs 212 env.clear(); 213 for (String x : LD_PATH_STRINGS) { 214 String pairs[] = x.split("="); 215 env.put(pairs[0], pairs[1]); 216 } 217 tr = doExec(env, javaCmd, "-d64", "-jar", 218 testJarFile.getAbsolutePath()); 219 verifyJavaLibraryPathOverride(tr, false); 220 221 // no override 222 env.clear(); 223 env.put(LD_LIBRARY_PATH, LD_LIBRARY_PATH_VALUE); 224 tr = doExec(env, javaCmd, "-jar", 225 testJarFile.getAbsolutePath()); 226 verifyJavaLibraryPathGeneric(tr); 227 } 228 229 // try changing the model from 64 to 32 bit 230 if (java64Cmd != null && is64Bit) { 231 // verify the override occurs 232 env.clear(); 233 for (String x : LD_PATH_STRINGS) { 234 String pairs[] = x.split("="); 235 env.put(pairs[0], pairs[1]); 236 } 237 tr = doExec(env, java64Cmd, "-d32", "-jar", 238 testJarFile.getAbsolutePath()); 239 verifyJavaLibraryPathOverride(tr, true); 240 241 // no override 242 env.clear(); 243 env.put(LD_LIBRARY_PATH, LD_LIBRARY_PATH_VALUE); 244 tr = doExec(env, java64Cmd, "-d32", "-jar", 245 testJarFile.getAbsolutePath()); 246 verifyJavaLibraryPathGeneric(tr); 247 } 248 } 249 } 250 251 private static void verifyJavaLibraryPathGeneric(TestResult tr) { 252 if (!tr.matches("java.library.path=.*" + LD_LIBRARY_PATH_VALUE + ".*")) { 253 System.out.print("FAIL: verifyJavaLibraryPath: "); 254 System.out.println(" java.library.path does not contain " + 255 LD_LIBRARY_PATH_VALUE); 256 System.out.println(tr); 257 errors++; 258 } else { 259 passes++; 260 } 261 } 262 263 private static void verifyJavaLibraryPathOverride(TestResult tr, 264 boolean is32Bit) { 265 // make sure the 32/64 bit value exists 266 if (!tr.matches("java.library.path=.*" + 267 (is32Bit ? LD_LIBRARY_PATH_32_VALUE : LD_LIBRARY_PATH_64_VALUE) + ".*")) { 268 System.out.print("FAIL: verifyJavaLibraryPathOverride: "); 269 System.out.println(" java.library.path does not contain " + 270 (is32Bit ? LD_LIBRARY_PATH_32_VALUE : LD_LIBRARY_PATH_64_VALUE)); 271 System.out.println(tr); 272 errors++; 273 } else { 274 passes++; 275 } 276 // make sure the generic value is absent 277 if (tr.matches("java.library.path=.*" + LD_LIBRARY_PATH_VALUE + ".*")) { 278 System.out.print("FAIL: verifyJavaLibraryPathOverride: "); 279 System.out.println(" java.library.path contains " + 280 LD_LIBRARY_PATH_VALUE); 281 System.out.println(tr); 282 errors++; 283 } else { 284 passes++; 285 } 286 } 287 288 /* 289 * ensures we have indeed exec'ed the correct vm of choice, all VMs support 290 * -server, however 32-bit VMs support -client and -server. 291 */ 292 static void verifyVmSelection() { 293 294 TestResult tr = null; 295 296 if (is32Bit) { 297 tr = doExec(javaCmd, "-client", "-version"); 298 if (!tr.matches(".*Client VM.*")) { 299 System.out.println("FAIL: the expected vm -client did not launch"); 300 System.out.println(tr); 301 errors++; 302 } else { 303 passes++; 304 } 305 } 306 tr = doExec(javaCmd, "-server", "-version"); 307 if (!tr.matches(".*Server VM.*")) { 308 System.out.println("FAIL: the expected vm -server did not launch"); 309 System.out.println(tr); 310 errors++; 311 } else { 312 passes++; 313 } 314 } 315 316 /* 317 * checks to see there is no extra libjvm.so than needed 318 */ 319 static void verifyNoSymLink() { 320 if (is64Bit) { 321 return; 322 } 323 324 File symLink = null; 325 String libPathPrefix = isSDK ? "jre/lib" : "/lib"; 326 symLink = new File(JAVAHOME, libPathPrefix + 327 getJreArch() + "/" + LIBJVM); 328 if (symLink.exists()) { 329 System.out.println("FAIL: The symlink exists " + 330 symLink.getAbsolutePath()); 331 errors++; 332 } else { 333 passes++; 334 } 335 } 336 337 public static void main(String... args) throws Exception { 338 if (isWindows) { 339 System.out.println("Warning: noop on windows"); 340 return; 341 } 342 // create our test jar first 343 createTestJar(); 344 ensureNoExec(); 345 verifyVmSelection(); 346 ensureEcoFriendly(); 347 verifyJavaLibraryPath(); 348 verifyNoSymLink(); 349 if (errors > 0) { 350 throw new Exception("ExecutionEnvironment: FAIL: with " + 351 errors + " errors and passes " + passes ); 352 } else { 353 System.out.println("ExecutionEnvironment: PASS " + passes); 354 } 355 } 356 }