a16z/jolt

Encountering Problem When Compiling Jolt as Dynamic Library

Opened this issue · 0 comments

I'm trying to compile a Rust program that uses Jolt as a dynamic library (cdylib).
But libloading crate failed to load the compiled library (.so file).

thread 'main' panicked at src/main.rs:8:58:
called `Result::unwrap()` on an `Err` value: DlOpen { desc: "../lib-producer/target/release/liblib_producer.so: undefined symbol: _HEAP_PTR" }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

To demonstrate the issue, I have provided the complete code in this repository .

There are 3 apps:

  • guest is the sample fibonacci guest code
  • lib-producer is a Rust library package
    • dependency: jolt-sdk and guest
    • target [lib] crate-type = ["rlib", "cdylib"]
    • in src/lib.rs it exports C functions arbitrary_string and execute_guest_code
    • it produces lib-producer/target/release/liblib_producer.so file
  • lib-consumer is a Rust app that will load the lib-producer/target/release/liblib_producer.so file and call the functions inside
    • dependency: libloading

In lib-producer/src/lib.rs, it exports function like this:

#[no_mangle]
#[allow(improper_ctypes_definitions)]
pub extern "C" fn arbitrary_string() -> String {
    "Hello, World!".to_string()
}

Then in lib-consumer/src/main.rs, it loads the dynamic library file and calls the function

    unsafe {
        // Load dynamic library
        let lib = libloading::Library::new(LIBRARY_PATH).unwrap(); // Error happened here, before executing any function inside

        // Execute arbitrary_string()
        let arbitrary_string_fn: libloading::Symbol<unsafe extern "C" fn() -> String> = lib
            .get(b"arbitrary_string")
            .unwrap();
        let arbitrary_string = arbitrary_string_fn();
        println!("arbitrary_string: {:?}", arbitrary_string);
}

You can see the complete code here.

So, first I run cargo build -r in lib-producer.
Then I run cargo run in lib-consumer.
But I always got this error:

thread 'main' panicked at src/main.rs:8:58:
called `Result::unwrap()` on an `Err` value: DlOpen { desc: "../lib-producer/target/release/liblib_producer.so: undefined symbol: _HEAP_PTR" }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Do you know what happened here?
Thank you.