From 2e4e221da872026fd3692b9c0727970a0b76301a Mon Sep 17 00:00:00 2001 From: ivberg Date: Fri, 1 Nov 2024 22:58:47 -0700 Subject: [PATCH] Fix crash when running ORT under low integrity process like Edge where ETW registration can fail (#22699) ### Description Make ETW provider registration non-fatal and not throw an exception Needs to work under build with exceptions enabled & --disable_exceptions ### Motivation and Context ORT should not crash Addresses #22475. Private tested by filer of that issue --- onnxruntime/core/platform/windows/logging/etw_sink.cc | 7 +++++-- onnxruntime/core/session/inference_session.cc | 8 ++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/onnxruntime/core/platform/windows/logging/etw_sink.cc b/onnxruntime/core/platform/windows/logging/etw_sink.cc index bf73a538ea..9e58b79c36 100644 --- a/onnxruntime/core/platform/windows/logging/etw_sink.cc +++ b/onnxruntime/core/platform/windows/logging/etw_sink.cc @@ -158,8 +158,9 @@ void EtwRegistrationManager::LazyInitialize() { initialization_status_ = InitializationStatus::Initializing; etw_status_ = ::TraceLoggingRegisterEx(etw_provider_handle, ORT_TL_EtwEnableCallback, nullptr); if (FAILED(etw_status_)) { + // Registration can fail when running under Low Integrity process, and should be non-fatal initialization_status_ = InitializationStatus::Failed; - ORT_THROW("ETW registration failed. Logging will be broken: " + std::to_string(etw_status_)); + LOGS_DEFAULT(WARNING) << "Error in ETW registration: " << std::to_string(etw_status_); } initialization_status_ = InitializationStatus::Initialized; } @@ -176,7 +177,9 @@ void EtwRegistrationManager::InvokeCallbacks(LPCGUID SourceId, ULONG IsEnabled, std::lock_guard lock(callbacks_mutex_); for (const auto& callback : callbacks_) { - (*callback)(SourceId, IsEnabled, Level, MatchAnyKeyword, MatchAllKeyword, FilterData, CallbackContext); + if (callback != nullptr) { + (*callback)(SourceId, IsEnabled, Level, MatchAnyKeyword, MatchAllKeyword, FilterData, CallbackContext); + } } } diff --git a/onnxruntime/core/session/inference_session.cc b/onnxruntime/core/session/inference_session.cc index 42a2b4ef3e..bc5db98e7c 100644 --- a/onnxruntime/core/session/inference_session.cc +++ b/onnxruntime/core/session/inference_session.cc @@ -735,8 +735,12 @@ InferenceSession::~InferenceSession() { // Unregister the session and ETW callbacks #ifdef _WIN32 std::lock_guard lock(active_sessions_mutex_); - WindowsTelemetry::UnregisterInternalCallback(callback_ML_ORT_provider_); - logging::EtwRegistrationManager::Instance().UnregisterInternalCallback(callback_ETWSink_provider_); + if (callback_ML_ORT_provider_ != nullptr) { + WindowsTelemetry::UnregisterInternalCallback(callback_ML_ORT_provider_); + } + if (callback_ETWSink_provider_ != nullptr) { + logging::EtwRegistrationManager::Instance().UnregisterInternalCallback(callback_ETWSink_provider_); + } #endif active_sessions_.erase(session_id_);