Skip to content

Commit 21c07bb

Browse files
committed
Add test locking down mismatched box;isinst;unbox.any pattern
1 parent d586ae2 commit 21c07bb

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/tests/Loader/classloader/generics/ByRefLike/InvalidCSharp.il

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,20 @@
237237
ret
238238
}
239239

240+
.method public hidebysig
241+
instance bool BoxIsinstUnboxAny_Mismatch(!T) cil managed
242+
{
243+
ldarg.1
244+
// Begin sequence
245+
box !T
246+
isinst ByRefLikeType2
247+
unbox.any ByRefLikeType2
248+
// End sequence
249+
pop
250+
ldc.i4.0
251+
ret
252+
}
253+
240254
.method public hidebysig
241255
instance bool BoxIsinstBranch(!T) cil managed
242256
{
@@ -443,6 +457,14 @@
443457
)
444458
}
445459

460+
.class public sequential ansi sealed beforefieldinit ByRefLikeType2
461+
extends [System.Runtime]System.ValueType
462+
{
463+
.custom instance void [System.Runtime]System.Runtime.CompilerServices.IsByRefLikeAttribute::.ctor() = (
464+
01 00 00 00
465+
)
466+
}
467+
446468
.class interface public auto ansi abstract InvalidCSharp.EmptyInterface
447469
{
448470
}
@@ -755,6 +777,22 @@
755777
ret
756778
}
757779

780+
.method public hidebysig static
781+
bool BoxIsinstUnboxAny_Mismatch() cil managed
782+
{
783+
.locals init (
784+
[0] valuetype InvalidCSharp.GenericByRefLike_Over`1<valuetype ByRefLikeType>
785+
)
786+
787+
ldloca.s 0
788+
initobj valuetype InvalidCSharp.GenericByRefLike_Over`1<valuetype ByRefLikeType>
789+
ldloca.s 0
790+
ldloc 0
791+
ldfld !0 valuetype InvalidCSharp.GenericByRefLike_Over`1<valuetype ByRefLikeType>::Field
792+
call instance bool valuetype InvalidCSharp.GenericByRefLike_Over`1<valuetype ByRefLikeType>::BoxIsinstUnboxAny_Mismatch(!0)
793+
ret
794+
}
795+
758796
.method public hidebysig static
759797
bool BoxIsinstBranch() cil managed
760798
{

src/tests/Loader/classloader/generics/ByRefLike/Validate.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ public static void Validate_RecognizedOpCodeSequences_Scenarios()
6767
Assert.True(Exec.BoxIsinstBranch());
6868
}
6969

70+
[Fact]
71+
public static void Validate_RecognizedOpCodeSequences_Mismatch()
72+
{
73+
Console.WriteLine($"{nameof(Validate_RecognizedOpCodeSequences_Mismatch)}...");
74+
75+
// box !T ; isinst S ; unbox.any S should always be guarded by a box !T ; isinst S;
76+
// brtrue/brfalse branch, so if it's ever executed and the types aren't equal that's invalid
77+
Assert.Throws<InvalidProgramException>(() => { Exec.BoxIsinstUnboxAny_Mismatch(); });
78+
}
79+
7080
[Fact]
7181
public static void Validate_InvalidOpCode_Scenarios()
7282
{
@@ -81,6 +91,7 @@ public static void Validate_InvalidOpCode_Scenarios()
8191

8292
// Test that explicitly tries to box a ByRefLike type.
8393
Assert.Throws<InvalidProgramException>(() => { Exec.BoxAsObject(); });
94+
8495
}
8596

8697
[Fact]
@@ -116,4 +127,4 @@ public static void Validate_MemberDiscoveryViaReflection_ForSpanReadOnlySpan()
116127
// Assert.NotNull(m);
117128
// }
118129
}
119-
}
130+
}

0 commit comments

Comments
 (0)