Skip to content

Commit

Permalink
[metadata] make GENERATE_TRY_GET_CLASS_WITH_CACHE usable in components
Browse files Browse the repository at this point in the history
Use a component API function to get corlib when building a dynamic
component, otherwise use mono_defaults directly as before.

Also update the debugger to use the public API to get common classes
in almost all cases.  There are still a few places where it needs
MonoDefaults directly
  • Loading branch information
lambdageek committed Jan 15, 2022
1 parent 02e22b0 commit 06cbfc0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 36 deletions.
41 changes: 12 additions & 29 deletions src/mono/mono/component/debugger-agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,6 @@
#pragma warning(disable:4312) // FIXME pointer cast to different size
#endif

#define GENERATE_TRY_GET_CLASS_WITH_CACHE_DBG(shortname,name_space,name) \
MonoClass* \
mono_class_try_get_##shortname##_class (void) \
{ \
static volatile MonoClass *tmp_class; \
static volatile gboolean inited; \
MonoClass *klass = (MonoClass *)tmp_class; \
mono_memory_barrier (); \
if (!inited) { \
klass = mono_class_try_load_from_name (mdbg_mono_defaults->corlib, name_space, name); \
tmp_class = klass; \
mono_memory_barrier (); \
inited = TRUE; \
} \
return klass; \
}

#ifndef MONO_HANDLE_TRACK_OWNER

#define MONO_HANDLE_NEW_DBG(type, object) \
Expand All @@ -162,13 +145,13 @@ mono_class_try_get_##shortname##_class (void) \
static inline MonoType*
mono_get_object_type_dbg (void)
{
return m_class_get_byval_arg (mdbg_mono_defaults->object_class);
return m_class_get_byval_arg (mono_get_object_class ());
}

static inline MonoType*
mono_get_void_type_dbg (void)
{
return m_class_get_byval_arg (mdbg_mono_defaults->void_class);
return m_class_get_byval_arg (mono_get_void_class ());
}

typedef struct {
Expand Down Expand Up @@ -532,7 +515,7 @@ static int handle_multiple_ss_requests (void);
static void mono_dbg_debugger_agent_user_break (void);


static GENERATE_TRY_GET_CLASS_WITH_CACHE_DBG (fixed_buffer, "System.Runtime.CompilerServices", "FixedBufferAttribute")
static GENERATE_TRY_GET_CLASS_WITH_CACHE (fixed_buffer, "System.Runtime.CompilerServices", "FixedBufferAttribute")

#ifndef DISABLE_SOCKET_TRANSPORT
static void
Expand Down Expand Up @@ -2498,7 +2481,7 @@ process_suspend (DebuggerTlsData *tls, MonoContext *ctx)
g_assert (ji);
/* Can't suspend in these methods */
method = jinfo_get_method (ji);
if (method->klass == mdbg_mono_defaults->string_class && (!strcmp (method->name, "memset") || strstr (method->name, "memcpy")))
if (method->klass == mono_get_string_class () && (!strcmp (method->name, "memset") || strstr (method->name, "memcpy")))
return;

save_thread_context (ctx);
Expand Down Expand Up @@ -2945,7 +2928,7 @@ static gint32 isFixedSizeArray (MonoClassField *f)
CattrNamedArg *arginfo;
int num_named_args;

mono_reflection_create_custom_attr_data_args_noalloc (mdbg_mono_defaults->corlib, attr->ctor, attr->data, attr->data_size,
mono_reflection_create_custom_attr_data_args_noalloc (mono_get_corlib (), attr->ctor, attr->data, attr->data_size,
&typed_args, &named_args, &num_named_args, &arginfo, error);
if (!is_ok (error)) {
ret = 0;
Expand Down Expand Up @@ -3244,9 +3227,9 @@ dbg_path_get_basename (const char *filename)
return g_strdup (&r[1]);
}

static GENERATE_TRY_GET_CLASS_WITH_CACHE_DBG (hidden_klass, "System.Diagnostics", "DebuggerHiddenAttribute")
static GENERATE_TRY_GET_CLASS_WITH_CACHE_DBG (step_through_klass, "System.Diagnostics", "DebuggerStepThroughAttribute")
static GENERATE_TRY_GET_CLASS_WITH_CACHE_DBG (non_user_klass, "System.Diagnostics", "DebuggerNonUserCodeAttribute")
static GENERATE_TRY_GET_CLASS_WITH_CACHE (hidden_klass, "System.Diagnostics", "DebuggerHiddenAttribute")
static GENERATE_TRY_GET_CLASS_WITH_CACHE (step_through_klass, "System.Diagnostics", "DebuggerStepThroughAttribute")
static GENERATE_TRY_GET_CLASS_WITH_CACHE (non_user_klass, "System.Diagnostics", "DebuggerNonUserCodeAttribute")

static void
init_jit_info_dbg_attrs (MonoJitInfo *ji)
Expand Down Expand Up @@ -3769,7 +3752,7 @@ runtime_initialized (MonoProfiler *prof)
{
process_profiler_event (EVENT_KIND_VM_START, mono_thread_current ());
if (CHECK_PROTOCOL_VERSION (2, 59))
process_profiler_event (EVENT_KIND_ASSEMBLY_LOAD, (mdbg_mono_defaults->corlib->assembly));
process_profiler_event (EVENT_KIND_ASSEMBLY_LOAD, (mono_get_corlib ()->assembly));
if (agent_config.defer) {
ERROR_DECL (error);
start_debugger_thread (error);
Expand Down Expand Up @@ -6734,7 +6717,7 @@ vm_commands (int command, int id, guint8 *p, guint8 *end, Buffer *buf)
wait_for_suspend ();

#ifdef TRY_MANAGED_SYSTEM_ENVIRONMENT_EXIT
env_class = mono_class_try_load_from_name (mdbg_mono_defaults->corlib, "System", "Environment");
env_class = mono_class_try_load_from_name (mono_get_corlib (), "System", "Environment");
if (env_class) {
ERROR_DECL (error);
exit_method = mono_class_get_method_from_name_checked (env_class, "Exit", 1, 0, error);
Expand Down Expand Up @@ -7115,7 +7098,7 @@ event_commands (int command, guint8 *p, guint8 *end, Buffer *buf)
if (exc_class) {
req->modifiers [i].data.exc_class = exc_class;

if (!mono_class_is_assignable_from_internal (mdbg_mono_defaults->exception_class, exc_class)) {
if (!mono_class_is_assignable_from_internal (mono_get_exception_class (), exc_class)) {
g_free (req);
return ERR_INVALID_ARGUMENT;
}
Expand Down Expand Up @@ -8741,7 +8724,7 @@ method_commands_internal (int command, MonoMethod *method, MonoDomain *domain, g
} else if (handle_class == mdbg_mono_defaults->methodhandle_class) {
buffer_add_byte (buf, TOKEN_TYPE_METHOD);
buffer_add_methodid (buf, domain, (MonoMethod *)val);
} else if (handle_class == mdbg_mono_defaults->string_class) {
} else if (handle_class == mono_get_string_class ()) {
char *s;

s = mono_string_to_utf8_checked_internal ((MonoString *)val, error);
Expand Down
4 changes: 2 additions & 2 deletions src/mono/mono/component/debugger-engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ mono_de_process_single_step (void *tls, gboolean from_signal)
* Stopping in memset makes half-initialized vtypes visible.
* Stopping in memcpy makes half-copied vtypes visible.
*/
if (method->klass == mdbg_mono_defaults->string_class && (!strcmp (method->name, "memset") || strstr (method->name, "memcpy")))
if (method->klass == mono_get_string_class () && (!strcmp (method->name, "memset") || strstr (method->name, "memcpy")))
goto exit;

/*
Expand Down Expand Up @@ -1719,7 +1719,7 @@ get_notify_debugger_of_wait_completion_method (void)
if (notify_debugger_of_wait_completion_method_cache != NULL)
return notify_debugger_of_wait_completion_method_cache;
ERROR_DECL (error);
MonoClass* task_class = mono_class_load_from_name (mdbg_mono_defaults->corlib, "System.Threading.Tasks", "Task");
MonoClass* task_class = mono_class_load_from_name (mono_get_corlib (), "System.Threading.Tasks", "Task");
GPtrArray* array = mono_class_get_methods_by_name (task_class, "NotifyDebuggerOfWaitCompletion", 0x24, 1, FALSE, error);
mono_error_assert_ok (error);
g_assert (array->len == 1);
Expand Down
20 changes: 15 additions & 5 deletions src/mono/mono/metadata/class-internals.h
Original file line number Diff line number Diff line change
Expand Up @@ -984,12 +984,25 @@ typedef struct {
MonoClass *appcontext_class;
} MonoDefaults;

/* If you need a MonoType, use one of the mono_get_*_type () functions in class-inlines.h */
extern MonoDefaults mono_defaults;

#define GENERATE_GET_CLASS_WITH_CACHE_DECL(shortname) \
MonoClass* mono_class_get_##shortname##_class (void);

#define GENERATE_TRY_GET_CLASS_WITH_CACHE_DECL(shortname) \
MonoClass* mono_class_try_get_##shortname##_class (void);

static inline MonoImage *
mono_class_generate_get_corlib_impl (void)
{
#ifdef COMPILING_COMPONENT_DYNAMIC
return mono_get_corlib ();
#else
return mono_defaults.corlib;
#endif
}

// GENERATE_GET_CLASS_WITH_CACHE attempts mono_class_load_from_name whenever
// its cache is null. i.e. potentially repeatedly, though it is expected to succeed
// the first time.
Expand All @@ -1001,7 +1014,7 @@ mono_class_get_##shortname##_class (void) \
static MonoClass *tmp_class; \
MonoClass *klass = tmp_class; \
if (!klass) { \
klass = mono_class_load_from_name (mono_defaults.corlib, name_space, name); \
klass = mono_class_load_from_name (mono_class_generate_get_corlib_impl (), name_space, name); \
mono_memory_barrier (); /* FIXME excessive? */ \
tmp_class = klass; \
} \
Expand All @@ -1023,7 +1036,7 @@ mono_class_try_get_##shortname##_class (void) \
MonoClass *klass = (MonoClass *)tmp_class; \
mono_memory_barrier (); \
if (!inited) { \
klass = mono_class_try_load_from_name (mono_defaults.corlib, name_space, name); \
klass = mono_class_try_load_from_name (mono_class_generate_get_corlib_impl (), name_space, name); \
tmp_class = klass; \
mono_memory_barrier (); \
inited = TRUE; \
Expand Down Expand Up @@ -1055,9 +1068,6 @@ GENERATE_TRY_GET_CLASS_WITH_CACHE_DECL(handleref)
GENERATE_GET_CLASS_WITH_CACHE_DECL (assembly_load_context)
GENERATE_GET_CLASS_WITH_CACHE_DECL (native_library)

/* If you need a MonoType, use one of the mono_get_*_type () functions in class-inlines.h */
extern MonoDefaults mono_defaults;

void
mono_loader_init (void);

Expand Down

0 comments on commit 06cbfc0

Please sign in to comment.