tkaitchuck / aHash

aHash is a non-cryptographic hashing algorithm that uses the AES hardware instruction
https://crates.io/crates/ahash
Apache License 2.0
986 stars 94 forks source link

Linking Errors with Specific Optimization Levels When Running Test Cases #225

Closed cicilzx closed 4 months ago

cicilzx commented 4 months ago

Hi, I've noticed that when running test cases for this crate, I get linking errors when using certain specific optimization levels, not sure if it's a problem with the crate or with rustc.

I am using cargo version: cargo 1.77.0-nightly (7bb7b5395 2024-01-20), and below is an example of triggering the issue.

By using the following command:

RUSTFLAGS="-C opt-level=2" cargo test

The tests will run successfully like this:

......
test src/lib.rs - (line 81) ... ok
test src/hash_set.rs - hash_set::&'_AHashSet<T,S>::bitand (line 149) ... ok
test src/hash_set.rs - hash_set::&'_AHashSet<T,S>::bitor (line 117) ... ok
test src/lib.rs - (line 54) ... ok

test result: ok. 26 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 3.03s

However, if I change the optimization level to 0/1/z, for example, with the command:

cargo clean
RUSTFLAGS="-C opt-level=0" cargo test

It will produce the follow panic:

error: linking with `cc` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/home/cici/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/cici/.vscode-server/bin/863d2581ecda6849923a2118d93a088b0745d9d6/bin/remote-cli:/home/cici/.local/bin:/home/cici/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/lib/wsl/lib:/mnt/c/Windows/system32:/mnt/c/Windows:/mnt/c/Windows/System32/Wbem:/mnt/c/Windows/System32/WindowsPowerShell/v1.0/:/mnt/c/Windows/System32/OpenSSH/:/mnt/c/Program Files (x86)/NVIDIA Corporation/PhysX/Common:/mnt/c/Program Files/NVIDIA Corporation/NVIDIA NvDLISR:/mnt/c/Program Files (x86)/NetSarang/Xshell 7/:/mnt/c/Program Files/Tailscale/:/mnt/d/Program Files/Git/cmd:/mnt/c/Program Files/Docker/Docker/resources/bin:/mnt/c/Users/lzxci/AppData/Local/Microsoft/WindowsApps:/mnt/d/Program Files/Microsoft VS Code/bin:/snap/bin" VSLANG="1033" "cc" "-m64" "/tmp/rustcrSV4JS/symbols.o" "/home/cici/aHash/target/debug/deps/nopanic-74574200cafb6558.test-0cc739d3ab01cf96.test.727ed03cf0e156ae-cgu.0.rcgu.o.rcgu.o" "-Wl,--as-needed" "-L" "/home/cici/aHash/target/debug/deps" "-L" "/home/cici/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/cici/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-3166674eacfcf914.rlib" "-Wl,-Bdynamic" "-lc" "-lm" "-lrt" "-lpthread" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/home/cici/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/home/cici/aHash/target/debug/deps/nopanic-74574200cafb6558" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs"
  = note: /usr/bin/ld: /home/cici/aHash/target/debug/deps/nopanic-74574200cafb6558.test-0cc739d3ab01cf96.test.727ed03cf0e156ae-cgu.0.rcgu.o.rcgu.o: in function `<nopanic::hash_test_final::__NoPanic as core::ops::drop::Drop>::drop':
          /home/cici/aHash/tests/nopanic.rs:8: undefined reference to `

          ERROR[no-panic]: detected panic in function `hash_test_final`
          '
          /usr/bin/ld: /home/cici/aHash/target/debug/deps/nopanic-74574200cafb6558.test-0cc739d3ab01cf96.test.727ed03cf0e156ae-cgu.0.rcgu.o.rcgu.o: in function `<nopanic::hash_test_random::__NoPanic as core::ops::drop::Drop>::drop':
          /home/cici/aHash/tests/nopanic.rs:63: undefined reference to `

          ERROR[no-panic]: detected panic in function `hash_test_random`
          '
          /usr/bin/ld: /home/cici/aHash/target/debug/deps/nopanic-74574200cafb6558.test-0cc739d3ab01cf96.test.727ed03cf0e156ae-cgu.0.rcgu.o.rcgu.o: in function `<nopanic::hash_test_specialize::__NoPanic as core::ops::drop::Drop>::drop':
          /home/cici/aHash/tests/nopanic.rs:47: undefined reference to `

          ERROR[no-panic]: detected panic in function `hash_test_specialize`
          '
          collect2: error: ld returned 1 exit status

  = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#rustc-link-lib)

error: could not compile `ahash` (test "nopanic") due to 1 previous error
warning: build failed, waiting for other jobs to finish...
warning: `ahash` (test "bench") generated 1 warning
warning: `ahash` (test "map_tests") generated 2 warnings (run `cargo fix --test "map_tests"` to apply 1 suggestion)
tkaitchuck commented 4 months ago

This is intentional. (And is a link error not a panic)

The tests are using the crate no-panic to assert that it is impossible for the hasher to cause a panic. This works by writing a test where a macro injects a call to a non-existent library inside of a panic catch block. Then if the optimizer is able to prove this code is unreachable it will be deleted and no link error will occur. For this to work the optimizer usually needs to be turned up to at least O2.