Debian Codegraph @

crates/wit-component/dl/src/lib.rs

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