Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

#pragma warning disable CA1823 // analyzer incorrectly flags fixed buffer length const (https://github.com/dotnet/roslyn/issues/37593)

internal static partial class Interop
{
internal static partial class Process
Expand Down Expand Up @@ -146,11 +144,11 @@ public unsafe struct @kinfo_proc
private byte ki_rqindex; /* Run queue index */
private byte ki_oncpu_old; /* Which cpu we are on (legacy) */
private byte ki_lastcpu_old; /* Last cpu we were on (legacy) */
public fixed byte ki_tdname[TDNAMLEN + 1]; /* thread name */
public TdNameBuffer ki_tdname; /* thread name */
private WmesgBuffer ki_wmesg; /* wchan message */
private LoginBuffer ki_login; /* setlogin name */
private LocknameBuffer ki_lockname; /* lock name */
public fixed byte ki_comm[COMMLEN + 1]; /* command name */
public CommBuffer ki_comm; /* command name */
private EmulNameBuffer ki_emul; /* emulation name */
private LoginClassBuffer ki_loginclass; /* login class */
private SpareStringsBuffer ki_sparestrings; /* spare string space */
Expand Down Expand Up @@ -182,6 +180,18 @@ private struct GroupsBuffer
private uint _element0;
}

[InlineArray(TDNAMLEN + 1)]
internal struct TdNameBuffer
{
private byte _element0;
}

[InlineArray(COMMLEN + 1)]
internal struct CommBuffer
{
private byte _element0;
}

[InlineArray(WMESGLEN + 1)]
private struct WmesgBuffer
{
Expand Down
6 changes: 4 additions & 2 deletions src/libraries/Common/src/Interop/FreeBSD/Interop.Process.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using System.Text;

#pragma warning disable CA1823 // analyzer incorrectly flags fixed buffer length const (https://github.com/dotnet/roslyn/issues/37593)

Expand Down Expand Up @@ -120,7 +120,9 @@ public static unsafe ProcessInfo GetProcessInfoById(int pid)
// Get the process information for the specified pid
info = new ProcessInfo();

info.ProcessName = Utf8StringMarshaller.ConvertToManaged(kinfo->ki_comm)!;
ReadOnlySpan<byte> comm = kinfo->ki_comm;
int nul = comm.IndexOf((byte)0);
Comment thread
MihaZupan marked this conversation as resolved.
info.ProcessName = Encoding.UTF8.GetString(nul >= 0 ? comm[..nul] : comm);
info.BasePriority = kinfo->ki_nice;
info.VirtualBytes = (long)kinfo->ki_size;
info.WorkingSet = kinfo->ki_rssize;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

#pragma warning disable CA1823 // analyzer incorrectly flags fixed buffer length const (https://github.com/dotnet/roslyn/issues/37593)

internal static partial class Interop
{
internal static partial class @libproc
Expand All @@ -18,7 +17,7 @@ internal static partial class @libproc

// From proc_info.h
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct proc_bsdinfo
internal struct proc_bsdinfo
{
internal uint pbi_flags;
internal uint pbi_status;
Expand All @@ -32,8 +31,8 @@ internal unsafe struct proc_bsdinfo
internal uint pbi_svuid;
internal uint pbi_svgid;
internal uint reserved;
internal fixed byte pbi_comm[MAXCOMLEN];
internal fixed byte pbi_name[MAXCOMLEN * 2];
internal CommBuffer pbi_comm;
internal NameBuffer pbi_name;
internal uint pbi_nfiles;
internal uint pbi_pgid;
internal uint pbi_pjobc;
Expand All @@ -42,6 +41,18 @@ internal unsafe struct proc_bsdinfo
internal int pbi_nice;
internal ulong pbi_start_tvsec;
internal ulong pbi_start_tvusec;

[InlineArray(MAXCOMLEN)]
internal struct CommBuffer
{
private byte _element0;
}

[InlineArray(MAXCOMLEN * 2)]
internal struct NameBuffer
{
private byte _element0;
}
}

// From proc_info.h
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public unsafe struct @kinfo_proc
private ushort p_xstat; /* U_SHORT: Exit status for wait; also stop signal. */
private ushort p_spare; /* U_SHORT: unused */

public fixed byte p_comm[KI_MAXCOMLEN]; /* command name */
public NameBuffer p_comm; /* command name */

private WmesgBuffer p_wmesg; /* wchan message */
private ulong p_wchan; /* PTR: sleep address. */
Expand Down Expand Up @@ -175,7 +175,7 @@ private struct EmulNameBuffer
}

[InlineArray(KI_MAXCOMLEN)]
private struct NameBuffer
internal struct NameBuffer
{
private byte _element0;
}
Expand Down
5 changes: 4 additions & 1 deletion src/libraries/Common/src/Interop/OpenBSD/Interop.Process.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;
using System.Text;

#pragma warning disable CA1823 // analyzer incorrectly flags fixed buffer length const (https://github.com/dotnet/roslyn/issues/37593)

Expand Down Expand Up @@ -177,7 +178,9 @@ public static unsafe string GetProcPath(int pid)
// Get the process information for the specified pid
info = new ProcessInfo();

info.ProcessName = Utf8StringMarshaller.ConvertToManaged(kinfo->p_comm)!;
ReadOnlySpan<byte> comm = kinfo->p_comm;
int nul = comm.IndexOf((byte)0);
info.ProcessName = Encoding.UTF8.GetString(nul >= 0 ? comm[..nul] : comm);

// p_comm is limited to KI_MAXCOMLEN - 1 characters. When the name is at that
// limit it may be truncated, so try to recover the full name from the process argv.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

internal static partial class Interop
Expand All @@ -20,22 +21,22 @@ internal static partial class Sys
// NOTE: `_isIPv6` cannot be of type `bool` because `bool` is not a blittable type and this struct is
// embedded in other structs for interop purposes.
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct IPAddress : IEquatable<IPAddress>
internal struct IPAddress : IEquatable<IPAddress>
{
public bool IsIPv6
{
get { return _isIPv6 != 0; }
set { _isIPv6 = value ? 1u : 0u; }
}

internal fixed byte Address[MAX_IP_ADDRESS_BYTES]; // Buffer to fit an IPv4 or IPv6 address
private uint _isIPv6; // Non-zero if this is an IPv6 address; zero for IPv4.
internal uint ScopeId; // Scope ID (IPv6 only)
internal AddressBuffer Address; // Buffer to fit an IPv4 or IPv6 address
private uint _isIPv6; // Non-zero if this is an IPv6 address; zero for IPv4.
internal uint ScopeId; // Scope ID (IPv6 only)

public override int GetHashCode()
{
HashCode h = default;
h.AddBytes(MemoryMarshal.CreateReadOnlySpan(ref Address[0], IsIPv6 ? IPv6AddressBytes : IPv4AddressBytes));
h.AddBytes(Address[..(IsIPv6 ? IPv6AddressBytes : IPv4AddressBytes)]);
return h.ToHashCode();
}

Expand Down Expand Up @@ -68,8 +69,13 @@ public bool Equals(IPAddress other)
addressByteCount = IPv4AddressBytes;
}

return MemoryMarshal.CreateReadOnlySpan(ref Address[0], addressByteCount).SequenceEqual(
new ReadOnlySpan<byte>(other.Address, addressByteCount));
return Address[..addressByteCount].SequenceEqual(other.Address[..addressByteCount]);
}

[InlineArray(MAX_IP_ADDRESS_BYTES)]
internal struct AddressBuffer
{
private byte _element0;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,24 @@ internal unsafe struct DNS_QUERY_RESULT
}

[StructLayout(LayoutKind.Sequential)]
internal unsafe struct DNS_QUERY_CANCEL
internal struct DNS_QUERY_CANCEL
{
public fixed byte Reserved[32];
public ReservedBuffer Reserved;

[InlineArray(32)]
Comment thread
EgorBo marked this conversation as resolved.
internal struct ReservedBuffer
{
private byte _element0;
}
}

[StructLayout(LayoutKind.Sequential)]
internal unsafe struct DNS_ADDR
internal struct DNS_ADDR
{
// SOCKET_ADDRESS-like: 32 bytes of SOCKADDR_STORAGE-ish + extras.
// DnsApi documents this struct as 64 bytes total with the first 32
// being the SOCKADDR (IPv4/IPv6 SOCKADDR fits within).
public fixed byte MaxSa[32];
public MaxSaBuffer MaxSa;
public uint DnsAddrUserDword0;
public uint DnsAddrUserDword1;
public uint DnsAddrUserDword2;
Expand All @@ -54,6 +60,12 @@ internal unsafe struct DNS_ADDR
public uint DnsAddrUserDword5;
public uint DnsAddrUserDword6;
public uint DnsAddrUserDword7;

[InlineArray(32)]
internal struct MaxSaBuffer
{
private byte _element0;
}
}

[StructLayout(LayoutKind.Sequential)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

internal static partial class Interop
Expand All @@ -13,33 +14,51 @@ internal static partial class IpHlpApi
public const int MAX_SCOPE_ID_LEN = 256;

[StructLayout(LayoutKind.Sequential)]
public unsafe struct FIXED_INFO
public struct FIXED_INFO
{
private fixed byte _hostName[MAX_HOSTNAME_LEN + 4];
public string HostName => CreateString(ref _hostName[0], MAX_HOSTNAME_LEN + 4);
private HostNameBuffer _hostName;
public string HostName => CreateString(_hostName);

private fixed byte _domainName[MAX_DOMAIN_NAME_LEN + 4];
public string DomainName => CreateString(ref _domainName[0], MAX_DOMAIN_NAME_LEN + 4);
private DomainNameBuffer _domainName;
public string DomainName => CreateString(_domainName);

public IntPtr currentDnsServer; // IpAddressList*
public IP_ADDR_STRING DnsServerList;
public uint nodeType;

private fixed byte _scopeId[MAX_SCOPE_ID_LEN + 4];
public string ScopeId => CreateString(ref _scopeId[0], MAX_SCOPE_ID_LEN + 4);
private ScopeIdBuffer _scopeId;
public string ScopeId => CreateString(_scopeId);

public uint enableRouting;
public uint enableProxy;
public uint enableDns;

private static string CreateString(ref byte firstByte, int maxLength)
private static unsafe string CreateString(ReadOnlySpan<byte> buffer)
{
fixed (byte* ptr = &firstByte)
int terminator = buffer.IndexOf((byte)0);
fixed (byte* ptr = buffer)
{
int terminator = new ReadOnlySpan<byte>(ptr, maxLength).IndexOf((byte)0);
return Marshal.PtrToStringAnsi((IntPtr)ptr, (terminator >= 0) ? terminator : maxLength);
return Marshal.PtrToStringAnsi((IntPtr)ptr, (terminator >= 0) ? terminator : buffer.Length);
}
}

[InlineArray(MAX_HOSTNAME_LEN + 4)]
private struct HostNameBuffer
{
private byte _element0;
}

[InlineArray(MAX_DOMAIN_NAME_LEN + 4)]
private struct DomainNameBuffer
{
private byte _element0;
}

[InlineArray(MAX_SCOPE_ID_LEN + 4)]
private struct ScopeIdBuffer
{
private byte _element0;
}
}
}
}
15 changes: 13 additions & 2 deletions src/libraries/Common/src/Interop/Windows/NCrypt/Interop.Keys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Cryptography;

Expand Down Expand Up @@ -72,12 +73,22 @@ internal struct CRYPT_PKCS12_PBE_PARAMS
}

[StructLayout(LayoutKind.Sequential)]
internal unsafe struct PBE_PARAMS
internal struct PBE_PARAMS
{
internal const int RgbSaltSize = 8;

internal CRYPT_PKCS12_PBE_PARAMS Params;
internal fixed byte rgbSalt[RgbSaltSize];
#if NET
internal SaltBuffer rgbSalt;

[InlineArray(RgbSaltSize)]
internal struct SaltBuffer
{
private byte _element0;
}
#else
internal unsafe fixed byte rgbSalt[RgbSaltSize];
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

internal static partial class Interop
{
internal static partial class SspiCli
{
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct TOKEN_SOURCE
internal struct TOKEN_SOURCE
{
internal fixed byte SourceName[TOKEN_SOURCE_LENGTH];
internal SourceNameBuffer SourceName;
internal LUID SourceIdentifier;

internal const int TOKEN_SOURCE_LENGTH = 8;

[InlineArray(TOKEN_SOURCE_LENGTH)]
internal struct SourceNameBuffer
{
private byte _element0;
}
}
}
}
Loading
Loading