Skip to content

Fix out-of-bounds read when a Loop/Scan body input is also an initializer - #32609

Open
Wei Wang (wangw-1991) wants to merge 1 commit into
microsoft:mainfrom
wangw-1991:fix_SessionState_OOB
Open

Wei Wang (wangw-1991) wants to merge 1 commit into
microsoft:mainfrom
wangw-1991:fix_SessionState_OOB

Conversation

@wangw-1991

Copy link
Copy Markdown
Contributor

Problem

OuterScopeNodeArgLocationAccumulator (session_state.cc) maps a Loop/Scan node's explicit inputs onto the subgraph's inputs using GraphViewer::GetInputs(), which excludes initializer-backed inputs. When a body declares an input that is also a
body initializer, GetInputs() is shorter than the parent node's input list, so indexing subgraph_inputs[arg_idx] reads past the end of the vector and dereferences an invalid NodeArg pointer during session initialization. This causes a crash / heap out-of-bounds read (observable under ASan) when loading a malformed model containing such a Loop/Scan.

Fix

In the Loop/Scan≥9 branch of OuterScopeNodeArgLocationAccumulator:

  • Select the subgraph input vector whose size matches the parent node's input count (prefer GetInputsIncludingInitializers(), fall back to GetInputs()), mirroring the logic already used in Graph::InferAndVerifySubgraphTypes. If neither matches return INVALID_GRAPH instead of indexing.
  • Add a defensive bounds check before indexing.
  • Add Loop.BodyInputAlsoInitializer_RejectedWithoutOutOfBoundsRead test: a Loop whose body input is also a body initializer is now rejected cleanly instead of crashing.

Copilot AI balanced review requested due to automatic review settings September 15, 2026 10:34
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The regression case still reaches an ORT_ENFORCE that aborts exception-disabled builds instead of returning a clean error.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Prevents out-of-bounds access while mapping Loop/Scan parent inputs to subgraph inputs.

Changes:

  • Selects a cardinality-matching subgraph input list and validates bounds.
  • Adds a Loop regression test for initializer-backed body inputs.
File summaries
File Description
onnxruntime/core/framework/session_state.cc Hardens subgraph input mapping.
onnxruntime/test/providers/cpu/controlflow/loop_test.cc Adds the regression test.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 1
  • Review effort level: Balanced (auto)

Note

Copilot is running an experiment and ran this review at Balanced.


💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +1529 to +1532
const auto* subgraph_inputs = &subgraph.GetInputsIncludingInitializers();
if (subgraph_inputs->size() != num_parent_inputs) {
const auto& required_subgraph_inputs = subgraph.GetInputs();
if (required_subgraph_inputs.size() != num_parent_inputs) {
@wangw-1991

Copy link
Copy Markdown
Contributor Author

Tianlei Wu (@tianleiwu) Can you help review this PR or help find suitable reviewers? Thanks.

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