Skip to content

Calling HashMap::new() from Dynamically loaded lib, causes Segfault, Illegal Instruction #18521

@ashleysommer

Description

@ashleysommer

I was running across this issue in my project last week.
See Stackoverflow question here and reddit.com/r/rust thread here

Basically, calling HashMap::new() inside my runtime-dynamically-loaded plugin lib is causing an unwind, and then rust segfaults while doing the unwind. Running the application on the command line gives Illegal Instruction output, running inside gdb gives:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff51a0c60 in ?? ()

I've produced a minimal test case:
main.rs

use std::dynamic_lib::DynamicLibrary;
use std::mem;

fn main()
{
    let p1 = match DynamicLibrary::open(Some("libplugin.so")) {
            Ok(lib) => lib,
            Err(error) => panic!("Could not load the library: {}", error)
    };
    let s1: extern "Rust" fn() = unsafe {
        match p1.symbol("init") {
            Err(error) => panic!("Could not load function init: {}", error),
            Ok(init) => mem::transmute::<*mut u8, _>(init)
        }
    };
    s1();
}

plugin.rs

use std::collections::hashmap::{HashMap};
use std::collections::treemap::{TreeMap};

#[no_mangle]
pub fn init()
{
     let h:HashMap<&str, &str> = HashMap::new();
     //let t:TreeMap<&str, &str> = TreeMap::new();
     println!("Loaded!");
}

Changing HashMap to TreeMap in plugin.rs causes the segfault to go away, and the program runs correctly.

Im using the latest Rust master codebase, compiled on linux x64.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-runtimeArea: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflowsI-crashIssue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions