From 702daf2ff473acafc56a7f872a9c74db73e19a58 Mon Sep 17 00:00:00 2001
From: gdkchan <gab.dark.100@gmail.com>
Date: Fri, 6 Apr 2018 15:39:39 -0300
Subject: [PATCH] [CPU] Fail early when the index/size of the vector is invalid

---
 .../Instruction/AInstEmitSimdHelper.cs        | 52 +++++++++++++------
 1 file changed, 36 insertions(+), 16 deletions(-)

diff --git a/ChocolArm64/Instruction/AInstEmitSimdHelper.cs b/ChocolArm64/Instruction/AInstEmitSimdHelper.cs
index b66419bd41..264919ab97 100644
--- a/ChocolArm64/Instruction/AInstEmitSimdHelper.cs
+++ b/ChocolArm64/Instruction/AInstEmitSimdHelper.cs
@@ -562,10 +562,7 @@ namespace ChocolArm64.Instruction
 
         public static void EmitVectorExtract(AILEmitterCtx Context, int Reg, int Index, int Size, bool Signed)
         {
-            if (Size < 0 || Size > 3)
-            {
-                throw new ArgumentOutOfRangeException(nameof(Size));
-            }
+            ThrowIfInvalid(Index, Size);
 
             IAOpCodeSimd Op = (IAOpCodeSimd)Context.CurrOp;
 
@@ -580,6 +577,8 @@ namespace ChocolArm64.Instruction
 
         public static void EmitVectorExtractF(AILEmitterCtx Context, int Reg, int Index, int Size)
         {
+            ThrowIfInvalidF(Index, Size);
+
             Context.EmitLdvec(Reg);
             Context.EmitLdc_I4(Index);
 
@@ -615,10 +614,7 @@ namespace ChocolArm64.Instruction
 
         public static void EmitVectorInsert(AILEmitterCtx Context, int Reg, int Index, int Size)
         {
-            if (Size < 0 || Size > 3)
-            {
-                throw new ArgumentOutOfRangeException(nameof(Size));
-            }
+            ThrowIfInvalid(Index, Size);
 
             Context.EmitLdvec(Reg);
             Context.EmitLdc_I4(Index);
@@ -631,10 +627,7 @@ namespace ChocolArm64.Instruction
 
         public static void EmitVectorInsertTmp(AILEmitterCtx Context, int Index, int Size)
         {
-            if (Size < 0 || Size > 3)
-            {
-                throw new ArgumentOutOfRangeException(nameof(Size));
-            }
+            ThrowIfInvalid(Index, Size);
 
             Context.EmitLdvectmp();
             Context.EmitLdc_I4(Index);
@@ -647,10 +640,7 @@ namespace ChocolArm64.Instruction
 
         public static void EmitVectorInsert(AILEmitterCtx Context, int Reg, int Index, int Size, long Value)
         {
-            if (Size < 0 || Size > 3)
-            {
-                throw new ArgumentOutOfRangeException(nameof(Size));
-            }
+            ThrowIfInvalid(Index, Size);
 
             Context.EmitLdc_I8(Value);
             Context.EmitLdvec(Reg);
@@ -664,6 +654,8 @@ namespace ChocolArm64.Instruction
 
         public static void EmitVectorInsertF(AILEmitterCtx Context, int Reg, int Index, int Size)
         {
+            ThrowIfInvalidF(Index, Size);
+
             Context.EmitLdvec(Reg);
             Context.EmitLdc_I4(Index);
 
@@ -685,6 +677,8 @@ namespace ChocolArm64.Instruction
 
         public static void EmitVectorInsertTmpF(AILEmitterCtx Context, int Index, int Size)
         {
+            ThrowIfInvalidF(Index, Size);
+
             Context.EmitLdvectmp();
             Context.EmitLdc_I4(Index);
 
@@ -703,5 +697,31 @@ namespace ChocolArm64.Instruction
 
             Context.EmitStvectmp();
         }
+
+        private static void ThrowIfInvalid(int Index, int Size)
+        {
+            if ((uint)Size > 3)
+            {
+                throw new ArgumentOutOfRangeException(nameof(Size));
+            }
+
+            if ((uint)Index >= 16 >> Size)
+            {
+                throw new ArgumentOutOfRangeException(nameof(Index));
+            }
+        }
+
+        private static void ThrowIfInvalidF(int Index, int Size)
+        {
+            if ((uint)Size > 1)
+            {
+                throw new ArgumentOutOfRangeException(nameof(Size));
+            }
+
+            if ((uint)Index >= 4 >> Size)
+            {
+                throw new ArgumentOutOfRangeException(nameof(Index));
+            }
+        }
     }
 }
\ No newline at end of file