Skip to content

Commit

Permalink
Fix running tests against our DNNE'd NativeExports project to work in…
Browse files Browse the repository at this point in the history
… NativeAOT (#84578)

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
  • Loading branch information
jkoritzinsky and jkotas authored Apr 18, 2023
1 parent 9cc8163 commit ce2838a
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public static unsafe IntPtr AllocateTypeAssociatedMemory(Type type, int size)
ArgumentOutOfRangeException.ThrowIfNegative(size);

// We don't support unloading; the memory will never be freed.
return (IntPtr)NativeMemory.Alloc((uint)size);
return (IntPtr)NativeMemory.AllocZeroed((uint)size);
}

public static void PrepareDelegate(Delegate d)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
<EnableLibraryImportGenerator>true</EnableLibraryImportGenerator>
<!-- These tests pull the attributes from Ancillary.Interop, so we don't need to include the attribute sources in this assembly. -->
<IncludeLibraryImportGeneratorSources>false</IncludeLibraryImportGeneratorSources>
<!-- suppress warning when referencing architecture specific NativeExports.csproj -->
<ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>None</ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>
<ReferencesNativeExports>true</ReferencesNativeExports>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project>
<PropertyGroup Condition="'$(ReferencesNativeExports)' == 'true'">
<ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>None</ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>
<IlcExportUnmanagedEntrypoints>true</IlcExportUnmanagedEntrypoints>
</PropertyGroup>
<!-- Expose unmanaged entry points from NativeExports -->
<ItemGroup Condition="'$(ReferencesNativeExports)' == 'true'">
<UnmanagedEntryPointsAssembly Include="Microsoft.Interop.Tests.NativeExports" />
<DirectPInvoke Include="Microsoft.Interop.Tests.NativeExportsNE" />
</ItemGroup>
<Import Project="$([MSBuild]::GetPathOfFileAbove(Directory.Build.targets, $(MSBuildThisFileDirectory)..))" />
</Project>
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<EnableLibraryImportGenerator>true</EnableLibraryImportGenerator>
<IncludeLibraryImportGeneratorSources>false</IncludeLibraryImportGeneratorSources>
<ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>None</ResolveAssemblyWarnOrErrorOnTargetArchitectureMismatch>
<ReferencesNativeExports>true</ReferencesNativeExports>
</PropertyGroup>

<ItemGroup>
<Compile Include="$(CommonPath)DisableRuntimeMarshalling.cs"
Link="Common\DisableRuntimeMarshalling.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Ancillary.Interop\Ancillary.Interop.csproj" />
<ProjectReference Include="..\TestAssets\NativeExports\NativeExports.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,12 @@ public static void AllocateTypeAssociatedMemoryInvalidArguments()

[Fact]
[SkipOnMono("Not presently implemented on Mono")]
public static void AllocateTypeAssociatedMemoryValidArguments()
public static unsafe void AllocateTypeAssociatedMemoryValidArguments()
{
IntPtr memory = RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(RuntimeHelpersTests), 32);
Assert.NotEqual(memory, IntPtr.Zero);
// Validate that the memory is zeroed out
Assert.True(new Span<byte>((void*)memory, 32).SequenceEqual(new byte[32]));
}

[StructLayoutAttribute(LayoutKind.Sequential)]
Expand Down
8 changes: 3 additions & 5 deletions src/libraries/tests.proj
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,9 @@
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Runtime.InteropServices.JavaScript\tests\JSImportGenerator.UnitTest\JSImportGenerator.Unit.Tests.csproj" />
<ProjectExclusions Include="$(MSBuildThisFileDirectory)Microsoft.Extensions.Configuration.Binder\tests\SourceGenerationTests\Microsoft.Extensions.Configuration.Binder.SourceGeneration.Tests.csproj" />

<!-- Depends on DNNE. Not particularly interesting because it tests marshalling-less p/invoke only. Low priority -->
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Runtime.InteropServices\tests\LibraryImportGenerator.Tests\LibraryImportGenerator.Tests.csproj" />

<!-- Depends on DNNE -->
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Runtime.InteropServices\tests\ComInterfaceGenerator.Tests\ComInterfaceGenerator.Tests.csproj" />
<!-- Depends on ComWrappers, which is not enabled on NativeAOT on non-Windows. Tracked by #76005 -->
<ProjectExclusions Include="$(MSBuildThisFileDirectory)System.Runtime.InteropServices\tests\ComInterfaceGenerator.Tests\ComInterfaceGenerator.Tests.csproj"
Condition="'$(TargetOS)' != 'windows'" />

<!-- Many test failures due to trimming and MakeGeneric. XmlSerializer is not currently supported with NativeAOT. Low priority -->
<ProjectExclusions Include="$(MSBuildThisFileDirectory)Microsoft.XmlSerializer.Generator\tests\Microsoft.XmlSerializer.Generator.Tests.csproj" />
Expand Down

0 comments on commit ce2838a

Please sign in to comment.