Repository navigation
Conversation
`CodeType.replace(co_code=...)` validates only that every byte decodes to a known opcode. It does not check that the resulting stream is consistent with itself, so an operand can address a value stack slot the frame never pushed. The accessors that read those slots relied on `debug_assert!`, which is compiled out in release builds: `COPY 100` on an empty stack wrapped `stack_len - idx` and read outside the frame's storage. Check the index in `LocalsPlus::stack_index`, `stack_index_mut`, `stack_swap` and `stack_truncate`, and check the arity-derived indices in `nth_value` and `execute_call_vectorcall`/`take_call_args`/`take_native_method_args` before the subtraction can wrap. An inconsistent instruction stream now stops with a message that names the index, like the existing `tried to pop from empty stack`. Assisted-by: ZCode:deepseek-v4.1-flash-eco
Codex Review Summary: ❌ FailedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
code.replace(co_code=...)#10014One of checkbox below must be checked.
Summary
CodeType.replace(co_code=...)only checks that every byte decodes to a knownopcode. It does not check that the instruction stream is consistent with
itself, so an operand can address a value stack slot the frame never pushed. The
accessors that read those slots guarded the index with
debug_assert!, which iscompiled out in release builds.
COPY 100on an empty stack therefore computedstack_len - idxby wrapping and read outside the frame's storage (issue#10014 reports this as an out-of-bounds read in a release build).
This keeps the existing "stop with a clear message" behaviour that bad bytecode
already gets (
tried to pop from empty stack) and extends it to the indexingpaths:
LocalsPlus::stack_indexandstack_index_mutcheck the slot index.LocalsPlus::stack_swapandstack_truncatecheck both the indices and the newlength, which also covers
SWAPwith operand0.FrameObject::nth_valuechecks beforestack_len - depth - 1can wrap.Copy,Swapand the call arity paths (execute_call_vectorcall,take_call_args,take_native_method_args) check the operand before itseeds a subtraction.
The out-of-line
#[cold]reporters keep the checked accessors as cheap asthe unchecked ones they replace on the normal path.
Test plan
New integration test
crates/vm/tests/stack_bounds.rsbuilds the twocode.replace(co_code=...)cases from the issue and asserts that theinterpreter rejects them instead of running the bad index.
The same test fails on the unmodified tree, which is the point of the change:
With the patch, in both profiles:
The interpreter tests that cover these paths still pass:
cargo test -p rustpython-vm --libpasses 218 of 219 tests; the one failure,vm::tests::frozen_origname_matches, fails on the unmodified tree too becauseit needs the frozen stdlib that
--no-default-featuresremoves. I could not runthe full workspace test command: this machine has no
libffi, so the defaulthost_env/ctypes build of the test binary aborts at load withLibrary not loaded: @rpath/libffi.8.dylib. That is an environment limit here, notsomething the patch changes; CI runs the full workspace.
Notes for review
satisfies these checks.
debug_assert!s that became run-time checks were each the onlyguard on a raw index, so this is not a new class of check on a hot path
beyond what the release build was already missing.
Assisted-by: ZCode:deepseek-v4.1-flash-ecois in the commit message per theAI policy; the change was reviewed and verified as described above.
Summary by CodeRabbit