runtime: verify startup_stubs trap header switches and exit codes #1548
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1555"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T13:53:08Z.
Change
Adds a standalone, device-free C++17 test executable (
tests/startup_trap_header_check.cpp) verifyingsrc/startup_stubs/trap.h(Refs #1149):x2w_trap::exit_code: 8-bit module ID and export index bit-masking (0xE2570000 | ...).x2w_trap::listed: unset/empty environment variables, wildcard*vs literal*alpha, comma-separated exact matching (first/middle/last/empty items, case sensitivity, superstring/prefix rejection), and 2047-byte accepted vs 2048-byte rejected buffer boundaries.X2W_TRAP_EXPORT:X2W_TRAP_CONTINUEreturning0withx2w-continue,X2W_TRAP_FAILreturningE_FAIL(0x80004005) withx2w-fail, continue-over-fail precedence when listed in both,x2w_trap::write_allnull/invalid handle safety, and a child-process default trap check verifyingTerminateProcessexit code and matchingx2w-traplines onstderrandX2W_TRAP_LOG.startup_trap_header_check,startup_trap_header, andWILL_FAILnegative controlsstartup_trap_header_prefix_control(--mutate prefix-match) andstartup_trap_header_precedence_control(--mutate fail-first) inCMakeLists.txt, and documents the check indocs/startup-runtime.md.Verification
Verified on commit
e451c80d(rebased ontoupstream/main8f438025):& .\tools\dev.ps1; ctest --preset dev -N->Total Tests: 983cmake --build --preset dev --target startup_trap_header_check-> exit 0 (0 errors, 0 warnings)cmake --build --preset release --target startup_trap_header_check-> exit 0 (0 errors, 0 warnings)ctest --preset dev --no-tests=error -R "^startup_trap_header(_prefix_control|_precedence_control)?$" -E "cpu_native_archive_" -V-> 3 passed, 0 skipped, 0 failed (startup_trap_header: 38 cases;startup_trap_header_prefix_controlandstartup_trap_header_precedence_control:WILL_FAILpassed)ctest --preset release --no-tests=error -R "^startup_trap_header(_prefix_control|_precedence_control)?$" -E "cpu_native_archive_" -V-> 3 passed, 0 skipped, 0 failed (startup_trap_header: 38 cases;startup_trap_header_prefix_controlandstartup_trap_header_precedence_control:WILL_FAILpassed).\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py->form coverage checks passed(exit 0).\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py->Checked 16 Markdown files and 342 links.(exit 0).\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py->Checked 1245 tracked source, script and build/CI files: no bidirectional controls.(exit 0)Evidence and follow-up
tests/startup_trap_header_check.cppis an original test written for this repository againstsrc/startup_stubs/trap.hand requires no GPU or title files.Checklist
AI-assisted.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T16:00:58Z.
Review: COMMENTED
Review: trap header switches and exit codes
Verified
tests/startup_trap_header_check.cppwith a MinGW-w64 cross compiler (-std=c++17 -O1 -static -I src) and ran it under Wine:startup_trap_header: PASS (38 cases), exit 0. The 38 matches the count indocs/startup-runtime.md. Local shim:_ReturnAddress()was replaced with__builtin_return_address(0)because MinGW has no such intrinsic. Header semantics are otherwise untouched.python tests/docs_links_check.py: self-test passed, 16 files / 342 links OK.startup_local_logpattern (include dir, C++17, twoWILL_FAILcontrols). No new dependencies.src/startup_stubs/trap.h(one change at a time, test rebuilt and run each time). Detected (test fails): exit-code base constant changed; module shift 8 -> 16; wildcard*branch removed; wildcard treated as prefix; case-insensitive compare; item length check removed (prefix match); list buffer 2048 -> 4096; E_FAIL value changed; CONTINUE return value 0 -> 1;continues()reading the FAIL variable; continue/fail precedence swapped (test process is killed by its own trap, rc != 0); X2W_TRAP_LOG write removed; stderr write removed; log file opened with CREATE_ALWAYS instead of append; CRLF -> LF;x2w-continuetag renamed;TerminateProcesscode with index forced to 0 / literal 1 /ExitProcess(3)instead of terminating.n >= sizeof(list)->n >cannot differ, becauseGetEnvironmentVariableAreturns length+1 when the buffer is too small;exit=0x%08lx->0x%lxis invisible because every code has 8 hex digits.Findings
P2 - the "masks to 8 bits" exit-code case cannot detect removal of either mask (
tests/startup_trap_header_check.cpp:64)exit_code(0x1ABu, 0x2CDu)expected0xE257ABCDu.0xE257already has bit 16 set;0xAB00already has bit 9 set). So the unmasked result is still0xE257ABCD.trap.h:28change((module_id & 0xFFu) << 8) | (index & 0xFFu)to(module_id << 8) | index(and separately drop only one mask): test still prints PASS (38 cases), exit 0. Expected: FAIL.0x100, index0(expect0xE2570000) and module0, index0x100(expect0xE2570000), or module0x1FF/ index0x200checked separately. The two 8-bit-masking clauses in the documentation (docs/startup-runtime.md, "8-bit module/index bit-masking") are therefore unguarded.P2 - the child process can be left running when it does not exit (
tests/startup_trap_header_check.cpp:230-235)WaitForSingleObject(pi.hProcess, 10000)is followed byCloseHandlewith noTerminateProcessonWAIT_TIMEOUT.fire()toSleep(INFINITE)instead ofTerminateProcess: the test reportsFAIL child trap terminates promptlyand exits 1, but the--child-fireprocess stayed alive (still running 4+ minutes later, and it kept the stdout pipe open so the parent shell/ctest output pipe never closed). Expected: the test kills the child on timeout.wait != WAIT_OBJECT_0, callTerminateProcess(pi.hProcess, 1)before closing the handle (or use a job object with kill-on-close).nit - an aborted or failed run leaves a temp folder that can poison later runs (
tests/startup_trap_header_check.cpp:132)x2w_trap_hdr_<pid>and is created withCREATE_NEWsemantics. When the test dies mid-run (for example the precedence mutant is killed by its own trap, or the timeout case above leaves files open), the folder stays. A later run with the same PID fails at "create temporary trap folder" (GetLastError=183) before testing anything. This was easy to hit under Wine (deterministic PIDs); on Windows it needs PID reuse, so it is rare. Consider a unique suffix (tick count / GUID) or removing a stale folder first.nit - the child exit code is compared with the code under test (
tests/startup_trap_header_check.cpp:236)child_exit == x2w_trap::exit_code(0x42u, 0x07u)recomputes the expectation with the header. It is covered independently by the literal checks at lines 61-64 and theexit=0xe2574207log literal, so no regression is missed today, but a literal0xE2574207uwould be self-contained.nit - whitespace and unmatched-case behaviour is not pinned (
tests/startup_trap_header_check.cpp:67-119)"Alpha, Beta"matchingBeta) passes. The header currently treats items literally; if that is intended, one case such as"Alpha, Beta"must not matchBetawould document it.nit - docs (
docs/startup-runtime.md, Switches table)X2W_TRAP_FAIL, but the Switches table still lists onlyX2W_TRAP_CONTINUE. Optional.nit - controls are redundant with the main run
startup_trap_header_prefix_controlandstartup_trap_header_precedence_controlflip the test's own expectation, so they fail on the correct header and passWILL_FAILonly if the main run's assertion would also fail. They are consistent with the existingstartup_local_log_guard_drop_controlpattern; I found no defect, only duplication.Not verified
_ReturnAddress/__fastfail/ MSVC codegen of the real header were not exercised (shimmed as described above).dev/prCMake presets and the actualctestinvocation were not run; test names and regex in the docs were checked by reading only.0xE2574207) is checked only as a Wine/Windows value; no POSIX 8-bit truncation case applies because the test is Win32-only.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T16:53:12Z.
Updated
tests/startup_trap_header_check.cppanddocs/startup-runtime.md(commit950c0578) with distinct 8-bit overflow mask checks (module and index separately verified against0xE2570000u), child process termination on timeout, and updated case count to 40, and verified in dev and release. AI-assisted.Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T17:03:51Z.
Review: COMMENTED
Re-review at
head_sha:950c057885270b968735a32d5c8b4787b240d1ee(new commit950c0578on top ofe451c80d).Findings
P2 still open, half fixed: the module mask is still unguarded (
tests/startup_trap_header_check.cpp:64)exit_code(0x100u, 0u) == 0xE2570000u("masks module overflow bits") cannot fail. Without the mask the module's bit 8 lands on bit 16 of the result, and0xE257already has bit 16 set (0x57=0101 0111). So the unmasked value is still0xE2570000.src/startup_stubs/trap.h:28I changed(module_id & 0xFFu) << 8to(module_id) << 8. The test still printsstartup_trap_header: PASS (40 cases), exit 0. Expected: FAIL.(index & 0xFFu)now givesFAIL exit_code masks index overflow bits,1 of 40 FAILED, exit 1.0xE257. Bit 19 is clear (bit 3 of0x57), soexit_code(0x800u, 0u) == 0xE2570000u. I added that one line to a local copy: with the module mask removed it fails (1 of 41 FAILED), and with the unmodified header it passes (PASS (41 cases)).docs/startup-runtime.mdwould then say 41 instead of 40.P2 resolved: child process left running on timeout.
fire()is now followed byif (wait != WAIT_OBJECT_0) TerminateProcess(pi.hProcess, 1);before the handles are closed. Reproduced the old failure scenario: I changedfire()in a copy of the header toSleep(INFINITE). The test now reportsFAIL child trap terminates promptlyandFAIL child trap exits with encoded module/index exit code(2 of 40 FAILED) and returns, andpgrepafterwards shows no leftover--child-fireprocess.Other changes in the commit, no issue: the temp folder name now has
GetTickCount64()appended (avoids a reused-PID collision); the child exit code is checked against the literal0xE2574207uinstead of callingexit_code()again, so the check no longer shares code with the thing it tests; docs count changed 38 -> 40, which matches the test output.Verified
Limits: Linux x86-64 only. This file is Windows code, so I cross-built it with MinGW-w64 (
x86_64-w64-mingw32-g++ -std=c++17 -O1 -static -I src) and ran it under Wine. Not MSVC, not real Windows, no ctest. Local shim:_ReturnAddress()replaced by__builtin_return_address(0)in both the test and a copy oftrap.h(MinGW has no such intrinsic); the semantics of the header are otherwise untouched.startup_trap_header: PASS (40 cases), exit 0.TerminateProcess->Sleep(INFINITE)infire()): detected, child is killed, no leftover process.trap.houtside the PR worktree.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T17:12:17Z.
Updated
tests/startup_trap_header_check.cpp(added the bit 19 module-overflow assertionexit_code(0x800u, 0u) == 0xE2570000u) anddocs/startup-runtime.md(updated case count to 41) in commit0a0dbf82, and verified in dev and release. AI-assisted.Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T19:28:36Z.
Review: COMMENTED
Re-review at
0a0dbf8222a4fcbe4dcaa874de080103cbe2d34c(new commit0a0dbf82on top of950c0578). Comment only, not an approval.The earlier P2 is fixed
My previous review said the module-mask case could not fail, because bit 8 of the module id lands on a bit of
0xE257that is already set. The new lineexit_code(0x800u, 0u) == 0xE2570000u(tests/startup_trap_header_check.cpp, "masks module overflow bit 19") uses bit 19, which is clear in0xE257, so it can fail. Confirmed on this head:trap.h)PASS (41 cases), exit 01 of 41 FAILED: "exit_code masks module overflow bit 19", exit 11 of 41 FAILED: "exit_code masks index overflow bits", exit 12 of 41 FAILED("max 8-bit fields", "masks module and index to 8 bits")1 of 41 FAILED: "X2W_TRAP_FAIL export returns E_FAIL"The case count in
docs/startup-runtime.mdnow says 41 and matches the output. The child-process timeout kill and the unique temp folder name from the previous commit are still in place.Commands run
x86_64-w64-mingw32-g++ -std=c++17 -O1 -staticon the test, run under wine, with the same two shims as before applied to a copy oftrap.houtside the worktree:_ReturnAddress()to__builtin_return_address(0), and__fastfailto__builtin_trapafterTerminateProcess.git diff --check origin/main...HEAD: clean.docs_links_check.py(16 files, 342 links) andbidi_controls_check.py(1245 files): pass.Still open, all nits
"Alpha, Beta"andBeta) is still not pinned by a case.docs/startup-runtime.mdSwitches table (line 290) listsX2W_TRAP_CONTINUEbut notX2W_TRAP_FAIL.WILL_FAILcontrols flip the test's own expectation, so they add no detection beyond the main run; they match the existingstartup_local_log_guard_drop_controlstyle.Not verified
Linux only: cross-built with MinGW-w64 and run under wine, not with MSVC on Windows.
_ReturnAddressand__fastfailwere shimmed as above. Thectestpresets were not run.Verdict
No P1 or P2 left on this head. The test now guards both masks, and the case count and docs agree.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T20:33:31Z.
Review: COMMENTED
Review of
0a0dbf8222a4fcbe4dcaa874de080103cbe2d34c(startup_trap_header_check, TIER_B_TESTS), checked out at exactly that SHA. No P1, no P2 found. Delta read of the earlier reviews: the earlier P2 (module mask bit 19 unguarded) is fixed by the newexit_code(0x800u, 0u)case.Commands executed locally (Linux):
git diff --check origin/main...HEAD: rc 0python3 tests/docs_links_check.py: rc 0 (16 files, 342 links)python3 tests/bidi_controls_check.py: rc 0 (1245 files)--mutate prefix-matchrc 1;--mutate fail-firstrc 1 (both negative controls fail as designed).Windows Server 2025 (
win2025 1555 --wait 900, target startup_trap_header_check, regex startup_trap_header):Mutations on src/startup_stubs/trap.h (each reverted with git checkout -- .):
(A narrower module mask 0x7FFu survives: it still clears bit 19. That is a gap in the 0x100 case only, not a defect.)
AI-assisted; commands listed were executed locally.