@@ -51,34 +51,36 @@ static mut ERROR: *const c_char = ptr::null();
5151static mut LIBRARIES : * const Libraries = ptr:: null ( ) ;
5252
5353unsafe fn invalid_handle ( library : * const c_void ) -> bool {
54- if LIBRARIES . is_null ( ) {
55- panic ! (
56- "`__wasm_set_libraries` should have been called during \
54+ unsafe {
55+ if LIBRARIES . is_null ( ) {
56+ panic ! (
57+ "`__wasm_set_libraries` should have been called during \
5758 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
59+ ) ;
60+ }
61+
62+ let library = library as * const Library ;
63+ if ( 0 ..( * LIBRARIES ) . count )
64+ . any ( |index| ( * LIBRARIES ) . libraries . add ( usize:: try_from ( index) . unwrap ( ) ) == library)
65+ {
66+ false
67+ } else {
68+ ERROR = c"invalid library handle" . as_ptr ( ) ;
69+ true
70+ }
6971 }
7072}
7173
7274/// # Safety
7375///
7476/// `library` must be a valid, not-yet-closed library pointer returned by
7577/// `dlopen`.
76- #[ no_mangle]
78+ #[ unsafe ( no_mangle) ]
7779pub unsafe extern "C" fn dlclose ( library : * mut c_void ) -> c_int {
78- if invalid_handle ( library) { -1 } else { 0 }
80+ unsafe { if invalid_handle ( library) { -1 } else { 0 } }
7981}
8082
81- #[ no_mangle]
83+ #[ unsafe ( no_mangle) ]
8284pub extern "C" fn dlerror ( ) -> * const c_char {
8385 unsafe {
8486 let value = ERROR ;
@@ -90,85 +92,89 @@ pub extern "C" fn dlerror() -> *const c_char {
9092/// # Safety
9193///
9294/// `name` must be a valid pointer to a null-terminated string.
93- #[ no_mangle]
95+ #[ unsafe ( no_mangle) ]
9496pub 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 \
97+ unsafe {
98+ if LIBRARIES . is_null ( ) {
99+ panic ! (
100+ "`__wasm_set_libraries` should have been called during \
98101 instantiation with a non-NULL value"
102+ ) ;
103+ }
104+
105+ if ( flags & !( RTLD_LAZY | RTLD_NOW | RTLD_GLOBAL ) ) != 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 ( ) ,
99116 ) ;
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 ( )
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+ }
125129 }
126130}
127131
128132/// # Safety
129133///
130134/// `library` must be a valid, not-yet-closed library pointer returned by
131135/// `dlopen`, and `name` must be a valid pointer to a null-terminated string.
132- #[ no_mangle]
136+ #[ unsafe ( no_mangle) ]
133137pub 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 ( )
138+ unsafe {
139+ if library as isize == RTLD_NEXT || library as isize == RTLD_DEFAULT {
140+ // TODO
141+ ERROR = c"dlsym RTLD_NEXT and RTLD_DEFAULT not yet supported" . as_ptr ( ) ;
142+ return ptr :: null ( ) ;
143+ }
144+ if invalid_handle ( library) {
145+ return ptr:: null ( ) ;
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+ }
162166 }
163167}
164168
165169/// # Safety
166170///
167171/// `libraries` must be a valid pointer to a `Libraries` object, and this
168172/// pointer must remain valid for the lifetime of the process.
169- #[ no_mangle]
173+ #[ unsafe ( no_mangle) ]
170174pub unsafe extern "C" fn __wasm_set_libraries ( libraries : * const Libraries ) {
171- LIBRARIES = libraries;
175+ unsafe {
176+ LIBRARIES = libraries;
177+ }
172178}
173179
174180#[ cfg( target_arch = "wasm32" ) ]
0 commit comments