Add test coverage for casting to pointers (dotnet/coreclr#26698)
authorMichal Strehovský <MichalStrehovsky@users.noreply.github.com>
Fri, 13 Sep 2019 15:47:03 +0000 (17:47 +0200)
committerGitHub <noreply@github.com>
Fri, 13 Sep 2019 15:47:03 +0000 (17:47 +0200)
I was fixing a customer-reported bug related to this in .NET Native and decided to check whether there's any test coverage for this in CoreCLR. There isn't, so adding a test.

Commit migrated from https://github.com/dotnet/coreclr/commit/3f8d0e92ada4f1bc37e93c644054acf0a43692cf

src/coreclr/tests/src/Loader/classloader/Casting/castclasspointer.il [new file with mode: 0644]
src/coreclr/tests/src/Loader/classloader/Casting/castclasspointer.ilproj [new file with mode: 0644]

diff --git a/src/coreclr/tests/src/Loader/classloader/Casting/castclasspointer.il b/src/coreclr/tests/src/Loader/classloader/Casting/castclasspointer.il
new file mode 100644 (file)
index 0000000..8cf7a8e
--- /dev/null
@@ -0,0 +1,40 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+.assembly castclasspointer { }
+.assembly extern mscorlib { }
+
+.method public static int32 Main()
+{
+  .entrypoint
+
+  ldnull
+  castclass void*
+  pop
+
+  newobj instance void [mscorlib]System.Object::.ctor()
+  isinst void*
+  brtrue BAD
+
+  .try
+  {
+    newobj instance void [mscorlib]System.Object::.ctor()
+    castclass void*
+    pop
+    leave BAD
+  }
+  catch [mscorlib]System.InvalidCastException
+  {
+    pop
+    leave OK
+  }
+
+BAD:
+  ldc.i4 1
+  ret
+
+OK:
+  ldc.i4 100
+  ret
+}
diff --git a/src/coreclr/tests/src/Loader/classloader/Casting/castclasspointer.ilproj b/src/coreclr/tests/src/Loader/classloader/Casting/castclasspointer.ilproj
new file mode 100644 (file)
index 0000000..ca22703
--- /dev/null
@@ -0,0 +1,11 @@
+<Project Sdk="Microsoft.NET.Sdk.IL">
+  <PropertyGroup>
+    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
+    <ReferenceLocalMscorlib>true</ReferenceLocalMscorlib>
+    <OutputType>Exe</OutputType>
+    <CLRTestKind>BuildAndRun</CLRTestKind>
+  </PropertyGroup>
+  <ItemGroup>
+    <Compile Include="castclasspointer.il" />
+  </ItemGroup>
+</Project>