Skip to content

sampler_source_census: refuse a --json that would overwrite the carve index, a listing or a container it reads - #6266

Open
evilclanker9000 wants to merge 3 commits into
ItsNotRexed:mainfrom
evilclanker9000:deepseek/8-sampler-census-json
Open

evilclanker9000 wants to merge 3 commits into
ItsNotRexed:mainfrom
evilclanker9000:deepseek/8-sampler-census-json

Conversation

@evilclanker9000

Copy link
Copy Markdown
Contributor

The census writes its --json report last, after it has read the carve index, every listing, every DXBC container and every FSSM header. It had no guard on that path, so a report naming one of those inputs silently replaced it: --json <work>/shaders/dxbc/index.json overwrote the carve index make_game.py trusts as proof the carve finished, --json <work>/shaders/dxbc/disasm/a.s replaced a listing, and --json <work>/shaders/dxbc/a.dxbc replaced a container the run had just read. tools/heap_index_census.py reads the same folder and already refuses those names; this does the same with the shared tools/report_paths.same_report_path helper, which also catches a hard link under another name, and records the refusal in the module docstring.

Test: python -I tests/test_sampler_source_census.py (no game files, no GPU).

Before (the tool from main): the two new tests fail, exit 0 with the input replaced.

python -I tests/test_sampler_source_census.py SamplerSourceCensus.test_json_report_refuses_to_overwrite_an_input SamplerSourceCensus.test_json_report_refuses_a_hard_link_to_an_input
Ran 2 tests in 0.358s
FAILED (failures=2)

After (this branch): all 32 pass.

python -I tests/test_sampler_source_census.py
Ran 32 tests in 2.687s
OK

The first test also checks that nothing is printed before the refusal and that the carve index still holds its bytes; the second ends with a negative control (a --json name with no link to an input is still written).

Item: EGEN-SAMPLER-CENSUS-JSON

Notes

  • Wrote the listing check into the same loop as the container checks rather than relying only on "must not write inside the disasm folder": the folder check cannot see a hard link placed outside it, and same_report_path can.
  • Left the exit code and the one-line ERROR: wording identical to heap_index_census, so a script that reads either tool sees the same shape.

No GPU needed.

AI-assisted.

…a listing it reads

The census is written last, after every input has been read, so a report landing on the carve index, a listing, a DXBC container or an FSSM header silently replaced an input the run just used. Refuse those names with report_paths.same_report_path, as heap_index_census already does, and say so in the module docstring.
… container is refused

Test fails on the previous sampler_source_census (exit 0 and the input replaced) and passes with the guard: the carve index, a listing inside disasm and a DXBC container each give ERROR and exit 2, nothing is printed, and the index still holds its bytes.
…s refused too

The same input under another name must not be written over either; a name with no link to an input is still written (negative control). os.path.samefile alone would miss the second name on a volume whose name rules hide it, which is why report_paths.same_report_path is used.
@AsgerCode

Copy link
Copy Markdown

Tested 80fc560 on Windows 11 (NTFS), Python 3.12.10 (CPU only, no GPU, no game files): pass

What I ran:

  • python -I tests/test_sampler_source_census.py: 32 tests, OK (at the PR head, and with the two PR files on current main f25ba44).
  • With the two PR files on main: tests/test_heap_index_census.py 13 OK, tests/test_report_paths.py 22 OK (1 skipped), tests/test_tool_startup.py OK.
  • Negative control (merge-base tools/sampler_source_census.py): both new tests fail, 0 != 2, as the description says.
  • Merge with current main (255 commits ahead): clean. main changed tools/report_paths.py since the merge base (report_paths: resolve each path once in same_report_path, not twice (EGEN-REPORTPATHS-RESOLVE-ONCE) #6268, resolve once). The by-hand runs below use that new same_report_path.

Edge cases by hand, using the test's Work fixture (a ps, c cs with an FSSM header), on main + this PR:

--json Result
index.json, or a hard link to it exit 2, carve index unchanged
disasm/a.s, disasm/new.json, and disasm given by its 8.3 short name with --json in it by the long name exit 2, "must not write inside the disasm folder"
a.dxbc, A.DXBC, a.dxbc by 8.3 short root, disasm/../a.dxbc, fssm/c.fssm exit 2, input unchanged
sibling disasm2/x.json (prefix check) exit 0, written
b.dxbc, where disasm/b.s exists but the carve index has no b row exit 0, b.dxbc replaced

Non-blocking point, the last row: census() takes its names from os.listdir(disasm), not from the carve index. read_shader() calls hic.sfi0_class() (which opens <name>.dxbc) before it skips a listing as "not in the carve index". The new guard only loops over stages, so the container of a listing with no index row is read and then replaced by the report. This needs a carve index and listings that disagree, so it is rare. heap_index_census.py on main has the same gap (its census() also calls sfi0_class before the stage check). A small fix, which I tried: loop over sorted(set(stages) | {n[:-2] for n in os.listdir(args.disasm) if n.endswith(".s")}). With that change the b.dxbc case gives exit 2 and the 32 tests still pass.

Overlap: no other open PR changes this guard. #4268 adds the whole tool again and already has add/add conflicts with main in both files, so it is not a duplicate of this change.

AI-assisted (Claude Code).

@kapitche

Copy link
Copy Markdown
Contributor

Tested 80fc560 merged with main 496da20 on Linux/Python 3.12.16: pass

Commands run (merged worktree, -I isolated mode):

  • python -I tests/test_sampler_source_census.py -> Ran 32 tests, OK
  • Negative control: git checkout upstream/main -- tools/sampler_source_census.py, rerun -> FAILED (failures=2): test_json_report_refuses_to_overwrite_an_input and test_json_report_refuses_a_hard_link_to_an_input. Restored with git checkout HEAD -- tools/sampler_source_census.py.

Diff read: --json is refused (exit 2, one ERROR: line) when it resolves inside the disasm folder it reads, or to the same file as any stage's .s listing, .dxbc container or .fssm header (via report_paths.same_report_path, so hard links and case aliases count); a path on another drive is treated as outside.

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.

3 participants