[mono][jit] read DebuggableAttribute(DebuggingModes) ctor; move to metadata/ (#49505)
authorAleksey Kliger (λgeek) <alklig@microsoft.com>
Fri, 12 Mar 2021 18:44:04 +0000 (13:44 -0500)
committerGitHub <noreply@github.com>
Fri, 12 Mar 2021 18:44:04 +0000 (13:44 -0500)
* [jit] read DebuggableAttribute(DebuggingModes) ctor; move to metadata/

   Pull is_jit_optimizer_disabled out of mini into metadata.

   Also support reading the DebuggingModes ctor

* fix whitespace

Co-authored-by: Thays Grazia <thaystg@gmail.com>
src/mono/mono/metadata/assembly-internals.h
src/mono/mono/metadata/assembly.c
src/mono/mono/metadata/metadata-internals.h
src/mono/mono/mini/method-to-ir.c

index 35b700f..656c37c 100644 (file)
@@ -147,4 +147,8 @@ mono_assembly_get_image_internal (MonoAssembly *assembly);
 void
 mono_set_assemblies_path_direct (char **path);
 
+gboolean
+mono_assembly_is_jit_optimizer_disabled (MonoAssembly *assembly);
+
+
 #endif /* __MONO_METADATA_ASSEMBLY_INTERNALS_H__ */
index 9a1b674..c532e10 100644 (file)
@@ -95,6 +95,8 @@ static const MonoBundledAssembly **bundles;
 static const MonoBundledSatelliteAssembly **satellite_bundles;
 
 /* Class lazy loading functions */
+static GENERATE_TRY_GET_CLASS_WITH_CACHE (debuggable_attribute, "System.Diagnostics", "DebuggableAttribute")
+
 static GENERATE_TRY_GET_CLASS_WITH_CACHE (internals_visible, "System.Runtime.CompilerServices", "InternalsVisibleToAttribute")
 static MonoAssembly*
 mono_assembly_invoke_search_hook_internal (MonoAssemblyLoadContext *alc, MonoAssembly *requesting, MonoAssemblyName *aname, gboolean postload);
@@ -3720,3 +3722,77 @@ mono_asmctx_get_name (const MonoAssemblyContext *asmctx)
        g_assert (asmctx->kind >= 0 && asmctx->kind <= MONO_ASMCTX_LAST);
        return names [asmctx->kind];
 }
+
+/**
+ * mono_assembly_is_jit_optimizer_disabled:
+ *
+ * \param assm the assembly
+ *
+ * Returns TRUE if the System.Diagnostics.DebuggableAttribute has the
+ *  DebuggingModes.DisableOptimizations bit set.
+ *
+ */
+gboolean
+mono_assembly_is_jit_optimizer_disabled (MonoAssembly *ass)
+{
+       ERROR_DECL (error);
+
+       g_assert (ass);
+       if (ass->jit_optimizer_disabled_inited)
+               return ass->jit_optimizer_disabled;
+
+       MonoClass *klass = mono_class_try_get_debuggable_attribute_class ();
+
+       if (!klass) {
+               /* Linked away */
+               ass->jit_optimizer_disabled = FALSE;
+               mono_memory_barrier ();
+               ass->jit_optimizer_disabled_inited = TRUE;
+               return FALSE;
+       }
+
+       gboolean disable_opts = FALSE;
+       MonoCustomAttrInfo* attrs = mono_custom_attrs_from_assembly_checked (ass, FALSE, error);
+       mono_error_cleanup (error); /* FIXME don't swallow the error */
+       if (attrs) {
+               for (int i = 0; i < attrs->num_attrs; ++i) {
+                       MonoCustomAttrEntry *attr = &attrs->attrs [i];
+                       const gchar *p;
+                       MonoMethodSignature *sig;
+
+                       if (!attr->ctor || attr->ctor->klass != klass)
+                               continue;
+                       /* Decode the attribute. See reflection.c */
+                       p = (const char*)attr->data;
+                       g_assert (read16 (p) == 0x0001);
+                       p += 2;
+
+                       // FIXME: Support named parameters
+                       sig = mono_method_signature_internal (attr->ctor);
+                       MonoClass *param_class;
+                       if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_BOOLEAN && sig->params [1]->type == MONO_TYPE_BOOLEAN) {
+
+                               /* Two boolean arguments */
+                               p ++;
+                               disable_opts = *p;
+                       } else if (sig->param_count == 1 &&
+                                  sig->params[0]->type == MONO_TYPE_VALUETYPE &&
+                                  (param_class = mono_class_from_mono_type_internal (sig->params[0])) != NULL &&
+                                  m_class_is_enumtype (param_class) &&
+                                  !strcmp (m_class_get_name (param_class), "DebuggingModes")) {
+                               /* System.Diagnostics.DebuggableAttribute+DebuggingModes */
+                               int32_t flags = read32 (p);
+                               p += 4;
+                               disable_opts = (flags & 0x0100) != 0;
+                       }
+               }
+               mono_custom_attrs_free (attrs);
+       }
+
+       ass->jit_optimizer_disabled = disable_opts;
+       mono_memory_barrier ();
+       ass->jit_optimizer_disabled_inited = TRUE;
+
+       return disable_opts;
+
+}
index 63e895d..6ec2d98 100644 (file)
@@ -1083,7 +1083,6 @@ mono_assembly_name_parse_full                  (const char           *name,
 gboolean
 mono_assembly_fill_assembly_name_full (MonoImage *image, MonoAssemblyName *aname, gboolean copyBlobs);
 
-
 MONO_API guint32 mono_metadata_get_generic_param_row (MonoImage *image, guint32 token, guint32 *owner);
 
 MonoGenericParam*
index 24a5b3a..ed3c96e 100644 (file)
@@ -40,6 +40,7 @@
 #include <mono/utils/memcheck.h>
 #include <mono/metadata/abi-details.h>
 #include <mono/metadata/assembly.h>
+#include <mono/metadata/assembly-internals.h>
 #include <mono/metadata/attrdefs.h>
 #include <mono/metadata/loader.h>
 #include <mono/metadata/tabledefs.h>
@@ -188,7 +189,6 @@ convert_value (MonoCompile *cfg, MonoType *type, MonoInst *ins);
 /* helper methods signatures */
 
 /* type loading helpers */
-static GENERATE_TRY_GET_CLASS_WITH_CACHE (debuggable_attribute, "System.Diagnostics", "DebuggableAttribute")
 static GENERATE_GET_CLASS_WITH_CACHE (iequatable, "System", "IEquatable`1")
 static GENERATE_GET_CLASS_WITH_CACHE (geqcomparer, "System.Collections.Generic", "GenericEqualityComparer`1");
 
@@ -5339,58 +5339,12 @@ is_exception_class (MonoClass *klass)
 static gboolean
 is_jit_optimizer_disabled (MonoMethod *m)
 {
-       ERROR_DECL (error);
        MonoAssembly *ass = m_class_get_image (m->klass)->assembly;
-       MonoCustomAttrInfo* attrs;
-       MonoClass *klass;
-       int i;
-       gboolean val = FALSE;
 
        g_assert (ass);
        if (ass->jit_optimizer_disabled_inited)
                return ass->jit_optimizer_disabled;
-
-       klass = mono_class_try_get_debuggable_attribute_class ();
-
-       if (!klass) {
-               /* Linked away */
-               ass->jit_optimizer_disabled = FALSE;
-               mono_memory_barrier ();
-               ass->jit_optimizer_disabled_inited = TRUE;
-               return FALSE;
-       }
-
-       attrs = mono_custom_attrs_from_assembly_checked (ass, FALSE, error);
-       mono_error_cleanup (error); /* FIXME don't swallow the error */
-       if (attrs) {
-               for (i = 0; i < attrs->num_attrs; ++i) {
-                       MonoCustomAttrEntry *attr = &attrs->attrs [i];
-                       const gchar *p;
-                       MonoMethodSignature *sig;
-
-                       if (!attr->ctor || attr->ctor->klass != klass)
-                               continue;
-                       /* Decode the attribute. See reflection.c */
-                       p = (const char*)attr->data;
-                       g_assert (read16 (p) == 0x0001);
-                       p += 2;
-
-                       // FIXME: Support named parameters
-                       sig = mono_method_signature_internal (attr->ctor);
-                       if (sig->param_count != 2 || sig->params [0]->type != MONO_TYPE_BOOLEAN || sig->params [1]->type != MONO_TYPE_BOOLEAN)
-                               continue;
-                       /* Two boolean arguments */
-                       p ++;
-                       val = *p;
-               }
-               mono_custom_attrs_free (attrs);
-       }
-
-       ass->jit_optimizer_disabled = val;
-       mono_memory_barrier ();
-       ass->jit_optimizer_disabled_inited = TRUE;
-
-       return val;
+       return mono_assembly_is_jit_optimizer_disabled (ass);
 }
 
 gboolean