From f41687f4c1948e9e111afd70e979e98ea5de52fa Mon Sep 17 00:00:00 2001 From: Mary Date: Mon, 1 Nov 2021 23:38:13 +0100 Subject: hle: Make Ryujinx.HLE project entirely safe (#2789) * Remove a bit of unsafety around * Regenerate StructArrayHelpers with a max element value of 256 * hle: remove unsafe marker from all struct that had it * hle: make SoftwareKeyboardRenderer.TryCopyTo safe * hle: remove unsafety in NpadDevice and remove AllowUnsafeBlocks from csproj * Revert "Regenerate StructArrayHelpers with a max element value of 256" This reverts commit f32a6e5be094f50571970eb1116b65c080781d05. * Introduce ByteArray of various size and use that instead of ArrayXXX to avoid stackoverflow in .NET runtime type resolution * Use ByteArray more * Add some missing spaces on Pack = 1 for various structs * Fix broken logic for TryCopyTo * Address gdkchan's comment * Address gdkchan's comment --- .../SoftwareKeyboard/SoftwareKeyboardRenderer.cs | 24 +++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardRenderer.cs') diff --git a/Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardRenderer.cs b/Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardRenderer.cs index c16b861e..dfd10925 100644 --- a/Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardRenderer.cs +++ b/Ryujinx.HLE/HOS/Applets/SoftwareKeyboard/SoftwareKeyboardRenderer.cs @@ -1,12 +1,14 @@ using Ryujinx.HLE.Ui; using Ryujinx.Memory; using System; +using System.Buffers.Binary; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Drawing.Text; using System.IO; +using System.Numerics; using System.Reflection; using System.Runtime.InteropServices; using System.Threading; @@ -652,7 +654,7 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard DrawString(graphics, ControllerToggleText, _labelsTextFont, _textNormalBrush, labelPosition); } - private unsafe bool TryCopyTo(IVirtualMemoryManager destination, ulong position) + private bool TryCopyTo(IVirtualMemoryManager destination, ulong position) { if (_surface == null) { @@ -666,23 +668,21 @@ namespace Ryujinx.HLE.HOS.Applets.SoftwareKeyboard Debug.Assert(surfaceData.Stride * surfaceData.Height == _surfaceInfo.Size); // Convert the pixel format used in System.Drawing to the one required by a Switch Surface. - int dataLength = surfaceData.Stride * surfaceData.Height; - byte* dataPointer = (byte*)surfaceData.Scan0; - byte* dataEnd = dataPointer + dataLength; + int dataLength = surfaceData.Stride * surfaceData.Height; - for (; dataPointer < dataEnd; dataPointer += 4) + byte[] data = new byte[dataLength]; + Span dataConvert = MemoryMarshal.Cast(data); + + Marshal.Copy(surfaceData.Scan0, data, 0, dataLength); + + for (int i = 0; i < dataConvert.Length; i++) { - *(uint*)dataPointer = (uint)( - (*(dataPointer + 0) << 16) | - (*(dataPointer + 1) << 8 ) | - (*(dataPointer + 2) << 0 ) | - (*(dataPointer + 3) << 24)); + dataConvert[i] = BitOperations.RotateRight(BinaryPrimitives.ReverseEndianness(dataConvert[i]), 8); } try { - Span dataSpan = new Span((void*)surfaceData.Scan0, dataLength); - destination.Write(position, dataSpan); + destination.Write(position, data); } finally { -- cgit v1.2.3