Segfault on calling function pointer obtained with dlopen() in Rust

I am currently playing with POSIX functions defined in dlfcn.h with Rust, with the goal of calling a function in a separated .so file.

The project actually contains 2 crates:

  • The binary loading the shared object and calling the function.
  • The dynamic library.

The dynamic library is 4 lines long and here is the code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#[no_mangle]
pub extern "Rust" fn hello_world() -> String {
String::from("Hello World !")
}
</code>
<code>#[no_mangle] pub extern "Rust" fn hello_world() -> String { String::from("Hello World !") } </code>
#[no_mangle]
pub extern "Rust" fn hello_world() -> String {
    String::from("Hello World !")
}

But the executable binary is a bit more complex and looks like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>#![feature(str_from_raw_parts)]
fn open_dl(path: &str) -> Result<*mut libc::c_void, String> {
let path = format!("{path}");
let path: *const libc::c_char = unsafe { std::mem::transmute(path.as_ptr()) };
let handle = unsafe { libc::dlopen(path, libc::RTLD_NOW) };
if handle.is_null() {
let dl_error: &str = unsafe {
let ptr = libc::dlerror();
let len = libc::strlen(ptr);
std::str::from_raw_parts(std::mem::transmute(ptr), len)
};
return Err(String::from(dl_error));
}
Ok(handle)
}
fn get_symbol<T>(handle: *mut libc::c_void, symbol: &str) -> Result<&T, String> {
let symbol_name = format!("{symbol}");
let symbol = unsafe { libc::dlsym(handle, std::mem::transmute(symbol_name.as_ptr())) };
if symbol.is_null() {
let dl_error: &str = unsafe {
let ptr = libc::dlerror();
let len = libc::strlen(ptr);
std::str::from_raw_parts(std::mem::transmute(ptr), len)
};
return Err(String::from(dl_error));
}
return unsafe { Ok(std::mem::transmute(symbol)) };
}
fn main() -> Result<(), String> {
let handle = open_dl("./libfoo.so")?;
// Tricky part, get the symbol with the correct signature and calling it
let symbol: &fn() -> String = get_symbol(handle, "hello_world")?;
let val = symbol(); // Blows up
println!("{val}");
Ok(())
}
</code>
<code>#![feature(str_from_raw_parts)] fn open_dl(path: &str) -> Result<*mut libc::c_void, String> { let path = format!("{path}"); let path: *const libc::c_char = unsafe { std::mem::transmute(path.as_ptr()) }; let handle = unsafe { libc::dlopen(path, libc::RTLD_NOW) }; if handle.is_null() { let dl_error: &str = unsafe { let ptr = libc::dlerror(); let len = libc::strlen(ptr); std::str::from_raw_parts(std::mem::transmute(ptr), len) }; return Err(String::from(dl_error)); } Ok(handle) } fn get_symbol<T>(handle: *mut libc::c_void, symbol: &str) -> Result<&T, String> { let symbol_name = format!("{symbol}"); let symbol = unsafe { libc::dlsym(handle, std::mem::transmute(symbol_name.as_ptr())) }; if symbol.is_null() { let dl_error: &str = unsafe { let ptr = libc::dlerror(); let len = libc::strlen(ptr); std::str::from_raw_parts(std::mem::transmute(ptr), len) }; return Err(String::from(dl_error)); } return unsafe { Ok(std::mem::transmute(symbol)) }; } fn main() -> Result<(), String> { let handle = open_dl("./libfoo.so")?; // Tricky part, get the symbol with the correct signature and calling it let symbol: &fn() -> String = get_symbol(handle, "hello_world")?; let val = symbol(); // Blows up println!("{val}"); Ok(()) } </code>
#![feature(str_from_raw_parts)]

fn open_dl(path: &str) -> Result<*mut libc::c_void, String> {
    let path = format!("{path}");

    let path: *const libc::c_char = unsafe { std::mem::transmute(path.as_ptr()) };
    let handle = unsafe { libc::dlopen(path, libc::RTLD_NOW) };

    if handle.is_null() {
        let dl_error: &str = unsafe {
            let ptr = libc::dlerror();
            let len = libc::strlen(ptr);
            std::str::from_raw_parts(std::mem::transmute(ptr), len)
        };

        return Err(String::from(dl_error));
    }
    Ok(handle)
}

fn get_symbol<T>(handle: *mut libc::c_void, symbol: &str) -> Result<&T, String> {
    let symbol_name = format!("{symbol}");
    let symbol = unsafe { libc::dlsym(handle, std::mem::transmute(symbol_name.as_ptr())) };

    if symbol.is_null() {
        let dl_error: &str = unsafe {
            let ptr = libc::dlerror();
            let len = libc::strlen(ptr);
            std::str::from_raw_parts(std::mem::transmute(ptr), len)
        };

        return Err(String::from(dl_error));
    }
    return unsafe { Ok(std::mem::transmute(symbol)) };
}

fn main() -> Result<(), String> {
    let handle = open_dl("./libfoo.so")?;

    // Tricky part, get the symbol with the correct signature and calling it
    let symbol: &fn() -> String = get_symbol(handle, "hello_world")?;
    let val = symbol(); // Blows up

    println!("{val}");

    Ok(())
}

My problem is that get_symbol seems to work fine, the obtained pointer is not null, so I would expect that calling the function behind it would cause no issue, but I get a SEGFAULT when I try to. Do you wonder why it does SEGFAULT ?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật