4

I'm trying to do some dynamic library loading in Rust. I'm getting a segmentation fault when passing a large Vec from a dynamically loaded library function. It's a basic function that creates a Vec<i32> of a specified size. If the Vec gets much bigger than 8MB, the program hits a segfault on OSX. I haven't had the same problem when running on linux, can anyone look at this and tell me if I'm doing something wrong here? I'm running this with:

$ cargo build --release
$ ./target/release/linkfoo
8281
[1]    84253 segmentation fault  ./target/release/linkfoo

Cargo.toml

[package]
name = "linkfoo"
version = "0.1.0"
authors = ["Nate Mara <nathan.mara@kroger.com>"]

[dependencies]
libloading = "0.3.0"

[lib]
name = "foo"
crate-type = ["dylib"]

main.rs

extern crate libloading as lib;

fn main() {
    let dylib = lib::Library::new("./target/release/libfoo.dylib").expect("Failed to load library");
    let func = unsafe {
        let wrapped_func: lib::Symbol<fn(&[i32]) -> Vec<i32>> = dylib.get(b"alloc")
            .expect("Failed to load function");

        wrapped_func.into_raw()
    };

    let args = vec![8182];
    println!("{}", func(&args).len());
}

lib.rs

#[no_mangle]
pub fn alloc(args: &[i32]) -> Vec<i32> {
    let size = args[0] as usize;
    let mut mat = Vec::with_capacity(size);

    for _ in 0..size {
        mat.push(0);
    }

    mat
}
Lily Mara
  • 3,859
  • 4
  • 29
  • 48
  • What version of the rust toolchain are you using ? I compiled and ran your code fine without a segfault on `rustc 1.10.0 (cfcb716cf 2016-07-03)` and `rustc 1.12.0-nightly (feeca9457 2016-07-26)` on El Capitan 10.11.6. – Christian Witts Jul 29 '16 at 07:54
  • I'm using 1.10 stable – Lily Mara Jul 29 '16 at 10:20
  • I am able to get it to crash now. If you run `rust-lldb target/release/linkfoo` and then `r` to run the program what is the segfault reason you get ? Mine bombs with ```stop reason = EXC_BAD_ACCESS (code=1, address=0x0) frame #0: 0x0000000100026404 segfault`je_huge_dalloc [inlined] je_rtree_val_read(dependent=true) + 4 at rtree.h:193``` – Christian Witts Jul 29 '16 at 11:09
  • Subsequent executions of the binary succeed without segfaulting. – Christian Witts Jul 29 '16 at 11:09
  • Have you seen http://stackoverflow.com/q/26602353/155423 ? – Shepmaster Jul 29 '16 at 12:04

1 Answers1

1

Rust uses the system allocator for dynamic libraries, and jemalloc for all other code. This difference in loaders was causing the segfault, and I was able to fix it by adding this to the top of main.rs:

#![feature(alloc_system)]
extern crate alloc_system;
Eric Platon
  • 9,819
  • 6
  • 41
  • 48
Lily Mara
  • 3,859
  • 4
  • 29
  • 48