File tree Expand file tree Collapse file tree 2 files changed +22
-13
lines changed
src/System.Management.Automation/CoreCLR Expand file tree Collapse file tree 2 files changed +22
-13
lines changed Original file line number Diff line number Diff line change @@ -596,17 +596,22 @@ public static unsafe class PowerShellUnsafeAssemblyLoad
596
596
/// <summary>
597
597
/// Load an assembly in memory from unmanaged code.
598
598
/// </summary>
599
- /// <param name="data">
600
- /// Unmanaged pointer to assembly data buffer.
601
- /// </param>
602
- /// <param name="size">
603
- /// Size in bytes of the assembly data buffer.
604
- /// </param>
599
+ /// <param name="data">Unmanaged pointer to assembly data buffer.</param>
600
+ /// <param name="size">Size in bytes of the assembly data buffer.</param>
601
+ /// <returns>Returns zero on success and non-zero on failure</returns>
605
602
[ UnmanagedCallersOnly ]
606
- public static void LoadAssemblyFromNativeMemory ( IntPtr data , int size )
603
+ public static int LoadAssemblyFromNativeMemory ( IntPtr data , int size )
607
604
{
608
- using var stream = new UnmanagedMemoryStream ( ( byte * ) data , size ) ;
609
- AssemblyLoadContext . Default . LoadFromStream ( stream ) ;
605
+ try
606
+ {
607
+ using var stream = new UnmanagedMemoryStream ( ( byte * ) data , size ) ;
608
+ AssemblyLoadContext . Default . LoadFromStream ( stream ) ;
609
+ return 0 ;
610
+ }
611
+ catch
612
+ {
613
+ return - 1 ;
614
+ }
610
615
}
611
616
}
612
617
}
Original file line number Diff line number Diff line change @@ -35,17 +35,21 @@ public static void TestLoadNativeInMemoryAssembly()
35
35
string asmFullName = SearchAssembly ( asmName . Name ) ;
36
36
Assert . Null ( asmFullName ) ;
37
37
38
- unsafe { LoadAssemblyTest ( testDll ) ; }
38
+ unsafe
39
+ {
40
+ int ret = LoadAssemblyTest ( testDll ) ;
41
+ Assert . Equal ( 0 , ret ) ;
42
+ }
39
43
40
44
asmFullName = SearchAssembly ( asmName . Name ) ;
41
45
Assert . Equal ( asmName . FullName , asmFullName ) ;
42
46
}
43
47
44
- private static unsafe void LoadAssemblyTest ( string assemblyPath )
48
+ private static unsafe int LoadAssemblyTest ( string assemblyPath )
45
49
{
46
50
// The 'LoadAssemblyFromNativeMemory' method is annotated with 'UnmanagedCallersOnly' attribute,
47
51
// so we have to use the 'unmanaged' function pointer to invoke it.
48
- delegate * unmanaged< IntPtr , int , void > funcPtr = & PowerShellUnsafeAssemblyLoad . LoadAssemblyFromNativeMemory ;
52
+ delegate * unmanaged< IntPtr , int , int > funcPtr = & PowerShellUnsafeAssemblyLoad . LoadAssemblyFromNativeMemory ;
49
53
50
54
int length = 0 ;
51
55
IntPtr nativeMem = IntPtr . Zero ;
@@ -62,7 +66,7 @@ private static unsafe void LoadAssemblyTest(string assemblyPath)
62
66
}
63
67
64
68
// Call the function pointer.
65
- funcPtr ( nativeMem , length ) ;
69
+ return funcPtr ( nativeMem , length ) ;
66
70
}
67
71
finally
68
72
{
You can’t perform that action at this time.
0 commit comments