代码演示一个约束的执行区域的重要性 [英] Code demonstrating the importance of a Constrained Execution Region

查看:274
本文介绍了代码演示一个约束的执行区域的重要性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以创建一个短采样打破,除非 [ReliabilityContract(Consistency.WillNotCorruptState,Cer.Success)] 应用?



我只是通过MSDN这个样品跑,但无法得到它打破,即使我注释掉ReliabilityContract属性。最后似乎总被调用。


解决方案

 使用系统;使用System.Runtime.CompilerServices 
;
使用System.Runtime.ConstrainedExecution;

类节目{
静态布尔cerWorked;

静态无效的主要(字串[] args){
尝试{
cerWorked = TRUE;
MyFn();
}
赶上(OutOfMemoryException异常){
Console.WriteLine(cerWorked);
}
到Console.ReadLine();
}

不安全的结构大{
公共固定字节字节[int.MaxValue]
}

//结果取决于该属性
[ReliabilityContract(Consistency.WillNotCorruptState,Cer.Success)]
不安全的静态无效的计算器(){是否存在等
大的大;
big.Bytes [int.MaxValue - 1] = 1;
}

静态无效MyFn(){
RuntimeHelpers.PrepareConstrainedRegions();
尝试{
cerWorked = FALSE;
}
终于{
计算器();
}
}
}

当MyFn被即时编译,它尝试从finally块创建ConstrainedRegion。




  • 在没有ReliabilityContract的情况下,没有适当的ConstrainedRegion可以形成,所以一个常规代码被发射。堆栈溢出异常被抛出在调用#1(执行try块后)。


  • 在与ReliabilityContract的情况下,可以形成一个ConstrainedRegion并在finally块方法栈的要求可以提升到MyFn。堆栈溢出异常,现在扔在调用MyFn(曾经被执行try块之前)。



Could anyone create a short sample that breaks, unless the [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] is applied?

I just ran through this sample on MSDN and am unable to get it to break, even if I comment out the ReliabilityContract attribute. Finally seems to always get called.

解决方案

using System;
using System.Runtime.CompilerServices;
using System.Runtime.ConstrainedExecution;

class Program {
    static bool cerWorked;

    static void Main( string[] args ) {
        try {
            cerWorked = true;
            MyFn();
        }
        catch( OutOfMemoryException ) {
            Console.WriteLine( cerWorked );
        }
        Console.ReadLine();
    }

    unsafe struct Big {
        public fixed byte Bytes[int.MaxValue];
    }

    //results depends on the existance of this attribute
    [ReliabilityContract( Consistency.WillNotCorruptState, Cer.Success )] 
    unsafe static void StackOverflow() {
        Big big;
        big.Bytes[ int.MaxValue - 1 ] = 1;
    }

    static void MyFn() {
        RuntimeHelpers.PrepareConstrainedRegions();
        try {
            cerWorked = false;
        }
        finally {
            StackOverflow();
        }
    }
}

When MyFn is jitted, it tries to create a ConstrainedRegion from the finally block.

  • In the case without the ReliabilityContract, no proper ConstrainedRegion could be formed, so a regular code is emitted. The stack overflow exception is thrown on the call to Stackoverflow (after the try block is executed).

  • In the case with the ReliabilityContract, a ConstrainedRegion could be formed and the stack requirements of methods in the finally block could be lifted into MyFn. The stack overflow exception is now thrown on the call to MyFn (before the try block is ever executed).

这篇关于代码演示一个约束的执行区域的重要性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆