Reject stack indices that the frame cannot have by Goldloli · Pull Request #10023 · RustPython/RustPython
Repository navigation
Reject stack indices that the frame cannot have - #10023
Open
Goldloli wants to merge 1 commit into RustPython:main from Goldloli:fix/stack-index-bounds
Open
Reject stack indices that the frame cannot have #10023 Goldloli wants to merge 1 commit into RustPython:main from Goldloli:fix/stack-index-bounds
Conversation
Goldloli
commented
Nov 25, 2026
·
edited by coderabbitai
Bot
Loading
Copy link Copy Markdown
One of checkbox below must be checked.
- I did not use AI to write the code of this patch.
- This PR follows our AI policy
Summary
CodeType.replace(co_code=...) only checks that every byte decodes to a known
opcode. 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 is
compiled out in release builds. COPY 100 on an empty stack therefore computed
stack_len - idx by 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 indexing
paths:
LocalsPlus::stack_indexandstack_index_mutcheck the slot index.LocalsPlus::stack_swapandstack_truncatecheck both the indices and the new
length, which also coversSWAPwith 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 it
seeds a subtraction.
The out-of-line #[cold] reporters keep the checked accessors as cheap as
the unchecked ones they replace on the normal path.
Test plan
New integration test crates/vm/tests/stack_bounds.rs builds the two
code.replace(co_code=...) cases from the issue and asserts that the
interpreter rejects them instead of running the bad index.
The same test fails on the unmodified tree, which is the point of the change:
# unmodified, release profile
$ cargo test -p rustpython-vm --no-default-features --features compiler,gc \
--release --test stack_bounds
index out of bounds: the len is 1 but the index is 18446744073709551615
...
test result: FAILED. 0 passed; 2 failed
# unmodified, debug profile
expected a "COPY index" rejection, got: CopyItem: stack underflow
With the patch, in both profiles:
$ cargo test -p rustpython-vm --no-default-features --features compiler,gc \
--test stack_bounds
test copy_operand_past_stack_is_rejected ... ok
test swap_operand_past_stack_is_rejected ... ok
$ cargo test -p rustpython-vm --no-default-features --features compiler,gc \
--release --test stack_bounds
test result: ok. 2 passed; 0 failed
The interpreter tests that cover these paths still pass:
$ ./rustpython -m test test_code test_code_module test_dis
All 3 tests OK. (199 tests)
$ ./rustpython -m test test_funcattrs test_generators test_coroutines test_with \
test_asyncgen test_exceptions test_opcodes test_compile
All 8 tests OK. (649 tests)
cargo test -p rustpython-vm --lib passes 218 of 219 tests; the one failure,
vm::tests::frozen_origname_matches, fails on the unmodified tree too because
it needs the frozen stdlib that --no-default-features removes. I could not run
the full workspace test command: this machine has no libffi, so the default
host_env/ctypes build of the test binary aborts at load with
Library not loaded: @rpath/libffi.8.dylib. That is an environment limit here, not
something the patch changes; CI runs the full workspace.
Notes for review
- No behaviour change for compiled code: the compiler's stack model already
satisfies these checks. - The three
debug_assert!s that became run-time checks were each the only
guard 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 the
AI policy; the change was reviewed and verified as described above.
Summary by CodeRabbit
- Bug Fixes
- Invalid stack operations now fail with a controlled error instead of risking unsafe memory access. This improves stability when executing malformed or invalid bytecode.
Reject stack indices that the frame cannot have
735f2f0
`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
chatgpt-codex-connector
Bot
commented
Nov 25, 2026
Copy link Copy Markdown
|
Codex Review Summary : ❌ Failed
This comment shows the latest Codex review activity on this pull request.
Your 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. |
chatgpt-codex-connector
Bot
commented
Nov 25, 2026
Copy link Copy Markdown
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
coderabbitai
Bot
commented
Nov 25, 2026
·
edited
Loading
Copy link Copy Markdown
Contributor
|
No actionable comments were generated in the recent review. Walkthrough
735f2
Thanks for using CodeRabbit ! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Share
Recent review info
Run configuration
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review. |