Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/launcher/ExecutionEnvironment.java
38833 views
/*1* Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4780570 4731671 6354700 6367077 6670965 488297426* @summary Checks for LD_LIBRARY_PATH and execution on *nixes27* @compile -XDignore.symbol.file ExecutionEnvironment.java28* @run main/othervm ExecutionEnvironment29*/3031/*32* This tests for various things as follows:33* Ensures that:34* 1. uneccessary execs do not occur35* 2. the environment is pristine, users environment variable wrt.36* LD_LIBRARY_PATH if set are not modified in any way.37* 3. the correct vm is chosen with -server and -client options38* 4. the VM on Solaris correctly interprets the LD_LIBRARY_PATH3239* and LD_LIBRARY_PATH64 variables if set by the user, ie.40* i. on 32 bit systems:41* a. if LD_LIBRARY_PATH32 is set it will override LD_LIBRARY_PATH42* b. LD_LIBRARY_PATH64 is ignored if set43* ii. on 64 bit systems:44* a. if LD_LIBRARY_PATH64 is set it will override LD_LIBRARY_PATH45* b. LD_LIBRARY_PATH32 is ignored if set46* 5. no extra symlink exists on Solaris ie.47* jre/lib/$arch/libjvm.so -> client/libjvm.so48* 6. Since 32-bit Solaris is no longer supported we continue to ensure that49* the appropriate paths are ignored or used, additionally we also test to50* ensure the 64-bit isadir exists and contains appropriate links.51* TODO:52* a. perhaps we need to add a test to audit all environment variables are53* in pristine condition after the launch, there may be a few that the54* launcher may add as implementation details.55* b. add a pldd for solaris to ensure only one libjvm.so is linked56*/57import java.io.File;58import java.io.FileNotFoundException;59import java.io.IOException;60import java.nio.file.DirectoryStream;61import java.nio.file.Files;62import java.nio.file.Path;63import java.util.ArrayList;64import java.util.HashMap;65import java.util.List;66import java.util.Map;67import static java.nio.file.LinkOption.*;68import java.util.regex.Pattern;697071public class ExecutionEnvironment extends TestHelper {72static final String LD_LIBRARY_PATH = TestHelper.isMacOSX73? "DYLD_LIBRARY_PATH"74: TestHelper.isAIX75? "LIBPATH"76: "LD_LIBRARY_PATH";77static final String LD_LIBRARY_PATH_32 = LD_LIBRARY_PATH + "_32";78static final String LD_LIBRARY_PATH_64 = LD_LIBRARY_PATH + "_64";7980// Note: these paths need not exist on the filesytem81static final String LD_LIBRARY_PATH_VALUE = "/Bridge/On/The/River/Kwai";82static final String LD_LIBRARY_PATH_32_VALUE = "/Lawrence/Of/Arabia";83static final String LD_LIBRARY_PATH_64_VALUE = "/A/Passage/To/India";8485static final String[] LD_PATH_STRINGS = {86LD_LIBRARY_PATH + "=" + LD_LIBRARY_PATH_VALUE,87LD_LIBRARY_PATH_32 + "=" + LD_LIBRARY_PATH_32_VALUE,88LD_LIBRARY_PATH_64 + "=" + LD_LIBRARY_PATH_64_VALUE89};9091static final File testJarFile = new File("EcoFriendly.jar");9293public ExecutionEnvironment() {94createTestJar();95}9697static void createTestJar() {98try {99List<String> codeList = new ArrayList<>();100codeList.add("static void printValue(String name, boolean property) {\n");101codeList.add(" String value = (property) ? System.getProperty(name) : System.getenv(name);\n");102codeList.add(" System.out.println(name + \"=\" + value);\n");103codeList.add("}\n");104codeList.add("public static void main(String... args) {\n");105codeList.add(" System.out.println(\"Execute test:\");\n");106codeList.add(" printValue(\"os.name\", true);\n");107codeList.add(" printValue(\"os.arch\", true);\n");108codeList.add(" printValue(\"os.version\", true);\n");109codeList.add(" printValue(\"sun.arch.data.model\", true);\n");110codeList.add(" printValue(\"java.library.path\", true);\n");111codeList.add(" printValue(\"" + LD_LIBRARY_PATH + "\", false);\n");112codeList.add(" printValue(\"" + LD_LIBRARY_PATH_32 + "\", false);\n");113codeList.add(" printValue(\"" + LD_LIBRARY_PATH_64 + "\", false);\n");114codeList.add("}\n");115String[] clist = new String[codeList.size()];116createJar(testJarFile, codeList.toArray(clist));117} catch (FileNotFoundException fnfe) {118throw new RuntimeException(fnfe);119}120}121private void flagError(TestResult tr, String message) {122System.err.println(tr);123throw new RuntimeException(message);124}125/*126* tests if the launcher pollutes the LD_LIBRARY_PATH variables ie. there127* should not be any new variables or pollution/mutations of any kind, the128* environment should be pristine.129*/130@Test131void testEcoFriendly() {132TestResult tr = null;133134Map<String, String> env = new HashMap<>();135for (String x : LD_PATH_STRINGS) {136String pairs[] = x.split("=");137env.put(pairs[0], pairs[1]);138}139140tr = doExec(env, javaCmd, "-jar", testJarFile.getAbsolutePath());141142if (!tr.isNotZeroOutput()) {143flagError(tr, "Error: No output at all. Did the test execute ?");144}145146for (String x : LD_PATH_STRINGS) {147if (!tr.contains(x)) {148if (TestHelper.isAIX && x.startsWith(LD_LIBRARY_PATH)) {149// AIX does not support the '-rpath' linker options so the150// launchers have to prepend the jdk library path to 'LIBPATH'.151String aixLibPath = LD_LIBRARY_PATH + "=" +152System.getenv(LD_LIBRARY_PATH) +153System.getProperty("path.separator") + LD_LIBRARY_PATH_VALUE;154if (!tr.matches(aixLibPath)) {155flagError(tr, "FAIL: did not get <" + aixLibPath + ">");156}157}158else {159flagError(tr, "FAIL: did not get <" + x + ">");160}161}162}163}164165/*166* ensures that there are no execs as long as we are in the same167* data model168*/169@Test170void testNoExec() {171Map<String, String> env = new HashMap<>();172env.put(JLDEBUG_KEY, "true");173TestResult tr = doExec(env, javaCmd, "-version");174if (tr.testOutput.contains(EXPECTED_MARKER)) {175flagError(tr, "testNoExec: found warning <" + EXPECTED_MARKER +176"> the process execing ?");177}178}179180/*181* This test ensures that LD_LIBRARY_PATH* values are interpreted by the VM182* and the expected java.library.path behaviour.183* For Generic platforms (All *nixes):184* * All LD_LIBRARY_PATH variable should be on java.library.path185* For Solaris 32-bit186* * The LD_LIBRARY_PATH_32 should override LD_LIBRARY_PATH if specified187* For Solaris 64-bit188* * The LD_LIBRARY_PATH_64 should override LD_LIBRARY_PATH if specified189*/190@Test191void testJavaLibraryPath() {192TestResult tr = null;193194Map<String, String> env = new HashMap<>();195196if (TestHelper.isLinux || TestHelper.isMacOSX || TestHelper.isAIX) {197for (String x : LD_PATH_STRINGS) {198String pairs[] = x.split("=");199env.put(pairs[0], pairs[1]);200}201202tr = doExec(env, javaCmd, "-jar", testJarFile.getAbsolutePath());203verifyJavaLibraryPathGeneric(tr);204} else { // Solaris205// no override206env.clear();207env.put(LD_LIBRARY_PATH, LD_LIBRARY_PATH_VALUE);208tr = doExec(env, javaCmd, "-jar", testJarFile.getAbsolutePath());209verifyJavaLibraryPathGeneric(tr);210211env.clear();212for (String x : LD_PATH_STRINGS) {213String pairs[] = x.split("=");214env.put(pairs[0], pairs[1]);215}216217// verify the override occurs for 64-bit system218tr = doExec(env, javaCmd, "-jar", testJarFile.getAbsolutePath());219verifyJavaLibraryPathOverride(tr, false);220}221}222223private void verifyJavaLibraryPathGeneric(TestResult tr) {224if (!tr.matches("java.library.path=.*" + LD_LIBRARY_PATH_VALUE + ".*")) {225flagError(tr, "testJavaLibraryPath: java.library.path does not contain " +226LD_LIBRARY_PATH_VALUE);227}228}229230private void verifyJavaLibraryPathOverride(TestResult tr,231boolean is32Bit) {232// make sure the 32/64 bit value exists233if (!tr.matches("java.library.path=.*" +234(is32Bit ? LD_LIBRARY_PATH_32_VALUE : LD_LIBRARY_PATH_64_VALUE) + ".*")) {235flagError(tr, "verifyJavaLibraryPathOverride: " +236" java.library.path does not contain " +237(is32Bit ? LD_LIBRARY_PATH_32_VALUE : LD_LIBRARY_PATH_64_VALUE));238239}240// make sure the generic value is absent241if (!tr.notMatches("java.library.path=.*" + LD_LIBRARY_PATH_VALUE + ".*")) {242flagError(tr, "verifyJavaLibraryPathOverride: " +243" java.library.path contains " + LD_LIBRARY_PATH_VALUE);244}245}246247/*248* ensures we have indeed exec'ed the correct vm of choice if it exists249*/250@Test251void testVmSelection() {252253TestResult tr = null;254255if (haveClientVM) {256tr = doExec(javaCmd, "-client", "-version");257if (!tr.matches(".*Client VM.*")) {258flagError(tr, "the expected vm -client did not launch");259}260}261if (haveServerVM) {262tr = doExec(javaCmd, "-server", "-version");263if (!tr.matches(".*Server VM.*")) {264flagError(tr, "the expected vm -server did not launch");265}266}267}268269/*270* checks to see there is no extra libjvm.so than needed271*/272@Test273void testNoSymLink() {274if (is64Bit) {275return;276}277278File symLink = null;279String libPathPrefix = isSDK ? "jre/lib" : "/lib";280symLink = new File(JAVAHOME, libPathPrefix +281getJreArch() + "/" + LIBJVM);282if (symLink.exists()) {283throw new RuntimeException("symlink exists " + symLink.getAbsolutePath());284}285}286287/*288* verify if all the symlinks in the images are created correctly,289* only on solaris, this test works only on images.290*/291@Test292void testSymLinks() throws Exception {293if (!isSolaris)294return;295verifySymLinks(JAVA_BIN);296verifySymLinks(JAVA_JRE_BIN);297}298// exclude non-consequential binaries or scripts co-packaged in other299// build phases300private final String excludeRE =301".*jvisualvm.*" +302"|.*javaws.*" +303"|.*ControlPanel.*" +304"|.*java-rmi.cgi" +305"|.*jcontrol.*";306private final Pattern symlinkExcludes = Pattern.compile(excludeRE);307308private void verifySymLinks(String bindir) throws IOException {309File binDir = new File(bindir);310System.err.println("verifying links in: " + bindir);311File isaDir = new File(binDir, getArch()).getAbsoluteFile();312if (!isaDir.exists()) {313throw new RuntimeException("dir: " + isaDir + " does not exist");314}315try (DirectoryStream<Path> ds = Files.newDirectoryStream(binDir.toPath())) {316for (Path p : ds) {317if (symlinkExcludes.matcher(p.toString()).matches() ||318Files.isDirectory(p, NOFOLLOW_LINKS)) {319continue;320}321Path link = new File(isaDir, p.getFileName().toString()).toPath();322if (Files.isSymbolicLink(link)) {323Path target = Files.readSymbolicLink(link);324if (target.startsWith("..") && p.endsWith(target.getFileName())) {325// System.out.println(target + " OK");326continue;327}328System.err.println("target:" + target);329System.err.println("file:" + p);330}331throw new RuntimeException("could not find link to " + p);332}333}334335}336public static void main(String... args) throws Exception {337if (isWindows) {338System.err.println("Warning: test not applicable to windows");339return;340}341ExecutionEnvironment ee = new ExecutionEnvironment();342ee.run(args);343}344}345346347