Files
mandy/runtime/test/check_atexit.cmake
Mike7581 1dd08900f6 process: the run's cleanup holds one lock, as the UCRT's exit lock; a throwing handler terminates
Review follow-up for #1564:
- Registration and the cleanup of exit, quick_exit and _cexit hold one recursive lock of the run's own, from before the thread-local callback through the table's drain (and _cexit's stream flush), as the UCRT holds its exit lock: two threads never clean up at once, and a registration from another thread waits for the cleanup. The exiting thread may still register from a handler, _cexit still returns, and the lock is released before the run ends (end_run), also when a handler ends the run from inside a drain.
- Each guest cleanup callback (atexit, at_quick_exit, the thread-local destructor callback) runs inside a C++ exception boundary: one that throws ends the run through game_abort, as the UCRT's terminate() would (exit code 3), instead of leaving the process with 0xE06D7363.

tiny_atexit gains four modes, held to the native sequence: concurrent_cexit (a second thread's _cexit, with a thread_local of its own, waits for the cleanup), quick_exit_register (an at_quick_exit from a second thread during a quick_exit drain waits, so its handler never runs), quick_exit_throw and exit_throw (exit code 3). In the concurrent modes the slow handler waits until the second thread is about to call, so the sequence is fixed under any scheduling with the lock. All four fail on c5b51e9 through --run.
2026-11-27 16:10:51 -03:00

101 lines
5.4 KiB
CMake

# atexit handlers, static destructors and every way out: tiny_atexit run through `gta6rt --run` must print the
# sequence written below and end with its exit code. That is what the program does natively on Windows, and the
# native run is held to the same sequence, so the two cannot agree on a wrong answer. Under Wine both runs go through
# RUNNER, and Wine's own CRT differs in places (it drops a handler registered while handlers run, and its _exit does
# not wait for the exit lock), so there a native difference is reported without failing; --run is still held to the
# Windows sequence.
get_filename_component(guest_dir "${GUEST}" DIRECTORY)
get_filename_component(guest_name "${GUEST}" NAME)
set(ctors "ctor first;ctor second;main")
set(handlers "atexit two;atexit registers late;atexit late;atexit one;dtor second;dtor first")
set(expect_return "${ctors};${handlers}")
set(code_return 3)
set(expect_exit "${ctors};${handlers}")
set(code_exit 7)
set(expect__exit "${ctors}")
set(code__exit 8)
set(expect_quick_exit "${ctors}")
set(code_quick_exit 9)
set(expect_ExitProcess "${ctors}")
set(code_ExitProcess 10)
set(expect_cexit "${ctors};${handlers};after _cexit")
set(code_cexit 11)
set(expect_c_exit "${ctors};after _c_exit;${handlers}")
set(code_c_exit 12)
# exit(7) is running a slow handler when a second thread calls _exit(7): the exit lock holds it back.
set(expect_concurrent_exit "${ctors};slow handler started;slow handler done;${handlers}")
set(code_concurrent_exit 7)
# The main thread's thread_local object ends before the atexit handlers and static destructors ([basic.start.term]):
# the UCRT calls the EXE's thread-local destructor callback first in exit() and _cexit().
set(expect_thread_local_exit "${ctors};ctor thread_local;dtor thread_local;${handlers}")
set(code_thread_local_exit 13)
set(expect_thread_local_cexit "${ctors};ctor thread_local;dtor thread_local;${handlers};after _cexit")
set(code_thread_local_cexit 14)
# at_quick_exit handlers: quick_exit runs them newest first and nothing else; exit and _exit do not run them.
set(expect_at_quick_exit "${ctors};at_quick_exit two;at_quick_exit one")
set(code_at_quick_exit 15)
set(expect_at_quick_exit_exit "${ctors};${handlers}")
set(code_at_quick_exit_exit 16)
set(expect_at_quick_exit__exit "${ctors}")
set(code_at_quick_exit__exit 17)
# The exit lock serializes cleanup: a second thread's _cexit waits for main's drain (its own thread_local destructor
# then runs, and the table is empty), and a registration from a second thread during a quick_exit drain waits too,
# so the handler it registers never runs.
set(expect_concurrent_cexit "${ctors};slow handler started;ctor thread_local second;second _cexit entered;slow handler done;${handlers};dtor thread_local second;second _cexit returned;after _cexit")
set(code_concurrent_cexit 18)
set(expect_quick_exit_register "${ctors};quick slow started;second registering;quick slow done;at_quick_exit one")
set(code_quick_exit_register 19)
# A handler that throws: terminate(), so abort() and its exit code 3; the handlers after it do not run.
set(expect_quick_exit_throw "${ctors};quick throw")
set(code_quick_exit_throw 3)
set(expect_exit_throw "${ctors};atexit throw")
set(code_exit_throw 3)
# The program's own lines, in order, without the prefix; everything gta6rt prints is dropped.
function(guest_lines output result)
string(REPLACE "\r\n" "\n" output "${output}")
string(REPLACE ";" "," output "${output}")
string(REPLACE "\n" ";" output "${output}")
set(lines)
foreach(line IN LISTS output)
if(line MATCHES "^tiny_atexit: (.*)$")
list(APPEND lines "${CMAKE_MATCH_1}")
endif()
endforeach()
set(${result} "${lines}" PARENT_SCOPE)
endfunction()
set(failures "")
set(modes return exit _exit quick_exit ExitProcess cexit c_exit concurrent_exit thread_local_exit thread_local_cexit
at_quick_exit at_quick_exit_exit at_quick_exit__exit concurrent_cexit quick_exit_register quick_exit_throw
exit_throw)
list(LENGTH modes mode_count)
foreach(mode IN LISTS modes)
execute_process(COMMAND ${RUNNER} "${GUEST}" ${mode}
WORKING_DIRECTORY "${guest_dir}" TIMEOUT 60
RESULT_VARIABLE native_code OUTPUT_VARIABLE native_out ERROR_VARIABLE native_err)
execute_process(COMMAND ${RUNNER} "${LOADER}" --run "${guest_name}" -- ${mode}
WORKING_DIRECTORY "${guest_dir}" TIMEOUT 60
RESULT_VARIABLE run_code OUTPUT_VARIABLE run_out ERROR_VARIABLE run_err)
guest_lines("${native_out}" native_lines)
guest_lines("${run_out}" run_lines)
if(NOT native_code STREQUAL code_${mode} OR NOT native_lines STREQUAL expect_${mode})
string(CONCAT native_diff "[${mode}] native run differs from the expected sequence"
"\n expected (${code_${mode}}): ${expect_${mode}}\n native (${native_code}): ${native_lines}")
if(RUNNER)
message(STATUS "${native_diff}\n (the host CRT differs from Windows; not a gta6rt failure)")
else()
string(APPEND failures "\n${native_diff}\n (the fixture or the host changed)")
endif()
endif()
if(NOT run_code STREQUAL code_${mode} OR NOT run_lines STREQUAL expect_${mode})
string(APPEND failures "\n[${mode}] gta6rt --run differs from the expected sequence"
"\n expected (${code_${mode}}): ${expect_${mode}}\n --run (${run_code}): ${run_lines}\n${run_err}")
endif()
endforeach()
if(failures)
message(FATAL_ERROR "atexit/termination differences:${failures}")
endif()
message(STATUS "tiny_atexit: ${mode_count} termination paths match the Windows sequence")