"""gta6rt.exe must have no C++ runtime TLS callbacks (TEST-GTA6RT-NO-TLS-CALLBACKS). A thread_local with a dynamic initializer or destructor in gta6rt makes the MSVC C++ runtime add two TLS callbacks, __dyn_tls_init (tlsdyn.obj) and __dyn_tls_dtor (tlsdtor.obj). The CRT startup then registers __dyn_tls_dtor with _register_thread_local_exe_atexit_callback, the UCRT accepts that only once per process, the title registers its own, and the game fast-fails at startup with 0xC0000409 (RT-XMEM-STARTUP-FASTFAIL). Trivially destructible thread_local data is fine: it adds no callback. Other components may bring their own TLS callback. Rust's std has one (std::sys::thread_local::guard::windows:: tls_callback; std::sys::windows::thread_local_key::on_tls_callback in older toolchains) that runs Rust thread-local destructors; it is linked in with the Rust audio decoders and does not touch the CRT's atexit registration. So each callback is named from the linker's map file (gta6rt links with /MAP) and sorted: - __dyn_tls_init, __dyn_tls_dtor, or anything from tlsdyn.obj or tlsdtor.obj: fail, this is the startup fast-fail; - Rust std's thread-local callback: allowed; - anything else, or a callback the map does not name: fail, until someone checks what it does and adds it here. Without a map, an image with no callbacks passes and one with callbacks fails, saying what to provide. python -I check_pe_tls_callbacks.py [--map ] The map defaults to the image's path with .map. Exit 0 when every callback is allowed (or there is none), 1 otherwise or when the files cannot be read. Standard library only; reads the files, so it also runs where gta6rt cannot. """ import os import re import struct import sys TLS_DIR_INDEX = 9 MAX_THUNK_HOPS = 4 CRT_DYN_TLS = frozenset(("__dyn_tls_init", "__dyn_tls_dtor")) CRT_DYN_TLS_OBJECTS = ("tlsdyn.obj", "tlsdtor.obj") RUST_STD_TLS_CALLBACKS = frozenset((("std", "sys", "thread_local", "guard", "windows", "tls_callback"), ("std", "sys", "windows", "thread_local_key", "on_tls_callback"))) DANGEROUS, ALLOWED, UNKNOWN = "dangerous", "allowed", "unknown" def _image(data): """(image_base, TimeDateStamp, sections as (va, size, raw)) of a PE32+ image.""" if data[:2] != b"MZ" or len(data) < 0x40: raise ValueError("not a PE (no MZ header)") pe = struct.unpack_from(" len(data): break rel, = struct.unpack_from("." % (exe, len(cbs), rvas, map_path)) return 1 if base is None or base != image_base or map_stamp is None or map_stamp != timestamp: print("FAIL: %s is not the map of %s (load address or timestamp differ); relink and run again." % (map_path, exe)) return 1 try: results = judge(data, symbols) except (ValueError, struct.error) as e: print("check_pe_tls_callbacks: cannot read %s: %s" % (exe, e)) return 1 for rva, verdict, named in results: print(" TLS callback at RVA 0x%x: %s (%s)" % (rva, verdict, "; ".join( "%s in %s" % (n, o or "?") for n, o in named) or "no symbol at this address in the map")) bad = [r for r in results if r[1] != ALLOWED] if any(v == DANGEROUS for _, v, _ in bad): print("FAIL: %s has the C++ runtime's thread_local callbacks (__dyn_tls_init/__dyn_tls_dtor): a thread_local " "with a dynamic initializer or destructor was linked in, and the game's own " "_register_thread_local_exe_atexit_callback then fast-fails (0xC0000409)." % exe) return 1 if bad: print("FAIL: %s has TLS callback(s) this check does not know. Find out what each one does; if it is not a " "C++ thread_local with a destructor, add it to the allowed callbacks in check_pe_tls_callbacks.py " "with the reason." % exe) return 1 print("ok: %s has %d TLS callback(s), all Rust std's thread-local callback" % (exe, len(cbs))) return 0 if __name__ == "__main__": sys.exit(main(sys.argv))