Skip to content

fix(tarpit): keep the Python peer slot deterministic across processes - #350

Merged
qybaihe merged 3 commits into
qybaihe:mainfrom
DeryFerd:fix/tarpit-python-peer-determinism
Nov 30, 2026
Merged

qybaihe merged 3 commits into
qybaihe:mainfrom
DeryFerd:fix/tarpit-python-peer-determinism

Conversation

@DeryFerd

Copy link
Copy Markdown
Contributor

generatePythonTarpit builds a shared slot and folds hash(name) of two generated functions into its peer field. Python randomises str.__hash__ per process through PYTHONHASHSEED, so the same hardened module produces a different peer on every run while epoch stays stable.

Runtime behaviour does not change because peer is never read back. The decoy state is documented as seed driven, though, and a value that drifts between runs is a cheap consistency signal for anyone comparing two executions of the same artifact.

What changed

  • The emitted expression uses sum(map(ord, name)) instead of hash(name). It still reads the function name, so the cross reference that the original line created is preserved, but the value no longer depends on the process hash seed (src/shields/tarpit/tarpit-generator.ts).
  • tests/tarpit-python-peer-determinism.test.ts generates the module once, executes it under PYTHONHASHSEED=1 and PYTHONHASHSEED=2, and asserts the slot value is identical. The test is skipped when no Python 3.8 or newer interpreter is available, matching the other Python tests.

Tradeoffs

The deterministic value is not the same number the old code produced at runtime, but nothing consumed the old value. Keeping the name expression costs a tiny amount of generated code and keeps the xref density that the line was added for. A constant would have removed the reference and changed the decoy shape.

Verification

  • npx vitest run tests/tarpit-python-peer-determinism.test.ts tests/tarpit-suite.test.ts tests/opaque-predicates.test.ts tests/exhaustion-suite.test.ts passes, 16 tests.
  • The new test fails on the old code under two different hash seeds, which is how the defect was reproduced.
  • npm run typecheck and npm run build are clean.

DeryFerd and others added 3 commits November 30, 2026 20:46
The generated Python tarpit folded str.__hash__ of function names into the shared-slot peer, and Python randomizes that hash per process. Two runs of the same hardened module therefore exposed different decoy state, a cheap consistency tell. sum(map(ord, ...)) keeps the name touch for xref density while staying reproducible; a test runs the module under two PYTHONHASHSEED values.
@qybaihe

qybaihe commented Nov 30, 2026

Copy link
Copy Markdown
Owner

Thanks @DeryFerd — good catch on PYTHONHASHSEED randomizing hash(name) across processes. Two small maintainer fixes were pushed onto your branch to get the full pipeline green:\n\n1. 168ad35: changed sum(map(ord, fn.__name__)) to sum(ord(ch) for ch in fn.__name__) in src/shields/tarpit/tarpit-generator.ts. Passing ord as a value tripped isolateInjectedPythonCode's template-drift guard (builtin name 'ord' is used in an unrecognised position); calling ord(ch) and sum(...) directly keeps the name touch deterministic and passes builtin qualification.\n2. 55dab70: updated tests/python-decoy-density.test.ts to assert the behavioral contract (the ["peer"] line reads sibling __name__s and no longer uses non-deterministic hash(...__name__)) instead of pinning the old hash(...) implementation.\n\nAll 4 CI jobs are green now (Ubuntu Node 22.23.4 & 24, macOS Node 24, Windows Node 24). Merging with rebase so your original commit stays intact with your authorship.

@qybaihe
qybaihe merged commit 7eb258f into qybaihe:main Nov 30, 2026
4 checks passed
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.

2 participants