ReadBytes function in AMemory, with cleaner range check. (#136)

This commit is contained in:
riperiperi 2018-06-09 01:15:02 +01:00 committed by gdkchan
parent 39ebb83453
commit 6fe51f9705
12 changed files with 46 additions and 47 deletions

View File

@ -301,6 +301,15 @@ namespace ChocolArm64.Memory
return *((ulong*)(RamPtr + (uint)Position));
}
public byte[] ReadBytes(long Position, long Size)
{
EnsureRangeIsValid(Position, Size, AMemoryPerm.Read);
byte[] Result = new byte[Size];
Marshal.Copy((IntPtr)(RamPtr + (uint)Position), Result, 0, (int)Size);
return Result;
}
public Vector128<float> ReadVector8Unchecked(long Position)
{
if (Sse2.IsSupported)
@ -611,6 +620,17 @@ namespace ChocolArm64.Memory
}
}
private void EnsureRangeIsValid(long Position, long Size, AMemoryPerm Perm)
{
long EndPos = (Position + Size);
Position = Position & ~AMemoryMgr.PageMask; //check base of each page
while (Position < EndPos)
{
EnsureAccessIsValid(Position, Perm);
Position += AMemoryMgr.PageSize;
}
}
public void Dispose()
{
Dispose(true);

View File

@ -22,18 +22,6 @@ namespace ChocolArm64.Memory
}
}
public static byte[] ReadBytes(AMemory Memory, long Position, long Size)
{
byte[] Data = new byte[Size];
for (long Offs = 0; Offs < Size; Offs++)
{
Data[Offs] = (byte)Memory.ReadByte(Position + Offs);
}
return Data;
}
public static void WriteBytes(AMemory Memory, long Position, byte[] Data)
{
for (int Offs = 0; Offs < Data.Length; Offs++)

View File

@ -330,7 +330,7 @@ namespace Ryujinx.Core.Gpu
{
Position = GetPhysicalAddress(Position);
return AMemoryHelper.ReadBytes(Memory, Position, Size);
return Memory.ReadBytes(Position, Size);
}
public void WriteByte(long Position, byte Value)

View File

@ -233,7 +233,7 @@ namespace Ryujinx.Core.OsHle.Kernel
{
KThread CurrThread = Process.GetThread(ThreadState.Tpidr);
byte[] CmdData = AMemoryHelper.ReadBytes(Memory, CmdPtr, Size);
byte[] CmdData = Memory.ReadBytes(CmdPtr, Size);
KSession Session = Process.HandleTable.GetData<KSession>(Handle);

View File

@ -49,7 +49,7 @@ namespace Ryujinx.Core.OsHle.Services.Am
Size = MaxSize;
}
byte[] Data = AMemoryHelper.ReadBytes(Context.Memory, Position, Size);
byte[] Data = Context.Memory.ReadBytes(Position, Size);
Buffer.BlockCopy(Data, 0, Storage.Data, (int)WritePosition, (int)Size);
}

View File

@ -75,7 +75,7 @@ namespace Ryujinx.Core.OsHle.Services.Aud
long Position = Context.Request.SendBuff[0].Position;
long Size = Context.Request.SendBuff[0].Size;
byte[] DeviceNameBuffer = AMemoryHelper.ReadBytes(Context.Memory, Position, Size);
byte[] DeviceNameBuffer = Context.Memory.ReadBytes(Position, Size);
string DeviceName = Encoding.ASCII.GetString(DeviceNameBuffer);
@ -160,7 +160,7 @@ namespace Ryujinx.Core.OsHle.Services.Aud
(long Position, long Size) = Context.Request.GetBufferType0x21();
byte[] DeviceNameBuffer = AMemoryHelper.ReadBytes(Context.Memory, Position, Size);
byte[] DeviceNameBuffer = Context.Memory.ReadBytes(Position, Size);
string DeviceName = Encoding.UTF8.GetString(DeviceNameBuffer);

View File

@ -69,8 +69,7 @@ namespace Ryujinx.Core.OsHle.Services.Aud
Context.Memory,
Context.Request.SendBuff[0].Position);
byte[] Buffer = AMemoryHelper.ReadBytes(
Context.Memory,
byte[] Buffer = Context.Memory.ReadBytes(
Data.SampleBufferPtr,
Data.SampleBufferSize);

View File

@ -102,8 +102,7 @@ namespace Ryujinx.Core.OsHle.Services.Bsd
//https://github.com/TuxSH/ftpd/blob/switch_pr/source/ftp.c#L1634
//https://linux.die.net/man/2/poll
byte[] SentBuffer = AMemoryHelper.ReadBytes(Context.Memory,
Context.Request.SendBuff[0].Position,
byte[] SentBuffer = Context.Memory.ReadBytes(Context.Request.SendBuff[0].Position,
Context.Request.SendBuff[0].Size);
int SocketId = Get32(SentBuffer, 0);
@ -152,8 +151,7 @@ namespace Ryujinx.Core.OsHle.Services.Bsd
int SocketId = Context.RequestData.ReadInt32();
int SocketFlags = Context.RequestData.ReadInt32();
byte[] SentBuffer = AMemoryHelper.ReadBytes(Context.Memory,
Context.Request.SendBuff[0].Position,
byte[] SentBuffer = Context.Memory.ReadBytes(Context.Request.SendBuff[0].Position,
Context.Request.SendBuff[0].Size);
try
@ -180,12 +178,10 @@ namespace Ryujinx.Core.OsHle.Services.Bsd
int SocketId = Context.RequestData.ReadInt32();
int SocketFlags = Context.RequestData.ReadInt32();
byte[] SentBuffer = AMemoryHelper.ReadBytes(Context.Memory,
Context.Request.SendBuff[0].Position,
byte[] SentBuffer = Context.Memory.ReadBytes(Context.Request.SendBuff[0].Position,
Context.Request.SendBuff[0].Size);
byte[] AddressBuffer = AMemoryHelper.ReadBytes(Context.Memory,
Context.Request.SendBuff[1].Position,
byte[] AddressBuffer = Context.Memory.ReadBytes(Context.Request.SendBuff[1].Position,
Context.Request.SendBuff[1].Size);
if (!Sockets[SocketId].Handle.Connected)
@ -291,8 +287,7 @@ namespace Ryujinx.Core.OsHle.Services.Bsd
{
int SocketId = Context.RequestData.ReadInt32();
byte[] AddressBuffer = AMemoryHelper.ReadBytes(Context.Memory,
Context.Request.SendBuff[0].Position,
byte[] AddressBuffer = Context.Memory.ReadBytes(Context.Request.SendBuff[0].Position,
Context.Request.SendBuff[0].Size);
try
@ -316,8 +311,7 @@ namespace Ryujinx.Core.OsHle.Services.Bsd
{
int SocketId = Context.RequestData.ReadInt32();
byte[] AddressBuffer = AMemoryHelper.ReadBytes(Context.Memory,
Context.Request.SendBuff[0].Position,
byte[] AddressBuffer = Context.Memory.ReadBytes(Context.Request.SendBuff[0].Position,
Context.Request.SendBuff[0].Size);
try
@ -369,8 +363,7 @@ namespace Ryujinx.Core.OsHle.Services.Bsd
SocketOptionLevel SocketLevel = (SocketOptionLevel)Context.RequestData.ReadInt32();
SocketOptionName SocketOptionName = (SocketOptionName)Context.RequestData.ReadInt32();
byte[] SocketOptionValue = AMemoryHelper.ReadBytes(Context.Memory,
Context.Request.PtrBuff[0].Position,
byte[] SocketOptionValue = Context.Memory.ReadBytes(Context.Request.PtrBuff[0].Position,
Context.Request.PtrBuff[0].Size);
int OptionValue = Get32(SocketOptionValue, 0);

View File

@ -62,7 +62,7 @@ namespace Ryujinx.Core.OsHle.Services.FspSrv
long Offset = Context.RequestData.ReadInt64();
long Size = Context.RequestData.ReadInt64();
byte[] Data = AMemoryHelper.ReadBytes(Context.Memory, Position, Size);
byte[] Data = Context.Memory.ReadBytes(Position, Size);
BaseStream.Seek(Offset, SeekOrigin.Begin);
BaseStream.Write(Data, 0, (int)Size);

View File

@ -23,8 +23,7 @@ namespace Ryujinx.Core.OsHle.Services.Lm
public long Log(ServiceCtx Context)
{
byte[] LogBuffer = AMemoryHelper.ReadBytes(
Context.Memory,
byte[] LogBuffer = Context.Memory.ReadBytes(
Context.Request.PtrBuff[0].Position,
Context.Request.PtrBuff[0].Size);

View File

@ -97,8 +97,8 @@ namespace Ryujinx.Core.OsHle.Services.Set
long ReplyPos = Context.Request.ReceiveBuff[0].Position;
long ReplySize = Context.Request.ReceiveBuff[0].Size;
byte[] Class = AMemoryHelper.ReadBytes(Context.Memory, ClassPos, ClassSize);
byte[] Name = AMemoryHelper.ReadBytes(Context.Memory, NamePos, NameSize);
byte[] Class = Context.Memory.ReadBytes(ClassPos, ClassSize);
byte[] Name = Context.Memory.ReadBytes(NamePos, NameSize);
string AskedSetting = Encoding.ASCII.GetString(Class).Trim('\0') + "!" + Encoding.ASCII.GetString(Name).Trim('\0');

View File

@ -41,7 +41,7 @@ namespace Ryujinx.Core.OsHle.Services.Vi
long DataPos = Context.Request.SendBuff[0].Position;
long DataSize = Context.Request.SendBuff[0].Size;
byte[] Data = AMemoryHelper.ReadBytes(Context.Memory, DataPos, DataSize);
byte[] Data = Context.Memory.ReadBytes(DataPos, DataSize);
Data = Parcel.GetParcelData(Data);
@ -55,7 +55,7 @@ namespace Ryujinx.Core.OsHle.Services.Vi
(long DataPos, long DataSize) = Context.Request.GetBufferType0x21();
byte[] Data = AMemoryHelper.ReadBytes(Context.Memory, DataPos, DataSize);
byte[] Data = Context.Memory.ReadBytes(DataPos, DataSize);
Data = Parcel.GetParcelData(Data);