Skip to content

Commit 9f85e5d

Browse files
authored
Replace fixed buffers with InlineArray (#125514)
Mostly just trivial places. Didn't touch (yet) those that require invasive changes at uses. [Diffs](MihuBot/runtime-utils#1811) do not look bad - a couple of extra bound checks from InlineArray, a few improvements (more than regressions).
1 parent 28009ec commit 9f85e5d

23 files changed

Lines changed: 377 additions & 142 deletions

File tree

src/libraries/Common/src/Interop/FreeBSD/Interop.Process.GetProcInfo.cs

Lines changed: 80 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Diagnostics;
6+
using System.Runtime.CompilerServices;
67
using System.Runtime.InteropServices;
78

89
#pragma warning disable CA1823 // analyzer incorrectly flags fixed buffer length const (https://github.com/dotnet/roslyn/issues/37593)
@@ -34,9 +35,9 @@ internal static partial class Process
3435

3536
// From sys/_sigset.h
3637
[StructLayout(LayoutKind.Sequential)]
37-
internal unsafe struct @sigset_t
38+
internal struct @sigset_t
3839
{
39-
private fixed int bits[4];
40+
private InlineArray4<int> bits;
4041
}
4142

4243
[StructLayout(LayoutKind.Sequential)]
@@ -119,7 +120,7 @@ public unsafe struct @kinfo_proc
119120
private gid_t ki_svgid; /* Saved effective group id */
120121
private short ki_ngroups; /* number of groups */
121122
private short ki_spare_short2; /* unused (just here for alignment) */
122-
private fixed uint ki_groups[KI_NGROUPS]; /* groups */
123+
private GroupsBuffer ki_groups; /* groups */
123124
public ulong ki_size; /* virtual size */
124125
public long ki_rssize; /* current resident set size in pages */
125126
private long ki_swrss; /* resident set size before last swap */
@@ -146,14 +147,14 @@ public unsafe struct @kinfo_proc
146147
private byte ki_oncpu_old; /* Which cpu we are on (legacy) */
147148
private byte ki_lastcpu_old; /* Last cpu we were on (legacy) */
148149
public fixed byte ki_tdname[TDNAMLEN + 1]; /* thread name */
149-
private fixed byte ki_wmesg[WMESGLEN + 1]; /* wchan message */
150-
private fixed byte ki_login[LOGNAMELEN + 1]; /* setlogin name */
151-
private fixed byte ki_lockname[LOCKNAMELEN + 1]; /* lock name */
150+
private WmesgBuffer ki_wmesg; /* wchan message */
151+
private LoginBuffer ki_login; /* setlogin name */
152+
private LocknameBuffer ki_lockname; /* lock name */
152153
public fixed byte ki_comm[COMMLEN + 1]; /* command name */
153-
private fixed byte ki_emul[KI_EMULNAMELEN + 1]; /* emulation name */
154-
private fixed byte ki_loginclass[LOGINCLASSLEN + 1]; /* login class */
155-
private fixed byte ki_sparestrings[50]; /* spare string space */
156-
private fixed int ki_spareints[KI_NSPARE_INT]; /* spare room for growth */
154+
private EmulNameBuffer ki_emul; /* emulation name */
155+
private LoginClassBuffer ki_loginclass; /* login class */
156+
private SpareStringsBuffer ki_sparestrings; /* spare string space */
157+
private SpareIntsBuffer ki_spareints; /* spare room for growth */
157158
private int ki_oncpu; /* Which cpu we are on */
158159
private int ki_lastcpu; /* Last cpu we were on */
159160
private int ki_tracer; /* Pid of tracing process */
@@ -163,7 +164,7 @@ public unsafe struct @kinfo_proc
163164
private int ki_jid; /* Process jail ID */
164165
public int ki_numthreads; /* XXXKSE number of threads in total */
165166
public int ki_tid; /* XXXKSE thread id */
166-
private fixed byte ki_pri[4]; /* process priority */
167+
private PriBuffer ki_pri; /* process priority */
167168
public rusage ki_rusage; /* process rusage statistics */
168169
/* XXX - most fields in ki_rusage_ch are not (yet) filled in */
169170
private rusage ki_rusage_ch; /* rusage of children processes */
@@ -172,8 +173,74 @@ public unsafe struct @kinfo_proc
172173
private void* ki_udata; /* User convenience pointer */
173174
public void* ki_tdaddr; /* address of thread */
174175

175-
private fixed long ki_spareptrs[KI_NSPARE_PTR]; /* spare room for growth */
176-
private fixed long ki_sparelongs[KI_NSPARE_LONG]; /* spare room for growth */
176+
private SparePtrsBuffer ki_spareptrs; /* spare room for growth */
177+
private SpareLongsBuffer ki_sparelongs; /* spare room for growth */
178+
179+
[InlineArray(KI_NGROUPS)]
180+
private struct GroupsBuffer
181+
{
182+
private uint _element0;
183+
}
184+
185+
[InlineArray(WMESGLEN + 1)]
186+
private struct WmesgBuffer
187+
{
188+
private byte _element0;
189+
}
190+
191+
[InlineArray(LOCKNAMELEN + 1)]
192+
private struct LocknameBuffer
193+
{
194+
private byte _element0;
195+
}
196+
197+
[InlineArray(KI_NSPARE_INT)]
198+
private struct SpareIntsBuffer
199+
{
200+
private int _element0;
201+
}
202+
203+
[InlineArray(4)]
204+
private struct PriBuffer
205+
{
206+
private byte _element0;
207+
}
208+
209+
[InlineArray(KI_NSPARE_PTR)]
210+
private struct SparePtrsBuffer
211+
{
212+
private long _element0;
213+
}
214+
215+
[InlineArray(KI_NSPARE_LONG)]
216+
private struct SpareLongsBuffer
217+
{
218+
private long _element0;
219+
}
220+
221+
[InlineArray(LOGNAMELEN + 1)]
222+
private struct LoginBuffer
223+
{
224+
private byte _element0;
225+
}
226+
227+
[InlineArray(KI_EMULNAMELEN + 1)]
228+
private struct EmulNameBuffer
229+
{
230+
private byte _element0;
231+
}
232+
233+
[InlineArray(LOGINCLASSLEN + 1)]
234+
private struct LoginClassBuffer
235+
{
236+
private byte _element0;
237+
}
238+
239+
[InlineArray(50)]
240+
private struct SpareStringsBuffer
241+
{
242+
private byte _element0;
243+
}
177244
private long ki_sflag; /* PS_* flags */
178245
private long ki_tdflags; /* XXXKSE kthread flag */
179246
}

src/libraries/Common/src/Interop/OSX/Interop.libproc.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.ComponentModel;
77
using System.Diagnostics;
8+
using System.Runtime.CompilerServices;
89
using System.Runtime.InteropServices;
910

1011
#pragma warning disable CA1823 // analyzer incorrectly flags fixed buffer length const (https://github.com/dotnet/roslyn/issues/37593)
@@ -48,9 +49,9 @@ internal enum ThreadFlags
4849

4950
// from sys\resource.h
5051
[StructLayout(LayoutKind.Sequential)]
51-
internal unsafe struct rusage_info_v3
52+
internal struct rusage_info_v3
5253
{
53-
internal fixed byte ri_uuid[16];
54+
internal InlineArray16<byte> ri_uuid;
5455
internal ulong ri_user_time;
5556
internal ulong ri_system_time;
5657
internal ulong ri_pkg_idle_wkups;
@@ -82,7 +83,7 @@ internal unsafe struct rusage_info_v3
8283

8384
// From proc_info.h
8485
[StructLayout(LayoutKind.Sequential)]
85-
internal unsafe struct proc_threadinfo
86+
internal struct proc_threadinfo
8687
{
8788
internal ulong pth_user_time;
8889
internal ulong pth_system_time;
@@ -94,7 +95,13 @@ internal unsafe struct proc_threadinfo
9495
internal int pth_curpri;
9596
internal int pth_priority;
9697
internal int pth_maxpriority;
97-
internal fixed byte pth_name[MAXTHREADNAMESIZE];
98+
internal ThreadNameBuffer pth_name;
99+
100+
[InlineArray(MAXTHREADNAMESIZE)]
101+
internal struct ThreadNameBuffer
102+
{
103+
private byte _element0;
104+
}
98105
}
99106

100107
[StructLayout(LayoutKind.Sequential)]

src/libraries/Common/src/Interop/Windows/BCrypt/Interop.Blobs.cs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Diagnostics;
6+
using System.Runtime.CompilerServices;
67
using System.Runtime.InteropServices;
78

89
internal static partial class Interop
@@ -178,28 +179,44 @@ internal struct BCRYPT_RSAKEY_BLOB
178179
/// The BCRYPT_DSA_KEY_BLOB structure is used as a v1 header for a DSA public key or private key BLOB in memory.
179180
/// </summary>
180181
[StructLayout(LayoutKind.Sequential)]
181-
internal unsafe struct BCRYPT_DSA_KEY_BLOB
182+
internal struct BCRYPT_DSA_KEY_BLOB
182183
{
183184
internal KeyBlobMagicNumber Magic;
184185
internal int cbKey;
185-
internal fixed byte Count[4];
186-
internal fixed byte Seed[20];
187-
internal fixed byte q[20];
186+
#if NET
187+
internal InlineArray4<byte> Count;
188+
internal KeyParamBuffer Seed;
189+
internal KeyParamBuffer q;
190+
191+
[InlineArray(20)]
192+
internal struct KeyParamBuffer
193+
{
194+
private byte _element0;
195+
}
196+
#else
197+
internal unsafe fixed byte Count[4];
198+
internal unsafe fixed byte Seed[20];
199+
internal unsafe fixed byte q[20];
200+
#endif
188201
}
189202

190203
/// <summary>
191204
/// The BCRYPT_DSA_KEY_BLOB structure is used as a v2 header for a DSA public key or private key BLOB in memory.
192205
/// </summary>
193206
[StructLayout(LayoutKind.Sequential)]
194-
internal unsafe struct BCRYPT_DSA_KEY_BLOB_V2
207+
internal struct BCRYPT_DSA_KEY_BLOB_V2
195208
{
196209
internal KeyBlobMagicNumber Magic;
197210
internal int cbKey;
198211
internal HASHALGORITHM_ENUM hashAlgorithm;
199212
internal DSAFIPSVERSION_ENUM standardVersion;
200213
internal int cbSeedLength;
201214
internal int cbGroupSize;
202-
internal fixed byte Count[4];
215+
#if NET
216+
internal InlineArray4<byte> Count;
217+
#else
218+
internal unsafe fixed byte Count[4];
219+
#endif
203220
}
204221

205222
public enum HASHALGORITHM_ENUM

src/libraries/Common/src/Interop/Windows/IpHlpApi/Interop.ICMP.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Net.NetworkInformation;
6+
using System.Runtime.CompilerServices;
67
using System.Runtime.InteropServices;
78
using Microsoft.Win32.SafeHandles;
89

@@ -54,14 +55,14 @@ internal struct ICMP_ECHO_REPLY
5455
}
5556

5657
[StructLayout(LayoutKind.Sequential, Pack = 1)]
57-
internal unsafe struct IPV6_ADDRESS_EX
58+
internal struct IPV6_ADDRESS_EX
5859
{
5960
internal ushort port;
6061
internal uint flowinfo;
6162

6263
// Replying address.
63-
private fixed byte _Address[16];
64-
internal byte[] Address => MemoryMarshal.CreateReadOnlySpan(ref _Address[0], 16).ToArray();
64+
private InlineArray16<byte> _Address;
65+
internal byte[] Address => ((ReadOnlySpan<byte>)_Address).ToArray();
6566

6667
internal uint ScopeID;
6768
}

src/libraries/Common/src/Interop/Windows/IpHlpApi/Interop.IP_ADDR_STRING.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5+
using System.Runtime.CompilerServices;
56
using System.Runtime.InteropServices;
67

78
internal static partial class Interop
@@ -12,8 +13,8 @@ internal static partial class IpHlpApi
1213
public unsafe struct IP_ADDR_STRING
1314
{
1415
public IP_ADDR_STRING* Next;
15-
public fixed byte IpAddress[16];
16-
public fixed byte IpMask[16];
16+
public InlineArray16<byte> IpAddress;
17+
public InlineArray16<byte> IpMask;
1718
public uint Context;
1819
}
1920
}

src/libraries/Common/src/Interop/Windows/IpHlpApi/Interop.NetworkInformation.cs

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,24 @@ internal unsafe struct IpAdapterAddresses
148148
private IntPtr _friendlyName;
149149
internal string FriendlyName => Marshal.PtrToStringUni(_friendlyName)!;
150150

151-
private fixed byte _address[MAX_ADAPTER_ADDRESS_LENGTH];
151+
private AddrBuffer _address;
152152
private uint _addressLength;
153-
internal byte[] Address => MemoryMarshal.CreateReadOnlySpan<byte>(ref _address[0], (int)_addressLength).ToArray();
153+
internal byte[] Address => ((ReadOnlySpan<byte>)_address).Slice(0, (int)_addressLength).ToArray();
154+
155+
[InlineArray(MAX_ADAPTER_ADDRESS_LENGTH)]
156+
private struct AddrBuffer
157+
{
158+
private byte _element0;
159+
}
154160

155161
internal AdapterFlags flags;
156162
internal uint mtu;
157163
internal NetworkInterfaceType type;
158164
internal OperationalStatus operStatus;
159165
internal uint ipv6Index;
160166

161-
private fixed uint _zoneIndices[16];
162-
internal uint[] ZoneIndices => MemoryMarshal.CreateReadOnlySpan<uint>(ref _zoneIndices[0], 16).ToArray();
167+
private InlineArray16<uint> _zoneIndices;
168+
internal uint[] ZoneIndices => ((ReadOnlySpan<uint>)_zoneIndices).ToArray();
163169

164170
internal IntPtr firstPrefix;
165171

@@ -172,11 +178,17 @@ internal unsafe struct IpAdapterAddresses
172178
internal ulong luid;
173179
internal IpSocketAddress dhcpv4Server;
174180
internal uint compartmentId;
175-
internal fixed byte networkGuid[16];
181+
internal InlineArray16<byte> networkGuid;
176182
internal InterfaceConnectionType connectionType;
177183
internal InterfaceTunnelType tunnelType;
178184
internal IpSocketAddress dhcpv6Server; // Never available in Windows.
179-
internal fixed byte dhcpv6ClientDuid[130];
185+
internal Dhcpv6ClientDuidBuffer dhcpv6ClientDuid;
186+
187+
[InlineArray(130)]
188+
internal struct Dhcpv6ClientDuidBuffer
189+
{
190+
private byte _element0;
191+
}
180192
internal uint dhcpv6ClientDuidLength;
181193
internal uint dhcpV6Iaid;
182194

@@ -224,13 +236,13 @@ internal struct IpPerAdapterInfo
224236
internal unsafe struct IpAddrString
225237
{
226238
internal IpAddrString* Next; /* struct _IpAddressList* */
227-
internal fixed byte IpAddress[16];
228-
internal fixed byte IpMask[16];
239+
internal InlineArray16<byte> IpAddress;
240+
internal InlineArray16<byte> IpMask;
229241
internal uint Context;
230242
}
231243

232244
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
233-
internal unsafe struct MibIfRow2 // MIB_IF_ROW2
245+
internal struct MibIfRow2 // MIB_IF_ROW2
234246
{
235247
private const int GuidLength = 16;
236248
private const int IfMaxStringSize = 256;
@@ -239,11 +251,23 @@ internal unsafe struct MibIfRow2 // MIB_IF_ROW2
239251
internal ulong interfaceLuid;
240252
internal uint interfaceIndex;
241253
internal Guid interfaceGuid;
242-
internal fixed char alias[IfMaxStringSize + 1]; // Null terminated string.
243-
internal fixed char description[IfMaxStringSize + 1]; // Null terminated string.
254+
internal AliasBuffer alias; // Null terminated string.
255+
internal AliasBuffer description; // Null terminated string.
244256
internal uint physicalAddressLength;
245-
internal fixed byte physicalAddress[IfMaxPhysAddressLength]; // ANSI
246-
internal fixed byte permanentPhysicalAddress[IfMaxPhysAddressLength]; // ANSI
257+
internal PhysAddrBuffer physicalAddress; // ANSI
258+
internal PhysAddrBuffer permanentPhysicalAddress; // ANSI
259+
260+
[InlineArray(IfMaxStringSize + 1)]
261+
internal struct AliasBuffer
262+
{
263+
private char _element0;
264+
}
265+
266+
[InlineArray(IfMaxPhysAddressLength)]
267+
internal struct PhysAddrBuffer
268+
{
269+
private byte _element0;
270+
}
247271
internal uint mtu;
248272
internal NetworkInterfaceType type;
249273
internal InterfaceTunnelType tunnelType;
@@ -370,11 +394,17 @@ internal struct MibIcmpInfoEx
370394
}
371395

372396
[StructLayout(LayoutKind.Sequential)]
373-
internal unsafe struct MibIcmpStatsEx
397+
internal struct MibIcmpStatsEx
374398
{
375399
internal uint dwMsgs;
376400
internal uint dwErrors;
377-
internal fixed uint rgdwTypeCount[256];
401+
internal TypeCountBuffer rgdwTypeCount;
402+
403+
[InlineArray(256)]
404+
internal struct TypeCountBuffer
405+
{
406+
private uint _element0;
407+
}
378408
}
379409

380410
[StructLayout(LayoutKind.Sequential)]

0 commit comments

Comments
 (0)