Skip to content

Commit 9134ab0

Browse files
committed
fixups to pass new tests from #88268
1 parent f923a2f commit 9134ab0

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/mono/mono/metadata/marshal-lightweight.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2354,6 +2354,10 @@ emit_unsafe_accessor_field_wrapper (MonoMethodBuilder *mb, MonoMethod *accessor_
23542354
mono_mb_emit_exception_full (mb, "System", "MissingFieldException", g_strdup_printf("UnsafeAccessorKind does not match expected static modifier on field '%s' in '%s'", member_name, m_class_get_name (target_class)));
23552355
return;
23562356
}
2357+
if (is_field_static && m_field_get_parent (target_field) != target_class) {
2358+
// don't look up static fields using the inheritance hierarchy
2359+
mono_mb_emit_exception_full (mb, "System", "MissingFieldException", g_strdup_printf("Field '%s' not found in '%s'", member_name, m_class_get_name (target_class)));
2360+
}
23572361

23582362
if (kind == MONO_UNSAFE_ACCESSOR_FIELD)
23592363
mono_mb_emit_ldarg (mb, 0);
@@ -2427,7 +2431,8 @@ emit_unsafe_accessor_ctor_wrapper (MonoMethodBuilder *mb, MonoMethod *accessor_m
24272431
{
24282432
// TODO: support coreCLR's weird "you can call the .ctor as a normal instance member" method
24292433
g_assert (kind == MONO_UNSAFE_ACCESSOR_CTOR);
2430-
if (!member_name)
2434+
// null or empty string member name is ok for a constructor
2435+
if (!member_name || member_name[0] == '\0')
24312436
member_name = ".ctor";
24322437
if (strcmp (member_name, ".ctor") != 0) {
24332438
mono_mb_emit_exception_full (mb, "System", "BadImageFormatException", "Invalid UnsafeAccessorAttribute for constructor.");
@@ -2479,6 +2484,11 @@ emit_unsafe_accessor_method_wrapper (MonoMethodBuilder *mb, MonoMethod *accessor
24792484

24802485
MonoClass *target_class = mono_class_from_mono_type_internal (target_type);
24812486

2487+
if (hasthis && m_class_is_valuetype (target_class) && !m_type_is_byref (target_type)) {
2488+
// If the non-static method access is for a value type, the instance must be byref.
2489+
mono_mb_emit_exception_full (mb, "System", "BadImageFormatException", "Invalid usage of UnsafeAccessorAttribute.");
2490+
}
2491+
24822492
ERROR_DECL(find_method_error);
24832493
MonoClass *in_class = mono_class_is_ginst (target_class) ? mono_class_get_generic_class (target_class)->container_class : target_class;
24842494
MonoMethod *target_method = mono_unsafe_accessor_find_method (in_class, member_name, member_sig, target_class, find_method_error);
@@ -2489,7 +2499,11 @@ emit_unsafe_accessor_method_wrapper (MonoMethodBuilder *mb, MonoMethod *accessor
24892499
return;
24902500
}
24912501
g_assert (target_method);
2492-
g_assert (target_method->klass == target_class);
2502+
if (!hasthis && target_method->klass != target_class) {
2503+
emit_missing_method_error (mb, find_method_error, member_name);
2504+
return;
2505+
}
2506+
g_assert (target_method->klass == target_class); // are instance methods allowed to be looked up using inheritance?
24932507

24942508
emit_unsafe_accessor_ldargs (mb, sig, !hasthis ? 1 : 0);
24952509

0 commit comments

Comments
 (0)