-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Set AppContext.BaseDirectory and the lookup path for AssemblyDirectory to the directory containing the NativeAOT module #112457
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jkoritzinsky
merged 10 commits into
dotnet:main
from
jkoritzinsky:nativeaot-appcontext-base
Mar 4, 2025
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5f90ffa
Set AppContext.BaseDirectory and the lookup path for AssemblyDirector…
jkoritzinsky 5ad0b1b
Fix calling convention
jkoritzinsky 0bb7c80
Fix missing using
jkoritzinsky bba4f9e
Rewrite an add comment
jkoritzinsky aaf93c7
Add test
jkoritzinsky 6f42c19
Merge branch 'main' into nativeaot-appcontext-base
jkoritzinsky 6f871eb
Make TryGetFullPathToApplicationModule nullable
jkoritzinsky 85ebfee
Don't use relative paths to load dependencies on Linux/macos. Those a…
jkoritzinsky 43e78ad
Add test for AppContext.BaseDirectory
jkoritzinsky e874ec0
Fix test on Windows
jkoritzinsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/tests/nativeaot/SmokeTests/SharedLibraryDependencyLoading/CMakeLists.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
project (SharedLibrary) | ||
include_directories(${INC_PLATFORM_DIR}) | ||
|
||
add_executable(SharedLibraryHost SharedLibraryHost.cpp) | ||
add_library(SharedLibraryDependency SHARED SharedLibraryDependency.cpp) | ||
|
||
if (CLR_CMAKE_TARGET_UNIX) | ||
target_link_libraries (SharedLibraryHost PRIVATE ${CMAKE_DL_LIBS}) | ||
endif() | ||
|
||
# If there's a dynamic ASAN runtime, then copy it to project output. | ||
if (NOT "${ASAN_RUNTIME}" STREQUAL "") | ||
file(COPY "${ASAN_RUNTIME}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}") | ||
endif() |
9 changes: 9 additions & 0 deletions
9
src/tests/nativeaot/SmokeTests/SharedLibraryDependencyLoading/SharedLibraryDependency.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#include <platformdefines.h> | ||
|
||
jkoritzinsky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
extern "C" DLL_EXPORT int32_t STDMETHODCALLTYPE MultiplyIntegers(int32_t a, int32_t b) | ||
{ | ||
return a * b; | ||
} |
29 changes: 29 additions & 0 deletions
29
...sts/nativeaot/SmokeTests/SharedLibraryDependencyLoading/SharedLibraryDependencyLoading.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace SharedLibrary | ||
{ | ||
public class ClassLibrary | ||
jkoritzinsky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
[UnmanagedCallersOnly(EntryPoint = "MultiplyIntegers", CallConvs = [typeof(CallConvStdcall)])] | ||
public static int MultiplyIntegersExport(int x, int y) | ||
{ | ||
return MultiplyIntegers(x, y); | ||
} | ||
|
||
[UnmanagedCallersOnly(EntryPoint = "GetBaseDirectory", CallConvs = [typeof(CallConvStdcall)])] | ||
public static IntPtr GetBaseDirectory() | ||
{ | ||
return Marshal.StringToCoTaskMemAnsi(AppContext.BaseDirectory); | ||
} | ||
|
||
[DllImport("SharedLibraryDependency")] | ||
[DefaultDllImportSearchPaths(DllImportSearchPath.AssemblyDirectory)] | ||
[UnmanagedCallConv(CallConvs = [typeof(CallConvStdcall)])] | ||
public static extern int MultiplyIntegers(int x, int y); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
...nativeaot/SmokeTests/SharedLibraryDependencyLoading/SharedLibraryDependencyLoading.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Library</OutputType> | ||
<CLRTestKind>BuildAndRun</CLRTestKind> | ||
<CLRTestPriority>0</CLRTestPriority> | ||
<AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
<NativeLib>Shared</NativeLib> | ||
<BuildAsStandalone>false</BuildAsStandalone> | ||
<!-- Unable to compile a project with the Library output type for apple mobile devices --> | ||
<CLRTestTargetUnsupported Condition="'$(TargetsAppleMobile)' == 'true'">true</CLRTestTargetUnsupported> | ||
<RequiresProcessIsolation>true</RequiresProcessIsolation> | ||
<SkipInferOutputType>true</SkipInferOutputType> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<CLRTestBatchPreCommands><![CDATA[ | ||
$(CLRTestBatchPreCommands) | ||
mkdir native 2>nul | ||
mkdir subdir 2>nul | ||
copy /y clang_rt.* native\ | ||
copy /y SharedLibraryDependency.dll subdir\ | ||
copy /y native\SharedLibraryDependencyLoading* subdir\ | ||
copy /y SharedLibraryHost.exe native\SharedLibraryDependencyLoading.exe | ||
]]></CLRTestBatchPreCommands> | ||
|
||
<CLRTestBashPreCommands><![CDATA[ | ||
$(CLRTestBashPreCommands) | ||
mkdir -p native | ||
mkdir -p subdir | ||
cp libclang_rt.* native/ | ||
cp libSharedLibraryDependency.* subdir/ | ||
cp native/SharedLibraryDependencyLoading* subdir/ | ||
cp SharedLibraryHost native/SharedLibraryDependencyLoading | ||
]]></CLRTestBashPreCommands> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="SharedLibraryDependencyLoading.cs" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<CMakeProjectReference Include="CMakeLists.txt" /> | ||
</ItemGroup> | ||
</Project> |
103 changes: 103 additions & 0 deletions
103
src/tests/nativeaot/SmokeTests/SharedLibraryDependencyLoading/SharedLibraryHost.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
#ifdef TARGET_WINDOWS | ||
#include "windows.h" | ||
#else | ||
#include "dlfcn.h" | ||
#endif | ||
#include <cstdint> | ||
#include <string> | ||
#include <memory> | ||
#include <iostream> | ||
|
||
#ifndef TARGET_WINDOWS | ||
#define __stdcall | ||
#endif | ||
|
||
// typedef for shared lib exported methods | ||
using f_MultiplyIntegers = int32_t(__stdcall *)(int32_t, int32_t); | ||
using f_getBaseDirectory = const char*(__stdcall *)(); | ||
|
||
#ifdef TARGET_WINDOWS | ||
template<typename T> | ||
struct CoTaskMemDeleter | ||
{ | ||
void operator()(T* p) const | ||
{ | ||
CoTaskMemFree((void*)p); | ||
} | ||
}; | ||
template<typename T> | ||
using CoTaskMemPtr = std::unique_ptr<T, CoTaskMemDeleter<T>>; | ||
#else | ||
template<typename T> | ||
using CoTaskMemPtr = std::unique_ptr<T>; | ||
#endif | ||
|
||
#ifdef TARGET_WINDOWS | ||
int __cdecl main(int argc, char* argv[]) | ||
#else | ||
int main(int argc, char* argv[]) | ||
#endif | ||
{ | ||
std::string pathToSubdir = argv[0]; | ||
// Step out of the current directory and the parent directory. | ||
pathToSubdir = pathToSubdir.substr(0, pathToSubdir.find_last_of("/\\")); | ||
pathToSubdir = pathToSubdir.substr(0, pathToSubdir.find_last_of("/\\")); | ||
#ifdef TARGET_WINDOWS | ||
pathToSubdir += "subdir\\"; | ||
// We need to include System32 to find system dependencies of SharedLibraryDependencyLoading.dll | ||
jkoritzinsky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
HINSTANCE handle = LoadLibraryEx("..\\subdir\\SharedLibraryDependencyLoading.dll", nullptr, LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32); | ||
#else | ||
#if TARGET_APPLE | ||
constexpr char const* ext = ".dylib"; | ||
#else | ||
constexpr char const* ext = ".so"; | ||
#endif | ||
|
||
pathToSubdir += "subdir/"; | ||
std::string path = pathToSubdir + "SharedLibraryDependencyLoading"; | ||
path += ext; | ||
void* handle = dlopen(path.c_str(), RTLD_LAZY); | ||
#endif | ||
|
||
if (!handle) | ||
return 1; | ||
|
||
#ifdef TARGET_WINDOWS | ||
f_MultiplyIntegers multiplyIntegers = (f_MultiplyIntegers)GetProcAddress(handle, "MultiplyIntegers"); | ||
#else | ||
f_MultiplyIntegers multiplyIntegers = (f_MultiplyIntegers)dlsym(handle, "MultiplyIntegers"); | ||
#endif | ||
|
||
if (multiplyIntegers(10, 7) != 70) | ||
return 2; | ||
|
||
CoTaskMemPtr<const char> baseDirectory; | ||
#ifdef TARGET_WINDOWS | ||
f_getBaseDirectory getBaseDirectory = (f_getBaseDirectory)GetProcAddress(handle, "GetBaseDirectory"); | ||
#else | ||
f_getBaseDirectory getBaseDirectory = (f_getBaseDirectory)dlsym(handle, "GetBaseDirectory"); | ||
#endif | ||
|
||
baseDirectory.reset(getBaseDirectory()); | ||
if (baseDirectory == nullptr) | ||
return 3; | ||
|
||
if (pathToSubdir != baseDirectory.get()) | ||
{ | ||
std::cout << "Expected base directory: " << pathToSubdir << std::endl; | ||
std::cout << "Actual base directory: " << baseDirectory.get() << std::endl; | ||
return 4; | ||
} | ||
|
||
return 100; | ||
} | ||
|
||
extern "C" const char* __stdcall __asan_default_options() | ||
{ | ||
// NativeAOT is not designed to be unloadable, so we'll leak a few allocations from the shared library. | ||
// Disable leak detection as we don't care about these leaks as of now. | ||
return "detect_leaks=0 use_sigaltstack=0"; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.