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)); 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) public Vector128<float> ReadVector8Unchecked(long Position)
{ {
if (Sse2.IsSupported) 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() public void Dispose()
{ {
Dispose(true); 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) public static void WriteBytes(AMemory Memory, long Position, byte[] Data)
{ {
for (int Offs = 0; Offs < Data.Length; Offs++) for (int Offs = 0; Offs < Data.Length; Offs++)

View file

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

View file

@ -233,7 +233,7 @@ namespace Ryujinx.Core.OsHle.Kernel
{ {
KThread CurrThread = Process.GetThread(ThreadState.Tpidr); 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); KSession Session = Process.HandleTable.GetData<KSession>(Handle);

View file

@ -49,7 +49,7 @@ namespace Ryujinx.Core.OsHle.Services.Am
Size = MaxSize; 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); 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 Position = Context.Request.SendBuff[0].Position;
long Size = Context.Request.SendBuff[0].Size; 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); string DeviceName = Encoding.ASCII.GetString(DeviceNameBuffer);
@ -160,7 +160,7 @@ namespace Ryujinx.Core.OsHle.Services.Aud
(long Position, long Size) = Context.Request.GetBufferType0x21(); (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); string DeviceName = Encoding.UTF8.GetString(DeviceNameBuffer);

View file

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

View file

@ -62,7 +62,7 @@ namespace Ryujinx.Core.OsHle.Services.FspSrv
long Offset = Context.RequestData.ReadInt64(); long Offset = Context.RequestData.ReadInt64();
long Size = 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.Seek(Offset, SeekOrigin.Begin);
BaseStream.Write(Data, 0, (int)Size); BaseStream.Write(Data, 0, (int)Size);

View file

@ -23,8 +23,7 @@ namespace Ryujinx.Core.OsHle.Services.Lm
public long Log(ServiceCtx Context) public long Log(ServiceCtx Context)
{ {
byte[] LogBuffer = AMemoryHelper.ReadBytes( byte[] LogBuffer = Context.Memory.ReadBytes(
Context.Memory,
Context.Request.PtrBuff[0].Position, Context.Request.PtrBuff[0].Position,
Context.Request.PtrBuff[0].Size); 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 ReplyPos = Context.Request.ReceiveBuff[0].Position;
long ReplySize = Context.Request.ReceiveBuff[0].Size; long ReplySize = Context.Request.ReceiveBuff[0].Size;
byte[] Class = AMemoryHelper.ReadBytes(Context.Memory, ClassPos, ClassSize); byte[] Class = Context.Memory.ReadBytes(ClassPos, ClassSize);
byte[] Name = AMemoryHelper.ReadBytes(Context.Memory, NamePos, NameSize); byte[] Name = Context.Memory.ReadBytes(NamePos, NameSize);
string AskedSetting = Encoding.ASCII.GetString(Class).Trim('\0') + "!" + Encoding.ASCII.GetString(Name).Trim('\0'); 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 DataPos = Context.Request.SendBuff[0].Position;
long DataSize = Context.Request.SendBuff[0].Size; 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); Data = Parcel.GetParcelData(Data);
@ -55,7 +55,7 @@ namespace Ryujinx.Core.OsHle.Services.Vi
(long DataPos, long DataSize) = Context.Request.GetBufferType0x21(); (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); Data = Parcel.GetParcelData(Data);