Move WriteBytes to AMemory, implement it with a Marshal copy like ReadBytes, fix regression on address range checking

This commit is contained in:
gdkchan 2018-06-09 13:05:41 -03:00
parent 0e8fd39636
commit 231539a9e8
2 changed files with 37 additions and 36 deletions

View file

@ -353,22 +353,6 @@ namespace ChocolArm64.Memory
return *((ulong*)(RamPtr + (uint)Position));
}
public byte[] ReadBytes(long Position, long Size)
{
if ((uint)Size > int.MaxValue)
{
throw new ArgumentOutOfRangeException(nameof(Size));
}
EnsureRangeIsValid(Position, Size, AMemoryPerm.Read);
byte[] Data = new byte[Size];
Marshal.Copy((IntPtr)(RamPtr + (uint)Position), Data, 0, (int)Size);
return Data;
}
public Vector128<float> ReadVector8Unchecked(long Position)
{
if (Sse2.IsSupported)
@ -433,6 +417,22 @@ namespace ChocolArm64.Memory
}
}
public byte[] ReadBytes(long Position, long Size)
{
if ((uint)Size > int.MaxValue)
{
throw new ArgumentOutOfRangeException(nameof(Size));
}
EnsureRangeIsValid(Position, Size, AMemoryPerm.Read);
byte[] Data = new byte[Size];
Marshal.Copy((IntPtr)(RamPtr + (uint)Position), Data, 0, (int)Size);
return Data;
}
public void WriteSByte(long Position, sbyte Value)
{
WriteByte(Position, (byte)Value);
@ -666,6 +666,27 @@ namespace ChocolArm64.Memory
}
}
public void WriteBytes(long Position, byte[] Data)
{
EnsureRangeIsValid(Position, (uint)Data.Length, AMemoryPerm.Write);
Marshal.Copy(Data, 0, (IntPtr)(RamPtr + (uint)Position), Data.Length);
}
private void EnsureRangeIsValid(long Position, long Size, AMemoryPerm Perm)
{
long EndPos = Position + Size;
Position &= ~AMemoryMgr.PageMask;
while ((ulong)Position < (ulong)EndPos)
{
EnsureAccessIsValid(Position, Perm);
Position += AMemoryMgr.PageSize;
}
}
private void EnsureAccessIsValid(long Position, AMemoryPerm Perm)
{
if (!Manager.IsMapped(Position))
@ -679,18 +700,6 @@ namespace ChocolArm64.Memory
}
}
private void EnsureRangeIsValid(long Position, long Size, AMemoryPerm Perm)
{
long EndPos = Position + Size;
while ((ulong)Position < (ulong)EndPos)
{
EnsureAccessIsValid(Position, Perm);
Position += AMemoryMgr.PageSize;
}
}
public void Dispose()
{
Dispose(true);

View file

@ -22,14 +22,6 @@ namespace ChocolArm64.Memory
}
}
public static void WriteBytes(AMemory Memory, long Position, byte[] Data)
{
for (int Offs = 0; Offs < Data.Length; Offs++)
{
Memory.WriteByte(Position + Offs, Data[Offs]);
}
}
public unsafe static T Read<T>(AMemory Memory, long Position) where T : struct
{
long Size = Marshal.SizeOf<T>();