Skip to content

Commit

Permalink
Improve error reporting when Crossgen2 crashes on Managed C++
Browse files Browse the repository at this point in the history
Based on Manish's feedback from another PR I investigated options
to improve Crossgen2 diagnostics in the presence of Managed C++
that currently crashes the metadata reader in several places.
This is the barebones version of such support, centralizing detection
and reporting of the failures. Right now we don't have any pretty
way of reporting localized strings from this low-level code, I'll
welcome any suggestions how to polish the change in that direction.

Thanks

Tomas
  • Loading branch information
trylek committed Jun 18, 2020
1 parent 11d70f5 commit 7793032
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/coreclr/src/tools/Common/TypeSystem/Ecma/EcmaMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Reflection;
Expand Down Expand Up @@ -448,17 +449,32 @@ public override bool IsPInvoke
}
}

public void CheckManagedCPlusPlusPInvoke()
{
Debug.Assert(IsPInvoke);
MethodDefinition methodDef = MetadataReader.GetMethodDefinition(_handle);
MethodImport import = methodDef.GetImport();
if (import.Module.IsNil || MetadataReader.GetModuleReference(import.Module).Name.IsNil)
{
// Managed C++ PInvoke into the same module
throw new NotImplementedException(ToString());
}
}

public override PInvokeMetadata GetPInvokeMethodMetadata()
{
if (!IsPInvoke)
return default(PInvokeMetadata);

CheckManagedCPlusPlusPInvoke();

MetadataReader metadataReader = MetadataReader;
MethodDefinition methodDef = metadataReader.GetMethodDefinition(_handle);
MethodImport import = methodDef.GetImport();
string name = metadataReader.GetString(import.Name);

ModuleReference moduleRef = metadataReader.GetModuleReference(import.Module);

string moduleName = metadataReader.GetString(moduleRef.Name);

MethodImportAttributes importAttributes = import.Attributes;
Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/src/tools/Common/TypeSystem/IL/EcmaMethodIL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public sealed partial class EcmaMethodIL : MethodIL

public static EcmaMethodIL Create(EcmaMethod method, bool clearInitLocals = false)
{
if (method.IsPInvoke)
{
method.CheckManagedCPlusPlusPInvoke();
}
var rva = method.MetadataReader.GetMethodDefinition(method.Handle).RelativeVirtualAddress;
if (rva == 0)
return null;
Expand Down

0 comments on commit 7793032

Please sign in to comment.