From d9ed827696700ef5b9b777031bab451f23fb837c Mon Sep 17 00:00:00 2001
From: gdkchan <gab.dark.100@gmail.com>
Date: Wed, 4 Mar 2020 16:25:27 -0300
Subject: [PATCH] Don't decode blocks starting outside mapped memory &
 undefined instead of throw on invalid sysreg coprocessor (#964)

* Don't decode blocks in invalid memory locations.

* Emit undefined instruction on invalid coprocessor

...rather than throwing.

* Call undefined instruction directly.
---
 ARMeilleure/Decoders/Decoder.cs              | 2 +-
 ARMeilleure/Instructions/InstEmitSystem32.cs | 9 ++++++---
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/ARMeilleure/Decoders/Decoder.cs b/ARMeilleure/Decoders/Decoder.cs
index 913d5082b6..7cbb62e6c5 100644
--- a/ARMeilleure/Decoders/Decoder.cs
+++ b/ARMeilleure/Decoders/Decoder.cs
@@ -47,7 +47,7 @@ namespace ARMeilleure.Decoders
             {
                 if (!visited.TryGetValue(blkAddress, out Block block))
                 {
-                    if (opsCount > MaxInstsPerFunction)
+                    if (opsCount > MaxInstsPerFunction || !memory.IsMapped((long)blkAddress))
                     {
                         return null;
                     }
diff --git a/ARMeilleure/Instructions/InstEmitSystem32.cs b/ARMeilleure/Instructions/InstEmitSystem32.cs
index 808b4fdd7a..ebf829a041 100644
--- a/ARMeilleure/Instructions/InstEmitSystem32.cs
+++ b/ARMeilleure/Instructions/InstEmitSystem32.cs
@@ -17,7 +17,8 @@ namespace ARMeilleure.Instructions
 
             if (op.Coproc != 15)
             {
-                throw new NotImplementedException($"Unknown MRC Coprocessor ID 0x{op.Coproc:X16} at 0x{op.Address:X16}.");
+                InstEmit.Und(context);
+                return;
             }
 
             if (op.Opc1 != 0)
@@ -70,7 +71,8 @@ namespace ARMeilleure.Instructions
 
             if (op.Coproc != 15)
             {
-                throw new NotImplementedException($"Unknown MRC Coprocessor ID 0x{op.Coproc:X16} at 0x{op.Address:X16}.");
+                InstEmit.Und(context);
+                return;
             }
 
             if (op.Opc1 != 0)
@@ -119,7 +121,8 @@ namespace ARMeilleure.Instructions
 
             if (op.Coproc != 15)
             {
-                throw new NotImplementedException($"Unknown MRC Coprocessor ID 0x{op.Coproc:X16} at 0x{op.Address:X16}.");
+                InstEmit.Und(context);
+                return;
             }
 
             var opc = op.MrrcOp;