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
This commit is contained in:
Mary 2021-11-01 23:38:13 +01:00 committed by GitHub
parent e48530e9d9
commit f41687f4c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 175 additions and 126 deletions

View file

@ -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<uint> dataConvert = MemoryMarshal.Cast<byte, uint>(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<byte> dataSpan = new Span<byte>((void*)surfaceData.Scan0, dataLength);
destination.Write(position, dataSpan);
destination.Write(position, data);
}
finally
{