Skip to content

Commit

Permalink
[loader] ReflectionOnlyLoad(byte[]) should not apply binding redirects (
Browse files Browse the repository at this point in the history
#18457)

* [loader] ReflectionOnlyLoad(byte[]) should not apply binding redirects

Match .NET Framework behavior.

Addresses #18388

* [tests] ReflectionOnlyLoad(byte[]) should not apply binding redirects

Regression test for #18388
  • Loading branch information
lambdageek authored and akoeplinger committed Jan 15, 2020
1 parent f4567e8 commit e7e81a4
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
3 changes: 2 additions & 1 deletion mono/metadata/appdomain.c
Original file line number Diff line number Diff line change
Expand Up @@ -2859,7 +2859,8 @@ mono_alc_load_raw_bytes (MonoAssemblyLoadContext *alc, guint8 *assembly_data, gu

MonoAssembly* redirected_asm = NULL;
MonoImageOpenStatus new_status = MONO_IMAGE_OK;
if ((redirected_asm = mono_assembly_binding_applies_to_image (alc, image, &new_status))) {
// http://blogs.microsoft.co.il/sasha/2010/06/09/assemblyreflectiononlyload-ignores-assembly-binding-redirects/
if (!refonly && (redirected_asm = mono_assembly_binding_applies_to_image (alc, image, &new_status))) {
mono_image_close (image);
image = redirected_asm->image;
mono_image_addref (image); /* so that mono_image close, below, has something to do */
Expand Down
6 changes: 5 additions & 1 deletion mono/tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ TESTS_CS_SRC= \
assembly-loadfile-bindingredirect.cs \
assembly-loadfrom-bindingredirect.cs \
assembly-loadfrom-simplename.cs \
assembly-refonly-load-bytes-bindingredirect.cs \
assemblyresolve_event.cs \
assemblyresolve_event3.cs \
assemblyresolve_event4.cs \
Expand Down Expand Up @@ -2997,8 +2998,11 @@ assembly-loadfile-bindingredirect.exe: assembly-load-dir1/LibStrongName.dll asse

assembly-load-bytes-bindingredirect.exe: assembly-load-dir1/LibStrongName.dll assembly-load-dir2/LibStrongName.dll assembly-load-bytes-bindingredirect.exe.config

EXTRA_DIST += assembly-loadfrom-bindingredirect.exe.config assembly-loadfile-bindingredirect.exe.config assembly-load-bytes-bindingredirect.exe.config
assembly-refonly-load-bytes-bindingredirect.exe: assembly-load-dir1/LibStrongName.dll assembly-load-dir2/LibStrongName.dll assembly-refonly-load-bytes-bindingredirect.exe.config


EXTRA_DIST += assembly-loadfrom-bindingredirect.exe.config assembly-loadfile-bindingredirect.exe.config assembly-load-bytes-bindingredirect.exe.config
EXTRA_DIST += assembly-refonly-load-bytes-bindingredirect.exe.config

assembly-load-dir1/LibSimpleName.dll: assembly-load-dir1/LibSimpleName.cs
$(MKDIR_P) $(dir $@)
Expand Down
55 changes: 55 additions & 0 deletions mono/tests/assembly-refonly-load-bytes-bindingredirect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.IO;
using System.Reflection;

public class TestAssemblyLoad {

public static int Main ()
{
return TestDriver.RunTests (typeof (TestAssemblyLoad));
}

public static int test_0_ReflectionOnlyLoadBytesBindingRedirect ()
{

string path1 = Path.Combine (AppDomain.CurrentDomain.BaseDirectory, "assembly-load-dir1", "LibStrongName.dll");

// Try to load from dir1/LibStrongName.dll, despite
// the assembly-refonly-load-bytes-bindingredirect.exe.config redirecting old versions to
// be mapped to version 2.0.0.0 which is in the assembly-load-dir2 directory
//
// The binding redict should not apply to reflection-only loads.
byte[] bytes1 = File.ReadAllBytes (path1);
Assembly asm1 = Assembly.ReflectionOnlyLoad (bytes1);
if (asm1 == null) {
Console.Error.WriteLine ("expected asm1 {0} to not be null", asm1);
return 1;
}

Type t1 = asm1.GetType ("LibClass");
if (t1 == null) {
Console.Error.WriteLine ("expected t1 {0} to not be null", t1);
return 2;
}

FieldInfo f1 = t1.GetField ("OnlyInVersion1");
if (f1 == null) {
Console.Error.WriteLine ("expected to find field OnlyInVersion1, but got null");
return 3;
}

if (f1.FieldType != typeof(int)) {
Console.Error.WriteLine ("Field OnlyInVersion1 has type {0}, expected int", f1.FieldType);
return 5;
}

FieldInfo f2 = t1.GetField ("OnlyInVersion2");

if (f2 != null) {
Console.Error.WriteLine ("expected not to find field OnlyInVersion2, but got {0}", f2);
return 4;
}

return 0;
}
}
12 changes: 12 additions & 0 deletions mono/tests/assembly-refonly-load-bytes-bindingredirect.exe.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<probing privatePath="assembly-load-dir2"/>
<dependentAssembly>
<assemblyIdentity name="LibStrongName" publicKeyToken="537eab56aa911cb7" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

0 comments on commit e7e81a4

Please sign in to comment.