Skip to content

8341735: Rewrite the build/AbsPathsInImage.java test to not load the entire file at once #26030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 23 additions & 10 deletions test/jdk/build/AbsPathsInImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
* @requires !vm.debug
* @comment ASAN keeps the 'unwanted' paths in the binaries because of its build options
* @requires !vm.asan
* @run main/othervm -Xmx900m AbsPathsInImage
* @run main AbsPathsInImage
*/
public class AbsPathsInImage {

Expand All @@ -51,6 +51,7 @@ public class AbsPathsInImage {
public static final String DIR_PROPERTY = "jdk.test.build.AbsPathsInImage.dir";
private static final boolean IS_WINDOWS = System.getProperty("os.name").toLowerCase().contains("windows");
private static final boolean IS_LINUX = System.getProperty("os.name").toLowerCase().contains("linux");
private static final int DEFAULT_BUFFER_SIZE = 8192;

private boolean matchFound = false;

Expand Down Expand Up @@ -180,7 +181,14 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
}

private void scanFile(Path file, List<byte[]> searchPatterns) throws IOException {
List<String> matches = scanBytes(Files.readAllBytes(file), searchPatterns);
int bytesRead;
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
List<String> matches = new ArrayList<>();
try (InputStream inputStream = Files.newInputStream(file)) {
while ((bytesRead = inputStream.read(buffer)) != -1) {
matches.addAll(scanBytes(buffer, bytesRead, searchPatterns));
}
}
if (matches.size() > 0) {
matchFound = true;
System.out.println(file + ":");
Expand All @@ -192,10 +200,15 @@ private void scanFile(Path file, List<byte[]> searchPatterns) throws IOException
}

private void scanZipFile(Path zipFile, List<byte[]> searchPatterns) throws IOException {
ZipEntry zipEntry;
int bytesRead;
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
try (ZipInputStream zipInputStream = new ZipInputStream(Files.newInputStream(zipFile))) {
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
List<String> matches = scanBytes(zipInputStream.readAllBytes(), searchPatterns);
List<String> matches = new ArrayList<>();
while ((bytesRead = zipInputStream.read(buffer)) != -1) {
matches.addAll(scanBytes(buffer, bytesRead, searchPatterns));
}
if (matches.size() > 0) {
matchFound = true;
System.out.println(zipFile + ", " + zipEntry.getName() + ":");
Expand All @@ -208,19 +221,19 @@ private void scanZipFile(Path zipFile, List<byte[]> searchPatterns) throws IOExc
}
}

private List<String> scanBytes(byte[] data, List<byte[]> searchPatterns) {
private List<String> scanBytes(byte[] data, int dataLength, List<byte[]> searchPatterns) {
List<String> matches = new ArrayList<>();
for (int i = 0; i < data.length; i++) {
for (int i = 0; i < dataLength; i++) {
for (byte[] searchPattern : searchPatterns) {
boolean found = true;
for (int j = 0; j < searchPattern.length; j++) {
if ((i + j >= data.length || data[i + j] != searchPattern[j])) {
if ((i + j >= dataLength || data[i + j] != searchPattern[j])) {
found = false;
break;
}
}
if (found) {
matches.add(new String(data, charsStart(data, i), charsOffset(data, i, searchPattern.length)));
matches.add(new String(data, charsStart(data, i), charsOffset(data, dataLength, i, searchPattern.length)));
// No need to search the same string for multiple patterns
break;
}
Expand All @@ -240,9 +253,9 @@ private int charsStart(byte[] data, int startIndex) {
return index + 1;
}

private int charsOffset(byte[] data, int startIndex, int startOffset) {
private int charsOffset(byte[] data, int dataLength, int startIndex, int startOffset) {
int offset = startOffset;
while (startIndex + ++offset < data.length) {
while (startIndex + ++offset < dataLength) {
byte datum = data[startIndex + offset];
if (datum < 32 || datum > 126) {
break;
Expand Down