Segfault with rust, musl and c++

Hi,

I have the following setup. In src/test.cc:

extern "C" {

void *test() {
    return new int();
}

}

and in src/main.rs:

use std::os::raw::c_void;

extern "C" {
    #[link_name = "test"]
    fn test() -> *const c_void;
}

fn main() {
    println!("{:?}", unsafe { test() });
}

and build.rs:

use std::path::Path;

fn main() {
    let dir = Path::new("src");

    cc::Build::new()
        .cpp(true)
        .include(&dir)
        .file(dir.join("test.cc"))
        .compile("test");
}

cargo run works fine with x86_64-unknown-linux-gnu but when I try running with x86_64-unknown-linux-musl I get:

Segmentation fault (core dumped)

I think the error is something to do with rust and musl-g++ disagreeing about which c++ standard library to use. gbd confirms that the segfault happens in the call to new int();. Are there any pointers as to what is going wrong here? I am using unsafe to call the external function but I don't think I am doing anything to deserve a segfault.

SSCCE here: https://github.com/harrysarson/musl-rust-segfault. (I am using a musl toolchain compiled from https://github.com/richfelker/musl-cross-make because arch linux don't have a prebuilt musl c++ that I can install).

1 Like

This is a minimised example though! For the real project I want to compile I do need to (ideally statically) link in libstdc++. My program compiles, links and runs fine with the (default) x86_64-unknown-linux-gnu target. With x86_64-unknown-linux-musl is compiles and links fine but segfaults when I run it, I am looking to understand why and how to fix it!

Hi, I think it's known issue while linking with c++ library.

I found few folks made it possible by linking additional C++ runtime, from comments. I copy-pasted the snippet to make a workaround succeeds with your example. Hope it helps.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.