边界检查逻辑是否出现在MSIL或机器代码中

时间:2016-04-14 15:47:45

标签: .net jit cil

我刚刚开始涉足MSIL的精彩世界,但我似乎无法在这里找到任何关于边界检查发生的答案。 C#编译器是否插入执行边界检查的MSIL指令,或者MSIL JIT编译器在将这些指令转换为机器代码时是否为所有适用的MSIL指令插入它们?

我问的原因是因为我需要知道在直接使用MSIL生成函数时是否需要添加这些指令。

编辑:收到一个答案并尝试验证后,似乎不正确。以下代码实际上失败了。单步执行调试器会发现第二个测试超出了数组范围,第三个测试更糟糕:

static class ArrayExtensions
{
#if false
    public static void ClearRangeReferenceImplementation(byte[] buffer, int offset, int byteCount)
    {
        for (int current = offset; current < offset + byteCount; ++current)
        {
            buffer[current] = 0;
        }
    }
#endif
    private static readonly Action<IntPtr, int, int> MemclearRaw = InitMemclearRaw();
    private static Action<IntPtr, int, int> InitMemclearRaw()
    {
        DynamicMethod memclearMethod = new DynamicMethod("Memclear", MethodAttributes.Public | MethodAttributes.Static, CallingConventions.Standard, null, new Type[] { typeof(IntPtr), typeof(int), typeof(int) }, typeof(this), true);
        ILGenerator il = memclearMethod.GetILGenerator();
        il.Emit(OpCodes.Ldarg_0);
        il.Emit(OpCodes.Ldarg_1);
        il.Emit(OpCodes.Add);
        il.Emit(OpCodes.Ldc_I4_0);
        il.Emit(OpCodes.Ldarg_2);
        il.Emit(OpCodes.Initblk);
        il.Emit(OpCodes.Ret);
        return (Action<IntPtr, int, int>)memclearMethod.CreateDelegate(typeof(Action<IntPtr, int, int>));
    }
    /// <summary>
    /// Clears the specified range of the specified buffer in the most optimal manner available without resorting to PInvoke or inline assembly.
    /// </summary>
    /// <param name="buffer">The buffer to acted upon.</param>
    /// <param name="offset">The offset in the buffer where the clearing is to start.</param>
    /// <param name="count">The number of bytes to be cleared.</param>
    public static void ClearRange(this byte[] buffer, int offset, int count)
    {
        if (count == 0) return;
        GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
        try
        {
            MemclearRaw(handle.AddrOfPinnedObject(), offset, count);
        }
        finally
        {
            handle.Free();
        }
    }
}
[TestClass]
public class TestArrayExtensions
{
    /// <summary>
    /// Performs failure tests on <see cref="ArrayExtensions.ClearRange"/>.
    /// </summary>
    [TestMethod]
    public void Array_ClearRangeExceptions()
    {
        byte[] b = Rand.NewBytes(0, 100);
        AssertExceptionClearRange(null, 0, b.Length, typeof(NullReferenceException));
        b = Rand.NewBytes(0, 100);
        AssertExceptionClearRange(b, 0, b.Length + 1, typeof(ArgumentOutOfRangeException));
        b = Rand.NewBytes(0, 100);
        AssertExceptionClearRange(b, 0, -1, typeof(ArgumentOutOfRangeException));
        b = Rand.NewBytes(0, 100);
        AssertExceptionClearRange(b, -1, b.Length, typeof(ArgumentOutOfRangeException));
    }

    private static void AssertExceptionClearRange(byte[] buffer, int offset, int count, Type exceptionType)
    {
        try
        {
            ArrayExtensions.ClearRange(buffer, offset, count);
            Assert.Fail("ArrayExtensions.ClearRange did not throw the expected exception!");
        }
        catch (Exception ex)
        {
            Assert.AreEqual(exceptionType, ex.GetType());
        }
    }

似乎MSIL只进行空指针检查,而不是边界检查。

我目前正在使用VS2012并针对.NET Framework 4.5.1,如果这有任何区别的话。

1 个答案:

答案 0 :(得分:2)

C#编译器的优化很少。

所有的魔法都发生在生成机器代码的JIT中。

要回答这个问题,如果你生成与C#或VB.NET相似的代码(如果JIT可以确定这是某种形式的for循环等),那么它将起作用。