Only do a null check in LDFLDA if the address is an object reference.
authorZoltan Varga <vargaz@gmail.com>
Fri, 13 Aug 2010 14:53:29 +0000 (16:53 +0200)
committerZoltan Varga <vargaz@gmail.com>
Fri, 13 Aug 2010 14:54:06 +0000 (16:54 +0200)
mono/mini/exceptions.cs
mono/mini/method-to-ir.c

index c0ae9107d19578bb7fadd77093944b850d60301f..4f782c9be09c03e6a83260a2088124f0ef3b507c 100644 (file)
@@ -2606,5 +2606,19 @@ class Tests {
 
                return 1;
        }
+
+       unsafe struct Foo
+       {
+               public int i;
+
+               public static Foo* pFoo;
+       }
+
+       /* MS.NET doesn't seem to throw in this case */
+       public unsafe static int test_0_ldflda_null_pointer () {
+               int* pi = &Foo.pFoo->i;
+
+               return 0;
+       }
 }
 
index 5618ab266d2a8e56ffff9a6f6c8e4cbd1897b130..59f7bfc816f8b47be81f8db216e55ffc240b19f1 100644 (file)
@@ -8213,8 +8213,10 @@ mono_method_to_ir (MonoCompile *cfg, MonoMethod *method, MonoBasicBlock *start_b
                                }
 
                                if (*ip == CEE_LDFLDA) {
-                                       MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, sp [0]->dreg, 0);
-                                       MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException");
+                                       if (sp [0]->type == STACK_OBJ) {
+                                               MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, sp [0]->dreg, 0);
+                                               MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException");
+                                       }
 
                                        dreg = alloc_preg (cfg);