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

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

问题描述

除非应用 [ReliabilityContract(Consistency.WillNotCorruptState,Cer.Success)] ,否则任何人都可以创建短样本



我刚刚浏览过 sample on MSDN ,我无法得到它断裂,即使我注释掉ReliabilityContract属性。终于似乎总是被调用。

解决方案

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

类程序{
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];
}

//结果取决于这个属性的存在
[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();
}
}
}

当MyFn




  • 在没有ReliabilityContract的情况下,不能形成正确的ConstrainedRegion,因此,发出常规代码。


  • 在使用ReliabilityContract的情况下,可能会形成一个ConstrainedRegion并且finally块中的方法的堆栈需求可以提升到MyFn。



现在在调用MyFn时会抛出堆栈溢出异常

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天全站免登陆