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

#[link(kind = "static", name = "c")] segfaults on run #124043

Open
GuillaumeGomez opened this issue Apr 16, 2024 · 2 comments
Open

#[link(kind = "static", name = "c")] segfaults on run #124043

GuillaumeGomez opened this issue Apr 16, 2024 · 2 comments
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. O-linux Operating system: Linux T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@GuillaumeGomez
Copy link
Member

While working on #123974, I had a failure when running doctests of the unstable book. Rustdoc generates this code:

#![allow(unused_extern_crates)]
#![allow(internal_features)]
#![feature(test)]
#![feature(rustc_attrs)]
#![feature(coverage_attribute)]
#![feature(link_arg_attribute)]

#![allow(unused)]
extern crate test;
mod __doctest_0 {

fn main()  {
    #[link(kind = "link-arg", name = "--start-group")]
#[link(kind = "static", name = "c")] // Causing the segfault
#[link(kind = "static", name = "gcc")]
#[link(kind = "link-arg", name = "--end-group")]
extern "C" {}
}
#[rustc_test_marker = "/home/imperio/rust/rust/src/doc/unstable-book/src/language-features/link-arg-attribute.md - The_tracking_issue_for_this_feature_is__ (line 11)"]
pub const TEST: test::TestDescAndFn = test::TestDescAndFn {
    desc: test::TestDesc {
        name: test::StaticTestName("/home/imperio/rust/rust/src/doc/unstable-book/src/language-features/link-arg-attribute.md - The_tracking_issue_for_this_feature_is__ (line 11)"),
        ignore: false,
        ignore_message: None,
        source_file: "_home_imperio_rust_rust_src_doc_unstable_book_src_language_features_link_arg_attribute_md",
        start_line: 11,
        start_col: 0,
        end_line: 0,
        end_col: 0,
        compile_fail: false,
        no_run: true,
        should_panic: test::ShouldPanic::No,
        test_type: test::TestType::UnitTest,
    },
    testfn: test::StaticTestFn(
        #[coverage(off)]
        || test::assert_test_result(Ok::<(), String>(())),
    )
};
}

#[rustc_main]
#[coverage(off)]
fn main() {
    test::test_main(&["rustdoctest".to_string(),], vec![__doctest_0::TEST], None);
}

If you compile and run this program (on linux x86_64), it'll segfault. Commenting #[link(kind = "static", name = "c")] will make it run without problem.

@GuillaumeGomez GuillaumeGomez added the C-bug Category: This is a bug. label Apr 16, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 16, 2024
@Noratrieb
Copy link
Member

I'd just like to interject for a moment. What you're refering to as Linux, is in fact, GNU/Linux, or as I've recently taken to calling it, GNU plus Linux.
No but if you're on GNU/Linux then I do expect that to end badly, because glibc is not supposed to be linked statically.
I also don't know how rustc deals with having several linkages to "c" (as the standard library also links to it) but probably not very well either.
it would be interesting to know where it segfaults, but this certainly isn't supposed to work :D.

@Noratrieb Noratrieb added A-linkage Area: linking into static, shared libraries and binaries O-linux Operating system: Linux T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Apr 17, 2024
@bjorn3
Copy link
Member

bjorn3 commented Apr 17, 2024

You have to use -Ctarget-feature=+crt-static to statically link libc. Otherwise you will still be producing a dynamically linked executable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. O-linux Operating system: Linux T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants