Files
mandy/runtime/test/check_tls_index0.cmake
Jan Trencansky 461efcde4d check_tls_index0.cmake: skip the failed-reservation half where TMP cannot be overridden
The second half of run_tiny_tls_ga (new in #984) points TMP and TEMP at a missing folder so that the home clone cannot be written and the loader must refuse the image. Under an emulator the override never reaches the loader: Wine gives the process the temporary folder of its prefix whatever the caller's environment says, so the reservation succeeds, --run returns 0 and the script failed with '--run without the reservation returned 0' (on #984's head too).

When the script runs the loader through an emulator it first asks --dry-run, which maps the image and names index 0 in a note when the reservation fails, without running any of its code. If that note is missing the override is not in effect: the script prints 'SKIP the failed-reservation half: TMP and TEMP set for <runner> do not reach the loader's GetTempPathW' and stops there, after the first half (the image gets index 0 and reads its TLS from slot 0) has passed. A native run is unchanged.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-24 20:18:37 -05:00

62 lines
3.6 KiB
CMake

# gta6rt --run and --init with tiny_tls_ga.exe, an executable built /GA whose thread_local reads go straight to
# ThreadLocalStoragePointer[0]. --run: the game thread and a second thread both read the template value 42 and
# _tls_index is 0. Both modes: the startup trace names gta6rt.exe as the image that held module TLS index 0 and the
# index its block moved to. Then, with TMP and TEMP naming a missing folder, the home clone cannot be written and the
# reservation fails before main: --run and --init refuse the image with that reason and run none of its code, and
# --dry-run maps it with the note. (A TMP longer than MAX_PATH does not get this far: GetTempPathW then skips it.)
foreach(mode run init)
set(trace "${TRACE_DIR}/tls_index0_${mode}.jsonl")
file(REMOVE "${trace}") # --trace only creates a new file
execute_process(
COMMAND ${RUNNER} "${LOADER}" --${mode} "${GUEST}" --trace "${trace}"
TIMEOUT 90
RESULT_VARIABLE code OUTPUT_VARIABLE output ERROR_VARIABLE error)
if(NOT code STREQUAL "0")
message(FATAL_ERROR "--${mode} returned ${code}\n${output}\n${error}")
endif()
if(mode STREQUAL "run" AND (NOT output MATCHES "tiny_tls_ga: tls_index=0 main=42 thread=42 OK"
OR NOT output MATCHES "game exited with code 0"))
message(FATAL_ERROR "--run did not read the TLS from slot 0\n${output}\n${error}")
endif()
if(error MATCHES "does not get module TLS index 0")
message(FATAL_ERROR "--${mode}: the executable was not given index 0\n${error}")
endif()
file(READ "${trace}" lines)
if(NOT lines MATCHES "\"kind\":\"tls_index0\",\"owner\":\"gta6rt.exe\",\"moved_to\":[1-9][0-9]*}")
message(FATAL_ERROR "--${mode}: no tls_index0 event naming gta6rt.exe in the trace\n${lines}")
endif()
file(REMOVE "${trace}")
endforeach()
set(missing "${TRACE_DIR}/leonida_no_such_temp")
file(REMOVE_RECURSE "${missing}")
# TMP and TEMP reach the loader's GetTempPathW only where the loader runs natively. Under an emulator (Wine) the
# process takes its temporary folder from the emulator's prefix whatever the caller's environment says, so the home
# clone is written, the reservation succeeds and there is nothing to refuse. --dry-run tells the two apart without
# running the image: with the override in effect it maps the image with the note about index 0.
if(RUNNER)
execute_process(
COMMAND ${CMAKE_COMMAND} -E env "TMP=${missing}" "TEMP=${missing}" ${RUNNER} "${LOADER}" --dry-run "${GUEST}"
TIMEOUT 90
RESULT_VARIABLE code OUTPUT_VARIABLE output ERROR_VARIABLE error)
if(code STREQUAL "0" AND NOT error MATCHES "module TLS index 0")
message(STATUS "SKIP the failed-reservation half: TMP and TEMP set for ${RUNNER} do not reach the loader's "
"GetTempPathW (the emulator takes its temporary folder from its prefix)")
return()
endif()
endif()
foreach(mode run init dry-run)
execute_process(
COMMAND ${CMAKE_COMMAND} -E env "TMP=${missing}" "TEMP=${missing}" ${RUNNER} "${LOADER}" --${mode} "${GUEST}"
TIMEOUT 90
RESULT_VARIABLE code OUTPUT_VARIABLE output ERROR_VARIABLE error)
set(reason "module TLS index 0 \\(the home clone of gta6rt_tls_host.dll cannot be loaded")
if(mode STREQUAL "dry-run")
if(NOT code STREQUAL "0" OR NOT error MATCHES "does not get ${reason}")
message(FATAL_ERROR "--dry-run without the reservation returned ${code}\n${output}\n${error}")
endif()
elseif(NOT code STREQUAL "1" OR NOT error MATCHES "cannot have ${reason}" OR output MATCHES "running ")
message(FATAL_ERROR "--${mode} without the reservation returned ${code}\n${output}\n${error}")
endif()
endforeach()