Lutok 0.7.1
Loading...
Searching...
No Matches
state_unwinding_test.cpp File Reference

These tests check that Lua errors do not bypass C++ destructors in callbacks. More...

#include <atf-c++.hpp>
#include "lutok/exceptions.hpp"
#include "lutok/state.hpp"

Classes

struct  anonymous_namespace{state_unwinding_test.cpp}::destruction_guard

Functions

void anonymous_namespace{state_unwinding_test.cpp}::check_error_unwinds_cpp (lutok::cxx_function callback, const char *script, const char *expected_message="fatal")
 ATF_TEST_CASE_WITHOUT_HEAD (open_string__destructor_longjmp)
 ATF_TEST_CASE_BODY (open_string__destructor_longjmp)
 ATF_TEST_CASE_WITHOUT_HEAD (open_table__destructor_longjmp)
 ATF_TEST_CASE_BODY (open_table__destructor_longjmp)
 ATF_TEST_CASE_WITHOUT_HEAD (open_all__destructor_longjmp)
 ATF_TEST_CASE_BODY (open_all__destructor_longjmp)
 ATF_TEST_CASE_WITHOUT_HEAD (get_global__destructor_longjmp)
 ATF_TEST_CASE_BODY (get_global__destructor_longjmp)
 ATF_TEST_CASE_WITHOUT_HEAD (set_global__destructor_longjmp)
 ATF_TEST_CASE_BODY (set_global__destructor_longjmp)
 ATF_TEST_CASE_WITHOUT_HEAD (get_table__destructor_longjmp)
 ATF_TEST_CASE_BODY (get_table__destructor_longjmp)
 ATF_TEST_CASE_WITHOUT_HEAD (set_table__destructor_longjmp)
 ATF_TEST_CASE_BODY (set_table__destructor_longjmp)
 ATF_TEST_CASE_WITHOUT_HEAD (pcall__destructor_longjmp)
 ATF_TEST_CASE_BODY (pcall__destructor_longjmp)
 ATF_TEST_CASE_WITHOUT_HEAD (raw_set__destructor_longjmp)
 ATF_TEST_CASE_BODY (raw_set__destructor_longjmp)
 ATF_INIT_TEST_CASES (tcs)

Variables

int anonymous_namespace{state_unwinding_test.cpp}::destroyed
const char * anonymous_namespace{state_unwinding_test.cpp}::reject_global_write

Detailed Description

These tests check that Lua errors do not bypass C++ destructors in callbacks.

With Lua using longjmp for error propagation, an unprotected Lua API call can jump to an outer lua_pcall without unwinding the intervening C++ frames. Bypassing non-trivial destructors of automatic objects this way is undefined behavior under [csetjmp.syn]/2 of the C++ standard.

Each test invokes a C++ callback from Lua. The callback creates a guard whose destructor increments a counter, then performs an operation that raises a Lua error. We check both that the outer call reports the expected error and that the guard was destroyed exactly once. Protected operations should pass, operations that let longjmp bypass the guard fail the check or may crash.

Please note that this suite doesn't test memory allocation failures.