Skip to content

Commit

Permalink
remove obsolete dl module (#118)
Browse files Browse the repository at this point in the history
Signed-off-by: Joel Dice <[email protected]>
Co-authored-by: Joel Dice <[email protected]>
  • Loading branch information
benbrandt and dicej authored Oct 28, 2024
1 parent 117b1bc commit 214128d
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 174 deletions.
173 changes: 0 additions & 173 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(*const T);

unsafe impl<T> Sync for Pointer<T> {}

static mut ERROR: Pointer<c_char> = Pointer(ptr::null());
static mut LIBRARIES: Pointer<Libraries> = 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
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,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);

for Library {
name,
Expand Down

0 comments on commit 214128d

Please sign in to comment.