예제 #1
0
    /// <summary>
    /// Gets the parent process of a specified process.
    /// </summary>
    /// <param name="handle">The process handle.</param>
    /// <returns>An instance of the Process class.</returns>
    public static Process GetParentProcess(IntPtr handle)
    {
        ProcessUtilities pbi = new ProcessUtilities();
        int returnLength;
        int status = NtQueryInformationProcess(handle, 0, ref pbi, Marshal.SizeOf(pbi), out returnLength);
        if (status != 0)
            throw new Win32Exception(status);

        try
        {
            return Process.GetProcessById(pbi.InheritedFromUniqueProcessId.ToInt32());
        }
        catch (ArgumentException)
        {
            // not found
            return null;
        }
    }
예제 #2
0
        /// <summary>
        /// Creates a pipe in which the specified ends are inheritable by child processes.
        /// In order for the pipe to be closable by the child, the inherited end should be closed after process creation.
        /// </summary>
        /// <remarks>
        /// This implementation creates a named pipe instance and immediately connects it. This is effectively the same implementation
        /// as <c>CreatePipe</c> (see %SDXROOT%\minkernel\kernelbase\pipe.c), but we compute a random pipe named rather than using the special
        /// 'anonymous' naming case of opening \\.\pipe or \Device\NamedPipe\ (without a subsequent path).
        /// We choose to not use <c>CreatePipe</c> since it opens both sides for synchronous I/O. Instead, for running build tools we want the
        /// BuildXL side opened for overlapped I/O and the build tool side opened for synchronous I/O (if the build tool side acts as a redirected
        /// console handle; recall that synchronous read and write attempts will fail on an async handle, which motivates the <c>CreatePipe</c> defaults
        /// in the first place). This is significant since many build tool console pipes are forever silent, and with sync handles we'd need N * 3 threads
        /// (stdin, stdout, stderr for N concurrent processes) to pessimistically drain them.
        /// </remarks>
        public static void CreateInheritablePipe(PipeInheritance inheritance, PipeFlags flags, out SafeFileHandle readHandle, out SafeFileHandle writeHandle)
        {
            string pipeName = @"\\.\pipe\BuildXL-" + Guid.NewGuid().ToString("N");

            writeHandle = ProcessUtilities.CreateNamedPipe(
                pipeName,
                PipeOpenMode.PipeAccessOutbound | (((flags & PipeFlags.WriteSideAsync) != 0) ? PipeOpenMode.FileFlagOverlapped : 0),
                PipeMode.PipeTypeByte | PipeMode.PipeRejectRemoteClients,
                nMaxInstances: 1,
                nOutBufferSize: PipeBufferSize,
                nInBufferSize: PipeBufferSize,
                nDefaultTimeout: 0,
                lpSecurityAttributes: IntPtr.Zero);

            if (writeHandle.IsInvalid)
            {
                throw new NativeWin32Exception(Marshal.GetLastWin32Error(), "CreateNamedPipeW failed");
            }

            var readSideFlags = FileFlagsAndAttributes.SecurityAnonymous;

            if ((flags & PipeFlags.ReadSideAsync) != 0)
            {
                readSideFlags |= FileFlagsAndAttributes.FileFlagOverlapped;
            }

            int maxRetry = 3;

            while (true)
            {
                OpenFileResult openReadSideResult = FileUtilities.TryCreateOrOpenFile(
                    pipeName,
                    FileDesiredAccess.GenericRead,
                    FileShare.None,
                    FileMode.Open,
                    readSideFlags,
                    out readHandle);

                if (openReadSideResult.Succeeded)
                {
                    break;
                }

                if (openReadSideResult.NativeErrorCode != NativeIOConstants.ErrorPipeBusy ||
                    maxRetry == 0)
                {
                    throw openReadSideResult.CreateExceptionForError();
                }

                bool success = false;

                // Wait for at most 5s.
                for (int i = 0; i < 10; ++i)
                {
                    success = ProcessUtilities.WaitNamedPipe(pipeName, 500);
                    if (success)
                    {
                        break;
                    }
                }

                if (!success)
                {
                    // After waiting for 5s, pipe is still not ready.
                    throw new NativeWin32Exception(Marshal.GetLastWin32Error(), "WaitNamedPipe");
                }

                --maxRetry;
            }

            if ((inheritance & PipeInheritance.InheritRead) != 0)
            {
                SetInheritable(readHandle);
            }

            if ((inheritance & PipeInheritance.InheritWrite) != 0)
            {
                SetInheritable(writeHandle);
            }
        }
예제 #3
0
        private static Dictionary <uint, ReportedProcess> GetSurvivingChildProcesses(JobObject jobObject)
        {
            if (!jobObject.TryGetProcessIds(out uint[] survivingChildProcessIds) || survivingChildProcessIds.Length == 0)
            {
                return(null);
            }

            var survivingChildProcesses = new Dictionary <uint, ReportedProcess>();

            foreach (uint processId in survivingChildProcessIds)
            {
                using (SafeProcessHandle processHandle = ProcessUtilities.OpenProcess(
                           ProcessSecurityAndAccessRights.PROCESS_QUERY_INFORMATION |
                           ProcessSecurityAndAccessRights.PROCESS_VM_READ,
                           false,
                           processId))
                {
                    if (processHandle.IsInvalid)
                    {
                        // we are too late: could not open process
                        continue;
                    }

                    if (!jobObject.ContainsProcess(processHandle))
                    {
                        // we are too late: process id got reused by another process
                        continue;
                    }

                    int exitCode;
                    if (!ProcessUtilities.GetExitCodeProcess(processHandle, out exitCode))
                    {
                        // we are too late: process id got reused by another process
                        continue;
                    }

                    using (PooledObjectWrapper <StringBuilder> wrap = Pools.GetStringBuilder())
                    {
                        StringBuilder sb = wrap.Instance;
                        if (sb.Capacity < MaxProcessPathLength)
                        {
                            sb.Capacity = MaxProcessPathLength;
                        }

                        if (ProcessUtilities.GetModuleFileNameEx(processHandle, IntPtr.Zero, sb, (uint)sb.Capacity) <= 0)
                        {
                            // we are probably too late
                            continue;
                        }

                        // Attempt to read the process arguments (command line) from the process
                        // memory. This is not fatal if it does not succeed.
                        string processArgs = string.Empty;

                        var  basicInfoSize = (uint)Marshal.SizeOf <Native.Processes.Windows.ProcessUtilitiesWin.PROCESS_BASIC_INFORMATION>();
                        var  basicInfoPtr  = Marshal.AllocHGlobal((int)basicInfoSize);
                        uint basicInfoReadLen;
                        try
                        {
                            if (Native.Processes.Windows.ProcessUtilitiesWin.NtQueryInformationProcess(
                                    processHandle,
                                    Native.Processes.Windows.ProcessUtilitiesWin.ProcessInformationClass.ProcessBasicInformation,
                                    basicInfoPtr, basicInfoSize, out basicInfoReadLen) == 0)
                            {
                                Native.Processes.Windows.ProcessUtilitiesWin.PROCESS_BASIC_INFORMATION basicInformation = Marshal.PtrToStructure <Native.Processes.Windows.ProcessUtilitiesWin.PROCESS_BASIC_INFORMATION>(basicInfoPtr);
                                Contract.Assert(basicInformation.UniqueProcessId == processId);

                                // NativeMethods.ReadProcessStructure and NativeMethods.ReadUnicodeString handle null\zero addresses
                                // passed into them. Since these are all value types, then there is no need to do any type
                                // of checking as passing zero through will just result in an empty process args string.
                                var peb = Native.Processes.Windows.ProcessUtilitiesWin.ReadProcessStructure <Native.Processes.Windows.ProcessUtilitiesWin.PEB>(processHandle, basicInformation.PebBaseAddress);
                                var processParameters = Native.Processes.Windows.ProcessUtilitiesWin.ReadProcessStructure <Native.Processes.Windows.ProcessUtilitiesWin.RTL_USER_PROCESS_PARAMETERS>(processHandle, peb.ProcessParameters);
                                processArgs = Native.Processes.Windows.ProcessUtilitiesWin.ReadProcessUnicodeString(processHandle, processParameters.CommandLine);
                            }
                        }
                        finally
                        {
                            Marshal.FreeHGlobal(basicInfoPtr);
                        }

                        string path = sb.ToString();
                        survivingChildProcesses.Add(processId, new ReportedProcess(processId, path, processArgs));
                    }
                }
            }


            return(survivingChildProcesses);
        }
예제 #4
0
        /// <inheritdoc />
        public ulong?GetActivePeakWorkingSet()
        {
            using (m_queryJobDataLock.AcquireReadLock())
            {
                var detouredProcess = m_detouredProcess;
                if (detouredProcess == null ||
                    !detouredProcess.HasStarted ||
                    detouredProcess.HasExited ||
                    m_disposeStarted)
                {
                    return(null);
                }

                ulong?currentPeakWorkingSet    = null;
                ulong?currentPeakPagefileUsage = null;

                var jobObject = detouredProcess.GetJobObject();
                if (jobObject == null || !jobObject.TryGetProcessIds(out uint[] childProcessIds) || childProcessIds.Length == 0)
                {
                    return(null);
                }

                foreach (uint processId in childProcessIds)
                {
                    using (SafeProcessHandle processHandle = ProcessUtilities.OpenProcess(
                               ProcessSecurityAndAccessRights.PROCESS_QUERY_INFORMATION,
                               false,
                               processId))
                    {
                        if (processHandle.IsInvalid)
                        {
                            // we are too late: could not open process
                            continue;
                        }

                        if (!jobObject.ContainsProcess(processHandle))
                        {
                            // we are too late: process id got reused by another process
                            continue;
                        }

                        int exitCode;
                        if (!ProcessUtilities.GetExitCodeProcess(processHandle, out exitCode))
                        {
                            // we are too late: process id got reused by another process
                            continue;
                        }

                        var memoryUsage = Interop.Dispatch.GetMemoryUsageCounters(processHandle.DangerousGetHandle());
                        if (memoryUsage != null)
                        {
                            currentPeakWorkingSet    = (currentPeakWorkingSet ?? 0) + memoryUsage.PeakWorkingSetSize;
                            currentPeakPagefileUsage = (currentPeakPagefileUsage ?? 0) + memoryUsage.PeakPagefileUsage;
                        }
                    }
                }

                m_peakWorkingSet.RegisterSample(currentPeakWorkingSet ?? 0);
                m_peakPagefileUsage.RegisterSample(currentPeakPagefileUsage ?? 0);

                return(currentPeakWorkingSet);
            }
        }
        bool StartTasks(string cron, string task, bool noLog, ref string errMsg)
        {
            errMsg = "";
            try
            {
                var crond = CronScheduling.CronDaemon.Start <string>(
                    value => {
                    try
                    {
                        string pathTask  = m_path + "\\" + task;
                        string invisible = "";

#pragma warning disable CS0162 // Unreachable code detected
                        if (ProjectInstaller.invisibleUserService)
                        {
                            FileHelper.StartProcess(pathTask); // Works in User service, invisible
                        }
                        else                                   // LocalSystem service

                        // Windows Home: OK
                        // Windows Workstation: OK
                        // Windows Server 2012: Random bug:
                        // StartProcessAsCurrentUser: GetSessionUserToken failed
                        // WTSQueryUserToken requires the SE_TCB_NAME privilege
                        // Use AdjustTokenPrivileges to enable it:
                        // (see bCreateProcess: StartImpersonation: AcquireLoadUserProfilePriveleges)
                        // http://www.vbforums.com/showthread.php?616830-RESOLVED-VB-Net-CreateProcessAsUser-API
                        {
                            string errMsg1 = "";
                            if (!ProcessExtensions.StartProcessAsCurrentUser(pathTask, out errMsg1))
                            {
                                // Solution: invisible run
                                invisible = " (" + errMsg1 + ": invisible)";
                                ProcessUtilities.CreateUIProcessForServiceRunningAsLocalSystem(
                                    pathTask, in_strArguments: "");
                            }
                        }
#pragma warning restore CS0162 // Unreachable code detected
                        if (!noLog)
                        {
                            LogMessage("Service Cron: " + value + invisible);
                        }
                    }
                    catch (Exception ex)
                    {
                        string errMsg1 = "Service Cron: " + value + crLf;
                        errMsg1       += " Error:" + ex.Message + ex.StackTrace;
                        LogMessage(errMsg1);
                    }
                });

                crond.Add(task + ": " + cron, cron);

                return(true);
            }
            catch (Exception ex)
            {
                errMsg = ex.Message;
                return(false);
            }
        }
예제 #6
0
        /// <nodoc/>
        public FactIfSupportedAttribute(
            bool requiresAdmin                       = false,
            bool requiresJournalScan                 = false,
            bool requiresSymlinkPermission           = false,
            bool requiresWindowsBasedOperatingSystem = false,
            bool requiresUnixBasedOperatingSystem    = false,
            bool requiresHeliumDriversAvailable      = false,
            bool requiresHeliumDriversNotAvailable   = false,
            bool requiresMacOperatingSystem          = false)
        {
            RequiresAdmin             = requiresAdmin;
            RequiresJournalScan       = requiresJournalScan;
            RequiresSymlinkPermission = requiresSymlinkPermission;

            if (Skip != null)
            {
                // If skip is specified, do nothing because the test will be skipped anyway.
                return;
            }

            if (requiresAdmin)
            {
                if (!s_isElevated.HasValue)
                {
                    s_isElevated = CurrentProcess.IsElevated;
                }

                if (!s_isElevated.Value)
                {
                    Skip = "Test must be run elevated!";
                    return;
                }
            }

            if (requiresSymlinkPermission)
            {
                if (!s_canCreateSymlink.HasValue)
                {
                    string tempFile    = FileUtilities.GetTempFileName();
                    string symlinkPath = FileUtilities.GetTempFileName();
                    FileUtilities.DeleteFile(symlinkPath);

                    // For reliable tests, we ensure that the symlink is created.
                    s_canCreateSymlink = FileUtilities.TryCreateSymbolicLink(symlinkPath, tempFile, true).Succeeded&& FileUtilities.FileExistsNoFollow(symlinkPath);
                    FileUtilities.DeleteFile(symlinkPath);
                    FileUtilities.DeleteFile(tempFile);
                }

                if (!s_canCreateSymlink.Value)
                {
                    Skip = "Test must be run with symbolic link creation privileged";
                    return;
                }
            }

            if (requiresJournalScan)
            {
                if (OperatingSystemHelper.IsUnixOS)
                {
                    Skip = $"Test requires a journaled Windows file system, can't be executed on non-windows systems.";
                    return;
                }

                if (!s_canScanJournal.HasValue)
                {
                    var loggingContext = new LoggingContext("Dummy", "Dummy");
                    var map            = JournalUtils.TryCreateMapOfAllLocalVolumes(loggingContext);
                    var accessor       = JournalUtils.TryGetJournalAccessorForTest(map);
                    s_canScanJournal = accessor.Succeeded;
                    if (!accessor.Succeeded)
                    {
                        s_journalAccessorFailure = accessor.Failure.Describe();
                    }
                }

                if (!s_canScanJournal.Value)
                {
                    Skip = $"Test requires access to the in process change journal scan but getting the journal access failed. {s_journalAccessorFailure ?? string.Empty}{Environment.NewLine}Either run elevated or install RS2.";
                    return;
                }
            }

            if (requiresWindowsBasedOperatingSystem)
            {
                if (OperatingSystemHelper.IsUnixOS)
                {
                    Skip = "Test must be run on the CLR on Windows based operating systems!";
                    return;
                }
            }

            if (requiresUnixBasedOperatingSystem)
            {
                if (!OperatingSystemHelper.IsUnixOS)
                {
                    Skip = "Test must be run on the CoreCLR on Unix based operating systems!";
                    return;
                }
            }

            if (requiresMacOperatingSystem)
            {
                if (!OperatingSystemHelper.IsMacOS)
                {
                    Skip = "Test must be run on macOS";
                    return;
                }
            }

            if (requiresHeliumDriversAvailable)
            {
                if (!s_isHeliumFiltersAvailable.HasValue)
                {
                    s_isHeliumFiltersAvailable = ProcessUtilities.IsWciAndBindFiltersAvailable();
                }

                if (!s_isHeliumFiltersAvailable.Value)
                {
                    Skip = "Test must be run elevated on a machine with WCI and Bind filters available.";
                    return;
                }
            }

            if (requiresHeliumDriversNotAvailable)
            {
                if (!s_isHeliumFiltersAvailable.HasValue)
                {
                    s_isHeliumFiltersAvailable = ProcessUtilities.IsWciAndBindFiltersAvailable();
                }
                if (s_isHeliumFiltersAvailable.Value)
                {
                    Skip = "Test must be run on a machine where WCI and Bind filters are NOT available.";
                    return;
                }
            }
        }
예제 #7
0
 public async Task Install(string file)
 {
     await ProcessUtilities.RunProcessAsync(file, "-passive");
 }
예제 #8
0
        /// <nodoc/>
        public FactIfSupportedAttribute(
            bool requiresAdmin                       = false,
            bool requiresJournalScan                 = false,
            bool requiresSymlinkPermission           = false,
            bool requiresWindowsBasedOperatingSystem = false,
            bool requiresUnixBasedOperatingSystem    = false,
            bool requiresHeliumDriversAvailable      = false,
            bool requiresHeliumDriversNotAvailable   = false,
            bool requiresMacOperatingSystem          = false,
            bool requiresWindowsOrMacOperatingSystem = false,
            TestRequirements additionalRequirements  = TestRequirements.None)
        {
            var requirements = additionalRequirements;

            AddRequirement(ref requirements, requiresAdmin, TestRequirements.Admin);
            AddRequirement(ref requirements, requiresJournalScan, TestRequirements.JournalScan);
            AddRequirement(ref requirements, requiresSymlinkPermission, TestRequirements.SymlinkPermission);
            AddRequirement(ref requirements, requiresWindowsBasedOperatingSystem, TestRequirements.WindowsOs);
            AddRequirement(ref requirements, requiresUnixBasedOperatingSystem, TestRequirements.UnixBasedOs);
            AddRequirement(ref requirements, requiresHeliumDriversAvailable, TestRequirements.HeliumDriversAvailable);
            AddRequirement(ref requirements, requiresHeliumDriversNotAvailable, TestRequirements.HeliumDriversNotAvailable);
            AddRequirement(ref requirements, requiresMacOperatingSystem, TestRequirements.MacOs);
            AddRequirement(ref requirements, requiresWindowsOrMacOperatingSystem, TestRequirements.WindowsOrMacOs);

            Requirements = requirements;

            if (Skip != null)
            {
                // If skip is specified, do nothing because the test will be skipped anyway.
                return;
            }

            CheckRequirement(
                TestRequirements.Admin,
                () =>
            {
                return(!CurrentProcess.IsElevated ? "Test must be run elevated!" : null);
            });

            CheckRequirement(
                TestRequirements.SymlinkPermission,
                () =>
            {
                string tempFile    = FileUtilities.GetTempFileName();
                string symlinkPath = FileUtilities.GetTempFileName();
                FileUtilities.DeleteFile(symlinkPath);

                // For reliable tests, we ensure that the symlink is created.
                var canCreateSymlink = FileUtilities.TryCreateSymbolicLink(symlinkPath, tempFile, true).Succeeded&& FileUtilities.FileExistsNoFollow(symlinkPath);
                FileUtilities.DeleteFile(symlinkPath);
                FileUtilities.DeleteFile(tempFile);

                return(!canCreateSymlink ? "Test must be run with symbolic link creation privileged" : null);
            });

            CheckRequirement(
                TestRequirements.JournalScan,
                () =>
            {
                if (OperatingSystemHelper.IsUnixOS)
                {
                    return($"Test requires a journaled Windows file system, can't be executed on non-windows systems.");
                }

                var loggingContext = new LoggingContext("Dummy", "Dummy");
                var map            = JournalUtils.TryCreateMapOfAllLocalVolumes(loggingContext);
                var accessor       = JournalUtils.TryGetJournalAccessorForTest(map);
                if (!accessor.Succeeded)
                {
                    return(accessor.Failure.Describe());
                }

                return(null);
            });

            CheckRequirement(
                TestRequirements.WindowsOs,
                () =>
            {
                if (OperatingSystemHelper.IsUnixOS)
                {
                    return("Test must be run on the CLR on Windows based operating systems!");
                }

                return(null);
            });

            CheckRequirement(
                TestRequirements.UnixBasedOs,
                () =>
            {
                if (!OperatingSystemHelper.IsUnixOS)
                {
                    return("Test must be run on the CoreCLR on Unix based operating systems!");
                }

                return(null);
            });

            CheckRequirement(
                TestRequirements.MacOs,
                () =>
            {
                if (!OperatingSystemHelper.IsMacOS)
                {
                    return("Test must be run on macOS");
                }

                return(null);
            });

            CheckRequirement(
                TestRequirements.HeliumDriversAvailable,
                () =>
            {
                if (!ProcessUtilities.IsWciAndBindFiltersAvailable())
                {
                    return("Test must be run elevated on a machine with WCI and Bind filters available.");
                }

                return(null);
            });

            CheckRequirement(
                TestRequirements.HeliumDriversNotAvailable,
                () =>
            {
                if (ProcessUtilities.IsWciAndBindFiltersAvailable())
                {
                    return("Test must be run on a machine where WCI and Bind filters are NOT available.");
                }

                return(null);
            });

            CheckRequirement(
                TestRequirements.WindowsProjFs,
                () =>
            {
                if (OperatingSystemHelper.IsUnixOS)
                {
                    return("WindowsProjFs requires running on Windows operating system");
                }

                bool foundGvfsService = System.Diagnostics.Process.GetProcessesByName("GVFS.Service").Length != 0;
                if (!foundGvfsService)
                {
                    return("Could not find GVFS.Service. Is Windows Projected FileSystem enabled?");
                }

                return(null);
            });

            CheckRequirement(
                TestRequirements.WindowsOrMacOs,
                () =>
            {
                if (OperatingSystemHelper.IsLinuxOS)
                {
                    return("Test must be run on Windows or macOS");
                }

                return(null);
            });
        }
예제 #9
0
        public RegexMatches Matches(string text, ICancellable cnc)
        {
            string flags = string.Concat(
                Options.i ? "i" : "",
                Options.m ? "m" : "",
                Options.s ? "s" : "",
                Options.u ? "u" : ""
                );

            string stdout_contents;
            string stderr_contents;

            Action <StreamWriter> stdin_writer = new Action <StreamWriter>(sw =>
            {
                sw.Write("m \"");
                WriteJavaScriptString(sw, Pattern);
                sw.Write("\" \"");
                sw.Write(flags);
                sw.Write("\" \"");
                WriteJavaScriptString(sw, text);
                sw.Write("\"");
            });


            if (!ProcessUtilities.InvokeExe(cnc, GetClientExePath( ), "i", stdin_writer, out stdout_contents, out stderr_contents, EncodingEnum.UTF8))
            {
                return(RegexMatches.Empty);                // (cancelled)
            }

            if (!string.IsNullOrWhiteSpace(stderr_contents))
            {
                throw new Exception(stderr_contents);
            }

            ResponseMatches client_response = JsonSerializer.Deserialize <ResponseMatches>(stdout_contents);

            if (client_response == null)
            {
                throw new Exception("JavaScript failed.");
            }

            if (!string.IsNullOrWhiteSpace(client_response.Error))
            {
                throw new Exception(client_response.Error);
            }

            string[] distributed_names = FigureOutGroupNames(client_response);
            Debug.Assert(distributed_names[0] == null);

            SimpleTextGetter stg     = new SimpleTextGetter(text);
            List <IMatch>    matches = new List <IMatch>( );

            foreach (var cm in client_response.Matches)
            {
                if (cm.Indices.Any( ))
                {
                    var start = cm.Indices[0][0];
                    var end   = cm.Indices[0][1];

                    var sm = SimpleMatch.Create(start, end - start, stg);

                    sm.AddGroup(sm.Index, sm.Length, true, "0");                       // (default group)

                    for (int i = 1; i < cm.Indices.Count; ++i)
                    {
                        string name;
                        if (i < distributed_names.Length && distributed_names[i] != null)
                        {
                            name = distributed_names[i];
                        }
                        else
                        {
                            name = i.ToString(CultureInfo.InvariantCulture);
                        }

                        var g = cm.Indices[i];

                        if (g == null)
                        {
                            sm.AddGroup(-1, 0, false, name);
                        }
                        else
                        {
                            start = cm.Indices[i][0];
                            end   = cm.Indices[i][1];

                            sm.AddGroup(start, end - start, true, name);
                        }
                    }

                    matches.Add(sm);
                }
            }

            return(new RegexMatches(matches.Count, matches));
        }
예제 #10
0
        public ProcessTreeContext(
            Guid payloadGuid,
            SafeHandle reportPipe,
            ArraySegment <byte> payload,
            string dllNameX64,
            string dllNameX86,
            int numRetriesPipeReadOnCancel,
            Action <string> debugPipeReporter,
            LoggingContext loggingContext)
        {
            // We cannot create this object in a wow64 process
            Contract.Assume(
                !ProcessUtilities.IsWow64Process(),
                "ProcessTreeContext:ctor - Cannot run injection server in a wow64 32 bit process");
            SafeFileHandle childHandle = null;

            m_loggingContext = loggingContext;
            NamedPipeServerStream serverStream = null;

            bool useNonDefaultPipeReader = PipeReaderFactory.GetKind() != PipeReaderFactory.Kind.Default;

            // This object will be the server for the tree. CreateSourceFile the pipe server.
            try
            {
                SafeFileHandle injectorHandle = null;

                if (useNonDefaultPipeReader)
                {
                    serverStream = Pipes.CreateNamedPipeServerStream(
                        PipeDirection.In,
                        PipeOptions.Asynchronous,
                        PipeOptions.None,
                        out childHandle);
                }
                else
                {
                    // Create a pipe for the requests
                    Pipes.CreateInheritablePipe(Pipes.PipeInheritance.InheritWrite, Pipes.PipeFlags.ReadSideAsync, out injectorHandle, out childHandle);
                }

                // Create the injector. This will duplicate the handles.
                Injector = ProcessUtilities.CreateProcessInjector(payloadGuid, childHandle, reportPipe, dllNameX86, dllNameX64, payload);

                if (useNonDefaultPipeReader)
                {
                    m_injectionRequestReader = PipeReaderFactory.CreateNonDefaultPipeReader(
                        serverStream,
                        InjectCallback,
                        Encoding.Unicode,
                        BufferSize);
                }
                else
                {
                    // Create the request reader. We don't start listening until requested
                    var injectionRequestFile = AsyncFileFactory.CreateAsyncFile(
                        injectorHandle,
                        FileDesiredAccess.GenericRead,
                        ownsHandle: true,
                        kind: FileKind.Pipe);
                    m_injectionRequestReader = new AsyncPipeReader(
                        injectionRequestFile,
                        InjectCallback,
                        Encoding.Unicode,
                        BufferSize,
                        numOfRetriesOnCancel: numRetriesPipeReadOnCancel,
                        debugPipeReporter: new AsyncPipeReader.DebugReporter(debugMsg => debugPipeReporter?.Invoke($"InjectionRequestReader: {debugMsg}")));
                }
            }
            catch (Exception exception)
            {
                if (Injector != null)
                {
                    Injector.Dispose();
                    Injector = null;
                }

                if (m_injectionRequestReader != null)
                {
                    m_injectionRequestReader.Dispose();
                    m_injectionRequestReader = null;
                }

                throw new BuildXLException("Process Tree Context injector could not be created", exception);
            }
            finally
            {
                // Release memory. Since the child handle is duplicated, it can be released

                if (childHandle != null && !childHandle.IsInvalid)
                {
                    childHandle.Dispose();
                }
            }
        }
예제 #11
0
 public App()
 {
     DispatcherUnhandledException += HandleExceptions;
     ProcessUtilities.EnsureDlls();
 }
예제 #12
0
        /// <summary>
        /// Copies support files. Throws if files are present already
        /// </summary>
        /// <param name="tests"></param>
        /// <param name="sourceDirectory"></param>
        /// <param name="destinationRootDirectory"></param>
        internal static void CopySupportFiles(List <TestRecord> tests, DirectoryInfo sourceDirectory, DirectoryInfo destinationRootDirectory)
        {
            //
            ThrowIfRelativePath(sourceDirectory);
            ThrowIfRelativePath(destinationRootDirectory);

            Collection <TestSupportFile> supportFiles;

            // In the normal case of not having a named execution group, all
            // tests share the same set of support files, so we can just grab
            // the first test's set.
            if (String.IsNullOrEmpty(tests.First().TestInfo.ExecutionGroup))
            {
                supportFiles = tests.First().TestInfo.SupportFiles;
            }
            // For a named execution group there can be distinct support files,
            // so we need to merge each test's set of support files.
            else
            {
                supportFiles = new Collection <TestSupportFile>();
                foreach (TestRecord test in tests)
                {
                    foreach (TestSupportFile supportFile in test.TestInfo.SupportFiles)
                    {
                        if (!supportFiles.Contains(supportFile))
                        {
                            supportFiles.Add(supportFile);
                        }
                    }
                }
            }

            foreach (TestSupportFile file in supportFiles)
            {
                ExecutionEventLog.RecordStatus("Copying File: " + file.Source);
                string source = Path.Combine(sourceDirectory.FullName, file.Source);

                source = ReplaceSupportFileVariables(source);
                DirectoryInfo destinationDirectory;
                // If the support file specifies an absolute path, that is our destination.
                if (Path.IsPathRooted(file.Destination))
                {
                    destinationDirectory = new DirectoryInfo(file.Destination);
                }
                // If the support file specified a path but it wasn't absolute, then it is
                // relative to the destination base path.
                else if (!string.IsNullOrEmpty(file.Destination))
                {
                    destinationDirectory = new DirectoryInfo(Path.Combine(destinationRootDirectory.FullName, file.Destination));
                }
                // Otherwise the support file didn't specify a destination, so it goes to the base
                else
                {
                    destinationDirectory = destinationRootDirectory;
                }

                // Given we've determined the destination, make sure the directory exists.
                if (!destinationDirectory.Exists)
                {
                    destinationDirectory.Create();
                }

                if (source.Contains("*")) //Copy * Wildcard
                {
                    ProcessUtilities.Run("cmd", string.Format(CultureInfo.InvariantCulture, "/c copy /y \"{0}\" \"{1}\"", source, destinationDirectory.FullName));
                }
                else if (Directory.Exists(source))  //Copy Directory
                {
                    ProcessUtilities.Run("cmd", string.Format(CultureInfo.InvariantCulture, "/c xcopy /ey \"{0}\" \"{1}\"\\", source, destinationDirectory.FullName));
                }
                // Exe files in net core generate deps.json and .dll, we need to copy them as well
                else if (source.Contains(".exe"))
                {
                    string wildcardedSource = source.Replace(".exe", ".*");
                    ProcessUtilities.Run("cmd", string.Format(CultureInfo.InvariantCulture, "/c copy /y \"{0}\" \"{1}\"", wildcardedSource, destinationDirectory.FullName));
                }
                else         //Copy individual File
                {
                    string destinationFile = Path.Combine(destinationDirectory.FullName, Path.GetFileName(source));
                    File.Copy(source, destinationFile, true);
                    File.SetAttributes(destinationFile, FileAttributes.Normal);
                }
            }
        }
예제 #13
0
파일: Process.cs 프로젝트: uilit/BuildXL
        /// <summary>
        /// Class constructor
        /// </summary>
        public Process(
            FileArtifact executable,
            AbsolutePath workingDirectory,
            PipData arguments,
            FileArtifact responseFile,
            PipData responseFileData,
            ReadOnlyArray <EnvironmentVariable> environmentVariables,
            StandardInput standardInput,
            FileArtifact standardOutput,
            FileArtifact standardError,
            AbsolutePath standardDirectory,
            TimeSpan?warningTimeout,
            TimeSpan?timeout,
            ReadOnlyArray <FileArtifact> dependencies,
            ReadOnlyArray <FileArtifactWithAttributes> outputs,
            ReadOnlyArray <DirectoryArtifact> directoryDependencies,
            ReadOnlyArray <DirectoryArtifact> directoryOutputs,
            ReadOnlyArray <PipId> orderDependencies,
            ReadOnlyArray <AbsolutePath> untrackedPaths,
            ReadOnlyArray <AbsolutePath> untrackedScopes,
            ReadOnlyArray <StringId> tags,
            ReadOnlyArray <int> successExitCodes,
            ReadOnlyArray <ProcessSemaphoreInfo> semaphores,
            PipProvenance provenance,
            StringId toolDescription,
            ReadOnlyArray <AbsolutePath> additionalTempDirectories,
            RegexDescriptor warningRegex               = default,
            RegexDescriptor errorRegex                 = default,
            bool enableMultiLineErrorScanning          = false,
            AbsolutePath uniqueOutputDirectory         = default,
            AbsolutePath uniqueRedirectedDirectoryRoot = default,
            AbsolutePath tempDirectory                 = default,
            Options options                    = default,
            bool testRetries                   = false,
            ServiceInfo serviceInfo            = null,
            ReadOnlyArray <int>?retryExitCodes = null,
            ReadOnlyArray <PathAtom>?allowedSurvivingChildProcessNames = null,
            TimeSpan?nestedProcessTerminationTimeout = null,
            AbsentPathProbeInUndeclaredOpaquesMode absentPathProbeMode = AbsentPathProbeInUndeclaredOpaquesMode.Unsafe,
            DoubleWritePolicy doubleWritePolicy             = DoubleWritePolicy.DoubleWritesAreErrors,
            ContainerIsolationLevel containerIsolationLevel = ContainerIsolationLevel.None,
            int?weight   = null,
            int?priority = null,
            ReadOnlyArray <AbsolutePath>?preserveOutputWhitelist = null,
            FileArtifact changeAffectedInputListWrittenFile      = default,
            int?preserveOutputsTrustLevel = null,
            ReadOnlyArray <PathAtom>?childProcessesToBreakawayFromSandbox = null)
        {
            Contract.Requires(executable.IsValid);
            Contract.Requires(workingDirectory.IsValid);
            Contract.Requires(arguments.IsValid);
            Contract.RequiresForAll(environmentVariables, environmentVariable => environmentVariable.Name.IsValid);
            Contract.RequiresForAll(environmentVariables, environmentVariable => environmentVariable.Value.IsValid ^ environmentVariable.IsPassThrough);
            Contract.Requires(dependencies.IsValid);
            Contract.RequiresForAll(dependencies, dependency => dependency.IsValid);
            Contract.Requires(directoryDependencies.IsValid);
            Contract.RequiresForAll(directoryDependencies, directoryDependency => directoryDependency.IsValid);
            Contract.Requires(outputs.IsValid);
            Contract.RequiresForAll(outputs, output => output.IsValid);
            Contract.Requires(directoryOutputs.IsValid);
            Contract.RequiresForAll(outputs, output => !output.IsSourceFile);
            Contract.RequiresForAll(directoryOutputs, directoryOutput => directoryOutput.IsValid);
            Contract.Requires(orderDependencies.IsValid);
            Contract.RequiresForAll(orderDependencies, dependency => dependency != PipId.Invalid);
            Contract.Requires(untrackedPaths.IsValid);
            Contract.RequiresForAll(untrackedPaths, path => path.IsValid);
            Contract.Requires(untrackedScopes.IsValid);
            Contract.RequiresForAll(untrackedScopes, scope => scope.IsValid);
            Contract.Requires(!timeout.HasValue || timeout.Value <= MaxTimeout);
            Contract.Requires(standardDirectory.IsValid || (standardOutput.IsValid && standardError.IsValid));
            Contract.Requires(provenance != null);
            Contract.Requires(additionalTempDirectories.IsValid);
            Contract.RequiresForAll(additionalTempDirectories, path => path.IsValid);
            Contract.Requires(tags.IsValid);
            // If the process needs to run in a container, the redirected directory has to be set
            Contract.Requires((options & Options.NeedsToRunInContainer) == Options.None || uniqueRedirectedDirectoryRoot.IsValid);

#if DEBUG   // a little too expensive for release builds
            Contract.Requires(Contract.Exists(dependencies, d => d == executable), "The executable must be declared as a dependency");
            Contract.Requires(
                !standardInput.IsFile || Contract.Exists(dependencies, d => d == standardInput.File),
                "If provided, the standard-input artifact must be declared as a dependency");
            Contract.Requires(
                !standardOutput.IsValid || Contract.Exists(outputs, o => o.ToFileArtifact() == standardOutput),
                "If provided, the standard-error artifact must be declared as an expected output");
            Contract.Requires(
                !standardError.IsValid || Contract.Exists(outputs, o => o.ToFileArtifact() == standardError),
                "If provided, the standard-error artifact must be declared as an expected output");
            Contract.Requires(
                !responseFile.IsValid ^ responseFileData.IsValid,
                "If provided, the response-file artifact must have a corresponding ResponseFileData");

            Contract.Requires(outputs.Length == outputs.Distinct().Count());
            Contract.Requires(directoryOutputs.Length == directoryOutputs.Distinct().Count());
            Contract.Requires(dependencies.Length == dependencies.Distinct().Count());
            Contract.Requires(directoryDependencies.Length == directoryDependencies.Distinct().Count());
            Contract.Requires(untrackedPaths.Length == untrackedPaths.Distinct().Count());
            Contract.Requires(untrackedScopes.Length == untrackedScopes.Distinct().Count());
            Contract.Requires(additionalTempDirectories.Length == additionalTempDirectories.Distinct().Count());
            Contract.RequiresForAll(semaphores, s => s.IsValid);
            Contract.Requires(semaphores.Length == semaphores.Distinct().Count());
            Contract.Requires(!(childProcessesToBreakawayFromSandbox?.Length > 0) || ProcessUtilities.SandboxSupportsProcessBreakaway(),
                              "A process is only allowed to specify child processes to breakaway if the underlying sandbox allows for it");
#endif

            Provenance        = provenance;
            Tags              = tags;
            Executable        = executable;
            ToolDescription   = toolDescription;
            WorkingDirectory  = workingDirectory;
            Arguments         = arguments;
            ResponseFile      = responseFile;
            ResponseFileData  = responseFileData;
            StandardOutput    = standardOutput;
            StandardError     = standardError;
            StandardInput     = standardInput;
            StandardDirectory = standardDirectory;
            WarningTimeout    = warningTimeout;
            Timeout           = timeout;

            // We allow any IEnumerable for these fields, but perform a copy up-front.
            // See the remarks of RemoveDuplicateFileArtifacts for why it is used on the input / output lists.
            Dependencies                                  = dependencies;
            DirectoryDependencies                         = directoryDependencies;
            FileOutputs                                   = outputs;
            DirectoryOutputs                              = directoryOutputs;
            OrderDependencies                             = orderDependencies;
            UntrackedPaths                                = untrackedPaths;
            UntrackedScopes                               = untrackedScopes;
            EnvironmentVariables                          = environmentVariables;
            SuccessExitCodes                              = successExitCodes;
            RetryExitCodes                                = retryExitCodes ?? ReadOnlyArray <int> .Empty;
            WarningRegex                                  = warningRegex;
            ErrorRegex                                    = errorRegex;
            EnableMultiLineErrorScanning                  = enableMultiLineErrorScanning;
            UniqueOutputDirectory                         = uniqueOutputDirectory;
            UniqueRedirectedDirectoryRoot                 = uniqueRedirectedDirectoryRoot;
            Semaphores                                    = semaphores;
            TempDirectory                                 = tempDirectory;
            TestRetries                                   = testRetries;
            ServiceInfo                                   = serviceInfo;
            AdditionalTempDirectories                     = additionalTempDirectories;
            AllowedSurvivingChildProcessNames             = allowedSurvivingChildProcessNames ?? ReadOnlyArray <PathAtom> .Empty;
            NestedProcessTerminationTimeout               = nestedProcessTerminationTimeout;
            ProcessAbsentPathProbeInUndeclaredOpaquesMode = absentPathProbeMode;
            DoubleWritePolicy                             = doubleWritePolicy;
            ContainerIsolationLevel                       = containerIsolationLevel;
            Weight   = weight.HasValue && weight.Value >= MinWeight ? weight.Value : MinWeight;
            Priority = priority.HasValue && priority.Value >= MinPriority ? (priority <= MaxPriority ? priority.Value : MaxPriority) : MinPriority;
            PreserveOutputWhitelist            = preserveOutputWhitelist ?? ReadOnlyArray <AbsolutePath> .Empty;
            ChangeAffectedInputListWrittenFile = changeAffectedInputListWrittenFile;

            if (PreserveOutputWhitelist.Length != 0)
            {
                options |= Options.HasPreserveOutputWhitelist;
            }

            ProcessOptions                       = options;
            PreserveOutputsTrustLevel            = preserveOutputsTrustLevel ?? (int)PreserveOutputsTrustValue.Lowest;
            ChildProcessesToBreakawayFromSandbox = childProcessesToBreakawayFromSandbox ?? ReadOnlyArray <PathAtom> .Empty;
        }
예제 #14
0
        protected override void OnStartup(StartupEventArgs e)
        {
            // Check if there was instance before this. If there was-close the current one.
            if (ProcessUtilities.ThisProcessIsAlreadyRunning())
            {
                ProcessUtilities.SetFocusToPreviousInstance("Carnac");
                Shutdown();
                return;
            }

            trayIcon = new CarnacTrayIcon();
            trayIcon.OpenPreferences += TrayIconOnOpenPreferences;
            var keyShowViewModel = new KeyShowViewModel(settings);

            keyShowView = new KeyShowView(keyShowViewModel);
            keyShowView.Show();

            carnac = new KeysController(keyShowViewModel.Messages, messageProvider, new ConcurrencyService(), settingsProvider);
            carnac.Start();

            MouseWatcher.OnMouseInput += (s, me) =>
            {
                if (!keyShowView.IsVisible)
                {
                    return;
                }
                var msg     = me.Message;
                var needAct = msg == MouseMessages.WM_LBUTTONUP || msg == MouseMessages.WM_RBUTTONUP;
                if (!needAct)
                {
                    return;
                }

                Dispatcher.Invoke(() =>
                {
                    keyShowViewModel.CursorPosition = keyShowView.PointFromScreen(new System.Windows.Point(me.Point.x, me.Point.y));
                    if (msg == MouseMessages.WM_LBUTTONUP)
                    {
                        keyShowView.LeftClick();
                    }
                    if (msg == MouseMessages.WM_RBUTTONUP)
                    {
                        keyShowView.RightClick();
                    }
                });
            };

            if (settings.ShowMouseClicks)
            {
                MouseWatcher.Start();
            }
            settings.PropertyChanged += (s, se) => {
                switch (se.PropertyName)
                {
                case "ShowMouseClicks":
                    if (this.settings.ShowMouseClicks)
                    {
                        MouseWatcher.Start();
                    }
                    else
                    {
                        MouseWatcher.Stop();
                    }
                    break;
                }
            };

#if !DEBUG
            if (settings.AutoUpdate)
            {
                Observable
                .Timer(TimeSpan.FromMinutes(5))
                .Subscribe(async x =>
                {
                    try
                    {
                        using (var mgr = UpdateManager.GitHubUpdateManager(carnacUpdateUrl))
                        {
                            await mgr.Result.UpdateApp();
                        }
                    }
                    catch
                    {
                        // Do something useful with the exception
                    }
                });
            }
#endif

            base.OnStartup(e);
        }
예제 #15
0
 /// <summary>
 /// Start tracing with Magellan. This can be done at scope of entire run or individual invocations
 /// </summary>
 internal static void BeginTrace()
 {
     ExecutionEventLog.RecordStatus("Starting Code Coverage session.");
     ProcessUtilities.Run(coverageToolPath, "/Close /session ALL");
     ProcessUtilities.Run(coverageToolPath, "/Reset /session ALL");
 }
예제 #16
0
        public async void ExportDataStoreToEXCEL(object sender, EventArgs e)
        {
            List <DataTable> tables = new List <DataTable>();

            try
            {
                string fileName = Path.ChangeExtension(storage.FileName, ".xlsx");

                // Show a message in the GUI.
                explorerPresenter.MainPresenter.ShowMessage("Exporting to excel...", Simulation.MessageType.Information);

                // Show a progress bar - this is currently the only way to get the stop/cancel button to appear.
                explorerPresenter.MainPresenter.ShowProgress(0, true);

                CancellationTokenSource cts = new CancellationTokenSource();

                // Read data from database (in the background).
                Task readTask = Task.Run(() =>
                {
                    ushort i = 0;
                    foreach (string tableName in storage.Reader.TableNames)
                    {
                        cts.Token.ThrowIfCancellationRequested();
                        DataTable table = storage.Reader.GetData(tableName);
                        table.TableName = tableName;
                        tables.Add(table);

                        double progress = 0.5 * (i + 1) / storage.Reader.TableNames.Count;
                        explorerPresenter.MainPresenter.ShowProgress(progress);

                        i++;
                    }
                }, cts.Token);

                // Add a handler to the stop button which cancels the excel export..
                EventHandler <EventArgs> stopHandler = (_, __) =>
                {
                    cts.Cancel();
                    explorerPresenter.MainPresenter.HideProgressBar();
                    explorerPresenter.MainPresenter.ShowMessage("Export to excel was cancelled.", Simulation.MessageType.Information, true);
                };
                explorerPresenter.MainPresenter.AddStopHandler(stopHandler);

                try
                {
                    // Wait for data to be read.
                    await readTask;

                    if (readTask.IsFaulted)
                    {
                        throw new Exception("Failed to read data from datastore", readTask.Exception);
                    }

                    if (readTask.IsCanceled || cts.Token.IsCancellationRequested)
                    {
                        return;
                    }

                    // Start the excel export as a task.
                    // todo: progress reporting and proper cancellation would be nice.
                    Task exportTask = Task.Run(() => Utility.Excel.WriteToEXCEL(tables.ToArray(), fileName), cts.Token);

                    // Wait for the excel file to be generated.
                    await exportTask;

                    if (exportTask.IsFaulted)
                    {
                        throw new Exception($"Failed to export to excel", exportTask.Exception);
                    }

                    if (exportTask.IsCanceled || cts.Token.IsCancellationRequested)
                    {
                        return;
                    }

                    // Show a success message.
                    explorerPresenter.MainPresenter.ShowMessage($"Excel successfully created: {fileName}", Simulation.MessageType.Information);

                    try
                    {
                        // Attempt to open the file - but don't display any errors if it doesn't work.
                        ProcessUtilities.ProcessStart(fileName);
                    }
                    catch
                    {
                    }
                }
                finally
                {
                    // Remove callback from the stop button.
                    explorerPresenter.MainPresenter.RemoveStopHandler(stopHandler);

                    // Remove the progress bar and stop button.
                    explorerPresenter.MainPresenter.HideProgressBar();
                }
            }
            catch (Exception err)
            {
                explorerPresenter.MainPresenter.ShowError(err);
            }
            finally
            {
                // Disposing of datatables isn't strictly necessary, but if we don't,
                // it could be a while before the memory is reclaimed.
                tables.ForEach(t => t.Dispose());
            }
        }
예제 #17
0
        /// <summary>
        /// Initializes the ES sandbox
        /// </summary>
        public SandboxConnection(SandboxKind kind, bool isInTestMode = false, bool measureCpuTimes = false)
        {
            Kind = kind;
            m_reportQueueLastEnqueueTime = 0;
            m_sandboxConnectionInfo      = new Sandbox.SandboxConnectionInfo()
            {
                Config = ConfigurationForSandboxKind(kind),
                Error  = Sandbox.SandboxSuccess
            };

            MeasureCpuTimes = measureCpuTimes;
            IsInTestMode    = isInTestMode;

            var process = System.Diagnostics.Process.GetCurrentProcess();

            Sandbox.InitializeSandbox(ref m_sandboxConnectionInfo, process.Id);
            if (m_sandboxConnectionInfo.Error != Sandbox.SandboxSuccess)
            {
                throw new BuildXLException($@"Unable to initialize generic sandbox, please check the sources for error code: {m_sandboxConnectionInfo.Error})");
            }

#if DEBUG
            ProcessUtilities.SetNativeConfiguration(true);
#else
            ProcessUtilities.SetNativeConfiguration(false);
#endif

            m_AccessReportCallback = (Sandbox.AccessReport report, int code) =>
            {
                if (code != Sandbox.ReportQueueSuccessCode)
                {
                    var message = "Sandbox event delivery failed with error: " + code;
                    throw new BuildXLException(message, ExceptionRootCause.MissingRuntimeDependency);
                }

                // Stamp the access report with a dequeue timestamp
                report.Statistics.DequeueTime = Sandbox.GetMachAbsoluteTime();

                // Update last received timestamp
                Volatile.Write(ref m_lastReportReceivedTimestampTicks, DateTime.UtcNow.Ticks);

                // Remember the latest enqueue time
                Volatile.Write(ref m_reportQueueLastEnqueueTime, report.Statistics.EnqueueTime);

                // The only way it can happen that no process is found for 'report.PipId' is when that pip is
                // explicitly terminated (e.g., because it timed out or Ctrl-c was pressed)
                if (m_pipProcesses.TryGetValue(report.PipId, out var process))
                {
                    // if the process is found, its ProcessId must match the RootPid of the report.
                    if (process.ProcessId != report.RootPid)
                    {
                        throw new BuildXLException("The process id from the lookup did not match the file access report process id", ExceptionRootCause.FailFast);
                    }
                    else
                    {
                        process.PostAccessReport(report);
                    }
                }
            };

            Sandbox.ObserverFileAccessReports(ref m_sandboxConnectionInfo, m_AccessReportCallback, Marshal.SizeOf <Sandbox.AccessReport>());
        }
예제 #18
0
 private void KillWithMoreForce(Process process)
 {
     ProcessUtilities.Run("taskkill.exe", "/f /pid " + process.Id.ToString());
 }
예제 #19
0
 public void RollbackState(StateModule settings)
 {
     ProcessUtilities.Run("regsvr32.exe", "/s /u " + Path.Combine(settings.TestBinariesDirectory.FullName, settings.Path));
 }
예제 #20
0
        /// <summary>
        ///

        private static void MakeDiagnosticRecord(DirectoryInfo directory)
        {
            ProcessUtilities.Run("dxdiag", "/whql:off /t " + Path.Combine(directory.FullName, "HardwareDiagnostic.txt"));
        }
예제 #21
0
        public static void IlasmTempAssembly(string declarations, bool appendDefaultHeader, bool includePdb, out string assemblyPath, out string pdbPath)
        {
            if (declarations == null)
            {
                throw new ArgumentNullException(nameof(declarations));
            }

            using (var sourceFile = new DisposableFile(extension: ".il"))
            {
                string sourceFileName = Path.GetFileNameWithoutExtension(sourceFile.Path);

                assemblyPath = Path.Combine(
                    TempRoot.Root,
                    Path.ChangeExtension(Path.GetFileName(sourceFile.Path), "dll"));

                string completeIL;
                if (appendDefaultHeader)
                {
                    const string corLibName    = "mscorlib";
                    const string corLibVersion = "4:0:0:0";
                    const string corLibKey     = "B7 7A 5C 56 19 34 E0 89";

                    completeIL =
                        $@".assembly '{sourceFileName}' {{}} 

.assembly extern {corLibName} 
{{
  .publickeytoken = ({corLibKey})
  .ver {corLibVersion}
}} 

{declarations}";
                }
                else
                {
                    completeIL = declarations.Replace("<<GeneratedFileName>>", sourceFileName);
                }

                sourceFile.WriteAllText(completeIL);

                var arguments = $"\"{sourceFile.Path}\" -DLL -out=\"{assemblyPath}\"";

                if (includePdb && !MonoHelpers.IsRunningOnMono())
                {
                    pdbPath    = Path.ChangeExtension(assemblyPath, "pdb");
                    arguments += string.Format(" -PDB=\"{0}\"", pdbPath);
                }
                else
                {
                    pdbPath = null;
                }

                var result = ProcessUtilities.Run(IlasmPath, arguments);

                if (result.ContainsErrors)
                {
                    throw new ArgumentException(
                              "The provided IL cannot be compiled." + Environment.NewLine +
                              IlasmPath + " " + arguments + Environment.NewLine +
                              result,
                              nameof(declarations));
                }
            }
        }
예제 #22
0
        /// <nodoc/>
        public FactIfSupportedAttribute(
            bool requiresAdmin                       = false,
            bool requiresJournalScan                 = false,
            bool requiresSymlinkPermission           = false,
            bool requiresWindowsBasedOperatingSystem = false,
            bool requiresUnixBasedOperatingSystem    = false,
            bool requiresHeliumDriversAvailable      = false,
            bool requiresHeliumDriversNotAvailable   = false)
        {
            RequiresAdmin             = requiresAdmin;
            RequiresJournalScan       = requiresJournalScan;
            RequiresSymlinkPermission = requiresSymlinkPermission;

            if (Skip != null)
            {
                // If skip is specified, do nothing because the test will be skipped anyway.
                return;
            }

            if (requiresAdmin)
            {
                if (!s_isElevated.HasValue)
                {
                    s_isElevated = CurrentProcess.IsElevated;
                }

                if (!s_isElevated.Value)
                {
                    Skip = "Test must be run elevated!";
                    return;
                }
            }

            if (requiresSymlinkPermission)
            {
                if (!s_canCreateSymlink.HasValue)
                {
                    string tempFile    = Path.GetTempFileName();
                    string symlinkPath = Path.GetTempFileName();
                    FileUtilities.DeleteFile(symlinkPath);

                    // For reliable tests, we ensure that the symlink is created.
                    s_canCreateSymlink = FileUtilities.TryCreateSymbolicLink(symlinkPath, tempFile, true).Succeeded&& FileUtilities.FileExistsNoFollow(symlinkPath);
                    FileUtilities.DeleteFile(symlinkPath);
                    FileUtilities.DeleteFile(tempFile);
                }

                if (!s_canCreateSymlink.Value)
                {
                    Skip = "Test must be run with symbolic link creation privileged";
                    return;
                }
            }

            if (requiresJournalScan)
            {
                if (!s_canScanJournal.HasValue)
                {
                    var loggingContext = new LoggingContext("Dummy", "Dummy");
                    var map            = VolumeMap.TryCreateMapOfAllLocalVolumes(loggingContext);
                    var accessor       = JournalAccessorGetter.TryGetJournalAccessor(loggingContext, map, AssemblyHelper.GetAssemblyLocation(Assembly.GetExecutingAssembly()));
                    s_canScanJournal = accessor.IsValid && accessor.Value != null;
                }

                if (!s_canScanJournal.Value)
                {
                    Skip = "Test requires access to the in process change journal scan. Either run elevated on install RS2.";
                    return;
                }
            }

            if (requiresWindowsBasedOperatingSystem)
            {
                if (OperatingSystemHelper.IsUnixOS)
                {
                    Skip = "Test must be run on the CLR on Windows based operating systems!";
                    return;
                }
            }

            if (requiresUnixBasedOperatingSystem)
            {
                if (!OperatingSystemHelper.IsUnixOS)
                {
                    Skip = "Test must be run on the CoreCLR on Unix based operating systems!";
                    return;
                }
            }

            if (requiresHeliumDriversAvailable)
            {
                if (!s_isHeliumFiltersAvailable.HasValue)
                {
                    s_isHeliumFiltersAvailable = ProcessUtilities.IsWciAndBindFiltersAvailable();
                }

                if (!s_isHeliumFiltersAvailable.Value)
                {
                    Skip = "Test must be run elevated on a machine with WCI and Bind filters available.";
                    return;
                }
            }

            if (requiresHeliumDriversNotAvailable)
            {
                if (!s_isHeliumFiltersAvailable.HasValue)
                {
                    s_isHeliumFiltersAvailable = ProcessUtilities.IsWciAndBindFiltersAvailable();
                }
                if (s_isHeliumFiltersAvailable.Value)
                {
                    Skip = "Test must be run on a machine where WCI and Bind filters are NOT available.";
                    return;
                }
            }
        }
예제 #23
0
 private static extern int NtQueryInformationProcess(IntPtr processHandle, int processInformationClass, ref ProcessUtilities processInformation, int processInformationLength, out int returnLength);
예제 #24
0
        /// <summary>
        /// Uses robocopy to copy one directory tree to another
        /// </summary>
        /// <param name="sourceDirectory">Source directory (this is typically a DirectoryInfo not VFP since we don't control it)</param>
        /// <param name="targetDirectory">Directory node to where the source directory tree should be copied</param>
        /// <param name="cr">How to handle collisions</param>
        /// <param name="deleteSourceDirectory">Remove source directory</param>
        public static void CopyDirectoryTree2
        (
            string sourceDirPath,
            string targetDirPath,
            CollisionRemediation cr,
            Boolean deleteSourceDirectory = false)
        {
            Debug.Assert(sourceDirPath != null);
            Debug.Assert(targetDirPath != null);

            if (!Directory.Exists(sourceDirPath))
            {
                throw new ScarabException("Source directory '{0}' does not exist", sourceDirPath);
            }

            string copyJob = string.Format
                             (
                "FileSync.CopyDirectoryTree2: {0} {1} {2} {3}",
                sourceDirPath,
                targetDirPath,
                cr.ToString(),
                deleteSourceDirectory
                             );

            Debug.WriteLine(copyJob);

            string  switches = "/E ";            // TODO Pri 2: /MT "; // Multi-threaded, copy full directory tree
            Boolean throwEx  = false;

            switch (cr)
            {
            case CollisionRemediation.Break:
            case CollisionRemediation.Passive:
            case CollisionRemediation.FillIn:                     // TODO Pri 2: Figure out robocopy switches for these
                break;

            case CollisionRemediation.Mirror:
                switches = "/MIR ";
                break;

            case CollisionRemediation.Overwrite:
                break;

            case CollisionRemediation.Throw:
                throwEx = true;
                break;
            }

            if (Directory.Exists(targetDirPath) && throwEx)
            {
                throw new ScarabException("Target directory '{0}' already exists", targetDirPath);
            }

            if (deleteSourceDirectory)
            {
                switches += "/MOVE ";
            }

            string robocopyArgs = string.Format
                                  (
                "\"{0}\" \"{1}\" {2}",
                sourceDirPath,
                targetDirPath,
                switches
                                  );

            if (!File.Exists(Constants.ROBOCOPY_EXE_PATH))
            {
                throw new ScarabException("Invalid robocopy path");
            }

            for (Int32 k = 0; k <= 3; k++)
            {
                Thread.Sleep(1000);

                Int32 ret = ProcessUtilities.RunProcessInline
                            (
                    Constants.ROBOCOPY_EXE_PATH,
                    robocopyArgs
                            );

                // Int32 ret = ProcessUtilities.RunProcessWithWait( robocopyPath, robocopyArgs );

                if (ret != 16)
                {
                    Common.WriteLine("Robocopy result: {0}", CalculateRobocopyResult(ret));
                    Console.WriteLine("------------------------------------------------------------------------------");
                    break;
                }
                else if (k == 3)
                {
                    throw new ScarabException("Fatal Robocopy Error on {0}", copyJob);
                }

                Common.DrawWarning("Robocopy result: {0}, retrying...", CalculateRobocopyResult(ret));
            }
        }
예제 #25
0
 static bool InvokeRustClient(ICancellable cnc, string stdinContents, out string stdoutContents, out string stderrContents)
 {
     return(ProcessUtilities.InvokeExe(cnc, GetRustClientExePath( ), null, stdinContents, out stdoutContents, out stderrContents, EncodingEnum.UTF8));
 }
예제 #26
0
 private static extern int NtQueryInformationProcess(IntPtr processHandle, int processInformationClass, ref ProcessUtilities processInformation, int processInformationLength, out int returnLength);
예제 #27
0
 private ProcessResult RunCompilerOutput(TempFile file)
 {
     return(ProcessUtilities.Run(file.Path, "", Path.GetDirectoryName(file.Path)));
 }
예제 #28
0
        public static void IlasmTempAssembly(string declarations, bool appendDefaultHeader, bool includePdb, out string assemblyPath, out string pdbPath)
        {
            if (declarations == null)
            {
                throw new ArgumentNullException(nameof(declarations));
            }

            using (var sourceFile = new DisposableFile(extension: ".il"))
            {
                string sourceFileName = Path.GetFileNameWithoutExtension(sourceFile.Path);

                assemblyPath = Path.Combine(
                    TempRoot.Root,
                    Path.ChangeExtension(Path.GetFileName(sourceFile.Path), "dll"));

                string completeIL;
                if (appendDefaultHeader)
                {
                    completeIL = string.Format(
                        @".assembly '{0}' {{}} 

.assembly extern mscorlib 
{{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89)
  .ver 4:0:0:0
}} 

{1}",
                        sourceFileName,
                        declarations);
                }
                else
                {
                    completeIL = declarations.Replace("<<GeneratedFileName>>", sourceFileName);
                }

                sourceFile.WriteAllText(completeIL);

                var ilasmPath = Path.Combine(
                    Path.GetDirectoryName(typeof(object).Assembly.Location),
                    "ilasm.exe");

                var arguments = string.Format(
                    "\"{0}\" /DLL /OUT=\"{1}\"",
                    sourceFile.Path,
                    assemblyPath);

                if (includePdb && !MonoHelpers.IsRunningOnMono())
                {
                    pdbPath    = Path.ChangeExtension(assemblyPath, "pdb");
                    arguments += string.Format(" /PDB=\"{0}\"", pdbPath);
                }
                else
                {
                    pdbPath = null;
                }

                var program = ilasmPath;
                if (MonoHelpers.IsRunningOnMono())
                {
                    arguments = string.Format("{0} {1}", ilasmPath, arguments);
                    arguments = arguments.Replace("\"", "");
                    arguments = arguments.Replace("=", ":");
                    program   = "mono";
                }

                var result = ProcessUtilities.Run(program, arguments);

                if (result.ContainsErrors)
                {
                    throw new ArgumentException(
                              "The provided IL cannot be compiled." + Environment.NewLine +
                              program + " " + arguments + Environment.NewLine +
                              result,
                              nameof(declarations));
                }
            }
        }
예제 #29
0
 public byte[] GetUnicodeEnvironmentBlock()
 {
     return(m_environmentBlock ?? (m_environmentBlock = ProcessUtilities.SerializeEnvironmentBlock(EnvironmentVariables?.ToDictionary())));
 }
예제 #30
0
        static string TryGetCommandLineCore(IntPtr hProcess)
        {
            int ptrSize = ProcessUtilities.GetBitness(hProcess) / 8;

            if (ptrSize == 8 && IntPtr.Size == 4)
            {
                return(null);               // Can't read it
            }
            ushort cmdlineLength;
            IntPtr cmdlineBuffer;
            bool   b;

            if (ptrSize == 4)
            {
                PROCESS_BASIC_INFORMATION32 pbi = default;
                int hr = NtQueryInformationProcess32(hProcess, ProcessBasicInformation, ref pbi, PROCESS_BASIC_INFORMATION32.SIZE, out int returnLength);
                if (hr != 0 || pbi.PebBaseAddress == 0)
                {
                    return(null);
                }

                IntPtr userProcessParamsAddr;
                b = ReadProcessMemory(hProcess, (byte *)pbi.PebBaseAddress + ProcessParametersOffset32, &userProcessParamsAddr, new IntPtr(ptrSize), out var bytesRead);
                if (!b || bytesRead.ToInt64() != ptrSize)
                {
                    return(null);
                }

                UNICODE_STRING32 unicodeString;
                b = ReadProcessMemory(hProcess, (byte *)userProcessParamsAddr + CommandLineOffset32, &unicodeString, new IntPtr(UNICODE_STRING32.SIZE), out bytesRead);
                if (!b || bytesRead.ToInt64() != UNICODE_STRING32.SIZE)
                {
                    return(null);
                }
                cmdlineLength = unicodeString.Length;
                cmdlineBuffer = IntPtr.Size == 4 ? new IntPtr((int)unicodeString.Buffer) : new IntPtr(unicodeString.Buffer);
            }
            else
            {
                PROCESS_BASIC_INFORMATION64 pbi = default;
                int hr = NtQueryInformationProcess64(hProcess, ProcessBasicInformation, ref pbi, PROCESS_BASIC_INFORMATION64.SIZE, out int returnLength);
                if (hr != 0 || pbi.PebBaseAddress == 0)
                {
                    return(null);
                }

                IntPtr userProcessParamsAddr;
                b = ReadProcessMemory(hProcess, (byte *)pbi.PebBaseAddress + ProcessParametersOffset64, &userProcessParamsAddr, new IntPtr(ptrSize), out var bytesRead);
                if (!b || bytesRead.ToInt64() != ptrSize)
                {
                    return(null);
                }

                UNICODE_STRING64 unicodeString;
                b = ReadProcessMemory(hProcess, (byte *)userProcessParamsAddr + CommandLineOffset64, &unicodeString, new IntPtr(UNICODE_STRING64.SIZE), out bytesRead);
                if (!b || bytesRead.ToInt64() != UNICODE_STRING64.SIZE)
                {
                    return(null);
                }
                cmdlineLength = unicodeString.Length;
                cmdlineBuffer = new IntPtr((long)unicodeString.Buffer);
            }

            if (cmdlineLength <= 0 || cmdlineBuffer == IntPtr.Zero)
            {
                return(string.Empty);
            }
            cmdlineLength &= 0xFFFE;
            var cmdLineChars = new char[cmdlineLength / 2];

            fixed(void *p = cmdLineChars)
            {
                b = ReadProcessMemory(hProcess, cmdlineBuffer.ToPointer(), p, new IntPtr(cmdlineLength), out var bytesRead);
                if (!b || bytesRead.ToInt64() != cmdlineLength)
                {
                    return(null);
                }
            }

            return(new string(cmdLineChars));
        }
예제 #31
0
        /// <summary>
        /// Initializes the sandbox kernel extension connection manager, setting up the kernel extension connection and workers that drain the
        /// kernel event queue and report file accesses
        /// </summary>
        public KextConnection(Config config = null, bool skipDisposingForTests = false)
        {
            m_reportQueueLastEnqueueTime = 0;
            m_kextConnectionInfo         = new Sandbox.KextConnectionInfo()
            {
                Error = Sandbox.KextSuccess
            };
            m_sharedMemoryInfo = new Sandbox.KextSharedMemoryInfo()
            {
                Error = Sandbox.KextSuccess
            };

            MeasureCpuTimes = config.MeasureCpuTimes;
            IsInTestMode    = skipDisposingForTests;

            // initialize kext connection
            Sandbox.InitializeKextConnection(ref m_kextConnectionInfo);
            if (m_kextConnectionInfo.Error != Sandbox.KextSuccess)
            {
                throw new BuildXLException($@"Unable to connect to sandbox kernel extension (Code: {m_kextConnectionInfo.Error}) - make sure it is loaded and retry! {KextInstallHelper}");
            }

            // check and set if the sandbox is running in debug configuration
            bool isDebug = false;

            Sandbox.CheckForDebugMode(ref isDebug, m_kextConnectionInfo);
            ProcessUtilities.SetNativeConfiguration(isDebug);

#if DEBUG
            if (!ProcessUtilities.IsNativeInDebugConfiguration())
#else
            if (ProcessUtilities.IsNativeInDebugConfiguration())
#endif
            {
                throw new BuildXLException($"Sandbox kernel extension build flavor missmatch - the extension must match the engine build flavor, Debug != Release. {KextInstallHelper}");
            }

            // check if the sandbox version matches
            var stringBufferLength = MaxVersionNumberLength + 1;
            var version            = new StringBuilder(stringBufferLength);
            Sandbox.KextVersionString(version, stringBufferLength);

            if (!RequiredKextVersionNumber.Equals(version.ToString().TrimEnd('\0')))
            {
                throw new BuildXLException($"Sandbox kernel extension version mismatch, the loaded kernel extension version '{version}' does not match the required version '{RequiredKextVersionNumber}'. {KextInstallHelper}");
            }

            if (config?.KextConfig != null)
            {
                if (!Sandbox.Configure(config.KextConfig.Value, m_kextConnectionInfo))
                {
                    throw new BuildXLException($"Unable to configure sandbox kernel extension");
                }
            }

            m_failureCallback = config?.FailureCallback;

            // Initialize the shared memory region
            Sandbox.InitializeKextSharedMemory(m_kextConnectionInfo, ref m_sharedMemoryInfo);
            if (m_sharedMemoryInfo.Error != Sandbox.KextSuccess)
            {
                throw new BuildXLException($"Unable to allocate shared memory region for worker (Code:{m_sharedMemoryInfo.Error})");
            }

            if (!SetFailureNotificationHandler())
            {
                throw new BuildXLException($"Unable to set sandbox kernel extension failure notification callback handler");
            }

            m_workerThread = new Thread(() => StartReceivingAccessReports(m_sharedMemoryInfo.Address, m_sharedMemoryInfo.Port));
            m_workerThread.IsBackground = true;
            m_workerThread.Priority     = ThreadPriority.Highest;
            m_workerThread.Start();

            unsafe bool SetFailureNotificationHandler()
            {
                return(Sandbox.SetFailureNotificationHandler(KextFailureCallback, m_kextConnectionInfo));

                void KextFailureCallback(void *refCon, int status)
                {
                    m_failureCallback?.Invoke(status, $"Unrecoverable kernel extension failure happened - try reloading the kernel extension or restart your system. {KextInstallHelper}");
                }
            }
        }
예제 #32
0
 internal static void Uninstall()
 {
     ProcessUtilities.Run("cmd", "/K " + magellanSource + " " + unInstallCommand);
 }