Skip to content

Commit

Permalink
Update changes so that we only vendor SharpZipLib (and use it) for ou…
Browse files Browse the repository at this point in the history
…r .NET Framework build of Datadog.Trace. This means there is no difference in behavior for other builds of Datadog.Trace.
  • Loading branch information
zacharycmontoya committed Oct 24, 2024
1 parent 89f01c0 commit 59971aa
Show file tree
Hide file tree
Showing 79 changed files with 279 additions and 49 deletions.
7 changes: 6 additions & 1 deletion tracer/build/_build/UpdateVendors/VendoredDependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ static VendoredDependency()
version: "1.3.3",
downloadUrl: "https://github.com/icsharpcode/SharpZipLib/archive/refs/tags/v1.3.3.zip",
pathToSrc: new[] { "SharpZipLib-1.3.3", "src", "ICSharpCode.SharpZipLib" },
transform: filePath => RewriteCsFileWithStandardTransform(filePath, originalNamespace: "ICSharpCode.SharpZipLib"));
transform: filePath => RewriteCsFileWithStandardTransform(filePath, originalNamespace: "ICSharpCode.SharpZipLib", AddIfNetFramework));
}

public static List<VendoredDependency> All { get; set; } = new List<VendoredDependency>();
Expand Down Expand Up @@ -170,6 +170,11 @@ private static string AddIfNetcoreapp31OrGreater(string filePath, string content
return "#if NETCOREAPP3_1_OR_GREATER" + Environment.NewLine + content + Environment.NewLine + "#endif";
}

private static string AddIfNetFramework(string filePath, string content)
{
return "#if NETFRAMEWORK" + Environment.NewLine + content + Environment.NewLine + "#endif";
}

private static string AddNullableDirectiveTransform(string filePath, string content)
{
if (!content.Contains("#nullable"))
Expand Down
2 changes: 1 addition & 1 deletion tracer/dependabot/Datadog.Dependabot.Vendors.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFrameworks>net8.0</TargetFrameworks>
<TargetFrameworks>netcoreapp3.1</TargetFrameworks>
<RootNamespace>Datadog.Dependabot.Honeypot</RootNamespace>
</PropertyGroup>

Expand Down
24 changes: 3 additions & 21 deletions tracer/src/Datadog.Trace.Trimming/build/Datadog.Trace.Trimming.xml
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@
<type fullname="System.IO.Compression.CompressionMode" />
<type fullname="System.IO.Compression.DeflateStream" />
<type fullname="System.IO.Compression.GZipStream" />
<type fullname="System.IO.Compression.ZipArchive" />
<type fullname="System.IO.Compression.ZipArchiveEntry" />
<type fullname="System.IO.Compression.ZipArchiveMode" />
</assembly>
<assembly fullname="System.IO.FileSystem">
<type fullname="System.IO.Directory" />
Expand Down Expand Up @@ -414,9 +417,6 @@
<assembly fullname="System.Net.WebProxy">
<type fullname="System.Net.WebProxy" />
</assembly>
<assembly fullname="System.Numerics.Vectors">
<type fullname="System.Numerics.Vector`1" />
</assembly>
<assembly fullname="System.ObjectModel">
<type fullname="System.Collections.ObjectModel.KeyedCollection`2" />
<type fullname="System.Collections.ObjectModel.ReadOnlyDictionary`2" />
Expand Down Expand Up @@ -613,7 +613,6 @@
<type fullname="System.IO.EndOfStreamException" />
<type fullname="System.IO.File" />
<type fullname="System.IO.FileAccess" />
<type fullname="System.IO.FileAttributes" />
<type fullname="System.IO.FileInfo" />
<type fullname="System.IO.FileMode" />
<type fullname="System.IO.FileNotFoundException" />
Expand All @@ -624,7 +623,6 @@
<type fullname="System.IO.IOException" />
<type fullname="System.IO.MemoryStream" />
<type fullname="System.IO.Path" />
<type fullname="System.IO.PathTooLongException" />
<type fullname="System.IO.SearchOption" />
<type fullname="System.IO.SeekOrigin" />
<type fullname="System.IO.Stream" />
Expand Down Expand Up @@ -787,7 +785,6 @@
<type fullname="System.RuntimeMethodHandle" />
<type fullname="System.RuntimeTypeHandle" />
<type fullname="System.SByte" />
<type fullname="System.Security.Cryptography.CryptographicException" />
<type fullname="System.Security.SecurityCriticalAttribute" />
<type fullname="System.Security.SecurityException" />
<type fullname="System.Security.SecurityRuleSet" />
Expand Down Expand Up @@ -961,13 +958,8 @@
</assembly>
<assembly fullname="System.Security.Cryptography" />
<assembly fullname="System.Security.Cryptography.Algorithms">
<type fullname="System.Security.Cryptography.Aes" />
<type fullname="System.Security.Cryptography.AsymmetricSignatureFormatter" />
<type fullname="System.Security.Cryptography.DeriveBytes" />
<type fullname="System.Security.Cryptography.IncrementalHash" />
<type fullname="System.Security.Cryptography.MD5" />
<type fullname="System.Security.Cryptography.RandomNumberGenerator" />
<type fullname="System.Security.Cryptography.Rfc2898DeriveBytes" />
<type fullname="System.Security.Cryptography.RSA" />
<type fullname="System.Security.Cryptography.RSAParameters" />
<type fullname="System.Security.Cryptography.RSAPKCS1SignatureFormatter" />
Expand All @@ -976,19 +968,9 @@
<type fullname="System.Security.Cryptography.SHA384" />
<type fullname="System.Security.Cryptography.SHA512" />
</assembly>
<assembly fullname="System.Security.Cryptography.Csp">
<type fullname="System.Security.Cryptography.RNGCryptoServiceProvider" />
</assembly>
<assembly fullname="System.Security.Cryptography.Primitives">
<type fullname="System.Security.Cryptography.AsymmetricAlgorithm" />
<type fullname="System.Security.Cryptography.CipherMode" />
<type fullname="System.Security.Cryptography.CryptoStream" />
<type fullname="System.Security.Cryptography.CryptoStreamMode" />
<type fullname="System.Security.Cryptography.HashAlgorithm" />
<type fullname="System.Security.Cryptography.HashAlgorithmName" />
<type fullname="System.Security.Cryptography.ICryptoTransform" />
<type fullname="System.Security.Cryptography.KeySizes" />
<type fullname="System.Security.Cryptography.SymmetricAlgorithm" />
</assembly>
<assembly fullname="System.Text.Encoding.Extensions">
<type fullname="System.Text.UnicodeEncoding" />
Expand Down
72 changes: 50 additions & 22 deletions tracer/src/Datadog.Trace/Logging/TracerFlare/DebugLogReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
using System.Threading;
using System.Threading.Tasks;
using Datadog.Trace.Util.Streams;
#if NETFRAMEWORK
using Datadog.Trace.Vendors.ICSharpCode.SharpZipLib;
using Datadog.Trace.Vendors.ICSharpCode.SharpZipLib.Core;
#endif

namespace Datadog.Trace.Logging.TracerFlare;

Expand Down Expand Up @@ -67,12 +69,14 @@ public static async Task WriteDebugLogArchiveToStream(Stream writeTo, string log
try
{
using var monitoringStream = new WriteCountingStream(writeTo);

#if NETFRAMEWORK
using var archive = new Vendors.ICSharpCode.SharpZipLib.Zip.ZipOutputStream(monitoringStream);
archive.IsStreamOwner = false; // Do not close the underlying stream when closing the archive
archive.SetLevel(9); // Set to optimal compression

// Create a fixed-size buffer to bound memory operations
byte[] buffer = new byte[4096];
#else
using var archive = new ZipArchive(monitoringStream, ZipArchiveMode.Create, true);
#endif

// Only sending .log files to avoid the risk of sending files we don't want.
// Also not recurrsing, in-case they have a weird log setup
Expand Down Expand Up @@ -126,25 +130,11 @@ public static async Task WriteDebugLogArchiveToStream(Stream writeTo, string log

try
{
// There's a SmallestSize for .NET 5+ but it doesn't give much benefit
// and we would rather not add the extra memory pressure for little gain
var entry = new Vendors.ICSharpCode.SharpZipLib.Zip.ZipEntry(Path.GetFileName(fileDetails.FullName));
// archive.PutNextEntry(entry);
archive.PutNextEntry(entry);

// Have to allow FileShare.ReadWrite as the logger will already have it open for writing
using var file = File.Open(fileDetails.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
// StreamUtils.Copy(file, archive, buffer);

// Using a fixed size buffer here makes no noticeable difference for output
// but keeps a lid on memory usage.
int sourceBytes;
do
{
sourceBytes = await file.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
await archive.WriteAsync(buffer, 0, sourceBytes).ConfigureAwait(false);
}
while (sourceBytes > 0);
#if NETFRAMEWORK
await AddZipEntryFrameworkAsync(fileDetails, archive).ConfigureAwait(false);
#else
await AddZipEntryCoreAsync(fileDetails, archive).ConfigureAwait(false);
#endif
}
catch (Exception ex)
{
Expand All @@ -158,6 +148,44 @@ public static async Task WriteDebugLogArchiveToStream(Stream writeTo, string log
}
}

#if NETFRAMEWORK
internal static async Task AddZipEntryFrameworkAsync(FileInfo fileDetails, Vendors.ICSharpCode.SharpZipLib.Zip.ZipOutputStream archive)
{
// Create a fixed-size buffer to bound memory operations
byte[] buffer = new byte[4096];

var entry = new Vendors.ICSharpCode.SharpZipLib.Zip.ZipEntry(Path.GetFileName(fileDetails.FullName));
archive.PutNextEntry(entry);

// Have to allow FileShare.ReadWrite as the logger will already have it open for writing
using var file = File.Open(fileDetails.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

// Using a fixed size buffer here makes no noticeable difference for output
// but keeps a lid on memory usage.
int sourceBytes;
do
{
sourceBytes = await file.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
await archive.WriteAsync(buffer, 0, sourceBytes).ConfigureAwait(false);
}
while (sourceBytes > 0);
}
#else
internal static async Task AddZipEntryCoreAsync(FileInfo fileDetails, ZipArchive archive)
{
// There's a SmallestSize for .NET 5+ but it doesn't give much benefit
// and we would rather not add the extra memory pressure for little gain
var entry = archive.CreateEntry(fileDetails.Name, CompressionLevel.Optimal);
using var entryStream = entry.Open();

// Have to allow FileShare.ReadWrite as the logger will already have it open for writing
using var file = File.Open(fileDetails.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

await file.CopyToAsync(entryStream).ConfigureAwait(false);
await entryStream.FlushAsync().ConfigureAwait(false);
}
#endif

internal static bool StreamHasCapacity(FileInfo fileDetails, long remainingCapacity)
{
// we estimate that the file compresses _roughly_ 10x, so if we're going to exceed that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// This file was automatically generated by the UpdateVendors tool.
//------------------------------------------------------------------------------
#pragma warning disable CS0618, CS0649, CS1574, CS1580, CS1581, CS1584, CS1591, CS1573, CS8018, SYSLIB0011, SYSLIB0023, SYSLIB0032
#if NETFRAMEWORK
using System;
using System.IO;

Expand Down Expand Up @@ -82,3 +83,5 @@ public static void Compress(Stream inStream, Stream outStream, bool isStreamOwne
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// This file was automatically generated by the UpdateVendors tool.
//------------------------------------------------------------------------------
#pragma warning disable CS0618, CS0649, CS1574, CS1580, CS1581, CS1584, CS1591, CS1573, CS8018, SYSLIB0011, SYSLIB0023, SYSLIB0032
#if NETFRAMEWORK
namespace Datadog.Trace.Vendors.ICSharpCode.SharpZipLib.BZip2
{
/// <summary>
Expand Down Expand Up @@ -120,3 +121,5 @@ internal static class BZip2Constants
public const int OvershootBytes = 20;
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// This file was automatically generated by the UpdateVendors tool.
//------------------------------------------------------------------------------
#pragma warning disable CS0618, CS0649, CS1574, CS1580, CS1581, CS1584, CS1591, CS1573, CS8018, SYSLIB0011, SYSLIB0023, SYSLIB0032
#if NETFRAMEWORK
using System;
using System.Runtime.Serialization;

Expand Down Expand Up @@ -57,3 +58,5 @@ protected BZip2Exception(SerializationInfo info, StreamingContext context)
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// This file was automatically generated by the UpdateVendors tool.
//------------------------------------------------------------------------------
#pragma warning disable CS0618, CS0649, CS1574, CS1580, CS1581, CS1584, CS1591, CS1573, CS8018, SYSLIB0011, SYSLIB0023, SYSLIB0032
#if NETFRAMEWORK
#if NETSTANDARD2_1_OR_GREATER || NETCOREAPP3_1_OR_GREATER
#define VECTORIZE_MEMORY_MOVE
#endif
Expand Down Expand Up @@ -1056,3 +1057,5 @@ private static void HbCreateDecodeTables(int[] limit, int[] baseArray, int[] per
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// This file was automatically generated by the UpdateVendors tool.
//------------------------------------------------------------------------------
#pragma warning disable CS0618, CS0649, CS1574, CS1580, CS1581, CS1584, CS1591, CS1573, CS8018, SYSLIB0011, SYSLIB0023, SYSLIB0032
#if NETFRAMEWORK
using Datadog.Trace.Vendors.ICSharpCode.SharpZipLib.Checksum;
using System;
using System.IO;
Expand Down Expand Up @@ -2036,3 +2037,5 @@ private struct StackElement
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// This file was automatically generated by the UpdateVendors tool.
//------------------------------------------------------------------------------
#pragma warning disable CS0618, CS0649, CS1574, CS1580, CS1581, CS1584, CS1591, CS1573, CS8018, SYSLIB0011, SYSLIB0023, SYSLIB0032
#if NETFRAMEWORK
using System;

namespace Datadog.Trace.Vendors.ICSharpCode.SharpZipLib.Checksum
Expand Down Expand Up @@ -166,3 +167,5 @@ public void Update(ArraySegment<byte> segment)
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// This file was automatically generated by the UpdateVendors tool.
//------------------------------------------------------------------------------
#pragma warning disable CS0618, CS0649, CS1574, CS1580, CS1581, CS1584, CS1591, CS1573, CS8018, SYSLIB0011, SYSLIB0023, SYSLIB0032
#if NETFRAMEWORK
using System;
using System.Runtime.CompilerServices;

Expand Down Expand Up @@ -174,3 +175,5 @@ private void SlowUpdateLoop(byte[] data, int offset, int end)
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// This file was automatically generated by the UpdateVendors tool.
//------------------------------------------------------------------------------
#pragma warning disable CS0618, CS0649, CS1574, CS1580, CS1581, CS1584, CS1591, CS1573, CS8018, SYSLIB0011, SYSLIB0023, SYSLIB0032
#if NETFRAMEWORK
using System;
using System.Runtime.CompilerServices;

Expand Down Expand Up @@ -176,3 +177,5 @@ private void SlowUpdateLoop(byte[] data, int offset, int end)
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// This file was automatically generated by the UpdateVendors tool.
//------------------------------------------------------------------------------
#pragma warning disable CS0618, CS0649, CS1574, CS1580, CS1581, CS1584, CS1591, CS1573, CS8018, SYSLIB0011, SYSLIB0023, SYSLIB0032
#if NETFRAMEWORK
using System.Runtime.CompilerServices;

namespace Datadog.Trace.Vendors.ICSharpCode.SharpZipLib.Checksum
Expand Down Expand Up @@ -161,3 +162,5 @@ private static uint UpdateDataCommon(byte[] input, int offset, uint[] crcTable,
}
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// This file was automatically generated by the UpdateVendors tool.
//------------------------------------------------------------------------------
#pragma warning disable CS0618, CS0649, CS1574, CS1580, CS1581, CS1584, CS1591, CS1573, CS8018, SYSLIB0011, SYSLIB0023, SYSLIB0032
#if NETFRAMEWORK
using System;

namespace Datadog.Trace.Vendors.ICSharpCode.SharpZipLib.Checksum
Expand Down Expand Up @@ -54,3 +55,5 @@ long Value
void Update(ArraySegment<byte> segment);
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// This file was automatically generated by the UpdateVendors tool.
//------------------------------------------------------------------------------
#pragma warning disable CS0618, CS0649, CS1574, CS1580, CS1581, CS1584, CS1591, CS1573, CS8018, SYSLIB0011, SYSLIB0023, SYSLIB0032
#if NETFRAMEWORK
using System;

namespace Datadog.Trace.Vendors.ICSharpCode.SharpZipLib.Core
Expand All @@ -20,3 +21,5 @@ internal static class EmptyArray<T>
#endif
}
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// This file was automatically generated by the UpdateVendors tool.
//------------------------------------------------------------------------------
#pragma warning disable CS0618, CS0649, CS1574, CS1580, CS1581, CS1584, CS1591, CS1573, CS8018, SYSLIB0011, SYSLIB0023, SYSLIB0032
#if NETFRAMEWORK
using System;
using System.Runtime.Serialization;

Expand Down Expand Up @@ -61,3 +62,5 @@ protected SharpZipBaseException(SerializationInfo info, StreamingContext context
}
}
}

#endif
Loading

0 comments on commit 59971aa

Please sign in to comment.