From 40ba6c214e32cbc3c61ed629fcbb0961ecce9cf9 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Thu, 13 Aug 2015 15:45:13 -0700 Subject: [PATCH 1/2] Fix GetUserName unit test A deadlock was occurring when executing `whoami` through System.Diagnostics.Process because we were calling `WaitForExit()` before `StandardOutput.ReadToEnd()`. According to the documentation, "To avoid deadlocks, always read the output stream first and then wait," which is what we do here now. Additionally, `Process` implements IDisposable, and is now automatically disposed via the `using` block. https://msdn.microsoft.com/en-us/library/system.diagnostics.process.standardoutput(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/System.Diagnostics.Process(v=vs.110).aspx --- src/ps_test/test_CorePsPlatform.cs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/ps_test/test_CorePsPlatform.cs b/src/ps_test/test_CorePsPlatform.cs index 6cf195a31..99a3d8f3f 100644 --- a/src/ps_test/test_CorePsPlatform.cs +++ b/src/ps_test/test_CorePsPlatform.cs @@ -53,15 +53,17 @@ namespace PSTests RedirectStandardOutput = true, UseShellExecute = false }; - Process process = Process.Start(startInfo); - // The process should always exit, but wait a set time just in case - process.WaitForExit(1000); - // The process should return an exit code of 0 on success - Assert.Equal(0, process.ExitCode); - // Get output of call to whoami without trailing newline - string username = process.StandardOutput.ReadToEnd().Trim(); - // It should be the same as what our platform code returns - Assert.Equal(username, Platform.NonWindowsGetUserName()); + using (Process process = Process.Start(startInfo)) + { + // Get output of call to whoami without trailing newline + string username = process.StandardOutput.ReadToEnd().Trim(); + process.WaitForExit(); + + // The process should return an exit code of 0 on success + Assert.Equal(0, process.ExitCode); + // It should be the same as what our platform code returns + Assert.Equal(username, Platform.NonWindowsGetUserName()); + } } } } From 1a3d4aa9a60b04919f3afa0cd8f6abb8b83a7422 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Fri, 21 Aug 2015 17:43:46 -0700 Subject: [PATCH 2/2] Refactor host usage Build scripts and powershell-simple.cs were refactored to work with the patch to CoreCLR and updated hosting code. - Paths in Makefile were fixed because the hosting code runs the assembly from the current working directory (usually app_base) - runps scripts were all fixed because the hosting code CLI changed - powershell-simple.cs was cleaned up because its Main function is now directly executed - monad submodule was repinned while we had the change - monad-ext submodule was updated with rebuild CoreCLR binaries - monad-native submodule was repinned to development, including fixed test and hosting code changes --- scripts/Makefile | 4 +- scripts/powershell-run.mk | 2 - scripts/runps-file.sh | 1 - scripts/runps-simple-debug.sh | 3 +- scripts/runps-simple-trace.sh | 3 +- scripts/runps-simple.sh | 3 +- scripts/runps.sh | 3 +- src/monad | 2 +- src/monad-ext | 2 +- src/monad-native | 2 +- src/powershell-run/powershell-simple.cs | 61 ------------------------- 11 files changed, 9 insertions(+), 77 deletions(-) diff --git a/scripts/Makefile b/scripts/Makefile index 9a1027347..1f49d7d91 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -183,14 +183,14 @@ run-file: $(RUN_TARGETS) internal-prepare-exec_env internal-prepare-release-clr pester-tests: $(RUN_TARGETS) internal-prepare-exec_env internal-prepare-release-clr # execute the pester tests, pester needs a TEMP environment variable to be set - exec_env/app_base/runps-simple.sh 'cd ../src/pester-tests; $$env:TEMP="/tmp"; invoke-pester' + exec_env/app_base/runps-simple.sh 'cd ../../../src/pester-tests; $$env:TEMP="/tmp"; invoke-pester' test: $(RUN_TARGETS) internal-prepare-exec_env internal-prepare-release-clr $(addprefix $(TESTRUN_FOLDER)/, ps_test.dll xunit.console.netcore.exe xunit.runner.utility.dll xunit.abstractions.dll xunit.execution.dll) pester-tests dotnetlibs/monad_native cd exec_env/app_base && PSMODULEPATH=$(shell pwd)/exec_env/app_base/Modules LD_LIBRARY_PATH=../coreclr:. ../coreclr/corerun xunit.console.netcore.exe ps_test.dll -xml ../../xunittests.xml # run the c++ tests cd dotnetlibs && ./monad_native # test the 3rdparty/hashbang sample - PATH=$(PATH):$(shell pwd)/exec_env/app_base 3rdparty/hashbang/script.ps1 + PATH=$(PATH):$(shell pwd)/exec_env/app_base $(shell pwd)/3rdparty/hashbang/script.ps1 run-debugclr: $(RUN_TARGETS) internal-prepare-exec_env internal-prepare-debug-clr PAL_DBG_CHANNELS="+LOADER.TRACE" exec_env/app_base/runps-simple.sh get-location diff --git a/scripts/powershell-run.mk b/scripts/powershell-run.mk index 712b75c69..71a741266 100644 --- a/scripts/powershell-run.mk +++ b/scripts/powershell-run.mk @@ -19,5 +19,3 @@ dotnetlibs/powershell-run.exe: $(POWERSHELL_RUN_SRCS) $(POWERSHELL_RUN_DEPS) dotnetlibs/powershell-simple.exe: $(POWERSHELL_SIMPLE_SRCS) $(POWERSHELL_RUN_DEPS) $(CSC) -out:$@ -noconfig -nostdlib -target:exe $(POWERSHELL_RUN_REFS) $(COREREF) $(POWERSHELL_SIMPLE_SRCS) - - diff --git a/scripts/runps-file.sh b/scripts/runps-file.sh index e3239ccdc..4dcf52cd1 100755 --- a/scripts/runps-file.sh +++ b/scripts/runps-file.sh @@ -3,4 +3,3 @@ SCRIPTDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) "$SCRIPTDIR/runps.sh" --file "$@" - diff --git a/scripts/runps-simple-debug.sh b/scripts/runps-simple-debug.sh index 6bbe7a82d..5d3fbe4fb 100755 --- a/scripts/runps-simple-debug.sh +++ b/scripts/runps-simple-debug.sh @@ -5,5 +5,4 @@ SCRIPTDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) cd "$SCRIPTDIR" echo "launching PS debug now" -PSMODULEPATH="$SCRIPTDIR/Modules" LD_LIBRARY_PATH="$SCRIPTDIR:/usr/lib/llvm-3.6/lib" lldb-3.6 ./host_cmdline -- -c ../coreclr -alc Microsoft.PowerShell.CoreCLR.AssemblyLoadContext.dll -tpa powershell-simple.exe "powershell-simple, version=1.0.0.0, culture=neutral, PublicKeyToken=null" "ps_hello_world.Program" "UnmanagedMain" --working-dir "$CWD" "$@" - +PSMODULEPATH="$SCRIPTDIR/Modules" LD_LIBRARY_PATH="$SCRIPTDIR:/usr/lib/llvm-3.6/lib" lldb-3.6 ./host_cmdline -- -c ../coreclr -alc Microsoft.PowerShell.CoreCLR.AssemblyLoadContext.dll -tpa powershell-simple.exe powershell-simple.exe "$@" diff --git a/scripts/runps-simple-trace.sh b/scripts/runps-simple-trace.sh index d8e08c031..f2932e827 100755 --- a/scripts/runps-simple-trace.sh +++ b/scripts/runps-simple-trace.sh @@ -5,5 +5,4 @@ SCRIPTDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) cd "$SCRIPTDIR" echo "launching PS trace now" -PSMODULEPATH="$SCRIPTDIR/Modules" LD_LIBRARY_PATH="$SCRIPTDIR" strace -e trace=file ./host_cmdline -c ../coreclr -alc Microsoft.PowerShell.CoreCLR.AssemblyLoadContext.dll -tpa powershell-simple.exe "powershell-simple, version=1.0.0.0, culture=neutral, PublicKeyToken=null" "ps_hello_world.Program" "UnmanagedMain" --working-dir "$CWD" "$@" - +PSMODULEPATH="$SCRIPTDIR/Modules" LD_LIBRARY_PATH="$SCRIPTDIR" strace -e trace=file ./host_cmdline -c ../coreclr -alc Microsoft.PowerShell.CoreCLR.AssemblyLoadContext.dll -tpa powershell-simple.exe powershell-simple.exe "$@" diff --git a/scripts/runps-simple.sh b/scripts/runps-simple.sh index 214fc6465..63615f271 100755 --- a/scripts/runps-simple.sh +++ b/scripts/runps-simple.sh @@ -4,5 +4,4 @@ CWD=$(pwd) SCRIPTDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) cd "$SCRIPTDIR" -PSMODULEPATH="$SCRIPTDIR/Modules" LD_LIBRARY_PATH="$SCRIPTDIR" ./host_cmdline -c ../coreclr -alc Microsoft.PowerShell.CoreCLR.AssemblyLoadContext.dll -tpa powershell-simple.exe "powershell-simple, version=1.0.0.0, culture=neutral, PublicKeyToken=null" "ps_hello_world.Program" "UnmanagedMain" --working-dir "$CWD" "$@" - +PSMODULEPATH="$SCRIPTDIR/Modules" LD_LIBRARY_PATH="$SCRIPTDIR" ./host_cmdline -c ../coreclr -alc Microsoft.PowerShell.CoreCLR.AssemblyLoadContext.dll -tpa powershell-simple.exe powershell-simple.exe "$@" diff --git a/scripts/runps.sh b/scripts/runps.sh index 5f672c053..d47a5e77e 100755 --- a/scripts/runps.sh +++ b/scripts/runps.sh @@ -4,5 +4,4 @@ CWD=$(pwd) SCRIPTDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) cd "$SCRIPTDIR" -PSMODULEPATH="$SCRIPTDIR/Modules" LD_LIBRARY_PATH="$SCRIPTDIR" ./host_cmdline -c ../coreclr -alc Microsoft.PowerShell.CoreCLR.AssemblyLoadContext.dll -tpa powershell-run.exe "powershell-run, version=1.0.0.0, culture=neutral, PublicKeyToken=null" "Microsoft.Samples.PowerShell.Host.PSListenerConsoleSample" "UnmanagedMain" --working-dir "$CWD" "$@" - +PSMODULEPATH="$SCRIPTDIR/Modules" LD_LIBRARY_PATH="$SCRIPTDIR" ./host_cmdline -c ../coreclr -alc Microsoft.PowerShell.CoreCLR.AssemblyLoadContext.dll -tpa powershell-run.exe powershell-run.exe "$@" diff --git a/src/monad b/src/monad index df562ce7e..2793b76e2 160000 --- a/src/monad +++ b/src/monad @@ -1 +1 @@ -Subproject commit df562ce7ecd60fb42e30702116123c8a58bf04d4 +Subproject commit 2793b76e2d48f012a82b8e9106b9e9f496d3b785 diff --git a/src/monad-ext b/src/monad-ext index 20468e65d..ea31eca62 160000 --- a/src/monad-ext +++ b/src/monad-ext @@ -1 +1 @@ -Subproject commit 20468e65dac6fa5a1b4cf6971ff490e548531822 +Subproject commit ea31eca6223992fa9729af67518930b6b45829d3 diff --git a/src/monad-native b/src/monad-native index a99c56eea..440cf6a23 160000 --- a/src/monad-native +++ b/src/monad-native @@ -1 +1 @@ -Subproject commit a99c56eea3264b8c492bc0edf5e3a65102397af9 +Subproject commit 440cf6a236067eba2d5dcfd5e3707a36295cee97 diff --git a/src/powershell-run/powershell-simple.cs b/src/powershell-run/powershell-simple.cs index ca0b39358..a3455c1b0 100644 --- a/src/powershell-run/powershell-simple.cs +++ b/src/powershell-run/powershell-simple.cs @@ -456,25 +456,6 @@ namespace ps_hello_world } } - public static void init() - { - //string psBasePath = System.IO.Directory.GetCurrentDirectory(); - //PowerShellAssemblyLoadContextInitializer.SetPowerShellAssemblyLoadContext(psBasePath); - - //Assembly a = Assembly.Load(new AssemblyName("System.Management.Automation, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")); - //Console.WriteLine("##########################\nloaded assembly a=" + a + " during init\n#########################"); - - // PH: this debugging requires a change in the core PS stuff that was stashed away - // during source cleanup - /* - PowerShellAssemblyLoadContext asmLoadContext = PowerShellAssemblyLoadContextInitializer.AsmLoadContext; - IEnumerable assemblies = asmLoadContext.GetAssemblies("System.Management.Automation"); - foreach (Assembly a in assemblies) - { - Console.WriteLine("a: " + a); - }*/ - } - static void test1(string[] args) { InitialSessionState iss = InitialSessionState.CreateDefault2(); @@ -525,50 +506,8 @@ namespace ps_hello_world } } - public static int UnmanagedMain(int argc, [MarshalAs(UnmanagedType.LPArray,ArraySubType=UnmanagedType.LPStr,SizeParamIndex=0)] String[] argv) - { - init(); - List allArgs = new List(); - - for (int i = 0; i < argc; ++i) - { - allArgs.Add(argv[i]); - } - - List filteredArgs = new List(); - if (allArgs.Count > 0) - { - for (int i = 0; i < allArgs.Count; ++i) - { - string arg = allArgs[i]; - bool hasNext = (i+1)