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()
{