From 251993ca3e2da77e5a0656399a592a2c335b5359 Mon Sep 17 00:00:00 2001
From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com>
Date: Mon, 11 May 2026 02:00:23 +0000
Subject: [PATCH] Add trimming test for Marshal.StructureToPtr with ByVal
DateTime array (issue #127952)
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/d5cc9194-87ba-4a47-aab5-a1c8f5c11865
Co-authored-by: MichalStrehovsky <13110571+MichalStrehovsky@users.noreply.github.com>
---
.../MarshalStructureToPtrByValDateArray.cs | 43 +++++++++++++++++++
...Runtime.InteropServices.TrimmingTests.proj | 5 +++
2 files changed, 48 insertions(+)
create mode 100644 src/libraries/System.Runtime.InteropServices/tests/TrimmingTests/MarshalStructureToPtrByValDateArray.cs
diff --git a/src/libraries/System.Runtime.InteropServices/tests/TrimmingTests/MarshalStructureToPtrByValDateArray.cs b/src/libraries/System.Runtime.InteropServices/tests/TrimmingTests/MarshalStructureToPtrByValDateArray.cs
new file mode 100644
index 00000000000000..f6133cfcd3209c
--- /dev/null
+++ b/src/libraries/System.Runtime.InteropServices/tests/TrimmingTests/MarshalStructureToPtrByValDateArray.cs
@@ -0,0 +1,43 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+// Regression test for https://github.com/dotnet/runtime/issues/127952
+// Marshal.StructureToPtr / Marshal.DestroyStructure on a struct containing a
+// ByValArray of DateTime values requires the DateMarshaler stub-helper to
+// survive trimming. Without the fix this throws:
+// System.Security.VerificationException: Method
+// System.StubHelpers.StubHelpers.FreeArrayContents: type argument
+// 'System.StubHelpers.DateMarshaler' violates the constraint of type
+// parameter 'TMarshaler'.
+
+using System;
+using System.Runtime.InteropServices;
+
+var structure = new StructWithDateArray()
+{
+ array = new DateTime[]
+ {
+ DateTime.Now, DateTime.Now
+ }
+};
+
+int size = Marshal.SizeOf(structure);
+IntPtr memory = Marshal.AllocHGlobal(size);
+try
+{
+ Marshal.StructureToPtr(structure, memory, false);
+ Marshal.StructureToPtr(structure, memory, true);
+}
+finally
+{
+ Marshal.DestroyStructure(memory, structure.GetType());
+ Marshal.FreeHGlobal(memory);
+}
+
+return 100;
+
+public struct StructWithDateArray
+{
+ [MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
+ public DateTime[] array;
+}
diff --git a/src/libraries/System.Runtime.InteropServices/tests/TrimmingTests/System.Runtime.InteropServices.TrimmingTests.proj b/src/libraries/System.Runtime.InteropServices/tests/TrimmingTests/System.Runtime.InteropServices.TrimmingTests.proj
index 9aa8090bd00f56..7e4c1dba9cc74f 100644
--- a/src/libraries/System.Runtime.InteropServices/tests/TrimmingTests/System.Runtime.InteropServices.TrimmingTests.proj
+++ b/src/libraries/System.Runtime.InteropServices/tests/TrimmingTests/System.Runtime.InteropServices.TrimmingTests.proj
@@ -26,6 +26,11 @@
IlcGenerateMstatFile;IlcGenerateDgmlFile
+
+
+
+
+