Closed iwillspeak closed 6 years ago
I have managed to prove that this segfault is due to a double-free on the Module
. This can be fixed in the examples by using mem::forget
:
diff --git a/examples/add.rs b/examples/add.rs
index 9050176..f1f8cf0 100644
--- a/examples/add.rs
+++ b/examples/add.rs
@@ -1,21 +1,25 @@
extern crate llvm;
+use std::mem;
use llvm::*;
use llvm::Attribute::*;
fn main() {
let ctx = Context::new();
let module = Module::new("add", &ctx);
- let func = module.add_function("add", Type::get::<fn(f64, f64) -> f64>(&ctx));
- func.add_attributes(&[NoUnwind, ReadNone]);
- let entry = func.append("entry");
- let builder = Builder::new(&ctx);
- builder.position_at_end(entry);
- let a = &func[0];
- let b = &func[1];
- let value = builder.build_add(a, b);
- builder.build_ret(value);
- module.verify().unwrap();
- let ee = JitEngine::new(&module, JitOptions {opt_level: 3}).unwrap();
- ee.with_function(func, |add:extern fn((f64, f64)) -> f64| {
- println!("{} + {} = {}", 1., 2., add((1., 2.)));
- });
+ {
+ let func = module.add_function("add", Type::get::<fn(f64, f64) -> f64>(&ctx));
+ func.add_attributes(&[NoUnwind, ReadNone]);
+ let entry = func.append("entry");
+ let builder = Builder::new(&ctx);
+ builder.position_at_end(entry);
+ let a = &func[0];
+ let b = &func[1];
+ let value = builder.build_add(a, b);
+ builder.build_ret(value);
+ module.verify().unwrap();
+ let ee = JitEngine::new(&module, JitOptions {opt_level: 3}).unwrap();
+ ee.with_function(func, |add:extern fn((f64, f64)) -> f64| {
+ println!("{} + {} = {}", 1., 2., add((1., 2.)));
+ });
+ }
+ mem::forget(module);
}
I guess this means that JitEngine::new
should take ownership of the module rather than receiving a borrow of it. Not sure if it's quite that simple though due to the lifetime of func
For anyone still concerned: you can also solve this issue by removing the module from the ExecutionEngine before the engine is dropped, i.e. ee.remove_module(&module);
.
Could the execution engine take ownership of the module when created so that Rust's ownership semantics protect us in this case?
I'm compiling on OS X with
rustc 1.13.0-nightly (f883b0bba 2016-08-19)
and LLVM3.8.1
from Homebrew.When running any of the examples I receive a segfault:
This seems to be from the drop implementation for
Module
I think this is because the
JitEngine
has taken ownership of the module and dropped it already.