Comments (3)
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
from llvm-rs.
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);
.
from llvm-rs.
Could the execution engine take ownership of the module when created so that Rust's ownership semantics protect us in this case?
from llvm-rs.
Related Issues (19)
- What is the status/roadmap of this crate HOT 1
- How to call `JitEngine.with_function` with more than 1 argument HOT 9
- Trouble building HOT 1
- Type checking code in `JitEngine::with_function` tends to fail with segfault.
- Please update crates.io, 0.3 doesn't build with rustc 1.10
- Build script behaves incorrectly on Windows HOT 1
- How do I insert intrinsics with this library? HOT 1
- with_function fails to pass args properly when number of args > 3 HOT 3
- doesn't handle memory correctly
- libffi not linked in?
- can not link in win/msvc.
- Is this project maintained? HOT 1
- builder::build_neg only works for integer inputs HOT 1
- crash when calling build_gep with a compiled 0usize
- Won't build HOT 2
- Varargs are missing.
- JitEngine::with_function has strange precondition when StructType::cast fails
- codegen for f64 return type seems broken
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from llvm-rs.