Skip to content

fix: implement recursive dependency resolution#40

Merged
Zouuup merged 1 commit into
Zouuup:mainfrom
amartani:martani/fix-ldd-recursion
Oct 1, 2025
Merged

fix: implement recursive dependency resolution#40
Zouuup merged 1 commit into
Zouuup:mainfrom
amartani:martani/fix-ldd-recursion

Conversation

@amartani
Copy link
Copy Markdown
Contributor

@amartani amartani commented Sep 6, 2025

As noted in #38 (comment), the previous implementation of GetLibraryDependencies only performed a shallow scan of the binary's dependencies. This caused issues when a shared library itself had dependencies that were not directly listed in the main binary.

This commit refactors GetLibraryDependencies to use a work queue to perform a breadth-first search of the entire dependency tree. This ensures that all transitive dependencies are discovered.

A new test case, TestRecursiveDependencies, is added to verify the new behavior. This test is self-contained: it compiles a set of C source files with a nested dependency (test_binary -> libb.so -> liba.so) on the fly to ensure the recursive logic is working correctly. The test is skipped if gcc is not available in the environment.

The previous implementation of `GetLibraryDependencies` only performed a shallow scan of the binary's dependencies. This caused issues when a shared library itself had dependencies that were not directly listed in the main binary.

This commit refactors `GetLibraryDependencies` to use a work queue to perform a breadth-first search of the entire dependency tree. This ensures that all transitive dependencies are discovered.

A new test case, `TestRecursiveDependencies`, is added to verify the new behavior. This test is self-contained: it compiles a set of C source files with a nested dependency (`test_binary` -> `libb.so` -> `liba.so`) on the fly to ensure the recursive logic is working correctly. The test is skipped if `gcc` is not available in the environment.
@thefossguy
Copy link
Copy Markdown

Can confirm this works, thank you!

Following is the output without and with your patch:

$ ldd /nix/store/7ksl2hh26hsjgiwqpb1k2za4b7j7260n-qemu-user-10.0.3/bin/qemu-x86_64
        linux-vdso.so.1 (0x0000ffffbd539000)
        libgnutls.so.30 => /nix/store/nlrfi2xsm3whpqawwr2xn76nzc889p9z-gnutls-3.8.10/lib/libgnutls.so.30 (0x0000ffffbcfd0000)
        libm.so.6 => /nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/libm.so.6 (0x0000ffffbcf20000)
        libglib-2.0.so.0 => /nix/store/9h0qgsb2r5shr143q0bq82pzzagp2pc5-glib-2.84.3/lib/libglib-2.0.so.0 (0x0000ffffbcd90000)
        libgmodule-2.0.so.0 => /nix/store/9h0qgsb2r5shr143q0bq82pzzagp2pc5-glib-2.84.3/lib/libgmodule-2.0.so.0 (0x0000ffffbcd60000)
        libc.so.6 => /nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/libc.so.6 (0x0000ffffbcb80000)
        /nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/ld-linux-aarch64.so.1 (0x0000ffffbd4f0000)
        libz.so.1 => /nix/store/ig768bxaswpmyajrrldicxp6nlh454xg-zlib-1.3.1/lib/libz.so.1 (0x0000ffffbcb40000)
        libp11-kit.so.0 => /nix/store/f988n0dncjhg2cacm1ngp4cppxkr9c31-p11-kit-0.25.5/lib/libp11-kit.so.0 (0x0000ffffbc960000)
        libidn2.so.0 => /nix/store/anixmx00k0fhjilpd9j927as20k8yq82-libidn2-2.3.8/lib/libidn2.so.0 (0x0000ffffbc910000)
        libunistring.so.5 => /nix/store/87pbspfhvv6ldyp7xp3p1xb479lk8nh7-libunistring-1.3/lib/libunistring.so.5 (0x0000ffffbc700000)
        libdl.so.2 => /nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/libdl.so.2 (0x0000ffffbc6d0000)
        libtasn1.so.6 => /nix/store/1rm9g0wqj489qqjz3i6s6zcwf7cycdz8-libtasn1-4.20.0/lib/libtasn1.so.6 (0x0000ffffbc690000)
        libhogweed.so.6 => /nix/store/4p7f5lp3grnpdsfa79i2i9cl5vjx7kwb-nettle-3.10.2/lib/libhogweed.so.6 (0x0000ffffbc620000)
        libnettle.so.8 => /nix/store/4p7f5lp3grnpdsfa79i2i9cl5vjx7kwb-nettle-3.10.2/lib/libnettle.so.8 (0x0000ffffbc5a0000)
        libgmp.so.10 => /nix/store/nzy86wzv86lm2iw0cswj9mkq35bz5bw3-gmp-with-cxx-6.3.0/lib/libgmp.so.10 (0x0000ffffbc500000)
        libpcre2-8.so.0 => /nix/store/8vz5n20qwma5035m6s7fk05q5cv4g7r2-pcre2-10.44/lib/libpcre2-8.so.0 (0x0000ffffbc450000)
        libffi.so.8 => /nix/store/x93ml5v28dmb4sc4v6hyaz3b7iv5ygqj-libffi-3.5.1/lib/libffi.so.8 (0x0000ffffbc410000)

$ /nix/store/dfx77f1pygqnrrxslr5hh9k8sibb7lbi-landrun-0.1.15/bin/landrun --log-level debug --ldd --rox /nix/store/7ksl2hh26hsjgiwqpb1k2za4b7j7260n-qemu-user-10.0.3/bin/qemu-x86_64 /nix/store/7ksl2hh26hsjgiwqpb1k2za4b7j7260n-qemu-user-10.0.3/bin/qemu-x86_64
[landrun:debug] 2025/09/07 12:08:11 Added library paths: [/nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/ld-linux-aarch64.so.1 /nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/libm.so.6 /nix/store/9h0qgsb2r5shr143q0bq82pzzagp2pc5-glib-2.84.3/lib/libglib-2.0.so.0 /nix/store/9h0qgsb2r5shr143q0bq82pzzagp2pc5-glib-2.84.3/lib/libgmodule-2.0.so.0 /nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/libc.so.6 /nix/store/nlrfi2xsm3whpqawwr2xn76nzc889p9z-gnutls-3.8.10/lib/libgnutls.so.30]
[landrun] 2025/09/07 12:08:11 Sandbox config: {ReadOnlyPaths:[/nix/store/7ksl2hh26hsjgiwqpb1k2za4b7j7260n-qemu-user-10.0.3/bin/qemu-x86_64] ReadWritePaths:[] ReadOnlyExecutablePaths:[/nix/store/7ksl2hh26hsjgiwqpb1k2za4b7j7260n-qemu-user-10.0.3/bin/qemu-x86_64 /nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/ld-linux-aarch64.so.1 /nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/libm.so.6 /nix/store/9h0qgsb2r5shr143q0bq82pzzagp2pc5-glib-2.84.3/lib/libglib-2.0.so.0 /nix/store/9h0qgsb2r5shr143q0bq82pzzagp2pc5-glib-2.84.3/lib/libgmodule-2.0.so.0 /nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/libc.so.6 /nix/store/nlrfi2xsm3whpqawwr2xn76nzc889p9z-gnutls-3.8.10/lib/libgnutls.so.30] ReadWriteExecutablePaths:[] BindTCPPorts:[] ConnectTCPPorts:[] BestEffort:false UnrestrictedFilesystem:false UnrestrictedNetwork:false}
[landrun:debug] 2025/09/07 12:08:11 Adding read-only executable path: /nix/store/7ksl2hh26hsjgiwqpb1k2za4b7j7260n-qemu-user-10.0.3/bin/qemu-x86_64
[landrun:debug] 2025/09/07 12:08:11 Adding read-only executable path: /nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/ld-linux-aarch64.so.1
[landrun:debug] 2025/09/07 12:08:11 Adding read-only executable path: /nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/libm.so.6
[landrun:debug] 2025/09/07 12:08:11 Adding read-only executable path: /nix/store/9h0qgsb2r5shr143q0bq82pzzagp2pc5-glib-2.84.3/lib/libglib-2.0.so.0
[landrun:debug] 2025/09/07 12:08:11 Adding read-only executable path: /nix/store/9h0qgsb2r5shr143q0bq82pzzagp2pc5-glib-2.84.3/lib/libgmodule-2.0.so.0
[landrun:debug] 2025/09/07 12:08:11 Adding read-only executable path: /nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/libc.so.6
[landrun:debug] 2025/09/07 12:08:11 Adding read-only executable path: /nix/store/nlrfi2xsm3whpqawwr2xn76nzc889p9z-gnutls-3.8.10/lib/libgnutls.so.30
[landrun:debug] 2025/09/07 12:08:11 Adding read-only path: /nix/store/7ksl2hh26hsjgiwqpb1k2za4b7j7260n-qemu-user-10.0.3/bin/qemu-x86_64
[landrun:debug] 2025/09/07 12:08:11 Applying Landlock restrictions
[landrun] 2025/09/07 12:08:11 Landlock restrictions applied successfully
[landrun] 2025/09/07 12:08:11 Executing: [/nix/store/7ksl2hh26hsjgiwqpb1k2za4b7j7260n-qemu-user-10.0.3/bin/qemu-x86_64]
/nix/store/7ksl2hh26hsjgiwqpb1k2za4b7j7260n-qemu-user-10.0.3/bin/qemu-x86_64: error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directory

$ /nix/store/smlxnkdlgw03h8vmd4zlrkjnnbcsm6qm-landrun-0.1.15/bin/landrun --log-level debug --ldd --rox /nix/store/7ksl2hh26hsjgiwqpb1k2za4b7j7260n-qemu-user-10.0.3/bin/qemu-x86_64 /nix/store/7ksl2hh26hsjgiwqpb1k2za4b7j7260n-qemu-user-10.0.3/bin/qemu-x86_64                                                                    
[landrun:debug] 2025/09/07 12:08:34 Added library paths: [/nix/store/8vz5n20qwma5035m6s7fk05q5cv4g7r2-pcre2-10.44/lib/libpcre2-8.so.0 /nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/ld-linux-aarch64.so.1 /nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/libdl.so.2 /nix/store/anixmx00k0fhjilpd9j927as20k8yq82-libidn2-2.3.8/lib/libidn2.so.0 /nix/store/1rm9g0wqj489qqjz3i6s6zcwf7cycdz8-libtasn1-4.20.0/lib/libtasn1.so.6 /nix/store/4p7f5lp3grnpdsfa79i2i9cl5vjx7kwb-nettle-3.10.2/lib/libnettle.so.8 /nix/store/9h0qgsb2r5shr143q0bq82pzzagp2pc5-glib-2.84.3/lib/libglib-2.0.so.0 /nix/store/9h0qgsb2r5shr143q0bq82pzzagp2pc5-glib-2.84.3/lib/libgmodule-2.0.so.0 /nix/store/ig768bxaswpmyajrrldicxp6nlh454xg-zlib-1.3.1/lib/libz.so.1 /nix/store/87pbspfhvv6ldyp7xp3p1xb479lk8nh7-libunistring-1.3/lib/libunistring.so.5 /nix/store/nlrfi2xsm3whpqawwr2xn76nzc889p9z-gnutls-3.8.10/lib/libgnutls.so.30 /nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/libm.so.6 /nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/libc.so.6 /nix/store/f988n0dncjhg2cacm1ngp4cppxkr9c31-p11-kit-0.25.5/lib/libp11-kit.so.0 /nix/store/x93ml5v28dmb4sc4v6hyaz3b7iv5ygqj-libffi-3.5.1/lib/libffi.so.8 /nix/store/nzy86wzv86lm2iw0cswj9mkq35bz5bw3-gmp-with-cxx-6.3.0/lib/libgmp.so.10 /nix/store/4p7f5lp3grnpdsfa79i2i9cl5vjx7kwb-nettle-3.10.2/lib/libhogweed.so.6]
[landrun] 2025/09/07 12:08:34 Sandbox config: {ReadOnlyPaths:[/nix/store/7ksl2hh26hsjgiwqpb1k2za4b7j7260n-qemu-user-10.0.3/bin/qemu-x86_64] ReadWritePaths:[] ReadOnlyExecutablePaths:[/nix/store/7ksl2hh26hsjgiwqpb1k2za4b7j7260n-qemu-user-10.0.3/bin/qemu-x86_64 /nix/store/8vz5n20qwma5035m6s7fk05q5cv4g7r2-pcre2-10.44/lib/libpcre2-8.so.0 /nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/ld-linux-aarch64.so.1 /nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/libdl.so.2 /nix/store/anixmx00k0fhjilpd9j927as20k8yq82-libidn2-2.3.8/lib/libidn2.so.0 /nix/store/1rm9g0wqj489qqjz3i6s6zcwf7cycdz8-libtasn1-4.20.0/lib/libtasn1.so.6 /nix/store/4p7f5lp3grnpdsfa79i2i9cl5vjx7kwb-nettle-3.10.2/lib/libnettle.so.8 /nix/store/9h0qgsb2r5shr143q0bq82pzzagp2pc5-glib-2.84.3/lib/libglib-2.0.so.0 /nix/store/9h0qgsb2r5shr143q0bq82pzzagp2pc5-glib-2.84.3/lib/libgmodule-2.0.so.0 /nix/store/ig768bxaswpmyajrrldicxp6nlh454xg-zlib-1.3.1/lib/libz.so.1 /nix/store/87pbspfhvv6ldyp7xp3p1xb479lk8nh7-libunistring-1.3/lib/libunistring.so.5 /nix/store/nlrfi2xsm3whpqawwr2xn76nzc889p9z-gnutls-3.8.10/lib/libgnutls.so.30 /nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/libm.so.6 /nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/libc.so.6 /nix/store/f988n0dncjhg2cacm1ngp4cppxkr9c31-p11-kit-0.25.5/lib/libp11-kit.so.0 /nix/store/x93ml5v28dmb4sc4v6hyaz3b7iv5ygqj-libffi-3.5.1/lib/libffi.so.8 /nix/store/nzy86wzv86lm2iw0cswj9mkq35bz5bw3-gmp-with-cxx-6.3.0/lib/libgmp.so.10 /nix/store/4p7f5lp3grnpdsfa79i2i9cl5vjx7kwb-nettle-3.10.2/lib/libhogweed.so.6] ReadWriteExecutablePaths:[] BindTCPPorts:[] ConnectTCPPorts:[] BestEffort:false UnrestrictedFilesystem:false UnrestrictedNetwork:false}
[landrun:debug] 2025/09/07 12:08:34 Adding read-only executable path: /nix/store/7ksl2hh26hsjgiwqpb1k2za4b7j7260n-qemu-user-10.0.3/bin/qemu-x86_64
[landrun:debug] 2025/09/07 12:08:34 Adding read-only executable path: /nix/store/8vz5n20qwma5035m6s7fk05q5cv4g7r2-pcre2-10.44/lib/libpcre2-8.so.0
[landrun:debug] 2025/09/07 12:08:34 Adding read-only executable path: /nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/ld-linux-aarch64.so.1
[landrun:debug] 2025/09/07 12:08:34 Adding read-only executable path: /nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/libdl.so.2
[landrun:debug] 2025/09/07 12:08:34 Adding read-only executable path: /nix/store/anixmx00k0fhjilpd9j927as20k8yq82-libidn2-2.3.8/lib/libidn2.so.0
[landrun:debug] 2025/09/07 12:08:34 Adding read-only executable path: /nix/store/1rm9g0wqj489qqjz3i6s6zcwf7cycdz8-libtasn1-4.20.0/lib/libtasn1.so.6
[landrun:debug] 2025/09/07 12:08:34 Adding read-only executable path: /nix/store/4p7f5lp3grnpdsfa79i2i9cl5vjx7kwb-nettle-3.10.2/lib/libnettle.so.8
[landrun:debug] 2025/09/07 12:08:34 Adding read-only executable path: /nix/store/9h0qgsb2r5shr143q0bq82pzzagp2pc5-glib-2.84.3/lib/libglib-2.0.so.0
[landrun:debug] 2025/09/07 12:08:34 Adding read-only executable path: /nix/store/9h0qgsb2r5shr143q0bq82pzzagp2pc5-glib-2.84.3/lib/libgmodule-2.0.so.0
[landrun:debug] 2025/09/07 12:08:34 Adding read-only executable path: /nix/store/ig768bxaswpmyajrrldicxp6nlh454xg-zlib-1.3.1/lib/libz.so.1
[landrun:debug] 2025/09/07 12:08:34 Adding read-only executable path: /nix/store/87pbspfhvv6ldyp7xp3p1xb479lk8nh7-libunistring-1.3/lib/libunistring.so.5
[landrun:debug] 2025/09/07 12:08:34 Adding read-only executable path: /nix/store/nlrfi2xsm3whpqawwr2xn76nzc889p9z-gnutls-3.8.10/lib/libgnutls.so.30
[landrun:debug] 2025/09/07 12:08:34 Adding read-only executable path: /nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/libm.so.6
[landrun:debug] 2025/09/07 12:08:34 Adding read-only executable path: /nix/store/2hcy8bvfswxsvsxq5s9bwibhivs25aif-glibc-2.40-66/lib/libc.so.6
[landrun:debug] 2025/09/07 12:08:34 Adding read-only executable path: /nix/store/f988n0dncjhg2cacm1ngp4cppxkr9c31-p11-kit-0.25.5/lib/libp11-kit.so.0
[landrun:debug] 2025/09/07 12:08:34 Adding read-only executable path: /nix/store/x93ml5v28dmb4sc4v6hyaz3b7iv5ygqj-libffi-3.5.1/lib/libffi.so.8
[landrun:debug] 2025/09/07 12:08:34 Adding read-only executable path: /nix/store/nzy86wzv86lm2iw0cswj9mkq35bz5bw3-gmp-with-cxx-6.3.0/lib/libgmp.so.10
[landrun:debug] 2025/09/07 12:08:34 Adding read-only executable path: /nix/store/4p7f5lp3grnpdsfa79i2i9cl5vjx7kwb-nettle-3.10.2/lib/libhogweed.so.6
[landrun:debug] 2025/09/07 12:08:34 Adding read-only path: /nix/store/7ksl2hh26hsjgiwqpb1k2za4b7j7260n-qemu-user-10.0.3/bin/qemu-x86_64
[landrun:debug] 2025/09/07 12:08:34 Applying Landlock restrictions
[landrun] 2025/09/07 12:08:34 Landlock restrictions applied successfully
[landrun] 2025/09/07 12:08:34 Executing: [/nix/store/7ksl2hh26hsjgiwqpb1k2za4b7j7260n-qemu-user-10.0.3/bin/qemu-x86_64]
qemu: no user program specified

@Zouuup
Copy link
Copy Markdown
Owner

Zouuup commented Sep 12, 2025

Thanks guys, I'll review over the weekend!

@Zouuup Zouuup merged commit 8466565 into Zouuup:main Oct 1, 2025
5 checks passed
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

Successfully merging this pull request may close these issues.

3 participants