-
Notifications
You must be signed in to change notification settings - Fork 215
Fix native library sample #270
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
71f945b
Fix native library sample
jkotas 6b0b66e
Add smoke test from #113
jkotas 8a849e7
Try to fix osx
jkotas c2f8928
Workaround for Linux bug
jkotas 3777f99
Fix Windows tests
jkotas 9ce3b9e
Make test harness happy
jkotas 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
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
11 changes: 11 additions & 0 deletions
11
src/tests/nativeaot/SmokeTests/SharedLibrary/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,11 @@ | ||
| project (SharedLibrary) | ||
| include_directories(${INC_PLATFORM_DIR}) | ||
|
|
||
| add_executable (SharedLibraryDriver SharedLibrary.cpp) | ||
|
|
||
| if (CLR_CMAKE_TARGET_UNIX) | ||
| target_link_libraries (SharedLibraryDriver ${CMAKE_DL_LIBS}) | ||
| endif() | ||
|
|
||
| # add the install targets | ||
| install (TARGETS PInvokeNative DESTINATION bin) |
84 changes: 84 additions & 0 deletions
84
src/tests/nativeaot/SmokeTests/SharedLibrary/SharedLibrary.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,84 @@ | ||
| // 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 "stdio.h" | ||
| #include "string.h" | ||
|
|
||
| #ifndef TARGET_WINDOWS | ||
| #define __stdcall | ||
| #endif | ||
|
|
||
| // typedef for shared lib exported methods | ||
| typedef int(__stdcall *f_ReturnsPrimitiveInt)(); | ||
| typedef bool(__stdcall *f_ReturnsPrimitiveBool)(); | ||
| typedef char(__stdcall *f_ReturnsPrimitiveChar)(); | ||
| typedef void(__stdcall *f_EnsureManagedClassLoaders)(); | ||
|
|
||
| #ifdef TARGET_WINDOWS | ||
| int main() | ||
| #else | ||
| int main(int argc, char* argv[]) | ||
| #endif | ||
| { | ||
| #ifdef TARGET_WINDOWS | ||
| HINSTANCE handle = LoadLibrary("SharedLibrary.dll"); | ||
| #elif __APPLE__ | ||
| void *handle = dlopen(strcat(argv[0], ".dylib"), RTLD_LAZY); | ||
| #else | ||
| void *handle = dlopen(strcat(argv[0], ".so"), RTLD_LAZY); | ||
| #endif | ||
|
|
||
| if (!handle) | ||
| return 1; | ||
|
|
||
| #ifdef TARGET_WINDOWS | ||
| f_ReturnsPrimitiveInt returnsPrimitiveInt = (f_ReturnsPrimitiveInt)GetProcAddress(handle, "ReturnsPrimitiveInt"); | ||
| f_ReturnsPrimitiveBool returnsPrimitiveBool = (f_ReturnsPrimitiveBool)GetProcAddress(handle, "ReturnsPrimitiveBool"); | ||
| f_ReturnsPrimitiveChar returnsPrimitiveChar = (f_ReturnsPrimitiveChar)GetProcAddress(handle, "ReturnsPrimitiveChar"); | ||
| f_EnsureManagedClassLoaders ensureManagedClassLoaders = (f_EnsureManagedClassLoaders)GetProcAddress(handle, "EnsureManagedClassLoaders"); | ||
| f_ReturnsPrimitiveInt checkSimpleGCCollect = (f_ReturnsPrimitiveInt)GetProcAddress(handle, "CheckSimpleGCCollect"); | ||
| f_ReturnsPrimitiveInt checkSimpleExceptionHandling = (f_ReturnsPrimitiveInt)GetProcAddress(handle, "CheckSimpleExceptionHandling"); | ||
| #else | ||
| f_ReturnsPrimitiveInt returnsPrimitiveInt = (f_ReturnsPrimitiveInt)dlsym(handle, "ReturnsPrimitiveInt"); | ||
| f_ReturnsPrimitiveBool returnsPrimitiveBool = (f_ReturnsPrimitiveBool)dlsym(handle, "ReturnsPrimitiveBool"); | ||
| f_ReturnsPrimitiveChar returnsPrimitiveChar = (f_ReturnsPrimitiveChar)dlsym(handle, "ReturnsPrimitiveChar"); | ||
| f_EnsureManagedClassLoaders ensureManagedClassLoaders = (f_EnsureManagedClassLoaders)dlsym(handle, "EnsureManagedClassLoaders"); | ||
| f_ReturnsPrimitiveInt checkSimpleGCCollect = (f_ReturnsPrimitiveInt)dlsym(handle, "CheckSimpleGCCollect"); | ||
| f_ReturnsPrimitiveInt checkSimpleExceptionHandling = (f_ReturnsPrimitiveInt)dlsym(handle, "CheckSimpleExceptionHandling"); | ||
| #endif | ||
|
|
||
| if (returnsPrimitiveInt() != 10) | ||
| return 1; | ||
|
|
||
| if (!returnsPrimitiveBool()) | ||
| return 2; | ||
|
|
||
| if (returnsPrimitiveChar() != 'a') | ||
| return 3; | ||
|
|
||
| // As long as no unmanaged exception is thrown | ||
| // managed class loaders were initialized successfully | ||
| ensureManagedClassLoaders(); | ||
|
|
||
| if (checkSimpleGCCollect() != 100) | ||
| return 4; | ||
|
|
||
| if (checkSimpleExceptionHandling() != 100) | ||
| return 5; | ||
|
|
||
| // CoreRT is not designed to be unloadable, so this won't actually unload the library properly. Verify that attempt | ||
| // to unload the library does not to crash at least. | ||
| #ifdef TARGET_WINDOWS | ||
| FreeLibrary(handle); | ||
| #else | ||
| // TODO: How to pin the library in memory on Unix? | ||
| // dlclose(handle); | ||
| #endif | ||
|
|
||
| return 100; | ||
| } |
95 changes: 95 additions & 0 deletions
95
src/tests/nativeaot/SmokeTests/SharedLibrary/SharedLibrary.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,95 @@ | ||
| // 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 | ||
| { | ||
| [UnmanagedCallersOnly(EntryPoint = "ReturnsPrimitiveInt", CallConvs = new Type[] { typeof(CallConvStdcall) })] | ||
| public static int ReturnsPrimitiveInt() | ||
| { | ||
| return 10; | ||
| } | ||
|
|
||
| [UnmanagedCallersOnly(EntryPoint = "ReturnsPrimitiveBool", CallConvs = new Type[] { typeof(CallConvStdcall) })] | ||
| public static bool ReturnsPrimitiveBool() | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| [UnmanagedCallersOnly(EntryPoint = "ReturnsPrimitiveChar", CallConvs = new Type[] { typeof(CallConvStdcall) })] | ||
| public static char ReturnsPrimitiveChar() | ||
| { | ||
| return 'a'; | ||
| } | ||
|
|
||
| [UnmanagedCallersOnly(EntryPoint = "EnsureManagedClassLoaders", CallConvs = new Type[] { typeof(CallConvStdcall) })] | ||
| public static void EnsureManagedClassLoaders() | ||
| { | ||
| Random random = new Random(); | ||
| random.Next(); | ||
| } | ||
|
|
||
| [UnmanagedCallersOnly(EntryPoint = "CheckSimpleExceptionHandling", CallConvs = new Type[] { typeof(CallConvStdcall) })] | ||
| public static int CheckSimpleExceptionHandling() | ||
| { | ||
| int result = 10; | ||
|
|
||
| try | ||
| { | ||
| Console.WriteLine("Throwing exception"); | ||
| throw new Exception(); | ||
| } | ||
| catch when (result == 10) | ||
| { | ||
| result += 20; | ||
| } | ||
| finally | ||
| { | ||
| result += 70; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| private static bool s_collected; | ||
|
|
||
| class ClassWithFinalizer | ||
| { | ||
| ~ClassWithFinalizer() { s_collected = true; } | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| private static void MakeGarbage() | ||
| { | ||
| GC.Collect(); | ||
| GC.WaitForPendingFinalizers(); | ||
| GC.Collect(); | ||
|
|
||
| object[] arr = new object[1024 * 1024]; | ||
| for (int i = 0; i < arr.Length; i++) | ||
| arr[i] = new object(); | ||
|
|
||
| new ClassWithFinalizer(); | ||
| } | ||
|
|
||
| [UnmanagedCallersOnly(EntryPoint = "CheckSimpleGCCollect", CallConvs = new Type[] { typeof(CallConvStdcall) })] | ||
| public static int CheckSimpleGCCollect() | ||
| { | ||
| string myString = string.Format("Hello {0}", "world"); | ||
|
|
||
| MakeGarbage(); | ||
|
|
||
| Console.WriteLine("Triggering GC"); | ||
| GC.Collect(); | ||
| GC.WaitForPendingFinalizers(); | ||
| GC.Collect(); | ||
|
|
||
| return s_collected ? (myString == "Hello world" ? 100 : 1) : 2; | ||
| } | ||
| } | ||
| } |
38 changes: 38 additions & 0 deletions
38
src/tests/nativeaot/SmokeTests/SharedLibrary/SharedLibrary.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,38 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <OutputType>Library</OutputType> | ||
| <CLRTestKind>BuildAndRun</CLRTestKind> | ||
| <CLRTestPriority>0</CLRTestPriority> | ||
| <AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
| </PropertyGroup> | ||
|
|
||
| <PropertyGroup> | ||
| <NativeAotProjectLines> | ||
| <![CDATA[ | ||
| <PropertyGroup> | ||
| <OutputType>Library</OutputType> | ||
| <NativeLib>Shared</NativeLib> | ||
| </PropertyGroup> | ||
| ]]> | ||
| </NativeAotProjectLines> | ||
|
|
||
| <CLRTestBatchPreCommands><![CDATA[ | ||
| $(CLRTestBatchPreCommands) | ||
| mkdir native 2>nul | ||
| copy /y SharedLibraryDriver.exe native\SharedLibrary.exe | ||
| ]]></CLRTestBatchPreCommands> | ||
|
|
||
| <BashCLRTestPreCommands><![CDATA[ | ||
| $(BashCLRTestPreCommands) | ||
| mkdir -p native | ||
| cp SharedLibraryDriver native/SharedLibrary | ||
| ]]></BashCLRTestPreCommands> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="SharedLibrary.cs" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="CMakeLists.txt" /> | ||
| </ItemGroup> | ||
| </Project> |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have started by introducing another variable for the SharedLib init entrypoint, but then decided against it as unnecessary abstraction. I think we should wait for a real case why it is useful.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be useful for the @kant2002 inspired project file of my Doom fire effect: https://github.com/MichalStrehovsky/sharpfire/blob/fee39991c22b39d263933d0fd1ec25d585d82e6e/nogui.csproj#L4 (that's why I added option to set your own entrypoint when I was messing with it).
I had to avoid specifying
WinExebecause then the entrypoint gets set up from here and breaks things.Arguably I never ended up using it because I have better things to do than maintaining my old hacks :).
Two weeks after the hack I learned about /ALTERNATENAME that can be used for this too I guess.
I don't particularly care, just wanted to share the history.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually we might be able to use /ALTERNATENAME to remove the need for the /ENTRY line.
Something like
/ALTERNATENAME:wWinMainCRTStartup=wmainCRTStartupshould also do the trick and we don't even need to condition it on WinExe.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have left it as is. The ALTERNATENAME trick is neat, but i did not look like an improvement to me.