Export unmangled name for get_hostfxr_path on nethost (Windows x86) (dotnet/core...
authorElinor Fung <47805090+elinor-fung@users.noreply.github.com>
Wed, 5 Jun 2019 21:56:19 +0000 (14:56 -0700)
committerGitHub <noreply@github.com>
Wed, 5 Jun 2019 21:56:19 +0000 (14:56 -0700)
* Use .def file for exports

Commit migrated from https://github.com/dotnet/core-setup/commit/637b9a70c32d8fc5c9df0c5768b3cf369a337ef5

src/installer/corehost/cli/nethost/CMakeLists.txt
src/installer/corehost/cli/nethost/Exports.def [new file with mode: 0644]
src/installer/corehost/cli/test/nativehost/nativehost.cpp
src/installer/test/HostActivationTests/NativeHosting/Nethost.cs

index 8528cf3..b56650b 100644 (file)
@@ -17,6 +17,11 @@ set(SOURCES
     ../fxr/fx_ver.cpp
 )
 
+if(WIN32)
+    list(APPEND SOURCES
+        Exports.def)
+endif()
+
 include(../lib.cmake)
 
 add_definitions(-DFEATURE_LIBHOST=1)
diff --git a/src/installer/corehost/cli/nethost/Exports.def b/src/installer/corehost/cli/nethost/Exports.def
new file mode 100644 (file)
index 0000000..d1cf531
--- /dev/null
@@ -0,0 +1,2 @@
+EXPORTS
+    get_hostfxr_path
\ No newline at end of file
index dec19ad..5b0b39e 100644 (file)
@@ -9,6 +9,7 @@
 #include "comhost_test.h"
 #include <hostfxr.h>
 #include "host_context_test.h"
+#include <utils.h>
 
 namespace
 {
@@ -35,14 +36,18 @@ int main(const int argc, const pal::char_t *argv[])
     const pal::char_t *command = argv[1];
     if (pal::strcmp(command, _X("get_hostfxr_path")) == 0)
     {
-        // args: ... [<assembly_path>] [<hostfxr_to_load>]
-        const pal::char_t *assembly_path = nullptr;
+        // args: ... [<explicit_load>] [<assembly_path>] [<hostfxr_to_load>]
+        bool explicit_load = false;
         if (argc >= 3)
-            assembly_path = argv[2];
+            explicit_load = pal::strcmp(pal::to_lower(pal::string_t{argv[2]}).c_str(), _X("true")) == 0;
 
+        const pal::char_t *assembly_path = nullptr;
         if (argc >= 4)
+            assembly_path = argv[3];
+
+        if (argc >= 5)
         {
-            pal::string_t to_load = argv[3];
+            pal::string_t to_load = argv[4];
             pal::dll_t fxr;
             if (!pal::load_library(&to_load, &fxr))
             {
@@ -51,13 +56,45 @@ int main(const int argc, const pal::char_t *argv[])
             }
         }
 
+        decltype(&get_hostfxr_path) get_hostfxr_path_fn;
+        if (explicit_load)
+        {
+            pal::string_t nethost_path;
+            if (!pal::get_own_executable_path(&nethost_path) || !pal::realpath(&nethost_path))
+            {
+                std::cout << "Failed to get path to current executable" << std::endl;
+                return EXIT_FAILURE;
+            }
+
+            nethost_path = get_directory(nethost_path);
+            nethost_path.append(MAKE_LIBNAME("nethost"));
+
+            pal::dll_t nethost;
+            if (!pal::load_library(&nethost_path, &nethost))
+            {
+                std::cout << "Failed to load library: " << tostr(nethost_path).data() << std::endl;
+                return EXIT_FAILURE;
+            }
+
+            get_hostfxr_path_fn = (decltype(get_hostfxr_path_fn))pal::get_symbol(nethost, "get_hostfxr_path");
+            if (get_hostfxr_path_fn == nullptr)
+            {
+                std::cout << "Failed to get get_hostfxr_path export from nethost" << std::endl;
+                return EXIT_FAILURE;
+            }
+        }
+        else
+        {
+            get_hostfxr_path_fn = get_hostfxr_path;
+        }
+
         pal::string_t fxr_path;
         size_t len = fxr_path.size();
-        int res = get_hostfxr_path(nullptr, &len, assembly_path);
+        int res = get_hostfxr_path_fn(nullptr, &len, assembly_path);
         if (static_cast<StatusCode>(res) == StatusCode::HostApiBufferTooSmall)
         {
             fxr_path.resize(len);
-            res = get_hostfxr_path(&fxr_path[0], &len, assembly_path);
+            res = get_hostfxr_path_fn(&fxr_path[0], &len, assembly_path);
         }
 
         if (static_cast<StatusCode>(res) == StatusCode::Success)
index bd27320..8a59ccd 100644 (file)
@@ -23,14 +23,18 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.NativeHosting
         }
 
         [Theory]
-        [InlineData(false, true)]
-        [InlineData(false, false)]
-        [InlineData(true, true)]
-        [InlineData(true, false)]
-        public void GetHostFxrPath_DotNetRootEnvironment(bool useAssemblyPath, bool isValid)
+        [InlineData(true, false, true)]
+        [InlineData(true, false, false)]
+        [InlineData(true, true, true)]
+        [InlineData(true, true, false)]
+        [InlineData(false, false, true)]
+        [InlineData(false, false, false)]
+        [InlineData(false, true, true)]
+        [InlineData(false, true, false)]
+        public void GetHostFxrPath_DotNetRootEnvironment(bool explicitLoad, bool useAssemblyPath, bool isValid)
         {
             string dotNetRoot = isValid ? Path.Combine(sharedState.ValidInstallRoot, "dotnet") : sharedState.InvalidInstallRoot;
-            CommandResult result = Command.Create(sharedState.NativeHostPath, $"{GetHostFxrPath} {(useAssemblyPath ? sharedState.TestAssemblyPath : string.Empty)}")
+            CommandResult result = Command.Create(sharedState.NativeHostPath, $"{GetHostFxrPath} {explicitLoad} {(useAssemblyPath ? sharedState.TestAssemblyPath : string.Empty)}")
                 .CaptureStdErr()
                 .CaptureStdOut()
                 .EnvironmentVariable("COREHOST_TRACE", "1")
@@ -55,15 +59,23 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.NativeHosting
         }
 
         [Theory]
-        [InlineData(false, true, false)]
-        [InlineData(false, true, true)]
-        [InlineData(false, false, false)]
-        [InlineData(false, false, true)]
-        [InlineData(true, true, false)]
-        [InlineData(true, true, true)]
-        [InlineData(true, false, false)]
-        [InlineData(true, false, true)]
-        public void GetHostFxrPath_GlobalInstallation(bool useAssemblyPath, bool useRegisteredLocation, bool isValid)
+        [InlineData(true, false, true, false)]
+        [InlineData(true, false, true, true)]
+        [InlineData(true, false, false, false)]
+        [InlineData(true, false, false, true)]
+        [InlineData(true, true, true, false)]
+        [InlineData(true, true, true, true)]
+        [InlineData(true, true, false, false)]
+        [InlineData(true, true, false, true)]
+        [InlineData(false, false, true, false)]
+        [InlineData(false, false, true, true)]
+        [InlineData(false, false, false, false)]
+        [InlineData(false, false, false, true)]
+        [InlineData(false, true, true, false)]
+        [InlineData(false, true, true, true)]
+        [InlineData(false, true, false, false)]
+        [InlineData(false, true, false, true)]
+        public void GetHostFxrPath_GlobalInstallation(bool explicitLoad, bool useAssemblyPath, bool useRegisteredLocation, bool isValid)
         {
             // Overide the registry key for self-registered global installs.
             // If using the registered location, set the install location value to the valid/invalid root.
@@ -78,7 +90,7 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.NativeHosting
                     registeredInstallLocationOverride.SetInstallLocation(installLocation, sharedState.RepoDirectories.BuildArchitecture);
                 }
 
-                result = Command.Create(sharedState.NativeHostPath, $"{GetHostFxrPath} {(useAssemblyPath ? sharedState.TestAssemblyPath : string.Empty)}")
+                result = Command.Create(sharedState.NativeHostPath, $"{GetHostFxrPath} {explicitLoad} {(useAssemblyPath ? sharedState.TestAssemblyPath : string.Empty)}")
                     .CaptureStdErr()
                     .CaptureStdOut()
                     .EnvironmentVariable("COREHOST_TRACE", "1")
@@ -105,8 +117,10 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.NativeHosting
             }
         }
 
-        [Fact]
-        public void GetHostFxrPath_WithAssemblyPath_AppLocalFxr()
+        [Theory]
+        [InlineData(true)]
+        [InlineData(false)]
+        public void GetHostFxrPath_WithAssemblyPath_AppLocalFxr(bool explicitLoad)
         {
             string appLocalFxrDir = Path.Combine(sharedState.BaseDirectory, "appLocalFxr");
             Directory.CreateDirectory(appLocalFxrDir);
@@ -115,7 +129,7 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.NativeHosting
             File.WriteAllText(assemblyPath, string.Empty);
             File.WriteAllText(hostFxrPath, string.Empty);
 
-            Command.Create(sharedState.NativeHostPath, $"{GetHostFxrPath} {assemblyPath}")
+            Command.Create(sharedState.NativeHostPath, $"{GetHostFxrPath} {explicitLoad} {assemblyPath}")
                 .CaptureStdErr()
                 .CaptureStdOut()
                 .EnvironmentVariable("COREHOST_TRACE", "1")
@@ -127,7 +141,7 @@ namespace Microsoft.DotNet.CoreSetup.Test.HostActivation.NativeHosting
         [Fact]
         public void GetHostFxrPath_HostFxrAlreadyLoaded()
         {
-            Command.Create(sharedState.NativeHostPath, $"{GetHostFxrPath} {sharedState.TestAssemblyPath} {sharedState.ProductHostFxrPath}")
+            Command.Create(sharedState.NativeHostPath, $"{GetHostFxrPath} false {sharedState.TestAssemblyPath} {sharedState.ProductHostFxrPath}")
                 .CaptureStdErr()
                 .CaptureStdOut()
                 .EnvironmentVariable("COREHOST_TRACE", "1")