Dont leak loader errors in the aot compiler, as it could cause later methods to not...
[mono.git] / mono / mini / aot-compiler.c
index 62c364ad0ae013267bf1956a9e492b8b062f3998..53c43efb270c0e19fed8a2cc8d2335f80c6e3a44 100644 (file)
@@ -185,7 +185,7 @@ typedef struct MonoAotCompile {
        MonoClass **typespec_classes;
        GString *llc_args;
        GString *as_args;
-       gboolean thumb_mixed, need_no_dead_strip;
+       gboolean thumb_mixed, need_no_dead_strip, need_pt_gnu_stack;
 } MonoAotCompile;
 
 typedef struct {
@@ -573,6 +573,10 @@ arch_init (MonoAotCompile *acfg)
        acfg->llvm_label_prefix = "_";
        acfg->need_no_dead_strip = TRUE;
 #endif
+
+#if defined(__linux__) && !defined(TARGET_ARM)
+       acfg->need_pt_gnu_stack = TRUE;
+#endif
 }
 
 /*
@@ -2080,6 +2084,13 @@ encode_method_ref (MonoAotCompile *acfg, MonoMethod *method, guint8 *buf, guint8
                                g_assert_not_reached ();
                        }
                        break;
+               case MONO_WRAPPER_CASTCLASS:
+                       if (!strcmp (method->name, "__castclass_with_cache")) {
+                               encode_value (MONO_AOT_WRAPPER_CASTCLASS_WITH_CACHE, p, &p);
+                       } else {
+                               g_assert_not_reached ();
+                       }
+                       break;
                default:
                        g_assert_not_reached ();
                }
@@ -2653,7 +2664,12 @@ add_wrappers (MonoAotCompile *acfg)
                        klass = mono_class_get (acfg->image, token);
                        if (klass)
                                add_method (acfg, mono_marshal_get_virtual_stelemref (mono_array_class_get (klass, 1)));
+                       else
+                               mono_loader_clear_error ();
                }
+
+               /* castclass_with_check wrapper */
+               add_method (acfg, mono_marshal_get_castclass_with_cache ());
        }
 
        /* 
@@ -2686,6 +2702,11 @@ add_wrappers (MonoAotCompile *acfg)
                token = MONO_TOKEN_TYPE_DEF | (i + 1);
                klass = mono_class_get (acfg->image, token);
 
+               if (!klass) {
+                       mono_loader_clear_error ();
+                       continue;
+               }
+
                if (klass->delegate && klass != mono_defaults.delegate_class && klass != mono_defaults.multicastdelegate_class && !klass->generic_container) {
                        method = mono_get_delegate_invoke (klass);
 
@@ -2793,6 +2814,11 @@ add_wrappers (MonoAotCompile *acfg)
                token = MONO_TOKEN_TYPE_DEF | (i + 1);
                klass = mono_class_get (acfg->image, token);
 
+               if (!klass) {
+                       mono_loader_clear_error ();
+                       continue;
+               }
+
                if (klass->valuetype && !klass->generic_container && can_marshal_struct (klass)) {
                        add_method (acfg, mono_marshal_get_struct_to_ptr (klass));
                        add_method (acfg, mono_marshal_get_ptr_to_struct (klass));
@@ -2851,6 +2877,34 @@ add_generic_class (MonoAotCompile *acfg, MonoClass *klass, gboolean force)
        add_generic_class_with_depth (acfg, klass, 0);
 }
 
+static gboolean
+check_type_depth (MonoType *t, int depth)
+{
+       int i;
+
+       if (depth > 8)
+               return TRUE;
+
+       switch (t->type) {
+       case MONO_TYPE_GENERICINST: {
+               MonoGenericClass *gklass = t->data.generic_class;
+               MonoGenericInst *ginst = gklass->context.class_inst;
+
+               if (ginst) {
+                       for (i = 0; i < ginst->type_argc; ++i) {
+                               if (check_type_depth (ginst->type_argv [i], depth + 1))
+                                       return TRUE;
+                       }
+               }
+               break;
+       }
+       default:
+               break;
+       }
+
+       return FALSE;
+}
+
 /*
  * add_generic_class:
  *
@@ -2862,9 +2916,6 @@ add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth)
        MonoMethod *method;
        gpointer iter;
 
-       if (depth > 8)
-               return;
-
        mono_class_init (klass);
 
        if (klass->generic_class && klass->generic_class->context.class_inst->is_open)
@@ -2876,6 +2927,9 @@ add_generic_class_with_depth (MonoAotCompile *acfg, MonoClass *klass, int depth)
        if (!klass->generic_class && !klass->rank)
                return;
 
+       if (check_type_depth (&klass->byval_arg, 0))
+               return;
+
        iter = NULL;
        while ((method = mono_class_get_methods (klass, &iter))) {
                if (mono_method_is_generic_sharable_impl_full (method, FALSE, FALSE))
@@ -3124,8 +3178,10 @@ add_generic_instances (MonoAotCompile *acfg)
                token = MONO_TOKEN_TYPE_SPEC | (i + 1);
 
                klass = mono_class_get (acfg->image, token);
-               if (!klass || klass->rank)
+               if (!klass || klass->rank) {
+                       mono_loader_clear_error ();
                        continue;
+               }
 
                add_generic_class (acfg, klass, FALSE);
        }
@@ -3523,6 +3579,7 @@ encode_patch (MonoAotCompile *acfg, MonoJumpInfo *patch_info, guint8 *buf, guint
                break;
        case MONO_PATCH_INFO_MSCORLIB_GOT_ADDR:
        case MONO_PATCH_INFO_GC_CARD_TABLE_ADDR:
+       case MONO_PATCH_INFO_CASTCLASS_CACHE:
                break;
        case MONO_PATCH_INFO_METHOD_REL:
                encode_value ((gint)patch_info->data.offset, p, &p);
@@ -3814,7 +3871,7 @@ emit_exception_debug_info (MonoAotCompile *acfg, MonoCompile *cfg)
 
        seq_points = cfg->seq_point_info;
 
-       buf_size = header->num_clauses * 256 + debug_info_size + 1024 + (seq_points ? (seq_points->len * 64) : 0) + cfg->gc_map_size;
+       buf_size = header->num_clauses * 256 + debug_info_size + 2048 + (seq_points ? (seq_points->len * 64) : 0) + cfg->gc_map_size;
        p = buf = g_malloc (buf_size);
 
 #ifdef MONO_ARCH_HAVE_XP_UNWIND
@@ -3994,6 +4051,8 @@ emit_klass_info (MonoAotCompile *acfg, guint32 token)
        gpointer iter = NULL;
 
        if (!klass) {
+               mono_loader_clear_error ();
+
                buf_size = 16;
 
                p = buf = g_malloc (buf_size);
@@ -4685,6 +4744,11 @@ can_encode_patch (MonoAotCompile *acfg, MonoJumpInfo *patch_info)
                                        return TRUE;
                                else
                                        return FALSE;
+                       case MONO_WRAPPER_CASTCLASS:
+                               if (!strcmp (method->name, "__castclass_with_cache"))
+                                       return TRUE;
+                               else
+                                       return FALSE;
                        default:
                                //printf ("Skip (wrapper call): %d -> %s\n", patch_info->type, mono_method_full_name (patch_info->data.method, TRUE));
                                return FALSE;
@@ -4788,6 +4852,7 @@ compile_method (MonoAotCompile *acfg, MonoMethod *method)
                return;
        }
        if (cfg->exception_type != MONO_EXCEPTION_NONE) {
+               //printf ("E: %s\n", mono_method_full_name (method, TRUE));
                /* Let the exception happen at runtime */
                return;
        }
@@ -5226,7 +5291,7 @@ emit_llvm_file (MonoAotCompile *acfg)
         * then removing tailcallelim + the global opts, and adding a second gvn.
         */
        opts = g_strdup ("-instcombine -simplifycfg");
-       opts = g_strdup ("-simplifycfg -domtree -domfrontier -scalarrepl -instcombine -simplifycfg -basiccg -prune-eh -inline -functionattrs -domtree -domfrontier -scalarrepl -simplify-libcalls -instcombine -simplifycfg -instcombine -simplifycfg -reassociate -domtree -loops -loopsimplify -domfrontier -loopsimplify -lcssa -loop-rotate -licm -lcssa -loop-unswitch -instcombine -scalar-evolution -loopsimplify -lcssa -iv-users -indvars -loop-deletion -loopsimplify -lcssa -loop-unroll -instcombine -memdep -gvn -memdep -memcpyopt -sccp -instcombine -domtree -memdep -dse -adce -gvn -simplifycfg -preverify -domtree -verify");
+       opts = g_strdup ("-simplifycfg -domtree -domfrontier -scalarrepl -instcombine -simplifycfg -basiccg -prune-eh -inline -functionattrs -domtree -domfrontier -scalarrepl -simplify-libcalls -instcombine -simplifycfg -instcombine -simplifycfg -reassociate -domtree -loops -loopsimplify -domfrontier -loopsimplify -lcssa -loop-rotate -licm -lcssa -loop-unswitch -instcombine -scalar-evolution -loopsimplify -lcssa -iv-users -indvars -loop-deletion -loopsimplify -lcssa -loop-unroll -instcombine -memdep -gvn -memdep -memcpyopt -sccp -instcombine -domtree -memdep -dse -adce -simplifycfg -preverify -domtree -verify");
 #if 1
        command = g_strdup_printf ("%sopt -f %s -o temp.opt.bc temp.bc", acfg->aot_opts.llvm_path, opts);
        printf ("Executing opt: %s\n", command);
@@ -5932,8 +5997,10 @@ emit_class_name_table (MonoAotCompile *acfg)
        for (i = 0; i < acfg->image->tables [MONO_TABLE_TYPEDEF].rows; ++i) {
                token = MONO_TOKEN_TYPE_DEF | (i + 1);
                klass = mono_class_get (acfg->image, token);
-               if (!klass)
+               if (!klass) {
+                       mono_loader_clear_error ();
                        continue;
+               }
                full_name = mono_type_get_name_full (mono_class_get_type (klass), MONO_TYPE_NAME_FORMAT_FULL_NAME);
                hash = mono_metadata_str_hash (full_name) % table_size;
                g_free (full_name);
@@ -7035,6 +7102,13 @@ mono_compile_assembly (MonoAssembly *ass, guint32 opts, const char *aot_options)
 
        emit_mem_end (acfg);
 
+       if (acfg->need_pt_gnu_stack) {
+               /* This is required so the .so doesn't have an executable stack */
+               /* The bin writer already emits this */
+               if (!acfg->use_bin_writer)
+                       fprintf (acfg->fp, "\n.section  .note.GNU-stack,\"\",@progbits\n");
+       }
+
        TV_GETTIME (btv);
 
        acfg->stats.gen_time = TV_ELAPSED (atv, btv);