Skip to content

Commit 7520173

Browse files
authored
Update vendored System.Runtime.CompilerServices.Unsafe (#8469)
## Summary of changes - Create re-usable vendoring process for System.Runtime.CompilerServices.Unsafe - Update the vendored code (it's actually unchanged) ## Reason for change We want to be able to update vendored code as required. For the vendored [System.Runtime.CompilerServices.Unsafe package](https://nuget.info/packages/System.Runtime.CompilerServices.Unsafe/6.1.2), the [source code](https://github.com/dotnet/maintenance-packages/blob/14e29655e53aec37342e933bfd7ba574167453ff/src/System.Runtime.CompilerServices.Unsafe/src/System.Runtime.CompilerServices.Unsafe.il) is written directly as IL, and [compiled using Ilasm.exe](https://learn.microsoft.com/en-us/dotnet/framework/tools/ilasm-exe-il-assembler). So this PR introduces a simple "IL to C#" converter that converts that file to its C# equivalent, using the same `InlineIL.Fody` approach that we currently have. The final result produces a file that is _virtually_ identical to the existing one (which is good!). The only difference I made was to add the original IL line as a comment next to the Fody equivalent. This also shows that the code has not _actually_ changed (and it's unlikely it _will_ tbh), so this just means we have a repeatable self-contained approach to regenerate this in the repo as required. ## Implementation details Told 🤖 to make the IL to C# converter, and it did 😄 I've given the code it generated a once-over to look for anything terrible, but the key thing is that the _output_ is sane, and that's visibly basically unchanged, so I think it's fine. ## Test coverage @dudikeleti already wrote some extensive tests for the `Unsafe` implementation back when he originally ported it, which verifies that we can compile the methods, call them, and that the generated IL is identical to the "real" versions 🎉 ## Other details https://datadoghq.atlassian.net/browse/APMLP-1207 Part of a stack updating our vendored system code - #8391 - #8454 - #8455 - #8459 - #8461
1 parent 7c74848 commit 7520173

8 files changed

Lines changed: 961 additions & 405 deletions

File tree

tracer/build/_build/UpdateVendors/ILToInlineILConverter.cs

Lines changed: 751 additions & 0 deletions
Large diffs are not rendered by default.

tracer/build/_build/UpdateVendors/VendoredDependency.cs

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,31 @@ static VendoredDependency()
310310

311311
Add(
312312
libraryName: "System.Runtime.CompilerServices.Unsafe",
313-
version: "1.0.0",
314-
downloadUrl: "https://github.com/DataDog/dotnet-vendored-code/archive/refs/tags/1.0.0.zip",
315-
pathToSrc: new[] { "dotnet-vendored-code-1.0.0", "System.Reflection.Metadata", "System.Runtime.CompilerServices.Unsafe" },
316-
transform: filePath => RewriteCsFileWithStandardTransform(filePath, originalNamespace: "System.Runtime", AddNullableDirectiveTransform, AddIgnoreNullabilityWarningDisablePragma));
313+
version: "6.1.2",
314+
// Download link for commit 14e29655e53aec37342e933bfd7ba574167453ff from https://github.com/dotnet/maintenance-packages
315+
downloadUrl: "https://codeload.github.com/dotnet/maintenance-packages/zip/14e29655e53aec37342e933bfd7ba574167453ff",
316+
pathToSrc: new[] { "maintenance-packages-14e29655e53aec37342e933bfd7ba574167453ff", "src", "System.Runtime.CompilerServices.Unsafe", "src" },
317+
transform: filePath =>
318+
{
319+
// Convert .il file to InlineIL-based .cs file
320+
if (string.Equals(Path.GetFileName(filePath), "System.Runtime.CompilerServices.Unsafe.il", StringComparison.OrdinalIgnoreCase))
321+
{
322+
var ilContent = File.ReadAllText(filePath);
323+
var csContent = ILToInlineILConverter.Convert(ilContent);
324+
325+
// Remove IL file, and replace with c# file instead
326+
File.Delete(filePath);
327+
filePath = Path.Combine(Path.GetDirectoryName(filePath)!, "Unsafe.cs");
328+
File.WriteAllText(
329+
filePath,
330+
csContent,
331+
new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
332+
333+
// Use the full namespace to avoid rewriting BCL usings
334+
RewriteCsFileWithStandardTransform(filePath, originalNamespace: "System.Runtime.CompilerServices.Unsafe");
335+
}
336+
},
337+
onlyIncludePaths: new[] { "System.Runtime.CompilerServices.Unsafe.il", });
317338

318339
Add(
319340
libraryName: "ICSharpCode.SharpZipLib",

tracer/dependabot/Datadog.Dependabot.Vendors.csproj

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,14 @@
4343
<!-- https://www.nuget.org/packages/System.Memory/4.6.3 -->
4444
<PackageReference Include="System.Memory" Version="4.6.3" />
4545

46-
<!-- https://www.nuget.org/packages/System.Private.CoreLib/1.0.0 -->
47-
<PackageReference Include="System.Private.CoreLib" Version="1.0.0" />
48-
4946
<!-- https://www.nuget.org/packages/System.Reflection.Metadata.Interop/7.0.20 -->
5047
<PackageReference Include="System.Reflection.Metadata.Interop" Version="7.0.20" />
5148

5249
<!-- https://www.nuget.org/packages/System.Reflection.Metadata/7.0.20 -->
5350
<PackageReference Include="System.Reflection.Metadata" Version="7.0.20" />
5451

55-
<!-- https://www.nuget.org/packages/System.Runtime.CompilerServices.Unsafe/1.0.0 -->
56-
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="1.0.0" />
52+
<!-- https://www.nuget.org/packages/System.Runtime.CompilerServices.Unsafe/6.1.2 -->
53+
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.1.2" />
5754

5855
<!-- https://www.nuget.org/packages/ICSharpCode.SharpZipLib/1.3.3 -->
5956
<PackageReference Include="ICSharpCode.SharpZipLib" Version="1.3.3" />

tracer/src/Datadog.Trace/Vendors/System.Runtime.CompilerServices.Unsafe/FodyWeavers.xml

Lines changed: 0 additions & 3 deletions
This file was deleted.

tracer/src/Datadog.Trace/Vendors/System.Runtime.CompilerServices.Unsafe/FodyWeavers.xsd

Lines changed: 0 additions & 82 deletions
This file was deleted.

tracer/src/Datadog.Trace/Vendors/System.Runtime.CompilerServices.Unsafe/System.Runtime.CompilerServices.Unsafe.csproj.txt

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)