Skip to content

Runtime unable to handle certain IL #1194

Closed
@borrrden

Description

@borrrden

Steps to Reproduce

Consider the following struct:

unsafe partial struct C4BlobKey
{
    public fixed byte bytes[20];
}

and a corresponding C struct

typedef struct C4BlobKey {
    uint8_t bytes[20];
} C4BlobKey;

I want to determine equality so I write the following Equals() function:

public override bool Equals(object obj)
{
    if(!(obj is C4BlobKey)) {
        return false;
    }

    var other = (C4BlobKey)obj;
    fixed(byte* b = bytes) {
        for(int i = 0; i < _Size; i++) {
            if(b[i] != other.bytes[i]) {
                return false;
            }
        }
    }

    return true;
 }

Debug and release mode both have the same following symptoms, but obviously with different lines since the release mode is optimized.

Invalid IL code in LiteCore.Interop.C4BlobKey.Equals (object); IL_001c: stloc.2

In fact I have to instances of this type of class that do similar things and they both fail in the same way. Here is the IL code (up to around the line in question):

.method public hidebysig virtual instance bool 
Equals(
  object obj
) cil managed 
{
.maxstack 3
.locals init (
  [0] valuetype LiteCore.Interop.C4BlobKey other,
  [1] unsigned int8* b,
  [2] unsigned int8*& pinned V_2,
  [3] int32 i
)

// [51 13 - 51 36]
IL_0000: ldarg.1      // obj
IL_0001: isinst       LiteCore.Interop.C4BlobKey
IL_0006: brtrue.s     IL_000a

// [52 17 - 52 30]
IL_0008: ldc.i4.0     
IL_0009: ret          

// [55 13 - 55 40]
IL_000a: ldarg.1      // obj
IL_000b: unbox.any    LiteCore.Interop.C4BlobKey
IL_0010: stloc.0      // other
IL_0011: ldarg.0      // this
IL_0012: ldflda       valuetype LiteCore.Interop.C4BlobKey/'<bytes>e__FixedBuffer' LiteCore.Interop.C4BlobKey::bytes
IL_0017: ldflda       unsigned int8 LiteCore.Interop.C4BlobKey/'<bytes>e__FixedBuffer'::FixedElementField
IL_001c: stloc.2      // V_2

// [56 19 - 56 34]
IL_001d: ldloc.2      // V_2
IL_001e: conv.u       
IL_001f: stloc.1      // b

// [57 21 - 57 30]
IL_0020: ldc.i4.0     
IL_0021: stloc.3      // i

IL_0022: br.s         IL_0040
// start of loop, entry point: IL_0040

This same IL works fine on UWP, and .NET Core. This problem also appears to exist in Xamarin iOS.

Expected Behavior

The IL gets handled as on other platforms

Actual Behavior

The above exception

Version Information

Microsoft Visual Studio Community 2017
Version 15.5.3
VisualStudio.15.Release/15.5.3+27130.2020
Microsoft .NET Framework
Version 4.7.02556

Installed Version: Community

Visual Basic 2017 00369-60000-00001-AA631
Microsoft Visual Basic 2017

Visual C# 2017 00369-60000-00001-AA631
Microsoft Visual C# 2017

Visual C++ 2017 00369-60000-00001-AA631
Microsoft Visual C++ 2017

Visual F# 4.1 00369-60000-00001-AA631
Microsoft Visual F# 4.1

Application Insights Tools for Visual Studio Package 8.10.01106.1
Application Insights Tools for Visual Studio

ASP.NET and Web Tools 2017 15.0.31127.0
ASP.NET and Web Tools 2017

ASP.NET Core Razor Language Services 1.0
Provides languages services for ASP.NET Core Razor.

ASP.NET Web Frameworks and Tools 2012 4.0.20601.0
For additional information, visit https://www.asp.net/

ASP.NET Web Frameworks and Tools 2017 5.2.51007.0
For additional information, visit https://www.asp.net/

Azure App Service Tools v3.0.0 15.0.31106.0
Azure App Service Tools v3.0.0

Common Azure Tools 1.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

JavaScript Language Service 2.0
JavaScript Language Service

JavaScript Project System 2.0
JavaScript Project System

JavaScript UWP Project System 2.0
JavaScript UWP Project System

JetBrains ReSharper Ultimate 2017.3.1 Build 111.0.20171221.145902
JetBrains ReSharper Ultimate package for Microsoft Visual Studio. For more information about ReSharper Ultimate, visit http://www.jetbrains.com/resharper. Copyright © 2018 JetBrains, Inc.

Merq 1.1.17-rc (cba4571)
Command Bus, Event Stream and Async Manager for Visual Studio extensions.

Microsoft Continuous Delivery Tools for Visual Studio 0.3
Simplifying the configuration of continuous build integration and continuous build delivery from within the Visual Studio IDE.

Microsoft JVM Debugger 1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft MI-Based Debugger 1.0
Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ Wizards 1.0
Microsoft Visual C++ Wizards

Microsoft Visual Studio VC Package 1.0
Microsoft Visual Studio VC Package

Mono Debugging for Visual Studio 4.8.4-pre (3fe64e3)
Support for debugging Mono processes with Visual Studio.

NuGet Package Manager 4.5.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

Project System Tools 1.0
Tools for working with C#, VisualBasic, and F# projects.

SQL Server Data Tools 15.1.61710.120
Microsoft SQL Server Data Tools

TypeScript Tools 15.5.11025.1
TypeScript Tools for Microsoft Visual Studio

Visual Studio Code Debug Adapter Host Package 1.0
Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Tools for CMake 1.0
Visual Studio Tools for CMake

Visual Studio Tools for Universal Windows Apps 15.0.27130.2020
The Visual Studio Tools for Universal Windows apps allow you to build a single universal app experience that can reach every device running Windows 10: phone, tablet, PC, and more. It includes the Microsoft Windows 10 Software Development Kit.

VisualStudio.Mac 1.0
Mac Extension for Visual Studio

Xamarin 4.8.0.757 (7f9ec2a)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer 4.8.188 (c5813fa34)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin.Android SDK 8.1.3.0 (HEAD/ef47226b7)
Xamarin.Android Reference Assemblies and MSBuild support.

Xamarin.iOS and Xamarin.Mac SDK 11.6.1.2 (6857dfc)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

Log File

Nothing of interest. This exception was caught and reported by our unit test runner.

Metadata

Metadata

Assignees

Labels

Area: Mono RuntimeMono-related issues: BCL bugs, AOT issues, etc.bugComponent does not function as intended.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions