From 35c71f1b471cefab08aca141bf725204ae9cea09 Mon Sep 17 00:00:00 2001 From: Smertig Date: Sun, 9 May 2021 13:07:01 +0300 Subject: [PATCH] Add test with misaligned allocation --- tests/CMakeLists.txt | 2 + ...XXXX - incorrect alignment calculation.cpp | 32 +++++++++ tests/regression_tests/simple/source/main.cpp | 69 ++++++++++--------- 3 files changed, 69 insertions(+), 34 deletions(-) create mode 100644 tests/regression_tests/simple/source/XXXX - incorrect alignment calculation.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 59927c90..98a2f27c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -78,6 +78,8 @@ function(sol2_create_basic_test test_target_name target_sol) PRIVATE ${target_sol}) target_compile_definitions(${test_target_name} PRIVATE SOL_ALL_SAFETIES_ON=1) + target_compile_definitions(${test_target_name} + PRIVATE SOL_TESTS_SIZEOF_VOID_P=${CMAKE_SIZEOF_VOID_P}) endfunction() add_subdirectory(inclusion) diff --git a/tests/regression_tests/simple/source/XXXX - incorrect alignment calculation.cpp b/tests/regression_tests/simple/source/XXXX - incorrect alignment calculation.cpp new file mode 100644 index 00000000..4f7fe580 --- /dev/null +++ b/tests/regression_tests/simple/source/XXXX - incorrect alignment calculation.cpp @@ -0,0 +1,32 @@ +#include + +#if SOL_TESTS_SIZEOF_VOID_P == 4 + +inline namespace sol2_regression_test_XXXX { + struct Test { + std::uint64_t dummy; + }; +} // namespace sol2_regression_test_XXXX + +unsigned int regression_XXXX() { + sol::state lua; + + static_assert(sizeof(Test) == 8); + static_assert(alignof(Test) == 8); + static_assert(sizeof(Test*) == 4); + static_assert(alignof(Test*) == 4); + + /// [sol2] An error occurred and panic has been invoked: aligned allocation of userdata block (data section) for 'sol2_regression_test_XXXX::Test' failed + /// Note: may not panic depending on alignment of local variable `alignment_shim` in sol::detail::aligned_space_for + lua["test"] = Test {}; + + return 0; +} + +#else + +unsigned int regression_XXXX() { + return 0; +} + +#endif diff --git a/tests/regression_tests/simple/source/main.cpp b/tests/regression_tests/simple/source/main.cpp index 234b46e3..aa59eba3 100644 --- a/tests/regression_tests/simple/source/main.cpp +++ b/tests/regression_tests/simple/source/main.cpp @@ -1,34 +1,35 @@ -#include - -using f_ptr = unsigned int(); - -extern unsigned int regression_1000(); -extern unsigned int regression_1008(); -extern unsigned int regression_1067(); -extern unsigned int regression_1072(); -extern unsigned int regression_1087(); -extern unsigned int regression_1095(); -extern unsigned int regression_1096(); -extern unsigned int regression_1149(); - -static f_ptr* const regression_tests_regressions[] - = { ®ression_1008, ®ression_1000, ®ression_1067, ®ression_1072, ®ression_1087, ®ression_1095, ®ression_1096, ®ression_1149 }; -static const int regression_tests_sizeof_regressions = sizeof(regression_tests_regressions) / sizeof(regression_tests_regressions[0]); - -int trampoline(f_ptr* f) { - try { - return f(); - } - catch (...) { - } - return 1; -} - -int main(int, char*[]) { - int r = 0; - for (std::size_t i = 0; i < regression_tests_sizeof_regressions; ++i) { - f_ptr* f = regression_tests_regressions[i]; - r += static_cast(trampoline(f) != 0u); - } - return r; -} +#include + +using f_ptr = unsigned int(); + +extern unsigned int regression_1000(); +extern unsigned int regression_1008(); +extern unsigned int regression_1067(); +extern unsigned int regression_1072(); +extern unsigned int regression_1087(); +extern unsigned int regression_1095(); +extern unsigned int regression_1096(); +extern unsigned int regression_1149(); +extern unsigned int regression_XXXX(); + +static f_ptr* const regression_tests_regressions[] + = { ®ression_1008, ®ression_1000, ®ression_1067, ®ression_1072, ®ression_1087, ®ression_1095, ®ression_1096, ®ression_1149, ®ression_XXXX }; +static const int regression_tests_sizeof_regressions = sizeof(regression_tests_regressions) / sizeof(regression_tests_regressions[0]); + +int trampoline(f_ptr* f) { + try { + return f(); + } + catch (...) { + } + return 1; +} + +int main(int, char*[]) { + int r = 0; + for (std::size_t i = 0; i < regression_tests_sizeof_regressions; ++i) { + f_ptr* f = regression_tests_regressions[i]; + r += static_cast(trampoline(f) != 0u); + } + return r; +}