Skip to content
Permalink
Browse files

Merge pull request #75 from quajak/master

Implement index out of range checks
  • Loading branch information...
charlesbetros committed Jun 24, 2019
2 parents 878393d + 2f4987b commit b322475afb9bc3c201657d6d5f5416b73742d1eb
@@ -20,6 +20,8 @@ public static void ThrowArgumentOutOfRange(string aError)

public static void ThrowDivideByZeroException() => throw new DivideByZeroException();

public static void ThrowIndexOutOfRangeException() => throw new IndexOutOfRangeException();

public static void ThrowInvalidOperation(string aError)
{
Console.WriteLine(aError);
@@ -60,5 +62,8 @@ public static class ExceptionHelperRefs

public static readonly MethodInfo ThrowNotFiniteNumberExceptionRef =
typeof(ExceptionHelper).GetMethod(nameof(ExceptionHelper.ThrowNotFiniteNumberException));

public static readonly MethodInfo ThrowIndexOutOfRangeException =
typeof(ExceptionHelper).GetMethod(nameof(ExceptionHelper.ThrowIndexOutOfRangeException));
}
}
@@ -27,6 +27,28 @@ public static void Assemble(Assembler aAssembler, uint aElementSize, bool isSign
// stack + 2 = array
DoNullReferenceCheck(aAssembler, debugEnabled, 8);

var xBaseLabel = GetLabel(aMethod, aOpCode);
var xNoIndexOutOfRangeExeptionLabel = xBaseLabel + "_NoIndexOutOfRangeException";
var xIndexOutOfRangeExeptionLabel = xBaseLabel + "_IndexOutOfRangeException";
XS.Pop(EBX); //get Position _, array, 0, index -> _, array, 0
XS.Push(ESP, true, 4); // _, array, 0 => _, array, 0, array
XS.Push(ESP, true, 12); // _, array, 0, array => _, array, 0, array, 0
Ldlen.Assemble(aAssembler, debugEnabled, false); // _, array, 0, array, 0 -> _, array, 0, length
XS.Pop(EAX); //Length of array _, array, 0, length -> _, array, 0
XS.Compare(EAX, EBX);
XS.Jump(CPUx86.ConditionalTestEnum.LessThanOrEqualTo, xIndexOutOfRangeExeptionLabel);

XS.Compare(EBX, 0);
XS.Jump(CPUx86.ConditionalTestEnum.GreaterThanOrEqualTo, xNoIndexOutOfRangeExeptionLabel);

XS.Label(xIndexOutOfRangeExeptionLabel);
XS.Pop(EAX);
XS.Pop(EAX);
Call.DoExecute(aAssembler, aMethod, ExceptionHelperRefs.ThrowIndexOutOfRangeException, aOpCode, xNoIndexOutOfRangeExeptionLabel, debugEnabled);

XS.Label(xNoIndexOutOfRangeExeptionLabel);
XS.Push(EBX); //_, array, 0 -> _, array, 0, index

// calculate element offset into array memory (including header)
XS.Pop(EAX);
XS.Set(EDX, aElementSize);
@@ -1,25 +1,34 @@
using Cosmos.IL2CPU.ILOpCodes;
using XSharp;
using XSharp.Assembler;
using static XSharp.XSRegisters;

namespace Cosmos.IL2CPU.X86.IL
{
[OpCode(ILOpCode.Code.Ldlen)]
public class Ldlen : ILOp
{
public Ldlen(Assembler aAsmblr)
: base(aAsmblr)
{
}
[OpCode(ILOpCode.Code.Ldlen)]
public class Ldlen : ILOp
{
public Ldlen(Assembler aAsmblr)
: base(aAsmblr)
{
}

public override void Execute(_MethodInfo aMethod, ILOpCode aOpCode)
{
DoNullReferenceCheck(Assembler, DebugEnabled, 4);
public override void Execute(_MethodInfo aMethod, ILOpCode aOpCode)
{
Assemble(Assembler, DebugEnabled);
}

XS.Add(ESP, 4);
XS.Pop(EAX);
public static void Assemble(Assembler aAssembler, bool debugEnabled, bool doNullReferenceCheck = true)
{
if (doNullReferenceCheck)
{
DoNullReferenceCheck(aAssembler, debugEnabled, 4);
}

XS.Push(EAX, displacement: 8);
}
}
XS.Add(ESP, 4);
XS.Pop(EAX);

XS.Push(EAX, displacement: 8);
}
}
}
@@ -254,6 +254,7 @@ public void Execute(MethodBase aStartMethod, IEnumerable<Assembly> plugsAssembli
Queue(ExceptionHelperRefs.ThrowInvalidCastExceptionRef, null, "Explicit Entry");
Queue(ExceptionHelperRefs.ThrowNotFiniteNumberExceptionRef, null, "Explicit Entry");
Queue(ExceptionHelperRefs.ThrowDivideByZeroExceptionRef, null, "Explicit Entry");
Queue(ExceptionHelperRefs.ThrowIndexOutOfRangeException, null, "Explicit Entry");

mAsmblr.ProcessField(typeof(string).GetField("Empty", BindingFlags.Static | BindingFlags.Public));

0 comments on commit b322475

Please sign in to comment.
You can’t perform that action at this time.