Skip to content

Commit 0be3889

Browse files
authored
[mono] Disable gsharing when Unsafe.ReadUnaligned/WriteUnaligned () is used with generic structures. (#89417)
Fixes #89398. For a method like ``` static void Write<T>(ref byte b, T value) => Unsafe.WriteUnaligned<T>(ref b, value); ``` And an instance ```Write<GStruct<string>>```, generic sharing will create a ```Write<T_INST>``` instance where T_INST is constrained to GStruct<T_REF>. The JIT currently calls ```mini_get_underlying_type ()``` in many places which transform T_INST into GStruct<T_REF>. This causes problems at runtime in the generic sharing code, which expects to find T_INST. I.e. ```inflate_info ()``` can inflate ```T_INST``` to ```GStruct<string>```, but it can't inflate ```GStruct<T_REF>``` to ```GStruct<string>```. As a workaround, disable gsharing in (some) of these cases.
1 parent 2300d0e commit 0be3889

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/mono/mono/mini/intrinsics.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,8 @@ MONO_RESTORE_WARNING
554554

555555
t = ctx->method_inst->type_argv [0];
556556
t = mini_get_underlying_type (t);
557+
if (cfg->gshared && t != ctx->method_inst->type_argv [0] && MONO_TYPE_ISSTRUCT (t) && mono_class_check_context_used (mono_class_from_mono_type_internal (t)))
558+
cfg->prefer_instances = TRUE;
557559
return mini_emit_memory_load (cfg, t, args [0], 0, MONO_INST_UNALIGNED);
558560
} else if (!strcmp (cmethod->name, "WriteUnaligned")) {
559561
g_assert (ctx);
@@ -562,7 +564,10 @@ MONO_RESTORE_WARNING
562564
g_assert (fsig->param_count == 2);
563565

564566
t = ctx->method_inst->type_argv [0];
567+
565568
t = mini_get_underlying_type (t);
569+
if (cfg->gshared && t != ctx->method_inst->type_argv [0] && MONO_TYPE_ISSTRUCT (t) && mono_class_check_context_used (mono_class_from_mono_type_internal (t)))
570+
cfg->prefer_instances = TRUE;
566571
mini_emit_memory_store (cfg, t, args [0], args [1], MONO_INST_UNALIGNED);
567572
MONO_INST_NEW (cfg, ins, OP_NOP);
568573
MONO_ADD_INS (cfg->cbb, ins);

0 commit comments

Comments
 (0)