Linking errors when using building fuzzing binaries

Hello,

I'm having trouble building some binaries using cargo fuzz build. Does anyone have any experience with errors in this area? After I run:

cargo +nightly fuzz build fuzz_tree_map_api

I get errors like:

error: linking with `cc` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin/self-contained:/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin/self-contained:/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin/self-contained:/home/declan/.elan/bin:/home/declan/.local/bin:/home/declan/.cargo/bin:/home/declan/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin" VSLANG="1033" "cc" "-m64" "/tmp/rustc29IIps/symbols.o" "-Wl,-Bstatic" "-Wl,--whole-archive" "-Wl,/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc-nightly_rt.asan.a" "-Wl,--no-whole-archive" "/home/declan/repos/github/declanvk/blart/fuzz/target/x86_64-unknown-linux-gnu/release/deps/fuzz_tree_map_api-75267ef0f779b0ec.fuzz_tree_map_api.4cc5fab0f974b524-cgu.0.rcgu.o" "/home/declan/repos/github/declanvk/blart/fuzz/target/x86_64-unknown-linux-gnu/release/deps/fuzz_tree_map_api-75267ef0f779b0ec.1y1mcp2bh25t01fjbp4c81huj.rcgu.o" "-Wl,--as-needed" "/home/declan/repos/github/declanvk/blart/fuzz/target/x86_64-unknown-linux-gnu/release/deps/liblibfuzzer_sys-e5fc9c7f0534b835.rlib" "/home/declan/repos/github/declanvk/blart/fuzz/target/x86_64-unknown-linux-gnu/release/deps/libarbitrary-93c39a0986e0573f.rlib" "/home/declan/repos/github/declanvk/blart/fuzz/target/x86_64-unknown-linux-gnu/release/deps/libblart-035256a1a7469c5e.rlib" "/home/declan/repos/github/declanvk/blart/fuzz/target/x86_64-unknown-linux-gnu/release/deps/libbytemuck-a5967d19cd2d22a2.rlib" "/home/declan/repos/github/declanvk/blart/fuzz/target/x86_64-unknown-linux-gnu/release/deps/libsptr-68e480c600282a5c.rlib" "/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-b7496091c0628861.rlib" "/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-88fbd7fc3f6191ac.rlib" "/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-3cde559bd38beda9.rlib" "/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-fa97315c69d0bf16.rlib" "/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-f71802864f365d8d.rlib" "/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-2fc763da12024778.rlib" "/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-de372216f105bcc0.rlib" "/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-c92aa9505ba54896.rlib" "/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-1290c243b075e81c.rlib" "/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-7367a33f078a78f7.rlib" "/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-fbbaaa45e62fc80e.rlib" "/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-34bb4537ed7f45c7.rlib" "/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-8ca34f59c17ace7d.rlib" "/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-47e924ffd8a98444.rlib" "/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-fbe0149ad3765332.rlib" "/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-b4f82c3eca2f0f4b.rlib" "/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-395a38b8e0851c9b.rlib" "/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-d453bab70303062c.rlib" "/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-75d2cc7090a2edf1.rlib" "-Wl,-Bdynamic" "-lstdc++" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-B/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin/gcc-ld" "-B/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin/gcc-ld" "-B/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin/gcc-ld" "-fuse-ld=lld" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/home/declan/repos/github/declanvk/blart/fuzz/target/x86_64-unknown-linux-gnu/release/build/libfuzzer-sys-b8898970852b42b1/out" "-L" "/home/declan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/home/declan/repos/github/declanvk/blart/fuzz/target/x86_64-unknown-linux-gnu/release/deps/fuzz_tree_map_api-75267ef0f779b0ec" "-pie" "-Wl,-z,relro,-z,now" "-Wl,-O1" "-nodefaultlibs"
  = note: rust-lld: error: unable to find library -lstdc++
          collect2: error: ld returned 1 exit status

Sorry for the long line output and my ignorance on the subject of linking.

I double checked that I did have the cpp standard library installed:

ldconfig -p | grep stdc++
        libstdc++.so.6 (libc6,x32) => /libx32/libstdc++.so.6
        libstdc++.so.6 (libc6,x86-64) => /lib/x86_64-linux-gnu/libstdc++.so.6
        libstdc++.so.6 (libc6) => /lib/i386-linux-gnu/libstdc++.so.6
        libstdc++.so.6 (libc6) => /lib32/libstdc++.so.6

The linking requirement comes from this line in libfuzzer-sys's build.rs file (behavior is implied by enabling cpp according to the cc docs).

I tried various other things like:

  • Setting the CUSTOM_LIBFUZZER_PATH env variable to a pre-built version of libfuzzer
  • Setting the CUSTOM_LIBFUZZER_STD_CXX env variable to none or other values which didn't work.
    • Setting it to none actually moved the error forward, but then it complained about missing symbol which (I assume) were meant to come from the cpp standard

None of these panned out either, most with the same error.

I also enabled some additional logging in rustc by running:

export RUSTC_LOG='rustc_codegen_ssa::back::link=info'

But this didn't turn up anything other than the original command from the very top. I can provide more of this output on request though.

I also tried swapping back to us the system linker by add RUSTFLAGS='-Z linker-features=-lld' to revert to the system linker, but this didn't change the error either.


Here are some versions:

  • cargo 1.83.0-nightly (c1fa840a8 2024-08-29)
  • Working from the tip of this repository (GitHub - declanvk/blart)
    • Beware the fuzz_raw_api fuzz target, that one is broken
  • cargo-fuzz 0.12.0
  • Linux 6.5.0-44-generic #44~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue Jun 18 14:36:16 UTC 2 x86_64 GNU/Linux

Installed stdc++ packages:

apt list --installed | grep libstdc++
libstdc++-11-dev/jammy-updates,jammy-security,now 11.4.0-1ubuntu1~22.04 amd64 [installed,automatic]
libstdc++6/jammy-updates,jammy-security,now 12.3.0-1ubuntu1~22.04 amd64 [installed]
libstdc++6/jammy-updates,jammy-security,now 12.3.0-1ubuntu1~22.04 i386 [installed,automatic]