From 3871b5ad8d1e2712989854b48ac022aab6930d76 Mon Sep 17 00:00:00 2001 From: Jan Dupej Date: Fri, 9 Dec 2022 14:34:41 +0100 Subject: [PATCH 1/4] [mono][llvm] Added a null-check for 2-dim array accessors, addressing #79022 --- src/mono/mono/mini/method-to-ir.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/mono/mono/mini/method-to-ir.c b/src/mono/mono/mini/method-to-ir.c index 16c5c9925892c9..8485efa0148369 100644 --- a/src/mono/mono/mini/method-to-ir.c +++ b/src/mono/mono/mini/method-to-ir.c @@ -4321,6 +4321,11 @@ mini_emit_ldelema_2_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, Mono // FIXME: Do we need to do something here for i8 indexes, like in ldelema_1_ins ? #endif + if (COMPILE_LLVM (cfg)) { + // null checking in LLVM to address https://github.com/dotnet/runtime/issues/79022 + MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, arr->dreg, 0); + MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException"); + } /* range checking */ MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg, arr->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds)); From b60793fe3e7f66311cffd03314fd20c4c591c073 Mon Sep 17 00:00:00 2001 From: Jan Dupej Date: Fri, 9 Dec 2022 15:27:21 +0100 Subject: [PATCH 2/4] [mono][llvm] Adding a regression test for issue https://github.com/dotnet/runtime/issues/79022 --- .../coreclr/GitHub_79022/test79022.cs | 38 +++++++++++++++++++ .../coreclr/GitHub_79022/test79022.csproj | 10 +++++ 2 files changed, 48 insertions(+) create mode 100644 src/tests/Regressions/coreclr/GitHub_79022/test79022.cs create mode 100644 src/tests/Regressions/coreclr/GitHub_79022/test79022.csproj diff --git a/src/tests/Regressions/coreclr/GitHub_79022/test79022.cs b/src/tests/Regressions/coreclr/GitHub_79022/test79022.cs new file mode 100644 index 00000000000000..d3dc57e0b1390a --- /dev/null +++ b/src/tests/Regressions/coreclr/GitHub_79022/test79022.cs @@ -0,0 +1,38 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System; +using System.Runtime.CompilerServices; + +class Program +{ + // This is a regression test for https://github.com/dotnet/runtime/issues/79022 + static ulong[,] s_1; + + [MethodImpl(MethodImplOptions.NoInlining)] + static void Test() + { + try + { + ushort vr10 = default(ushort); + bool vr11 = 0 < ((s_1[0, 0] * (uint)(0 / vr10)) % 1); + } + catch + { + } + } + + public static int Main() + { + try + { + Test(); + } + catch + { + return -1; + } + + return 100; + } +} diff --git a/src/tests/Regressions/coreclr/GitHub_79022/test79022.csproj b/src/tests/Regressions/coreclr/GitHub_79022/test79022.csproj new file mode 100644 index 00000000000000..370aed7d68b591 --- /dev/null +++ b/src/tests/Regressions/coreclr/GitHub_79022/test79022.csproj @@ -0,0 +1,10 @@ + + + Exe + 1 + CS0649 + + + + + From 9511f60e4b19e62e01e61966aac47da82b8cda7d Mon Sep 17 00:00:00 2001 From: Jan Dupej Date: Mon, 19 Dec 2022 10:54:13 +0100 Subject: [PATCH 3/4] [mono][llvm] Null check for 2D arrays is now more DRY. --- src/mono/mono/mini/method-to-ir.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/mono/mono/mini/method-to-ir.c b/src/mono/mono/mini/method-to-ir.c index 8485efa0148369..5080796c84a533 100644 --- a/src/mono/mono/mini/method-to-ir.c +++ b/src/mono/mono/mini/method-to-ir.c @@ -4321,15 +4321,16 @@ mini_emit_ldelema_2_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, Mono // FIXME: Do we need to do something here for i8 indexes, like in ldelema_1_ins ? #endif + /* range checking */ if (COMPILE_LLVM (cfg)) { // null checking in LLVM to address https://github.com/dotnet/runtime/issues/79022 - MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, arr->dreg, 0); - MONO_EMIT_NEW_COND_EXC (cfg, EQ, "NullReferenceException"); + MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, bounds_reg, + arr->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds)); + } else { + MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg, + arr->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds)); } - /* range checking */ - MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg, - arr->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds)); - + MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low1_reg, bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound)); MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx1_reg, index1, low1_reg); From ab04bd56c66e42d983bec672d68538101f519588 Mon Sep 17 00:00:00 2001 From: Jan Dupej Date: Tue, 20 Dec 2022 10:24:21 +0100 Subject: [PATCH 4/4] [mono][llvm] Null-checking on 2D array access, removed restriction to LLVM. --- src/mono/mono/mini/method-to-ir.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/mono/mono/mini/method-to-ir.c b/src/mono/mono/mini/method-to-ir.c index 5080796c84a533..fbfa0c6ad937c3 100644 --- a/src/mono/mono/mini/method-to-ir.c +++ b/src/mono/mono/mini/method-to-ir.c @@ -4322,15 +4322,9 @@ mini_emit_ldelema_2_ins (MonoCompile *cfg, MonoClass *klass, MonoInst *arr, Mono #endif /* range checking */ - if (COMPILE_LLVM (cfg)) { - // null checking in LLVM to address https://github.com/dotnet/runtime/issues/79022 - MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, bounds_reg, - arr->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds)); - } else { - MONO_EMIT_NEW_LOAD_MEMBASE (cfg, bounds_reg, - arr->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds)); - } - + MONO_EMIT_NEW_LOAD_MEMBASE_FAULT (cfg, bounds_reg, + arr->dreg, MONO_STRUCT_OFFSET (MonoArray, bounds)); + MONO_EMIT_NEW_LOAD_MEMBASE_OP (cfg, OP_LOADI4_MEMBASE, low1_reg, bounds_reg, MONO_STRUCT_OFFSET (MonoArrayBounds, lower_bound)); MONO_EMIT_NEW_BIALU (cfg, OP_PSUB, realidx1_reg, index1, low1_reg);