From deb9248c9284111039e6c33f5e634ce910bf5c0e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 11 Feb 2026 22:46:15 +0000 Subject: [PATCH 1/2] Initial plan From db36cc21f06ca057666b0f5238f2bad8fa5ba600 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 11 Feb 2026 23:02:45 +0000 Subject: [PATCH 2/2] Improve exception message for empty assembly loading - Added new resource string BadImageFormat_EmptyAssembly - Updated Assembly.Load and AssemblyLoadContext.LoadFromStream to use the new message - Updated tests to verify the new error message contains "empty" Co-authored-by: elinor-fung <47805090+elinor-fung@users.noreply.github.com> --- .../System.Private.CoreLib/src/Resources/Strings.resx | 3 +++ .../src/System/Reflection/Assembly.cs | 2 +- .../src/System/Runtime/Loader/AssemblyLoadContext.cs | 2 +- .../tests/System.Reflection.Tests/AssemblyTests.cs | 4 +++- .../System.Reflection.Tests/CoreCLR/AssemblyTests.cs | 11 +++++++++++ 5 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx b/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx index f511ebeb21810d..17a2ddef6a97b6 100644 --- a/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx +++ b/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx @@ -2018,6 +2018,9 @@ Bad IL format. + + Assembly image is empty. The stream or byte array must contain a valid PE file. + Corrupt .resources file. The specified type doesn't exist. diff --git a/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs b/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs index ebb0093edfe219..9866c0e660c04a 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Reflection/Assembly.cs @@ -259,7 +259,7 @@ public static Assembly Load(byte[] rawAssembly, byte[]? rawSymbolStore) ArgumentNullException.ThrowIfNull(rawAssembly); if (rawAssembly.Length == 0) - throw new BadImageFormatException(SR.BadImageFormat_BadILFormat); + throw new BadImageFormatException(SR.BadImageFormat_EmptyAssembly); SerializationInfo.ThrowIfDeserializationInProgress("AllowAssembliesFromByteArrays", ref s_cachedSerializationSwitch); diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/Loader/AssemblyLoadContext.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/Loader/AssemblyLoadContext.cs index 53579d3060d613..0bd7bd3d6ea866 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/Loader/AssemblyLoadContext.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/Loader/AssemblyLoadContext.cs @@ -386,7 +386,7 @@ public Assembly LoadFromStream(Stream assembly, Stream? assemblySymbols) ReadOnlySpan spanAssembly = ReadAllBytes(assembly); if (spanAssembly.IsEmpty) { - throw new BadImageFormatException(SR.BadImageFormat_BadILFormat); + throw new BadImageFormatException(SR.BadImageFormat_EmptyAssembly); } // Read the symbol stream if provided diff --git a/src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs b/src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs index 5f8e098624b15d..e6f1ebfe40eb91 100644 --- a/src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs +++ b/src/libraries/System.Runtime/tests/System.Reflection.Tests/AssemblyTests.cs @@ -841,7 +841,9 @@ public void AssemblyLoadFromBytes() public void AssemblyLoadFromBytesNeg() { Assert.Throws(() => Assembly.Load((byte[])null)); - Assert.Throws(() => Assembly.Load(new byte[0])); + + BadImageFormatException ex = Assert.Throws(() => Assembly.Load(new byte[0])); + Assert.Contains("empty", ex.Message, StringComparison.OrdinalIgnoreCase); } [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsAssemblyLoadingSupported), nameof(PlatformDetection.HasAssemblyFiles))] diff --git a/src/libraries/System.Runtime/tests/System.Reflection.Tests/CoreCLR/AssemblyTests.cs b/src/libraries/System.Runtime/tests/System.Reflection.Tests/CoreCLR/AssemblyTests.cs index 0146537db43527..7d76478b716965 100644 --- a/src/libraries/System.Runtime/tests/System.Reflection.Tests/CoreCLR/AssemblyTests.cs +++ b/src/libraries/System.Runtime/tests/System.Reflection.Tests/CoreCLR/AssemblyTests.cs @@ -29,6 +29,17 @@ public void LoadFromStream_Location_IsEmpty() Assert.Empty(assembly.Location); } + [Fact] + public void LoadFromStream_EmptyStream_ThrowsBadImageFormatException() + { + using (var emptyStream = new MemoryStream()) + { + BadImageFormatException ex = Assert.Throws( + () => AssemblyLoadContext.Default.LoadFromStream(emptyStream)); + Assert.Contains("empty", ex.Message, StringComparison.OrdinalIgnoreCase); + } + } + [Fact] public void EntryPoint() {