Skip to content

Commit

Permalink
[reflection] Add GC Unsafe transitions on embedding API functions (do…
Browse files Browse the repository at this point in the history
…tnet#52769)

Transition to GC Unsafe mode on every MONO_RT_EXTERNAL_ONLY function in
reflection.c

In particular, fix mono_reflection_type_from_name which is used in
https://github.com/xamarin/xamarin-android/blob/681887ebdbd192ce7ce1cd02221d4939599ba762/src/monodroid/jni/embedded-assemblies.cc#L350

Fixes stack traces like

```
05-14 08:06:12.848 31274 31274 F DEBUG   :       #00 pc 00000b99  [vdso] (__kernel_vsyscall+9)
05-14 08:06:12.848 31274 31274 F DEBUG   :       #1 pc 0005ad68  /apex/com.android.runtime/lib/bionic/libc.so (syscall+40) (BuildId: 6e3a0180fa6637b68c0d181c343e6806)
05-14 08:06:12.848 31274 31274 F DEBUG   :       #2 pc 00076511  /apex/com.android.runtime/lib/bionic/libc.so (abort+209) (BuildId: 6e3a0180fa6637b68c0d181c343e6806)
05-14 08:06:12.848 31274 31274 F DEBUG   :       #3 pc 0002afcd  /data/app/~~rMrkpKmVPaBpM5jKb8fPAg==/com.microsoft.maui-JfRo8RWSDJaNtJuBa0y7_Q==/lib/x86/libmonodroid.so (xamarin::android::internal::MonodroidRuntime::mono_log_handler(char const*, char const*, char const*, int, void*)+141) (BuildId: 9726f32ad5f8fa5e7c5762baf2f6e3294da41cc1)
05-14 08:06:12.848 31274 31274 F DEBUG   :       #4 pc 00112c5d  /data/app/~~rMrkpKmVPaBpM5jKb8fPAg==/com.microsoft.maui-JfRo8RWSDJaNtJuBa0y7_Q==/lib/x86/libmonosgen-2.0.so (eglib_log_adapter+141) (BuildId: b67e93dd750dafdd6f65f408b021b6a3a74868ac)
05-14 08:06:12.849 31274 31274 F DEBUG   :       #5 pc 00020fdf  /data/app/~~rMrkpKmVPaBpM5jKb8fPAg==/com.microsoft.maui-JfRo8RWSDJaNtJuBa0y7_Q==/lib/x86/libmonosgen-2.0.so (monoeg_g_logv+175) (BuildId: b67e93dd750dafdd6f65f408b021b6a3a74868ac)
05-14 08:06:12.849 31274 31274 F DEBUG   :       #6 pc 0002113a  /data/app/~~rMrkpKmVPaBpM5jKb8fPAg==/com.microsoft.maui-JfRo8RWSDJaNtJuBa0y7_Q==/lib/x86/libmonosgen-2.0.so (monoeg_g_log+42) (BuildId: b67e93dd750dafdd6f65f408b021b6a3a74868ac)
05-14 08:06:12.849 31274 31274 F DEBUG   :       #7 pc 00128892  /data/app/~~rMrkpKmVPaBpM5jKb8fPAg==/com.microsoft.maui-JfRo8RWSDJaNtJuBa0y7_Q==/lib/x86/libmonosgen-2.0.so (mono_threads_transition_do_blocking+258) (BuildId: b67e93dd750dafdd6f65f408b021b6a3a74868ac)
05-14 08:06:12.849 31274 31274 F DEBUG   :       #8 pc 0012a406  /data/app/~~rMrkpKmVPaBpM5jKb8fPAg==/com.microsoft.maui-JfRo8RWSDJaNtJuBa0y7_Q==/lib/x86/libmonosgen-2.0.so (mono_threads_enter_gc_safe_region_unbalanced_with_info+134) (BuildId: b67e93dd750dafdd6f65f408b021b6a3a74868ac)
05-14 08:06:12.849 31274 31274 F DEBUG   :       #9 pc 0012a27e  /data/app/~~rMrkpKmVPaBpM5jKb8fPAg==/com.microsoft.maui-JfRo8RWSDJaNtJuBa0y7_Q==/lib/x86/libmonosgen-2.0.so (mono_threads_enter_gc_safe_region_internal+46) (BuildId: b67e93dd750dafdd6f65f408b021b6a3a74868ac)
05-14 08:06:12.849 31274 31274 F DEBUG   :       #10 pc 000799a7  /data/app/~~rMrkpKmVPaBpM5jKb8fPAg==/com.microsoft.maui-JfRo8RWSDJaNtJuBa0y7_Q==/lib/x86/libmonosgen-2.0.so (mono_loader_lock+71) (BuildId: b67e93dd750dafdd6f65f408b021b6a3a74868ac)
05-14 08:06:12.849 31274 31274 F DEBUG   :       #11 pc 000447a1  /data/app/~~rMrkpKmVPaBpM5jKb8fPAg==/com.microsoft.maui-JfRo8RWSDJaNtJuBa0y7_Q==/lib/x86/libmonosgen-2.0.so (mono_class_create_from_typedef+129) (BuildId: b67e93dd750dafdd6f65f408b021b6a3a74868ac)
05-14 08:06:12.849 31274 31274 F DEBUG   :       #12 pc 0003c073  /data/app/~~rMrkpKmVPaBpM5jKb8fPAg==/com.microsoft.maui-JfRo8RWSDJaNtJuBa0y7_Q==/lib/x86/libmonosgen-2.0.so (mono_class_get_checked+99) (BuildId: b67e93dd750dafdd6f65f408b021b6a3a74868ac)
05-14 08:06:12.849 31274 31274 F DEBUG   :       #13 pc 0003cc0f  /data/app/~~rMrkpKmVPaBpM5jKb8fPAg==/com.microsoft.maui-JfRo8RWSDJaNtJuBa0y7_Q==/lib/x86/libmonosgen-2.0.so (mono_class_from_name_checked_aux+735) (BuildId: b67e93dd750dafdd6f65f408b021b6a3a74868ac)
05-14 08:06:12.849 31274 31274 F DEBUG   :       #14 pc 00037989  /data/app/~~rMrkpKmVPaBpM5jKb8fPAg==/com.microsoft.maui-JfRo8RWSDJaNtJuBa0y7_Q==/lib/x86/libmonosgen-2.0.so (mono_class_from_name_checked+73) (BuildId: b67e93dd750dafdd6f65f408b021b6a3a74868ac)
05-14 08:06:12.849 31274 31274 F DEBUG   :       #15 pc 000cc5f4  /data/app/~~rMrkpKmVPaBpM5jKb8fPAg==/com.microsoft.maui-JfRo8RWSDJaNtJuBa0y7_Q==/lib/x86/libmonosgen-2.0.so (mono_reflection_get_type_internal+132) (BuildId: b67e93dd750dafdd6f65f408b021b6a3a74868ac)
05-14 08:06:12.849 31274 31274 F DEBUG   :       #16 pc 000c9bce  /data/app/~~rMrkpKmVPaBpM5jKb8fPAg==/com.microsoft.maui-JfRo8RWSDJaNtJuBa0y7_Q==/lib/x86/libmonosgen-2.0.so (mono_reflection_get_type_with_rootimage+126) (BuildId: b67e93dd750dafdd6f65f408b021b6a3a74868ac)
05-14 08:06:12.849 31274 31274 F DEBUG   :       #17 pc 000ca204  /data/app/~~rMrkpKmVPaBpM5jKb8fPAg==/com.microsoft.maui-JfRo8RWSDJaNtJuBa0y7_Q==/lib/x86/libmonosgen-2.0.so (_mono_reflection_get_type_from_info+292) (BuildId: b67e93dd750dafdd6f65f408b021b6a3a74868ac)
05-14 08:06:12.849 31274 31274 F DEBUG   :       #18 pc 000ca06e  /data/app/~~rMrkpKmVPaBpM5jKb8fPAg==/com.microsoft.maui-JfRo8RWSDJaNtJuBa0y7_Q==/lib/x86/libmonosgen-2.0.so (mono_reflection_type_from_name_checked+334) (BuildId: b67e93dd750dafdd6f65f408b021b6a3a74868ac)
05-14 08:06:12.849 31274 31274 F DEBUG   :       #19 pc 000c9f01  /data/app/~~rMrkpKmVPaBpM5jKb8fPAg==/com.microsoft.maui-JfRo8RWSDJaNtJuBa0y7_Q==/lib/x86/libmonosgen-2.0.so (mono_reflection_type_from_name+49) (BuildId: b67e93dd750dafdd6f65f408b021b6a3a74868ac)
05-14 08:06:12.849 31274 31274 F DEBUG   :       #20 pc 0001b40b  /data/app/~~rMrkpKmVPaBpM5jKb8fPAg==/com.microsoft.maui-JfRo8RWSDJaNtJuBa0y7_Q==/lib/x86/libmonodroid.so (xamarin::android::internal::EmbeddedAssemblies::typemap_java_to_managed(char const*)+427) (BuildId: 9726f32ad5f8fa5e7c5762baf2f6e3294da41cc1)
05-14 08:06:12.849 31274 31274 F DEBUG   :       #21 pc 0001b551  /data/app/~~rMrkpKmVPaBpM5jKb8fPAg==/com.microsoft.maui-JfRo8RWSDJaNtJuBa0y7_Q==/lib/x86/libmonodroid.so (xamarin::android::internal::EmbeddedAssemblies::typemap_java_to_managed(_MonoString*)+113) (BuildId: 9726f32ad5f8fa5e7c5762baf2f6e3294da41cc1)
05-14 08:06:12.849 31274 31274 F DEBUG   :       #22 pc 000211a7  /data/app/~~rMrkpKmVPaBpM5jKb8fPAg==/com.microsoft.maui-JfRo8RWSDJaNtJuBa0y7_Q==/lib/x86/libmonodroid.so (xamarin::android::internal::MonodroidRuntime::typemap_java_to_managed(_MonoString*)+39) (BuildId: 9726f32ad5f8fa5e7c5762baf2f6e3294da41cc1)
```
  • Loading branch information
lambdageek authored May 18, 2021
1 parent 0d6fcd5 commit d59db3b
Showing 1 changed file with 45 additions and 11 deletions.
56 changes: 45 additions & 11 deletions src/mono/mono/metadata/reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,13 @@ mono_assembly_get_object_handle (MonoAssembly *assembly, MonoError *error)
MonoReflectionModule*
mono_module_get_object (MonoDomain *domain, MonoImage *image)
{
MonoReflectionModuleHandle result;
HANDLE_FUNCTION_ENTER ();
MONO_ENTER_GC_UNSAFE;
ERROR_DECL (error);
MonoReflectionModuleHandle result = mono_module_get_object_handle (image, error);
result = mono_module_get_object_handle (image, error);
mono_error_cleanup (error);
MONO_EXIT_GC_UNSAFE;
HANDLE_FUNCTION_RETURN_OBJ (result);
}

Expand Down Expand Up @@ -308,10 +311,13 @@ mono_module_get_object_handle (MonoImage *image, MonoError *error)
MonoReflectionModule*
mono_module_file_get_object (MonoDomain *domain, MonoImage *image, int table_index)
{
MonoReflectionModuleHandle result;
HANDLE_FUNCTION_ENTER ();
MONO_ENTER_GC_UNSAFE;
ERROR_DECL (error);
MonoReflectionModuleHandle result = mono_module_file_get_object_handle (image, table_index, error);
result = mono_module_file_get_object_handle (image, table_index, error);
mono_error_cleanup (error);
MONO_EXIT_GC_UNSAFE;
HANDLE_FUNCTION_RETURN_OBJ (result);
}

Expand Down Expand Up @@ -692,10 +698,13 @@ mono_method_clear_object (MonoMethod *method)
MonoReflectionField*
mono_field_get_object (MonoDomain *domain, MonoClass *klass, MonoClassField *field)
{
MonoReflectionFieldHandle result;
HANDLE_FUNCTION_ENTER ();
MONO_ENTER_GC_UNSAFE;
ERROR_DECL (error);
MonoReflectionFieldHandle result = mono_field_get_object_handle (klass, field, error);
result = mono_field_get_object_handle (klass, field, error);
mono_error_cleanup (error);
MONO_EXIT_GC_UNSAFE;
HANDLE_FUNCTION_RETURN_OBJ (result);
}

Expand Down Expand Up @@ -770,10 +779,13 @@ mono_field_get_object_checked (MonoClass *klass, MonoClassField *field, MonoErro
MonoReflectionProperty*
mono_property_get_object (MonoDomain *domain, MonoClass *klass, MonoProperty *property)
{
MonoReflectionPropertyHandle result;
HANDLE_FUNCTION_ENTER ();
MONO_ENTER_GC_UNSAFE;
ERROR_DECL (error);
MonoReflectionPropertyHandle result = mono_property_get_object_handle (klass, property, error);
result = mono_property_get_object_handle (klass, property, error);
mono_error_cleanup (error);
MONO_EXIT_GC_UNSAFE;
HANDLE_FUNCTION_RETURN_OBJ (result);
}

Expand Down Expand Up @@ -834,10 +846,13 @@ mono_property_get_object_checked (MonoClass *klass, MonoProperty *property, Mono
MonoReflectionEvent*
mono_event_get_object (MonoDomain *domain, MonoClass *klass, MonoEvent *event)
{
MonoReflectionEventHandle result;
HANDLE_FUNCTION_ENTER ();
MONO_ENTER_GC_UNSAFE;
ERROR_DECL (error);
MonoReflectionEventHandle result = mono_event_get_object_handle (klass, event, error);
result = mono_event_get_object_handle (klass, event, error);
mono_error_cleanup (error);
MONO_EXIT_GC_UNSAFE;
HANDLE_FUNCTION_RETURN_OBJ (result);
}

Expand Down Expand Up @@ -1134,10 +1149,13 @@ mono_param_get_objects_internal (MonoMethod *method, MonoClass *refclass, MonoEr
MonoArray*
mono_param_get_objects (MonoDomain *domain, MonoMethod *method)
{
MonoArrayHandle result;
HANDLE_FUNCTION_ENTER ();
MONO_ENTER_GC_UNSAFE;
ERROR_DECL (error);
MonoArrayHandle result = mono_param_get_objects_internal (method, NULL, error);
result = mono_param_get_objects_internal (method, NULL, error);
mono_error_assert_ok (error);
MONO_EXIT_GC_UNSAFE;
HANDLE_FUNCTION_RETURN_OBJ (result);
}

Expand Down Expand Up @@ -1202,10 +1220,13 @@ add_exception_handling_clause_to_array (MonoMethodHeader *header, int idx, MonoA
MonoReflectionMethodBody*
mono_method_body_get_object (MonoDomain *domain, MonoMethod *method)
{
MonoReflectionMethodBodyHandle result;
HANDLE_FUNCTION_ENTER ();
MONO_ENTER_GC_UNSAFE;
ERROR_DECL (error);
MonoReflectionMethodBodyHandle result = mono_method_body_get_object_handle (method, error);
result = mono_method_body_get_object_handle (method, error);
mono_error_cleanup (error);
MONO_EXIT_GC_UNSAFE;
HANDLE_FUNCTION_RETURN_OBJ (result);
}

Expand Down Expand Up @@ -1902,9 +1923,12 @@ mono_identifier_unescape_info (MonoTypeNameParse *info)
int
mono_reflection_parse_type (char *name, MonoTypeNameParse *info)
{
gboolean result;
MONO_ENTER_GC_UNSAFE;
ERROR_DECL (error);
gboolean result = mono_reflection_parse_type_checked (name, info, error);
result = mono_reflection_parse_type_checked (name, info, error);
mono_error_cleanup (error);
MONO_EXIT_GC_UNSAFE;
return result ? 1 : 0;
}

Expand Down Expand Up @@ -2133,9 +2157,12 @@ mono_reflection_get_type_internal (MonoAssemblyLoadContext *alc, MonoImage *root
MonoType*
mono_reflection_get_type (MonoImage* image, MonoTypeNameParse *info, gboolean ignorecase, gboolean *type_resolve)
{
MonoType *result;
MONO_ENTER_GC_UNSAFE;
ERROR_DECL (error);
MonoType *result = mono_reflection_get_type_with_rootimage (mono_alc_get_default (), image, image, info, ignorecase, TRUE, type_resolve, error);
result = mono_reflection_get_type_with_rootimage (mono_alc_get_default (), image, image, info, ignorecase, TRUE, type_resolve, error);
mono_error_cleanup (error);
MONO_EXIT_GC_UNSAFE;
return result;
}

Expand Down Expand Up @@ -2333,13 +2360,16 @@ mono_reflection_free_type_info (MonoTypeNameParse *info)
MonoType*
mono_reflection_type_from_name (char *name, MonoImage *image)
{
MonoType *result;
MONO_ENTER_GC_UNSAFE;
ERROR_DECL (error);

MonoAssemblyLoadContext *alc = mono_alc_get_default ();

MonoType * const result = mono_reflection_type_from_name_checked (name, alc, image, error);
result = mono_reflection_type_from_name_checked (name, alc, image, error);

mono_error_cleanup (error);
MONO_EXIT_GC_UNSAFE;
return result;
}

Expand Down Expand Up @@ -2385,11 +2415,15 @@ mono_reflection_type_from_name_checked (char *name, MonoAssemblyLoadContext *alc
guint32
mono_reflection_get_token (MonoObject *obj_raw)
{
guint32 result;
HANDLE_FUNCTION_ENTER ();
MONO_ENTER_GC_UNSAFE;
MONO_HANDLE_DCL (MonoObject, obj);
ERROR_DECL (error);
guint32 result = mono_reflection_get_token_checked (obj, error);
result = mono_reflection_get_token_checked (obj, error);
mono_error_assert_ok (error);

MONO_EXIT_GC_UNSAFE;
HANDLE_FUNCTION_RETURN_VAL (result);
}

Expand Down

0 comments on commit d59db3b

Please sign in to comment.