Skip to content

file_system: T:\ is a drive root for file calls; move and copy hold the folder lock (RT-FS-OVERLAY-ROOT-LOCK-LONGPATH) - #2534

Open
Tyrannys wants to merge 1 commit into
ProjectLeonida:mainfrom
Tyrannys:claude/fs-overlay-root-lock-longpath
Open

Tyrannys wants to merge 1 commit into
ProjectLeonida:mainfrom
Tyrannys:claude/fs-overlay-root-lock-longpath

Conversation

@Tyrannys

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: RT-FS-OVERLAY-ROOT-LOCK-LONGPATH (claim: #1608 (comment)). Found by @kevincommits, item 2 of #1608 (comment). Thanks!

What and why

Three fixes to the GAME: / T: overlay in runtime/shims/file_system.cpp:

  1. T:\ slipped past the root checks. parse_console(L"T:\\") gives the scratch folder's parts (.gta6rt-temp, <run>), not an empty list. RemoveDirectory and MoveFileEx already use drive_root() for that, but the write-open (write_target), DeleteFileW, the copy destination and SetFileAttributesW only checked parts.empty(). So SetFileAttributesW(T:\) changed the host scratch folder's attributes, and a write-open of T:\ was not refused. All four now use drive_root() and give T:\ the same error they give GAME:\ (ERROR_INVALID_NAME, or ERROR_ACCESS_DENIED for attributes).
  2. Move and copy did not hold the destination folder's lock. A write-open and CreateDirectory hold folder_lock(parent) from "does the folder exist" to the host call that makes the entry, so a concurrent RemoveDirectory of that folder comes wholly before or after (the comment at g_folder_locks). MoveFileEx and copy_destination (CopyFileW, CopyFileExW, CopyFile2) did not, so the remove-then-resurrect race stayed open for them. Now:
    • MoveFileEx holds the lock from its destination checks to the host move.
    • copy_destination takes it before its checks and hands it to the caller, which holds it through the host copy.
    • A copy with a progress routine (CopyFileExW with progress, CopyFile2 with pProgressRoutine) keeps the old behaviour. The routine is the title's code, and nothing that holds a folder lock may call back into the title (the same comment). A routine that touched a folder in the same lock stripe would otherwise deadlock.
  3. A long marker path in CreateDirectory. Recreating a removed folder deletes its marker with std::filesystem::remove(marker(...) / kRemovedDirMarker) and no win32_io_path(), unlike every other marker access. On a host without long path support, a marker beyond MAX_PATH cannot be removed, and the new folder is rolled back. It now goes through win32_io_path().

Tested

file_system_test (CPU only, synthetic package and overlay folders, no game data):

  • New T:\ root cases: SetFileAttributesW is refused and the host folder is not hidden; DeleteFileW, a GENERIC_WRITE open and CopyFileW onto T:\ are ERROR_INVALID_NAME. On main all 4 fail; with the fix the whole test passes (544 checks, 0 failures).
  • New long-path case: CreateDirectoryW recreates a removed folder whose marker lies beyond MAX_PATH, and the folder is listed again. It also passes on main on this machine, because long path support is on here (LongPathsEnabled = 1), so it does not show the bug; it keeps the path covered.
  • The folder lock for move and copy has no test of its own: the race needs a RemoveDirectory to land between the check and the host call, which a test cannot place reliably. The existing move, copy and CopyFile2 cases all pass with the lock held.
  • Built with MSVC 17, Release; no new warnings.

GPUs tested: n/a (CPU test).

AI-assisted: yes (Claude Code)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request. (The three overlay fixes of one finding.)
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • Overlap checked: file_system: a move onto an existing GAME: folder reports ERROR_ALREADY_EXISTS (FS-MOVE-ONTO-FOLDER) #2375 changes the "already exists" check in MoveFileEx a few lines above the new lock; whichever lands second needs a small merge there. No other open PR touches these lines.

🤖 Generated with Claude Code

…he folder lock

Three fixes to the GAME:/T: overlay shim:

- The T:\ root parses to its scratch folder's parts, not to an empty
  list, so the parts.empty() root checks in write_target, DeleteFileW,
  the copy destination and SetFileAttributesW let it through: a
  SetFileAttributesW(T:\) reached the host folder. They use
  drive_root(), as RemoveDirectory and MoveFileEx already do, with the
  error each gave for GAME:\.
- MoveFileEx and the CopyFile family did not hold the destination
  folder's lock from the "does the folder exist" check to the host
  move or copy, as a write-open and CreateDirectory do, so a concurrent
  RemoveDirectory of that folder could still be undone. A copy with a
  progress routine does not hold it across the copy, because the
  routine is the title's code.
- CreateDirectory removed a recreated folder's marker without
  win32_io_path(), so a marker beyond MAX_PATH could not be removed on a
  host without long path support, and the folder was not recreated.

Found by kevincommits (ProjectLeonida#1608).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@timbot103

Copy link
Copy Markdown
Contributor

Reviewed at head 11b63a53b on Windows 11 (MSVC 2022 19.44).

Verification

  • Built file_system_test.
  • file_system_test: 544/544 checks passed (including the 2,000-round create/remove directory concurrency checks).

Notes (non-blocking)

  • Drive-root recognition for T:\ (file_system.cpp:249-266, 310-313, 531, 1136-1139, 1389-1390, 1563), extended-length marker cleanup (1213-1220), and holding the destination folder lock across ordinary moves and callback-free copies while dropping it around title progress callbacks (1319-1455, 1543-1551) all look solid.
  • Test coverage suggestion (file_system_test.cpp:939-997, 1437-1456): Consider adding the same RemoveDirectoryW race test for MoveFileExW and callback-free CopyFileW/CopyFileExW/CopyFile2 against removal of the destination parent to directly pin the new lock scope.
  • Pre-existing note (file_system.cpp:1297-1335, 1394-1411): Renaming an overlay-only source directory only locks the new parent stripe, so a concurrent copy into that directory can still race a rename; noted as pre-existing and outside this PR's RemoveDirectoryW scope.

Approve.

AI-assisted review + local MSVC verification.

@zmasi

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

#2534 | head 11b63a5 (merged cleanly with main 6dfca64) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^file_system$" --output-on-failure | red on main: file_system_test fails (544 check(s), 4 failure(s): SetFileAttributesW(T:\), DeleteFileW(T:\), write-open CreateFileW(T:\), and CopyFileW onto T:\ treated T:\ as its per-process host folder rather than a drive root because parts was non-empty) | green on PR+main: 1/1 CTest passes (file_system 544/544 checks in 17.10s, covering T:\ drive-root refusal, destination folder_lock serialization across MoveFileExW/CopyFile*, and extended-length > MAX_PATH .leonida_removed_dir marker cleanup on CreateDirectoryW); check_no_game_data.py 0 violations | Windows 11, RTX 3090 (610.88), Vulkan SDK 1.4.363.1 | AI-assisted: yes

@timbot103 timbot103 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Static code audit against PR head 11b63a53b and origin/main ec740bcc3.

  • runtime/shims/file_system.cpp:1547 checks only pProgressRoutine. A valid CopyFile2 V2 block can supply only pProgressRoutineOld, so native CopyFile2 can call back while the destination stripe is held. A callback that mutates that folder can attempt to reacquire the same non-recursive mutex. Please check both callback fields for V2 and add a native Windows regression for a re-entrant legacy callback. Win32 V2 parameters.
  • runtime/shims/file_system.cpp:1311 still mirrors the parent during a case-only package move before taking the new lock. If deletion hides the source and RemoveDirectoryW then hides the parent after the move's source lookup, copy-up brings the parent back. Extend the destination lock and recheck the parent and source on that branch.

The new checks cover canonical T:\ refusals and long-folder recreation. The concurrency fixture covers create/remove. A direct marker absence assertion and move/copy removal cases would pin the other changes.

AI-assisted.

@lixonik lixonik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed 11b63a53b and tested file_system on Windows 11.

Verification (Windows 11, MSVC 19.44, Ninja Release):

  • Built file_system_test.
  • ctest -R '^file_system$': 1/1 passed (12.27 s).
  • python -I scripts/check_no_game_data.py: passed.

Code audit:

  • The drive_root() guards for T:\, the win32_io_path() marker removal, and holding folder_lock(parent_of(dst)) across move_file_ex_w / copy_file_w / copy_file_ex_w are solid.
  • Independently confirming both findings raised by @timbot103 before approving:
    1. runtime/shims/file_system.cpp (copy_file2): calls_back only checks parameters->pProgressRoutine. In Windows SDK 10.0.26100.0 (winbase.h), COPYFILE2_EXTENDED_PARAMETERS_V2 also carries LPPROGRESS_ROUTINE pProgressRoutineOld (which takes precedence over pProgressRoutine), so a V2 caller setting only pProgressRoutineOld would execute guest callback code while holding the non-recursive folder_lock.
    2. runtime/shims/file_system.cpp (move_file_ex_w, same_entry(src, dst) branch): the case-only rename copy-up (create_directories + CopyFileW when !from_view.in_overlay) still executes before std::lock_guard folder(folder_lock(parent_of(dst))), leaving a window against concurrent RemoveDirectoryW.

AI-assisted (Antigravity, Gemini).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants