CPython Runtime Introspection
Prove the process and runtime
- Handle one-file parent and child processes separately.
- Record module bases and the exact Python version.
- Capture an input or output stack and confirm it enters CPython.
- Match injector and target architecture.
Prefer semantic injection
Inject Python-level code instead of reverse engineering the evaluator first:
- Acquire the GIL with
PyGILState_Ensure. - Execute a small payload with
PyRun_SimpleStringFlagsor a compatible API. - Release the GIL.
- Install
scripts/frame_probe.pybefore the interestinginput()or verifier executes.
See references/injection-boundaries.md for native API sequencing.
Walk caller frames
At a semantic choke point such as builtins.input:
- start at
sys._getframe(1) - walk
frame.f_back - record
co_filename,co_name,f_lineno,co_names, andco_consts - safely serialize local/global values
- write
marshal.dumps(frame.f_code)using the target runtime - disassemble in the same Python version
Do not rely on native stacks alone; they usually stop at generic evaluator functions.
Fallback boundaries
If Python-level injection is unavailable, instrument in this order:
PyEval_EvalCodeor version-specific evaluator boundaryPyMarshal_ReadObjectFromStringPyImport_ExecCodeModule*- input or comparison builtins
Quality gates
- Preserve the original input function and restore it after capture.
- Limit repr length and handle repr exceptions.
- Export code objects before modifying locals.
- Confirm dumped marshal objects load under the exact Python version.
- Validate the recovered verifier independently against live behavior.


