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

Commit f0a032e

Browse files
authored
Clear initlocals in a bunch of assemblies (#26993)
* Clear initlocals in a bunch of assemblies Sets ```XML <ILLinkClearInitLocals>true</ILLinkClearInitLocals> ``` for the assemblies: - System.IO.FileSystem - System.IO.Pipes - System.Memory - System.Net.Http - System.Net.Http.WinHttpHandler - System.Net.NameResolution - System.Net.NetworkInformation - System.Net.Primitives - System.Net.Requests - System.Net.Security - System.Net.Sockets - System.Net.WebSockets - System.Net.WebSockets.Client - System.Net.WebSockets.WebSocketsProtocol - System.Private.Uri - System.Runtime - System.Runtime.Extensions Eventually we'd ideally set the flag for the whole repo; these are just the ones I audited thus far. In a few cases I had to make code changes to adapt, where the code was expecting stackallocs to be cleared. I also looked at a few other projects (System.Collections, System.Runtime.Numerics) where there were more significant dependencies on clearing and I decided to leave those for later. * Disable ILLinkClearInitLocals running on Unix until it works there
1 parent 23709d0 commit f0a032e

File tree

23 files changed

+165
-85
lines changed

23 files changed

+165
-85
lines changed

illink.targets

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@
7272
<!-- reflection heuristics to apply -->
7373
<ILLinkArgs>$(ILLinkArgs) -h LdtokenTypeMethods,InstanceConstructors</ILLinkArgs>
7474
<!-- add a linker step to clear initlocals flag on all assemblies before the output step -->
75-
<ILLinkArgs Condition="'$(ILLinkClearInitLocals)' == 'true'">$(ILLinkArgs) -s ILLink.CustomSteps.ClearInitLocalsStep,ILLink.CustomSteps:OutputStep</ILLinkArgs>
75+
<!-- TODO Issue #27045: Remove the OS-specific condition once the custom step runs on Unix -->
76+
<ILLinkArgs Condition="'$(ILLinkClearInitLocals)' == 'true' AND '$(OS)' == 'Windows_NT'">$(ILLinkArgs) -s ILLink.CustomSteps.ClearInitLocalsStep,ILLink.CustomSteps:OutputStep</ILLinkArgs>
7677
</PropertyGroup>
7778

7879
<MakeDir Directories="$(ILLinkTrimInputPath)" />

src/Common/src/System/Net/Logging/NetEventSource.Common.cs

Lines changed: 110 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -502,17 +502,26 @@ private unsafe void WriteEvent(int eventId, string arg1, string arg2, string arg
502502
const int NumEventDatas = 4;
503503
var descrs = stackalloc EventData[NumEventDatas];
504504

505-
descrs[0].DataPointer = (IntPtr)string1Bytes;
506-
descrs[0].Size = ((arg1.Length + 1) * 2);
507-
508-
descrs[1].DataPointer = (IntPtr)string2Bytes;
509-
descrs[1].Size = ((arg2.Length + 1) * 2);
510-
511-
descrs[2].DataPointer = (IntPtr)string3Bytes;
512-
descrs[2].Size = ((arg3.Length + 1) * 2);
513-
514-
descrs[3].DataPointer = (IntPtr)string4Bytes;
515-
descrs[3].Size = ((arg4.Length + 1) * 2);
505+
descrs[0] = new EventData
506+
{
507+
DataPointer = (IntPtr)string1Bytes,
508+
Size = ((arg1.Length + 1) * 2)
509+
};
510+
descrs[1] = new EventData
511+
{
512+
DataPointer = (IntPtr)string2Bytes,
513+
Size = ((arg2.Length + 1) * 2)
514+
};
515+
descrs[2] = new EventData
516+
{
517+
DataPointer = (IntPtr)string3Bytes,
518+
Size = ((arg3.Length + 1) * 2)
519+
};
520+
descrs[3] = new EventData
521+
{
522+
DataPointer = (IntPtr)string4Bytes,
523+
Size = ((arg4.Length + 1) * 2)
524+
};
516525

517526
WriteEventCore(eventId, NumEventDatas, descrs);
518527
}
@@ -536,17 +545,26 @@ private unsafe void WriteEvent(int eventId, string arg1, string arg2, byte[] arg
536545
const int NumEventDatas = 4;
537546
var descrs = stackalloc EventData[NumEventDatas];
538547

539-
descrs[0].DataPointer = (IntPtr)arg1Ptr;
540-
descrs[0].Size = (arg1.Length + 1) * sizeof(char);
541-
542-
descrs[1].DataPointer = (IntPtr)arg2Ptr;
543-
descrs[1].Size = (arg2.Length + 1) * sizeof(char);
544-
545-
descrs[2].DataPointer = (IntPtr)(&bufferLength);
546-
descrs[2].Size = 4;
547-
548-
descrs[3].DataPointer = (IntPtr)arg3Ptr;
549-
descrs[3].Size = bufferLength;
548+
descrs[0] = new EventData
549+
{
550+
DataPointer = (IntPtr)arg1Ptr,
551+
Size = (arg1.Length + 1) * sizeof(char)
552+
};
553+
descrs[1] = new EventData
554+
{
555+
DataPointer = (IntPtr)arg2Ptr,
556+
Size = (arg2.Length + 1) * sizeof(char)
557+
};
558+
descrs[2] = new EventData
559+
{
560+
DataPointer = (IntPtr)(&bufferLength),
561+
Size = 4
562+
};
563+
descrs[3] = new EventData
564+
{
565+
DataPointer = (IntPtr)arg3Ptr,
566+
Size = bufferLength
567+
};
550568

551569
WriteEventCore(eventId, NumEventDatas, descrs);
552570
}
@@ -565,17 +583,26 @@ private unsafe void WriteEvent(int eventId, string arg1, int arg2, int arg3, int
565583
const int NumEventDatas = 4;
566584
var descrs = stackalloc EventData[NumEventDatas];
567585

568-
descrs[0].DataPointer = (IntPtr)(arg1Ptr);
569-
descrs[0].Size = (arg1.Length + 1) * sizeof(char);
570-
571-
descrs[1].DataPointer = (IntPtr)(&arg2);
572-
descrs[1].Size = sizeof(int);
573-
574-
descrs[2].DataPointer = (IntPtr)(&arg3);
575-
descrs[2].Size = sizeof(int);
576-
577-
descrs[3].DataPointer = (IntPtr)(&arg4);
578-
descrs[3].Size = sizeof(int);
586+
descrs[0] = new EventData
587+
{
588+
DataPointer = (IntPtr)(arg1Ptr),
589+
Size = (arg1.Length + 1) * sizeof(char)
590+
};
591+
descrs[1] = new EventData
592+
{
593+
DataPointer = (IntPtr)(&arg2),
594+
Size = sizeof(int)
595+
};
596+
descrs[2] = new EventData
597+
{
598+
DataPointer = (IntPtr)(&arg3),
599+
Size = sizeof(int)
600+
};
601+
descrs[3] = new EventData
602+
{
603+
DataPointer = (IntPtr)(&arg4),
604+
Size = sizeof(int)
605+
};
579606

580607
WriteEventCore(eventId, NumEventDatas, descrs);
581608
}
@@ -596,14 +623,21 @@ private unsafe void WriteEvent(int eventId, string arg1, int arg2, string arg3)
596623
const int NumEventDatas = 3;
597624
var descrs = stackalloc EventData[NumEventDatas];
598625

599-
descrs[0].DataPointer = (IntPtr)(arg1Ptr);
600-
descrs[0].Size = (arg1.Length + 1) * sizeof(char);
601-
602-
descrs[1].DataPointer = (IntPtr)(&arg2);
603-
descrs[1].Size = sizeof(int);
604-
605-
descrs[2].DataPointer = (IntPtr)(arg3Ptr);
606-
descrs[2].Size = (arg3.Length + 1) * sizeof(char);
626+
descrs[0] = new EventData
627+
{
628+
DataPointer = (IntPtr)(arg1Ptr),
629+
Size = (arg1.Length + 1) * sizeof(char)
630+
};
631+
descrs[1] = new EventData
632+
{
633+
DataPointer = (IntPtr)(&arg2),
634+
Size = sizeof(int)
635+
};
636+
descrs[2] = new EventData
637+
{
638+
DataPointer = (IntPtr)(arg3Ptr),
639+
Size = (arg3.Length + 1) * sizeof(char)
640+
};
607641

608642
WriteEventCore(eventId, NumEventDatas, descrs);
609643
}
@@ -624,14 +658,21 @@ private unsafe void WriteEvent(int eventId, string arg1, string arg2, int arg3)
624658
const int NumEventDatas = 3;
625659
var descrs = stackalloc EventData[NumEventDatas];
626660

627-
descrs[0].DataPointer = (IntPtr)(arg1Ptr);
628-
descrs[0].Size = (arg1.Length + 1) * sizeof(char);
629-
630-
descrs[1].DataPointer = (IntPtr)(arg2Ptr);
631-
descrs[1].Size = (arg2.Length + 1) * sizeof(char);
632-
633-
descrs[2].DataPointer = (IntPtr)(&arg3);
634-
descrs[2].Size = sizeof(int);
661+
descrs[0] = new EventData
662+
{
663+
DataPointer = (IntPtr)(arg1Ptr),
664+
Size = (arg1.Length + 1) * sizeof(char)
665+
};
666+
descrs[1] = new EventData
667+
{
668+
DataPointer = (IntPtr)(arg2Ptr),
669+
Size = (arg2.Length + 1) * sizeof(char)
670+
};
671+
descrs[2] = new EventData
672+
{
673+
DataPointer = (IntPtr)(&arg3),
674+
Size = sizeof(int)
675+
};
635676

636677
WriteEventCore(eventId, NumEventDatas, descrs);
637678
}
@@ -654,17 +695,26 @@ private unsafe void WriteEvent(int eventId, string arg1, string arg2, string arg
654695
const int NumEventDatas = 4;
655696
var descrs = stackalloc EventData[NumEventDatas];
656697

657-
descrs[0].DataPointer = (IntPtr)(arg1Ptr);
658-
descrs[0].Size = (arg1.Length + 1) * sizeof(char);
659-
660-
descrs[1].DataPointer = (IntPtr)(arg2Ptr);
661-
descrs[1].Size = (arg2.Length + 1) * sizeof(char);
662-
663-
descrs[2].DataPointer = (IntPtr)(arg3Ptr);
664-
descrs[2].Size = (arg3.Length + 1) * sizeof(char);
665-
666-
descrs[3].DataPointer = (IntPtr)(&arg4);
667-
descrs[3].Size = sizeof(int);
698+
descrs[0] = new EventData
699+
{
700+
DataPointer = (IntPtr)(arg1Ptr),
701+
Size = (arg1.Length + 1) * sizeof(char)
702+
};
703+
descrs[1] = new EventData
704+
{
705+
DataPointer = (IntPtr)(arg2Ptr),
706+
Size = (arg2.Length + 1) * sizeof(char)
707+
};
708+
descrs[2] = new EventData
709+
{
710+
DataPointer = (IntPtr)(arg3Ptr),
711+
Size = (arg3.Length + 1) * sizeof(char)
712+
};
713+
descrs[3] = new EventData
714+
{
715+
DataPointer = (IntPtr)(&arg4),
716+
Size = sizeof(int)
717+
};
668718

669719
WriteEventCore(eventId, NumEventDatas, descrs);
670720
}

src/System.IO.FileSystem/src/System.IO.FileSystem.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
88
<IsPartialFacadeAssembly>true</IsPartialFacadeAssembly>
99
<UWPCompatible Condition="'$(TargetGroup)' == 'uap' or '$(TargetGroup)' == 'uapaot'">true</UWPCompatible>
10+
<ILLinkClearInitLocals>true</ILLinkClearInitLocals>
1011
</PropertyGroup>
1112
<PropertyGroup Condition="'$(TargetsUnix)' == 'true'">
1213
<NoWarn>$(NoWarn);414</NoWarn>
@@ -421,4 +422,4 @@
421422
<Reference Include="System.Threading" />
422423
</ItemGroup>
423424
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
424-
</Project>
425+
</Project>

src/System.IO.FileSystem/src/System/IO/Enumeration/FileSystemName.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ private static bool MatchPattern(ReadOnlySpan<char> expression, ReadOnlySpan<cha
140140
char expressionChar;
141141

142142
Span<int> temp = stackalloc int[0];
143-
Span<int> priorMatches = stackalloc int[16];
144143
Span<int> currentMatches = stackalloc int[16];
144+
Span<int> priorMatches = stackalloc int[16];
145+
priorMatches.Clear();
145146

146147
int maxState = expression.Length * 2;
147148
int currentState;

src/System.IO.Pipes/src/System.IO.Pipes.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
88
<DefineConstants>$(DefineConstants)</DefineConstants>
99
<OmitTransitiveCompileReferences>true</OmitTransitiveCompileReferences>
10+
<ILLinkClearInitLocals>true</ILLinkClearInitLocals>
1011
</PropertyGroup>
1112
<!-- Compiled Source Files -->
1213
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Unix-Debug|AnyCPU'" />

src/System.Memory/src/System.Memory.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<DefineConstants Condition="'$(IsPartialFacadeAssembly)' != 'true'">$(DefineConstants);netstandard;FEATURE_PORTABLE_SPAN</DefineConstants>
1010
<DefineConstants Condition="'$(TargetGroup)'=='netcoreapp'">$(DefineConstants);netcoreapp</DefineConstants>
1111
<DefineConstants Condition="'$(TargetGroup)'=='netstandard1.1'">$(DefineConstants);netstandard11</DefineConstants>
12+
<ILLinkClearInitLocals>true</ILLinkClearInitLocals>
1213
</PropertyGroup>
1314
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Unix-Debug|AnyCPU'" />
1415
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-Unix-Release|AnyCPU'" />
@@ -157,4 +158,4 @@
157158
<ProjectReference Include="..\..\System.Numerics.Vectors\src\System.Numerics.Vectors.csproj" />
158159
</ItemGroup>
159160
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
160-
</Project>
161+
</Project>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<!-- Although we have a NS configuration, the actual UWP implementation is a shim over WinRT: so just validate against OneCore -->
1111
<UWPCompatible>false</UWPCompatible>
1212
<IncludeDllSafeSearchPathAttribute>true</IncludeDllSafeSearchPathAttribute>
13+
<ILLinkClearInitLocals>true</ILLinkClearInitLocals>
1314
</PropertyGroup>
1415
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netfx-Windows_NT-Debug|AnyCPU'" />
1516
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netfx-Windows_NT-Release|AnyCPU'" />

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<NoWarn>$(NoWarn);0436</NoWarn>
1111
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1212
<DefineConstants>$(DefineConstants);HTTP_DLL</DefineConstants>
13+
<ILLinkClearInitLocals>true</ILLinkClearInitLocals>
1314
</PropertyGroup>
1415
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-OSX-Debug|AnyCPU'" />
1516
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'netcoreapp-OSX-Release|AnyCPU'" />

src/System.Net.NameResolution/src/System.Net.NameResolution.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
<AssemblyName>System.Net.NameResolution</AssemblyName>
66
<ProjectGuid>{1714448C-211E-48C1-8B7E-4EE667D336A1}</ProjectGuid>
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
8-
</PropertyGroup>
8+
<ILLinkClearInitLocals>true</ILLinkClearInitLocals>
9+
</PropertyGroup>
910
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='uap-Windows_NT-Debug|AnyCPU'" />
1011
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='uap-Windows_NT-Release|AnyCPU'" />
1112
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='netcoreapp-Windows_NT-Debug|AnyCPU'" />

src/System.Net.NameResolution/src/System/Net/NameResolutionPal.Unix.cs

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -205,26 +205,36 @@ public static unsafe string TryGetNameInfo(IPAddress addr, out SocketError socke
205205
{
206206
byte* buffer = stackalloc byte[Interop.Sys.NI_MAXHOST + 1 /*for null*/];
207207

208-
// TODO #2891: Remove the copying step to improve performance. This requires a change in the contracts.
209-
byte[] addressBuffer = addr.GetAddressBytes();
210-
211-
int error;
212-
fixed (byte* rawAddress = &addressBuffer[0])
213-
{
214-
error = Interop.Sys.GetNameInfo(
215-
rawAddress,
216-
unchecked((uint)addressBuffer.Length),
217-
addr.AddressFamily == AddressFamily.InterNetworkV6 ? (byte)1 : (byte)0,
218-
buffer,
219-
Interop.Sys.NI_MAXHOST,
220-
null,
221-
0,
222-
Interop.Sys.GetNameInfoFlags.NI_NAMEREQD);
208+
byte isIPv6;
209+
int rawAddressLength;
210+
if (addr.AddressFamily == AddressFamily.InterNetwork)
211+
{
212+
isIPv6 = 0;
213+
rawAddressLength = IPAddressParserStatics.IPv4AddressBytes;
214+
}
215+
else
216+
{
217+
isIPv6 = 1;
218+
rawAddressLength = IPAddressParserStatics.IPv6AddressBytes;
223219
}
224220

221+
byte* rawAddress = stackalloc byte[rawAddressLength];
222+
addr.TryWriteBytes(new Span<byte>(rawAddress, rawAddressLength), out int bytesWritten);
223+
Debug.Assert(bytesWritten == rawAddressLength);
224+
225+
int error = Interop.Sys.GetNameInfo(
226+
rawAddress,
227+
(uint)rawAddressLength,
228+
isIPv6,
229+
buffer,
230+
Interop.Sys.NI_MAXHOST,
231+
null,
232+
0,
233+
Interop.Sys.GetNameInfoFlags.NI_NAMEREQD);
234+
225235
socketError = GetSocketErrorForNativeError(error);
226236
nativeErrorCode = error;
227-
return socketError != SocketError.Success ? null : Marshal.PtrToStringAnsi((IntPtr)buffer);
237+
return socketError != SocketError.Success ? null : Marshal.PtrToStringAnsi((IntPtr)buffer);
228238
}
229239

230240
public static string GetHostName()

0 commit comments

Comments
 (0)