From c81e86ef7168376c81ee2bd265de6973630efbbc Mon Sep 17 00:00:00 2001 From: halx99 Date: Tue, 2 Dec 2025 00:26:26 +0800 Subject: [PATCH 1/4] Refactor signal handling in ContainerTests Refactor signal handling macros and improve exception safety in tests. --- .../Source/axmol/tlx/ContainerTests.cpp | 45 +++++++++++-------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/tests/unit-tests/Source/axmol/tlx/ContainerTests.cpp b/tests/unit-tests/Source/axmol/tlx/ContainerTests.cpp index cd8f353abd3..95ccbfa8634 100644 --- a/tests/unit-tests/Source/axmol/tlx/ContainerTests.cpp +++ b/tests/unit-tests/Source/axmol/tlx/ContainerTests.cpp @@ -25,7 +25,7 @@ #if defined(_WIN32) || defined(_WIN64) -# define __TRY(SIGID) if (true) +# define __TRY if (true) # define __CATCH else # define __FINALLY if (true) # define __ENDTRY @@ -51,7 +51,10 @@ void __doctest_signal_handler(int sig) sa.sa_handler = __doctest_signal_handler; \ sigemptyset(&sa.sa_mask); \ sa.sa_flags = 0; \ - sigaction(__sig_num, &sa, nullptr); \ + for (int i = 1; i < NSIG; ++i) { \ + if (i == SIGKILL || i == SIGSTOP) continue; \ + sigaction(i, &sa, nullptr); \ + } \ int __ret = sigsetjmp(__doctest_jumpbuf, 1); \ if (__ret == 0) @@ -63,7 +66,10 @@ void __doctest_signal_handler(int sig) sa_default.sa_handler = SIG_DFL; \ sigemptyset(&sa_default.sa_mask); \ sa_default.sa_flags = 0; \ - sigaction(__sig_num, &sa_default, nullptr); \ + for (int i = 1; i < NSIG; ++i) { \ + if (i == SIGKILL || i == SIGSTOP) continue; \ + sigaction(__sig_num, &sa_default, nullptr); \ + } \ } # define __ENDTRY \ @@ -255,12 +261,28 @@ TEST_SUITE("tlx/Containers") CHECK((arr3[0].value == 0 && arr3[1].value == 0)); // while, if you run unittest with Xcode debugger, the debugger still raise exception - __TRY(SIGILL) + __TRY { // pod vector, no auto fill when dtor is trivial tlx::pod_vector arr4; arr4.resize(2); CHECK((arr4[0].value != 123 && arr4[1].value != 123 && arr4.size() == 2)); + + tlx::pod_vector arr5; + arr5.resize(2); + +#ifndef __APPLE__ + CHECK((arr5[0].value != 0 && arr5[1].value != 0)); +#endif + // we can safe access initialized member without exception catch + arr5.resize(4, TrivalCtor1{39}); + CHECK((arr5[2].value == 39 && arr5[3].value == 39)); + + arr5.resize(128, TrivalCtor1{66}); + CHECK((arr5[2].value == 39 && arr5[3].value == 39)); + + CHECK((arr5[10].value == 66 && arr5[22].value == 66)); + AXLOGI("Access uninitialzed object membmer done (non optimized build or non-Apple platforms)"); } __CATCH @@ -273,21 +295,6 @@ TEST_SUITE("tlx/Containers") } __ENDTRY; - tlx::pod_vector arr5; - arr5.resize(2); - -#ifndef __APPLE__ - CHECK((arr5[0].value != 0 && arr5[1].value != 0)); -#endif - // we can safe access initialized member without exception catch - arr5.resize(4, TrivalCtor1{39}); - CHECK((arr5[2].value == 39 && arr5[3].value == 39)); - - arr5.resize(128, TrivalCtor1{66}); - CHECK((arr5[2].value == 39 && arr5[3].value == 39)); - - CHECK((arr5[10].value == 66 && arr5[22].value == 66)); - // shoud report compile error, non trivial dtors types can't use tlx::pod_vector // tlx::pod_vector arr6; From 3afdc10901fefe8fa9c2de40bb15531f98ed401e Mon Sep 17 00:00:00 2001 From: halx99 Date: Tue, 2 Dec 2025 00:26:48 +0800 Subject: [PATCH 2/4] Fix signal handling in ContainerTests.cpp --- tests/unit-tests/Source/axmol/tlx/ContainerTests.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit-tests/Source/axmol/tlx/ContainerTests.cpp b/tests/unit-tests/Source/axmol/tlx/ContainerTests.cpp index 95ccbfa8634..ae35ced9037 100644 --- a/tests/unit-tests/Source/axmol/tlx/ContainerTests.cpp +++ b/tests/unit-tests/Source/axmol/tlx/ContainerTests.cpp @@ -68,7 +68,7 @@ void __doctest_signal_handler(int sig) sa_default.sa_flags = 0; \ for (int i = 1; i < NSIG; ++i) { \ if (i == SIGKILL || i == SIGSTOP) continue; \ - sigaction(__sig_num, &sa_default, nullptr); \ + sigaction(i, &sa_default, nullptr); \ } \ } From 9a3f31bfea9ee24aeb91b0b3480ed5fc2842ab6c Mon Sep 17 00:00:00 2001 From: halx99 Date: Tue, 2 Dec 2025 00:27:32 +0800 Subject: [PATCH 3/4] Log caught signals in doctest signal handler --- tests/unit-tests/Source/axmol/tlx/ContainerTests.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/unit-tests/Source/axmol/tlx/ContainerTests.cpp b/tests/unit-tests/Source/axmol/tlx/ContainerTests.cpp index ae35ced9037..199cb14a7f9 100644 --- a/tests/unit-tests/Source/axmol/tlx/ContainerTests.cpp +++ b/tests/unit-tests/Source/axmol/tlx/ContainerTests.cpp @@ -39,14 +39,13 @@ static thread_local sigjmp_buf __doctest_jumpbuf; void __doctest_signal_handler(int sig) { - if (sig != SIGKILL) - ::siglongjmp(__doctest_jumpbuf, 1); // jump back to safe point + AXLOGI("Caught signal: {}", sig); + ::siglongjmp(__doctest_jumpbuf, 1); // jump back to safe point } # define __TRY(sig_num) \ do \ { \ - const auto __sig_num = sig_num; \ struct sigaction sa{}; \ sa.sa_handler = __doctest_signal_handler; \ sigemptyset(&sa.sa_mask); \ From f5cbaa6452aa2fcfa260b2395eaf218ad8b2af02 Mon Sep 17 00:00:00 2001 From: halx99 Date: Tue, 2 Dec 2025 01:02:46 +0800 Subject: [PATCH 4/4] fixup --- .../Source/axmol/tlx/ContainerTests.cpp | 44 ++++++++++--------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/tests/unit-tests/Source/axmol/tlx/ContainerTests.cpp b/tests/unit-tests/Source/axmol/tlx/ContainerTests.cpp index 199cb14a7f9..a18284d4fbe 100644 --- a/tests/unit-tests/Source/axmol/tlx/ContainerTests.cpp +++ b/tests/unit-tests/Source/axmol/tlx/ContainerTests.cpp @@ -25,9 +25,9 @@ #if defined(_WIN32) || defined(_WIN64) -# define __TRY if (true) -# define __CATCH else -# define __FINALLY if (true) +# define __TRY if (true) +# define __CATCH else +# define __FINALLY if (true) # define __ENDTRY #else // POSIX / Unix-like @@ -43,32 +43,36 @@ void __doctest_signal_handler(int sig) ::siglongjmp(__doctest_jumpbuf, 1); // jump back to safe point } -# define __TRY(sig_num) \ +# define __TRY \ do \ { \ struct sigaction sa{}; \ sa.sa_handler = __doctest_signal_handler; \ sigemptyset(&sa.sa_mask); \ sa.sa_flags = 0; \ - for (int i = 1; i < NSIG; ++i) { \ - if (i == SIGKILL || i == SIGSTOP) continue; \ + for (int i = 1; i < NSIG; ++i) \ + { \ + if (i == SIGKILL || i == SIGSTOP) \ + continue; \ sigaction(i, &sa, nullptr); \ - } \ + } \ int __ret = sigsetjmp(__doctest_jumpbuf, 1); \ if (__ret == 0) # define __CATCH else -# define __FINALLY \ - { \ - struct sigaction sa_default{}; \ - sa_default.sa_handler = SIG_DFL; \ - sigemptyset(&sa_default.sa_mask); \ - sa_default.sa_flags = 0; \ - for (int i = 1; i < NSIG; ++i) { \ - if (i == SIGKILL || i == SIGSTOP) continue; \ +# define __FINALLY \ + { \ + struct sigaction sa_default{}; \ + sa_default.sa_handler = SIG_DFL; \ + sigemptyset(&sa_default.sa_mask); \ + sa_default.sa_flags = 0; \ + for (int i = 1; i < NSIG; ++i) \ + { \ + if (i == SIGKILL || i == SIGSTOP) \ + continue; \ sigaction(i, &sa_default, nullptr); \ - } \ + } \ } # define __ENDTRY \ @@ -269,19 +273,19 @@ TEST_SUITE("tlx/Containers") tlx::pod_vector arr5; arr5.resize(2); - + #ifndef __APPLE__ CHECK((arr5[0].value != 0 && arr5[1].value != 0)); #endif // we can safe access initialized member without exception catch arr5.resize(4, TrivalCtor1{39}); CHECK((arr5[2].value == 39 && arr5[3].value == 39)); - + arr5.resize(128, TrivalCtor1{66}); CHECK((arr5[2].value == 39 && arr5[3].value == 39)); - + CHECK((arr5[10].value == 66 && arr5[22].value == 66)); - + AXLOGI("Access uninitialzed object membmer done (non optimized build or non-Apple platforms)"); } __CATCH