Skip to content

Commit e1f98a1

Browse files
authored
Generic types are invalid target types (#101850)
1 parent 5b11bb2 commit e1f98a1

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,6 +2355,8 @@ unsafe_accessor_target_type_forbidden (MonoType *target_type)
23552355
case MONO_TYPE_VOID:
23562356
case MONO_TYPE_PTR:
23572357
case MONO_TYPE_FNPTR:
2358+
case MONO_TYPE_VAR:
2359+
case MONO_TYPE_MVAR:
23582360
return TRUE;
23592361
default:
23602362
return FALSE;
@@ -2422,16 +2424,17 @@ emit_unsafe_accessor_ctor_wrapper (MonoMethodBuilder *mb, MonoMethod *accessor_m
24222424
}
24232425

24242426
MonoType *target_type = sig->ret; // for constructors the return type is the target type
2425-
if (target_type == NULL || m_type_is_byref (target_type) || unsafe_accessor_target_type_forbidden (target_type)) {
2426-
mono_mb_emit_exception_full (mb, "System", "BadImageFormatException", "Invalid usage of UnsafeAccessorAttribute.");
2427-
return;
2428-
}
2429-
24302427
MonoClass *target_class = mono_class_from_mono_type_internal (target_type);
24312428

24322429
ERROR_DECL(find_method_error);
24332430
if (accessor_method->is_inflated) {
24342431
sig = update_signature(accessor_method);
2432+
target_type = sig->ret;
2433+
}
2434+
2435+
if (target_type == NULL || m_type_is_byref (target_type) || unsafe_accessor_target_type_forbidden (target_type)) {
2436+
mono_mb_emit_exception_full (mb, "System", "BadImageFormatException", "Invalid usage of UnsafeAccessorAttribute.");
2437+
return;
24352438
}
24362439

24372440
MonoMethodSignature *member_sig = ctor_sig_from_accessor_sig (mb, sig, ctx);
@@ -2467,10 +2470,7 @@ emit_unsafe_accessor_method_wrapper (MonoMethodBuilder *mb, MonoMethod *accessor
24672470
// We explicitly allow calling a constructor as if it was an instance method, but we need some hacks in a couple of places
24682471
gboolean ctor_as_method = !strcmp (member_name, ".ctor");
24692472

2470-
if (sig->param_count < 1 || sig->params[0] == NULL || unsafe_accessor_target_type_forbidden (sig->params[0])) {
2471-
mono_mb_emit_exception_full (mb, "System", "BadImageFormatException", "Invalid usage of UnsafeAccessorAttribute.");
2472-
return;
2473-
}
2473+
24742474

24752475
MonoType *target_type = sig->params[0];
24762476
gboolean hasthis = kind == MONO_UNSAFE_ACCESSOR_METHOD;
@@ -2484,6 +2484,12 @@ emit_unsafe_accessor_method_wrapper (MonoMethodBuilder *mb, MonoMethod *accessor
24842484
ERROR_DECL(find_method_error);
24852485
if (accessor_method->is_inflated) {
24862486
sig = update_signature(accessor_method);
2487+
target_type = sig->params[0];
2488+
}
2489+
2490+
if (sig->param_count < 1 || target_type == NULL || unsafe_accessor_target_type_forbidden (target_type)) {
2491+
mono_mb_emit_exception_full (mb, "System", "BadImageFormatException", "Invalid usage of UnsafeAccessorAttribute.");
2492+
return;
24872493
}
24882494

24892495
MonoMethodSignature *member_sig = method_sig_from_accessor_sig (mb, hasthis, sig, ctx);

src/tests/baseservices/compilerservices/UnsafeAccessors/UnsafeAccessorsTests.Generics.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,6 @@ class Invalid<T>
437437
}
438438

439439
[Fact]
440-
[ActiveIssue("https://github.com/dotnet/runtime/issues/89439", TestRuntimes.Mono)]
441440
public static void Verify_Generic_InvalidUseUnsafeAccessor()
442441
{
443442
Console.WriteLine($"Running {nameof(Verify_Generic_InvalidUseUnsafeAccessor)}");

0 commit comments

Comments
 (0)