Skip to content

Crypto support : App supply I/O callbacks to EP + callback and fallback helpers#28624

Merged
GopalakrishnanN merged 51 commits into
mainfrom
gokrishnan/EpContextHelperUtilities
Jun 27, 2026
Merged

Crypto support : App supply I/O callbacks to EP + callback and fallback helpers#28624
GopalakrishnanN merged 51 commits into
mainfrom
gokrishnan/EpContextHelperUtilities

Conversation

@GopalakrishnanN

@GopalakrishnanN GopalakrishnanN commented May 22, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR adds an opt-in mechanism that lets an application supply its own I/O callbacks for an execution provider's EPContext binary data, so the data can live somewhere other than a plain file on disk (for example, an encrypted store or an in-memory buffer). It introduces the callback APIs end-to-end and demonstrates their use with a sample helper in the AutoEP example plugin EP.

When an EP compiles a model into an EPContext model, it may emit the compiled blob either embedded in the ONNX model or as a separate external payload. For the external case, ORT previously assumed the payload is a file. These callbacks let the application own that read/write instead, while ORT core stays policy-neutral and never imposes a storage format.

What this PR adds

  • Write callback (OrtWriteNamedBufferFunc) + setter OrtCompileApi::ModelCompilationOptions_SetEpContextDataWriteFunc. Set on OrtModelCompilationOptions, because writing EPContext binary data happens only during compilation. Passing a NULL callback clears a previously set one.
  • Read callback (OrtReadNamedBufferFunc) + setter OrtApi::SessionOptions_SetEpContextDataReadFunc. Set on OrtSessionOptions, because reading external EPContext binary data happens during session load / inference. Passing a NULL callback clears a previously set one.
  • EP-facing access via OrtEpContextConfig. Both callbacks are surfaced to execution providers through a single unified handle, OrtEpContextConfig, obtained via OrtEpApi::SessionOptions_GetEpContextConfig (getters EpContextConfig_GetEpContextDataReadFunc / EpContextConfig_GetEpContextDataWriteFunc, released with ReleaseEpContextConfig). This keeps the application-facing setters scoped to the correct lifecycle while giving EPs one consistent place to retrieve both callbacks. Each setter's doc comment cross-references the other so the split is discoverable.
  • Experimental API surface + C++ accessors. These functions ship through ORT's experimental API mechanism (declared in include/onnxruntime/core/session/onnxruntime_experimental_c_api.inc), so they are reached via the generated Ort::Experimental::Get_<name>_SinceV28_Fn(...) / ...FnOrThrow(...) accessors rather than fixed OrtApi slots. A move-only RAII wrapper, Ort::Experimental::EpContextConfig (in onnxruntime_experimental_cxx_api.h), owns an OrtEpContextConfig and exposes GetReadFunc() / GetWriteFunc(); it can be constructed directly from a C++ SessionOptions / ConstSessionOptions.
  • Sample-only helper utilities (onnxruntime/test/autoep/library/ep_context_data_utils.h) implementing callback-or-file fallback behavior: if a callback is supplied it is used, otherwise the helper falls back to direct file I/O. The AutoEP example plugin EP uses this helper for its external EPContext read/write paths. Because the names read on the load side originate from the untrusted EPContext model (ep_cache_context attribute), the helper validates them: it rejects absolute/rooted paths, .. traversal, and directory-like names (. or a trailing separator), and confines model-relative names to the model directory (resolving ./.. and symlinks via std::filesystem::weakly_canonical). It reports all failures via OrtStatus* (no exceptions) and lives outside the public C API / EP ABI, so it is purely illustrative and imposes no policy on ORT core; its doc comments note that production EPs should still apply their own sandboxing and payload size limits.

The callback typedef names (OrtReadNamedBufferFunc / OrtWriteNamedBufferFunc) are intentionally generic. They are currently used for EPContext binary data, but the contract is deliberately storage-agnostic so future APIs can reuse the same callback shape for other named data payloads.

Note on the Android workflow change

.github/workflows/android.yml bumps the minimal-build binary-size threshold (1436672 -> 1438720 bytes) to accommodate the small size increase from compiling the new experimental API into the Android minimal build.

Testing

  • Built and tested in RelWithDebInfo: python tools/ci_build/build.py --config RelWithDebInfo --build --parallel --test --build_dir build\Windows.
  • Focused EPContext suites:
    • Public C/C++ API: onnxruntime_shared_lib_test.exe --gtest_filter=EpContextDataApiTest.* -> 9 passed.
    • AutoEP helper + compile/load end-to-end (callbacks and file fallback): onnxruntime_autoep_test.exe --gtest_filter=*EpContext* -> 17 passed, 1 skipped (EpContextDataUtils_ResolvePathRejectsSymlinkEscape requires the Windows "create symbolic link" privilege).
  • clang-format clean on touched C++ files; git diff --check: clean.

Test layout: public EPContext API tests in onnxruntime/test/shared_lib/test_ep_context_data_api.cc; sample-helper unit tests in onnxruntime/test/autoep/ep_context_data_utils_test.cc; compile/load end-to-end tests in onnxruntime/test/autoep/test_execution.cc.

@GopalakrishnanN GopalakrishnanN requested a review from edgchen1 May 29, 2026 23:07
@GopalakrishnanN GopalakrishnanN force-pushed the gokrishnan/EpContextHelperUtilities branch from 97cfedb to ffbbb4f Compare June 2, 2026 17:02

@github-actions github-actions Bot 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.

You can commit the suggested changes from lintrunner.

Comment thread include/onnxruntime/core/session/onnxruntime_c_api.h Outdated
Comment thread include/onnxruntime/core/session/onnxruntime_ep_c_api.h Outdated
Comment thread include/onnxruntime/core/session/onnxruntime_ep_c_api.h Outdated
Comment thread include/onnxruntime/core/session/onnxruntime_ep_c_api.h Outdated
Comment thread include/onnxruntime/core/session/onnxruntime_ep_c_api.h Outdated
Comment thread onnxruntime/test/autoep/test_execution.cc Outdated
Comment thread onnxruntime/test/autoep/test_execution.cc Outdated
Comment thread onnxruntime/test/autoep/test_execution.cc Outdated

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.

Pull request overview

This PR adds sample-only EPContext data I/O helper utilities (callback-or-file fallback) and wires them through the EP plugin APIs so example/test EPs can read/write EPContext payloads without forcing policy into ORT core/public ABI.

Changes:

  • Add new EPContext read/write callback types and APIs (C, C++ wrappers, EP API extraction via OrtEpContextConfig).
  • Add sample ep_context_data_utils helpers for path handling and callback/file fallback read/write.
  • Update AutoEP example plugin EP + tests to use the helpers for external EPContext read/write paths.

Reviewed changes

Copilot reviewed 23 out of 23 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
onnxruntime/test/framework/ep_plugin_provider_test.cc Adds unit tests for new EPContext callback APIs and C++ wrappers.
onnxruntime/test/autoep/test_execution.cc Adds AutoEP tests covering callback/file fallback behavior for EPContext data.
onnxruntime/test/autoep/library/example_plugin_ep/ep.h Extends example EP config and plumbs Ort::EpContextConfig into the EP instance.
onnxruntime/test/autoep/library/example_plugin_ep/ep.cc Uses the new helper to read/write EPContext external data with callback/file fallback.
onnxruntime/test/autoep/library/example_plugin_ep/ep_factory.cc Extracts/stores EPContext callbacks from session options and reads embed/path config.
onnxruntime/test/autoep/library/ep_context_data_utils.h Adds sample-only helper utilities for path conversion + callback/file fallback I/O.
onnxruntime/core/session/plugin_ep/ep_api.h Declares new EP plugin API entrypoints for EPContext config + callbacks.
onnxruntime/core/session/plugin_ep/ep_api.cc Implements EPContext config handle + accessor functions and appends to OrtEpApi.
onnxruntime/core/session/ort_apis.h Declares new public C API entrypoint for EPContext read callback registration.
onnxruntime/core/session/onnxruntime_c_api.cc Exposes SessionOptions_SetEpContextDataReadFunc in the versioned OrtApi table.
onnxruntime/core/session/model_compilation_options.h Adds ModelCompilationOptions::SetEpContextDataWriteFunc API.
onnxruntime/core/session/model_compilation_options.cc Stores EPContext write callback into model-gen options.
onnxruntime/core/session/compile_api.h Declares new compile API function to set EPContext write callback.
onnxruntime/core/session/compile_api.cc Implements compile API function and appends to OrtCompileApi.
onnxruntime/core/session/abi_session_options.cc Implements SessionOptions_SetEpContextDataReadFunc.
onnxruntime/core/framework/session_options.h Adds fields to store EPContext read callback + state in SessionOptions.
onnxruntime/core/framework/ep_context_options.h Adds holder for EPContext write callback in model-gen options.
onnxruntime/core/framework/ep_context_options.cc Adds accessor for EPContext write callback holder.
include/onnxruntime/core/session/onnxruntime_ep_c_api.h Adds EP ABI surface for OrtEpContextConfig and callback accessors.
include/onnxruntime/core/session/onnxruntime_cxx_inline.h Adds inline C++ wrappers for EpContextConfig + new setter wrappers.
include/onnxruntime/core/session/onnxruntime_cxx_api.h Adds owning C++ wrapper type Ort::EpContextConfig.
include/onnxruntime/core/session/onnxruntime_c_api.h Adds callback typedefs + new OrtApi/OrtCompileApi entrypoints.
cmake/onnxruntime_unittests.cmake Adds the new helper header to the AutoEP example plugin build sources.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread onnxruntime/core/session/plugin_ep/ep_api.cc Outdated
Comment thread onnxruntime/core/session/plugin_ep/ep_api.cc Outdated
Comment thread onnxruntime/core/session/compile_api.cc Outdated
Comment thread onnxruntime/core/session/compile_api.cc Outdated
Comment thread onnxruntime/test/autoep/library/example_plugin_ep/ep.cc
Comment thread onnxruntime/test/autoep/library/ep_context_data_utils.h Outdated

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.

Pull request overview

Copilot reviewed 23 out of 23 changed files in this pull request and generated 2 comments.

Comment thread onnxruntime/test/framework/ep_plugin_provider_test.cc
Comment thread onnxruntime/test/autoep/library/ep_context_data_utils.h

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.

Pull request overview

Copilot reviewed 23 out of 23 changed files in this pull request and generated no new comments.

@ericcraw ericcraw 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.

The ep apis look good to me.

@MayureshV1 MayureshV1 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.

EP interfaces look good !

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.

Pull request overview

Copilot reviewed 23 out of 23 changed files in this pull request and generated 3 comments.

Comment thread onnxruntime/test/autoep/library/example_plugin_ep/ep_factory.cc Outdated
Comment thread onnxruntime/test/autoep/library/example_plugin_ep/ep.h Outdated
Comment thread onnxruntime/core/session/compile_api.cc Outdated

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.

Pull request overview

Copilot reviewed 24 out of 24 changed files in this pull request and generated 2 comments.

Comment thread onnxruntime/core/session/onnxruntime_c_api.cc
Comment thread onnxruntime/test/autoep/library/example_plugin_ep/ep_factory.cc Outdated
Comment thread include/onnxruntime/core/session/onnxruntime_c_api.h Outdated

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.

Pull request overview

Copilot reviewed 24 out of 24 changed files in this pull request and generated 2 comments.

Comment thread onnxruntime/core/session/plugin_ep/ep_api.cc Outdated
Comment thread onnxruntime/core/session/compile_api.cc Outdated
@GopalakrishnanN GopalakrishnanN changed the title Add sample EPContext data fallback helpers Add EPContext data callbacks and fallback helpers for EP-Owned I/O Jun 4, 2026
@GopalakrishnanN GopalakrishnanN changed the title Add EPContext data callbacks and fallback helpers for EP-Owned I/O App supply I/O callbacks to EP + callback and fallback helpers Jun 4, 2026
@GopalakrishnanN GopalakrishnanN force-pushed the gokrishnan/EpContextHelperUtilities branch from 18124ff to c0bb9f2 Compare June 10, 2026 21:51

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.

Pull request overview

Copilot reviewed 20 out of 20 changed files in this pull request and generated 1 comment.

Comment thread include/onnxruntime/core/session/onnxruntime_experimental_c_api.inc Outdated
- Fix doc to reference OrtCompileApi (not OrtApi) for ModelCompilationOptions_SetEpContextBinaryInformation.
- EpContextConfig: drop the default and raw OrtSessionOptions* constructors; keep only the SessionOptions/ConstSessionOptions overloads (wrap at the ep_factory call site).
- example EP: comment that the load-side EPContext read during compile is intentionally exercised then discarded.
- ep_context_data_utils: low-level *WithFileFallback overloads now take the callback + state directly; high-level overloads extract them from OrtEpContextConfig.
- Allow a NULL EPContext write callback to clear a previously set one (symmetric with the read setter); update doc/SAL and tests.
- Move the public EPContext write-callback tests out of the framework provider test into test/shared_lib/test_ep_context_data_api.cc.

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.

Pull request overview

Copilot reviewed 20 out of 20 changed files in this pull request and generated 1 comment.

Comment thread onnxruntime/test/framework/ep_plugin_provider_test.cc

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.

Pull request overview

Copilot reviewed 21 out of 21 changed files in this pull request and generated 3 comments.

Comment thread onnxruntime/test/autoep/library/ep_context_data_utils.h
Comment thread onnxruntime/test/autoep/library/ep_context_data_utils.h
Comment thread onnxruntime/test/autoep/library/ep_context_data_utils.h Outdated
edgchen1
edgchen1 previously approved these changes Jun 25, 2026
Comment thread onnxruntime/test/autoep/library/example_plugin_ep/ep_factory.cc Outdated
Comment thread onnxruntime/core/session/experimental_c_api.cc Outdated
Comment thread onnxruntime/test/autoep/library/ep_context_data_utils.h
Comment thread onnxruntime/test/autoep/library/ep_context_data_utils.h Outdated
Comment thread onnxruntime/test/framework/ep_plugin_provider_test.cc Outdated
Comment thread onnxruntime/test/framework/ep_plugin_provider_test.cc Outdated
Comment thread onnxruntime/test/shared_lib/test_ep_context_data_api.cc Outdated
Replaces the manual Ort::Exception try/catch in ExampleEpFactory::CreateEpImpl with the EXCEPTION_TO_RETURNED_STATUS_BEGIN/END macros (consistent with the example EP kernels), which also map std::exception and unknown exceptions to a returned OrtStatus to match the noexcept contract.
Gopalakrishnan Nallasamy added 2 commits June 25, 2026 16:30
…te a directory

ValidateEpContextDataName and the model-derived (untrusted) branch of ResolveEpContextDataPath now reject a name whose final component is empty (a trailing separator) or is a dot, which would otherwise resolve to a directory and only surface later as a confusing file I/O failure. Adds an IsDirectoryOrEmptyName helper and covering tests.
Reword the OrtEpContextConfig definition comment (it is not opaque at the definition site); make ReadEpContextDataWithFileFallback exception-free by using the C allocator API (GetAllocatorWithDefaultOptions / AllocatorFree) instead of Ort::AllocatorWithDefaultOptions; alphabetize includes in ep_plugin_provider_test.cc; and use the Ort::Experimental::EpContextConfig RAII wrapper in the read-back tests in test_ep_context_data_api.cc.

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.

Pull request overview

Copilot reviewed 21 out of 21 changed files in this pull request and generated 1 comment.

Comment thread onnxruntime/test/autoep/library/ep_context_data_utils.h
@GopalakrishnanN GopalakrishnanN merged commit 49efb32 into main Jun 27, 2026
98 of 177 checks passed
@GopalakrishnanN GopalakrishnanN deleted the gokrishnan/EpContextHelperUtilities branch June 27, 2026 00:26
GopalakrishnanN pushed a commit that referenced this pull request Jun 27, 2026
…h resolution

Addresses #28624 review feedback. Now that IsResolvedPathWithinBase() does real model-directory containment for untrusted model-relative names, the lexical ContainsPathTraversal() guard is no longer applied on the trusted graph==nullptr branch of ResolveEpContextDataPath(): trusted callers already may pass absolute paths and own their paths, so there is no model directory to contain against. ContainsPathTraversal() is kept solely for ValidateEpContextDataName(), which validates the logical callback-namespace name written into the model ep_cache_context attribute (never resolved against a filesystem base). Updates the two trusted-branch tests; logical-name and model-directory containment coverage is unchanged.
GopalakrishnanN pushed a commit that referenced this pull request Jun 27, 2026
…h resolution

Addresses #28624 review feedback. Now that IsResolvedPathWithinBase() does real model-directory containment for untrusted model-relative names, the lexical ContainsPathTraversal() guard is no longer applied on the trusted graph==nullptr branch of ResolveEpContextDataPath(): trusted callers already may pass absolute paths and own their paths, so there is no model directory to contain against. ContainsPathTraversal() is kept solely for ValidateEpContextDataName(), which validates the logical callback-namespace name written into the model ep_cache_context attribute (never resolved against a filesystem base). Updates the two trusted-branch tests; logical-name and model-directory containment coverage is unchanged.
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.

7 participants