Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/build/AbsPathsInImage.java
66642 views
1
/*
2
* Copyright (c) 2019, 2022, 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
import java.io.IOException;
25
import java.io.InputStream;
26
import java.nio.file.FileVisitResult;
27
import java.nio.file.Files;
28
import java.nio.file.Path;
29
import java.nio.file.Paths;
30
import java.nio.file.SimpleFileVisitor;
31
import java.nio.file.attribute.BasicFileAttributes;
32
import java.util.ArrayList;
33
import java.util.List;
34
import java.util.Properties;
35
import java.util.zip.ZipEntry;
36
import java.util.zip.ZipInputStream;
37
38
/*
39
* @test
40
* @bug 8226346
41
* @summary Check all output files for absolute path fragments
42
* @requires !vm.debug
43
* @run main AbsPathsInImage
44
*/
45
public class AbsPathsInImage {
46
47
// Set this property on command line to scan an alternate dir or file:
48
// JTREG=JAVA_OPTIONS=-Djdk.test.build.AbsPathInImage.dir=/path/to/dir
49
public static final String DIR_PROPERTY = "jdk.test.build.AbsPathsInImage.dir";
50
private static final boolean IS_WINDOWS = System.getProperty("os.name").toLowerCase().contains("windows");
51
private static final boolean IS_LINUX = System.getProperty("os.name").toLowerCase().contains("linux");
52
53
private boolean matchFound = false;
54
55
public static void main(String[] args) throws Exception {
56
String jdkPathString = System.getProperty("test.jdk");
57
Path jdkHome = Paths.get(jdkPathString);
58
59
Path dirToScan = jdkHome;
60
String overrideDir = System.getProperty(DIR_PROPERTY);
61
if (overrideDir != null) {
62
dirToScan = Paths.get(overrideDir);
63
}
64
65
String buildWorkspaceRoot = null;
66
String buildOutputRoot = null;
67
String testImageDirString = System.getenv("TEST_IMAGE_DIR");
68
if (testImageDirString != null) {
69
Path testImageDir = Paths.get(testImageDirString);
70
Path buildInfoPropertiesFile = testImageDir.resolve("build-info.properties");
71
System.out.println("Getting patterns from " + buildInfoPropertiesFile.toString());
72
Properties buildInfoProperties = new Properties();
73
try (InputStream inStream = Files.newInputStream(buildInfoPropertiesFile)) {
74
buildInfoProperties.load(inStream);
75
}
76
buildWorkspaceRoot = buildInfoProperties.getProperty("build.workspace.root");
77
buildOutputRoot = buildInfoProperties.getProperty("build.output.root");
78
} else {
79
System.out.println("Getting patterns from local environment");
80
// Try to resolve the workspace root based on the jtreg test root dir
81
String testRootDirString = System.getProperty("test.root");
82
if (testRootDirString != null) {
83
Path testRootDir = Paths.get(testRootDirString);
84
// Remove /test/jdk suffix
85
buildWorkspaceRoot = testRootDir.getParent().getParent().toString();
86
}
87
// Remove /jdk
88
Path buildOutputRootPath = jdkHome.getParent();
89
if (buildOutputRootPath.endsWith("images")) {
90
buildOutputRootPath = buildOutputRootPath.getParent();
91
}
92
buildOutputRoot = buildOutputRootPath.toString();
93
}
94
if (buildWorkspaceRoot == null) {
95
throw new Error("Could not find workspace root, test cannot run");
96
}
97
if (buildOutputRoot == null) {
98
throw new Error("Could not find build output root, test cannot run");
99
}
100
// Validate the root paths
101
if (!Paths.get(buildWorkspaceRoot).isAbsolute()) {
102
throw new Error("Workspace root is not an absolute path: " + buildWorkspaceRoot);
103
}
104
if (!Paths.get(buildOutputRoot).isAbsolute()) {
105
throw new Error("Output root is not an absolute path: " + buildOutputRoot);
106
}
107
108
List<byte[]> searchPatterns = new ArrayList<>();
109
expandPatterns(searchPatterns, buildWorkspaceRoot);
110
expandPatterns(searchPatterns, buildOutputRoot);
111
112
System.out.println("Looking for:");
113
for (byte[] searchPattern : searchPatterns) {
114
System.out.println(new String(searchPattern));
115
}
116
System.out.println();
117
118
AbsPathsInImage absPathsInImage = new AbsPathsInImage();
119
absPathsInImage.scanFiles(dirToScan, searchPatterns);
120
121
if (absPathsInImage.matchFound) {
122
throw new Exception("Test failed");
123
}
124
}
125
126
/**
127
* Add path pattern to list of patterns to search for. Create all possible
128
* variants depending on platform.
129
*/
130
private static void expandPatterns(List<byte[]> searchPatterns, String pattern) {
131
if (IS_WINDOWS) {
132
String forward = pattern.replace('\\', '/');
133
String back = pattern.replace('/', '\\');
134
if (pattern.charAt(1) == ':') {
135
String forwardUpper = String.valueOf(pattern.charAt(0)).toUpperCase() + forward.substring(1);
136
String forwardLower = String.valueOf(pattern.charAt(0)).toLowerCase() + forward.substring(1);
137
String backUpper = String.valueOf(pattern.charAt(0)).toUpperCase() + back.substring(1);
138
String backLower = String.valueOf(pattern.charAt(0)).toLowerCase() + back.substring(1);
139
searchPatterns.add(forwardUpper.getBytes());
140
searchPatterns.add(forwardLower.getBytes());
141
searchPatterns.add(backUpper.getBytes());
142
searchPatterns.add(backLower.getBytes());
143
} else {
144
searchPatterns.add(forward.getBytes());
145
searchPatterns.add(back.getBytes());
146
}
147
} else {
148
searchPatterns.add(pattern.getBytes());
149
}
150
}
151
152
private void scanFiles(Path root, List<byte[]> searchPatterns) throws IOException {
153
Files.walkFileTree(root, new SimpleFileVisitor<>() {
154
@Override
155
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
156
String dirName = dir.toString();
157
if (dirName.endsWith(".dSYM")) {
158
return FileVisitResult.SKIP_SUBTREE;
159
}
160
return super.preVisitDirectory(dir, attrs);
161
}
162
163
@Override
164
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
165
String fileName = file.toString();
166
if (Files.isSymbolicLink(file)) {
167
return super.visitFile(file, attrs);
168
} else if ((fileName.endsWith(".debuginfo") && !IS_LINUX) || fileName.endsWith(".pdb")) {
169
// Do nothing
170
} else if (fileName.endsWith(".zip")) {
171
scanZipFile(file, searchPatterns);
172
} else {
173
scanFile(file, searchPatterns);
174
}
175
return super.visitFile(file, attrs);
176
}
177
});
178
}
179
180
private void scanFile(Path file, List<byte[]> searchPatterns) throws IOException {
181
List<String> matches = scanBytes(Files.readAllBytes(file), searchPatterns);
182
if (matches.size() > 0) {
183
matchFound = true;
184
System.out.println(file + ":");
185
for (String match : matches) {
186
System.out.println(match);
187
}
188
System.out.println();
189
}
190
}
191
192
private void scanZipFile(Path zipFile, List<byte[]> searchPatterns) throws IOException {
193
try (ZipInputStream zipInputStream = new ZipInputStream(Files.newInputStream(zipFile))) {
194
ZipEntry zipEntry;
195
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
196
List<String> matches = scanBytes(zipInputStream.readAllBytes(), searchPatterns);
197
if (matches.size() > 0) {
198
matchFound = true;
199
System.out.println(zipFile + ", " + zipEntry.getName() + ":");
200
for (String match : matches) {
201
System.out.println(match);
202
}
203
System.out.println();
204
}
205
}
206
}
207
}
208
209
private List<String> scanBytes(byte[] data, List<byte[]> searchPatterns) {
210
List<String> matches = new ArrayList<>();
211
for (int i = 0; i < data.length; i++) {
212
for (byte[] searchPattern : searchPatterns) {
213
boolean found = true;
214
for (int j = 0; j < searchPattern.length; j++) {
215
if ((i + j >= data.length || data[i + j] != searchPattern[j])) {
216
found = false;
217
break;
218
}
219
}
220
if (found) {
221
matches.add(new String(data, charsStart(data, i), charsOffset(data, i, searchPattern.length)));
222
// No need to search the same string for multiple patterns
223
break;
224
}
225
}
226
}
227
return matches;
228
}
229
230
private int charsStart(byte[] data, int startIndex) {
231
int index = startIndex;
232
while (--index > 0) {
233
byte datum = data[index];
234
if (datum < 32 || datum > 126) {
235
break;
236
}
237
}
238
return index + 1;
239
}
240
241
private int charsOffset(byte[] data, int startIndex, int startOffset) {
242
int offset = startOffset;
243
while (startIndex + ++offset < data.length) {
244
byte datum = data[startIndex + offset];
245
if (datum < 32 || datum > 126) {
246
break;
247
}
248
}
249
return offset;
250
}
251
}
252
253