Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/launcher/ExecutionEnvironment.java
38833 views
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
: TestHelper.isAIX
76
? "LIBPATH"
77
: "LD_LIBRARY_PATH";
78
static final String LD_LIBRARY_PATH_32 = LD_LIBRARY_PATH + "_32";
79
static final String LD_LIBRARY_PATH_64 = LD_LIBRARY_PATH + "_64";
80
81
// Note: these paths need not exist on the filesytem
82
static final String LD_LIBRARY_PATH_VALUE = "/Bridge/On/The/River/Kwai";
83
static final String LD_LIBRARY_PATH_32_VALUE = "/Lawrence/Of/Arabia";
84
static final String LD_LIBRARY_PATH_64_VALUE = "/A/Passage/To/India";
85
86
static final String[] LD_PATH_STRINGS = {
87
LD_LIBRARY_PATH + "=" + LD_LIBRARY_PATH_VALUE,
88
LD_LIBRARY_PATH_32 + "=" + LD_LIBRARY_PATH_32_VALUE,
89
LD_LIBRARY_PATH_64 + "=" + LD_LIBRARY_PATH_64_VALUE
90
};
91
92
static final File testJarFile = new File("EcoFriendly.jar");
93
94
public ExecutionEnvironment() {
95
createTestJar();
96
}
97
98
static void createTestJar() {
99
try {
100
List<String> codeList = new ArrayList<>();
101
codeList.add("static void printValue(String name, boolean property) {\n");
102
codeList.add(" String value = (property) ? System.getProperty(name) : System.getenv(name);\n");
103
codeList.add(" System.out.println(name + \"=\" + value);\n");
104
codeList.add("}\n");
105
codeList.add("public static void main(String... args) {\n");
106
codeList.add(" System.out.println(\"Execute test:\");\n");
107
codeList.add(" printValue(\"os.name\", true);\n");
108
codeList.add(" printValue(\"os.arch\", true);\n");
109
codeList.add(" printValue(\"os.version\", true);\n");
110
codeList.add(" printValue(\"sun.arch.data.model\", true);\n");
111
codeList.add(" printValue(\"java.library.path\", true);\n");
112
codeList.add(" printValue(\"" + LD_LIBRARY_PATH + "\", false);\n");
113
codeList.add(" printValue(\"" + LD_LIBRARY_PATH_32 + "\", false);\n");
114
codeList.add(" printValue(\"" + LD_LIBRARY_PATH_64 + "\", false);\n");
115
codeList.add("}\n");
116
String[] clist = new String[codeList.size()];
117
createJar(testJarFile, codeList.toArray(clist));
118
} catch (FileNotFoundException fnfe) {
119
throw new RuntimeException(fnfe);
120
}
121
}
122
private void flagError(TestResult tr, String message) {
123
System.err.println(tr);
124
throw new RuntimeException(message);
125
}
126
/*
127
* tests if the launcher pollutes the LD_LIBRARY_PATH variables ie. there
128
* should not be any new variables or pollution/mutations of any kind, the
129
* environment should be pristine.
130
*/
131
@Test
132
void testEcoFriendly() {
133
TestResult tr = null;
134
135
Map<String, String> env = new HashMap<>();
136
for (String x : LD_PATH_STRINGS) {
137
String pairs[] = x.split("=");
138
env.put(pairs[0], pairs[1]);
139
}
140
141
tr = doExec(env, javaCmd, "-jar", testJarFile.getAbsolutePath());
142
143
if (!tr.isNotZeroOutput()) {
144
flagError(tr, "Error: No output at all. Did the test execute ?");
145
}
146
147
for (String x : LD_PATH_STRINGS) {
148
if (!tr.contains(x)) {
149
if (TestHelper.isAIX && x.startsWith(LD_LIBRARY_PATH)) {
150
// AIX does not support the '-rpath' linker options so the
151
// launchers have to prepend the jdk library path to 'LIBPATH'.
152
String aixLibPath = LD_LIBRARY_PATH + "=" +
153
System.getenv(LD_LIBRARY_PATH) +
154
System.getProperty("path.separator") + LD_LIBRARY_PATH_VALUE;
155
if (!tr.matches(aixLibPath)) {
156
flagError(tr, "FAIL: did not get <" + aixLibPath + ">");
157
}
158
}
159
else {
160
flagError(tr, "FAIL: did not get <" + x + ">");
161
}
162
}
163
}
164
}
165
166
/*
167
* ensures that there are no execs as long as we are in the same
168
* data model
169
*/
170
@Test
171
void testNoExec() {
172
Map<String, String> env = new HashMap<>();
173
env.put(JLDEBUG_KEY, "true");
174
TestResult tr = doExec(env, javaCmd, "-version");
175
if (tr.testOutput.contains(EXPECTED_MARKER)) {
176
flagError(tr, "testNoExec: found warning <" + EXPECTED_MARKER +
177
"> the process execing ?");
178
}
179
}
180
181
/*
182
* This test ensures that LD_LIBRARY_PATH* values are interpreted by the VM
183
* and the expected java.library.path behaviour.
184
* For Generic platforms (All *nixes):
185
* * All LD_LIBRARY_PATH variable should be on java.library.path
186
* For Solaris 32-bit
187
* * The LD_LIBRARY_PATH_32 should override LD_LIBRARY_PATH if specified
188
* For Solaris 64-bit
189
* * The LD_LIBRARY_PATH_64 should override LD_LIBRARY_PATH if specified
190
*/
191
@Test
192
void testJavaLibraryPath() {
193
TestResult tr = null;
194
195
Map<String, String> env = new HashMap<>();
196
197
if (TestHelper.isLinux || TestHelper.isMacOSX || TestHelper.isAIX) {
198
for (String x : LD_PATH_STRINGS) {
199
String pairs[] = x.split("=");
200
env.put(pairs[0], pairs[1]);
201
}
202
203
tr = doExec(env, javaCmd, "-jar", testJarFile.getAbsolutePath());
204
verifyJavaLibraryPathGeneric(tr);
205
} else { // Solaris
206
// no override
207
env.clear();
208
env.put(LD_LIBRARY_PATH, LD_LIBRARY_PATH_VALUE);
209
tr = doExec(env, javaCmd, "-jar", testJarFile.getAbsolutePath());
210
verifyJavaLibraryPathGeneric(tr);
211
212
env.clear();
213
for (String x : LD_PATH_STRINGS) {
214
String pairs[] = x.split("=");
215
env.put(pairs[0], pairs[1]);
216
}
217
218
// verify the override occurs for 64-bit system
219
tr = doExec(env, javaCmd, "-jar", testJarFile.getAbsolutePath());
220
verifyJavaLibraryPathOverride(tr, false);
221
}
222
}
223
224
private void verifyJavaLibraryPathGeneric(TestResult tr) {
225
if (!tr.matches("java.library.path=.*" + LD_LIBRARY_PATH_VALUE + ".*")) {
226
flagError(tr, "testJavaLibraryPath: java.library.path does not contain " +
227
LD_LIBRARY_PATH_VALUE);
228
}
229
}
230
231
private void verifyJavaLibraryPathOverride(TestResult tr,
232
boolean is32Bit) {
233
// make sure the 32/64 bit value exists
234
if (!tr.matches("java.library.path=.*" +
235
(is32Bit ? LD_LIBRARY_PATH_32_VALUE : LD_LIBRARY_PATH_64_VALUE) + ".*")) {
236
flagError(tr, "verifyJavaLibraryPathOverride: " +
237
" java.library.path does not contain " +
238
(is32Bit ? LD_LIBRARY_PATH_32_VALUE : LD_LIBRARY_PATH_64_VALUE));
239
240
}
241
// make sure the generic value is absent
242
if (!tr.notMatches("java.library.path=.*" + LD_LIBRARY_PATH_VALUE + ".*")) {
243
flagError(tr, "verifyJavaLibraryPathOverride: " +
244
" java.library.path contains " + LD_LIBRARY_PATH_VALUE);
245
}
246
}
247
248
/*
249
* ensures we have indeed exec'ed the correct vm of choice if it exists
250
*/
251
@Test
252
void testVmSelection() {
253
254
TestResult tr = null;
255
256
if (haveClientVM) {
257
tr = doExec(javaCmd, "-client", "-version");
258
if (!tr.matches(".*Client VM.*")) {
259
flagError(tr, "the expected vm -client did not launch");
260
}
261
}
262
if (haveServerVM) {
263
tr = doExec(javaCmd, "-server", "-version");
264
if (!tr.matches(".*Server VM.*")) {
265
flagError(tr, "the expected vm -server did not launch");
266
}
267
}
268
}
269
270
/*
271
* checks to see there is no extra libjvm.so than needed
272
*/
273
@Test
274
void testNoSymLink() {
275
if (is64Bit) {
276
return;
277
}
278
279
File symLink = null;
280
String libPathPrefix = isSDK ? "jre/lib" : "/lib";
281
symLink = new File(JAVAHOME, libPathPrefix +
282
getJreArch() + "/" + LIBJVM);
283
if (symLink.exists()) {
284
throw new RuntimeException("symlink exists " + symLink.getAbsolutePath());
285
}
286
}
287
288
/*
289
* verify if all the symlinks in the images are created correctly,
290
* only on solaris, this test works only on images.
291
*/
292
@Test
293
void testSymLinks() throws Exception {
294
if (!isSolaris)
295
return;
296
verifySymLinks(JAVA_BIN);
297
verifySymLinks(JAVA_JRE_BIN);
298
}
299
// exclude non-consequential binaries or scripts co-packaged in other
300
// build phases
301
private final String excludeRE =
302
".*jvisualvm.*" +
303
"|.*javaws.*" +
304
"|.*ControlPanel.*" +
305
"|.*java-rmi.cgi" +
306
"|.*jcontrol.*";
307
private final Pattern symlinkExcludes = Pattern.compile(excludeRE);
308
309
private void verifySymLinks(String bindir) throws IOException {
310
File binDir = new File(bindir);
311
System.err.println("verifying links in: " + bindir);
312
File isaDir = new File(binDir, getArch()).getAbsoluteFile();
313
if (!isaDir.exists()) {
314
throw new RuntimeException("dir: " + isaDir + " does not exist");
315
}
316
try (DirectoryStream<Path> ds = Files.newDirectoryStream(binDir.toPath())) {
317
for (Path p : ds) {
318
if (symlinkExcludes.matcher(p.toString()).matches() ||
319
Files.isDirectory(p, NOFOLLOW_LINKS)) {
320
continue;
321
}
322
Path link = new File(isaDir, p.getFileName().toString()).toPath();
323
if (Files.isSymbolicLink(link)) {
324
Path target = Files.readSymbolicLink(link);
325
if (target.startsWith("..") && p.endsWith(target.getFileName())) {
326
// System.out.println(target + " OK");
327
continue;
328
}
329
System.err.println("target:" + target);
330
System.err.println("file:" + p);
331
}
332
throw new RuntimeException("could not find link to " + p);
333
}
334
}
335
336
}
337
public static void main(String... args) throws Exception {
338
if (isWindows) {
339
System.err.println("Warning: test not applicable to windows");
340
return;
341
}
342
ExecutionEnvironment ee = new ExecutionEnvironment();
343
ee.run(args);
344
}
345
}
346
347