Skip to content

Launcher: roll back and the Engine.ini revert change only WrongGPU's bytes, through one handle - #209

Merged
phantoslayer21 merged 6 commits into
mainfrom
fix/rollback-engine-ini-ownership
Nov 15, 2026
Merged

phantoslayer21 merged 6 commits into
mainfrom
fix/rollback-engine-ini-ownership

Conversation

@phantoslayer21

@phantoslayer21 phantoslayer21 commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

Roll back and the Engine.ini revert now follow the ownership rule from #47. A file is changed only through one handle:
open it, hash or read it, then write, replace or delete it from that handle, with no write, rename or delete sharing in
between. It is changed only when its bytes are WrongGPU's. Otherwise the file is kept and the result names it. A missing
file is created only with fail-if-present (CREATE_NEW).

Fixes #184
Fixes #186

One PR for both issues because they share the new primitives in util.h / util.cpp: PinnedFile::Create (held
CREATE_NEW), ReadBytes, CopyOut, SetReadOnly, OverwriteFrom, Flush, and WriteFileNew. They also share the
test seams: RestoreStep is no longer file-local, and SetFaultHook / TestFault inject a write fault at named steps.

Review fixes

One line per review item (line numbers of the reviewed head 28dbde8). All tests are in tests/launcher_core_tests.cpp.

  1. previous / previous.swap cleanup. RemoveTree is gone from roll back. RemoveListed deletes only the files
    previous.txt lists, each only while its bytes are the stated hash (DeleteIfSha: hashed and deleted through one held
    handle, never through a link in the path). previous.txt itself goes last, only while it is the list that was read.
    Folders go only when empty. Anything else stays and is named in the result. The cleanup runs after the writes and the
    record. The new swap folder is always fresh (previous.swap, else previous.swap-2, ...). An earlier swap folder is
    never cleared before the writes; afterwards it goes by its own list. Tests: "previous\ goes only by its list..." and
    "an earlier swap folder is never cleared before the writes...". Negative control: putting RemoveTree(prev) back fails
    the first of these.

  2. Partial roll back. Every earlier file is held and hashed before the first write, and written through that handle
    (OverwriteFrom). A damaged one stops the roll back before any write. If a write fails after the writes have started,
    every file already changed is put back from its swap copy through its held handle. That includes the torn file, a file
    the roll back created (deleted again) and a file it removed (created again). The record is saved only after every file
    is in. A failed save is undone the same way. Tests: "a write that fails on the second file..." (fault seam
    rollback-write; record bytes and previous\ unchanged), "...a file the roll back created goes again...", "an earlier
    file with other bytes...", "the earlier files are held from their check to the write". Negative control: skipping the
    undo fails the write-fault test.

  3. Double roll back after a missing payload file. The new previous.txt records it as file=absent:<record hash>|<name>.
    The second roll back removes the recreated file through its held handle, only while its bytes are the ones the record
    names. Other bytes stop that roll back with a clear message ("...had no such file..."), and nothing changes. An older
    launcher reads absent:... as a hash no kept file has, so it refuses ("damaged") rather than mixing two versions.
    Tests: "a payload file missing at the roll back is recorded as absent; the second roll back takes the recreated file
    out...", "...a third roll back creates it again", "...a file there with other bytes stops the second roll back...".

  4. Engine.ini policy and crash-safe write. Policy, now stated in the README: only WrongGPU's own line goes, also from an
    Engine.ini the player changed elsewhere. A file WrongGPU created is deleted only while its bytes are exactly the ones
    WrongGPU wrote; a changed one stays, without the line (before, it was also deleted when only whitespace was left).
    The write never goes over the file in place:

    • the corrected bytes go into a new file in the same folder (CREATE_NEW), which is flushed (result checked) and
      hashed back;
    • the held original is read again and must still be the bytes the edit was computed from;
    • then ReplaceFileW, with a backup name and no ignore flags, which keeps attributes, ACL and creation time.

    The remaining window (documented in the README): the handle closes just before the replace. Another program's change in
    that moment ends up at the backup name, is compared with the bytes read, and is put back at the name if it differs.
    The result says so. Tests: "from a file the player changed elsewhere only WrongGPU's line goes (policy); a corrected copy
    replaces the file (its attributes kept, nothing left beside it)...", "a write fault part way leaves Engine.ini byte for
    byte as it was..." (seam rrconfig-write), "a change by another program in the moment before the replace is not lost..."
    (step rrconfig-replace), "a file WrongGPU created and something changed since stays...". Negative control: writing in
    place again (with the seam at that write) fails the policy test (the file id check) and, with that check left out, the
    torn-write test.

  5. rr-config backup deletion. The backup's SHA-256 is recorded when it is made, in a new record line
    rrconfig-backup\t<sha>. An older launcher keeps it as a line it does not know. The revert deletes the backup only while
    its bytes are that hash (a record from before this line: the bytes this revert read), through DeleteIfSha. Otherwise it
    stays and the result names it. A backup that is not the recorded copy is also never used to recreate a missing
    Engine.ini. Tests: "a backup with other bytes than the recorded hash is not deleted...", "the backup's hash is kept in a
    line of its own (rrconfig-backup) and read back".

  6. Read-only restore. The result of putting back the read-only attribute is checked (in-place, replaced and recreated
    file). A failure is said in the result and the backup is not deleted. Test: "a read-only attribute that cannot be put back
    is said, and the backup stays" (seam rrconfig-readonly).

Re-check fixes (REVIEW-209b)

a. The corrected copy is verified after the replace. The copy is closed before ReplaceFileW (the replace needs it
unshared), so it is not trusted afterwards. The live Engine.ini is then held (no write, rename or delete sharing) and
must hash to the corrected bytes. If it doesn't, it is left as it is and never overwritten. The file it replaced stays
beside it as Engine.ini.wronggpu-old, the backup is kept, and the result says so. Test: "a corrected copy swapped
before the replace is caught after it..." (the copy is replaced at step rrconfig-replace). Negative control: dropping
the hash check fails it.
b. The race recovery never overwrites an unverified destination. The old file is no longer moved over Engine.ini by
name (MoveFileExW(REPLACE_EXISTING) is gone). A change made just before the replace is written back through the held
live file, and only after that file is verified as WrongGPU's own copy. A change made after the replace fails that
check, so it is left as it is, the earlier change is kept beside it, and the player is told. Test: "a second change
after the replace is never overwritten by the recovery..." (changes at rrconfig-replace and at a new step
rrconfig-replaced).
c. Empty content stays empty. When taking out WrongGPU's line leaves nothing, the empty file is written. The backup from
before the fix is never put back. A file is deleted only if WrongGPU created it and it is still byte for byte what
WrongGPU wrote. Test: "taking out WrongGPU's line from a file the player emptied of their own settings leaves it
empty...". Negative control: putting the backup fallback back fails it.
d. Read-only after a failed replace. The read-only attribute is put back, the result is checked, and the warning names
any failure. PinnedFile::Close and a failed PinnedFile::Open now report a read-only bit they could not put back
(ReadOnlyLost), and the Engine.ini open-failure messages include it. Tests: "a failed replace puts the read-only bit
back..." and "a failed replace whose read-only restore fails says so in the warning..." (fault seams
rrconfig-replacefile, rrconfig-readonly).

Re-check 2 fixes (REVIEW-209c)

  • b. No write into the live file after the replace. Matching bytes no longer count as ours. The live Engine.ini is
    WrongGPU's corrected file only if it is the copy itself: the same volume and file index, captured while the copy was
    held. It must also hold the corrected bytes. The recovery write is gone. When another program wrote Engine.ini just
    before the replace, its version is kept beside the corrected file as Engine.ini.wronggpu-their-change (moved only
    into a free name), the backups stay, and the warning names both files. Nothing after the replace writes into a file
    WrongGPU did not just create. Test: "a distinct file with the corrected bytes swapped in after the replace is not taken
    for WrongGPU's...": a new file with identical bytes is swapped in at rrconfig-replaced, and its file id and bytes stay
    untouched. The earlier "change before the replace" test now expects the their-change file. Negative control: dropping
    the identity check fails the new test.
  • P2. Read-only on the verification paths. The live file is opened without write access, so its read-only attribute
    is never cleared there. ReadOnlyLost() and the Close() result are still checked on the open-failure and mismatch
    path; a failure is said in the warning, and the backups stay. Tests: "the verification opens another program's
    read-only file without changing its attribute..." and "on the verification's mismatch path a read-only restore failure
    is said in the warning..." (fault seam rrconfig-verify-readonly).

#184: Roll back (launcher.cpp Rollback), as it is now

  1. Every earlier file in previous\ is opened with PinnedFile::Open(path, false) and hashed through its handle. Each must
    match previous.txt.
  2. Every payload file of the roll back is opened with PinnedFile::Open(path, forWrite) and hashed through the handle,
    then compared with the record's inst for that path. A file previous.txt lists as absent must be WrongGPU's too; it is
    removed at the end.
  3. Missing payload files (the player deleted one) are created with PinnedFile::Create (CREATE_NEW) and held like the
    others.
  4. If any file is not WrongGPU's, the roll back stops before anything changes, with exit 2 (refused). The message and the
    warnings name each file. This covers other bytes, a hard link (ERROR_TOO_MANY_LINKS), a link or folder, and a file
    that appeared where one was missing. Files created in step 3 are deleted again through their handles.
  5. The current bytes are copied out through the held handles into a fresh swap folder (CopyOut, CREATE_NEW), with
    previous.txt (absent ones listed as such).
  6. The earlier payload is written through the held handles from the held sources and verified through them. Files the
    earlier version did not have are deleted last. A failure undoes everything (Review fix 2).
  7. The record is saved, the handles are released, previous\ is removed by its list, the swap folder takes its place, and
    older swap folders go by their lists.

#186: Engine.ini revert (rrconfig.cpp RevertRrConfig, called from uninstall, =off, and relocation)

  • Engine.ini is opened once (PinnedFile::Open(path, true)) and read through that handle, and the edit is computed from
    those bytes. Then either a corrected copy replaces it (ReplaceEngineIni, Review fix 4), or the file WrongGPU created is
    deleted through the handle (Delete). Read-only goes back as the player had it (wasReadOnly), and the result is checked.
  • If the file is absent, the player's copy is created with CREATE_NEW through a held handle. A file another program
    created in the meantime stays. The player's copy is kept in wronggpu-setup\rr-config (in a new Engine.ini.kept-<time>
    file when the caller held the copy only in memory, as relocation does). The result names that location, and the backup is
    not deleted.
  • A hard-linked or linked Engine.ini is not written. The existing warning tells the player to take the line out by hand.

What is guaranteed, and what is not

Guaranteed, within the limits of the PinnedFile model described in #47 and in util.h:

  • Roll back writes or removes a payload file only while that file is held and its bytes, hashed through the held handle,
    equal the record's hash. If any file fails that check, nothing is changed. The earlier files are held and checked before
    the first write.
  • A write that fails part way is undone from the swap copies through the held handles. The record changes only after every
    file is in.
  • Roll back deletes in wronggpu-setup\previous and swap folders only files their list names with the stated bytes, and
    only empty folders.
  • While a file is held, a normal open by another program for writing, renaming or deleting fails. So does a replace onto
    that name.
  • A missing payload file or a missing Engine.ini is created only where nothing exists. A file that appears first is never
    replaced.
  • The corrected Engine.ini never goes over the file in place. A stop or power cut leaves the old file or the new one, whole.
    After the replace, the live file counts as done only when it is held, is the copy WrongGPU wrote (same file identity),
    and holds the corrected bytes.

Not guaranteed (remaining narrow races and limits):

  • Folders above the file are resolved by name when it is opened, as in Launcher: coexist with other OptiScaler setups #47. A program that renames or replaces a
    parent folder between operations can make the open reach a different file. For roll back, that file is still compared
    with the record's hash. For Engine.ini, a different file at that path is the one read and edited, but only WrongGPU's
    own line is removed from it.
  • Engine.ini before the open: if another program replaces Engine.ini before WrongGPU opens it, the replacement is the
    file that gets read and edited, and only WrongGPU's line is removed (the stated policy).
  • Engine.ini replace window: the handles close just before ReplaceFileW. Changes made in that moment are detected
    afterwards, when the live file is held and its identity and bytes are checked. That covers the original, the copy, or
    Engine.ini changed or replaced right after the replace. Nothing is then written into the live file. The other
    version is kept beside it, and the result says so. A stop between the replace and those checks leaves the old bytes
    next to it as Engine.ini.wronggpu-old. When another program's version is kept beside the corrected file, the player
    chooses between the two files; WrongGPU does not merge them.
  • Payload writes are in place through the held handle (a game DLL is not replaced by rename). A failure is undone. A
    power cut mid-write can leave a torn payload file, with the record still naming the newer hashes. The next roll back then
    refuses, and Repair puts WrongGPU's file back.
  • Visible while being written: a newly created file (a missing payload file, the restored Engine.ini) exists while it
    is being written, and another program can read it partly written then.
  • Not changed by this PR: the update's SavePrevious still replaces previous\ as a whole (RemoveTree). Also not
    changed: adding the Engine.ini line (ApplyRrConfig, path-based like the install's writes; Launcher: install, repair and update can delete or overwrite a file replaced after the backup #185 area), and the install,
    repair and update writes (Launcher: install, repair and update can delete or overwrite a file replaced after the backup #185).
  • Read-only attribute: when a read-only file is opened for writing, the read-only bit is cleared through a probe handle
    before the held open, as in Launcher: coexist with other OptiScaler setups #47's PinnedFile::Open. It is put back if the file is then not written.

Tested

Windows, CPU only, 1 compile job, on the pushed head (main merged in). The end-to-end runs for the re-check fixes ran with
APPDATA, LOCALAPPDATA and ProgramData pointed at scratch folders.

  • CI launcher configuration (-DWRONGGPU_TESTING=OFF -DWRONGGPU_PRESET_K=OFF -DWRONGGPU_DIRECT_ROUTE=OFF) ctest:
    wg-core, launcher-core, coexist-core, convert-ignore, model-choice, source-size and wording all pass (7/7).
    launcher-core has 386 checks: 348 before this PR, 13 from its first commit, 17 from the review fixes, 5 from the
    re-check fixes, 3 from the re-check 2 fixes. The new ones are listed in the fix sections above. The 13 earlier ones are unchanged: player-changed, hard-linked, held, missing and
    appeared payload files at roll back; Engine.ini held against replace, delete and write; replaced before the open;
    created before the absent-file restore (on disk and in memory); restored when gone; hard-linked.
  • Negative controls (each built and run with one fix reverted, then restored byte for byte):
    • item 1, RemoveTree(prev) back: fails at "previous\ goes only by its list...";
    • item 2, no undo: fails at "a write that fails on the second file...";
    • item 4, in-place write back: fails at the policy test's file-id check, and with that check left out at "a write fault
      part way leaves Engine.ini byte for byte as it was...";
    • re-check a, no hash check of the live file: fails at "a corrected copy swapped before the replace...";
    • re-check c, the backup fallback back: fails at "...leaves it empty; the backup from before the fix is not put back";
    • re-check 2 b, the live file's identity not checked: fails at "a distinct file with the corrected bytes swapped in...".
  • End to end with a WRONGGPU_TESTING launcher (mock payload and map from run_installer_tests.py --prepare-only,
    -DWRONGGPU_PRESET_K=OFF, direct route on):
    • run_launcher_tests.py --only install_settings_uninstall rr_config_fix launcher_uninstall: 186 passed, 1 failed.
      Roll back and roll back again pass, and so do the Engine.ini =off and uninstall cases byte for byte (read-only, BOM,
      the key's earlier value). The failure is "uninstall.exe is the small restorer, not the launcher": it depends on the size
      of a local build, and Launcher: coexist with other OptiScaler setups #47 lists it as a baseline failure on local builds.
    • run_coexist_tests.py: 227 passed, 0 failed.

Not tested

  • The rest of run_launcher_tests.py (rr, re9, direct_rr, adopt / self-install, ui_dump), run_installer_tests.py,
    and the Preset K and direct-route-only builds. None of them reaches the changed code beyond what is listed above.
  • An older launcher reading the new previous.txt (absent:) or the rrconfig-backup record line. This is reasoned from
    its parser (unknown hash, so "damaged"; unknown line, so kept), not run.
  • Any elevated (UAC) run. Nobody was at the PC, so the elevated worker's =off / uninstall path was not exercised; it
    calls the same RevertRrConfig.
  • A parent-folder swap, a real concurrent process (the tests act from the same process at the named steps and through the
    fault seams), a real full disk, a real power cut.
  • ReplaceFileW on file systems other than NTFS, and under Wine.
  • In a game.

…bytes, through one handle

Roll back (#184): every payload file is held (PinnedFile: one handle, no write, rename or
delete sharing), hashed through that handle and compared with the hash the record says
WrongGPU installed. The current files are copied out for a later update and the earlier
ones written through the same held handles. A file with other bytes, a hard-linked file,
a link, or a file that appears where a payload file was missing stops the roll back
before anything changes; the result names it and the file stays. A missing payload file
is created only where nothing is (CREATE_NEW, held).

Engine.ini revert (#186): Engine.ini is opened once, read, compared and then rewritten or
deleted through that same handle. A missing file is created only with CREATE_NEW; a file
another program created meanwhile stays, the player's copy is kept in
wronggpu-setup\rr-config and the result says where. Hard-linked or linked files are not
written.

New PinnedFile primitives (Create, ReadBytes, CopyOut, SetReadOnly) and WriteFileNew;
RestoreStep is shared so the tests can act at the new steps. launcher-core gains 13
checks: player-changed, hard-linked, held, missing and appeared payload files at roll
back; Engine.ini replaced or written while held, replaced before the open, created before
the absent-file restore (on disk and in-memory copy), restored when gone, hard-linked.

Fixes #184
Fixes #186

Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
…ni-ownership

Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
@phantoslayer21
phantoslayer21 marked this pull request as ready for review November 14, 2026 22:17
Roll back:
- Every earlier file is held and hashed before the first write and copied
  through that handle.
- A write that fails part way puts the files already changed back from their
  swap copies, through their held handles; the record changes only after every
  file is in (a failed record save is undone the same way).
- previous and a left-over previous.swap folder go only after the writes, file
  by file: a file previous.txt lists is deleted only while its bytes are the
  stated hash, through one handle; anything else stays and is named; a folder
  goes only when empty. The new swap folder is always a fresh one.
- A payload file missing at a roll back is recorded as absent in previous.txt
  (file=absent:<hash>|<name>); the second roll back removes the recreated file
  only while its bytes are the recorded ones, and refuses otherwise.

Engine.ini revert:
- Policy (documented): only WrongGPU's own line goes, also from a file changed
  elsewhere. A file WrongGPU created is deleted only while it is byte for byte
  what WrongGPU wrote.
- The corrected bytes go into a new file in the same folder (flushed, read
  back), the held original is checked again, then ReplaceFileW (attributes and
  ACL kept). A change by another program in the moment before the replace is
  put back and said.
- The rr-config backup is deleted only while it matches the hash recorded when
  it was made (new record line rrconfig-backup), through one handle; otherwise
  it stays and is named.
- The read-only attribute result is checked; a failure is said and the backup
  stays.

Tests: launcher-core gains 17 checks (write fault mid roll back through a new
test seam, absent round trip, list-only cleanup, Engine.ini policy, torn write,
the replace window, backup hash, read-only failure).

Refs #184
Refs #186

Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
…ni-ownership

Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
@phantoslayer21
phantoslayer21 marked this pull request as draft November 14, 2026 23:16
@phantoslayer21
phantoslayer21 marked this pull request as ready for review November 14, 2026 23:34
…fter the replace)

- The corrected copy is closed before ReplaceFileW, so it is not trusted after
  it: the live Engine.ini is then held (no write, rename or delete sharing) and
  must hash to the corrected bytes. Otherwise it is left as it is, the file it
  replaced stays beside it as Engine.ini.wronggpu-old, the backup is kept and
  the result says so.
- The race recovery no longer moves the old file over Engine.ini by name: a
  change made before the replace is written back through the held, verified
  live file; a change made after it is never overwritten.
- When taking out WrongGPU's line leaves nothing, the file stays empty; the
  backup from before the fix is never put back over it.
- A failed replace puts the read-only attribute back and the warning names a
  failure to do so; PinnedFile::Close and a failed Open now report a read-only
  bit they could not put back.

Tests: launcher-core gains 5 checks (copy swapped before the replace, a second
change after it, an emptied file, a failed replace with and without a failed
read-only restore, through the fault seams).

Refs #184
Refs #186

Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
… replace

- The live Engine.ini counts as WrongGPU's corrected file only when it is the
  copy itself (its volume and file index, taken while the copy was held) and
  holds the corrected bytes; equal bytes in another file are not enough.
- The recovery no longer writes anything into the live file: a version another
  program wrote just before the replace is kept beside it as
  Engine.ini.wronggpu-their-change (moved only into a free name), the backups
  stay, and the warning names both files.
- The verification opens the live file without write access, so another
  program's read-only file is never changed; a read-only bit that could not be
  put back (ReadOnlyLost, the Close result) is still checked and said, and the
  backups stay.

Tests: launcher-core gains 3 checks (a distinct file with identical bytes
swapped in after the replace is left untouched; the verification's mismatch
path with a read-only file, with and without a read-only restore failure).

Refs #184
Refs #186

Signed-off-by: phantoslayer21 <156617979+phantoslayer21@users.noreply.github.com>
@phantoslayer21
phantoslayer21 merged commit 66c3048 into main Nov 15, 2026
9 checks passed
@phantoslayer21
phantoslayer21 deleted the fix/rollback-engine-ini-ownership branch November 15, 2026 00:33
j0nsh1n added a commit to j0nsh1n/wronggpu that referenced this pull request Nov 15, 2026
Brings the launcher branch up to upstream main (217 commits: phantoslayer21#47, phantoslayer21#105,
phantoslayer21#196, phantoslayer21#197, phantoslayer21#209, Model K/L, frame generation, ...). Changes beyond
resolving the three conflicts (CMakeLists.txt project(), util.h's includes,
the restorer test):

- POSIX layer: the Win32 calls main's handle-based restore uses
  (SetFileInformationByHandle with FileBasicInfo, FileDispositionInfo and
  FileDispositionInfoEx's POSIX delete, GetFileInformationByHandleEx
  FileBasicInfo, SetEndOfFile, ExitProcess, GetProcessHeap/HeapFree, CDECL,
  ERROR_TOO_MANY_LINKS/FILE_CORRUPT/READ_FAULT), with posix-layer tests.
- run_launcher_tests.py stays below the 256 KB source limit: the edition
  section moves unchanged to launcher_edition_cases.py and the Linux run's
  plumbing to native_linux.NativeT.
- launcher-native runs only in a -DWRONGGPU_PRESET_K=OFF build (like CI's
  launcher build: the suite expects no seeded wronggpu-asi.ini); its two
  checks that fail on main's own launcher too (phantoslayer21#219) are skipped while
  they fail (native_linux.KNOWN_ON_MAIN).

Verified: fresh build, ctest 22/22 (launcher-native 365 passed, 0 failed,
6 skipped: 4 Windows-only sections, the 2 phantoslayer21#219 checks); reuse lint passes.

Signed-off-by: Jonathan Shin <85975340+j0nsh1n@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant