Skip to content
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

ImageMagick: Converting from PDFs is broken with imagemagickBig (--with-gslib) #55118

Closed
erikarvstedt opened this issue Feb 3, 2019 · 1 comment

Comments

@erikarvstedt
Copy link
Member

erikarvstedt commented Feb 3, 2019

This bug is specific to NixOS and concerns all versions of imageMagickBig.

Reproduce

showBug='
  # Create simple demo PDF
  convert -size 40x20 xc:white -font "DejaVu-Sans" -annotate +0+10 "demo" a.pdf
  # This fails with output "Aborted"
  convert a.pdf b.png
'
nix run -f channel:nixos-unstable imagemagick7Big -c bash -c "
  tmpDir=$(mktemp -d)
  trap \"rm -rf $tmpDir\" EXIT
  cd $tmpDir
  $showBug
"

The error also appears with ImageMagic 6 (imagemagickBig).

Running convert -debug all a.pdf b.png gives more details (full debug output).

This bug doesn't appear on other Linux distros that provide --with-gslib ImageMagick distributions, like Alpine or Arch (imagemagick-full).

Workaround

Use imagemagick instead of imagemagickBig while ghostscript is in PATH. This way, the gs binary is used instead of the broken gslib.

Debug

GDB reveals a segfault triggered by lcms2, via ghostscript:

read -d '' src <<'EOF'
let
  pkgs17_09 = builtins.fetchTarball {
    url = https://github.com/NixOS/nixpkgs-channels/archive/14f9ee66e63077539252f8b4550049381a082518.tar.gz;
    sha256 = "1wn7nmb1cqfk2j91l3rwc6yhimfkzxprb8wknw5wi57yhq9m6lv1";
  };
  pkgs18_03 = builtins.fetchTarball {
    url = https://github.com/NixOS/nixpkgs-channels/archive/138f2cc707d7ee13d93c86db3285460e244c402c.tar.gz;
    sha256 = "0h49j1cbnccqx996x80z7na9p7slnj9liz646s73s55am8wc9q8q";
  };
  pkgs18_09 = builtins.fetchTarball {
    url = https://github.com/NixOS/nixpkgs-channels/archive/168cbb39691cca2822ce1fdb3e8c0183af5c6d0d.tar.gz;
    sha256 = "0fqasswfqrz2rbag9bz17j8y7615s0p9l23cw4sk2f384gk0zf6c";
  };
  pkgsUnstable = builtins.fetchTarball {
    url = https://github.com/NixOS/nixpkgs-channels/archive/929cc78363e6878e044556bd291382eab37bcbed.tar.gz;
    sha256 = "1ghzjk6fq8f2aimp23p45awnfzlcqc20sf7p1xp98myis1sqniwb";
  };
# pkgsSrc = pkgs17_09;
# pkgsSrc = pkgs18_03;
# pkgsSrc = pkgs18_09;
  pkgsSrc = pkgsUnstable;
  pkgs = import pkgsSrc {};
in
with (import pkgsUnstable {});
mkShell rec {
  buildInputs = [ gdb ];
  im = pkgs.imagemagick7Big;
  gs = pkgs.ghostscript;
  imDebug = enableDebugging (im.override {
    ghostscript = enableDebugging gs;
  });
  imSrc = im.src;
  gsSrc = runCommand "gsSrc" {} "mkdir $out; tar xfJ ${gs.src} --strip-components=1 -C $out";
}
EOF
nix-shell -E "$src" --run '
# Create demo PDF
$imDebug/bin/convert -size 40x20 xc:white -font "DejaVu-Sans" -annotate +0+10 "demo" /tmp/a.pdf

# Start debugger, show backtrace, quit
gdb -ex=r -ex=bt  -ex="set confirm off" -ex=quit --directory=$imSrc:$gsSrc --args $imDebug/bin/convert /tmp/a.pdf /tmp/a.png
'

Full backtrace

The reason for the exception shown in convert's output is that ShredFile tries to delete a temp file which has been previously deleted by an exception handler triggered by the segfault.

Running the above script with older nixpkgs versions yields:

  • 17.09: Works without errors
  • 18.03: SIGSEGV in __strlen_sse2 (libc), via liblcms2
    or alternatively: SIGSEV in pthread_mutex_destroy (libpthread), via liblcms2, backtrace
  • 18.09: SIGSEGV in __memmove_ssse3 (libc), via liblcms2, backtrace
  • unstable (as demonstrated above): SIGSEGV in __memmove_ssse3 or __memmove_avx_unaligned_erms (libc), via liblcms2, backtrace

cc: @the-kenny

@erikarvstedt erikarvstedt changed the title ImageMagick: Error when converting from PDFs ImageMagick: Converting from PDFs is universally broken Feb 4, 2019
@erikarvstedt erikarvstedt changed the title ImageMagick: Converting from PDFs is universally broken ImageMagick: Converting from PDFs is broken with imagemagickBig (--with-gslib) Feb 12, 2019
@delroth
Copy link
Contributor

delroth commented Mar 10, 2019

#55634 broke building zbar and thus electrum on my system. I don't think this issue really warranted breaking the build of dependencies just because an optional feature doesn't work...

So I started looking into the root cause of this. My understanding is that ghostscript has its own fork of lcms2 inside its codebase ("lcms2mt" directory). We build against it, as shown by build flags at compile time: -DWHICH_CMS="lcms2mt". But then at runtime libgs ends up using liblcms2.so from the lcms2 derivation instead. This is bound to cause ABI problems which could manifest in SEGV.

The easiest fix is probably to do like Arch is currently doing: using systemwide lcms2 and removing Ghostscript's fork. I'll give it a shot.

andersk added a commit to andersk/nixpkgs that referenced this issue Mar 26, 2019
imagemagickBig and imagemagick7Big were marked broken in NixOS#55634 due
to NixOS#55118.  NixOS#57313 fixed the bug but reverted the broken bit only on
imagemagickBig.  Revert it on imagemagick7Big too.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
vdemeester pushed a commit to vdemeester/nixpkgs that referenced this issue Mar 27, 2019
imagemagickBig and imagemagick7Big were marked broken in NixOS#55634 due
to NixOS#55118.  NixOS#57313 fixed the bug but reverted the broken bit only on
imagemagickBig.  Revert it on imagemagick7Big too.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants