NixOS / nixpkgs

Nix Packages collection & NixOS
MIT License
17.99k stars 14.01k forks source link

KOReader segmentation fault #316621

Open Mr-Andersen opened 4 months ago

Mr-Andersen commented 4 months ago

Describe the bug

KOReader fails with segmentation fault on every startup.

Steps To Reproduce

Steps to reproduce the behavior:

  1. nix run github:NixOS/nixpkgs/805a384895c696f802a9bf5bf4720f37385df547#koreader

Expected behavior

KOReader starts up.

Logs

---------------------------------------------
                launching...
  _  _____  ____                _
 | |/ / _ \|  _ \ ___  __ _  __| | ___ _ __
 | ' / | | | |_) / _ \/ _` |/ _` |/ _ \ '__|
 | . \ |_| |  _ <  __/ (_| | (_| |  __/ |
 |_|\_\___/|_| \_\___|\__,_|\__,_|\___|_|

 It's a scroll... It's a codex... It's KOReader!

 [*] Current time: 06/02/24-11:50:33
 [*] Version: v2024.03.1

ffi.load: libs/libutf8proc.so.3
ffi.load: blitbuffer
ffi.load (assisted searchpath): ./libs/libblitbuffer.so
ffi.load: SDL2
ffi.load: SDL2
Starting SDL in /nix/store/1fgwcl6nws60jic6pq1whiacl3sa28v5-luajit-2.1.1693350652/bin/
ffi.load: libs/liblodepng.so
06/02/24-11:50:33 INFO  initializing for device Linux
06/02/24-11:50:33 INFO  framebuffer resolution: {
  h = 800,
  w = 600
} --[[table: 0x7f81db4b0160]]
ffi.load: libs/libmupdf.so
ffi.load: libs/libwrap-mupdf.so
ffi.load: sqlite3
ffi.load (assisted searchpath): ./libs/libsqlite3.so
06/02/24-11:50:33 INFO  Performing one-time migration for 20191129
06/02/24-11:50:33 INFO  Performing one-time migration for 20200421
ffi.load: libs/libfreetype.so.6
ffi.load: libs/libharfbuzz.so.0
ffi.load: libs/libzstd.so.1
06/02/24-11:50:33 INFO  /home/aka_dude/.config/koreader/cache/fontlist/fontinfo.dat fopen: No such file or directory -> initializing it
/nix/store/b0819s02jmirc82lzwwd5isdxl23r0rs-koreader-2024.03.1/bin/.koreader-wrapped: line 25: 14871 Segmentation fault      (core dumped) ./reader.lua "${ARGS}"

Notify maintainers

@contrun @neonfuz

Metadata

Please run nix-shell -p nix-info --run "nix-info -m" and paste the result.

aka_dude@this-system ~> nix-shell -p nix-info --run "nix-info -m"
 - system: `"x86_64-linux"`
 - host os: `Linux 6.6.32, NixOS, 24.05 (Uakari), 24.05.20240531.805a384`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.18.2`
 - channels(root): `""`
 - channels(aka_dude): `""`
 - nixpkgs: `/nix/store/i1aw9jjgxcvyd642s12kw3iasmarwd42-source`
anpandey commented 3 months ago

Using this overlay to downgrade back to 2023.04 worked for me to fix the segfault. Note that you might have to delete ~/.config/koreader/cache/fontlist/:

(self: super: {
      koreader = super.koreader.overrideAttrs (old: let
        luajit_lua52 = super.luajit.override { enable52Compat = true; };
        src_repo = super.fetchFromGitHub {
          repo = "koreader";
          owner = "koreader";
          rev = "v2023.04";
          fetchSubmodules = true;
          hash = "sha256-c3j6hs0W0H2jDg6JVfU6ov7r7kucbqrQqf9PAvYBcJ0=";
        };
        src = super.fetchurl {
          url = "https://github.com/koreader/koreader/releases/download/v2023.04/koreader-2023.04-amd64.deb";
          hash = "sha256-tRUeRB1+UcWT49dchN0YDvd0L5n1YRdtMSFc8yy6m5o=";
        };
        in {
        version = "2023.04";
        unpackCmd = "dpkg-deb -x ${src} .";
        installPhase = ''
          mkdir -p $out
          cp -R usr/* $out/
          ln -sf ${luajit_lua52}/bin/luajit $out/lib/koreader/luajit
          ln -sf ${super.sdcv}/bin/sdcv $out/lib/koreader/sdcv
          ln -sf ${super.gnutar}/bin/tar $out/lib/koreader/tar
          find ${src_repo}/resources/fonts -type d -execdir cp -r '{}' $out/lib/koreader/fonts \;
          find $out -xtype l -print -delete
          wrapProgram $out/bin/koreader --prefix LD_LIBRARY_PATH : ${
            lib.makeLibraryPath [ super.gtk3-x11 super.SDL2 super.glib ]
          }
         '';
      });
    })
anpandey commented 3 months ago

The stack trace isn't very helpful because of the Lua runtime:

Stack trace of thread 519576:
#0  0x00007fb2649d45be free (libc.so.6 + 0x9e5be)
#1  0x00007fb23d02e00a ZSTD_compress (/nix/store/4hscmwibkgq7lpg26sdz19mhw12l74bh-koreader-2024.04/lib/koreader/libs/libzstd.so.1 + 0x2e00a)
#2  0x0000000000471009 n/a (luajit-2.1.1713773202 + 0x71009)
#3  0x000000000044f453 n/a (luajit-2.1.1713773202 + 0x4f453)
#4  0x000000000044fc6d n/a (luajit-2.1.1713773202 + 0x4fc6d)
#5  0x000000000046eb7b n/a (luajit-2.1.1713773202 + 0x6eb7b)
#6  0x000000000045fcbe n/a (luajit-2.1.1713773202 + 0x5fcbe)
#7  0x000000000046eb7b n/a (luajit-2.1.1713773202 + 0x6eb7b)
#8  0x000000000045fcbe n/a (luajit-2.1.1713773202 + 0x5fcbe)
#9  0x000000000046eb7b n/a (luajit-2.1.1713773202 + 0x6eb7b)
#10 0x000000000045fcbe n/a (luajit-2.1.1713773202 + 0x5fcbe)
#11 0x000000000046eb7b n/a (luajit-2.1.1713773202 + 0x6eb7b)
#12 0x000000000045fcbe n/a (luajit-2.1.1713773202 + 0x5fcbe)
#13 0x000000000046eb7b n/a (luajit-2.1.1713773202 + 0x6eb7b)
#14 0x000000000046187c n/a (luajit-2.1.1713773202 + 0x6187c)
#15 0x000000000046eb7b n/a (luajit-2.1.1713773202 + 0x6eb7b)
#16 0x000000000046187c n/a (luajit-2.1.1713773202 + 0x6187c)
#17 0x000000000046eb7b n/a (luajit-2.1.1713773202 + 0x6eb7b)
#18 0x00000000004600fc lua_pcall (luajit-2.1.1713773202 + 0x600fc)
#19 0x000000000040587b n/a (luajit-2.1.1713773202 + 0x587b)
#20 0x0000000000406aaf n/a (luajit-2.1.1713773202 + 0x6aaf)
#21 0x000000000046eb7b n/a (luajit-2.1.1713773202 + 0x6eb7b)
#22 0x000000000046027d lua_cpcall (luajit-2.1.1713773202 + 0x6027d)
#23 0x00000000004056da main (luajit-2.1.1713773202 + 0x56da)
#24 0x00007fb26496010e __libc_start_call_main (libc.so.6 + 0x2a10e)
#25 0x00007fb2649601c9 __libc_start_main@@GLIBC_2.34 (libc.so.6 + 0x2a1c9)
#26 0x0000000000405755 _start (luajit-2.1.1713773202 + 0x5755)

But at least we know it's failing at the built-in libzstd call with ~/.config/koreader/cache/fontlist/fontinfo.dat now.

jys1670 commented 3 months ago

I managed to get 2024.04 (unstable channel) and the latest 2024.07 running by replacing the built-in libzstd with nixpkgs one:

{
  lib,
  stdenv,
  fetchurl,
  makeWrapper,
  fetchFromGitHub,
  dpkg,
  glib,
  gnutar,
  gtk3-x11,
  luajit,
  sdcv,
  SDL2,
  zstd,
}: 
let
  luajit_lua52 = luajit.override {enable52Compat = true;};
in
  stdenv.mkDerivation rec {
    pname = "koreader";
    version = "2024.07";

    src =
      if stdenv.isAarch64
      then
        fetchurl {
          url = "https://github.com/koreader/koreader/releases/download/v${version}/koreader-${version}-arm64.deb";
          hash = "sha256-KrkY1lTwq8mIomUUCQ9KvfZqinJ74Y86fkPexsFiOPg=";
        }
      else
        fetchurl {
          url = "https://github.com/koreader/koreader/releases/download/v${version}/koreader-${version}-amd64.deb";
          hash = "sha256-Xs9Ci5a3FntRwb6kkn1dCGh62pjj/TF154IG1uiDpRQ=";
        };

    src_repo = fetchFromGitHub {
      repo = "koreader";
      owner = "koreader";
      rev = "v${version}";
      fetchSubmodules = true;
      hash = "sha256-IBGyjw9mzSOBY4GpeHab+aUYh1+Sb0ErfQsc2goO3cM=";
    };

    sourceRoot = ".";
    nativeBuildInputs = [makeWrapper dpkg];
    buildInputs = [
      glib
      gnutar
      gtk3-x11
      luajit_lua52
      sdcv
      SDL2
      zstd.out
    ];
    unpackCmd = "dpkg-deb -x ${src} .";

    dontConfigure = true;
    dontBuild = true;

    installPhase = ''
      mkdir -p $out
      cp -R usr/* $out/
      ln -sf ${luajit_lua52}/bin/luajit $out/lib/koreader/luajit
      ln -sf ${sdcv}/bin/sdcv $out/lib/koreader/sdcv
      ln -sf ${gnutar}/bin/tar $out/lib/koreader/tar
      ln -sf ${zstd.out}/lib/libzstd.so $out/lib/koreader/libs/libzstd.so.1
      find ${src_repo}/resources/fonts -type d -execdir cp -r '{}' $out/lib/koreader/fonts \;
      find $out -xtype l -print -delete
      wrapProgram $out/bin/koreader --prefix LD_LIBRARY_PATH : ${
        lib.makeLibraryPath [gtk3-x11 SDL2 glib stdenv.cc.cc.lib]
      }
    '';

    meta = with lib; {
      homepage = "https://github.com/koreader/koreader";
      changelog = "https://github.com/koreader/koreader/releases/tag/v${version}";
      description = "An ebook reader application supporting PDF, DjVu, EPUB, FB2 and many more formats, running on Cervantes, Kindle, Kobo, PocketBook and Android devices";
      mainProgram = "koreader";
      sourceProvenance = with sourceTypes; [binaryNativeCode];
      platforms = ["aarch64-linux" "x86_64-linux"];
      license = licenses.agpl3Only;
      maintainers = with maintainers; [contrun neonfuz];
    };
  }

However, this is really fragile and prone to breaking, so I won't open PR myself (but anyone is free to do so). Failed to allocate ZSTD decompression buffer error occurs during one time data migration to the latest version, so I was forced to do rm -rf ~/.config/koreader to get it running. Ideally, packages like this should be built from source, but koreader seems very complicated.

ClockGen commented 2 months ago

Note that koreader also performs migration on the first run, it's not possible to just install it for the first time and use it right now. @jys1670 can you open PR please?