1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*!
Traits and types related to loading an abi_stable dynamic library,
as well as functions/modules within.
*/

use std::{
    fmt::{self, Display},
    io,
    path::{Path,PathBuf},
    sync::atomic,
};

use core_extensions::prelude::*;

use libloading::{
    Library as LibLoadingLibrary,
    Symbol as LLSymbol,
};

use abi_stable_derive_lib::{
    mangle_library_getter_ident,
    mangle_initialize_globals_with_ident,
};



use crate::{
    abi_stability::{
        AbiInfoWrapper,
        stable_abi_trait::SharedStableAbi,
    },
    globals::{self,InitializeGlobalsWithFn},
    lazy_static_ref::LazyStaticRef,
    prefix_type::PrefixTypeTrait,
    version::{ParseVersionError, VersionNumber, VersionStrings},
    utils::leak_value,
    std_types::{RVec,RBoxError},
};


/// A handle to any dynamically loaded library,
/// not necessarily ones that export abi_stable compatible modules.
#[derive(Copy, Clone)]
pub struct Library {
    path:&'static Path,
    library: &'static LibLoadingLibrary,
}


/// What naming convention to expect when loading a library from a directory.
#[derive(Debug,Copy,Clone,PartialEq,Eq,Ord,PartialOrd,Hash)]
pub enum LibrarySuffix{
    /// Loads a dynamic library at `<folder>/<base_name>.extension`
    NoSuffix,
    
    /// Loads a dynamic library at `<folder>/<base_name>-<pointer_size>.<extension>`
    Suffix,
}


impl Library {
    /// Gets the full path a library would be loaded from,
    pub fn get_library_path(
        folder: &Path,
        base_name: &str,
        suffix:LibrarySuffix,
    )->PathBuf{
        let formatted:String;

        let (prefix,extension) = match (cfg!(windows), cfg!(target_os="macos")) {
            (false, false) => ("lib","so"),
            (false, true) => ("lib","dylib"),
            (true, false) => ("","dll"),
            _ => unreachable!("system is both windows and mac"),
        };
        
        let is_64_bits =
            cfg!(any(x86_64, powerpc64, aarch64)) || ::std::mem::size_of::<usize>() == 8;
        let bits = if is_64_bits { "64" } else { "32" };

        let maybe_suffixed_name=match suffix {
            LibrarySuffix::NoSuffix=>{
                formatted=format!("{}-{}", base_name, bits);
                &*formatted
            }
            LibrarySuffix::Suffix=>{
                base_name
            }
        };

        let name=format!("{}{}.{}",prefix, maybe_suffixed_name, extension);
        folder.join(name)
    }

    /// Loads the dynamic library at the `full_path` path.
    pub fn load_at(full_path:&Path) -> Result<&'static Self,LibraryError> {
        LibLoadingLibrary::new(full_path)
            .map_err(|io|{
                LibraryError::OpenError{ path:full_path.to_owned(), io }
            })?
            .piped(leak_value)
            .piped(|library| Self { path:leak_value(full_path.to_owned()), library })
            .piped(leak_value)
            .piped(Ok)
    }

    /// Loads the dynamic library from the `folder`.
    /// 
    /// The full filename of the library is determined by `suffix`.
    pub fn load_in(
        folder: &Path,
        base_name: &str,
        suffix:LibrarySuffix,
    ) -> Result<&'static Self,LibraryError> {
        let path=Self::get_library_path(folder,base_name,suffix);
        Self::load_at(&path)
    }

    /// Gets access to a static/function declared by the library.
    ///
    /// # Safety
    ///
    /// Passing a `T` of a type different than the compiled library declared is
    /// undefined behavior.
    ///
    ///
    ///
    unsafe fn get_static<T>(
        &self, 
        symbol_name: &[u8]
    ) -> Result<LLSymbol<'static,T>,LibraryError> 
    where T:'static
    {
        match self.library.get::<T>(symbol_name) {
            Ok(symbol)=>Ok(symbol),
            Err(io)=>{
                let symbol=symbol_name.to_owned();
                Err(LibraryError::GetSymbolError{ 
                    library:self.path.clone(),
                    symbol, 
                    io 
                })
            }
        }
    }
}

//////////////////////////////////////////////////////////////////////

/// A type alias for a function that exports a module 
/// (a struct of function pointers that implements RootModule).
pub type LibraryGetterFn<T>=
    extern "C" fn() -> WithLayout<T>;

//////////////////////////////////////////////////////////////////////


/// The root module of a dynamic library,
/// which may contain other modules,function pointers,and static references.
///
/// For an example of a type implementing this trait you can look 
/// for the `example/example_*_interface` crates  in this crates' repository .
pub trait RootModule: Sized+SharedStableAbi  {

    /// The late-initialized reference to the Library handle.
    fn raw_library_ref()->&'static LazyStaticRef<Library>;

    /// The base name of the dynamic library,which is the same on all platforms.
    /// This is generally the name of the `implementation crate`.
    const BASE_NAME: &'static str;

    /// The name of the library used in error messages.
    const NAME: &'static str;

    /// The version number of this library.
    /// 
    /// Initialize this with ` package_version_strings!() `
    const VERSION_STRINGS: VersionStrings;

    /// The name of the function which constructs this module.
    ///
    /// The function signature for the loader is:
    ///
    /// `extern "C" fn()->WithLayout<Self>`
    const LOADER_FN: &'static str;

    /// Returns the path the library would be loaded from.
    fn get_library_path(directory:&Path)-> PathBuf {
        let base_name=Self::BASE_NAME;
        Library::get_library_path(directory, base_name,LibrarySuffix::Suffix)
    }

    /// Loads this module from the library in the `directory` directory,
    /// first loading the dynamic library from the `directory` if it wasn't already loaded.
    fn load_from_library_in(directory: &Path) -> Result<&'static Self, LibraryError>{
        Self::raw_library_ref()
            .try_init(||{
                let path=Self::get_library_path(directory);
                // println!("loading library at:\n\t{}\n",path.display());
                Library::load_at(&path) 
            })
            .and_then(Self::load_with)
    }
    
    /// Loads this module from the library at the `full_path` path,
    /// first loading the dynamic library from the `directory` if it wasn't already loaded.
    fn load_from_library_at(full_path: &Path) -> Result<&'static Self, LibraryError>{
        Self::raw_library_ref()
            .try_init(|| Library::load_at(full_path)  )
            .and_then(Self::load_with)
    }

    /// Loads this module from the `raw_library`.
    fn load_with(raw_library:&'static Library)->Result<&'static Self,LibraryError>{

        let library_getter: LLSymbol<'static,LibraryGetterFn<Self>> =unsafe{
            let mut mangled=mangle_library_getter_ident(Self::LOADER_FN);
            mangled.push('\0');
            raw_library.get_static::<LibraryGetterFn<Self>>(mangled.as_bytes())?
        };
        

        let initialize_globals_with: LLSymbol<'static,InitializeGlobalsWithFn>=unsafe{
            let mut mangled=mangle_initialize_globals_with_ident(Self::LOADER_FN);
            mangled.push('\0');
            raw_library.get_static::<InitializeGlobalsWithFn>(mangled.as_bytes())?
        };
        

        let globals=globals::initialized_globals();
        

        // This has to run before anything else.
        initialize_globals_with(globals);
        
        
        let items = library_getter();
        
        
        let expected_version = Self::VERSION_STRINGS
            .piped(VersionNumber::new)?;
        let actual_version = items.version_strings().piped(VersionNumber::new)?;

        if expected_version.major != actual_version.major || 
            (expected_version.major==0) && expected_version.minor > actual_version.minor
        {
            return Err(LibraryError::IncompatibleVersionNumber {
                library_name: Self::NAME,
                expected_version,
                actual_version,
            });
        }

        items.check_layout()?
            .initialization()
    }

    /// Defines behavior that happens once the module is loaded.
    fn initialization(self: &'static Self) -> Result<&'static Self, LibraryError> {
        Ok(self)
    }
}


//////////////////////////////////////////////////////////////////////

mod with_layout {
    use super::*;

    /// Used to check the layout of modules returned by module-loading functions
    /// exported by dynamic libraries.
    #[repr(C)]
    #[derive(StableAbi)]
    #[sabi(inside_abi_stable_crate)]
    pub struct WithLayout <T:'static>{
        magic_number: usize,

        version_strings:VersionStrings,
        layout: &'static AbiInfoWrapper,
        value: &'static T,
    }

    impl<T> WithLayout<T> {
        /// Constructs a WithLayout from the `Type_Prefix` struct of a type 
        /// deriving `StableAbi` with 
        /// `#[sabi(kind(Prefix(prefix_struct="Type_Prefix" )))]`.
        pub fn from_prefix(ref_:&'static T)->Self
        where
            T: RootModule,
        {
            Self {
                magic_number: MAGIC_NUMBER,
                version_strings:T::VERSION_STRINGS,
                layout: <&T>::S_ABI_INFO,
                value:ref_,
            }
        }

        /// Constructs a WithLayout from the 
        /// type deriving `StableAbi` with `#[sabi(kind(Prefix(..)))]`,
        /// leaking the value in the process.
        pub fn new<M>(value:M) -> Self
        where
            M:PrefixTypeTrait<Prefix=T>+'static,
            T: RootModule,
        {
            // println!("constructing a WithLayout");
                        
            value.leak_into_prefix()
                .piped(Self::from_prefix)
        }

        /// The version string of the library the module is being loaded from.
        pub fn version_strings(&self)->VersionStrings{
            self.version_strings
        }

        /// Checks that the layout of the `T` from the dynamic library is 
        /// compatible with the caller's .
        pub fn check_layout(self) -> Result<&'static T, LibraryError>
        where
            T: RootModule,
        {
            if self.magic_number != MAGIC_NUMBER {
                return Err(LibraryError::InvalidMagicNumber(self.magic_number));
            }

            // Using this instead of
            // crate::abi_stability::abi_checking::check_abi_stability
            // so that if this is called in a dynamic-library that loads 
            // another dynamic-library,
            // it uses the layout checker of the executable,
            // ensuring a globally unique view of the layout of types.
            //
            // This might also reduce the code in the library,
            // because it doesn't have to compile the layout checker for every library.
            (globals::initialized_globals().layout_checking)
                (<&T>::S_ABI_INFO, self.layout)
                .into_result()
                .map_err(LibraryError::AbiInstability)?;
            
            atomic::compiler_fence(atomic::Ordering::SeqCst);
            
            Ok(self.value)
        }
    }

}

pub use self::with_layout::WithLayout;

// ABI version 0.2
// Format:
// ABI_(A for pre-1.0 version number ,B for major version number)_(version number)
const MAGIC_NUMBER: usize = 0xAB1_A_0002;

//////////////////////////////////////////////////////////////////////


/// All the possible errors that could happen when loading a library,
/// or a module.
#[derive(Debug)]
pub enum LibraryError {
    /// When a library can't be loaded, because it doesn't exist.
    OpenError{
        path:PathBuf,
        io:io::Error,
    },
    /// When a function/static does not exist.
    GetSymbolError{
        library:&'static Path,
        /// The name of the function/static.Does not have to be utf-8.
        symbol:Vec<u8>,
        io:io::Error,
    },
    /// The version string could not be parsed into a version number.
    ParseVersionError(ParseVersionError),
    /// The version numbers of the library was incompatible.
    IncompatibleVersionNumber {
        library_name: &'static str,
        expected_version: VersionNumber,
        actual_version: VersionNumber,
    },
    /// The abi is incompatible.
    /// The error is opaque,since the error always comes from the main binary
    /// (dynamic libraries can be loaded from other dynamic libraries),
    /// and no approach for extensible enums is settled on yet.
    AbiInstability(RBoxError),
    /// The magic number used to check that this is a compatible abi_stable
    /// is not the same.
    InvalidMagicNumber(usize),
    /// There could have been 0 or more errors in the function.
    Many(RVec<Self>),
}

impl From<ParseVersionError> for LibraryError {
    fn from(v: ParseVersionError) -> LibraryError {
        LibraryError::ParseVersionError(v)
    }
}

impl Display for LibraryError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("\n")?;
        match self {
            LibraryError::OpenError{path,io} => writeln!(
                f,
                "Could not open library at:\n\t{}\nbecause:\n\t{}",
                path.display(),io
            ),
            LibraryError::GetSymbolError{library,symbol,io} => writeln!(
                f,
                "Could load symbol:\n\t{}\nin library:\n\t{}\nbecause:\n\t{}",
                String::from_utf8_lossy(symbol),
                library.display(),
                io
            ),
            LibraryError::ParseVersionError(x) => fmt::Display::fmt(x, f),
            LibraryError::IncompatibleVersionNumber {
                library_name,
                expected_version,
                actual_version,
            } => writeln!(
                f,
                "\n'{}' library version mismatch:\nuser:{}\nlibrary:{}",
                library_name, expected_version, actual_version,
            ),
            LibraryError::AbiInstability(x) => fmt::Display::fmt(x, f),
            LibraryError::InvalidMagicNumber(found) => write!(
                f,
                "magic number used to load a library was {},when this library expected {}",
                found, MAGIC_NUMBER,
            ),
            LibraryError::Many(list)=>{
                for e in list {
                    Display::fmt(e,f)?;
                }
                Ok(())
            }
        }?;
        f.write_str("\n")?;
        Ok(())
    }
}

impl ::std::error::Error for LibraryError {}