AdoptOpenJDK / jitwatch

Log analyser / visualiser for Java HotSpot JIT compiler. Inspect inlining decisions, hot methods, bytecode, and assembly. View results in the JavaFX user interface.
Other
3.06k stars 437 forks source link

java.util.zip generates corrupted zip files in openjdk11-openj9:jdk-11.0.1.13-alpine-slim #293

Closed blindpirate closed 5 years ago

blindpirate commented 5 years ago

Context

Recently someone reported a bug in Gradle issue tracker, after some debugging, it seems like a bug in adoptopenjdk/openjdk11-openj9:jdk-11.0.1.13-alpine-slim docker image.

Step to reproduce

zipbug.zip

There're two files in the directory. If I run the simple Java code to generate a zip in adoptopenjdk/openjdk11-openj9:jdk-11.0.1.13-alpine-slim container, it's corrupted (all tools I have tried complain it's corrupted), but it works on jdk-11.0.1_13_openj9 Mac and Ubuntu.

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Main {
    private static final int BUFFER_SIZE = 8192;

    public static void main(String[] args) throws IOException {
    // write your code here
        ZipOutputStream outputStream = openJarOutputStream(new File("output.zip"));
        writeEntry(outputStream, "org/gradle/invocation/DefaultGradle$2.class", Files.readAllBytes(new File("DefaultGradle$2.class").toPath()));
        outputStream.finish();
        outputStream.close();
    }

    private static void writeEntry(ZipOutputStream outputStream, String name, byte[] content) throws IOException {
        ZipEntry zipEntry = newZipEntryWithFixedTime(name);
        outputStream.putNextEntry(zipEntry);
        outputStream.write(content);
        outputStream.closeEntry();
    }

    private static ZipEntry newZipEntryWithFixedTime(String name) {
        ZipEntry entry = new ZipEntry(name);
        entry.setTime(new GregorianCalendar(1980, Calendar.FEBRUARY, 1, 0, 0, 0).getTimeInMillis());
        return entry;
    }

    private static ZipOutputStream openJarOutputStream(File outputJar) {
        try {
            ZipOutputStream outputStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outputJar), BUFFER_SIZE));
            outputStream.setLevel(0);
            return outputStream;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

The console output:

# zhb @ bs-MacBook-Pro in ~/Projects/tmp/zipbug [14:23:53]
$ docker run -it -v `pwd`:/zipbug adoptopenjdk/openjdk11-openj9:jdk-11.0.1.13-alpine-slim
/ # cd zipbug/
/zipbug # ls
DefaultGradle$2.class  Main.java
/zipbug # java -version
openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.1+13)
Eclipse OpenJ9 VM AdoptOpenJDK (build openj9-0.11.0, JRE 11 Linux amd64-64-Bit Compressed References 20181115_18 (JIT enabled, AOT enabled)
OpenJ9   - 090ff9dcd
OMR      - ea548a66
JCL      - d4455071ce based on jdk-11.0.1+13)
/zipbug # javac Main.java
/zipbug # java Main
/zipbug # ls -al
total 20
drwxr-xr-x    6 root     root           192 Dec  7 06:24 .
drwxr-xr-x    1 root     root          4096 Dec  7 06:24 ..
-rw-r--r--    1 root     root          1079 Dec  7 06:23 DefaultGradle$2.class
-rw-r--r--    1 root     root          1838 Dec  7 06:24 Main.class
-rw-r--r--    1 root     root          1669 Dec  7 06:23 Main.java
-rw-r--r--    1 root     root           718 Dec  7 06:24 output.zip
/zipbug # unzip output.zip -d .
Archive:  output.zip
  inflating: org/gradle/invocation/DefaultGradle$2.class
unzip: unexpected end of file
unzip: inflate error

On my Mac:

# zhb @ bs-MacBook-Pro in ~/Projects/tmp/zipbug [14:25:33] C:130
$ export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-11.0.1+13-openj9/Contents/Home

# zhb @ bs-MacBook-Pro in ~/Projects/tmp/zipbug [14:26:13]
$ ls
DefaultGradle$2.class Main.java

# zhb @ bs-MacBook-Pro in ~/Projects/tmp/zipbug [14:26:14]
$ javac Main.java

# zhb @ bs-MacBook-Pro in ~/Projects/tmp/zipbug [14:26:18]
$ java Main

# zhb @ bs-MacBook-Pro in ~/Projects/tmp/zipbug [14:26:22]
$ unzip output.zip -d .
Archive:  output.zip
  inflating: ./org/gradle/invocation/DefaultGradle$2.class
chriswhocodes commented 5 years ago

Hi @blindpirate is this related to JITWatch?

blindpirate commented 5 years ago

I'm not sure... actually I'm not aware of JITWatch, can you please provide some context here?

blindpirate commented 5 years ago

Ah...sorry I think I'm creating issue in the wrong issue tracker... sorry I'll find the right space.