Git Product home page Git Product logo

Comments (3)

iwillspeak avatar iwillspeak commented on June 22, 2024

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.

willcrichton avatar willcrichton commented on June 22, 2024

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.

iwillspeak avatar iwillspeak commented on June 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.