SWdV
SWdV

Reputation: 1997

How to deal with crates exporting symbols with identical names

I'd like to use both just-argon2 and sodiumoxide which are bindings to Argon2 and libsodium respectively. The first I will use for password hashing, the second for encryption and more.

However, because libsodium itself also includes the Argon2 code, I get the following linker error when building:

error: linking with `link.exe` failed: exit code: 1169
  |
  = note: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Preview\\VC\\Tools\\MSVC\\14.29.30132\\bin\\HostX64\\x64\\link.exe" "/NOLOGO" "/NXCOMPAT" "/LIBPATH:<omitted>\\rust-ms\\tools\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.0.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.1.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.10.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.11.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.12.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.13.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.14.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.15.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.2.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.3.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.4.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.5.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.6.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.7.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.8.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.9.rcgu.o" "/OUT:<omitted>\\target\\debug\\deps\\sw_password_vault.exe" "<omitted>\\target\\debug\\deps\\sw_password_vault.2501tt6qpnxmq6dq.rcgu.o" "/OPT:REF,NOICF" "/DEBUG" "/NATVIS:<omitted>\\rust-ms\\tools\\lib\\rustlib\\etc\\intrinsic.natvis" "/NATVIS:<omitted>\\rust-ms\\tools\\lib\\rustlib\\etc\\liballoc.natvis" "/NATVIS:<omitted>\\rust-ms\\tools\\lib\\rustlib\\etc\\libcore.natvis" "/NATVIS:<omitted>\\rust-ms\\tools\\lib\\rustlib\\etc\\libstd.natvis" "/LIBPATH:<omitted>\\target\\debug\\deps" "/LIBPATH:<omitted>\\target\\debug\\build\\just-argon2-fba8d38863f06c3e\\out" "/LIBPATH:C:\\Users\\Steven\\.cargo\\registry\\src\\github.com-1ecc6299db9ec823\\libsodium-sys-0.2.7\\msvc/x64/Debug/v142/" "/LIBPATH:<omitted>\\rust-ms\\tools\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "<omitted>\\target\\debug\\deps\\libargon2-47416e342f12fc80.rlib" "<omitted>\\target\\debug\\deps\\libbitflags-033856bffcb41e1a.rlib" "<omitted>\\target\\debug\\deps\\libsodiumoxide-ae3f70995957a7e5.rlib" "<omitted>\\target\\debug\\deps\\libserde-02c238cdfdb411bb.rlib" "<omitted>\\target\\debug\\deps\\libed25519-a72dcd735d2405a2.rlib" "<omitted>\\target\\debug\\deps\\libsignature-cb8ca284112f3bdb.rlib" "<omitted>\\target\\debug\\deps\\liblibsodium_sys-90db65a5d41df800.rlib" "<omitted>\\target\\debug\\deps\\liblibc-db8e81727092f722.rlib" "<omitted>\\rust-ms\\tools\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libstd-7d8f1d3120dc2b31.rlib" "<omitted>\\lib\\libpanic_unwind-00b4871c13b6f72d.rlib" "<omitted>\\lib\\libstd_detect-38300272c9518b1b.rlib" "<omitted>\\lib\\librustc_demangle-008ea074760d3d54.rlib" "<omitted>\\lib\\libhashbrown-5d102da67e495133.rlib" "<omitted>\\lib\\librustc_std_workspace_alloc-108ed3dcf248b873.rlib" "<omitted>\\lib\\libunwind-bc9246c93f49e49a.rlib" "<omitted>\\lib\\libcfg_if-7ca234fdcd728c26.rlib" "<omitted>\\lib\\liblibc-e328514fb4ed0383.rlib" "<omitted>\\lib\\liballoc-5d0a4c6a1ffa6373.rlib" "<omitted>\\lib\\librustc_std_workspace_core-d379e9227cab087f.rlib" "<omitted>\\lib\\libcore-825774e96423c2c5.rlib" "<omitted>\\lib\\libcompiler_builtins-d5d1908505fa83bc.rlib" "advapi32.lib" "ws2_32.lib" "userenv.lib" "msvcrt.lib"
  = note: liblibsodium_sys-90db65a5d41df800.rlib(blake2b-ref.obj) : error LNK2005: blake2b_init already defined in libargon2-47416e342f12fc80.rlib(blake2b.o)
          liblibsodium_sys-90db65a5d41df800.rlib(blake2b-ref.obj) : error LNK2005: blake2b_init_key already defined in libargon2-47416e342f12fc80.rlib(blake2b.o)
          liblibsodium_sys-90db65a5d41df800.rlib(blake2b-ref.obj) : error LNK2005: blake2b_init_param already defined in libargon2-47416e342f12fc80.rlib(blake2b.o)
          liblibsodium_sys-90db65a5d41df800.rlib(blake2b-ref.obj) : error LNK2005: blake2b_update already defined in libargon2-47416e342f12fc80.rlib(blake2b.o)
          liblibsodium_sys-90db65a5d41df800.rlib(blake2b-ref.obj) : error LNK2005: blake2b_final already defined in libargon2-47416e342f12fc80.rlib(blake2b.o)
          liblibsodium_sys-90db65a5d41df800.rlib(blake2b-ref.obj) : error LNK2005: blake2b already defined in libargon2-47416e342f12fc80.rlib(blake2b.o)
             Creating library C:\dev\Rust\SWPasswordVault\target\debug\deps\sw_password_vault.lib and object C:\dev\Rust\SWPasswordVault\target\debug\deps\sw_password_vault.exp
          LINK : warning LNK4098: defaultlib 'LIBCMTD' conflicts with use of other libs; use /NODEFAULTLIB:library
          C:\dev\Rust\SWPasswordVault\target\debug\deps\sw_password_vault.exe : fatal error LNK1169: one or more multiply defined symbols found

Both libraries export the same blake2b_* symbols, which are used by the Argon2 implementations, so the names clash.

Now you may be wondering: why not just use the libsodium pwhash API, which uses Argon2 internally (hence the linker error)? Because I want to use parallel Argon2, which is not exposed by libsodium, as it hardcodes the degree of parallelism to 1.

Apparently, the blake2b_* symbols are marked as ARGON2_LOCAL, so they are not exported, but I guess that this does not matter since they still exist for the linker.

One solution could be to use a pure Rust implementation of Argon2 such as rust-argon2, but I tested that one and unfortunately it is a lot slower.

How can I still use both libraries together? Do I have to make a separate library crate using just just-argon2 and export the functions provided by the just-argon2 crate or something like that, or would that not work / is there a better way? EDIT: this does not work.

Minimum non-compiling program:

use sodiumoxide;
use argon2;

fn main() { sodiumoxide::init(); }

While composing this program I noticed that when I swap the use statements it compiles fine, but it crashes at runtime:

use argon2;
use sodiumoxide;

fn main() {
    sodiumoxide::init().expect("Could not init libsodium");

    let password = "pass";

    let salt = sodiumoxide::crypto::pwhash::gen_salt().0;
    let mut key_bytes = [0u8; 32];

    // INSECURE cost parameters
    argon2::id_hash_raw(1, 1 << 10 /*1 MiB*/, 1,
                        Some(password.as_bytes()), Some(&salt), &mut key_bytes)
        .unwrap_or_else(|err| panic!("Error hashing: {:?}", err));
}

This crashes with a STATUS_STACK_BUFFER_OVERRUN Windows exception (__fastfail(FAST_FAIL_STACK_COOKIE_CHECK_FAILURE)), while it works fine when not using any sodiumoxide functions (and an all-zero salt, for example). Apparently the different exports clash or something?

By the way: I'm very new to Rust so it's safe to say I don't know all the ins and outs of it.

Upvotes: 4

Views: 778

Answers (0)

Related Questions