aboutsummaryrefslogtreecommitdiff
path: root/ChocolArm64/Memory/AMemory.cs
diff options
context:
space:
mode:
authorgdkchan <gab.dark.100@gmail.com>2018-06-09 13:05:41 -0300
committergdkchan <gab.dark.100@gmail.com>2018-06-09 13:05:41 -0300
commit7f5a8effbb0bbf7e972dbbfb6792cdea48455739 (patch)
tree1e498d073ccbf7692ad57aeee191d2280d8656fb /ChocolArm64/Memory/AMemory.cs
parent91420a402c9e786ddef2811bb671003dd0a40565 (diff)
Move WriteBytes to AMemory, implement it with a Marshal copy like ReadBytes, fix regression on address range checking
Diffstat (limited to 'ChocolArm64/Memory/AMemory.cs')
-rw-r--r--ChocolArm64/Memory/AMemory.cs59
1 files changed, 34 insertions, 25 deletions
diff --git a/ChocolArm64/Memory/AMemory.cs b/ChocolArm64/Memory/AMemory.cs
index 2adf5403..fe250d4b 100644
--- a/ChocolArm64/Memory/AMemory.cs
+++ b/ChocolArm64/Memory/AMemory.cs
@@ -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,23 +666,19 @@ namespace ChocolArm64.Memory
}
}
- private void EnsureAccessIsValid(long Position, AMemoryPerm Perm)
+ public void WriteBytes(long Position, byte[] Data)
{
- if (!Manager.IsMapped(Position))
- {
- throw new VmmPageFaultException(Position);
- }
+ EnsureRangeIsValid(Position, (uint)Data.Length, AMemoryPerm.Write);
- if (!Manager.HasPermission(Position, Perm))
- {
- throw new VmmAccessViolationException(Position, Perm);
- }
+ 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);
@@ -691,6 +687,19 @@ namespace ChocolArm64.Memory
}
}
+ private void EnsureAccessIsValid(long Position, AMemoryPerm Perm)
+ {
+ if (!Manager.IsMapped(Position))
+ {
+ throw new VmmPageFaultException(Position);
+ }
+
+ if (!Manager.HasPermission(Position, Perm))
+ {
+ throw new VmmAccessViolationException(Position, Perm);
+ }
+ }
+
public void Dispose()
{
Dispose(true);