Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 5012dfe

Browse files
authored
Restore the Linux X509Certificates tests to 0 SafeX509Handle finalizations
Since the last drive down to zero finalizations (ensuring no platform-induced ones) a few have slipped back in. This caused unnecessary distraction while investigating a different problem. This also leaves the "print a stack trace during finalization" code in place for DEBUG builds, behind an environment variable check (cached at type load).
1 parent a3bdfa1 commit 5012dfe

File tree

7 files changed

+35
-2
lines changed

7 files changed

+35
-2
lines changed

src/Common/src/Microsoft/Win32/SafeHandles/SafeX509Handles.Unix.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,30 @@
44

55
using System;
66
using System.Diagnostics;
7-
using System.Security;
87
using System.Runtime.InteropServices;
98

109
namespace Microsoft.Win32.SafeHandles
1110
{
1211
internal sealed class SafeX509Handle : SafeHandle
1312
{
13+
#if DEBUG
14+
private static readonly bool s_captureTrace =
15+
Environment.GetEnvironmentVariable("DEBUG_SAFEX509HANDLE_FINALIZATION") != null;
16+
17+
private readonly StackTrace _stacktrace =
18+
s_captureTrace ? new StackTrace(fNeedFileInfo: true) : null;
19+
20+
~SafeX509Handle()
21+
{
22+
if (s_captureTrace)
23+
{
24+
Interop.Sys.PrintF(
25+
"%s\n\n",
26+
$"0x{handle.ToInt64():x} {_stacktrace?.ToString() ?? "no stacktrace..."}");
27+
}
28+
}
29+
#endif
30+
1431
internal static readonly SafeX509Handle InvalidHandle = new SafeX509Handle();
1532

1633
private SafeX509Handle() :

src/System.Net.Http/src/System.Net.Http.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,7 @@
647647
<Reference Include="System.IO.Compression.Brotli" />
648648
</ItemGroup>
649649
<ItemGroup Condition="'$(TargetsUnix)' == 'true'">
650+
<Reference Include="System.Diagnostics.StackTrace" />
650651
<Reference Include="System.IO.FileSystem" />
651652
<Reference Include="System.Security.Cryptography.Algorithms" />
652653
<Reference Include="System.Security.Cryptography.Encoding" />

src/System.Net.Security/src/System.Net.Security.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@
222222
<Compile Include="$(CommonPath)\CoreLib\Interop\Unix\Interop.Errors.cs">
223223
<Link>Common\CoreLib\Interop\Unix\Interop.Errors.cs</Link>
224224
</Compile>
225+
<Compile Include="$(CommonPath)\Interop\Unix\System.Native\Interop.PrintF.cs">
226+
<Link>Common\Interop\Unix\System.Native\Interop.PrintF.cs</Link>
227+
</Compile>
225228
<Compile Include="$(CommonPath)\Interop\Unix\System.Net.Security.Native\Interop.Initialization.cs">
226229
<Link>Common\Interop\Unix\System.Net.Security.Native\Interop.Initialization.cs</Link>
227230
</Compile>
@@ -411,6 +414,7 @@
411414
<Reference Include="System.Threading.ThreadPool" />
412415
</ItemGroup>
413416
<ItemGroup Condition="'$(TargetsUnix)' == 'true'">
417+
<Reference Include="System.Diagnostics.StackTrace" />
414418
<Reference Include="System.Security.Cryptography.Algorithms" />
415419
<Reference Include="System.Security.Cryptography.OpenSsl" />
416420
<Reference Include="System.Security.Cryptography.Primitives" />

src/System.Security.Cryptography.X509Certificates/src/System.Security.Cryptography.X509Certificates.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@
302302
<Compile Include="Microsoft\Win32\SafeHandles\SafePasswordHandle.Unix.cs" />
303303
</ItemGroup>
304304
<ItemGroup Condition=" '$(TargetsUnix)' == 'true' AND '$(TargetsOSX)' != 'true' ">
305+
<Compile Include="$(CommonPath)\Interop\Unix\System.Native\Interop.PrintF.cs">
306+
<Link>Common\Interop\Unix\System.Native\Interop.PrintF.cs</Link>
307+
</Compile>
305308
<AsnXml Include="System\Security\Cryptography\X509Certificates\Asn1\DistributionPointAsn.xml" />
306309
<Compile Include="System\Security\Cryptography\X509Certificates\Asn1\DistributionPointAsn.xml.cs">
307310
<DependentUpon>System\Security\Cryptography\X509Certificates\Asn1\DistributionPointAsn.xml</DependentUpon>
@@ -701,6 +704,7 @@
701704
<Reference Include="System.Security.Cryptography.Csp" />
702705
</ItemGroup>
703706
<ItemGroup Condition="'$(TargetsUnix)' == 'true' AND '$(TargetsOSX)' != 'true'">
707+
<Reference Include="System.Diagnostics.StackTrace" />
704708
<Reference Include="System.Security.Cryptography.OpenSsl" />
705709
</ItemGroup>
706710
<ItemGroup>

src/System.Security.Cryptography.X509Certificates/tests/CertificateCreation/CertificateRequestChainTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ public static void ChainCertRequirements(bool useIntermed, bool? isCA, X509KeyUs
187187
}
188188

189189
RunChain(chain, leafCert, true, "Chain verification");
190+
DisposeChainCerts(chain);
190191
}
191192
}
192193
finally
@@ -513,6 +514,7 @@ public static void CreateChain_RSAPSS()
513514
chain.ChainPolicy.VerificationTime = notBefore.ToLocalTime().DateTime;
514515

515516
RunChain(chain, leafCert, true, "Chain build");
517+
DisposeChainCerts(chain);
516518
}
517519
}
518520
finally

src/System.Security.Cryptography.X509Certificates/tests/CertificateCreation/CertificateRequestUsageTests.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ public static void ReproduceBigExponentCert()
114114
"07BFD721B73DDB1751123F99D8FC0D533798C4DBD14719D5D8A85B00A144A367" +
115115
"677B48891A9B56F045334811BACB7A";
116116

117-
Assert.Equal(expectedHex, cert.RawData.ByteArrayToHex());
117+
using (cert)
118+
{
119+
Assert.Equal(expectedHex, cert.RawData.ByteArrayToHex());
120+
}
118121
}
119122

120123
[Theory]

src/System.Security.Cryptography.X509Certificates/tests/ChainTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ public static void TestResetMethod()
177177
chain.ChainPolicy.ExtraStore.Add(sampleCert);
178178
bool valid = chain.Build(sampleCert);
179179
Assert.False(valid);
180+
chainHolder.DisposeChainElements();
180181

181182
chain.ChainPolicy.VerificationFlags = X509VerificationFlags.AllowUnknownCertificateAuthority;
182183
chain.ChainPolicy.VerificationTime = new DateTime(2015, 10, 15, 12, 01, 01, DateTimeKind.Local);
@@ -186,6 +187,7 @@ public static void TestResetMethod()
186187
Assert.True(valid, "Chain built validly");
187188

188189
Assert.Equal(1, chain.ChainElements.Count);
190+
chainHolder.DisposeChainElements();
189191

190192
chain.Reset();
191193
Assert.Equal(0, chain.ChainElements.Count);

0 commit comments

Comments
 (0)