diff --git a/src/Common/src/Interop/BSD/System.Native/Interop.Sysctl.cs b/src/Common/src/Interop/BSD/System.Native/Interop.Sysctl.cs index 2b4c51e165dc..d887b072bc39 100644 --- a/src/Common/src/Interop/BSD/System.Native/Interop.Sysctl.cs +++ b/src/Common/src/Interop/BSD/System.Native/Interop.Sysctl.cs @@ -26,7 +26,7 @@ internal static partial class Sys public static unsafe int Sysctl(Span name, ref byte* value, ref int len) { - fixed (int * ptr = &name.DangerousGetPinnableReference()) + fixed (int * ptr = &MemoryMarshal.GetReference(name)) { return Sysctl(ptr, name.Length, ref value, ref len); } diff --git a/src/Common/src/Interop/OSX/Interop.CoreFoundation.CFData.cs b/src/Common/src/Interop/OSX/Interop.CoreFoundation.CFData.cs index 0e736a4d0a5e..5920e32038e8 100644 --- a/src/Common/src/Interop/OSX/Interop.CoreFoundation.CFData.cs +++ b/src/Common/src/Interop/OSX/Interop.CoreFoundation.CFData.cs @@ -62,7 +62,7 @@ internal static unsafe bool TryCFWriteData(SafeCFDataHandle cfData, Span d } byte* dataBytes = CFDataGetBytePtr(cfData); - fixed (byte* destinationPtr = &destination.DangerousGetPinnableReference()) + fixed (byte* destinationPtr = &MemoryMarshal.GetReference(destination)) { Buffer.MemoryCopy(dataBytes, destinationPtr, destination.Length, length); } diff --git a/src/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Digest.cs b/src/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Digest.cs index 8d8b13aeb3bf..9135ee7e62bb 100644 --- a/src/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Digest.cs +++ b/src/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Digest.cs @@ -17,13 +17,13 @@ internal static partial class AppleCrypto internal static extern SafeDigestCtxHandle DigestCreate(PAL_HashAlgorithm algorithm, out int cbDigest); internal static int DigestUpdate(SafeDigestCtxHandle ctx, ReadOnlySpan pbData, int cbData) => - DigestUpdate(ctx, ref pbData.DangerousGetPinnableReference(), cbData); + DigestUpdate(ctx, ref MemoryMarshal.GetReference(pbData), cbData); [DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_DigestUpdate")] private static extern int DigestUpdate(SafeDigestCtxHandle ctx, ref byte pbData, int cbData); internal static int DigestFinal(SafeDigestCtxHandle ctx, Span pbOutput, int cbOutput) => - DigestFinal(ctx, ref pbOutput.DangerousGetPinnableReference(), cbOutput); + DigestFinal(ctx, ref MemoryMarshal.GetReference(pbOutput), cbOutput); [DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_DigestFinal")] private static extern int DigestFinal(SafeDigestCtxHandle ctx, ref byte pbOutput, int cbOutput); diff --git a/src/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Hmac.cs b/src/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Hmac.cs index 6dc7d97d7142..1120aca17734 100644 --- a/src/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Hmac.cs +++ b/src/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.Hmac.cs @@ -20,13 +20,13 @@ internal static partial class AppleCrypto internal static extern unsafe int HmacInit(SafeHmacHandle ctx, [In] byte[] pbKey, int cbKey); internal static int HmacUpdate(SafeHmacHandle ctx, ReadOnlySpan pbData, int cbData) => - HmacUpdate(ctx, ref pbData.DangerousGetPinnableReference(), cbData); + HmacUpdate(ctx, ref MemoryMarshal.GetReference(pbData), cbData); [DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_HmacUpdate")] private static extern int HmacUpdate(SafeHmacHandle ctx, ref byte pbData, int cbData); internal static int HmacFinal(SafeHmacHandle ctx, ReadOnlySpan pbOutput, int cbOutput) => - HmacFinal(ctx, ref pbOutput.DangerousGetPinnableReference(), cbOutput); + HmacFinal(ctx, ref MemoryMarshal.GetReference(pbOutput), cbOutput); [DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_HmacFinal")] private static extern unsafe int HmacFinal(SafeHmacHandle ctx, ref byte pbOutput, int cbOutput); diff --git a/src/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.RSA.cs b/src/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.RSA.cs index 06d86859095c..1c99e6c5e6da 100644 --- a/src/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.RSA.cs +++ b/src/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.RSA.cs @@ -28,7 +28,7 @@ private static int RsaEncryptOaep( PAL_HashAlgorithm mgfAlgorithm, out SafeCFDataHandle pEncryptedOut, out SafeCFErrorHandle pErrorOut) => - RsaEncryptOaep(publicKey, ref pbData.DangerousGetPinnableReference(), cbData, mgfAlgorithm, out pEncryptedOut, out pErrorOut); + RsaEncryptOaep(publicKey, ref MemoryMarshal.GetReference(pbData), cbData, mgfAlgorithm, out pEncryptedOut, out pErrorOut); [DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_RsaEncryptOaep")] private static extern int RsaEncryptOaep( @@ -45,7 +45,7 @@ private static int RsaEncryptPkcs( int cbData, out SafeCFDataHandle pEncryptedOut, out SafeCFErrorHandle pErrorOut) => - RsaEncryptPkcs(publicKey, ref pbData.DangerousGetPinnableReference(), cbData, out pEncryptedOut, out pErrorOut); + RsaEncryptPkcs(publicKey, ref MemoryMarshal.GetReference(pbData), cbData, out pEncryptedOut, out pErrorOut); [DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_RsaEncryptPkcs")] private static extern int RsaEncryptPkcs( @@ -62,7 +62,7 @@ private static int RsaDecryptOaep( PAL_HashAlgorithm mgfAlgorithm, out SafeCFDataHandle pEncryptedOut, out SafeCFErrorHandle pErrorOut) => - RsaDecryptOaep(publicKey, ref pbData.DangerousGetPinnableReference(), cbData, mgfAlgorithm, out pEncryptedOut, out pErrorOut); + RsaDecryptOaep(publicKey, ref MemoryMarshal.GetReference(pbData), cbData, mgfAlgorithm, out pEncryptedOut, out pErrorOut); [DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_RsaDecryptOaep")] private static extern int RsaDecryptOaep( @@ -79,7 +79,7 @@ private static int RsaDecryptPkcs( int cbData, out SafeCFDataHandle pEncryptedOut, out SafeCFErrorHandle pErrorOut) => - RsaDecryptPkcs(publicKey, ref pbData.DangerousGetPinnableReference(), cbData, out pEncryptedOut, out pErrorOut); + RsaDecryptPkcs(publicKey, ref MemoryMarshal.GetReference(pbData), cbData, out pEncryptedOut, out pErrorOut); [DllImport(Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_RsaDecryptPkcs")] private static extern int RsaDecryptPkcs( diff --git a/src/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.SecKeyRef.cs b/src/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.SecKeyRef.cs index 83fa6433ca1b..37dd66d5acb2 100644 --- a/src/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.SecKeyRef.cs +++ b/src/Common/src/Interop/OSX/System.Security.Cryptography.Native.Apple/Interop.SecKeyRef.cs @@ -28,7 +28,7 @@ private static int AppleCryptoNative_GenerateSignature( out SafeCFDataHandle pSignatureOut, out SafeCFErrorHandle pErrorOut) => AppleCryptoNative_GenerateSignature( - privateKey, ref pbDataHash.DangerousGetPinnableReference(), cbDataHash, out pSignatureOut, out pErrorOut); + privateKey, ref MemoryMarshal.GetReference(pbDataHash), cbDataHash, out pSignatureOut, out pErrorOut); [DllImport(Libraries.AppleCryptoNative)] private static extern int AppleCryptoNative_GenerateSignature( @@ -46,7 +46,7 @@ private static int AppleCryptoNative_GenerateSignatureWithHashAlgorithm( out SafeCFDataHandle pSignatureOut, out SafeCFErrorHandle pErrorOut) => AppleCryptoNative_GenerateSignatureWithHashAlgorithm( - privateKey, ref pbDataHash.DangerousGetPinnableReference(), cbDataHash, hashAlgorithm, out pSignatureOut, out pErrorOut); + privateKey, ref MemoryMarshal.GetReference(pbDataHash), cbDataHash, hashAlgorithm, out pSignatureOut, out pErrorOut); [DllImport(Libraries.AppleCryptoNative)] private static extern int AppleCryptoNative_GenerateSignatureWithHashAlgorithm( @@ -66,9 +66,9 @@ private static int AppleCryptoNative_VerifySignature( out SafeCFErrorHandle pErrorOut) => AppleCryptoNative_VerifySignature( publicKey, - ref pbDataHash.DangerousGetPinnableReference(), + ref MemoryMarshal.GetReference(pbDataHash), cbDataHash, - ref pbSignature.DangerousGetPinnableReference(), + ref MemoryMarshal.GetReference(pbSignature), cbSignature, out pErrorOut); @@ -91,9 +91,9 @@ private static int AppleCryptoNative_VerifySignatureWithHashAlgorithm( out SafeCFErrorHandle pErrorOut) => AppleCryptoNative_VerifySignatureWithHashAlgorithm( publicKey, - ref pbDataHash.DangerousGetPinnableReference(), + ref MemoryMarshal.GetReference(pbDataHash), cbDataHash, - ref pbSignature.DangerousGetPinnableReference(), + ref MemoryMarshal.GetReference(pbSignature), cbSignature, hashAlgorithm, out pErrorOut); diff --git a/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Dsa.cs b/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Dsa.cs index 4144e9d710a0..bb610f60f3ad 100644 --- a/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Dsa.cs +++ b/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Dsa.cs @@ -67,14 +67,14 @@ internal static int DsaKeySize(SafeDsaHandle dsa) } internal static bool DsaSign(SafeDsaHandle dsa, ReadOnlySpan hash, int hashLength, ReadOnlySpan refSignature, out int outSignatureLength) => - DsaSign(dsa, ref hash.DangerousGetPinnableReference(), hashLength, ref refSignature.DangerousGetPinnableReference(), out outSignatureLength); + DsaSign(dsa, ref MemoryMarshal.GetReference(hash), hashLength, ref MemoryMarshal.GetReference(refSignature), out outSignatureLength); [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_DsaSign")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool DsaSign(SafeDsaHandle dsa, ref byte hash, int hashLength, ref byte refSignature, out int outSignatureLength); internal static bool DsaVerify(SafeDsaHandle dsa, ReadOnlySpan hash, int hashLength, ReadOnlySpan signature, int signatureLength) => - DsaVerify(dsa, ref hash.DangerousGetPinnableReference(), hashLength, ref signature.DangerousGetPinnableReference(), signatureLength); + DsaVerify(dsa, ref MemoryMarshal.GetReference(hash), hashLength, ref MemoryMarshal.GetReference(signature), signatureLength); [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_DsaVerify")] [return: MarshalAs(UnmanagedType.Bool)] diff --git a/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EVP.cs b/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EVP.cs index b2ce394c75b4..67a9b542306a 100644 --- a/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EVP.cs +++ b/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EVP.cs @@ -20,7 +20,7 @@ internal static partial class Crypto internal extern static int EvpDigestReset(SafeEvpMdCtxHandle ctx, IntPtr type); internal static int EvpDigestUpdate(SafeEvpMdCtxHandle ctx, ReadOnlySpan d, int cnt) => - EvpDigestUpdate(ctx, ref d.DangerousGetPinnableReference(), cnt); + EvpDigestUpdate(ctx, ref MemoryMarshal.GetReference(d), cnt); [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpDigestUpdate")] private extern static int EvpDigestUpdate(SafeEvpMdCtxHandle ctx, ref byte d, int cnt); diff --git a/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EcDsa.cs b/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EcDsa.cs index d466797e7137..91c1beab99fe 100644 --- a/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EcDsa.cs +++ b/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EcDsa.cs @@ -11,14 +11,14 @@ internal static partial class Interop internal static partial class Crypto { internal static bool EcDsaSign(ReadOnlySpan dgst, int dlen, Span sig, [In, Out] ref int siglen, SafeEcKeyHandle ecKey) => - EcDsaSign(ref dgst.DangerousGetPinnableReference(), dlen, ref sig.DangerousGetPinnableReference(), ref siglen, ecKey); + EcDsaSign(ref MemoryMarshal.GetReference(dgst), dlen, ref MemoryMarshal.GetReference(sig), ref siglen, ecKey); [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EcDsaSign")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool EcDsaSign(ref byte dgst, int dlen, ref byte sig, [In, Out] ref int siglen, SafeEcKeyHandle ecKey); internal static unsafe int EcDsaVerify(ReadOnlySpan dgst, int dgst_len, ReadOnlySpan sigbuf, int sig_len, SafeEcKeyHandle ecKey) => - EcDsaVerify(ref dgst.DangerousGetPinnableReference(), dgst_len, ref sigbuf.DangerousGetPinnableReference(), sig_len, ecKey); + EcDsaVerify(ref MemoryMarshal.GetReference(dgst), dgst_len, ref MemoryMarshal.GetReference(sigbuf), sig_len, ecKey); /*- * returns diff --git a/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Hmac.cs b/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Hmac.cs index bf151fa0aea8..6849b4f2e09e 100644 --- a/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Hmac.cs +++ b/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Hmac.cs @@ -20,7 +20,7 @@ internal static partial class Crypto internal extern static int HmacReset(SafeHmacCtxHandle ctx); internal static int HmacUpdate(SafeHmacCtxHandle ctx, ReadOnlySpan data, int len) => - HmacUpdate(ctx, ref data.DangerousGetPinnableReference(), len); + HmacUpdate(ctx, ref MemoryMarshal.GetReference(data), len); [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_HmacUpdate")] private extern static int HmacUpdate(SafeHmacCtxHandle ctx, ref byte data, int len); diff --git a/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.OpenSsl.cs b/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.OpenSsl.cs index 13b38b7604f8..dd10bf54bab3 100644 --- a/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.OpenSsl.cs +++ b/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.OpenSsl.cs @@ -374,7 +374,7 @@ private static unsafe int AlpnServerSelectCallback(IntPtr ssl, out byte* outp, o Span clientProto = clientList.Slice(1, length); if (clientProto.SequenceEqual(protocolList[i].Protocol.Span)) { - fixed (byte* p = &clientProto.DangerousGetPinnableReference()) outp = p; + fixed (byte* p = &MemoryMarshal.GetReference(clientProto)) outp = p; outlen = length; return Ssl.SSL_TLSEXT_ERR_OK; } diff --git a/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Rsa.cs b/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Rsa.cs index 549727b31dce..1748a66547c8 100644 --- a/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Rsa.cs +++ b/src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Rsa.cs @@ -31,7 +31,7 @@ internal static int RsaPublicEncrypt( Span to, SafeRsaHandle rsa, RsaPadding padding) => - RsaPublicEncrypt(flen, ref from.DangerousGetPinnableReference(), ref to.DangerousGetPinnableReference(), rsa, padding); + RsaPublicEncrypt(flen, ref MemoryMarshal.GetReference(from), ref MemoryMarshal.GetReference(to), rsa, padding); [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_RsaPublicEncrypt")] private extern static int RsaPublicEncrypt( @@ -47,7 +47,7 @@ internal static int RsaPrivateDecrypt( Span to, SafeRsaHandle rsa, RsaPadding padding) => - RsaPrivateDecrypt(flen, ref from.DangerousGetPinnableReference(), ref to.DangerousGetPinnableReference(), rsa, padding); + RsaPrivateDecrypt(flen, ref MemoryMarshal.GetReference(from), ref MemoryMarshal.GetReference(to), rsa, padding); [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_RsaPrivateDecrypt")] private extern static int RsaPrivateDecrypt( @@ -64,14 +64,14 @@ private extern static int RsaPrivateDecrypt( internal static extern int RsaGenerateKeyEx(SafeRsaHandle rsa, int bits, SafeBignumHandle e); internal static bool RsaSign(int type, ReadOnlySpan m, int m_len, Span sigret, out int siglen, SafeRsaHandle rsa) => - RsaSign(type, ref m.DangerousGetPinnableReference(), m_len, ref sigret.DangerousGetPinnableReference(), out siglen, rsa); + RsaSign(type, ref MemoryMarshal.GetReference(m), m_len, ref MemoryMarshal.GetReference(sigret), out siglen, rsa); [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_RsaSign")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool RsaSign(int type, ref byte m, int m_len, ref byte sigret, out int siglen, SafeRsaHandle rsa); internal static bool RsaVerify(int type, ReadOnlySpan m, int m_len, ReadOnlySpan sigbuf, int siglen, SafeRsaHandle rsa) => - RsaVerify(type, ref m.DangerousGetPinnableReference(), m_len, ref sigbuf.DangerousGetPinnableReference(), siglen, rsa); + RsaVerify(type, ref MemoryMarshal.GetReference(m), m_len, ref MemoryMarshal.GetReference(sigbuf), siglen, rsa); [DllImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_RsaVerify")] [return: MarshalAs(UnmanagedType.Bool)] diff --git a/src/Common/src/Interop/Windows/BCrypt/Interop.BCryptFinishHash.cs b/src/Common/src/Interop/Windows/BCrypt/Interop.BCryptFinishHash.cs index 3bf2877685d0..7ffda6ca6c03 100644 --- a/src/Common/src/Interop/Windows/BCrypt/Interop.BCryptFinishHash.cs +++ b/src/Common/src/Interop/Windows/BCrypt/Interop.BCryptFinishHash.cs @@ -11,7 +11,7 @@ internal partial class Interop internal partial class BCrypt { internal static NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, Span pbOutput, int cbOutput, int dwFlags) => - BCryptFinishHash(hHash, ref pbOutput.DangerousGetPinnableReference(), cbOutput, dwFlags); + BCryptFinishHash(hHash, ref MemoryMarshal.GetReference(pbOutput), cbOutput, dwFlags); [DllImport(Libraries.BCrypt, CharSet = CharSet.Unicode)] private static extern NTSTATUS BCryptFinishHash(SafeBCryptHashHandle hHash, ref byte pbOutput, int cbOutput, int dwFlags); diff --git a/src/Common/src/Interop/Windows/BCrypt/Interop.BCryptHashData.cs b/src/Common/src/Interop/Windows/BCrypt/Interop.BCryptHashData.cs index 1ea3ade6ec4c..2832c7b68c44 100644 --- a/src/Common/src/Interop/Windows/BCrypt/Interop.BCryptHashData.cs +++ b/src/Common/src/Interop/Windows/BCrypt/Interop.BCryptHashData.cs @@ -12,7 +12,7 @@ internal partial class Interop internal partial class BCrypt { internal static NTSTATUS BCryptHashData(SafeBCryptHashHandle hHash, ReadOnlySpan pbInput, int cbInput, int dwFlags) => - BCryptHashData(hHash, ref pbInput.DangerousGetPinnableReference(), cbInput, dwFlags); + BCryptHashData(hHash, ref MemoryMarshal.GetReference(pbInput), cbInput, dwFlags); [DllImport(Libraries.BCrypt, CharSet = CharSet.Unicode)] private static extern NTSTATUS BCryptHashData(SafeBCryptHashHandle hHash, ref byte pbInput, int cbInput, int dwFlags); diff --git a/src/Common/src/Interop/Windows/NCrypt/Interop.EncryptDecrypt.cs b/src/Common/src/Interop/Windows/NCrypt/Interop.EncryptDecrypt.cs index 1690876e8b61..d9d9a397c7c2 100644 --- a/src/Common/src/Interop/Windows/NCrypt/Interop.EncryptDecrypt.cs +++ b/src/Common/src/Interop/Windows/NCrypt/Interop.EncryptDecrypt.cs @@ -11,13 +11,13 @@ internal static partial class Interop internal static partial class NCrypt { internal static unsafe ErrorCode NCryptEncrypt(SafeNCryptKeyHandle hKey, ReadOnlySpan pbInput, int cbInput, void* pPaddingInfo, Span pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags) => - NCryptEncrypt(hKey, ref pbInput.DangerousGetPinnableReference(), cbInput, pPaddingInfo, ref pbOutput.DangerousGetPinnableReference(), cbOutput, out pcbResult, dwFlags); + NCryptEncrypt(hKey, ref MemoryMarshal.GetReference(pbInput), cbInput, pPaddingInfo, ref MemoryMarshal.GetReference(pbOutput), cbOutput, out pcbResult, dwFlags); [DllImport(Interop.Libraries.NCrypt, CharSet = CharSet.Unicode)] private static extern unsafe ErrorCode NCryptEncrypt(SafeNCryptKeyHandle hKey, ref byte pbInput, int cbInput, void* pPaddingInfo, ref byte pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags); internal static unsafe ErrorCode NCryptDecrypt(SafeNCryptKeyHandle hKey, ReadOnlySpan pbInput, int cbInput, void* pPaddingInfo, Span pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags) => - NCryptDecrypt(hKey, ref pbInput.DangerousGetPinnableReference(), cbInput, pPaddingInfo, ref pbOutput.DangerousGetPinnableReference(), cbOutput, out pcbResult, dwFlags); + NCryptDecrypt(hKey, ref MemoryMarshal.GetReference(pbInput), cbInput, pPaddingInfo, ref MemoryMarshal.GetReference(pbOutput), cbOutput, out pcbResult, dwFlags); [DllImport(Interop.Libraries.NCrypt, CharSet = CharSet.Unicode)] private static extern unsafe ErrorCode NCryptDecrypt(SafeNCryptKeyHandle hKey, ref byte pbInput, int cbInput, void* pPaddingInfo, ref byte pbOutput, int cbOutput, out int pcbResult, AsymmetricPaddingMode dwFlags); diff --git a/src/Common/src/Interop/Windows/NCrypt/Interop.SignVerify.cs b/src/Common/src/Interop/Windows/NCrypt/Interop.SignVerify.cs index ddbaa508c14a..9626e0ba2e43 100644 --- a/src/Common/src/Interop/Windows/NCrypt/Interop.SignVerify.cs +++ b/src/Common/src/Interop/Windows/NCrypt/Interop.SignVerify.cs @@ -11,13 +11,13 @@ internal static partial class Interop internal static partial class NCrypt { internal static unsafe ErrorCode NCryptSignHash(SafeNCryptKeyHandle hKey, void* pPaddingInfo, ReadOnlySpan pbHashValue, int cbHashValue, Span pbSignature, int cbSignature, out int pcbResult, AsymmetricPaddingMode dwFlags) => - NCryptSignHash(hKey, pPaddingInfo, ref pbHashValue.DangerousGetPinnableReference(), cbHashValue, ref pbSignature.DangerousGetPinnableReference(), cbSignature, out pcbResult, dwFlags); + NCryptSignHash(hKey, pPaddingInfo, ref MemoryMarshal.GetReference(pbHashValue), cbHashValue, ref MemoryMarshal.GetReference(pbSignature), cbSignature, out pcbResult, dwFlags); [DllImport(Libraries.NCrypt, CharSet = CharSet.Unicode)] private static extern unsafe ErrorCode NCryptSignHash(SafeNCryptKeyHandle hKey, void* pPaddingInfo, ref byte pbHashValue, int cbHashValue, ref byte pbSignature, int cbSignature, out int pcbResult, AsymmetricPaddingMode dwFlags); internal static unsafe ErrorCode NCryptVerifySignature(SafeNCryptKeyHandle hKey, void* pPaddingInfo, ReadOnlySpan pbHashValue, int cbHashValue, ReadOnlySpan pbSignature, int cbSignature, AsymmetricPaddingMode dwFlags) => - NCryptVerifySignature(hKey, pPaddingInfo, ref pbHashValue.DangerousGetPinnableReference(), cbHashValue, ref pbSignature.DangerousGetPinnableReference(), cbSignature, dwFlags); + NCryptVerifySignature(hKey, pPaddingInfo, ref MemoryMarshal.GetReference(pbHashValue), cbHashValue, ref MemoryMarshal.GetReference(pbSignature), cbSignature, dwFlags); [DllImport(Libraries.NCrypt, CharSet = CharSet.Unicode)] private static extern unsafe ErrorCode NCryptVerifySignature(SafeNCryptKeyHandle hKey, void* pPaddingInfo, ref byte pbHashValue, int cbHashValue, ref byte pbSignature, int cbSignature, AsymmetricPaddingMode dwFlags); diff --git a/src/Common/src/Interop/Windows/kernel32/Interop.GetSystemDirectoryW.cs b/src/Common/src/Interop/Windows/kernel32/Interop.GetSystemDirectoryW.cs index 25c59d743bb1..cf1b31d62c0c 100644 --- a/src/Common/src/Interop/Windows/kernel32/Interop.GetSystemDirectoryW.cs +++ b/src/Common/src/Interop/Windows/kernel32/Interop.GetSystemDirectoryW.cs @@ -14,7 +14,7 @@ internal static partial class Kernel32 internal static unsafe int GetSystemDirectoryW(Span buffer) { - fixed (char* bufferPtr = &buffer.DangerousGetPinnableReference()) + fixed (char* bufferPtr = &MemoryMarshal.GetReference(buffer)) { return GetSystemDirectoryW(bufferPtr, buffer.Length); } diff --git a/src/Common/src/System/Globalization/FormatProvider.Number.cs b/src/Common/src/System/Globalization/FormatProvider.Number.cs index 28c8e0d5c169..fa92c9cce9bc 100644 --- a/src/Common/src/System/Globalization/FormatProvider.Number.cs +++ b/src/Common/src/System/Globalization/FormatProvider.Number.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Diagnostics; +using System.Runtime.InteropServices; using System.Text; namespace System.Globalization @@ -564,7 +565,7 @@ internal static unsafe bool TryStringToNumber(ReadOnlySpan str, NumberStyl { Debug.Assert(numfmt != null); - fixed (char* stringPointer = &str.DangerousGetPinnableReference()) + fixed (char* stringPointer = &MemoryMarshal.GetReference(str)) { char* p = stringPointer; if (!ParseNumber(ref p, options, ref number, sb, numfmt, parseDecimal) @@ -1211,7 +1212,7 @@ private static unsafe int FindSection(ReadOnlySpan format, int section) return 0; } - fixed (char* pFormat = &format.DangerousGetPinnableReference()) + fixed (char* pFormat = &MemoryMarshal.GetReference(format)) { int src = 0; for (;;) @@ -1287,7 +1288,7 @@ internal static unsafe void NumberToStringFormat(ref ValueStringBuilder sb, ref scaleAdjust = 0; src = section; - fixed (char* pFormat = &format.DangerousGetPinnableReference()) + fixed (char* pFormat = &MemoryMarshal.GetReference(format)) { while (src < format.Length && (ch = pFormat[src++]) != 0 && ch != ';') { @@ -1477,7 +1478,7 @@ internal static unsafe void NumberToStringFormat(ref ValueStringBuilder sb, ref bool decimalWritten = false; - fixed (char* pFormat = &format.DangerousGetPinnableReference()) + fixed (char* pFormat = &MemoryMarshal.GetReference(format)) { char* cur = dig; diff --git a/src/Common/src/System/Memory/FixedBufferExtensions.cs b/src/Common/src/System/Memory/FixedBufferExtensions.cs index 08cb16fe1f63..ce6b827afc8d 100644 --- a/src/Common/src/System/Memory/FixedBufferExtensions.cs +++ b/src/Common/src/System/Memory/FixedBufferExtensions.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Runtime.InteropServices; + namespace System { internal static class FixedBufferExtensions @@ -11,7 +13,7 @@ internal static class FixedBufferExtensions /// internal unsafe static string GetStringFromFixedBuffer(this ReadOnlySpan span) { - fixed (char* c = &span.DangerousGetPinnableReference()) + fixed (char* c = &MemoryMarshal.GetReference(span)) { return new string(c, 0, span.GetFixedBufferStringLength()); } diff --git a/src/Common/src/System/Net/SocketAddressPal.Unix.cs b/src/Common/src/System/Net/SocketAddressPal.Unix.cs index 93f3377939bc..f7eaf04fbde2 100644 --- a/src/Common/src/System/Net/SocketAddressPal.Unix.cs +++ b/src/Common/src/System/Net/SocketAddressPal.Unix.cs @@ -5,6 +5,7 @@ using System; using System.Diagnostics; using System.Net.Sockets; +using System.Runtime.InteropServices; using System.Text; namespace System.Net @@ -119,7 +120,7 @@ public static unsafe void GetIPv6Address(byte[] buffer, Span address, out uint localScope; Interop.Error err; fixed (byte* rawAddress = buffer) - fixed (byte* ipAddress = &address.DangerousGetPinnableReference()) + fixed (byte* ipAddress = &MemoryMarshal.GetReference(address)) { err = Interop.Sys.GetIPv6Address(rawAddress, buffer.Length, ipAddress, address.Length, &localScope); } @@ -147,7 +148,7 @@ public static unsafe void SetIPv4Address(byte[] buffer, byte* address) public static unsafe void SetIPv6Address(byte[] buffer, Span address, uint scope) { - fixed (byte* rawInput = &address.DangerousGetPinnableReference()) + fixed (byte* rawInput = &MemoryMarshal.GetReference(address)) { SetIPv6Address(buffer, rawInput, address.Length, scope); } diff --git a/src/Common/src/System/Security/Cryptography/AsnReader.cs b/src/Common/src/System/Security/Cryptography/AsnReader.cs index af60e0a54518..54601ff32117 100644 --- a/src/Common/src/System/Security/Cryptography/AsnReader.cs +++ b/src/Common/src/System/Security/Cryptography/AsnReader.cs @@ -1930,8 +1930,8 @@ private static unsafe bool TryCopyCharacterString( return true; } - fixed (byte* bytePtr = &source.DangerousGetPinnableReference()) - fixed (char* charPtr = &destination.DangerousGetPinnableReference()) + fixed (byte* bytePtr = &MemoryMarshal.GetReference(source)) + fixed (char* charPtr = &MemoryMarshal.GetReference(destination)) { try { @@ -1981,7 +1981,7 @@ private string GetCharacterString( { unsafe { - fixed (byte* bytePtr = &contents.DangerousGetPinnableReference()) + fixed (byte* bytePtr = &MemoryMarshal.GetReference(contents)) { try { diff --git a/src/Common/src/System/Security/Cryptography/AsnWriter.cs b/src/Common/src/System/Security/Cryptography/AsnWriter.cs index 89c92604a642..27ae44e153e8 100644 --- a/src/Common/src/System/Security/Cryptography/AsnWriter.cs +++ b/src/Common/src/System/Security/Cryptography/AsnWriter.cs @@ -11,6 +11,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Numerics; +using System.Runtime.InteropServices; namespace System.Security.Cryptography.Asn1 { @@ -1431,7 +1432,7 @@ private void WriteCharacterStringCore(Asn1Tag tag, Text.Encoding encoding, ReadO // TODO: Split this for netstandard vs netcoreapp for span?. unsafe { - fixed (char* strPtr = &str.DangerousGetPinnableReference()) + fixed (char* strPtr = &MemoryMarshal.GetReference(str)) { size = encoding.GetByteCount(strPtr, str.Length); @@ -1448,7 +1449,7 @@ private void WriteCharacterStringCore(Asn1Tag tag, Text.Encoding encoding, ReadO // TODO: Split this for netstandard vs netcoreapp for span?. unsafe { - fixed (char* strPtr = &str.DangerousGetPinnableReference()) + fixed (char* strPtr = &MemoryMarshal.GetReference(str)) { if (size < 0) { @@ -1460,7 +1461,7 @@ private void WriteCharacterStringCore(Asn1Tag tag, Text.Encoding encoding, ReadO WriteLength(size); Span dest = _buffer.AsSpan().Slice(_offset, size); - fixed (byte* destPtr = &dest.DangerousGetPinnableReference()) + fixed (byte* destPtr = &MemoryMarshal.GetReference(dest)) { int written = encoding.GetBytes(strPtr, str.Length, destPtr, dest.Length); @@ -1485,7 +1486,7 @@ private void WriteConstructedCerCharacterString(Asn1Tag tag, Text.Encoding encod // TODO: Split this for netstandard vs netcoreapp for span?. unsafe { - fixed (char* strPtr = &str.DangerousGetPinnableReference()) + fixed (char* strPtr = &MemoryMarshal.GetReference(str)) { tmp = ArrayPool.Shared.Rent(size); diff --git a/src/System.IO.Compression/src/System.IO.Compression.csproj b/src/System.IO.Compression/src/System.IO.Compression.csproj index 7f0beb87da08..7144c677ef79 100644 --- a/src/System.IO.Compression/src/System.IO.Compression.csproj +++ b/src/System.IO.Compression/src/System.IO.Compression.csproj @@ -103,6 +103,7 @@ + diff --git a/src/System.IO.Compression/src/System/IO/Compression/Crc32Helper.ZLib.cs b/src/System.IO.Compression/src/System/IO/Compression/Crc32Helper.ZLib.cs index bbfe6acd6e04..459252ae4b97 100644 --- a/src/System.IO.Compression/src/System/IO/Compression/Crc32Helper.ZLib.cs +++ b/src/System.IO.Compression/src/System/IO/Compression/Crc32Helper.ZLib.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Diagnostics; +using System.Runtime.InteropServices; namespace System.IO.Compression { @@ -20,7 +21,7 @@ public static unsafe uint UpdateCrc32(uint crc32, byte[] buffer, int offset, int public static unsafe uint UpdateCrc32(uint crc32, ReadOnlySpan buffer) { - fixed (byte* bufferPtr = &buffer.DangerousGetPinnableReference()) + fixed (byte* bufferPtr = &MemoryMarshal.GetReference(buffer)) { return Interop.zlib.crc32(crc32, bufferPtr, buffer.Length); } diff --git a/src/System.IO.Compression/src/System/IO/Compression/DeflateZLib/DeflateStream.cs b/src/System.IO.Compression/src/System/IO/Compression/DeflateZLib/DeflateStream.cs index 7a7b9f07edee..26939accc608 100644 --- a/src/System.IO.Compression/src/System/IO/Compression/DeflateZLib/DeflateStream.cs +++ b/src/System.IO.Compression/src/System/IO/Compression/DeflateZLib/DeflateStream.cs @@ -5,6 +5,7 @@ using System.Buffers; using System.Diagnostics; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; @@ -493,7 +494,7 @@ internal void WriteCore(ReadOnlySpan source) unsafe { // Pass new bytes through deflater and write them too: - fixed (byte* bufferPtr = &source.DangerousGetPinnableReference()) + fixed (byte* bufferPtr = &MemoryMarshal.GetReference(source)) { _deflater.SetInput(bufferPtr, source.Length); WriteDeflaterOutput(); diff --git a/src/System.IO.Compression/src/System/IO/Compression/DeflateZLib/Inflater.cs b/src/System.IO.Compression/src/System/IO/Compression/DeflateZLib/Inflater.cs index e55da6de3cfb..e045555d3615 100644 --- a/src/System.IO.Compression/src/System/IO/Compression/DeflateZLib/Inflater.cs +++ b/src/System.IO.Compression/src/System/IO/Compression/DeflateZLib/Inflater.cs @@ -70,7 +70,7 @@ public unsafe int Inflate(Span destination) if (destination.Length == 0) return 0; - fixed (byte* bufPtr = &destination.DangerousGetPinnableReference()) + fixed (byte* bufPtr = &MemoryMarshal.GetReference(destination)) { return InflateVerified(bufPtr, destination.Length); } diff --git a/src/System.IO.FileSystem/src/System/IO/CharSpanExtensions.Unix.cs b/src/System.IO.FileSystem/src/System/IO/CharSpanExtensions.Unix.cs index e985003d2e47..a1fae547774f 100644 --- a/src/System.IO.FileSystem/src/System/IO/CharSpanExtensions.Unix.cs +++ b/src/System.IO.FileSystem/src/System/IO/CharSpanExtensions.Unix.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Runtime.InteropServices; namespace System.IO { @@ -16,8 +17,8 @@ internal static unsafe bool EqualsOrdinal(ReadOnlySpan first, ReadOnlySpan if (!ignoreCase) return first.SequenceEqual(second); - fixed (char* fp = &first.DangerousGetPinnableReference()) - fixed (char* sp = &second.DangerousGetPinnableReference()) + fixed (char* fp = &MemoryMarshal.GetReference(first)) + fixed (char* sp = &MemoryMarshal.GetReference(second)) { char* f = fp; char* s = sp; diff --git a/src/System.IO.FileSystem/src/System/IO/CharSpanExtensions.Windows.cs b/src/System.IO.FileSystem/src/System/IO/CharSpanExtensions.Windows.cs index a0c1481048ca..05036350384a 100644 --- a/src/System.IO.FileSystem/src/System/IO/CharSpanExtensions.Windows.cs +++ b/src/System.IO.FileSystem/src/System/IO/CharSpanExtensions.Windows.cs @@ -13,9 +13,9 @@ internal static partial class CharSpanExtensions internal static unsafe int CompareOrdinal(ReadOnlySpan first, ReadOnlySpan second, bool ignoreCase = false) { int result = Interop.Kernel32.CompareStringOrdinal( - ref first.DangerousGetPinnableReference(), + ref MemoryMarshal.GetReference(first), first.Length, - ref second.DangerousGetPinnableReference(), + ref MemoryMarshal.GetReference(second), second.Length, ignoreCase); diff --git a/src/System.IO.FileSystem/src/System/IO/PathHelpers.Windows.cs b/src/System.IO.FileSystem/src/System/IO/PathHelpers.Windows.cs index 8fa39312e5aa..990527577b05 100644 --- a/src/System.IO.FileSystem/src/System/IO/PathHelpers.Windows.cs +++ b/src/System.IO.FileSystem/src/System/IO/PathHelpers.Windows.cs @@ -2,13 +2,15 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Runtime.InteropServices; + namespace System.IO { internal static partial class PathInternal { internal static unsafe int GetRootLength(ReadOnlySpan path) { - fixed (char* p = &path.DangerousGetPinnableReference()) + fixed (char* p = &MemoryMarshal.GetReference(path)) { return (int)GetRootLength(p, (uint)path.Length); } diff --git a/src/System.IO.FileSystem/src/System/IO/PathHelpers.cs b/src/System.IO.FileSystem/src/System/IO/PathHelpers.cs index 58fb6e5ecd66..aa54d434febe 100644 --- a/src/System.IO.FileSystem/src/System/IO/PathHelpers.cs +++ b/src/System.IO.FileSystem/src/System/IO/PathHelpers.cs @@ -4,6 +4,7 @@ using System.Diagnostics; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; namespace System.IO { diff --git a/src/System.IO.Pipes/src/System.IO.Pipes.csproj b/src/System.IO.Pipes/src/System.IO.Pipes.csproj index fb683ea8dc9b..e927afffe58f 100644 --- a/src/System.IO.Pipes/src/System.IO.Pipes.csproj +++ b/src/System.IO.Pipes/src/System.IO.Pipes.csproj @@ -245,6 +245,7 @@ + diff --git a/src/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Unix.cs b/src/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Unix.cs index 4a44d62487dc..1f4d2a9e263a 100644 --- a/src/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Unix.cs +++ b/src/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Unix.cs @@ -6,6 +6,7 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Net.Sockets; +using System.Runtime.InteropServices; using System.Security; using System.Threading; using System.Threading.Tasks; @@ -114,7 +115,7 @@ private unsafe int ReadCore(Span buffer) } // For anonymous pipes, read from the file descriptor. - fixed (byte* bufPtr = &buffer.DangerousGetPinnableReference()) + fixed (byte* bufPtr = &MemoryMarshal.GetReference(buffer)) { int result = CheckPipeCall(Interop.Sys.Read(_handle, bufPtr, buffer.Length)); Debug.Assert(result <= buffer.Length); @@ -149,7 +150,7 @@ private unsafe void WriteCore(ReadOnlySpan buffer) } // For anonymous pipes, write the file descriptor. - fixed (byte* bufPtr = &buffer.DangerousGetPinnableReference()) + fixed (byte* bufPtr = &MemoryMarshal.GetReference(buffer)) { while (buffer.Length > 0) { @@ -223,7 +224,7 @@ private async Task WriteAsyncCore(ReadOnlyMemory source, CancellationToken // accepts a Memory in the near future. byte[] buffer; int offset, count; - if (source.DangerousTryGetArray(out ArraySegment segment)) + if (MemoryMarshal.TryGetArray(source, out ArraySegment segment)) { buffer = segment.Array; offset = segment.Offset; diff --git a/src/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Windows.cs b/src/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Windows.cs index 7d5fe092c108..7f5e654bc8ba 100644 --- a/src/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Windows.cs +++ b/src/System.IO.Pipes/src/System/IO/Pipes/PipeStream.Windows.cs @@ -343,7 +343,7 @@ private unsafe int ReadFileNative(SafePipeHandle handle, Span buffer, Nati int r = 0; int numBytesRead = 0; - fixed (byte* p = &buffer.DangerousGetPinnableReference()) + fixed (byte* p = &MemoryMarshal.GetReference(buffer)) { r = _isAsync ? Interop.Kernel32.ReadFile(handle, p, buffer.Length, IntPtr.Zero, overlapped) : @@ -381,7 +381,7 @@ private unsafe int WriteFileNative(SafePipeHandle handle, ReadOnlySpan buf int r = 0; int numBytesWritten = 0; - fixed (byte* p = &buffer.DangerousGetPinnableReference()) + fixed (byte* p = &MemoryMarshal.GetReference(buffer)) { r = _isAsync ? Interop.Kernel32.WriteFile(handle, p, buffer.Length, IntPtr.Zero, overlapped) : diff --git a/src/System.Memory/src/System/Buffers/Binary/Reader.cs b/src/System.Memory/src/System/Buffers/Binary/Reader.cs index 1f2dab094a5d..c9d4fefef332 100644 --- a/src/System.Memory/src/System/Buffers/Binary/Reader.cs +++ b/src/System.Memory/src/System/Buffers/Binary/Reader.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; #if !netstandard using Internal.Runtime.CompilerServices; @@ -120,7 +121,7 @@ public static T ReadMachineEndian(ReadOnlySpan buffer) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length); } - return Unsafe.ReadUnaligned(ref buffer.DangerousGetPinnableReference()); + return Unsafe.ReadUnaligned(ref MemoryMarshal.GetReference(buffer)); } /// @@ -147,7 +148,7 @@ public static bool TryReadMachineEndian(ReadOnlySpan buffer, out T valu value = default; return false; } - value = Unsafe.ReadUnaligned(ref buffer.DangerousGetPinnableReference()); + value = Unsafe.ReadUnaligned(ref MemoryMarshal.GetReference(buffer)); return true; } } diff --git a/src/System.Memory/src/System/Buffers/Binary/Writer.cs b/src/System.Memory/src/System/Buffers/Binary/Writer.cs index cd9669fdbb52..fe59623138aa 100644 --- a/src/System.Memory/src/System/Buffers/Binary/Writer.cs +++ b/src/System.Memory/src/System/Buffers/Binary/Writer.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; #if !netstandard using Internal.Runtime.CompilerServices; @@ -34,7 +35,7 @@ public static void WriteMachineEndian(Span buffer, ref T value) { ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length); } - Unsafe.WriteUnaligned(ref buffer.DangerousGetPinnableReference(), value); + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(buffer), value); } /// @@ -60,7 +61,7 @@ public static bool TryWriteMachineEndian(Span buffer, ref T value) { return false; } - Unsafe.WriteUnaligned(ref buffer.DangerousGetPinnableReference(), value); + Unsafe.WriteUnaligned(ref MemoryMarshal.GetReference(buffer), value); return true; } } diff --git a/src/System.Memory/src/System/Buffers/Text/Base64Decoder.cs b/src/System.Memory/src/System/Buffers/Text/Base64Decoder.cs index de69c5fedd02..8070b9a2a34e 100644 --- a/src/System.Memory/src/System/Buffers/Text/Base64Decoder.cs +++ b/src/System.Memory/src/System/Buffers/Text/Base64Decoder.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; #if !netstandard using Internal.Runtime.CompilerServices; @@ -31,8 +32,8 @@ public static partial class Base64 /// public static OperationStatus DecodeFromUtf8(ReadOnlySpan utf8, Span bytes, out int consumed, out int written, bool isFinalBlock = true) { - ref byte srcBytes = ref utf8.DangerousGetPinnableReference(); - ref byte destBytes = ref bytes.DangerousGetPinnableReference(); + ref byte srcBytes = ref MemoryMarshal.GetReference(utf8); + ref byte destBytes = ref MemoryMarshal.GetReference(bytes); int srcLength = utf8.Length & ~0x3; // only decode input up to the closest multiple of 4. int destLength = bytes.Length; @@ -211,7 +212,7 @@ public static OperationStatus DecodeFromUtf8InPlace(Span buffer, out int w if (bufferLength == 0) goto DoneExit; - ref byte bufferBytes = ref buffer.DangerousGetPinnableReference(); + ref byte bufferBytes = ref MemoryMarshal.GetReference(buffer); ref sbyte decodingMap = ref s_decodingMap[0]; diff --git a/src/System.Memory/src/System/Buffers/Text/Base64Encoder.cs b/src/System.Memory/src/System/Buffers/Text/Base64Encoder.cs index 3d55956b33ed..fab5542c03e4 100644 --- a/src/System.Memory/src/System/Buffers/Text/Base64Encoder.cs +++ b/src/System.Memory/src/System/Buffers/Text/Base64Encoder.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; #if !netstandard using Internal.Runtime.CompilerServices; @@ -32,8 +33,8 @@ public static partial class Base64 /// public static OperationStatus EncodeToUtf8(ReadOnlySpan bytes, Span utf8, out int consumed, out int written, bool isFinalBlock = true) { - ref byte srcBytes = ref bytes.DangerousGetPinnableReference(); - ref byte destBytes = ref utf8.DangerousGetPinnableReference(); + ref byte srcBytes = ref MemoryMarshal.GetReference(bytes); + ref byte destBytes = ref MemoryMarshal.GetReference(utf8); int srcLength = bytes.Length; int destLength = utf8.Length; @@ -141,7 +142,7 @@ public static OperationStatus EncodeToUtf8InPlace(Span buffer, int dataLen int result = 0; ref byte encodingMap = ref s_encodingMap[0]; - ref byte bufferBytes = ref buffer.DangerousGetPinnableReference(); + ref byte bufferBytes = ref MemoryMarshal.GetReference(buffer); // encode last pack to avoid conditional in the main loop if (leftover != 0) diff --git a/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Date.G.cs b/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Date.G.cs index 312016e1c7d3..aeebd7d65d40 100644 --- a/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Date.G.cs +++ b/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Date.G.cs @@ -8,6 +8,8 @@ using System.Runtime.CompilerServices; #endif +using System.Runtime.InteropServices; + namespace System.Buffers.Text { public static partial class Utf8Formatter @@ -41,7 +43,7 @@ private static bool TryFormatDateTimeG(DateTime value, TimeSpan offset, Span buffer, out in return false; } - ref byte utf8Bytes = ref buffer.DangerousGetPinnableReference(); + ref byte utf8Bytes = ref MemoryMarshal.GetReference(buffer); byte[] dayAbbrev = DayAbbreviationsLowercase[(int)value.DayOfWeek]; Unsafe.Add(ref utf8Bytes, 0) = dayAbbrev[0]; diff --git a/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Date.O.cs b/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Date.O.cs index 86de3ce21322..d4b12c62f002 100644 --- a/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Date.O.cs +++ b/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Date.O.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; #if !netstandard using Internal.Runtime.CompilerServices; @@ -52,7 +53,7 @@ private static bool TryFormatDateTimeO(DateTime value, TimeSpan offset, Span buffer, out in return false; } - ref byte utf8Bytes = ref buffer.DangerousGetPinnableReference(); + ref byte utf8Bytes = ref MemoryMarshal.GetReference(buffer); byte[] dayAbbrev = DayAbbreviations[(int)value.DayOfWeek]; Unsafe.Add(ref utf8Bytes, 0) = dayAbbrev[0]; diff --git a/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Guid.cs b/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Guid.cs index 97b9e1234c7a..9ef19033c519 100644 --- a/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Guid.cs +++ b/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Guid.cs @@ -8,6 +8,8 @@ using System.Runtime.CompilerServices; #endif +using System.Runtime.InteropServices; + namespace System.Buffers.Text { public static partial class Utf8Formatter @@ -86,7 +88,7 @@ public static unsafe bool TryFormat(Guid value, Span buffer, out int bytes return false; } - ref byte utf8Bytes = ref buffer.DangerousGetPinnableReference(); + ref byte utf8Bytes = ref MemoryMarshal.GetReference(buffer); byte* bytes = (byte*)&value; int idx = 0; diff --git a/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Signed.D.cs b/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Signed.D.cs index 392e60a0b398..1a26979f4bc7 100644 --- a/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Signed.D.cs +++ b/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Signed.D.cs @@ -8,6 +8,8 @@ using System.Runtime.CompilerServices; #endif +using System.Runtime.InteropServices; + namespace System.Buffers.Text { /// @@ -30,7 +32,7 @@ private static bool TryFormatInt64D(long value, byte precision, Span buffe return false; } - ref byte utf8Bytes = ref buffer.DangerousGetPinnableReference(); + ref byte utf8Bytes = ref MemoryMarshal.GetReference(buffer); int idx = 0; if (value < 0) diff --git a/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Signed.N.cs b/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Signed.N.cs index 1309e6926e15..ccc7043c0a77 100644 --- a/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Signed.N.cs +++ b/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Signed.N.cs @@ -10,6 +10,8 @@ using System.Runtime.CompilerServices; #endif +using System.Runtime.InteropServices; + namespace System.Buffers.Text { /// @@ -40,7 +42,7 @@ private static bool TryFormatInt64N(long value, byte precision, Span buffe return false; } - ref byte utf8Bytes = ref buffer.DangerousGetPinnableReference(); + ref byte utf8Bytes = ref MemoryMarshal.GetReference(buffer); long v = value; if (v < 0) diff --git a/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.D.cs b/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.D.cs index 8b598934b1a7..fe16bd8f30e5 100644 --- a/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.D.cs +++ b/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.D.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Runtime.InteropServices; + namespace System.Buffers.Text { /// @@ -32,7 +34,7 @@ private static bool TryFormatUInt64D(ulong value, byte precision, Span buf return false; } - ref byte utf8Bytes = ref buffer.DangerousGetPinnableReference(); + ref byte utf8Bytes = ref MemoryMarshal.GetReference(buffer); FormattingHelpers.WriteDigits(lastDigit, 1, ref utf8Bytes, bytesWritten); bytesWritten += 1; return true; diff --git a/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.N.cs b/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.N.cs index 290890098c78..9bce3fb70790 100644 --- a/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.N.cs +++ b/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.N.cs @@ -8,6 +8,8 @@ using System.Runtime.CompilerServices; #endif +using System.Runtime.InteropServices; + namespace System.Buffers.Text { /// @@ -46,7 +48,7 @@ private static bool TryFormatUInt64N(ulong value, byte precision, Span buf return false; } - ref byte utf8Bytes = ref buffer.DangerousGetPinnableReference(); + ref byte utf8Bytes = ref MemoryMarshal.GetReference(buffer); // Write the last group Unsafe.Add(ref utf8Bytes, idx++) = Utf8Constants.Separator; diff --git a/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.X.cs b/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.X.cs index 5f3cbb95c5c1..04b21657fa37 100644 --- a/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.X.cs +++ b/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Integer.Unsigned.X.cs @@ -8,6 +8,8 @@ using System.Runtime.CompilerServices; #endif +using System.Runtime.InteropServices; + namespace System.Buffers.Text { /// @@ -52,7 +54,7 @@ private static bool TryFormatUInt64X(ulong value, byte precision, bool useLower, } string hexTable = useLower ? HexTableLower : HexTableUpper; - ref byte utf8Bytes = ref buffer.DangerousGetPinnableReference(); + ref byte utf8Bytes = ref MemoryMarshal.GetReference(buffer); int idx = bytesWritten; for (v = value; digits-- > 0; v >>= 4) diff --git a/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.TimeSpan.cs b/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.TimeSpan.cs index 5c0e2adcfc02..d72677b2ed0f 100644 --- a/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.TimeSpan.cs +++ b/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.TimeSpan.cs @@ -8,6 +8,8 @@ using System.Runtime.CompilerServices; #endif +using System.Runtime.InteropServices; + namespace System.Buffers.Text { public static partial class Utf8Formatter @@ -86,7 +88,7 @@ public static bool TryFormat(TimeSpan value, Span buffer, out int bytesWri return false; } - ref byte utf8Bytes = ref buffer.DangerousGetPinnableReference(); + ref byte utf8Bytes = ref MemoryMarshal.GetReference(buffer); int idx = 0; if (showSign) diff --git a/src/System.Memory/tests/MemoryMarshal/GetReference.cs b/src/System.Memory/tests/MemoryMarshal/GetReference.cs index f0a58c7a8cf6..3993b353e089 100644 --- a/src/System.Memory/tests/MemoryMarshal/GetReference.cs +++ b/src/System.Memory/tests/MemoryMarshal/GetReference.cs @@ -4,9 +4,9 @@ using Xunit; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using static System.TestHelpers; -using System.Runtime.InteropServices; namespace System.SpanTests { diff --git a/src/System.Net.Http/src/System/Net/Http/ByteArrayHelpers.cs b/src/System.Net.Http/src/System/Net/Http/ByteArrayHelpers.cs index 0105cc7e18bc..8ae15c6ce61d 100644 --- a/src/System.Net.Http/src/System/Net/Http/ByteArrayHelpers.cs +++ b/src/System.Net.Http/src/System/Net/Http/ByteArrayHelpers.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Diagnostics; +using System.Runtime.InteropServices; using System.Text; namespace System @@ -44,7 +45,7 @@ internal static bool EqualsOrdinalAsciiIgnoreCase(string left, ReadOnlySpan bytes) { // TODO #22431: Use new Span-based Encoding overload when available - fixed (byte* p = &bytes.DangerousGetPinnableReference()) + fixed (byte* p = &MemoryMarshal.GetReference(bytes)) { return Encoding.ASCII.GetString(p, bytes.Length); } diff --git a/src/System.Net.Http/src/System/Net/Http/Headers/KnownHeaders.cs b/src/System.Net.Http/src/System/Net/Http/Headers/KnownHeaders.cs index 128da03b1a87..2d1d035bf00e 100644 --- a/src/System.Net.Http/src/System/Net/Http/Headers/KnownHeaders.cs +++ b/src/System.Net.Http/src/System/Net/Http/Headers/KnownHeaders.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Diagnostics; +using System.Runtime.InteropServices; namespace System.Net.Http.Headers { @@ -370,7 +371,7 @@ internal static KnownHeader TryGetKnownHeader(string name) internal unsafe static KnownHeader TryGetKnownHeader(ReadOnlySpan name) { - fixed (byte* p = &name.DangerousGetPinnableReference()) + fixed (byte* p = &MemoryMarshal.GetReference(name)) { KnownHeader candidate = GetCandidate(new BytePtrAccessor(p, name.Length)); if (candidate != null && ByteArrayHelpers.EqualsOrdinalAsciiIgnoreCase(candidate.Name, name)) diff --git a/src/System.Net.Http/src/System/Net/Http/Managed/HttpConnection.cs b/src/System.Net.Http/src/System/Net/Http/Managed/HttpConnection.cs index 8d3ad634dffe..1e54d7bf649a 100644 --- a/src/System.Net.Http/src/System/Net/Http/Managed/HttpConnection.cs +++ b/src/System.Net.Http/src/System/Net/Http/Managed/HttpConnection.cs @@ -9,6 +9,7 @@ using System.Net.Http.Headers; using System.Net.Security; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -547,7 +548,7 @@ private void ParseStatusLine(Span line, HttpResponseMessage response) { try { - fixed (byte* reasonPtr = &reasonBytes.DangerousGetPinnableReference()) + fixed (byte* reasonPtr = &MemoryMarshal.GetReference(reasonBytes)) { response.ReasonPhrase = Encoding.ASCII.GetString(reasonPtr, reasonBytes.Length); } diff --git a/src/System.Net.Http/src/System/Net/Http/ReadOnlyMemoryContent.cs b/src/System.Net.Http/src/System/Net/Http/ReadOnlyMemoryContent.cs index 2f42bd197320..0a9588d71ac5 100644 --- a/src/System.Net.Http/src/System/Net/Http/ReadOnlyMemoryContent.cs +++ b/src/System.Net.Http/src/System/Net/Http/ReadOnlyMemoryContent.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.IO; +using System.Runtime.InteropServices; using System.Threading.Tasks; namespace System.Net.Http @@ -14,7 +15,7 @@ public sealed class ReadOnlyMemoryContent : HttpContent public ReadOnlyMemoryContent(ReadOnlyMemory content) { _content = content; - if (content.DangerousTryGetArray(out ArraySegment array)) + if (MemoryMarshal.TryGetArray(content, out ArraySegment array)) { // If we have an array, allow HttpClient to take optimized paths by just // giving it the array content to use as its already buffered data. diff --git a/src/System.Net.NameResolution/src/System.Net.NameResolution.csproj b/src/System.Net.NameResolution/src/System.Net.NameResolution.csproj index 6b4b016d2683..bc0ed8ff90eb 100644 --- a/src/System.Net.NameResolution/src/System.Net.NameResolution.csproj +++ b/src/System.Net.NameResolution/src/System.Net.NameResolution.csproj @@ -180,6 +180,7 @@ + diff --git a/src/System.Net.Ping/src/System.Net.Ping.csproj b/src/System.Net.Ping/src/System.Net.Ping.csproj index b5b56273c127..61b55ca48d5d 100644 --- a/src/System.Net.Ping/src/System.Net.Ping.csproj +++ b/src/System.Net.Ping/src/System.Net.Ping.csproj @@ -144,6 +144,7 @@ + diff --git a/src/System.Net.Primitives/src/System/Net/IPAddressParser.cs b/src/System.Net.Primitives/src/System/Net/IPAddressParser.cs index ba02a6278db1..930914f9b2dc 100644 --- a/src/System.Net.Primitives/src/System/Net/IPAddressParser.cs +++ b/src/System.Net.Primitives/src/System/Net/IPAddressParser.cs @@ -5,6 +5,7 @@ using System.Diagnostics; using System.IO; using System.Net.Sockets; +using System.Runtime.InteropServices; using System.Text; namespace System.Net @@ -53,7 +54,7 @@ internal static unsafe bool IPv4AddressToString(uint address, Span formatt return false; } - fixed (char* formattedPtr = &formatted.DangerousGetPinnableReference()) + fixed (char* formattedPtr = &MemoryMarshal.GetReference(formatted)) { charsWritten = IPv4AddressToStringHelper(address, formattedPtr); } @@ -160,7 +161,7 @@ public static unsafe bool Ipv4StringToAddress(ReadOnlySpan ipSpan, out lon int end = ipSpan.Length; long tmpAddr; - fixed (char* ipStringPtr = &ipSpan.DangerousGetPinnableReference()) + fixed (char* ipStringPtr = &MemoryMarshal.GetReference(ipSpan)) { tmpAddr = IPv4AddressHelper.ParseNonCanonical(ipStringPtr, 0, ref end, notImplicitFile: true); } @@ -192,7 +193,7 @@ public static unsafe bool Ipv6StringToAddress(ReadOnlySpan ipSpan, ushort* int end = ipSpan.Length; bool isValid = false; - fixed (char* ipStringPtr = &ipSpan.DangerousGetPinnableReference()) + fixed (char* ipStringPtr = &MemoryMarshal.GetReference(ipSpan)) { isValid = IPv6AddressHelper.IsValidStrict(ipStringPtr, 0, ref end); } diff --git a/src/System.Net.Security/tests/UnitTests/SslApplicationProtocolTests.cs b/src/System.Net.Security/tests/UnitTests/SslApplicationProtocolTests.cs index 8aeeefbb199c..0706c73300ad 100644 --- a/src/System.Net.Security/tests/UnitTests/SslApplicationProtocolTests.cs +++ b/src/System.Net.Security/tests/UnitTests/SslApplicationProtocolTests.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Collections.Generic; +using System.Runtime.InteropServices; using System.Text; using Xunit; @@ -45,7 +46,7 @@ public void Constructor_ByteArray_Copies() SslApplicationProtocol byteProtocol = new SslApplicationProtocol(expected); ArraySegment arraySegment; - Assert.True(byteProtocol.Protocol.DangerousTryGetArray(out arraySegment)); + Assert.True(MemoryMarshal.TryGetArray(byteProtocol.Protocol, out arraySegment)); Assert.Equal(expected, arraySegment.Array); Assert.NotSame(expected, arraySegment.Array); } diff --git a/src/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs b/src/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs index b3976b2a774c..fc796379ec02 100644 --- a/src/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs +++ b/src/System.Net.Sockets/src/System/Net/Sockets/Socket.Tasks.cs @@ -389,7 +389,7 @@ internal ValueTask SendAsync(ReadOnlyMemory buffer, SocketFlags socke /// Implements Task-returning SendAsync on top of Begin/EndSend. private Task SendAsyncApm(ReadOnlyMemory buffer, SocketFlags socketFlags) { - if (buffer.DangerousTryGetArray(out ArraySegment bufferArray)) + if (MemoryMarshal.TryGetArray(buffer, out ArraySegment bufferArray)) { var tcs = new TaskCompletionSource(this); BeginSend(bufferArray.Array, bufferArray.Offset, bufferArray.Count, socketFlags, iar => diff --git a/src/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs b/src/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs index 195dc869b14d..deecfe35e20e 100644 --- a/src/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs +++ b/src/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncContext.Unix.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Threading; namespace System.Net.Sockets @@ -1302,7 +1303,7 @@ public unsafe SocketError ReceiveFrom(Span buffer, ref SocketFlags flags, return errorCode; } - fixed (byte* bufferPtr = &buffer.DangerousGetPinnableReference()) + fixed (byte* bufferPtr = &MemoryMarshal.GetReference(buffer)) { var operation = new BufferPtrReceiveOperation(this) { @@ -1564,7 +1565,7 @@ public unsafe SocketError SendTo(ReadOnlySpan buffer, SocketFlags flags, b return errorCode; } - fixed (byte* bufferPtr = &buffer.DangerousGetPinnableReference()) + fixed (byte* bufferPtr = &MemoryMarshal.GetReference(buffer)) { var operation = new BufferPtrSendOperation(this) { diff --git a/src/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.Windows.cs b/src/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.Windows.cs index 6622d2db7593..7bcabcfba40c 100644 --- a/src/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.Windows.cs +++ b/src/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.Windows.cs @@ -271,7 +271,7 @@ internal unsafe SocketError DoOperationReceiveSingleBuffer(SafeCloseSocket handl NativeOverlapped* overlapped = AllocateNativeOverlapped(); try { - fixed (byte* bufferPtr = &_buffer.Span.DangerousGetPinnableReference()) + fixed (byte* bufferPtr = &MemoryMarshal.GetReference(_buffer.Span)) { Debug.Assert(_singleBufferHandleState == SingleBufferHandleState.None, $"Expected None, got {_singleBufferHandleState}"); _singleBufferHandleState = SingleBufferHandleState.InProcess; @@ -350,7 +350,7 @@ internal unsafe SocketError DoOperationReceiveFromSingleBuffer(SafeCloseSocket h NativeOverlapped* overlapped = AllocateNativeOverlapped(); try { - fixed (byte* bufferPtr = &_buffer.Span.DangerousGetPinnableReference()) + fixed (byte* bufferPtr = &MemoryMarshal.GetReference(_buffer.Span)) { Debug.Assert(_singleBufferHandleState == SingleBufferHandleState.None); _singleBufferHandleState = SingleBufferHandleState.InProcess; @@ -558,7 +558,7 @@ internal unsafe SocketError DoOperationSendSingleBuffer(SafeCloseSocket handle) NativeOverlapped* overlapped = AllocateNativeOverlapped(); try { - fixed (byte* bufferPtr = &_buffer.Span.DangerousGetPinnableReference()) + fixed (byte* bufferPtr = &MemoryMarshal.GetReference(_buffer.Span)) { Debug.Assert(_singleBufferHandleState == SingleBufferHandleState.None); _singleBufferHandleState = SingleBufferHandleState.InProcess; @@ -739,7 +739,7 @@ internal unsafe SocketError DoOperationSendToSingleBuffer(SafeCloseSocket handle NativeOverlapped* overlapped = AllocateNativeOverlapped(); try { - fixed (byte* bufferPtr = &_buffer.Span.DangerousGetPinnableReference()) + fixed (byte* bufferPtr = &MemoryMarshal.GetReference(_buffer.Span)) { Debug.Assert(_singleBufferHandleState == SingleBufferHandleState.None); _singleBufferHandleState = SingleBufferHandleState.InProcess; diff --git a/src/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Unix.cs b/src/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Unix.cs index e3555325cdae..3367ac02594a 100644 --- a/src/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Unix.cs +++ b/src/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Unix.cs @@ -69,7 +69,7 @@ private static unsafe int Receive(SafeCloseSocket socket, SocketFlags flags, Spa int sockAddrLen = socketAddress != null ? socketAddressLen : 0; fixed (byte* sockAddr = socketAddress) - fixed (byte* b = &buffer.DangerousGetPinnableReference()) + fixed (byte* b = &MemoryMarshal.GetReference(buffer)) { var iov = new Interop.Sys.IOVector { Base = b, @@ -107,7 +107,7 @@ private static unsafe int Send(SafeCloseSocket socket, SocketFlags flags, ReadOn { int sent; fixed (byte* sockAddr = socketAddress) - fixed (byte* b = &buffer.DangerousGetPinnableReference()) + fixed (byte* b = &MemoryMarshal.GetReference(buffer)) { var iov = new Interop.Sys.IOVector { @@ -355,7 +355,7 @@ private static unsafe int ReceiveMessageFrom(SafeCloseSocket socket, SocketFlags long received; fixed (byte* rawSocketAddress = socketAddress) - fixed (byte* b = &buffer.DangerousGetPinnableReference()) + fixed (byte* b = &MemoryMarshal.GetReference(buffer)) { var iov = new Interop.Sys.IOVector { Base = b, diff --git a/src/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Windows.cs b/src/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Windows.cs index 61d118ea2394..226fabd2d1d8 100644 --- a/src/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Windows.cs +++ b/src/System.Net.Sockets/src/System/Net/Sockets/SocketPal.Windows.cs @@ -175,7 +175,7 @@ public static unsafe SocketError Send(SafeCloseSocket handle, byte[] buffer, int public static unsafe SocketError Send(SafeCloseSocket handle, ReadOnlySpan buffer, SocketFlags socketFlags, out int bytesTransferred) { int bytesSent; - fixed (byte* bufferPtr = &buffer.DangerousGetPinnableReference()) + fixed (byte* bufferPtr = &MemoryMarshal.GetReference(buffer)) { bytesSent = Interop.Winsock.send(handle.DangerousGetHandle(), bufferPtr, buffer.Length, socketFlags); } @@ -295,7 +295,7 @@ public static unsafe SocketError Receive(SafeCloseSocket handle, byte[] buffer, public static unsafe SocketError Receive(SafeCloseSocket handle, Span buffer, SocketFlags socketFlags, out int bytesTransferred) { int bytesReceived; - fixed (byte* bufferPtr = &buffer.DangerousGetPinnableReference()) + fixed (byte* bufferPtr = &MemoryMarshal.GetReference(buffer)) { bytesReceived = Interop.Winsock.recv(handle.DangerousGetHandle(), bufferPtr, buffer.Length, socketFlags); } diff --git a/src/System.Net.WebSockets.Client/src/System.Net.WebSockets.Client.csproj b/src/System.Net.WebSockets.Client/src/System.Net.WebSockets.Client.csproj index 2a3ce58b58cf..729b0071ce5f 100644 --- a/src/System.Net.WebSockets.Client/src/System.Net.WebSockets.Client.csproj +++ b/src/System.Net.WebSockets.Client/src/System.Net.WebSockets.Client.csproj @@ -123,6 +123,7 @@ + diff --git a/src/System.Net.WebSockets.Client/src/System/Net/WebSockets/WinHttpWebSocket.cs b/src/System.Net.WebSockets.Client/src/System/Net/WebSockets/WinHttpWebSocket.cs index 58157456456f..62052b29bb30 100644 --- a/src/System.Net.WebSockets.Client/src/System/Net/WebSockets/WinHttpWebSocket.cs +++ b/src/System.Net.WebSockets.Client/src/System/Net/WebSockets/WinHttpWebSocket.cs @@ -429,7 +429,7 @@ private Task InternalReceiveAsync(Memory pinnedBuffer) IntPtr pinnedBufferPtr; unsafe { - fixed (byte* p = &pinnedBuffer.Span.DangerousGetPinnableReference()) + fixed (byte* p = &MemoryMarshal.GetReference(pinnedBuffer.Span)) { pinnedBufferPtr = (IntPtr)p; } diff --git a/src/System.Net.WebSockets/src/System/Net/WebSockets/ManagedWebSocket.cs b/src/System.Net.WebSockets/src/System/Net/WebSockets/ManagedWebSocket.cs index 89857f1e5f73..410c43afc198 100644 --- a/src/System.Net.WebSockets/src/System/Net/WebSockets/ManagedWebSocket.cs +++ b/src/System.Net.WebSockets/src/System/Net/WebSockets/ManagedWebSocket.cs @@ -1265,7 +1265,7 @@ private static unsafe int ApplyMask(Span toMask, int mask, int maskIndex) int count = toMask.Length; if (count > 0) { - fixed (byte* toMaskPtr = &toMask.DangerousGetPinnableReference()) + fixed (byte* toMaskPtr = &MemoryMarshal.GetReference(toMask)) { byte* p = toMaskPtr; diff --git a/src/System.Net.WebSockets/src/System/Net/WebSockets/WebSocket.cs b/src/System.Net.WebSockets/src/System/Net/WebSockets/WebSocket.cs index 42b87b027460..8013388494bc 100644 --- a/src/System.Net.WebSockets/src/System/Net/WebSockets/WebSocket.cs +++ b/src/System.Net.WebSockets/src/System/Net/WebSockets/WebSocket.cs @@ -5,6 +5,7 @@ using System.Buffers; using System.ComponentModel; using System.IO; +using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; @@ -56,7 +57,7 @@ public virtual async ValueTask ReceiveAsync(Memory< } public virtual Task SendAsync(ReadOnlyMemory buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken) => - buffer.DangerousTryGetArray(out ArraySegment arraySegment) ? + MemoryMarshal.TryGetArray(buffer, out ArraySegment arraySegment) ? SendAsync(arraySegment, messageType, endOfMessage, cancellationToken) : SendWithArrayPoolAsync(buffer, messageType, endOfMessage, cancellationToken); diff --git a/src/System.Private.Xml/src/System.Private.Xml.csproj b/src/System.Private.Xml/src/System.Private.Xml.csproj index 0148daf3afc2..fa2aa252789c 100644 --- a/src/System.Private.Xml/src/System.Private.Xml.csproj +++ b/src/System.Private.Xml/src/System.Private.Xml.csproj @@ -522,6 +522,7 @@ + diff --git a/src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs b/src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs index cc161b4f1fa7..8f1515f42768 100644 --- a/src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs +++ b/src/System.Private.Xml/src/System/Xml/Core/XmlTextReaderImpl.cs @@ -9,6 +9,7 @@ using System.Globalization; using System.Collections.Generic; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Threading.Tasks; namespace System.Xml diff --git a/src/System.Runtime.Extensions/src/System/IO/StreamReader.cs b/src/System.Runtime.Extensions/src/System/IO/StreamReader.cs index 4229011162ec..74c9999cb1df 100644 --- a/src/System.Runtime.Extensions/src/System/IO/StreamReader.cs +++ b/src/System.Runtime.Extensions/src/System/IO/StreamReader.cs @@ -5,6 +5,7 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Diagnostics.Contracts; +using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; diff --git a/src/System.Runtime.Extensions/src/System/IO/StreamWriter.cs b/src/System.Runtime.Extensions/src/System/IO/StreamWriter.cs index e5fb9866148a..5826f917be2e 100644 --- a/src/System.Runtime.Extensions/src/System/IO/StreamWriter.cs +++ b/src/System.Runtime.Extensions/src/System/IO/StreamWriter.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Diagnostics; +using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -394,7 +395,7 @@ private unsafe void WriteCore(ReadOnlySpan buffer, bool autoFlush) throw new ObjectDisposedException(null, SR.ObjectDisposed_WriterClosed); } - fixed (char* bufferPtr = &buffer.DangerousGetPinnableReference()) + fixed (char* bufferPtr = &MemoryMarshal.GetReference(buffer)) fixed (char* dstPtr = &charBuffer[0]) { char* srcPtr = bufferPtr; diff --git a/src/System.Runtime.Extensions/src/System/IO/TextWriter.cs b/src/System.Runtime.Extensions/src/System/IO/TextWriter.cs index 4748c706f0e6..48e702be6744 100644 --- a/src/System.Runtime.Extensions/src/System/IO/TextWriter.cs +++ b/src/System.Runtime.Extensions/src/System/IO/TextWriter.cs @@ -7,6 +7,7 @@ using System.Globalization; using System.Threading.Tasks; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Buffers; namespace System.IO @@ -548,7 +549,7 @@ public virtual Task WriteAsync(char[] buffer, int index, int count) } public virtual Task WriteAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default(CancellationToken)) => - buffer.DangerousTryGetArray(out ArraySegment array) ? + MemoryMarshal.TryGetArray(buffer, out ArraySegment array) ? WriteAsync(array.Array, array.Offset, array.Count) : Task.Factory.StartNew(state => { @@ -600,7 +601,7 @@ public virtual Task WriteLineAsync(char[] buffer, int index, int count) } public virtual Task WriteLineAsync(ReadOnlyMemory buffer, CancellationToken cancellationToken = default(CancellationToken)) => - buffer.DangerousTryGetArray(out ArraySegment array) ? + MemoryMarshal.TryGetArray(buffer, out ArraySegment array) ? WriteLineAsync(array.Array, array.Offset, array.Count) : Task.Factory.StartNew(state => { diff --git a/src/System.Runtime/tests/System/StringTests.netcoreapp.cs b/src/System.Runtime/tests/System/StringTests.netcoreapp.cs index b29b8cf13790..8f14fe13e670 100644 --- a/src/System.Runtime/tests/System/StringTests.netcoreapp.cs +++ b/src/System.Runtime/tests/System/StringTests.netcoreapp.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; +using System.Runtime.InteropServices; using Xunit; namespace System.Tests @@ -565,7 +566,7 @@ public static unsafe void ImplicitCast_ResultingSpanMatches(string s) ReadOnlySpan span = s; Assert.Equal(s.Length, span.Length); fixed (char* stringPtr = s) - fixed (char* spanPtr = &span.DangerousGetPinnableReference()) + fixed (char* spanPtr = &MemoryMarshal.GetReference(span)) { Assert.Equal((IntPtr)stringPtr, (IntPtr)spanPtr); } diff --git a/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/HashProviderDispenser.Unix.cs b/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/HashProviderDispenser.Unix.cs index 01f5b4da0af0..999167d6d178 100644 --- a/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/HashProviderDispenser.Unix.cs +++ b/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/HashProviderDispenser.Unix.cs @@ -95,7 +95,7 @@ public override bool TryFinalizeHashAndReset(Span destination, out int byt } uint length = (uint)destination.Length; - Check(Interop.Crypto.EvpDigestFinalEx(_ctx, ref destination.DangerousGetPinnableReference(), ref length)); + Check(Interop.Crypto.EvpDigestFinalEx(_ctx, ref MemoryMarshal.GetReference(destination), ref length)); Debug.Assert(length == _hashSize); bytesWritten = (int)length; @@ -132,7 +132,7 @@ public HmacHashProvider(IntPtr algorithmEvp, byte[] key) throw new CryptographicException(); } - _hmacCtx = Interop.Crypto.HmacCreate(ref new Span(key).DangerousGetPinnableReference(), key.Length, algorithmEvp); + _hmacCtx = Interop.Crypto.HmacCreate(ref MemoryMarshal.GetReference(new Span(key)), key.Length, algorithmEvp); Interop.Crypto.CheckValidOpenSslHandle(_hmacCtx); } @@ -157,7 +157,7 @@ public override unsafe bool TryFinalizeHashAndReset(Span destination, out } int length = destination.Length; - Check(Interop.Crypto.HmacFinal(_hmacCtx, ref destination.DangerousGetPinnableReference(), ref length)); + Check(Interop.Crypto.HmacFinal(_hmacCtx, ref MemoryMarshal.GetReference(destination), ref length)); Debug.Assert(length == _hashSize); bytesWritten = length; diff --git a/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.cs b/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.cs index 57e0be7b794b..5d528f158f12 100644 --- a/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.cs +++ b/src/System.Security.Cryptography.Algorithms/src/Internal/Cryptography/RandomNumberGeneratorImplementation.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System.Runtime.InteropServices; + namespace System.Security.Cryptography { internal sealed partial class RandomNumberGeneratorImplementation : RandomNumberGenerator @@ -22,7 +24,7 @@ public override void GetBytes(Span data) { if (data.Length > 0) { - GetBytes(ref data.DangerousGetPinnableReference(), data.Length); + GetBytes(ref MemoryMarshal.GetReference(data), data.Length); } } diff --git a/src/System.Security.Cryptography.Algorithms/src/System.Security.Cryptography.Algorithms.csproj b/src/System.Security.Cryptography.Algorithms/src/System.Security.Cryptography.Algorithms.csproj index 2b1d29be342d..5c3405383857 100644 --- a/src/System.Security.Cryptography.Algorithms/src/System.Security.Cryptography.Algorithms.csproj +++ b/src/System.Security.Cryptography.Algorithms/src/System.Security.Cryptography.Algorithms.csproj @@ -481,6 +481,7 @@ + diff --git a/src/System.Security.Cryptography.Cng/src/System.Security.Cryptography.Cng.csproj b/src/System.Security.Cryptography.Cng/src/System.Security.Cryptography.Cng.csproj index 00dbe6f98a8a..203784556094 100644 --- a/src/System.Security.Cryptography.Cng/src/System.Security.Cryptography.Cng.csproj +++ b/src/System.Security.Cryptography.Cng/src/System.Security.Cryptography.Cng.csproj @@ -280,6 +280,7 @@ + diff --git a/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/ComprehensiveReadTests.cs b/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/ComprehensiveReadTests.cs index 768a56e94808..0d80672dad5f 100644 --- a/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/ComprehensiveReadTests.cs +++ b/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/ComprehensiveReadTests.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Security.Cryptography.Asn1; using Test.Cryptography; using Xunit; @@ -190,7 +191,7 @@ private static void AssertRdn( Assert.Equal(valueTag, actualTag); AssertRefSame( - ref valueSpan.DangerousGetPinnableReference(), + ref MemoryMarshal.GetReference(valueSpan), ref bytes[offset], $"{label} is at bytes[{offset}]"); @@ -207,7 +208,7 @@ ref valueSpan.DangerousGetPinnableReference(), private static void AssertRefSame(ReadOnlyMemory a, ref byte b, string msg) { - AssertRefSame(ref a.Span.DangerousGetPinnableReference(), ref b, msg); + AssertRefSame(ref MemoryMarshal.GetReference(a.Span), ref b, msg); } private static void AssertRefSame(ref byte a, ref byte b, string msg) diff --git a/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/PeekTests.cs b/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/PeekTests.cs index c5814b41c83d..f09e92095e89 100644 --- a/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/PeekTests.cs +++ b/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/PeekTests.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Security.Cryptography.Asn1; using Test.Cryptography; using Xunit; @@ -172,7 +173,7 @@ public static void PeekContentSpan_ExtremelyNested(bool fullArray) AsnReader reader = new AsnReader(dataBytes, AsnEncodingRules.BER); ReadOnlyMemory contents = reader.PeekContentBytes(); Assert.Equal(expectedLength, contents.Length); - Assert.True(Unsafe.AreSame(ref dataBytes[2], ref contents.Span.DangerousGetPinnableReference())); + Assert.True(Unsafe.AreSame(ref dataBytes[2], ref MemoryMarshal.GetReference(contents.Span))); } [Theory] @@ -207,7 +208,7 @@ public static void PeekEncodedValue_ExtremelyNested(bool fullArray) AsnReader reader = new AsnReader(dataBytes, AsnEncodingRules.BER); ReadOnlyMemory contents = reader.PeekEncodedValue(); Assert.Equal(expectedLength, contents.Length); - Assert.True(Unsafe.AreSame(ref dataBytes[0], ref contents.Span.DangerousGetPinnableReference())); + Assert.True(Unsafe.AreSame(ref dataBytes[0], ref MemoryMarshal.GetReference(contents.Span))); } } } diff --git a/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/ReadBMPString.cs b/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/ReadBMPString.cs index c8e8c42cea51..62b8439e0ae2 100644 --- a/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/ReadBMPString.cs +++ b/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/ReadBMPString.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Security.Cryptography.Asn1; using Test.Cryptography; using Xunit; @@ -229,7 +230,7 @@ public static void TryGetBMPStringBytes( Assert.True( Unsafe.AreSame( - ref contents.Span.DangerousGetPinnableReference(), + ref MemoryMarshal.GetReference(contents.Span), ref inputData[2])); } else diff --git a/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/ReadBitString.cs b/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/ReadBitString.cs index 2455c2efcb14..7d4bae41d326 100644 --- a/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/ReadBitString.cs +++ b/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/ReadBitString.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Security.Cryptography.Asn1; using Test.Cryptography; using Xunit; @@ -149,7 +150,7 @@ public static void TryGetBitStringBytes_Success_CER_MaxLength() // Check that it is, in fact, the same memory. No copies with this API. Assert.True( Unsafe.AreSame( - ref contents.Span.DangerousGetPinnableReference(), + ref MemoryMarshal.GetReference(contents.Span), ref input[5])); } diff --git a/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/ReadIA5String.cs b/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/ReadIA5String.cs index 83194935133f..42dc62a77ea7 100644 --- a/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/ReadIA5String.cs +++ b/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/ReadIA5String.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Security.Cryptography.Asn1; using Test.Cryptography; using Xunit; @@ -223,7 +224,7 @@ public static void TryGetIA5StringBytes( Assert.True( Unsafe.AreSame( - ref contents.Span.DangerousGetPinnableReference(), + ref MemoryMarshal.GetReference(contents.Span), ref inputData[2])); } else diff --git a/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/ReadOctetString.cs b/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/ReadOctetString.cs index 4167418b4310..2283f4f95a69 100644 --- a/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/ReadOctetString.cs +++ b/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/ReadOctetString.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Security.Cryptography.Asn1; using Test.Cryptography; using Xunit; @@ -124,7 +125,7 @@ public static void TryGetOctetStringBytes_Success_CER_MaxLength() // Check that it is, in fact, the same memory. No copies with this API. Assert.True( Unsafe.AreSame( - ref contents.Span.DangerousGetPinnableReference(), + ref MemoryMarshal.GetReference(contents.Span), ref input[4])); } diff --git a/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/ReadUTF8String.cs b/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/ReadUTF8String.cs index 8397bfc65419..5ab1b6dc518d 100644 --- a/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/ReadUTF8String.cs +++ b/src/System.Security.Cryptography.Encoding/tests/Asn1/Reader/ReadUTF8String.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Security.Cryptography.Asn1; using Test.Cryptography; using Xunit; @@ -237,7 +238,7 @@ public static void TryGetUTF8StringBytes( Assert.True( Unsafe.AreSame( - ref contents.Span.DangerousGetPinnableReference(), + ref MemoryMarshal.GetReference(contents.Span), ref inputData[2])); } else diff --git a/src/System.Security.Cryptography.Encoding/tests/Asn1/Serializer/SimpleDeserialize.cs b/src/System.Security.Cryptography.Encoding/tests/Asn1/Serializer/SimpleDeserialize.cs index a99e323abc11..6ca6a210366b 100644 --- a/src/System.Security.Cryptography.Encoding/tests/Asn1/Serializer/SimpleDeserialize.cs +++ b/src/System.Security.Cryptography.Encoding/tests/Asn1/Serializer/SimpleDeserialize.cs @@ -403,7 +403,7 @@ public static void ReadAnyValueWithExpectedTag() Assert.Equal("0.0", data.Id); Assert.Equal(5, data.Data.Length); - Assert.True(Unsafe.AreSame(ref data.Data.Span.DangerousGetPinnableReference(), ref inputData[5])); + Assert.True(Unsafe.AreSame(ref MemoryMarshal.GetReference(data.Data.Span), ref inputData[5])); // Change [Constructed] SEQUENCE to [Constructed] Context-Specific 0. inputData[5] = 0xA0; diff --git a/src/System.Security.Cryptography.OpenSsl/src/System.Security.Cryptography.OpenSsl.csproj b/src/System.Security.Cryptography.OpenSsl/src/System.Security.Cryptography.OpenSsl.csproj index a653c9eff82e..89245e9cb1c3 100644 --- a/src/System.Security.Cryptography.OpenSsl/src/System.Security.Cryptography.OpenSsl.csproj +++ b/src/System.Security.Cryptography.OpenSsl/src/System.Security.Cryptography.OpenSsl.csproj @@ -124,6 +124,7 @@ + diff --git a/src/System.Security.Cryptography.X509Certificates/src/System.Security.Cryptography.X509Certificates.csproj b/src/System.Security.Cryptography.X509Certificates/src/System.Security.Cryptography.X509Certificates.csproj index c235728e73c0..a63a66ff53ed 100644 --- a/src/System.Security.Cryptography.X509Certificates/src/System.Security.Cryptography.X509Certificates.csproj +++ b/src/System.Security.Cryptography.X509Certificates/src/System.Security.Cryptography.X509Certificates.csproj @@ -412,6 +412,7 @@ +