-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Add tests for IMallocSpy
#71106
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
AaronRobinsonMSFT
merged 2 commits into
dotnet:main
from
AaronRobinsonMSFT:add_imallocspy_testing
Jun 22, 2022
Merged
Add tests for IMallocSpy
#71106
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // 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.InteropServices; | ||
|
|
||
| using COM; | ||
|
|
||
| using TestLibrary; | ||
| using Xunit; | ||
|
|
||
| public class ExtensionPoints | ||
| { | ||
| unsafe class MallocSpy : IMallocSpy | ||
| { | ||
| private int _called = 0; | ||
| public int Called => _called; | ||
|
|
||
| public virtual nuint PreAlloc(nuint cbRequest) | ||
| { | ||
| _called++; | ||
| return cbRequest; | ||
| } | ||
|
|
||
| public virtual unsafe void* PostAlloc(void* pActual) => pActual; | ||
| public virtual unsafe void* PreFree(void* pRequest, [MarshalAs(UnmanagedType.Bool)] bool fSpyed) | ||
| { | ||
| _called++; | ||
| return pRequest; | ||
| } | ||
|
|
||
| public virtual void PostFree([MarshalAs(UnmanagedType.Bool)] bool fSpyed) { } | ||
| public virtual unsafe nuint PreRealloc(void* pRequest, nuint cbRequest, void** ppNewRequest, [MarshalAs(UnmanagedType.Bool)] bool fSpyed) => cbRequest; | ||
| public virtual unsafe void* PostRealloc(void* pActual, [MarshalAs(UnmanagedType.Bool)] bool fSpyed) => pActual; | ||
| public virtual unsafe void* PreGetSize(void* pRequest, [MarshalAs(UnmanagedType.Bool)] bool fSpyed) => pRequest; | ||
| public virtual nuint PostGetSize(nuint cbActual, [MarshalAs(UnmanagedType.Bool)] bool fSpyed) => cbActual; | ||
| public virtual unsafe void* PreDidAlloc(void* pRequest, [MarshalAs(UnmanagedType.Bool)] bool fSpyed) => pRequest; | ||
| public virtual unsafe int PostDidAlloc(void* pRequest, [MarshalAs(UnmanagedType.Bool)] bool fSpyed, int fActual) => fActual; | ||
| public virtual void PreHeapMinimize() { } | ||
| public virtual void PostHeapMinimize() { } | ||
| } | ||
|
|
||
| [Fact] | ||
| public static unsafe void Validate_Managed_IMallocSpy() | ||
| { | ||
| Console.WriteLine($"Running {nameof(Validate_Managed_IMallocSpy)}..."); | ||
| var mallocSpy = new MallocSpy(); | ||
| int result = Ole32.CoRegisterMallocSpy(mallocSpy); | ||
| Assert.Equal(0, result); | ||
| try | ||
| { | ||
| var arr = new [] { "", "", "", "", null }; | ||
|
|
||
| // The goal of this test is to trigger paths in which CoTaskMemAlloc | ||
| // will be implicitly used and validate that the registered managed | ||
| // IMallocSpy can be called successful. The validation is for confirming | ||
| // the transition to Preemptive mode was performed. | ||
| // | ||
| // Casting the function pointer to one in which an IL stub will be | ||
| // used to marshal the string[]. | ||
| var fptr = (delegate*unmanaged<string[], int>)(delegate*unmanaged<char**, int>)&ArrayLen; | ||
| int len = fptr(arr); | ||
| Assert.Equal(arr.Length - 1, len); | ||
|
|
||
| // Allocate 1 for the array, 1 for each non-null element, then double it for Free. | ||
| Assert.Equal((1 + (arr.Length - 1)) * 2, mallocSpy.Called); | ||
| } | ||
| finally | ||
| { | ||
| Ole32.CoRevokeMallocSpy(); | ||
| } | ||
|
|
||
| [UnmanagedCallersOnly] | ||
| static int ArrayLen(char** ptr) | ||
| { | ||
| char** begin = ptr; | ||
| while (*ptr != null) | ||
| ptr++; | ||
| return (int)(ptr - begin); | ||
| } | ||
| } | ||
| } | ||
10 changes: 10 additions & 0 deletions
10
src/tests/Interop/COM/ExtensionPoints/ExtensionPoints.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,10 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <RequiresProcessIsolation>true</RequiresProcessIsolation> | ||
| <AllowUnsafeBlocks>true</AllowUnsafeBlocks> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="ExtensionPoints.cs" /> | ||
| <Compile Include="Interfaces.cs" /> | ||
| </ItemGroup> | ||
| </Project> |
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,78 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Runtime.InteropServices; | ||
|
|
||
| namespace COM | ||
| { | ||
| static class Ole32 | ||
| { | ||
| [DllImport(nameof(Ole32), ExactSpelling = true)] | ||
| public static extern int CoRegisterMallocSpy(IMallocSpy mallocSpy); | ||
|
|
||
| [DllImport(nameof(Ole32), ExactSpelling = true)] | ||
| public static extern int CoRevokeMallocSpy(); | ||
| } | ||
|
|
||
| [ComImport] | ||
| [Guid("0000001d-0000-0000-C000-000000000046")] | ||
| [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] | ||
| public unsafe interface IMallocSpy | ||
| { | ||
| [PreserveSig] | ||
| nuint PreAlloc( | ||
| nuint cbRequest); | ||
|
|
||
| [PreserveSig] | ||
| void* PostAlloc( | ||
| void* pActual); | ||
|
|
||
| [PreserveSig] | ||
| void* PreFree( | ||
| void* pRequest, | ||
| [MarshalAs(UnmanagedType.Bool)] bool fSpyed); | ||
|
|
||
| [PreserveSig] | ||
| void PostFree( | ||
| [MarshalAs(UnmanagedType.Bool)] bool fSpyed); | ||
|
|
||
| [PreserveSig] | ||
| nuint PreRealloc( | ||
| void* pRequest, | ||
| nuint cbRequest, | ||
| void** ppNewRequest, | ||
| [MarshalAs(UnmanagedType.Bool)] bool fSpyed); | ||
|
|
||
| [PreserveSig] | ||
| void* PostRealloc( | ||
| void* pActual, | ||
| [MarshalAs(UnmanagedType.Bool)] bool fSpyed); | ||
|
|
||
| [PreserveSig] | ||
| void* PreGetSize( | ||
| void* pRequest, | ||
| [MarshalAs(UnmanagedType.Bool)] bool fSpyed); | ||
|
|
||
| [PreserveSig] | ||
| nuint PostGetSize( | ||
| nuint cbActual, | ||
| [MarshalAs(UnmanagedType.Bool)] bool fSpyed); | ||
|
|
||
| [PreserveSig] | ||
| void* PreDidAlloc( | ||
| void* pRequest, | ||
| [MarshalAs(UnmanagedType.Bool)] bool fSpyed); | ||
|
|
||
| [PreserveSig] | ||
| int PostDidAlloc( | ||
| void* pRequest, | ||
| [MarshalAs(UnmanagedType.Bool)] bool fSpyed, | ||
| int fActual); | ||
|
|
||
| [PreserveSig] | ||
| void PreHeapMinimize(); | ||
|
|
||
| [PreserveSig] | ||
| void PostHeapMinimize(); | ||
| } | ||
| } |
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.