From 0026a3d98fd3206860a096859c46189747c42bb4 Mon Sep 17 00:00:00 2001 From: Simon Nattress Date: Fri, 8 Jan 2021 13:44:23 -0800 Subject: [PATCH 1/2] Improve R2RDump error message when an assembly is not R2R * Fix `TryLocateNativeReadyToRunHeader` to swallow `BadImageFormatException` and return true / false whether the image has a native R2R header (for composite images). * Running R2RDump on IL assemblies with no R2R now emits an error message that the assembly is not R2R instead of an unhelpful error about some RVA offset conversion failing. --- .../ReadyToRunReader.cs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/ReadyToRunReader.cs b/src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/ReadyToRunReader.cs index 3454d4ebbd270..91143e55dc8b1 100644 --- a/src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/ReadyToRunReader.cs +++ b/src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/ReadyToRunReader.cs @@ -481,12 +481,21 @@ public IReadOnlyDictionary GetCustomMethodToRuntimeFu private bool TryLocateNativeReadyToRunHeader() { - PEExportTable exportTable = CompositeReader.GetExportTable(); - if (exportTable.TryGetValue("RTR_HEADER", out _readyToRunHeaderRVA)) + try { - _composite = true; - return true; + PEExportTable exportTable = CompositeReader.GetExportTable(); + if (exportTable.TryGetValue("RTR_HEADER", out _readyToRunHeaderRVA)) + { + _composite = true; + return true; + } } + catch (BadImageFormatException) + { + // MSIL assemblies with no ready-to-run payload typically have no export table + return false; + } + return false; } From 9895b78dc7fa546faa21f34605c0d181eaf2c8eb Mon Sep 17 00:00:00 2001 From: Simon Nattress Date: Mon, 11 Jan 2021 14:39:58 -0800 Subject: [PATCH 2/2] [R2RDump] Fix signature parsing when module override token present * Fix handling of module override token in signature parser. When the override is present, a new SignatureDecoder is created and used as the decoder for the final signature with the fixup kind (and module override flag which is stored in the upper bit of the fixup kind byte) already parsed. This causes the remainder of the signature to be parsed as a full R2R signature which is now missing the fixup type. * Instead of creating a new decoder when a module override is present, set up the initial decoder's metadata reader in the constructor by detecting the module override up front. --- .../ReadyToRunSignature.cs | 36 +++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/ReadyToRunSignature.cs b/src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/ReadyToRunSignature.cs index 938103f3fa6ca..96d440b3bc12d 100644 --- a/src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/ReadyToRunSignature.cs +++ b/src/coreclr/tools/aot/ILCompiler.Reflection.ReadyToRun/ReadyToRunSignature.cs @@ -458,13 +458,15 @@ protected void UpdateOffset(int offset) /// Signature offset within the PE file byte array public R2RSignatureDecoder(IR2RSignatureTypeProvider provider, TGenericContext context, MetadataReader metadataReader, ReadyToRunReader r2rReader, int offset) { - _metadataReader = metadataReader; - _outerReader = metadataReader; Context = context; _provider = provider; _image = r2rReader.Image; _originalOffset = _offset = offset; _contextReader = r2rReader; + MetadataReader moduleOverrideMetadataReader = TryGetModuleOverrideMetadataReader(); + _metadataReader = moduleOverrideMetadataReader ?? metadataReader; + _outerReader = moduleOverrideMetadataReader ?? metadataReader; + Reset(); } /// @@ -480,11 +482,27 @@ public R2RSignatureDecoder(IR2RSignatureTypeProvider @@ -1098,17 +1116,15 @@ private ReadyToRunSignature ParseSignature(StringBuilder builder) bool moduleOverride = (fixupType & (byte)ReadyToRunFixupKind.ModuleOverride) != 0; SignatureDecoder moduleDecoder = this; - // Check first byte for a module override being encoded + // Check first byte for a module override being encoded. The metadata reader for the module + // override is configured in the R2RSignatureDecoder constructor. if (moduleOverride) { fixupType &= ~(uint)ReadyToRunFixupKind.ModuleOverride; - int moduleIndex = (int)ReadUIntAndEmitInlineSignatureBinary(builder); - IAssemblyMetadata refAsmEcmaReader = _contextReader.OpenReferenceAssembly(moduleIndex); - moduleDecoder = new SignatureDecoder(Context.AssemblyResolver, Context.Options, refAsmEcmaReader.MetadataReader, _image, Offset, refAsmEcmaReader.MetadataReader, _contextReader); + ReadUIntAndEmitInlineSignatureBinary(builder); } - ReadyToRunSignature result = moduleDecoder.ParseSignature((ReadyToRunFixupKind)fixupType, builder); - UpdateOffset(moduleDecoder.Offset); + ReadyToRunSignature result = ParseSignature((ReadyToRunFixupKind)fixupType, builder); return result; }