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
use super::*;

use crate::{
    prefix_type::{PrefixRef, PrefixRefTrait},
    sabi_types::RRef,
};

/// Used to check the layout of modules returned by module-loading functions
/// exported by dynamic libraries.
///
/// Module-loading functions are declared with the [`export_root_module`] attribute.
///
/// [`export_root_module`]: ../attr.export_root_module.html
#[repr(C)]
#[derive(StableAbi)]
pub struct LibHeader {
    header: AbiHeader,
    root_mod_consts: RootModuleConsts,
    init_globals_with: InitGlobalsWith,
    module: LateStaticRef<PrefixRef<ErasedPrefix>>,
    constructor: extern "C" fn() -> RootModuleResult,
}

impl LibHeader {
    /// Constructs a LibHeader from the root module loader.
    ///
    /// # Safety
    ///
    /// The `PrefixRef<ErasedPrefix>` returned by the `constructor` function
    /// must have been transmuted from a `PrefixRef<M>`.
    pub const unsafe fn from_constructor<M>(
        constructor: extern "C" fn() -> RootModuleResult,
        check_layout: CheckTypeLayout,
    ) -> Self
    where
        M: RootModule,
    {
        Self {
            header: AbiHeader::VALUE,
            root_mod_consts: match check_layout {
                CheckTypeLayout::Yes => M::CONSTANTS,
                CheckTypeLayout::No => M::CONSTANTS_NO_ABI_INFO,
            },
            init_globals_with: INIT_GLOBALS_WITH,
            module: LateStaticRef::new(),
            constructor,
        }
    }

    /// Constructs a LibHeader from the module.
    pub fn from_module<M>(value: M) -> Self
    where
        M: RootModule,
    {
        Self {
            header: AbiHeader::VALUE,
            root_mod_consts: M::CONSTANTS,
            init_globals_with: INIT_GLOBALS_WITH,
            module: {
                let erased = unsafe { value.to_prefix_ref().cast::<ErasedPrefix>() };
                LateStaticRef::from_prefixref(erased)
            },
            constructor: GetAbortingConstructor::aborting_constructor,
        }
    }

    /// All the important constants of a `RootModule` for some erased type.
    pub const fn root_mod_consts(&self) -> &RootModuleConsts {
        &self.root_mod_consts
    }

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

    /// Gets the layout of the root module.
    ///
    /// This returns a None if the root module layout is not included
    /// because the `#[unsafe_no_layout_constant]`
    /// helper attribute was used on the function exporting the root module.
    pub const fn layout(&self) -> Option<&'static TypeLayout> {
        self.root_mod_consts.layout().into_option()
    }

    pub(super) fn initialize_library_globals(&self, globals: &'static Globals) {
        (self.init_globals_with.0)(globals);
    }

    fn check_version<M>(&self) -> Result<(), LibraryError>
    where
        M: RootModule,
    {
        let expected_version = M::VERSION_STRINGS.piped(VersionNumber::new)?;

        let actual_version = self.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: M::NAME,
                expected_version,
                actual_version,
            });
        }
        Ok(())
    }

    /// Checks that the library is compatible, returning the root module on success.
    ///
    /// It checks that these are compatible:
    ///
    /// - The version number of the library
    ///
    /// - The layout of the root module.
    ///
    /// # Warning
    ///
    /// If this function is called within a dynamic library,
    /// it must be called at or after the function that exports its root module is called.
    ///
    /// **DO NOT** call this in the static initializer of a dynamic library,
    /// since this library relies on setting up its global state before
    /// calling the root module loader.
    ///
    /// # Errors
    ///
    /// This returns these errors:
    ///
    /// - `LibraryError::ParseVersionError`:
    /// If the version strings in the library can't be parsed as version numbers,
    /// this can only happen if the version strings are manually constructed.
    ///
    /// - `LibraryError::IncompatibleVersionNumber`:
    /// If the version number of the library is incompatible.
    ///
    /// - `LibraryError::AbiInstability`:
    /// If the layout of the root module is not the expected one.
    ///
    /// - `LibraryError::RootModule` :
    /// If the root module initializer returned an error or panicked.
    ///
    ///
    pub fn init_root_module<M>(&self) -> Result<M, LibraryError>
    where
        M: RootModule,
    {
        self.check_version::<M>()?;
        self.check_layout::<M>()
    }

    /// Checks that the version number of the library is compatible,
    /// returning the root module on success.
    ///
    /// This function transmutes the root module type,
    /// without checking that the layout is compatible first.
    ///
    /// # Warning
    ///
    /// If this function is called within a dynamic library,
    /// it must be called at or after the function that exports its root module is called.
    ///
    /// **DO NOT** call this in the static initializer of a dynamic library,
    /// since this library relies on setting up its global state before
    /// calling the root module loader.
    ///
    /// # Safety
    ///
    /// The caller must ensure that `M` has the expected layout.
    ///
    /// # Errors
    ///
    /// This returns these errors:
    ///
    /// - `LibraryError::ParseVersionError`:
    /// If the version strings in the library can't be parsed as version numbers,
    /// this can only happen if the version strings are manually constructed.
    ///
    /// - `LibraryError::IncompatibleVersionNumber`:
    /// If the version number of the library is incompatible.
    ///
    /// - `LibraryError::RootModule` :
    /// If the root module initializer returned an error or panicked.
    ///
    ///
    pub unsafe fn init_root_module_with_unchecked_layout<M>(&self) -> Result<M, LibraryError>
    where
        M: RootModule,
    {
        self.check_version::<M>()?;
        unsafe { self.unchecked_layout() }.map_err(RootModuleError::into_library_error::<M>)
    }

    /// Checks that the layout of the `M` root module from the dynamic library is
    /// compatible with the expected layout.
    ///     
    /// # Errors
    ///
    /// This returns these errors:
    ///
    /// - `LibraryError::AbiInstability`:
    /// If the layout of the root module is not the expected one.
    ///
    /// - `LibraryError::RootModule` :
    /// If the root module initializer returned an error or panicked.
    ///
    pub fn ensure_layout<M>(&self) -> Result<(), LibraryError>
    where
        M: RootModule,
    {
        if let IsLayoutChecked::Yes(root_mod_layout) = self.root_mod_consts.layout() {
            // Using this instead of
            // crate::abi_stability::abi_checking::check_layout_compatibility
            // 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)(<M>::LAYOUT, root_mod_layout)
                .into_result()
                .map_err(|e| {
                    // Fixes the bug where printing the error causes a segfault because it
                    // contains static references and function pointers into the unloaded library.
                    //
                    // This isn't strictly required anymore because abi_stable doesn't
                    // unload libraries right now.
                    let formatted = e.to_formatted_error();
                    LibraryError::AbiInstability(formatted)
                })?;
        }

        atomic::compiler_fence(atomic::Ordering::SeqCst);

        Ok(())
    }

    /// Gets the root module,first
    /// checking that the layout of the `M` from the dynamic library is
    /// compatible with the expected layout.
    ///     
    /// # Errors
    ///
    /// This returns these errors:
    ///
    /// - `LibraryError::AbiInstability`:
    /// If the layout of the root module is not the expected one.
    ///
    /// - `LibraryError::RootModule` :
    /// If the root module initializer returned an error or panicked.
    ///
    pub fn check_layout<M>(&self) -> Result<M, LibraryError>
    where
        M: RootModule,
    {
        self.ensure_layout::<M>()?;

        unsafe {
            self.unchecked_layout()
                .map_err(RootModuleError::into_library_error::<M>)
        }
    }

    /// Gets the root module without checking that the layout of `M` is the expected one.
    /// This is effectively a transmute.
    ///
    /// This is useful if a user keeps a cache of which dynamic libraries
    /// have been checked for layout compatibility.
    ///
    /// # Safety
    ///
    /// The caller must ensure that `M` has the expected layout.
    ///
    /// # Errors
    ///
    /// This function can return a `RootModuleError`
    /// because the root module failed to initialize.
    ///
    pub unsafe fn unchecked_layout<M>(&self) -> Result<M, RootModuleError>
    where
        M: PrefixRefTrait,
    {
        let reff = self
            .module
            .try_init(|| (self.constructor)().into_result())
            .map_err(|mut err| {
                // Making sure that the error doesn't contain references into
                // the unloaded library.
                //
                // This isn't strictly required anymore because abi_stable doesn't
                // unload libraries right now.
                err.reallocate();
                err
            })?;
        unsafe { Ok(M::from_prefix_ref(reff.cast::<M::PrefixFields>())) }
    }
}

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

struct GetAbortingConstructor<T>(T);

impl<T> GetAbortingConstructor<T> {
    extern "C" fn aborting_constructor() -> T {
        extern_fn_panic_handling! {
            panic!(
                "BUG:\n\
                 This function \
                 (abi_stable::library::lib_header::GetAbortingConstructor::aborting_constructor) \
                 must only be used \
                 as a dummy functions when initializing `LibHeader` \
                 within `LibHeader::from_module`."
            );
        }
    }
}

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

#[repr(C)]
#[derive(StableAbi, Copy, Clone)]
struct InitGlobalsWith(pub extern "C" fn(&'static Globals));

const INIT_GLOBALS_WITH: InitGlobalsWith = InitGlobalsWith(crate::globals::initialize_globals_with);

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

/// A handle to the [`AbiHeader`] of a library.
///
/// This can be dereferenced into the `AbiHeader`,
/// and [fallibly upgraded](#method.upgrade) into a `&'static LibHeader`.
///
/// [`AbiHeader`]: ./struct.AbiHeader.html
#[repr(transparent)]
#[derive(Debug, StableAbi, Copy, Clone)]
pub struct AbiHeaderRef(pub(super) RRef<'static, AbiHeader>);

impl std::ops::Deref for AbiHeaderRef {
    type Target = AbiHeader;

    fn deref(&self) -> &AbiHeader {
        self.0.get()
    }
}

/// Represents the abi_stable version used by a compiled dynamic library,
/// which if incompatible would produce a [`LibraryError::InvalidAbiHeader`]
///
/// [`LibraryError::InvalidAbiHeader`]: ./enum.LibraryError.html#variant.InvalidAbiHeader
#[repr(C)]
#[derive(Debug, StableAbi, Copy, Clone)]
// This type will never have new fields clippy, that's the point <_<
#[allow(clippy::manual_non_exhaustive)]
pub struct AbiHeader {
    /// A magic string used to check that this is actually abi_stable.
    pub magic_string: [u8; 32],
    /// The major abi version of abi_stable
    pub abi_major: u32,
    /// The minor abi version of abi_stable
    pub abi_minor: u32,
    _priv: (),
}

impl AbiHeader {
    /// The value of the AbiHeader stored in dynamic libraries that use this
    /// version of abi_stable
    pub const VALUE: AbiHeader = {
        mod value {
            use super::*;
            abi_stable_derive::construct_abi_header! {}
        }
        value::ABI_HEADER
    };
}

impl AbiHeader {
    /// Checks whether this AbiHeader is compatible with `other`.
    pub fn is_compatible(&self, other: &Self) -> bool {
        self.magic_string == other.magic_string
            && self.abi_major == other.abi_major
            && (self.abi_major != 0 || self.abi_minor == other.abi_minor)
    }

    /// Checks whether the abi_stable version of this AbiHeader is
    /// compatible with the one from where this function is called.
    pub fn is_valid(&self) -> bool {
        self.is_compatible(&AbiHeader::VALUE)
    }
}

impl AbiHeaderRef {
    /// Gets the LibHeader of a library.
    ///
    /// # Errors
    ///
    /// This returns these errors:
    ///
    /// - `LibraryError::InvalidAbiHeader`:
    /// If the abi_stable used by the library is not compatible.
    ///
    /// - `LibraryError::InvalidCAbi`:
    /// If the C abi used by the library is not compatible.
    pub fn upgrade(self) -> Result<&'static LibHeader, LibraryError> {
        if !self.is_valid() {
            return Err(LibraryError::InvalidAbiHeader(*self));
        }

        let lib_header: &'static LibHeader = unsafe { self.0.transmute_into_ref() };

        let c_abi_testing_fns = lib_header.root_mod_consts().c_abi_testing_fns();
        crate::library::c_abi_testing::run_tests(c_abi_testing_fns)?;

        let globals = globals::initialized_globals();

        lib_header.initialize_library_globals(globals);

        Ok(lib_header)
    }
}