Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmake/onnxruntime_unittests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -1979,7 +1979,7 @@ endif()
# Build library that can be used with RegisterExecutionProviderLibrary and automatic EP selection
# We need a shared lib build to use that as a dependency for the test library
# Currently we only have device discovery on Windows so no point building the test app on other platforms.
if (WIN32 AND onnxruntime_BUILD_SHARED_LIB AND
if (onnxruntime_BUILD_SHARED_LIB AND
NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten" AND
NOT onnxruntime_MINIMAL_BUILD)
# example_plugin_ep
Expand Down
26 changes: 26 additions & 0 deletions onnxruntime/core/platform/posix/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ limitations under the License.
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <filesystem>
#if !defined(_AIX)
#include <sys/syscall.h>
#endif
Expand Down Expand Up @@ -592,6 +593,31 @@ class PosixEnv : public Env {
return val == NULL ? std::string() : std::string(val);
}

// Return the path of the executable/shared library for the current running code. This is to make it
// possible to load other shared libraries installed next to our core runtime code.
PathString GetRuntimePath() const override {
Copy link
Contributor

Choose a reason for hiding this comment

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

You may directly use std::filesystem::path. The class PathString was created before we migrated the code to C++17.

Dl_info dl_info{};
// Must be one of the symbols exported in libonnxruntime.{so,dynlib}.
void* symbol_from_this_library = dlsym(RTLD_DEFAULT, "OrtGetApiBase");
PathString runtime_path;
// We will find OrtGetApiBase if onnxruntime is loaded as a shared library
if (dladdr(symbol_from_this_library, &dl_info) && dl_info.dli_fname) {
// Converting to absolute path since running a program with and without gdb attached can make a difference whether
// dli_fname will be absolute or relative. Converting to absolute for consistent result
runtime_path = PathString(std::filesystem::absolute(std::filesystem::path(dl_info.dli_fname).parent_path().string())) + "/";
Copy link
Author

Choose a reason for hiding this comment

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

I saw that returning an absolute vs an relative path makes a difference on whether ORT will attempt to call CreateEp which CUDA EP does not implement.

} else {
// else use path of current executable to mirror Windows behavior
#if __linux__
runtime_path = PathString(std::filesystem::absolute(std::filesystem::read_symlink(std::filesystem::path("/proc/self/exe")).parent_path())) + "/";
#else
// TODO: MacOS could use _NSGetExecutablePath, but this needs to be tested!
runtime_path = PathString();
#endif
Copy link
Author

@theHamsta theHamsta Oct 28, 2025

Choose a reason for hiding this comment

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

I don't have a Mac to test. Can this be deferred to another PR? It is not required for the tests as the tests use libonnxruntime.dynlib

}
LOGS_DEFAULT(VERBOSE) << "Determined PosixEnv runtime path as \"" << runtime_path << "\"";
return runtime_path;
}

private:
Telemetry telemetry_provider_;
#ifdef ORT_USE_CPUINFO
Expand Down
2 changes: 2 additions & 0 deletions onnxruntime/core/providers/cuda/version_script.lds
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
VERS_1.0 {
global:
GetProvider;
CreateEpFactories;
ReleaseEpFactory;
Copy link
Author

Choose a reason for hiding this comment

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

this enables the autoep test cases. The exports are already present on Windows.

_binary_*;

# Hide everything else.
Expand Down
10 changes: 5 additions & 5 deletions onnxruntime/test/autoep/test_selection.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

// registration/selection is only supported on windows as there's no device discovery on other platforms
#ifdef _WIN32

#include <filesystem>
// #include <absl/base/config.h>
Expand Down Expand Up @@ -171,7 +169,11 @@ TEST(AutoEpSelection, CpuEP) {
TEST(AutoEpSelection, CudaEP) {
Ort::KeyValuePairs provider_options;
provider_options.Add("prefer_nhwc", "1");
RunBasicTest(kCudaExecutionProvider, "onnxruntime_providers_cuda", provider_options);
#if __linux__
RunBasicTest(kCudaExecutionProvider, "libonnxruntime_providers_cuda.so", provider_options);
#else
RunBasicTest(kCudaExecutionProvider, "onnxruntime_providers_cuda.dll", provider_options);
#endif
}
#endif

Expand Down Expand Up @@ -502,5 +504,3 @@ TEST(AutoEpSelection, PolicyDelegateReturnsError) {

} // namespace test
} // namespace onnxruntime

#endif // _WIN32