ext_php_rs/zend/
try_catch.rs

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
use crate::ffi::{
    ext_php_rs_zend_bailout, ext_php_rs_zend_first_try_catch, ext_php_rs_zend_try_catch,
};
use std::ffi::c_void;
use std::panic::{catch_unwind, resume_unwind, RefUnwindSafe};
use std::ptr::null_mut;

#[derive(Debug)]
pub struct CatchError;

pub(crate) unsafe extern "C" fn panic_wrapper<R, F: FnMut() -> R + RefUnwindSafe>(
    ctx: *const c_void,
) -> *const c_void {
    // we try to catch panic here so we correctly shutdown php if it happens
    // mandatory when we do assert on test as other test would not run correctly
    let panic = catch_unwind(|| (*(ctx as *mut F))());

    Box::into_raw(Box::new(panic)) as *mut c_void
}

/// PHP propose a try catch mechanism in C using setjmp and longjmp (bailout)
/// It store the arg of setjmp into the bailout field of the global executor
/// If a bailout is triggered, the executor will jump to the setjmp and restore
/// the previous setjmp
///
/// try_catch allow to use this mechanism
///
/// # Returns
///
/// * `Ok(R)` - The result of the function
/// * `Err(CatchError)` - A bailout occurred during the execution
pub fn try_catch<R, F: FnMut() -> R + RefUnwindSafe>(func: F) -> Result<R, CatchError> {
    do_try_catch(func, false)
}

/// PHP propose a try catch mechanism in C using setjmp and longjmp (bailout)
/// It store the arg of setjmp into the bailout field of the global executor
/// If a bailout is triggered, the executor will jump to the setjmp and restore
/// the previous setjmp
///
/// try_catch_first allow to use this mechanism
///
/// This functions differs from ['try_catch'] as it also initialize the bailout
/// mechanism for the first time
///
/// # Returns
///
/// * `Ok(R)` - The result of the function
/// * `Err(CatchError)` - A bailout occurred during the execution
pub fn try_catch_first<R, F: FnMut() -> R + RefUnwindSafe>(func: F) -> Result<R, CatchError> {
    do_try_catch(func, true)
}

fn do_try_catch<R, F: FnMut() -> R + RefUnwindSafe>(func: F, first: bool) -> Result<R, CatchError> {
    let mut panic_ptr = null_mut();
    let has_bailout = unsafe {
        if first {
            ext_php_rs_zend_first_try_catch(
                panic_wrapper::<R, F>,
                &func as *const F as *const c_void,
                (&mut panic_ptr) as *mut *mut c_void,
            )
        } else {
            ext_php_rs_zend_try_catch(
                panic_wrapper::<R, F>,
                &func as *const F as *const c_void,
                (&mut panic_ptr) as *mut *mut c_void,
            )
        }
    };

    let panic = panic_ptr as *mut std::thread::Result<R>;

    // can be null if there is a bailout
    if panic.is_null() || has_bailout {
        return Err(CatchError);
    }

    match unsafe { *Box::from_raw(panic as *mut std::thread::Result<R>) } {
        Ok(r) => Ok(r),
        Err(err) => {
            // we resume the panic here so it can be catched correctly by the test framework
            resume_unwind(err);
        }
    }
}

/// Trigger a bailout
///
/// This function will stop the execution of the current script
/// and jump to the last try catch block
///
/// # Safety
///
/// This function is unsafe because it can cause memory leaks
/// Since it will jump to the last try catch block, it will not call the
/// destructor of the current scope
///
/// When using this function you should ensure that all the memory allocated in
/// the current scope is released
pub unsafe fn bailout() -> ! {
    ext_php_rs_zend_bailout();
}

#[cfg(feature = "embed")]
#[cfg(test)]
mod tests {
    use crate::embed::Embed;
    use crate::zend::{bailout, try_catch};
    use std::ptr::null_mut;

    #[test]
    fn test_catch() {
        Embed::run(|| {
            let catch = try_catch(|| {
                unsafe {
                    bailout();
                }

                #[allow(unreachable_code)]
                {
                    assert!(false);
                }
            });

            assert!(catch.is_err());
        });
    }

    #[test]
    fn test_no_catch() {
        Embed::run(|| {
            let catch = try_catch(|| {
                assert!(true);
            });

            assert!(catch.is_ok());
        });
    }

    #[test]
    fn test_bailout() {
        Embed::run(|| {
            unsafe {
                bailout();
            }

            #[allow(unreachable_code)]
            {
                assert!(false);
            }
        });
    }

    #[test]
    #[should_panic]
    fn test_panic() {
        Embed::run(|| {
            let _ = try_catch(|| {
                panic!("should panic");
            });
        });
    }

    #[test]
    fn test_return() {
        let foo = Embed::run(|| {
            let result = try_catch(|| {
                return "foo";
            });

            assert!(result.is_ok());

            result.unwrap()
        });

        assert_eq!(foo, "foo");
    }

    #[test]
    fn test_memory_leak() {
        let mut ptr = null_mut();

        let _ = try_catch(|| {
            let mut result = "foo".to_string();
            ptr = &mut result;

            unsafe {
                bailout();
            }
        });

        // Check that the string is never released
        let result = unsafe { &*ptr as &str };

        assert_eq!(result, "foo");
    }
}