lifetime-extender 0.1.1

This crate is published for a severe bug that allow user write safe code to extend the lifetime of any object, which is unacceptable since such behavior may easily leads to UAF, double free and many UB. This crate should NOT used in the real life, since it relies on a BUG, not a feature.
Documentation
#![forbid(unsafe_code)] // WTF?
#![cfg_attr(doc, feature(doc_auto_cfg))]
//! # A Very unsafe lifetime extender with only safe Rust
//! This crate is published for a severe bug that allow user write safe code to extend the lifetime
//! of any object, which is unacceptable since such behavior may easily leads to UAF, double free
//! and many UB. This crate should NOT used in the real life, since it relies on a BUG, not a
//! feature.
//!
//! Here, 2 functions are provided, one is `extend`, which could extend any borrow's lifetime to
//! `'static` (should be the longest lifetime), and another is the mutable version `extend_mut`,
//! which could extend the mutable borrows. The former one may easily leads to UAF, and the second
//! one
//!
//! The crate uses `!#[forbid(unsafe_code)]` to ensure only safe rust is used.
//!
//! All the functions are ZCA, which could be directly optimized out.
//!
//! By default, all functions are hidden since you should use none of them. You could test them
//! with the related feature (in case some of them is fixed and others have not.)
//! Using the most dangerous `i-can-do-whatever-the-fxxk-i-want` feature gate would enable all
//! the features.
/// extend the current lifetime to `'static`.
#[inline(always)]
#[cfg(any(feature = "test-borrow", doc))]
pub fn extend<'a, T>(input: &'a T) -> &'static T {
    struct Bounded<'a, 'b: 'static, T>(&'a T, [&'b (); 0]);
    let n: Box<dyn FnOnce(&T) -> Bounded<'static, '_, T>> = Box::new(|x| Bounded(x, []));
    n(input).0
}
/// extend the current mutable borrow's lifetime to `'static`
#[inline(always)]
#[cfg(any(feature = "test-borrow-mut", doc))]
pub fn extend_mut<'a, T>(input: &'a mut T) -> &'static mut T {
    struct Bounded<'a, 'b: 'static, T>(&'a mut T, [&'b (); 0]);
    let mut n: Box<dyn FnMut(&mut T) -> Bounded<'static, '_, T>> = Box::new(|x| Bounded(x, []));
    n(input).0
}
/// a similar function from fake-static, doing the same things as [`extend`]
#[inline(always)]
#[cfg(any(feature = "test-fake-static-borrow", doc))]
pub fn make_static<'a, T>(input: &'a T) -> &'static T {
    fn helper<'a, T>(_: [&'static &'a (); 0], v: &'a T) -> &'static T {
        v
    }
    let f: fn([&'static &(); 0], &T) -> &'static T = helper;
    f([], input) // we need not to create a &'a&'b unit directly.
}
/// a similar function from fake-static, doing the same things as [`extend_mut`]
#[inline(always)]
#[cfg(any(feature = "test-fake-static-borrow-mut", doc))]
pub fn make_static_mut<'a, T>(input: &'a mut T) -> &'static mut T {
    fn helper_mut<'a, T>(_: [&'static &'a (); 0], v: &'a mut T) -> &'static mut T {
        v
    }
    let f: fn([&'static &'static (); 0], &'a mut T) -> &'static mut T = helper_mut;
    // let f: fn([&'static&();0], &mut T) -> &'static mut T = helper_mut; // it also works
    f([], input)
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    #[should_panic]
    #[cfg(feature = "test-borrow-mut")]
    fn it_panics() {
        let mut a = vec![1, 2, 3, 4, 5, 6, 7, 8];
        let b = extend_mut(&mut a);
        drop(a);
        //b.push(0);// double free is not panic!
        panic!("{b:?} is still readable!");
    }
    #[test]
    #[should_panic]
    #[cfg(feature = "test-borrow")]
    fn uaf() {
        let a = vec![1, 2, 3, 4];
        let b = extend(&a);
        drop(a);
        assert_eq!(b, &[1, 2, 3, 4]);
    }
    #[test]
    #[should_panic]
    #[cfg(feature = "test-fake-static-borrow-mut")]
    fn it_panics_2() {
        let mut a = vec![1, 2, 3, 4, 5, 6, 7, 8];
        let b = make_static_mut(&mut a);
        drop(a);
        //b.push(0);// double free is not panic!
        panic!("{b:?} is still readable!");
    }
    #[test]
    #[should_panic]
    #[cfg(feature = "test-fake-static-borrow")]
    fn uaf_2() {
        let a = vec![1, 2, 3, 4];
        let b = make_static(&a);
        drop(a);
        assert_eq!(b, &[1, 2, 3, 4]);
    }
}