diff options
| author | NotRexed <therealnothingfn@gmail.com> | 2026-11-27 17:50:37 -0500 |
|---|---|---|
| committer | NotRexed <therealnothingfn@gmail.com> | 2026-11-27 17:50:37 -0500 |
| commit | b87abdef5735adc68a614b2025ffcf9869a47cc5 (patch) | |
| tree | 0d8b473e8e753883a01e00153175994d6b7949c2 /runtime/test/process_cmdline_test.cpp | |
| parent | 84e728faffb36cd96a08934e5632ba9b194801f3 (diff) | |
| parent | 34e33d3ddc4dd95ad14a074db886df3d25588201 (diff) | |
Merge mandy's main into port/d3d12-nvidia
Brings the D3D12 runtime branch (d3d12/, its launcher scripts and notes, and the pull requests reviewed and applied
on it) together with mandy's main.
Conflicts (67 files, none under d3d12/): docs/, make_game.py, runtime/, scripts/build_runtime.ps1, tests/ and
tools/. Every one takes main's side: main is where that code is maintained, and this branch's copies were older
states of the same files or the same pull requests in another form. tests/test_fspx_unpack.py is main's too, as it
tests main's tools/fspx_unpack.py. Nothing under d3d12/ is changed by the merge.
Checked before pushing: python -m compileall over make_game.py, tools and tests; make_game.py --help;
scripts/check_no_game_data.py over the files this adds to main; tests/test_text_write_line_endings.py. The
translator suite, the C++ runtime build and CI have not run on the result.
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/process_cmdline_test.cpp')
| -rw-r--r-- | runtime/test/process_cmdline_test.cpp | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/runtime/test/process_cmdline_test.cpp b/runtime/test/process_cmdline_test.cpp index 89a1b7f03..a550910b9 100644 --- a/runtime/test/process_cmdline_test.cpp +++ b/runtime/test/process_cmdline_test.cpp | |||
| @@ -6,6 +6,9 @@ | |||
| 6 | #include <windows.h> | 6 | #include <windows.h> |
| 7 | #include <shellapi.h> | 7 | #include <shellapi.h> |
| 8 | 8 | ||
| 9 | #include <fcntl.h> | ||
| 10 | #include <io.h> | ||
| 11 | |||
| 9 | #include <cstdio> | 12 | #include <cstdio> |
| 10 | #include <cstring> | 13 | #include <cstring> |
| 11 | #include <cwchar> | 14 | #include <cwchar> |
| @@ -248,6 +251,173 @@ int main() { | |||
| 248 | } | 251 | } |
| 249 | } | 252 | } |
| 250 | 253 | ||
| 254 | // A console title pins threads to cores the host may lack. SetThreadAffinityMask must then not fail: it leaves the | ||
| 255 | // thread where it is and answers with the process mask. A bad handle still fails, and a mask the host accepts is | ||
| 256 | // applied and answers with the mask the thread had. SetThreadpoolAffinityMask (console-only) succeeds. | ||
| 257 | { | ||
| 258 | using SetMask = DWORD_PTR(WINAPI*)(HANDLE, DWORD_PTR); | ||
| 259 | using SetPool = BOOL(WINAPI*)(PTP_POOL, DWORD_PTR); | ||
| 260 | const auto set_mask = reinterpret_cast<SetMask>(rt::shims::find("kernel32.dll", "SetThreadAffinityMask")); | ||
| 261 | const auto set_pool = reinterpret_cast<SetPool>(rt::shims::find("kernel32.dll", "SetThreadpoolAffinityMask")); | ||
| 262 | DWORD_PTR process_mask = 0, system_mask = 0; | ||
| 263 | GetProcessAffinityMask(GetCurrentProcess(), &process_mask, &system_mask); | ||
| 264 | const DWORD_PTR missing = ~process_mask & (process_mask + 1); // the lowest core outside the process mask, 0 if none | ||
| 265 | const DWORD_PTR lowest = process_mask & (~process_mask + 1); | ||
| 266 | if (!set_mask || !set_pool) { | ||
| 267 | std::fprintf(stderr, "FAIL SetThreadAffinityMask or SetThreadpoolAffinityMask is not served\n"); | ||
| 268 | ++failures; | ||
| 269 | } else { | ||
| 270 | const HANDLE self = GetCurrentThread(); | ||
| 271 | if (missing) { | ||
| 272 | SetLastError(ERROR_GEN_FAILURE); | ||
| 273 | const DWORD_PTR previous = set_mask(self, missing); | ||
| 274 | if (previous != process_mask) { | ||
| 275 | std::fprintf(stderr, "FAIL a mask of cores the host lacks answered 0x%llx, want the process mask 0x%llx\n", | ||
| 276 | static_cast<unsigned long long>(previous), static_cast<unsigned long long>(process_mask)); | ||
| 277 | ++failures; | ||
| 278 | } | ||
| 279 | } | ||
| 280 | const DWORD_PTR first = set_mask(self, lowest); | ||
| 281 | const DWORD_PTR second = set_mask(self, process_mask); | ||
| 282 | if (first != process_mask || second != lowest) { | ||
| 283 | std::fprintf(stderr, "FAIL a valid mask answered 0x%llx then 0x%llx, want 0x%llx then 0x%llx\n", | ||
| 284 | static_cast<unsigned long long>(first), static_cast<unsigned long long>(second), | ||
| 285 | static_cast<unsigned long long>(process_mask), static_cast<unsigned long long>(lowest)); | ||
| 286 | ++failures; | ||
| 287 | } | ||
| 288 | if (set_mask(reinterpret_cast<HANDLE>(static_cast<intptr_t>(-7)), lowest) != 0) { | ||
| 289 | std::fprintf(stderr, "FAIL SetThreadAffinityMask on a bad handle did not fail\n"); | ||
| 290 | ++failures; | ||
| 291 | } | ||
| 292 | if (!set_pool(nullptr, ~DWORD_PTR{0})) { | ||
| 293 | std::fprintf(stderr, "FAIL SetThreadpoolAffinityMask did not succeed\n"); | ||
| 294 | ++failures; | ||
| 295 | } | ||
| 296 | |||
| 297 | // Every name a title can import it under reaches the shim (a missing alias falls through to the host). | ||
| 298 | for (const char* dll : {"kernelbase.dll", "api-ms-win-core-processthreads-l1-1-0.dll", | ||
| 299 | "api-ms-win-core-processthreads-l1-1-1.dll", "api-ms-win-core-processthreads-l1-1-2.dll", | ||
| 300 | "api-ms-win-core-processthreads-l1-1-3.dll"}) | ||
| 301 | if (rt::shims::find(dll, "SetThreadAffinityMask") != reinterpret_cast<void*>(set_mask)) { | ||
| 302 | std::fprintf(stderr, "FAIL SetThreadAffinityMask is not served under %s\n", dll); | ||
| 303 | ++failures; | ||
| 304 | } | ||
| 305 | if (rt::shims::find("kernelbase.dll", "SetThreadpoolAffinityMask") != reinterpret_cast<void*>(set_pool)) { | ||
| 306 | std::fprintf(stderr, "FAIL SetThreadpoolAffinityMask is not served under kernelbase.dll\n"); | ||
| 307 | ++failures; | ||
| 308 | } | ||
| 309 | |||
| 310 | // An empty mask names no CPU: it fails with ERROR_INVALID_PARAMETER, as on Windows. | ||
| 311 | SetLastError(ERROR_SUCCESS); | ||
| 312 | if (set_mask(self, 0) != 0 || GetLastError() != ERROR_INVALID_PARAMETER) { | ||
| 313 | std::fprintf(stderr, "FAIL SetThreadAffinityMask with an empty mask did not fail with ERROR_INVALID_PARAMETER\n"); | ||
| 314 | ++failures; | ||
| 315 | } | ||
| 316 | |||
| 317 | if (missing) { | ||
| 318 | // The substituted answer is the thread's own mask, so a caller that restores it leaves the thread | ||
| 319 | // pinned (with one allowed CPU the two masks are the same and this passes either way). | ||
| 320 | set_mask(self, lowest); | ||
| 321 | const DWORD_PTR answered = set_mask(self, missing); | ||
| 322 | const DWORD_PTR still = set_mask(self, process_mask); // answers the mask the thread has now | ||
| 323 | if (answered != lowest || still != lowest) { | ||
| 324 | std::fprintf(stderr, | ||
| 325 | "FAIL a pinned thread asking for absent cores answered 0x%llx and was left on 0x%llx, want 0x%llx twice\n", | ||
| 326 | static_cast<unsigned long long>(answered), static_cast<unsigned long long>(still), | ||
| 327 | static_cast<unsigned long long>(lowest)); | ||
| 328 | ++failures; | ||
| 329 | } | ||
| 330 | |||
| 331 | // The note about the substitution goes to stderr; when that pipe is closed the write fails and leaves | ||
| 332 | // its error behind, but the call succeeded, so the last error is clear. | ||
| 333 | std::fflush(stderr); | ||
| 334 | int fds[2]; | ||
| 335 | const int saved = _dup(2); | ||
| 336 | if (_pipe(fds, 4096, _O_BINARY) == 0 && saved >= 0) { | ||
| 337 | _close(fds[0]); // no reader | ||
| 338 | _dup2(fds[1], 2); | ||
| 339 | _close(fds[1]); | ||
| 340 | setvbuf(stderr, nullptr, _IONBF, 0); | ||
| 341 | SetLastError(ERROR_GEN_FAILURE); | ||
| 342 | set_mask(self, missing); | ||
| 343 | const DWORD error = GetLastError(); | ||
| 344 | std::fflush(stderr); | ||
| 345 | _dup2(saved, 2); | ||
| 346 | if (error != ERROR_SUCCESS) { | ||
| 347 | std::fprintf(stderr, "FAIL a substituted answer left last error %lu with stderr closed, want 0\n", error); | ||
| 348 | ++failures; | ||
| 349 | } | ||
| 350 | } | ||
| 351 | if (saved >= 0) _close(saved); | ||
| 352 | } | ||
| 353 | } | ||
| 354 | } | ||
| 355 | |||
| 356 | // The retail title pins threads with SetThreadGroupAffinity. A GROUP_AFFINITY of cores or a group the PC lacks must | ||
| 357 | // succeed, leave the thread where it is and hand back the affinity it had; an empty mask still fails. | ||
| 358 | { | ||
| 359 | using SetGroup = BOOL(WINAPI*)(HANDLE, const GROUP_AFFINITY*, PGROUP_AFFINITY); | ||
| 360 | const auto set_group = reinterpret_cast<SetGroup>(rt::shims::find("kernel32.dll", "SetThreadGroupAffinity")); | ||
| 361 | if (!set_group) { | ||
| 362 | std::fprintf(stderr, "FAIL SetThreadGroupAffinity is not served\n"); | ||
| 363 | ++failures; | ||
| 364 | } else { | ||
| 365 | for (const char* dll : {"kernelbase.dll", "api-ms-win-core-processtopology-l1-1-0.dll", | ||
| 366 | "api-ms-win-core-processtopology-l1-1-1.dll", "api-ms-win-core-processtopology-l1-1-2.dll"}) | ||
| 367 | if (rt::shims::find(dll, "SetThreadGroupAffinity") != reinterpret_cast<void*>(set_group)) { | ||
| 368 | std::fprintf(stderr, "FAIL SetThreadGroupAffinity is not served under %s\n", dll); | ||
| 369 | ++failures; | ||
| 370 | } | ||
| 371 | const HANDLE self = GetCurrentThread(); | ||
| 372 | GROUP_AFFINITY own = {}; | ||
| 373 | GetThreadGroupAffinity(self, &own); | ||
| 374 | // A processor group the PC does not have. | ||
| 375 | GROUP_AFFINITY absent_group = {~KAFFINITY{0}, static_cast<WORD>(GetActiveProcessorGroupCount()), {}}; | ||
| 376 | GROUP_AFFINITY before = {1, 7, {}}; | ||
| 377 | SetLastError(ERROR_GEN_FAILURE); | ||
| 378 | const BOOL ok = set_group(self, &absent_group, &before); | ||
| 379 | GROUP_AFFINITY after = {}; | ||
| 380 | GetThreadGroupAffinity(self, &after); | ||
| 381 | if (!ok || GetLastError() != ERROR_SUCCESS || before.Group != own.Group || before.Mask != own.Mask || | ||
| 382 | after.Group != own.Group || after.Mask != own.Mask) { | ||
| 383 | std::fprintf(stderr, "FAIL an absent group answered ok=%d error=%lu previous %u/0x%llx, want TRUE, 0, %u/0x%llx\n", | ||
| 384 | ok, GetLastError(), before.Group, static_cast<unsigned long long>(before.Mask), own.Group, | ||
| 385 | static_cast<unsigned long long>(own.Mask)); | ||
| 386 | ++failures; | ||
| 387 | } | ||
| 388 | // Cores of the thread's own group that the process lacks (when it has every core of the group there is none). | ||
| 389 | DWORD_PTR process_mask = 0, system_mask = 0; | ||
| 390 | GetProcessAffinityMask(GetCurrentProcess(), &process_mask, &system_mask); | ||
| 391 | const KAFFINITY missing = ~process_mask & (process_mask + 1); | ||
| 392 | if (missing) { | ||
| 393 | GROUP_AFFINITY absent_core = {missing, own.Group, {}}; | ||
| 394 | GROUP_AFFINITY before_core = {}; | ||
| 395 | if (!set_group(self, &absent_core, &before_core) || before_core.Mask != own.Mask) { | ||
| 396 | std::fprintf(stderr, "FAIL absent cores answered previous 0x%llx, want 0x%llx\n", | ||
| 397 | static_cast<unsigned long long>(before_core.Mask), static_cast<unsigned long long>(own.Mask)); | ||
| 398 | ++failures; | ||
| 399 | } | ||
| 400 | } | ||
| 401 | // A mask the host accepts is applied; the answer is the affinity the thread had. | ||
| 402 | const KAFFINITY lowest = own.Mask & (~own.Mask + 1); | ||
| 403 | GROUP_AFFINITY one = {lowest, own.Group, {}}, prior = {}; | ||
| 404 | GROUP_AFFINITY now = {}; | ||
| 405 | if (!set_group(self, &one, &prior) || prior.Mask != own.Mask || !GetThreadGroupAffinity(self, &now) || | ||
| 406 | now.Mask != lowest) { | ||
| 407 | std::fprintf(stderr, "FAIL a valid group mask was not applied with the old mask answered\n"); | ||
| 408 | ++failures; | ||
| 409 | } | ||
| 410 | set_group(self, &own, nullptr); | ||
| 411 | // An empty mask names no CPU: it fails with ERROR_INVALID_PARAMETER, as on Windows. | ||
| 412 | GROUP_AFFINITY empty = {0, own.Group, {}}; | ||
| 413 | SetLastError(ERROR_SUCCESS); | ||
| 414 | if (set_group(self, &empty, nullptr) || GetLastError() != ERROR_INVALID_PARAMETER) { | ||
| 415 | std::fprintf(stderr, "FAIL SetThreadGroupAffinity with an empty mask did not fail with ERROR_INVALID_PARAMETER\n"); | ||
| 416 | ++failures; | ||
| 417 | } | ||
| 418 | } | ||
| 419 | } | ||
| 420 | |||
| 251 | // The game thread that ends a run must park, not ExitProcess, even when run_entry has already returned and | 421 | // The game thread that ends a run must park, not ExitProcess, even when run_entry has already returned and |
| 252 | // cleared the run: main.cpp still prints "game exited with code N" and cleans up after it. On one CPU the | 422 | // cleared the run: main.cpp still prints "game exited with code N" and cleans up after it. On one CPU the |
| 253 | // host preempts the game thread right after SetEvent(g_done), and Sleep lets the game thread run again before | 423 | // host preempts the game thread right after SetEvent(g_done), and Sleep lets the game thread run again before |