Explorar o código

[reflection] Assembly.LoadFile should throw ArgumentException for relative paths (#10081)

* [reflection] Assembly.LoadFile should throw ArgumentException for relative paths

   Fixes https://github.com/mono/mono/issues/9943

* [mcs/tests] Use full path in the compiler-tester

   Otherwise the call to LoadFile will fail

* [mono/tests] Use full path in LoadFile in test-multi-netmodule-4-exe

* [mono/tests] Use the test runner for a couple of tests

* [fullaot] Disable test-multi-netmodule-4-exe and custom-attr-errors.exe

* [tests] Use Path.GetFullPath and Path.Combine in AssemblyTest

   Fix windows test failures
Aleksey Kliger (λgeek) %!s(int64=7) %!d(string=hai) anos
pai
achega
c636efec40

+ 16 - 0
mcs/class/corlib/Test/System.Reflection/AssemblyTest.cs

@@ -1287,6 +1287,22 @@ namespace MonoTests.System.Reflection
 			} catch (NotImplementedException){
 			}
 		}
+
+		[Test]
+		[ExpectedException (typeof (ArgumentException))]
+		public void LoadFileRelativeThrows ()
+		{
+			Assembly.LoadFile (Path.Combine ("non-existent", "relative", "path.dll"));
+		}
+
+		[Test]
+		[ExpectedException (typeof (FileNotFoundException))]
+		public void LoadFileAbsoluteNotFoundThrows ()
+		{
+			// have to use GetFullPath so we get C:\... on Windows
+			var abspath = Path.GetFullPath (Path.Combine ("non-existent", "absolute", "path.dll"));
+			Assembly.LoadFile (abspath);
+		}
 	}
 
 	public class TestDefinedTypes

+ 2 - 0
mcs/tools/compiler-tester/compiler-tester.cs

@@ -1573,6 +1573,8 @@ namespace TestRunner {
 				return 1;
 			}
 
+			compiler = Path.GetFullPath (compiler);
+
 			ITester tester;
 			try {
 				Console.WriteLine ("Loading " + compiler + " ...");

+ 5 - 0
mono/metadata/appdomain.c

@@ -2372,6 +2372,11 @@ ves_icall_System_Reflection_Assembly_LoadFile_internal (MonoStringHandle fname,
 	filename = mono_string_handle_to_utf8 (fname, error);
 	goto_if_nok (error, leave);
 
+	if (!g_path_is_absolute (filename)) {
+		mono_error_set_argument (error, "assemblyFile", "Absolute path information is required.");
+		goto leave;
+	}
+
 	MonoImageOpenStatus status;
 	MonoMethod *executing_method;
 	executing_method = mono_runtime_get_caller_no_system_or_reflection ();

+ 6 - 2
mono/tests/Makefile.am

@@ -1245,6 +1245,7 @@ PROFILE_DISABLED_TESTS += \
 	invalid-token.exe \
 	call_missing_method.exe \
 	call_missing_class.exe \
+	custom-attr-errors.exe \
 	ldfld_missing_field.exe \
 	ldfld_missing_class.exe \
 	vt-sync-method.exe \
@@ -1374,6 +1375,9 @@ PROFILE_DISABLED_TESTS += \
 	assembly-load-bytes.exe \
 	assembly-load-bytes-bindingredirect.exe
 
+# Tests which require AOT of a multi-netmodule assemblies
+PROFILE_DISABLED_TESTS += test-multi-netmodule-4-exe.exe
+
 # Tests which depend on a case-insensitive filesystem when using AOT
 if HOST_DARWIN
 else
@@ -2062,7 +2066,7 @@ test-multi-netmodule-4-exe.exe: test-multi-netmodule-4-exe.cs test-multi-netmodu
 	$(Q) $(MCS) -r:test-multi-netmodule-2-dll1.dll -out:$@ $<
 
 test-multi-netmodule: test-multi-netmodule-4-exe.exe
-	$(Q) $(RUNTIME) test-multi-netmodule-4-exe.exe > test-multi-netmodule-4-exe.exe.stdout 2> test-multi-netmodule-4-exe.exe.stderr
+	$(Q) $(TOOLS_RUNTIME) $(TEST_RUNNER) --testsuite-name $@ --disabled "$(DISABLED_TESTS)" $<
 
 EXTRA_DIST += custom-attr-errors.cs custom-attr-errors-lib.cs
 
@@ -2072,7 +2076,7 @@ custom-attr-errors.exe custom-attr-errors-lib.dll: custom-attr-errors.cs custom-
 	$(Q) $(MCS) /t:library custom-attr-errors-lib.cs
 
 test-cattr-type-load: custom-attr-errors.exe
-	$(Q) $(RUNTIME) custom-attr-errors.exe > custom-attr-errors.exe.stdout 2> custom-attr-errors.exe.stderr
+	$(Q) $(TOOLS_RUNTIME) $(TEST_RUNNER) --testsuite-name $@ --disabled "$(DISABLED_TESTS)" $<
 
 EXTRA_DIST += reflection-load-with-context-lib.cs reflection-load-with-context-second-lib.cs reflection-load-with-context.cs
 

+ 1 - 1
mono/tests/test-multi-netmodule-4-exe.cs

@@ -9,7 +9,7 @@ public class M4 {
 
 		// Expecting failure
 		try {
-			var DLL = Assembly.LoadFile(@"test-multi-netmodule-3-dll2.dll");
+			var DLL = Assembly.LoadFile(System.IO.Path.GetFullPath ("test-multi-netmodule-3-dll2.dll"));
 	        var m3Type = DLL.GetType("M3");
 	        var m3 = Activator.CreateInstance(m3Type);
 	        var m3m1Field = m3Type.GetField("m1");