Skip to content

Commit 1c0d595

Browse files
committed
Ensure that Mono doesn't assert for bounds checked indices
1 parent a1bab9a commit 1c0d595

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/mono/mono/mini/simd-intrinsics.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2553,6 +2553,9 @@ emit_sri_vector (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsi
25532553
if (index < 0 || index >= elems) {
25542554
MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, args [1]->dreg, elems);
25552555
MONO_EMIT_NEW_COND_EXC (cfg, GE_UN, "ArgumentOutOfRangeException");
2556+
2557+
// Fixup the index to be in range so codegen is still valid
2558+
index %= elems;
25562559
}
25572560

25582561
// Bounds check is elided if we know the index is safe.
@@ -3220,8 +3223,11 @@ emit_sri_vector (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *fsi
32203223
// If the index is provably a constant, we can generate vastly better code.
32213224
int index = GTMREG_TO_INT (args[1]->inst_c0);
32223225
if (index < 0 || index >= elems) {
3223-
MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, args [1]->dreg, elems);
3224-
MONO_EMIT_NEW_COND_EXC (cfg, GE_UN, "ArgumentOutOfRangeException");
3226+
MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, args [1]->dreg, elems);
3227+
MONO_EMIT_NEW_COND_EXC (cfg, GE_UN, "ArgumentOutOfRangeException");
3228+
3229+
// Fixup the index to be in range so codegen is still valid
3230+
index %= elems;
32253231
}
32263232

32273233
return emit_vector_insert_element (cfg, klass, args [0], arg0_type, args [2], index, FALSE);
@@ -3766,6 +3772,9 @@ emit_vector_2_3_4 (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *f
37663772
if (index < 0 || index >= len) {
37673773
MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, args [1]->dreg, len);
37683774
MONO_EMIT_NEW_COND_EXC (cfg, GE_UN, "ArgumentOutOfRangeException");
3775+
3776+
// Fixup the index to be in range so codegen is still valid
3777+
index %= len;
37693778
}
37703779

37713780
int opcode = type_to_extract_op (ty);
@@ -3852,6 +3861,9 @@ emit_vector_2_3_4 (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignature *f
38523861
if (index < 0 || index >= len) {
38533862
MONO_EMIT_NEW_BIALU_IMM (cfg, OP_COMPARE_IMM, -1, args [1]->dreg, len);
38543863
MONO_EMIT_NEW_COND_EXC (cfg, GE_UN, "ArgumentOutOfRangeException");
3864+
3865+
// Fixup the index to be in range so codegen is still valid
3866+
index %= len;
38553867
}
38563868

38573869
if (args [0]->dreg == dreg) {

0 commit comments

Comments
 (0)