<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>The world is a better place if you generate verifiable IL</title><link>http://blogs.msdn.com/davidnotario/archive/2005/02/04/367556.aspx</link><description>If you are writing a compiler that targets IL or just emitting IL, you may find this an interesting read: The JIT compiler will always try to generate code, even if the IL is bad. From the JIT’s point of view, IL code falls in 3 categories: 1) Verifiable</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: The world is a better place if you generate verifiable IL</title><link>http://blogs.msdn.com/davidnotario/archive/2005/02/04/367556.aspx#367592</link><pubDate>Sat, 05 Feb 2005 07:37:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:367592</guid><dc:creator>Sam</dc:creator><description>Good stuff,&lt;br&gt;&lt;br&gt;Do you have any links to information on *how* to produce verifiable IL when using reflection emit?&lt;br&gt;I would like to learn more...</description></item><item><title>re: The world is a better place if you generate verifiable IL</title><link>http://blogs.msdn.com/davidnotario/archive/2005/02/04/367556.aspx#370045</link><pubDate>Thu, 10 Feb 2005 00:49:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:370045</guid><dc:creator>David Notario</dc:creator><description>Sam, if your question is how to verify that the IL you generate is verifiable, 2 ways of doing it:&lt;br&gt;&lt;br&gt;1) Save the assembly you emit to disk, run peverify on it. This will make sure that all methods in your assembly are verifiable&lt;br&gt;&lt;br&gt;2) Refuse SkipVerification permission. You can add the following attribute to your assembly:&lt;br&gt;&lt;br&gt;[assembly:PermissionSetAttribute(SecurityAction::RequestRefuse,Name=&amp;quot;SkipVerification&amp;quot;)];&lt;br&gt;&lt;br&gt;The downside of this aproach is that verification will be done at runtime (ie, it verifies as you go, so maybe you won't cover all your code if not all is used).&lt;br&gt;&lt;br&gt;Producing verifiable code is about following the IL verification rules as described in ECMA spec, they're mostly type safety rules, so I don't think you will find much stuff there that is suprising.&lt;br&gt;&lt;br&gt;Let me know if you have any other questions.&lt;br&gt;&lt;br&gt;&lt;br&gt;</description></item><item><title>re: The world is a better place if you generate verifiable IL</title><link>http://blogs.msdn.com/davidnotario/archive/2005/02/04/367556.aspx#372204</link><pubDate>Mon, 14 Feb 2005 13:41:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:372204</guid><dc:creator>Jeroen Frijters</dc:creator><description>Hi,&lt;br&gt;&lt;br&gt;I try to always generate verifiable code, but in some cases that isn't as easy as you might think. For example, is the following code verifiable? Whidbey's PEVerify doesn't think so.&lt;br&gt;&lt;br&gt;.assembly extern mscorlib {}&lt;br&gt;.assembly test {}&lt;br&gt;.module test.exe&lt;br&gt;&lt;br&gt;.class private auto ansi beforefieldinit Test&lt;br&gt;       extends [mscorlib]System.Object&lt;br&gt;{&lt;br&gt;  .method private hidebysig specialname rtspecialname &lt;br&gt;          instance void  .ctor() cil managed&lt;br&gt;  {&lt;br&gt;    .maxstack  1&lt;br&gt;    .try&lt;br&gt;    {&lt;br&gt;      ldstr      &amp;quot;try&amp;quot;&lt;br&gt;      call       void [mscorlib]System.Console::WriteLine(string)&lt;br&gt;      leave.s    label&lt;br&gt;    }&lt;br&gt;    finally&lt;br&gt;    {&lt;br&gt;      ldstr      &amp;quot;finally&amp;quot;&lt;br&gt;      call       void [mscorlib]System.Console::WriteLine(string)&lt;br&gt;      endfinally&lt;br&gt;    }&lt;br&gt;label:&lt;br&gt;    ldarg.0&lt;br&gt;    call       instance void [mscorlib]System.Object::.ctor()&lt;br&gt;    ret&lt;br&gt;  }&lt;br&gt;&lt;br&gt;  .method private hidebysig static void  Main() cil managed&lt;br&gt;  {&lt;br&gt;    .entrypoint&lt;br&gt;    .maxstack  1&lt;br&gt;    newobj     instance void Test::.ctor()&lt;br&gt;    pop&lt;br&gt;    ret&lt;br&gt;  }&lt;br&gt;}&lt;br&gt;</description></item><item><title>re: The world is a better place if you generate verifiable IL</title><link>http://blogs.msdn.com/davidnotario/archive/2005/02/04/367556.aspx#374061</link><pubDate>Wed, 16 Feb 2005 08:42:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:374061</guid><dc:creator>David Notario</dc:creator><description>Jeroen, I believe the verifier wants the .ctor of Test to call its base constructor before doing anything else (to guarantee the state of the base class). If you just call Object::.ctor as the first thing in your .ctor you'll become verifiable. &lt;br&gt;&lt;br&gt;I'll confirm this with verifier guys.</description></item><item><title>re: The world is a better place if you generate verifiable IL</title><link>http://blogs.msdn.com/davidnotario/archive/2005/02/04/367556.aspx#374062</link><pubDate>Wed, 16 Feb 2005 08:43:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:374062</guid><dc:creator>David Notario</dc:creator><description>Jeroen, I believe the verifier wants the .ctor of Test to call its base constructor before doing anything else (to guarantee the state of the base class). If you just call Object::.ctor as the first thing in your .ctor you'll become verifiable. &lt;br&gt;&lt;br&gt;I'll confirm this with verifier guys.</description></item><item><title>re: The world is a better place if you generate verifiable IL</title><link>http://blogs.msdn.com/davidnotario/archive/2005/02/04/367556.aspx#374466</link><pubDate>Wed, 16 Feb 2005 19:22:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:374466</guid><dc:creator>Jeroen Frijters</dc:creator><description>Thanks. Believe me, I do understand the issues involved. I've written a Java VM for .NET and this is kind of annoying (I could work around it by hoisting the try/finally code into a static method).&lt;br&gt;BTW, the ECMA spec doesn't say anything about this (apart from the fact that you must call be base class constructor before returning from the constructor).&lt;br&gt;There's also a related issue. I'd like to be able to do (also in a constructor):&lt;br&gt;&lt;br&gt;try&lt;br&gt;{&lt;br&gt;  // constructor body&lt;br&gt;  // including call to base class ctor&lt;br&gt;}&lt;br&gt;finally&lt;br&gt;{&lt;br&gt;  GC.KeepAlive(this);&lt;br&gt;}&lt;br&gt;&lt;br&gt;This is needed to make sure the object isn't GC'ed before the constructor ends (which is required by the JVM spec). However, this code isn't verifiable because this may be uninitialized and the verifier doesn't understand that GC.KeepAlive is a harmless method.&lt;br&gt;(This same issue also applies to Monitor.Enter/Exit, the verifier should also allow these methods to be called with an uninitialized this).</description></item><item><title>re: The world is a better place if you generate verifiable IL</title><link>http://blogs.msdn.com/davidnotario/archive/2005/02/04/367556.aspx#375087</link><pubDate>Thu, 17 Feb 2005 10:06:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375087</guid><dc:creator>David Notario</dc:creator><description>Jeroen, first, thanks for doing the right thing (being verifiable) I agree with you that the status quo is not the best it could be. I spoke with the verifier people. It turns out that some of our verifier rules are not in ECMA. Some of them are just implementation limitations we don't want to standarize and others that will eventually get there.&lt;br&gt;&lt;br&gt;Let me know of other stuff you find, I'll make sure we're tracking it (I can start a post where I can add these as we find them, as a way of having them public until they get standarized)&lt;br&gt;&lt;br&gt;I assume you need the KeepAlive for the case where the instance .ctor gets inlined and you have code like this&lt;br&gt;&lt;br&gt;.ctor()&lt;br&gt;{&lt;br&gt;  some_field = 0;&lt;br&gt;  // if .ctor was inlined at this point, with no other references it could be collected.&lt;br&gt;  Console.WriteLine(&amp;quot;Hello&amp;quot;);&lt;br&gt;}&lt;br&gt;&lt;br&gt;Why do you need the GC.KeepAlive inside a finally? GC.KeepAlive should be treated as a side effect, so just adding the KeepAlive at the end should do what you want (assuming that in the case of an exception you don't care, as the .ctor will never complete)&lt;br&gt;&lt;br&gt;If that doesn't work:&lt;br&gt;&lt;br&gt;Translate&lt;br&gt;&lt;br&gt;java_equivalent_of_newobj&lt;br&gt;&lt;br&gt;to:&lt;br&gt;&lt;br&gt;newobj instance_ctor&lt;br&gt;dup&lt;br&gt;call GC.KeepAlive&lt;br&gt;&lt;br&gt;In both cases you also get rid of the EH overhead.&lt;br&gt;&lt;br&gt;Let me know if you need anything else.</description></item><item><title>re: The world is a better place if you generate verifiable IL</title><link>http://blogs.msdn.com/davidnotario/archive/2005/02/04/367556.aspx#375161</link><pubDate>Thu, 17 Feb 2005 12:46:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375161</guid><dc:creator>Jeroen Frijters</dc:creator><description>Thanks. I think the exception scenario may also be important. For some constructor parameter values, the JIT may be able to inline the constructor and eliminate code in such a way that it can detect that an exception will be throw, this will affect the liveness of this the pointer. BTW, the reason this is important is not so much because of external side effects (like the Console.WriteLine example you give), but because the constructor might contain native method calls that use an unmanaged resource. If the finalizer runs before the constructor terminates, the unmanaged resource will be freed will the native code is running. This is obviously not good ;-)&lt;br&gt;&lt;br&gt;I did consider your alternate suggestion (adding the KeepAlive after the newobj), but that doesn't satisfy me either, because other .NET languages obvious won't do that, so interop will suffer.&lt;br&gt;&lt;br&gt;One potential solution I thought of yesterday is to add a call to KeepAlive after every call that occurs in the constructor (once the this has been initialized). That could still theoretically (in some obscure scenarios) cause problems when the base class constructor throws an exception, but I think it might be the best workaround.&lt;br&gt;</description></item><item><title>re: The world is a better place if you generate verifiable IL</title><link>http://blogs.msdn.com/davidnotario/archive/2005/02/04/367556.aspx#375164</link><pubDate>Thu, 17 Feb 2005 12:51:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:375164</guid><dc:creator>Jeroen Frijters</dc:creator><description>Oh, and additionally, the try/finally before the base class ctor call is actually a real issue as well (well, it's actually try/catch since the JVM doesn't have a try/finally construct).&lt;br&gt;&lt;br&gt;Take the following Java code:&lt;br&gt;&lt;br&gt;class Base {&lt;br&gt;  Base(Class c) { ... }&lt;br&gt;}&lt;br&gt;&lt;br&gt;class Derived {&lt;br&gt;  Derived() {&lt;br&gt;    super(SomeClass.class);&lt;br&gt;  }&lt;br&gt;}&lt;br&gt;&lt;br&gt;(The &amp;quot;SomeClass.class&amp;quot; construct is equivalent to C#'s typeof(SomeClass))&lt;br&gt;&lt;br&gt;One particular Java compiler compiles the Derived ctor as:&lt;br&gt;&lt;br&gt;Class c;&lt;br&gt;try {&lt;br&gt;  c = Class.forName(&amp;quot;SomeClass&amp;quot;);&lt;br&gt;} catch(ClassNotFoundException) {&lt;br&gt;  throw new NoClassDefFoundError();&lt;br&gt;}&lt;br&gt;super(c);&lt;br&gt;&lt;br&gt;My current (straight-forward) translation of this construct is rejected by PEverify is not verifiable (however, note that the CLR verifier doesn't agree with PEVerify on this!)&lt;br&gt;&lt;br&gt;What I'd like to know is whether PEVerify is right, or the runtime is right.</description></item><item><title>Dynamic Assemblies and Declarative Security</title><link>http://blogs.msdn.com/davidnotario/archive/2005/02/04/367556.aspx#422766</link><pubDate>Sat, 28 May 2005 02:50:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:422766</guid><dc:creator>.Net Security Blog</dc:creator><description>Speaking of dynamic IL generation ...&lt;br&gt;Before Whidbey, the framework supplied two ways of creating code...</description></item><item><title>Managed C++ codegen for new, array manipulation, delete</title><link>http://blogs.msdn.com/davidnotario/archive/2005/02/04/367556.aspx#460616</link><pubDate>Sun, 04 Sep 2005 06:37:42 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:460616</guid><dc:creator>Mike Stall's .NET Debugging Blog</dc:creator><description>&lt;br&gt;Managed C++ (MC++) code generation is a cool accomplishment. I think it's &lt;br&gt;another good testimony...</description></item><item><title>Managed C++ codegen for new, array manipulation, delete</title><link>http://blogs.msdn.com/davidnotario/archive/2005/02/04/367556.aspx#460617</link><pubDate>Sun, 04 Sep 2005 06:39:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:460617</guid><dc:creator>Mike Stall's .NET Debugging Blog</dc:creator><description>&lt;br&gt;&lt;br&gt;Managed C++ (MC++) code generation is a cool accomplishment. I think it's &lt;br&gt;another good testimony...</description></item><item><title>Managed C++ codegen for new, array manipulation, delete</title><link>http://blogs.msdn.com/davidnotario/archive/2005/02/04/367556.aspx#460618</link><pubDate>Sun, 04 Sep 2005 06:52:29 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:460618</guid><dc:creator>Mike Stall's .NET Debugging Blog</dc:creator><description>&lt;br&gt;&lt;br&gt;Managed C++ (MC++) code generation is a cool accomplishment. I think it's &lt;br&gt;another good testimony...</description></item><item><title>Managed C++ codegen for new, array manipulation, delete</title><link>http://blogs.msdn.com/davidnotario/archive/2005/02/04/367556.aspx#462304</link><pubDate>Thu, 08 Sep 2005 08:10:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:462304</guid><dc:creator>Mike Stall's .NET Debugging Blog</dc:creator><description>Managed C++ (MC++) code generation is a cool accomplishment. I think it's another good testimony to the...</description></item></channel></rss>