OpenGL segmentation fault on classic core22 snap

I’m currently building a classic snap that has use of OpenGL. However, even after packaging the required driver as mentioned on this thread, the application still ends up with a segmentation fault.

The snap was able to run “fine” on Ubuntu 22.04 once mentioned dependencies were included, but running on any other system caused the application to have a segmentation fault.

After some investigation, the segfault seems to come from this file /snap/tess/x1/usr/lib/x86_64-linux-gnu/dri/kms_swrast_dri.so. Running ldd on it shows that all dependencies are correctly resolved, either from the snap itself or from the core22 base snap. Additionally, none of the libraries used by the application appear to be loaded from the host.

Here is the snapcraft.yaml:

base: core22
name: tess
version: 1.0.0
title: Tess
summary: Lorem ipsum
description: |
    Lorem ipsum
grade: stable
confinement: classic

parts:
    tess:
        plugin: rust
        source: .
        rust-path:
            - src-tauri
        build-snaps:
            - node/20/stable
        build-packages:
            - libglib2.0-dev
            - libgtk-3-dev
            - libjavascriptcoregtk-4.1-dev
            - libsoup-3.0-dev
            - libwebkit2gtk-4.1-dev
        stage-packages:
            - libwebkit2gtk-4.1-0
            - libibus-1.0-5
            - ibus-gtk3
            - libgtk-3-0
            - libgtk-3-bin
            - libltdl7
            - libcanberra-gtk3-module
            - libcanberra0
            - gvfs
            - gvfs-daemons
            - gvfs-libs
            - libcurl3-gnutls
        stage:
            - -usr/lib/${SNAPCRAFT_ARCH_TRIPLET}/dri
        build-attributes:
            - enable-patchelf
        override-build: |
            npm install
            craftctl default
        override-stage: |
            craftctl default
            mkdir -p $SNAPCRAFT_PRIME/usr/lib/x86_64-linux-gnu/gtk-3.0/3.0.0/
            $SNAPCRAFT_STAGE/usr/lib/x86_64-linux-gnu/libgtk-3-0/gtk-query-immodules-3.0 > $SNAPCRAFT_PRIME/usr/lib/x86_64-linux-gnu/gtk-3.0/3.0.0/immodules.cache
            sed -i "s|/usr/lib|/snap/tess/current/usr/lib|g" $SNAPCRAFT_PRIME/usr/lib/x86_64-linux-gnu/gtk-3.0/3.0.0/immodules.cache
            find "$SNAPCRAFT_STAGE/usr/lib" -name 'libwebkit*' -exec sed -i -e "s|/usr/lib/x86_64-linux-gnu/webkit2gtk-4.1|/snap/tess/current/webkit2gtk-4.1/./././|g" '{}' \;
            cp -a "$SNAPCRAFT_STAGE/usr/lib/x86_64-linux-gnu/webkit2gtk-4.1" "$SNAPCRAFT_PRIME/"
    mesa-patchelf:
        plugin: nil
        stage-packages:
            - libgl1-mesa-dri            
            - freeglut3-dev
            - libglu1-mesa
        build-attributes:
           - enable-patchelf
        stage:
            - -usr/lib/${SNAPCRAFT_ARCH_TRIPLET}/dri

    mesa-no-patchelf:
        plugin: nil
        stage-packages:
            - libgl1-mesa-dri
            - freeglut3-dev
            - libglu1-mesa
        stage:
            - usr/lib/${SNAPCRAFT_ARCH_TRIPLET}/dri

apps:
    tess:
        command: bin/tess
        environment:
            LD_LIBRARY_PATH: $SNAP/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu:$SNAP/usr/lib/x86_64-linux-gnu/webkit2gtk-4.1:$LD_LIBRARY_PATH
            GIO_MODULE_DIR: $SNAP/usr/lib/x86_64-linux-gnu/gio/modules
            GTK_IM_MODULE_FILE: $SNAP/usr/lib/x86_64-linux-gnu/gtk-3.0/3.0.0/immodules.cache
            __EGL_VENDOR_LIBRARY_DIRS: $SNAP/etc/glvnd/egl_vendor.d:$SNAP/usr/share/glvnd/egl_vendor.d
            LIBGL_DRIVERS_PATH: $SNAP/usr/lib/${SNAPCRAFT_ARCH_TRIPLET}/dri

After further investigation, I was able to reproduce the issue on a fresh installation of Ubuntu 22.04 (in a VM). This seems odd, since the binary runs perfectly fine outside the snap on the same machine.

I think I’m missing something in my snap configuration, but I can’t pinpoint what.

I advise you to do a deep cleaning otherwise you will curse like me for many nights. Moreover please enable the --verbosity-debug, it helps alot.

Full clean

sudo snapcraft clean --use-lxd --verbosity debug

Pack your application

sudo snapcraft pack --verbosity debug;

Remove all from your old snap

sudo snap remove --purge YOU_SNAP_NAME;

Remove the remaining old stuff if exists

sudo rm -rf /snap/YOU_SNAP_NAME && rm -rf ~/snap/YOU_SNAP_NAME 

Install and execute the new snap

sudo snap install YOU_SNAP_NAME_*_*.snap --devmode && YOU_SNAP_NAME

Hope it helps.