diff options
| author | tedin7 <tommasoderosa94@gmail.com> | 2026-11-23 16:57:53 +0100 |
|---|---|---|
| committer | ubiquitarianism <283527159+ubiquitarianism@users.noreply.github.com> | 2026-11-23 19:43:03 -0500 |
| commit | f35d19cb861d00154dbeadd16035af7a79095d4c (patch) | |
| tree | 37f6077694c61c3ff8cf5ca38de8113afbeaaea8 /runtime/test/process_cmdline_test.cpp | |
| parent | e7f17716f1b3a02018cd4f0e03591d601eb9c068 (diff) | |
fix: SetThreadGroupAffinity answers a GROUP_AFFINITY of absent cores or groups with the thread's own affinity, served under kernel32, kernelbase and every processtopology API set (retail exe imports it; reported by dadsmallbuk1 on #6942)
(cherry picked from commit fc4de7d2272a43dcdb0cc8eed83d6821c36f117e)
(cherry picked from commit bbee83abfc1afe5b44ef9bc56c52ea517f6afa86)
Diffstat (limited to 'runtime/test/process_cmdline_test.cpp')
| -rw-r--r-- | runtime/test/process_cmdline_test.cpp | 167 |
1 files changed, 167 insertions, 0 deletions
diff --git a/runtime/test/process_cmdline_test.cpp b/runtime/test/process_cmdline_test.cpp index fb0a0498c..fc5d4ab02 100644 --- a/runtime/test/process_cmdline_test.cpp +++ b/runtime/test/process_cmdline_test.cpp | |||
| @@ -248,6 +248,173 @@ int main() { | |||
| 248 | } | 248 | } |
| 249 | } | 249 | } |
| 250 | 250 | ||
| 251 | // A console title pins threads to cores the host may lack. SetThreadAffinityMask must then not fail: it leaves the | ||
| 252 | // thread where it is and answers with the process mask. A bad handle still fails, and a mask the host accepts is | ||
| 253 | // applied and answers with the mask the thread had. SetThreadpoolAffinityMask (console-only) succeeds. | ||
| 254 | { | ||
| 255 | using SetMask = DWORD_PTR(WINAPI*)(HANDLE, DWORD_PTR); | ||
| 256 | using SetPool = BOOL(WINAPI*)(PTP_POOL, DWORD_PTR); | ||
| 257 | const auto set_mask = reinterpret_cast<SetMask>(rt::shims::find("kernel32.dll", "SetThreadAffinityMask")); | ||
| 258 | const auto set_pool = reinterpret_cast<SetPool>(rt::shims::find("kernel32.dll", "SetThreadpoolAffinityMask")); | ||
| 259 | DWORD_PTR process_mask = 0, system_mask = 0; | ||
| 260 | GetProcessAffinityMask(GetCurrentProcess(), &process_mask, &system_mask); | ||
| 261 | const DWORD_PTR missing = ~process_mask & (process_mask + 1); // the lowest core outside the process mask, 0 if none | ||
| 262 | const DWORD_PTR lowest = process_mask & (~process_mask + 1); | ||
| 263 | if (!set_mask || !set_pool) { | ||
| 264 | std::fprintf(stderr, "FAIL SetThreadAffinityMask or SetThreadpoolAffinityMask is not served\n"); | ||
| 265 | ++failures; | ||
| 266 | } else { | ||
| 267 | const HANDLE self = GetCurrentThread(); | ||
| 268 | if (missing) { | ||
| 269 | SetLastError(ERROR_GEN_FAILURE); | ||
| 270 | const DWORD_PTR previous = set_mask(self, missing); | ||
| 271 | if (previous != process_mask) { | ||
| 272 | std::fprintf(stderr, "FAIL a mask of cores the host lacks answered 0x%llx, want the process mask 0x%llx\n", | ||
| 273 | static_cast<unsigned long long>(previous), static_cast<unsigned long long>(process_mask)); | ||
| 274 | ++failures; | ||
| 275 | } | ||
| 276 | } | ||
| 277 | const DWORD_PTR first = set_mask(self, lowest); | ||
| 278 | const DWORD_PTR second = set_mask(self, process_mask); | ||
| 279 | if (first != process_mask || second != lowest) { | ||
| 280 | std::fprintf(stderr, "FAIL a valid mask answered 0x%llx then 0x%llx, want 0x%llx then 0x%llx\n", | ||
| 281 | static_cast<unsigned long long>(first), static_cast<unsigned long long>(second), | ||
| 282 | static_cast<unsigned long long>(process_mask), static_cast<unsigned long long>(lowest)); | ||
| 283 | ++failures; | ||
| 284 | } | ||
| 285 | if (set_mask(reinterpret_cast<HANDLE>(static_cast<intptr_t>(-7)), lowest) != 0) { | ||
| 286 | std::fprintf(stderr, "FAIL SetThreadAffinityMask on a bad handle did not fail\n"); | ||
| 287 | ++failures; | ||
| 288 | } | ||
| 289 | if (!set_pool(nullptr, ~DWORD_PTR{0})) { | ||
| 290 | std::fprintf(stderr, "FAIL SetThreadpoolAffinityMask did not succeed\n"); | ||
| 291 | ++failures; | ||
| 292 | } | ||
| 293 | |||
| 294 | // Every name a title can import it under reaches the shim (a missing alias falls through to the host). | ||
| 295 | for (const char* dll : {"kernelbase.dll", "api-ms-win-core-processthreads-l1-1-0.dll", | ||
| 296 | "api-ms-win-core-processthreads-l1-1-1.dll", "api-ms-win-core-processthreads-l1-1-2.dll", | ||
| 297 | "api-ms-win-core-processthreads-l1-1-3.dll"}) | ||
| 298 | if (rt::shims::find(dll, "SetThreadAffinityMask") != reinterpret_cast<void*>(set_mask)) { | ||
| 299 | std::fprintf(stderr, "FAIL SetThreadAffinityMask is not served under %s\n", dll); | ||
| 300 | ++failures; | ||
| 301 | } | ||
| 302 | if (rt::shims::find("kernelbase.dll", "SetThreadpoolAffinityMask") != reinterpret_cast<void*>(set_pool)) { | ||
| 303 | std::fprintf(stderr, "FAIL SetThreadpoolAffinityMask is not served under kernelbase.dll\n"); | ||
| 304 | ++failures; | ||
| 305 | } | ||
| 306 | |||
| 307 | // An empty mask names no CPU: it fails with ERROR_INVALID_PARAMETER, as on Windows. | ||
| 308 | SetLastError(ERROR_SUCCESS); | ||
| 309 | if (set_mask(self, 0) != 0 || GetLastError() != ERROR_INVALID_PARAMETER) { | ||
| 310 | std::fprintf(stderr, "FAIL SetThreadAffinityMask with an empty mask did not fail with ERROR_INVALID_PARAMETER\n"); | ||
| 311 | ++failures; | ||
| 312 | } | ||
| 313 | |||
| 314 | if (missing) { | ||
| 315 | // The substituted answer is the thread's own mask, so a caller that restores it leaves the thread | ||
| 316 | // pinned (with one allowed CPU the two masks are the same and this passes either way). | ||
| 317 | set_mask(self, lowest); | ||
| 318 | const DWORD_PTR answered = set_mask(self, missing); | ||
| 319 | const DWORD_PTR still = set_mask(self, process_mask); // answers the mask the thread has now | ||
| 320 | if (answered != lowest || still != lowest) { | ||
| 321 | std::fprintf(stderr, | ||
| 322 | "FAIL a pinned thread asking for absent cores answered 0x%llx and was left on 0x%llx, want 0x%llx twice\n", | ||
| 323 | static_cast<unsigned long long>(answered), static_cast<unsigned long long>(still), | ||
| 324 | static_cast<unsigned long long>(lowest)); | ||
| 325 | ++failures; | ||
| 326 | } | ||
| 327 | |||
| 328 | // The note about the substitution goes to stderr; when that pipe is closed the write fails and leaves | ||
| 329 | // its error behind, but the call succeeded, so the last error is clear. | ||
| 330 | std::fflush(stderr); | ||
| 331 | int fds[2]; | ||
| 332 | const int saved = _dup(2); | ||
| 333 | if (_pipe(fds, 4096, _O_BINARY) == 0 && saved >= 0) { | ||
| 334 | _close(fds[0]); // no reader | ||
| 335 | _dup2(fds[1], 2); | ||
| 336 | _close(fds[1]); | ||
| 337 | setvbuf(stderr, nullptr, _IONBF, 0); | ||
| 338 | SetLastError(ERROR_GEN_FAILURE); | ||
| 339 | set_mask(self, missing); | ||
| 340 | const DWORD error = GetLastError(); | ||
| 341 | std::fflush(stderr); | ||
| 342 | _dup2(saved, 2); | ||
| 343 | if (error != ERROR_SUCCESS) { | ||
| 344 | std::fprintf(stderr, "FAIL a substituted answer left last error %lu with stderr closed, want 0\n", error); | ||
| 345 | ++failures; | ||
| 346 | } | ||
| 347 | } | ||
| 348 | if (saved >= 0) _close(saved); | ||
| 349 | } | ||
| 350 | } | ||
| 351 | } | ||
| 352 | |||
| 353 | // The retail title pins threads with SetThreadGroupAffinity. A GROUP_AFFINITY of cores or a group the PC lacks must | ||
| 354 | // succeed, leave the thread where it is and hand back the affinity it had; an empty mask still fails. | ||
| 355 | { | ||
| 356 | using SetGroup = BOOL(WINAPI*)(HANDLE, const GROUP_AFFINITY*, PGROUP_AFFINITY); | ||
| 357 | const auto set_group = reinterpret_cast<SetGroup>(rt::shims::find("kernel32.dll", "SetThreadGroupAffinity")); | ||
| 358 | if (!set_group) { | ||
| 359 | std::fprintf(stderr, "FAIL SetThreadGroupAffinity is not served\n"); | ||
| 360 | ++failures; | ||
| 361 | } else { | ||
| 362 | for (const char* dll : {"kernelbase.dll", "api-ms-win-core-processtopology-l1-1-0.dll", | ||
| 363 | "api-ms-win-core-processtopology-l1-1-1.dll", "api-ms-win-core-processtopology-l1-1-2.dll"}) | ||
| 364 | if (rt::shims::find(dll, "SetThreadGroupAffinity") != reinterpret_cast<void*>(set_group)) { | ||
| 365 | std::fprintf(stderr, "FAIL SetThreadGroupAffinity is not served under %s\n", dll); | ||
| 366 | ++failures; | ||
| 367 | } | ||
| 368 | const HANDLE self = GetCurrentThread(); | ||
| 369 | GROUP_AFFINITY own = {}; | ||
| 370 | GetThreadGroupAffinity(self, &own); | ||
| 371 | // A processor group the PC does not have. | ||
| 372 | GROUP_AFFINITY absent_group = {~KAFFINITY{0}, static_cast<WORD>(GetActiveProcessorGroupCount()), {}}; | ||
| 373 | GROUP_AFFINITY before = {1, 7, {}}; | ||
| 374 | SetLastError(ERROR_GEN_FAILURE); | ||
| 375 | const BOOL ok = set_group(self, &absent_group, &before); | ||
| 376 | GROUP_AFFINITY after = {}; | ||
| 377 | GetThreadGroupAffinity(self, &after); | ||
| 378 | if (!ok || GetLastError() != ERROR_SUCCESS || before.Group != own.Group || before.Mask != own.Mask || | ||
| 379 | after.Group != own.Group || after.Mask != own.Mask) { | ||
| 380 | std::fprintf(stderr, "FAIL an absent group answered ok=%d error=%lu previous %u/0x%llx, want TRUE, 0, %u/0x%llx\n", | ||
| 381 | ok, GetLastError(), before.Group, static_cast<unsigned long long>(before.Mask), own.Group, | ||
| 382 | static_cast<unsigned long long>(own.Mask)); | ||
| 383 | ++failures; | ||
| 384 | } | ||
| 385 | // Cores of the thread's own group that the process lacks (when it has every core of the group there is none). | ||
| 386 | DWORD_PTR process_mask = 0, system_mask = 0; | ||
| 387 | GetProcessAffinityMask(GetCurrentProcess(), &process_mask, &system_mask); | ||
| 388 | const KAFFINITY missing = ~process_mask & (process_mask + 1); | ||
| 389 | if (missing) { | ||
| 390 | GROUP_AFFINITY absent_core = {missing, own.Group, {}}; | ||
| 391 | GROUP_AFFINITY before_core = {}; | ||
| 392 | if (!set_group(self, &absent_core, &before_core) || before_core.Mask != own.Mask) { | ||
| 393 | std::fprintf(stderr, "FAIL absent cores answered previous 0x%llx, want 0x%llx\n", | ||
| 394 | static_cast<unsigned long long>(before_core.Mask), static_cast<unsigned long long>(own.Mask)); | ||
| 395 | ++failures; | ||
| 396 | } | ||
| 397 | } | ||
| 398 | // A mask the host accepts is applied; the answer is the affinity the thread had. | ||
| 399 | const KAFFINITY lowest = own.Mask & (~own.Mask + 1); | ||
| 400 | GROUP_AFFINITY one = {lowest, own.Group, {}}, prior = {}; | ||
| 401 | GROUP_AFFINITY now = {}; | ||
| 402 | if (!set_group(self, &one, &prior) || prior.Mask != own.Mask || !GetThreadGroupAffinity(self, &now) || | ||
| 403 | now.Mask != lowest) { | ||
| 404 | std::fprintf(stderr, "FAIL a valid group mask was not applied with the old mask answered\n"); | ||
| 405 | ++failures; | ||
| 406 | } | ||
| 407 | set_group(self, &own, nullptr); | ||
| 408 | // An empty mask names no CPU: it fails with ERROR_INVALID_PARAMETER, as on Windows. | ||
| 409 | GROUP_AFFINITY empty = {0, own.Group, {}}; | ||
| 410 | SetLastError(ERROR_SUCCESS); | ||
| 411 | if (set_group(self, &empty, nullptr) || GetLastError() != ERROR_INVALID_PARAMETER) { | ||
| 412 | std::fprintf(stderr, "FAIL SetThreadGroupAffinity with an empty mask did not fail with ERROR_INVALID_PARAMETER\n"); | ||
| 413 | ++failures; | ||
| 414 | } | ||
| 415 | } | ||
| 416 | } | ||
| 417 | |||
| 251 | // The game thread that ends a run must park, not ExitProcess, even when run_entry has already returned and | 418 | // 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 | 419 | // 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 | 420 | // host preempts the game thread right after SetEvent(g_done), and Sleep lets the game thread run again before |