TheDan64 / inkwell

It's a New Kind of Wrapper for Exposing LLVM (Safely)
https://thedan64.github.io/inkwell/
Apache License 2.0
2.38k stars 229 forks source link

after importing LLVM IR, a segmentation fault occurs when calling its internal method #458

Open KKould opened 11 months ago

KKould commented 11 months ago

Describe the Bug after importing LLVM IR, a segmentation fault occurs when calling its internal method

To Reproduce sum.rs

#[no_mangle]
fn hello() {
    println!("hello world");
}

fn main() {}

main.rs

fn main() {
    let context = Context::create();
    let memory = MemoryBuffer::create_from_file("./sum.ll".as_ref()).unwrap();
    let module = context.create_module_from_ir(memory).unwrap();
    let ee = module
        .create_jit_execution_engine(OptimizationLevel::None)
        .unwrap();
    unsafe {
        let fn_hello = ee.get_function::<unsafe extern "C" fn()>("hello").unwrap();

        fn_hello.call();
    }
}

I first use rustc --emit=llvm-ir /home/kould/rust-llvm-practises/helloworld/src/example/sum.rs to convert sum.rs to LLVM IR sum.ll, and then call the hello method after importing it as a Module

Process finished with exit code 139 (interrupted by signal 11:SIGSEGV)

Expected Behavior displayed in terminal

hello world

LLVM Version (please complete the following information):

Desktop (please complete the following information):

Additional Context

TheDan64 commented 9 months ago

Doesn't hello have to be extern "C"? Not sure no mangle is enough

JCBurnside commented 9 months ago

I don't think extern "C" changes anything here since all types are already in a c compatible arrangement but can't hurt