// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. #if !defined(ORT_MINIMAL_BUILD) #include "core/graph/model_helpers.h" #include #include #include #include #include #include "core/graph/function_utils.h" #include "core/graph/onnx_protobuf.h" namespace onnxruntime { namespace { using NodeRange = const google::protobuf::RepeatedPtrField*; using PendingNodeRanges = InlinedVector>; Status AddAttributeSubgraphs(const ONNX_NAMESPACE::AttributeProto& attr, size_t subgraph_depth, PendingNodeRanges& pending) { if ((attr.has_g() || !attr.graphs().empty()) && subgraph_depth > kMaxModelSubgraphDepth) { return ORT_MAKE_STATUS( ONNXRUNTIME, NOT_IMPLEMENTED, "Model subgraph depth ", subgraph_depth, " exceeds the maximum supported depth of ", kMaxModelSubgraphDepth, "."); } if (attr.has_g()) { pending.push_back({&attr.g().node(), subgraph_depth}); } for (const auto& graph : attr.graphs()) { pending.push_back({&graph.node(), subgraph_depth}); } return Status::OK(); } Status ValidateSubgraphDepth( const google::protobuf::RepeatedPtrField& root_nodes, const google::protobuf::RepeatedPtrField* root_attributes = nullptr) { PendingNodeRanges pending{{&root_nodes, 0}}; if (root_attributes != nullptr) { for (const auto& attr : *root_attributes) { ORT_RETURN_IF_ERROR(AddAttributeSubgraphs(attr, 1, pending)); } } while (!pending.empty()) { const auto [nodes, depth] = pending.back(); pending.pop_back(); for (const auto& node : *nodes) { for (const auto& attr : node.attribute()) { ORT_RETURN_IF_ERROR(AddAttributeSubgraphs(attr, depth + 1, pending)); } } } return Status::OK(); } // Iterative collection of local function calls from a sequence of nodes, // including nodes inside nested subgraph attributes. Avoids recursion to // prevent stack overflow from maliciously deep subgraph nesting. template void CollectLocalFunctionCalls( const NodeRange& nodes, const std::unordered_map& model_local_functions, InlinedHashSet& seen_calls, InlinedVector& called_functions) { InlinedVector pending_graphs; auto process_nodes = [&](const auto& node_range) { for (const auto& node : node_range) { const auto function_id = function_utils::GetFunctionIdentifier( node.domain(), node.op_type(), node.overload()); auto it = model_local_functions.find(function_id); if (it != model_local_functions.end()) { // Use string_view into the map key (stable storage). std::string_view key_view = it->first; if (seen_calls.insert(key_view).second) { called_functions.push_back(key_view); } } for (const auto& attr : node.attribute()) { if (attr.has_g()) { pending_graphs.push_back(&attr.g()); } for (const auto& sub_graph : attr.graphs()) { pending_graphs.push_back(&sub_graph); } } } }; process_nodes(nodes); while (!pending_graphs.empty()) { const auto* graph = pending_graphs.back(); pending_graphs.pop_back(); process_nodes(graph->node()); } } } // namespace Status ValidateModelSubgraphDepth(const ONNX_NAMESPACE::ModelProto& model_proto) { ORT_RETURN_IF_ERROR(ValidateSubgraphDepth(model_proto.graph().node())); for (const auto& function : model_proto.functions()) { ORT_RETURN_IF_ERROR(ValidateFunctionSubgraphDepth(function)); } return Status::OK(); } Status ValidateFunctionSubgraphDepth(const ONNX_NAMESPACE::FunctionProto& function_proto) { return ValidateSubgraphDepth(function_proto.node(), &function_proto.attribute_proto()); } Status BuildLocalFunctionCallGraph( const std::unordered_map& model_local_functions, LocalFunctionCallGraph& call_graph) { call_graph.reserve(model_local_functions.size()); for (const auto& [function_id, function_proto] : model_local_functions) { if (function_proto == nullptr) { return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, "Null function proto for function id: ", function_id); } InlinedHashSet seen_calls; InlinedVector callees; CollectLocalFunctionCalls(function_proto->node(), model_local_functions, seen_calls, callees); call_graph.emplace(std::string_view(function_id), std::move(callees)); } return Status::OK(); } Status ValidateCallGraphAcyclic(const LocalFunctionCallGraph& call_graph) { enum class VisitState { kNotVisited, kVisiting, kVisited }; InlinedHashMap visit_states; visit_states.reserve(call_graph.size()); for (const auto& [function_id, _] : call_graph) { ORT_UNUSED_PARAMETER(_); visit_states.emplace(function_id, VisitState::kNotVisited); } // Each frame records the function being visited and a pointer to its callees vector // in the call graph (no per-frame allocation). struct DfsFrame { std::string_view function_id; const InlinedVector* callees; size_t next_callee_index; }; std::vector dfs_stack; for (const auto& [root_id, root_callees] : call_graph) { auto root_state_it = visit_states.find(root_id); if (root_state_it == visit_states.end() || root_state_it->second == VisitState::kVisited) { continue; } root_state_it->second = VisitState::kVisiting; dfs_stack.push_back({root_id, &root_callees, 0}); while (!dfs_stack.empty()) { auto& frame = dfs_stack.back(); if (frame.next_callee_index >= frame.callees->size()) { // All callees processed — mark as fully visited and pop. auto it = visit_states.find(frame.function_id); ORT_ENFORCE(it != visit_states.end()); it->second = VisitState::kVisited; dfs_stack.pop_back(); continue; } std::string_view callee_id = (*frame.callees)[frame.next_callee_index]; frame.next_callee_index++; auto callee_state_it = visit_states.find(callee_id); if (callee_state_it == visit_states.end()) { // Callee not in the graph — skip. continue; } if (callee_state_it->second == VisitState::kVisited) { continue; } if (callee_state_it->second == VisitState::kVisiting) { // Cycle detected. Build cycle description from the stack. std::string cycle; bool in_cycle = false; for (const auto& f : dfs_stack) { if (f.function_id == callee_id) { in_cycle = true; } if (in_cycle) { if (!cycle.empty()) { cycle.append(" -> "); } cycle.append(f.function_id); } } cycle.append(" -> "); cycle.append(callee_id); return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_GRAPH, "Model local function definitions must not be recursive. Cycle detected: ", cycle); } // Push callee onto the DFS stack. auto callee_graph_it = call_graph.find(callee_id); if (callee_graph_it == call_graph.end()) { continue; } callee_state_it->second = VisitState::kVisiting; dfs_stack.push_back({callee_id, &callee_graph_it->second, 0}); } } return Status::OK(); } Status ValidateModelLocalFunctionAcyclic( const std::unordered_map& model_local_functions) { LocalFunctionCallGraph call_graph; ORT_RETURN_IF_ERROR(BuildLocalFunctionCallGraph(model_local_functions, call_graph)); return ValidateCallGraphAcyclic(call_graph); } } // namespace onnxruntime #endif // !defined(ORT_MINIMAL_BUILD)