purpleprotocol / mimalloc_rust

A Rust wrapper over Microsoft's MiMalloc memory allocator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Overriding `opt-level` can cause build failure

athre0z opened this issue · comments

Overriding opt-level in the profile section of Cargo.toml can cause the build to fail in some configurations.

Configurations tested:

config opt-level result is default for config
release 0 error
release 1 ok
release 2 ok
release 3 ok yes
release s ok
release z ok
dev 0 ok yes
dev 1 error
dev 2 error
dev 3 error
dev s error
dev z error

All configurations have been tested on macOS 10.15 using rustc 1.43.0-nightly (61d9231ff 2020-02-15), however the issue is also reproducible on stable.

cargo build
   Compiling cc v1.0.50
   Compiling cmake v0.1.42
   Compiling libmimalloc-sys v0.1.11
   Compiling mimalloc v0.1.15
   Compiling mimalloc-bug v0.1.0 (/Users/ath/Desktop/mimalloc-bug)
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-m64" "-L" "/Users/ath/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/Users/ath/Desktop/mimalloc-bug/target/debug/deps/mimalloc_bug-42788d712a0b88c0.3o6rag49wvizlx5g.rcgu.o" "/Users/ath/Desktop/mimalloc-bug/target/debug/deps/mimalloc_bug-42788d712a0b88c0.4c5l0qvhb8yxh6zm.rcgu.o" "-o" "/Users/ath/Desktop/mimalloc-bug/target/debug/deps/mimalloc_bug-42788d712a0b88c0" "/Users/ath/Desktop/mimalloc-bug/target/debug/deps/mimalloc_bug-42788d712a0b88c0.58227kppbbj41572.rcgu.o" "-Wl,-dead_strip" "-nodefaultlibs" "-L" "/Users/ath/Desktop/mimalloc-bug/target/debug/deps" "-L" "/Users/ath/Desktop/mimalloc-bug/target/debug/build/libmimalloc-sys-9b850dda39d061a7/out/./build" "-L" "/Users/ath/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib" "/Users/ath/Desktop/mimalloc-bug/target/debug/deps/libmimalloc-0f6424fe8ef935fd.rlib" "/Users/ath/Desktop/mimalloc-bug/target/debug/deps/liblibmimalloc_sys-f18c2d2d62137939.rlib" "/Users/ath/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libstd-cfe2101dd1e70619.rlib" "/Users/ath/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-f4fdcbfc0e831e44.rlib" "/Users/ath/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libhashbrown-f174dbd124f70dd8.rlib" "/Users/ath/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_alloc-30c786bb77075347.rlib" "/Users/ath/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libbacktrace-d2d4b2bc9b0063cc.rlib" "/Users/ath/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libbacktrace_sys-e46ee1024fcddcbd.rlib" "/Users/ath/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_demangle-30932f9b784d6391.rlib" "/Users/ath/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libunwind-f7b9755d6d963692.rlib" "/Users/ath/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcfg_if-b51b81f72ad5e041.rlib" "/Users/ath/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liblibc-1b8d433af5825310.rlib" "/Users/ath/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/liballoc-0b36d32c9554147c.rlib" "/Users/ath/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_core-cc380a1bd62ec40f.rlib" "/Users/ath/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcore-75018f89db63cb56.rlib" "/Users/ath/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-76e3e5da2d7d9f6d.rlib" "-lmimalloc-secure-debug" "-lSystem" "-lresolv" "-lc" "-lm"
  = note: ld: library not found for -lmimalloc-secure-debug
          clang: error: linker command failed with exit code 1 (use -v to see invocation)


error: aborting due to previous error

error: could not compile `mimalloc-bug`.

To learn more, run the command again with --verbose.

I attached an example project to experiment with.
mimalloc-bug.tar.gz

This might be somewhat similar to #9. It looks like this is down to cmake-rs choosing to do CMake debug, release or minsize builds based on the opt-level as well as a few other factors (click, click) which isn't exactly matched by the same decision logic in mimalloc-rs' build.rs that determines which lib to link against.

I kind of see three ways of fixing this:

  • Just go with .profile("3"), hardcoding optimized builds for mimalloc
    • I don't know much about mimalloc internals and whether that might be a bad idea. If mimalloc enabled additional memory corruption / overflow checks in debug mode, that is probably not an option
  • Replicate cmake-rs' logic for deciding the profile
  • Ask the cmake-rs people whether it might be possible to export this code that does the automatic decision via an API, so that we can just reuse it. This will probably take a while.

@athre0z Hi and thank you for opening this!

I would honestly go with option 2. We can read the PROFILE and OPT_LEVEL env variables and determine the correct paths according to the same logic the cmake-rs crate follows.

I believe this might also provide a fix for #9.

Fixed in #16