From 2cb8541462e3cc31e9a4ffe63f430168c0c747d1 Mon Sep 17 00:00:00 2001 From: gdkchan Date: Sat, 20 Oct 2018 19:07:52 -0300 Subject: [PATCH] Print stack trace on invalid memory accesses (#461) * Print stack trace on invalid memory accesses * Rebased, change code region base address for 39-bits address space, print stack trace on break and undefined instructions too --- ChocolArm64/Events/AInvalidAccessEventArgs.cs | 14 +++++++++++ ChocolArm64/Memory/AMemory.cs | 7 ++++++ Ryujinx.HLE/HOS/Kernel/KMemoryManager.cs | 2 +- Ryujinx.HLE/HOS/Process.cs | 24 +++++++++++++++++++ .../Aud/AudioRenderer/VoiceContext.cs | 7 ++++++ 5 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 ChocolArm64/Events/AInvalidAccessEventArgs.cs diff --git a/ChocolArm64/Events/AInvalidAccessEventArgs.cs b/ChocolArm64/Events/AInvalidAccessEventArgs.cs new file mode 100644 index 000000000..a5c472a88 --- /dev/null +++ b/ChocolArm64/Events/AInvalidAccessEventArgs.cs @@ -0,0 +1,14 @@ +using System; + +namespace ChocolArm64.Events +{ + public class AInvalidAccessEventArgs : EventArgs + { + public long Position { get; private set; } + + public AInvalidAccessEventArgs(long Position) + { + this.Position = Position; + } + } +} \ No newline at end of file diff --git a/ChocolArm64/Memory/AMemory.cs b/ChocolArm64/Memory/AMemory.cs index bb6a2b549..2854871e3 100644 --- a/ChocolArm64/Memory/AMemory.cs +++ b/ChocolArm64/Memory/AMemory.cs @@ -1,3 +1,4 @@ +using ChocolArm64.Events; using ChocolArm64.Exceptions; using ChocolArm64.State; using System; @@ -51,6 +52,8 @@ namespace ChocolArm64.Memory private byte*** PageTable; + public event EventHandler InvalidAccess; + public AMemory(IntPtr Ram) { Monitors = new Dictionary(); @@ -512,6 +515,8 @@ Unmapped: return (byte*)Ptr + (Position & PageMask); } + InvalidAccess?.Invoke(this, new AInvalidAccessEventArgs(Position)); + throw new VmmPageFaultException(Position); } @@ -560,6 +565,8 @@ Unmapped: return (byte*)Ptr + (Position & PageMask); } + InvalidAccess?.Invoke(this, new AInvalidAccessEventArgs(Position)); + throw new VmmPageFaultException(Position); } diff --git a/Ryujinx.HLE/HOS/Kernel/KMemoryManager.cs b/Ryujinx.HLE/HOS/Kernel/KMemoryManager.cs index 8871cbe7d..8cf73785e 100644 --- a/Ryujinx.HLE/HOS/Kernel/KMemoryManager.cs +++ b/Ryujinx.HLE/HOS/Kernel/KMemoryManager.cs @@ -91,7 +91,7 @@ namespace Ryujinx.HLE.HOS.Kernel break; case AddressSpaceType.Addr39Bits: - CodeRegionStart = 0; + CodeRegionStart = 0x8000000; CodeRegionSize = 0x80000000; MapRegionSize = 0x1000000000; HeapRegionSize = 0x180000000; diff --git a/Ryujinx.HLE/HOS/Process.cs b/Ryujinx.HLE/HOS/Process.cs index 448f1afac..1d8bfeb8f 100644 --- a/Ryujinx.HLE/HOS/Process.cs +++ b/Ryujinx.HLE/HOS/Process.cs @@ -67,6 +67,8 @@ namespace Ryujinx.HLE.HOS Memory = new AMemory(Device.Memory.RamPointer); + Memory.InvalidAccess += CpuInvalidAccessHandler; + MemoryManager = new KMemoryManager(this); TlsPages = new List(); @@ -283,11 +285,15 @@ namespace Ryujinx.HLE.HOS private void BreakHandler(object sender, AInstExceptionEventArgs e) { + PrintStackTraceForCurrentThread(); + throw new GuestBrokeExecutionException(); } private void UndefinedHandler(object sender, AInstUndefinedEventArgs e) { + PrintStackTraceForCurrentThread(); + throw new UndefinedInstructionException(e.Position, e.RawOpCode); } @@ -334,6 +340,24 @@ namespace Ryujinx.HLE.HOS return Translator; } + private void CpuInvalidAccessHandler(object sender, AInvalidAccessEventArgs e) + { + PrintStackTraceForCurrentThread(); + } + + private void PrintStackTraceForCurrentThread() + { + foreach (KThread Thread in Threads.Values) + { + if (Thread.Context.IsCurrentThread()) + { + PrintStackTrace(Thread.Context.ThreadState); + + break; + } + } + } + public void PrintStackTrace(AThreadState ThreadState) { StringBuilder Trace = new StringBuilder(); diff --git a/Ryujinx.HLE/HOS/Services/Aud/AudioRenderer/VoiceContext.cs b/Ryujinx.HLE/HOS/Services/Aud/AudioRenderer/VoiceContext.cs index bee574ff8..044b2efc2 100644 --- a/Ryujinx.HLE/HOS/Services/Aud/AudioRenderer/VoiceContext.cs +++ b/Ryujinx.HLE/HOS/Services/Aud/AudioRenderer/VoiceContext.cs @@ -125,6 +125,13 @@ namespace Ryujinx.HLE.HOS.Services.Aud.AudioRenderer //As of now, it assumes that HostChannelsCount == 2. WaveBuffer Wb = WaveBuffers[BufferIndex]; + if (Wb.Position == 0) + { + Samples = new int[0]; + + return; + } + if (SampleFormat == SampleFormat.PcmInt16) { int SamplesCount = (int)(Wb.Size / (sizeof(short) * ChannelsCount));