dbmdz/imageio-jnr

SIGSEGV on Debian

Closed this issue ยท 11 comments

eroux commented

We're having trouble using the imageio-turbojpeg package under Debian. We get a SIGSEGV with the following code:

    public void iiiftext() throws IOException {
        InputStream input = IIIFImageTest.class.getClassLoader().getResourceAsStream("08860041.tif");
        ImageInputStream iis;
        try {
          iis = ImageIO.createImageInputStream(input);
        } catch (IOException e) {
          e.printStackTrace();
          return;
        }
        ImageReader reader = ImageIO.getImageReaders(iis).next();
        System.out.println("using reader "+(reader.getClass()));
        reader.setInput(iis);
        ImageReadParam readParam = reader.getDefaultReadParam();
        BufferedImage outImg = reader.read(0, readParam);
        ImageWriter writer = ImageIO.getImageWritersByMIMEType("image/jpeg").next();
        System.out.println("using writer "+(writer.getClass()));
        OutputStream os = new FileOutputStream(new File("/tmp/imgresult.jpg"));
        ImageOutputStream ios = ImageIO.createImageOutputStream(os);
        writer.setOutput(ios);
        System.out.println("debug step 1");
        writer.write(outImg);
        writer.dispose();
        ios.flush();
    }

which gives, in short:

using reader class com.twelvemonkeys.imageio.plugins.tiff.TIFFImageReader
using writer class de.digitalcollections.turbojpeg.imageio.TurboJpegImageWriter
debug step 1
#
# A fatal error has been detected by the Java Runtime Environment:
#
#  SIGSEGV (0xb) at pc=0x00007f6c29560658, pid=12121, tid=0x00007f6c47bfd700
#
# Problematic frame:
# C  [libturbojpeg.so.0.1.0+0x4658]

The core dump and detailed log can be found here. The TIF file we tested with can be found here. The pom used for the test can be found here. You can run the test directly in this branch.

The test platform is a Debian sid. The libturbojpeg0 package is installed with Version: 1:1.5.2-2+b1.

Java is:

java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
eroux commented

same on another box with a Debian stable:

coredump.zip

with same version of libturbojpeg0 and Java:

openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-8u181-b13-2~deb9u1-b13)

Thank you for the great writeup! I did some initial digging, and it seems to be related to our handling of colorspaces:

#9  grayscale_convert (cinfo=<optimized out>, input_buf=0x7f6cecb148b8, output_buf=0x7f6cecb14e20, output_row=1, num_rows=0) at jccolor.c:449
#10 0x00007f6c7398fc41 in pre_process_data (cinfo=0x7f6cecafcd00, input_buf=0x7f6cecb14320, in_row_ctr=0x7f6cf0695dd4, in_rows_avail=318, output_buf=0x7f6cecb15150, out_row_group_ctr=0x7f6cecb15144, out_row_groups_avail=8) at jcprepct.c:148
#11 0x00007f6c7398c2b6 in process_data_simple_main (cinfo=0x7f6cecafcd00, input_buf=0x7f6cecb14320, in_row_ctr=0x7f6cf0695dd4, in_rows_avail=318) at jcmainct.c:87
#12 0x00007f6c7397f98a in jpeg_write_scanlines (cinfo=cinfo@entry=0x7f6cecafcd00, scanlines=0x7f6cecb14320, num_lines=<optimized out>) at jcapistd.c:109
#13 0x00007f6c739afb15 in tjCompress2 (handle=0x7f6cecafcd00, srcBuf=0x7f6cecaff9e0 '\377' <repeats 200 times>..., width=2115, pitch=<optimized out>, height=318, pixelFormat=6, jpegBuf=0x7f6cf0695fc0, jpegSize=0x7f6cf0695fa0, jpegSubsamp=3, jpegQual=85, flags=0) at turbojpeg.c:821

Looks like we have a bug in our handling of grayscale images. I'll try to investigate some more next week.

eroux commented

Glad to help! Thanks a lot for looking at this, this will be a great help for us!

eroux commented

(as a note: the image we test with is bitonal I think, not grayscale, I don't know if that changes anything)

Just so you know: I was able to reproduce the issue on Mac OS resulting in a slightly more verbose message regarding "problematic frame' and grayscale convert:

JRE version: Java(TM) SE Runtime Environment (8.0_201-b09) (build 1.8.0_201-b09)
Java VM: Java HotSpot(TM) 64-Bit Server VM (25.201-b09 mixed mode bsd-amd64 compressed oops)
Problematic frame:
C  [libturbojpeg.dylib+0x2640]  grayscale_convert+0x26

I just pushed a fix for this, your test image is now encoded correctly into a Grayscale JPEG. Can you test it on your side?

eroux commented

Running mvn test, checkstyle is unhappy with:

[INFO] 
[INFO] --- maven-checkstyle-plugin:3.0.0:check (validate) @ imageio-turbojpeg ---
[INFO] Starting audit...
[ERROR] /home/eroux/BUDA/softs/imageio-jnr/imageio-turbojpeg/src/main/java/de/digitalcollections/turbojpeg/TurboJpeg.java:169:19: WhitespaceAround: '=' is not preceded with whitespace. [WhitespaceAround]
[ERROR] /home/eroux/BUDA/softs/imageio-jnr/imageio-turbojpeg/src/main/java/de/digitalcollections/turbojpeg/TurboJpeg.java:169:20: WhitespaceAround: '=' is not followed by whitespace. Empty blocks may only be represented as {} when not part of a multi-block statement (4.1.3) [WhitespaceAround]
[WARN] /home/eroux/BUDA/softs/imageio-jnr/imageio-turbojpeg/src/main/java/de/digitalcollections/turbojpeg/imageio/TurboJpegImageReadParam.java:19:3: Missing a Javadoc comment. [JavadocMethod]
Audit done.
eroux commented

other than that the test is passing on my machine, thanks a lot! are you planning a new release with this patch?

Yes, I'll grab a bite to eat and then 0.3.1 should be out early this afternoon :-)
Thanks for the help!

eroux commented

great!