-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Fix a memory leak in runtime interop stubs when using an array of structs of types that use old-style managed marshalers #93089
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
8 commits
Select commit
Hold shift + click to select a range
a06ae14
When we don't have a managed value in a ClearNative call, the managed…
jkoritzinsky 42b8994
Fix duplicate assignment
jkoritzinsky 2baa6da
Add test using a combination of ComWrappers and built-in COM to valid…
jkoritzinsky c34d03b
We already normalize null refs early, so we don't need to normalize h…
jkoritzinsky 6ff0065
Simplify test
jkoritzinsky 1a902f2
Update src/tests/Interop/ArrayMarshalling/StructArray/MarshalStructAr…
jkoritzinsky a5ea5f6
Rewrite test to be more reliable and add lots of comments about what …
jkoritzinsky 6dc66b1
Remove now-dead code
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
96 changes: 96 additions & 0 deletions
96
src/tests/Interop/ArrayMarshalling/StructArray/MarshalStructArrayTest.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,96 @@ | ||
| // 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.Collections; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.InteropServices; | ||
| using TestLibrary; | ||
| using Xunit; | ||
|
|
||
| public static unsafe class MarshalStructArrayTest | ||
| { | ||
|
|
||
| [SkipOnMono("Mono doesn't support built-in COM interop, which this test requires")] | ||
| [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsBuiltInComEnabled))] | ||
| public static void ArrayElementsInStructFreed() | ||
| { | ||
| // Validate that we free the native resources of fields that use "managed" marshallers in CoreCLR's IL Stub | ||
| // marshalling system when they are fields of structs and we don't have the managed value avaliable at the time we are | ||
| // releasing the resources. | ||
|
|
||
| // To test this, we are using a structure with a "ByValArray" field that contains a runtime-implemented RCW. | ||
| // The marshaller for the ByValArray unmanaged type uses the marshaller kind under test. | ||
| // Using an RCW as the element type allows us to hook the release mechanism to validate that the value has been freed. | ||
| // Since the runtime will unwrap an RCW when passing it down to native code (instead of re-wrapping in a CCW), this will | ||
| // allow us to directly test with the custom release mechanism we have implemented without needing to do GC.Collect calls. | ||
| object underlyingObject = new(); | ||
| SimpleComWrappers wrappers = new(); | ||
| nint unk = wrappers.GetOrCreateComInterfaceForObject(underlyingObject, CreateComInterfaceFlags.CallerDefinedIUnknown); | ||
| object builtinWrapper = Marshal.GetUniqueObjectForIUnknown(unk); | ||
| Marshal.Release(unk); | ||
|
|
||
| StructWithObjectArrayField str = new() { objs = [builtinWrapper] }; | ||
|
|
||
| IntPtr ptr = (IntPtr)NativeMemory.Alloc((nuint)Marshal.SizeOf<StructWithObjectArrayField>()); | ||
| Marshal.StructureToPtr(str, ptr, false); | ||
| SimpleComWrappers.ReleaseCalled = false; | ||
| Marshal.DestroyStructure<StructWithObjectArrayField>(ptr); | ||
|
|
||
| Assert.True(SimpleComWrappers.ReleaseCalled); | ||
| NativeMemory.Free((void*)ptr); | ||
|
|
||
| // Make sure that the runtime-implemented RCW isn't collected (and Release called) | ||
| // when we are trying to test that DestroyStructure calls release. | ||
| GC.KeepAlive(builtinWrapper); | ||
| } | ||
| } | ||
|
|
||
| public struct StructWithObjectArrayField | ||
| { | ||
| [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] | ||
| public object[] objs; | ||
| } | ||
|
|
||
| public sealed unsafe class SimpleComWrappers : ComWrappers | ||
| { | ||
| private static readonly ComInterfaceEntry* customIUnknown = CreateCustomIUnknownInterfaceEntry(); | ||
|
|
||
| private static ComInterfaceEntry* CreateCustomIUnknownInterfaceEntry() | ||
| { | ||
| ComInterfaceEntry* entry = (ComInterfaceEntry*)NativeMemory.AllocZeroed((nuint)sizeof(ComInterfaceEntry)); | ||
| entry->IID = Guid.Parse("00000000-0000-0000-C000-000000000046"); | ||
| nint* vtable = (nint*)NativeMemory.Alloc((nuint)(sizeof(void*) * 3)); | ||
| GetIUnknownImpl(out vtable[0], out vtable[1], out _); | ||
| vtable[2] = (nint)(delegate* unmanaged<nint, uint>)&Release; | ||
| entry->Vtable = (nint)vtable; | ||
| return entry; | ||
| } | ||
|
|
||
| protected override unsafe ComInterfaceEntry* ComputeVtables(object obj, CreateComInterfaceFlags flags, out int count) | ||
| { | ||
| Assert.True(flags.HasFlag(CreateComInterfaceFlags.CallerDefinedIUnknown)); | ||
| count = 1; | ||
| return customIUnknown; | ||
| } | ||
|
|
||
| protected override object? CreateObject(nint externalComObject, CreateObjectFlags flags) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| protected override void ReleaseObjects(IEnumerable objects) | ||
| { | ||
| throw new NotImplementedException(); | ||
| } | ||
|
|
||
| [ThreadStatic] | ||
| public static bool ReleaseCalled = false; | ||
|
|
||
| [UnmanagedCallersOnly] | ||
| private static uint Release(nint thisPtr) | ||
| { | ||
| ReleaseCalled = true; | ||
| GetIUnknownImpl(out _, out _, out var release); | ||
| return ((delegate* unmanaged<IntPtr, uint>)release)(thisPtr); | ||
| } | ||
| } | ||
12 changes: 12 additions & 0 deletions
12
src/tests/Interop/ArrayMarshalling/StructArray/MarshalStructArrayTest.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,12 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="MarshalStructArrayTest.cs" /> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="$(TestSourceDir)Common/CoreCLRTestLibrary/CoreCLRTestLibrary.csproj" /> | ||
| </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.
Uh oh!
There was an error while loading. Please reload this page.