Beispiel #1
0
        public unsafe ExecutionResult Execute(Assembly assembly, RuntimeGuardToken guardToken, IWorkSession session)
        {
            try {
                Output.Reset();
                Flow.Reset();
                Console.SetOut(Output.Writer);

                var main = assembly.EntryPoint;
                if (main == null)
                {
                    throw new ArgumentException("Entry point not found in " + assembly, nameof(assembly));
                }
                using (guardToken.Scope(NewRuntimeGuardSettings())) {
                    var args = main.GetParameters().Length > 0 ? new object[] { new string[0] } : null;

                    PerformanceLog.Checkpoint("Executor.Invoke.Start");
                    var result = main.Invoke(null, args);
                    PerformanceLog.Checkpoint("Executor.Invoke.End");

                    if (main.ReturnType != typeof(void))
                    {
                        result.Inspect("Return");
                    }
                    return(new ExecutionResult(Output.Stream, Flow.Steps));
                }
            }
            catch (Exception ex) {
                PerformanceLog.Checkpoint("Executor.Invoke.Exception");
                if (ex is TargetInvocationException invocationEx)
                {
                    ex = invocationEx.InnerException ?? ex;
                }

                if (ex is RegexMatchTimeoutException)
                {
                    ex = new TimeGuardException("Time limit reached while evaluating a Regex.\r\nNote that timeout was added by SharpLab — in real code this would not throw, but might run for a very long time.", ex);
                }

                if (ex is StackGuardException sgex)
                {
                    throw new Exception($"{sgex.Message} {sgex.StackBaseline} {sgex.StackOffset} {sgex.StackLimit} {sgex.StackSize}");
                }

                Flow.ReportException(ex);
                Output.Write(new SimpleInspection("Exception", ex.ToString()));
                if (ShouldMonitorException(ex))
                {
                    _monitor.Exception(ex !, session);
                }
                return(new ExecutionResult(Output.Stream, Flow.Steps));
            }
        }
        public static unsafe ExecutionResultWithException Execute(Assembly assembly, Guid guardTokenGuid, int processId, bool profilerActive)
        {
            try {
                Console.SetOut(Output.Writer);
                InspectionSettings.CurrentProcessId = processId;
                InspectionSettings.ProfilerActive   = profilerActive;

                var main = assembly.EntryPoint;
                if (main == null)
                {
                    throw new ArgumentException("Entry point not found in " + assembly, nameof(assembly));
                }
                using (new RuntimeGuardToken(guardTokenGuid).Scope(NewRuntimeGuardSettings())) {
                    var args       = main.GetParameters().Length > 0 ? new object[] { new string[0] } : null;
                    var stackStart = stackalloc byte[1];
                    InspectionSettings.StackStart = (ulong)stackStart;
                    var result = main.Invoke(null, args);
                    if (main.ReturnType != typeof(void))
                    {
                        result.Inspect("Return");
                    }
                    return(new ExecutionResultWithException(new ExecutionResult(Output.Stream, Flow.Steps), null));
                }
            }
            catch (Exception ex) {
                if (ex is TargetInvocationException invocationEx)
                {
                    ex = invocationEx.InnerException ?? ex;
                }

                if (ex is RegexMatchTimeoutException)
                {
                    ex = new TimeGuardException("Time limit reached while evaluating a Regex.\r\nNote that timeout was added by SharpLab — in real code this would not throw, but might run for a very long time.", ex);
                }

                if (ex is StackGuardException sgex)
                {
                    throw new Exception($"{sgex.Message} {sgex.StackBaseline} {sgex.StackOffset} {sgex.StackLimit} {sgex.StackSize}");
                }

                Flow.ReportException(ex);
                Output.Write(new SimpleInspection("Exception", ex.ToString()));
                return(new ExecutionResultWithException(new ExecutionResult(Output.Stream, Flow.Steps), ex));
            }
        }
Beispiel #3
0
            public static unsafe ExecutionResultWrapper Execute(Stream assemblyStream, RuntimeGuardToken guardToken, int processId)
            {
                try {
                    Console.SetOut(Output.Writer);
                    InspectionSettings.CurrentProcessId = processId;

                    var assembly = Assembly.Load(ReadAllBytes(assemblyStream));
                    var main     = assembly.EntryPoint;
                    using (guardToken.Scope(NewRuntimeGuardSettings())) {
                        var   args       = main.GetParameters().Length > 0 ? new object[] { new string[0] } : null;
                        byte *stackStart = stackalloc byte[1];
                        InspectionSettings.StackStart = (ulong)stackStart;
                        var result = main.Invoke(null, args);
                        if (main.ReturnType != typeof(void))
                        {
                            result.Inspect("Return");
                        }
                        return(new ExecutionResultWrapper(new ExecutionResult(Output.Stream, Flow.Steps), null));
                    }
                }
                catch (Exception ex) {
                    if (ex is TargetInvocationException invocationEx)
                    {
                        ex = invocationEx.InnerException;
                    }

                    if (ex is RegexMatchTimeoutException)
                    {
                        ex = new TimeGuardException("Time limit reached while evaluating a Regex.\r\nNote that timeout was added by SharpLab — in real code this would not throw, but might run for a very long time.", ex);
                    }

                    Flow.ReportException(ex);
                    ex.Inspect("Exception");
                    return(new ExecutionResultWrapper(new ExecutionResult(Output.Stream, Flow.Steps), ex));
                }
            }