Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build.rs panics due to _FORTIFY_SOURCE warnings #108

Open
cratelyn opened this issue Nov 25, 2024 · 13 comments
Open

build.rs panics due to _FORTIFY_SOURCE warnings #108

cratelyn opened this issue Nov 25, 2024 · 13 comments

Comments

@cratelyn
Copy link

first, thanks for maintaining this crate. it is a very useful piece of software.

i have found that the build.rs panics when i attempt to build this package using clang 17, due to warnings related a missing optimization flag:

the panic looks like:

  thread 'main' panicked at /home/user/.cargo/registry/src/index.crates.io-6f17d22bba15001f/jemalloc-sys-0.5.4+5.3.0-patched/build.rs:351:9:
  command did not execute successfully: cd "/home/user/project/target/debug/build/jemalloc-sys-3acf0278209d6cd4/out/build" && "make" "-j" "24"
  expected success, got: exit status: 2
  stack backtrace:
     0:     0x55d9c7f25c46 - std::backtrace_rs::backtrace::libunwind::trace::hbee8a7973eeb6c93
                                 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5
     1:     0x55d9c7f25c46 - std::backtrace_rs::backtrace::trace_unsynchronized::hc8ac75eea3aa6899
                                 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
     2:     0x55d9c7f25c46 - std::sys_common::backtrace::_print_fmt::hc7f3e3b5298b1083
                                 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:68:5
     3:     0x55d9c7f25c46 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hbb235daedd7c6190
                                 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:44:22
     4:     0x55d9c7f4b570 - core::fmt::rt::Argument::fmt::h76c38a80d925a410
                                 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/fmt/rt.rs:142:9
     5:     0x55d9c7f4b570 - core::fmt::write::h3ed6aeaa977c8e45
                                 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/fmt/mod.rs:1120:17
     6:     0x55d9c7f2342f - std::io::Write::write_fmt::h78b18af5775fedb5
                                 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/io/mod.rs:1810:15
     7:     0x55d9c7f25a24 - std::sys_common::backtrace::_print::h5d645a07e0fcfdbb
                                 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:47:5
     8:     0x55d9c7f25a24 - std::sys_common::backtrace::print::h85035a511aafe7a8
                                 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:34:9
     9:     0x55d9c7f27197 - std::panicking::default_hook::{{closure}}::hcce8cea212785a25
    10:     0x55d9c7f26ef9 - std::panicking::default_hook::hf5fcb0f213fe709a
                                 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:292:9
    11:     0x55d9c7f27628 - std::panicking::rust_panic_with_hook::h095fccf1dc9379ee
                                 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:779:13
    12:     0x55d9c7f27502 - std::panicking::begin_panic_handler::{{closure}}::h032ba12139b353db
                                 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:657:13
    13:     0x55d9c7f26146 - std::sys_common::backtrace::__rust_end_short_backtrace::h9259bc2ff8fd0f76
                                 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:171:18
    14:     0x55d9c7f27260 - rust_begin_unwind
                                 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:645:5
    15:     0x55d9c7ea27e5 - core::panicking::panic_fmt::h784f20a50eaab275
                                 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:72:14
    16:     0x55d9c7ea9f3a - build_script_build::execute::hbc4736f388b7b82a
    17:     0x55d9c7ea9976 - build_script_build::run::he8ae1f16b0712186
    18:     0x55d9c7ea8cd3 - build_script_build::main::hcfc9252a05fd2a7e
    19:     0x55d9c7eaaeb3 - core::ops::function::FnOnce::call_once::h69b966a051d1c3c4
    20:     0x55d9c7eaa696 - std::sys_common::backtrace::__rust_begin_short_backtrace::ha21bc1e2f23e880e
    21:     0x55d9c7eaa939 - std::rt::lang_start::{{closure}}::h864902cc94152336
    22:     0x55d9c7f1ee91 - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::h37600b1e5eea4ecd
                                 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/ops/function.rs:284:13
    23:     0x55d9c7f1ee91 - std::panicking::try::do_call::hb4bda49fa13a0c2b
                                 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:552:40
    24:     0x55d9c7f1ee91 - std::panicking::try::h8bbf75149211aaaa
                                 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:516:19
    25:     0x55d9c7f1ee91 - std::panic::catch_unwind::h8c78ec68ebea34cb
                                 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panic.rs:142:14
    26:     0x55d9c7f1ee91 - std::rt::lang_start_internal::{{closure}}::hffdf44a19fd9e220
                                 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/rt.rs:148:48
    27:     0x55d9c7f1ee91 - std::panicking::try::do_call::hcb3194972c74716d
                                 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:552:40
    28:     0x55d9c7f1ee91 - std::panicking::try::hcdc6892c5f0dba4c
                                 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:516:19
    29:     0x55d9c7f1ee91 - std::panic::catch_unwind::h4910beb4573f4776
                                 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panic.rs:142:14
    30:     0x55d9c7f1ee91 - std::rt::lang_start_internal::h6939038e2873596b
                                 at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/rt.rs:148:20
    31:     0x55d9c7eaa917 - std::rt::lang_start::h698ca701eec9f6fd
    32:     0x55d9c7eaa4a5 - main
    33:     0x7faeb41c010e - __libc_start_call_main
    34:     0x7faeb41c01c9 - __libc_start_main@@GLIBC_2.34
    35:     0x55d9c7ea3045 - _start
    36:                0x0 - <unknown>
warning: build failed, waiting for other jobs to finish...

and i see warnings (promoted to errors) that look like this:

  /nix/store/yp441ann6ch705yk708h61ywxn7ayhj3-glibc-2.39-52-dev/include/features.h:414:4: warning: _FORTIFY_SOURCE requires compiling with optimization (-O) [-W#warnings]
    414 | #  warning _FORTIFY_SOURCE requires compiling with optimization (-O)                                                                                                                                          
        |    ^                                                                                              
  1 warning generated.
  In file included from src/pac.c:1:
  In file included from include/jemalloc/internal/jemalloc_preamble.h:5:
  In file included from include/jemalloc/internal/jemalloc_internal_decls.h:4:
  In file included from /nix/store/yp441ann6ch705yk708h61ywxn7ayhj3-glibc-2.39-52-dev/include/math.h:27:
  In file included from /nix/store/yp441ann6ch705yk708h61ywxn7ayhj3-glibc-2.39-52-dev/include/bits/libc-header-start.h:33:
  /nix/store/yp441ann6ch705yk708h61ywxn7ayhj3-glibc-2.39-52-dev/include/features.h:414:4: warning: _FORTIFY_SOURCE requires compiling with optimization (-O) [-W#warnings]
    414 | #  warning _FORTIFY_SOURCE requires compiling with optimization (-O)
        |    ^                                                                                                                                                                                                          
  1 warning generated.                                                                                                                                                                                                  
  In file included from src/peak_event.c:1:
  In file included from include/jemalloc/internal/jemalloc_preamble.h:5:
  In file included from include/jemalloc/internal/jemalloc_internal_decls.h:4:                
  In file included from /nix/store/yp441ann6ch705yk708h61ywxn7ayhj3-glibc-2.39-52-dev/include/math.h:27:                                                                                                                
  In file included from /nix/store/yp441ann6ch705yk708h61ywxn7ayhj3-glibc-2.39-52-dev/include/bits/libc-header-start.h:33:
  /nix/store/yp441ann6ch705yk708h61ywxn7ayhj3-glibc-2.39-52-dev/include/features.h:414:4: warning: _FORTIFY_SOURCE requires compiling with optimization (-O) [-W#warnings]
    414 | #  warning _FORTIFY_SOURCE requires compiling with optimization (-O)        
        |    ^                                                                                                                                                                                                          
  In file included from src/prof_data.c:1:                                                                                                                                                                              
  In file included from include/jemalloc/internal/jemalloc_preamble.h:5:                                                                                                                                                
  In file included from include/jemalloc/internal/jemalloc_internal_decls.h:4:
  In file included from /nix/store/yp441ann6ch705yk708h61ywxn7ayhj3-glibc-2.39-52-dev/include/math.h:27:                                                                                                                
  In file included from /nix/store/yp441ann6ch705yk708h61ywxn7ayhj3-glibc-2.39-52-dev/include/bits/libc-header-start.h:33:
  /nix/store/yp441ann6ch705yk708h61ywxn7ayhj3-glibc-2.39-52-dev/include/features.h:414:4: warning: _FORTIFY_SOURCE requires compiling with optimization (-O) [-W#warnings]
    414 | #  warning _FORTIFY_SOURCE requires compiling with optimization (-O)
        |    ^                                                                     

these are observed with the following clang/llvm toolchain:

[nix-shell:~/linkerd/linkerd2-proxy]$ clang --version
clang version 17.0.6
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /nix/store/95iw6rgr5fbmywa8cbpzqgyakfkjh0ka-clang-17.0.6/bin

however, i do not see this panic if i revert to an older version of clang,

[nix-shell:~/linkerd/linkerd2-proxy]$ clang --version
clang version 14.0.6
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /nix/store/fx89cvsnz3wrx1ljsng8d54n6x4kq3cq-clang-14.0.6/bin
@BusyJay
Copy link
Member

BusyJay commented Nov 26, 2024

and i see warnings (promoted to errors)

We don't enable warnings_into_errors flag, and according to https://docs.rs/cc/latest/cc/struct.Build.html#method.warnings_into_errors, it's disabled by default.

Can you compile github.com/jemalloc/jemalloc successfully?

@cratelyn
Copy link
Author

interesting, thank you @BusyJay. i'll close this issue, pardon the trouble! it sounds like the -W flag is coming from elsewhere.

@cratelyn cratelyn closed this as not planned Won't fix, can't repro, duplicate, stale Nov 26, 2024
@cratelyn
Copy link
Author

cratelyn commented Nov 26, 2024

and i see warnings (promoted to errors)

We don't enable warnings_into_errors flag, and according to https://docs.rs/cc/latest/cc/struct.Build.html#method.warnings_into_errors, it's disabled by default.

Can you compile github.com/jemalloc/jemalloc successfully?

https://github.com/tikv/jemallocator/blob/main/jemalloc-sys/configure/configure#L3811-L3819

it looks like -Wall is set by the configure script within this repo?

this leads me to see these CFLAGS in my build output:

CFLAGS="-O0 -ffunction-sections -fdata-sections -fPIC -gdwarf-4 -fno-omit-frame-pointer -m64 --target=x86_64-unknown-linux-gnu -Wall"          

jemalloc builds successfully if i pin myself to an older LLVM/clang version. that will work for me in the short term, i provide these extra notes in case they're of use to someone else someday. 🙂

@dpc
Copy link

dpc commented Dec 14, 2024

I came here to report the same issue.

--- a/Cargo.toml
+++ b/Cargo.toml
@@ -160,6 +160,7
[profile.dev.package]
 ring = { opt-level = 3 }
+tikv-jemalloc-sys = { opt-level = 3 }

fixes it.

I was not aware it is caused by -Wall. I was actually going to ask for build.rs to automatically compile with -O2 or something.

Anyway, this is definitely a problem, possibly in newer versions of LLVM, and it affects all our downstream users, and explaining to them they should add that line to their Cargo.toml is not a great workaround. And rocksdb without jemalloc suffers from terrible memory fragmentation, so we greatly appreciate tikv-jemallocator and would love to keep using it.

@cratelyn
Copy link
Author

i'll reopen this on behalf of @dpc, since this is affecting others as well.

@cratelyn cratelyn reopened this Dec 16, 2024
@dpc
Copy link

dpc commented Dec 17, 2024

Huh...

I found some time to do more investigation, and it seems the actual failure was:

 fedi-fedimint-pkgs-group-ci-deps>   /nix/store/wmq01pqz681xdgq2n0hl5w7bbdr738g1-clang-wrapper-16.0.6/bin/clang -std=gnu11 -Werror=unknown-warning-option -Wall -Wextra -Wshorten-64-to-32 -Wsign-compare -Wundef -Wno-format-zero-length -Wpointer-arith -Wno-missing-braces -Wno-missing-field-initializers -pipe -g3 -fvisibility=hidden -Wimplicit-fallthrough -O3 -funroll-loops -O0 -ffunction-sections -fdata-sections -fPIC -gdwarf-4 -fno-omit-frame-pointer -m64 --target=x86_64-unknown-linux-gnu -Wall -c -O0 -ffunction-sections -fdata-sections -fPIC -gdwarf-4 -fno-omit-frame-pointer -m64 --target=x86_64-unknown-linux-gnu -Wall -D_GNU_SOURCE -D_REENTRANT -Iinclude -Iinclude -DJEMALLOC_NO_PRIVATE_NAMESPACE -o src/psset.sym.o src/psset.c


...


fedi-fedimint-pkgs-group-ci-deps>   src/malloc_io.c:107:9: error: incompatible pointer to integer conversion returning 'char *' from a function with result type 'int' [-Wint-conversion]
fedi-fedimint-pkgs-group-ci-deps>           return strerror_r(err, buf, buflen);
fedi-fedimint-pkgs-group-ci-deps>                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
fedi-fedimint-pkgs-group-ci-deps>   1 warning generated.
fedi-fedimint-pkgs-group-ci-deps>   1 warning and 1 error generated.
fedi-fedimint-pkgs-group-ci-deps>   1 warning generated.
fedi-fedimint-pkgs-group-ci-deps>   make: *** [Makefile:480: src/malloc_io.sym.o] Error 1
fedi-fedimint-pkgs-group-ci-deps>   make: *** Waiting for unfinished jobs....

clang 16, on Linux, the _FORTIFY_SOURCE warning seems like a red hearing-ish?

I verified again that forcing opt-level = 3 makes the problem go away.

@BusyJay
Copy link
Member

BusyJay commented Dec 18, 2024

Can someone provide full build logs? Especially the file in target/debug/build/tikv-jemalloc-sys-*/{output,stderr,out/build/config.log}.

@BusyJay
Copy link
Member

BusyJay commented Dec 18, 2024

I can't reproduce it on an alpine image that has clang 19. I notice that all provided logs in the issue are related to nix. However, I have never used nix before. I suspects there is some misconfiguration during compilation.

strerror_r should return an int when it's XSI-compliant, but GNU-specific version will return a char*. From the configure logs, jemalloc detects that strerror_r is not GNU-specific:

checking whether strerror_r returns char with gnu source is compilable... no

however, the function somehow returns char* in the end, which leads to the error. This can happen only when different headers are used between detection and compilation.

@dpc
Copy link

dpc commented Dec 18, 2024

It isn't impossible it is a NixOS issue. It seems it doesn't happen with clang14 provided by NixOS, but it does with clang16 (would need to confirm it again). It's unlikely the c toolchain got switched during building of a single cargo crate, but it could be something internally that goes wrong / confuses autoconf?

@BusyJay
Copy link
Member

BusyJay commented Dec 18, 2024

It's unlikely the c toolchain got switched during building of a single cargo crate

I don't mean header is switched. configure only checks strerror_r from string.h, it's possible when compiling malloc_io.c, a different version of strerror_r from different header is used.

@jr1221
Copy link

jr1221 commented Jan 3, 2025

I have the same problem on NixOS with gcc 14.2.1. Downgrading to gcc13 fixed the issue for me. Very peculiar, and most definitely related to Nix.

@dpc
Copy link

dpc commented Jan 3, 2025

I guess it makes sense to report to nixpkgs then: NixOS/nixpkgs#370494

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

No branches or pull requests

4 participants