diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index e166bce..2a8e09e 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -1099,179 +1099,6 @@ pub extern "C" fn componentize_py_to_canon_handle( } } -// TODO: Update to the latest `wit-component`, which has a `use_built_in_libdl` option that makes the following -// unecessary: -pub mod dl { - use std::{ - ffi::{c_char, c_int, c_void, CStr}, - ptr, slice, - }; - - const RTLD_LAZY: c_int = 1; - const RTLD_NOW: c_int = 2; - - const RTLD_NEXT: isize = -1; - const RTLD_DEFAULT: isize = 0; - - #[repr(C)] - pub struct Name { - length: u32, - data: *const u8, - } - - #[repr(C)] - pub struct Symbol { - name: Name, - address: *const c_void, - } - - #[repr(C)] - pub struct Symbols { - count: u32, - symbols: *const Symbol, - } - - #[repr(C)] - pub struct Library { - name: Name, - symbols: Symbols, - } - - #[repr(C)] - pub struct Libraries { - count: u32, - libraries: *const Library, - } - - struct Pointer(*const T); - - unsafe impl Sync for Pointer {} - - static mut ERROR: Pointer = Pointer(ptr::null()); - static mut LIBRARIES: Pointer = Pointer(ptr::null()); - - unsafe fn invalid_handle(library: *const c_void) -> bool { - if LIBRARIES.0.is_null() { - panic!( - "`__wasm_set_libraries` should have been called during \ - instantiation with a non-NULL value" - ); - } - - let library = library as *const Library; - if (0..(*LIBRARIES.0).count).any(|index| { - (*LIBRARIES.0) - .libraries - .add(usize::try_from(index).unwrap()) - == library - }) { - false - } else { - ERROR.0 = b"invalid library handle\0" as *const _ as _; - true - } - } - - /// # Safety - /// TODO - #[no_mangle] - pub unsafe extern "C" fn dlclose(library: *mut c_void) -> c_int { - if invalid_handle(library) { - -1 - } else { - 0 - } - } - - /// # Safety - /// TODO - #[no_mangle] - pub unsafe extern "C" fn dlerror() -> *const c_char { - let value = ERROR.0; - ERROR.0 = ptr::null(); - value - } - - /// # Safety - /// TODO - #[no_mangle] - pub unsafe extern "C" fn dlopen(name: *const c_char, flags: c_int) -> *const c_void { - if LIBRARIES.0.is_null() { - panic!( - "`__wasm_set_libraries` should have been called during \ - instantiation with a non-NULL value" - ); - } - - if (flags & !(RTLD_LAZY | RTLD_NOW)) != 0 { - // TODO - ERROR.0 = b"dlopen flags not yet supported\0" as *const _ as _; - return ptr::null(); - } - - let name = CStr::from_ptr(name); - let name = name.to_bytes(); - let libraries = slice::from_raw_parts( - (*LIBRARIES.0).libraries, - usize::try_from((*LIBRARIES.0).count).unwrap(), - ); - if let Ok(index) = libraries.binary_search_by(|library| { - slice::from_raw_parts( - library.name.data, - usize::try_from(library.name.length).unwrap(), - ) - .cmp(name) - }) { - &libraries[index] as *const _ as _ - } else { - ERROR.0 = "library not found\0" as *const _ as _; - ptr::null() - } - } - - /// # Safety - /// TODO - #[no_mangle] - pub unsafe extern "C" fn dlsym(library: *const c_void, name: *const c_char) -> *const c_void { - if library as isize == RTLD_NEXT || library as isize == RTLD_DEFAULT { - // TODO - ERROR.0 = "dlsym RTLD_NEXT and RTLD_DEFAULT not yet supported\0" as *const _ as _; - return ptr::null(); - } - - if invalid_handle(library) { - return ptr::null(); - } - - let library = library as *const Library; - let name = CStr::from_ptr(name); - let name = name.to_bytes(); - let symbols = slice::from_raw_parts( - (*library).symbols.symbols, - usize::try_from((*library).symbols.count).unwrap(), - ); - if let Ok(index) = symbols.binary_search_by(|symbol| { - slice::from_raw_parts( - symbol.name.data, - usize::try_from(symbol.name.length).unwrap(), - ) - .cmp(name) - }) { - symbols[index].address - } else { - ERROR.0 = "library not found\0" as *const _ as _; - ptr::null() - } - } - - /// # Safety - /// TODO - #[no_mangle] - pub unsafe extern "C" fn __wasm_set_libraries(libraries: *const Libraries) { - LIBRARIES.0 = libraries; - } -} - // As of this writing, recent Rust `nightly` builds include a version of the `libc` crate that expects `wasi-libc` // to define the following global variables, but `wasi-libc` defines them as preprocessor constants which aren't // visible at link time, so we need to define them somewhere. Ideally, we should fix this upstream, but for now we diff --git a/src/lib.rs b/src/lib.rs index e8f5b06..76b0f36 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -285,7 +285,9 @@ pub async fn componentize( }); // Link all the libraries (including any native extensions) into a single component. - let mut linker = wit_component::Linker::default().validate(true); + let mut linker = wit_component::Linker::default() + .validate(true) + .use_built_in_libdl(true); let mut wasi_imports = HashMap::new(); for Library {