|
| 1 | +#![no_std] |
| 2 | + |
| 3 | +//! This library uses lookup tables generated by `wasm-tools component link` to |
| 4 | +//! provide runtime library and symbol resolution via `dlopen` and `dlsym`. The |
| 5 | +//! tables are provided via a call to `__wasm_set_libraries`, which `wasm-tools |
| 6 | +//! component link` arranges as part of component instantiation. |
| 7 | +
|
| 8 | +use core::{ |
| 9 | + ffi::{c_char, c_int, c_void, CStr}, |
| 10 | + ptr, slice, |
| 11 | +}; |
| 12 | + |
| 13 | +const RTLD_LAZY: c_int = 1; |
| 14 | +const RTLD_NOW: c_int = 2; |
| 15 | + |
| 16 | +const RTLD_NEXT: isize = -1; |
| 17 | +const RTLD_DEFAULT: isize = 0; |
| 18 | + |
| 19 | +#[repr(C)] |
| 20 | +pub struct Name { |
| 21 | + length: u32, |
| 22 | + data: *const u8, |
| 23 | +} |
| 24 | + |
| 25 | +#[repr(C)] |
| 26 | +pub struct Symbol { |
| 27 | + name: Name, |
| 28 | + address: *const c_void, |
| 29 | +} |
| 30 | + |
| 31 | +#[repr(C)] |
| 32 | +pub struct Symbols { |
| 33 | + count: u32, |
| 34 | + symbols: *const Symbol, |
| 35 | +} |
| 36 | + |
| 37 | +#[repr(C)] |
| 38 | +pub struct Library { |
| 39 | + name: Name, |
| 40 | + symbols: Symbols, |
| 41 | +} |
| 42 | + |
| 43 | +#[repr(C)] |
| 44 | +pub struct Libraries { |
| 45 | + count: u32, |
| 46 | + libraries: *const Library, |
| 47 | +} |
| 48 | + |
| 49 | +static mut ERROR: *const c_char = ptr::null(); |
| 50 | +static mut LIBRARIES: *const Libraries = ptr::null(); |
| 51 | + |
| 52 | +unsafe fn invalid_handle(library: *const c_void) -> bool { |
| 53 | + if LIBRARIES.is_null() { |
| 54 | + panic!( |
| 55 | + "`__wasm_set_libraries` should have been called during \ |
| 56 | + instantiation with a non-NULL value" |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + let library = library as *const Library; |
| 61 | + if (0..(*LIBRARIES).count) |
| 62 | + .any(|index| (*LIBRARIES).libraries.add(usize::try_from(index).unwrap()) == library) |
| 63 | + { |
| 64 | + false |
| 65 | + } else { |
| 66 | + ERROR = c"invalid library handle".as_ptr(); |
| 67 | + true |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/// # Safety |
| 72 | +/// |
| 73 | +/// `library` must be a valid, not-yet-closed library pointer returned by |
| 74 | +/// `dlopen`. |
| 75 | +#[no_mangle] |
| 76 | +pub unsafe extern "C" fn dlclose(library: *mut c_void) -> c_int { |
| 77 | + if invalid_handle(library) { |
| 78 | + -1 |
| 79 | + } else { |
| 80 | + 0 |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +#[no_mangle] |
| 85 | +pub extern "C" fn dlerror() -> *const c_char { |
| 86 | + unsafe { |
| 87 | + let value = ERROR; |
| 88 | + ERROR = ptr::null(); |
| 89 | + value |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +/// # Safety |
| 94 | +/// |
| 95 | +/// `name` must be a valid pointer to a null-terminated string. |
| 96 | +#[no_mangle] |
| 97 | +pub unsafe extern "C" fn dlopen(name: *const c_char, flags: c_int) -> *const c_void { |
| 98 | + if LIBRARIES.is_null() { |
| 99 | + panic!( |
| 100 | + "`__wasm_set_libraries` should have been called during \ |
| 101 | + instantiation with a non-NULL value" |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + if (flags & !(RTLD_LAZY | RTLD_NOW)) != 0 { |
| 106 | + // TODO |
| 107 | + ERROR = c"dlopen flags not yet supported".as_ptr(); |
| 108 | + return ptr::null(); |
| 109 | + } |
| 110 | + |
| 111 | + let name = CStr::from_ptr(name); |
| 112 | + let name = name.to_bytes(); |
| 113 | + let libraries = slice::from_raw_parts( |
| 114 | + (*LIBRARIES).libraries, |
| 115 | + usize::try_from((*LIBRARIES).count).unwrap(), |
| 116 | + ); |
| 117 | + if let Ok(index) = libraries.binary_search_by(|library| { |
| 118 | + slice::from_raw_parts( |
| 119 | + library.name.data, |
| 120 | + usize::try_from(library.name.length).unwrap(), |
| 121 | + ) |
| 122 | + .cmp(name) |
| 123 | + }) { |
| 124 | + &libraries[index] as *const _ as _ |
| 125 | + } else { |
| 126 | + ERROR = c"library not found".as_ptr(); |
| 127 | + ptr::null() |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +/// # Safety |
| 132 | +/// |
| 133 | +/// `library` must be a valid, not-yet-closed library pointer returned by |
| 134 | +/// `dlopen`, and `name` must be a valid pointer to a null-terminated string. |
| 135 | +#[no_mangle] |
| 136 | +pub unsafe extern "C" fn dlsym(library: *const c_void, name: *const c_char) -> *const c_void { |
| 137 | + if library as isize == RTLD_NEXT || library as isize == RTLD_DEFAULT { |
| 138 | + // TODO |
| 139 | + ERROR = c"dlsym RTLD_NEXT and RTLD_DEFAULT not yet supported".as_ptr(); |
| 140 | + return ptr::null(); |
| 141 | + } |
| 142 | + |
| 143 | + if invalid_handle(library) { |
| 144 | + return ptr::null(); |
| 145 | + } |
| 146 | + |
| 147 | + let library = library as *const Library; |
| 148 | + let name = CStr::from_ptr(name); |
| 149 | + let name = name.to_bytes(); |
| 150 | + let symbols = slice::from_raw_parts( |
| 151 | + (*library).symbols.symbols, |
| 152 | + usize::try_from((*library).symbols.count).unwrap(), |
| 153 | + ); |
| 154 | + if let Ok(index) = symbols.binary_search_by(|symbol| { |
| 155 | + slice::from_raw_parts( |
| 156 | + symbol.name.data, |
| 157 | + usize::try_from(symbol.name.length).unwrap(), |
| 158 | + ) |
| 159 | + .cmp(name) |
| 160 | + }) { |
| 161 | + symbols[index].address |
| 162 | + } else { |
| 163 | + ERROR = c"symbol not found".as_ptr(); |
| 164 | + ptr::null() |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +/// # Safety |
| 169 | +/// |
| 170 | +/// `libraries` must be a valid pointer to a `Libraries` object, and this |
| 171 | +/// pointer must remain valid for the lifetime of the process. |
| 172 | +#[no_mangle] |
| 173 | +pub unsafe extern "C" fn __wasm_set_libraries(libraries: *const Libraries) { |
| 174 | + LIBRARIES = libraries; |
| 175 | +} |
| 176 | + |
| 177 | +#[cfg(target_arch = "wasm32")] |
| 178 | +#[panic_handler] |
| 179 | +fn panic(_info: &core::panic::PanicInfo) -> ! { |
| 180 | + core::arch::wasm32::unreachable() |
| 181 | +} |
0 commit comments