public void LogException(Exception exception, IWorkSession session)
 {
     if (exception is WebSocketException)
     {
         return;
     }
     _monitor.Exception(exception, session);
 }
 public void LogException(Exception exception, IWorkSession session)
 {
     // Note/TODO: need to see if OperationCanceledException can be avoided
     // https://github.com/ashmind/SharpLab/issues/617
     if (exception is WebSocketException or OperationCanceledException)
     {
         return;
     }
     _monitor.Exception(exception, session);
 }
Exemple #3
0
 public void Stop(bool immediate)
 {
     if (immediate)
     {
         return;
     }
     try {
         _monitor.Event("Application Shutdown", null, new Dictionary <string, string> {
             { "Reason", HostingEnvironment.ShutdownReason.ToString() }
         });
     }
     catch (Exception ex) {
         _monitor.Exception(ex, null);
     }
 }
Exemple #4
0
        private ExecutionResult ExecuteInAppDomain(MemoryStream assemblyStream, RuntimeGuardToken guardToken, IWorkSession session)
        {
            var currentSetup = AppDomain.CurrentDomain.SetupInformation;

            using (var context = AppDomainContext.Create(new AppDomainSetup {
                ApplicationBase = currentSetup.ApplicationBase,
                PrivateBinPath = currentSetup.PrivateBinPath
            })) {
                context.LoadAssembly(LoadMethod.LoadFrom, Assembly.GetExecutingAssembly().GetAssemblyFile().FullName);
                var(result, exception) = RemoteFunc.Invoke(context.Domain, assemblyStream, guardToken, CurrentProcess.Id, Remote.Execute);
                if (ShouldMonitorException(exception))
                {
                    _monitor.Exception(exception, session);
                }
                return(result);
            }
        }
Exemple #5
0
        public ExecutionResult Execute(CompilationStreamPair streams, IWorkSession session)
        {
            var readerParameters = new ReaderParameters {
                ReadSymbols          = streams.SymbolStream != null,
                SymbolStream         = streams.SymbolStream,
                AssemblyResolver     = _assemblyResolver,
                SymbolReaderProvider = streams.SymbolStream != null ? _symbolReaderProvider : null
            };

            using (streams)
                using (var definition = AssemblyDefinition.ReadAssembly(streams.AssemblyStream, readerParameters)) {
                    AssemblyLog.Log("1.Initial", definition);

                    foreach (var rewriter in _rewriters)
                    {
                        rewriter.Rewrite(definition, session);
                    }
                    AssemblyLog.Log("2.WithFlow", definition);
                    if (definition.EntryPoint == null)
                    {
                        throw new ArgumentException("Failed to find an entry point (Main?) in assembly.", nameof(streams));
                    }

                    var guardToken = AssemblyGuard.Rewrite(definition, _guardSettings);
                    using (var rewrittenStream = _memoryStreamManager.GetStream()) {
                        definition.Write(rewrittenStream);
                        AssemblyLog.Log("3.Unbreakable", definition);

                        rewrittenStream.Seek(0, SeekOrigin.Begin);
                        var(result, exception) = ExecuteWithIsolation(rewrittenStream, guardToken, session);
                        if (ShouldMonitorException(exception))
                        {
                            _monitor.Exception(exception, session);
                        }

                        return(result);
                    }
                }
        }
Exemple #6
0
        public async Task <object?> ProcessAsync(IWorkSession session, IList <Diagnostic> diagnostics, CancellationToken cancellationToken)
        {
            PerformanceLog.Checkpoint("SlowUpdate.ProcessAsync.Start");
            var targetName = GetAndEnsureTargetName(session);

            _topLevelProgramSupport.UpdateOutputKind(session, diagnostics);

            if (targetName is TargetNames.Ast or TargetNames.Explain)
            {
                var astTarget = _astTargets[session.LanguageName];
                var ast       = await astTarget.GetAstAsync(session, cancellationToken).ConfigureAwait(false);

                if (targetName == TargetNames.Explain)
                {
                    return(await _explainer.ExplainAsync(ast, session, cancellationToken).ConfigureAwait(false));
                }
                return(ast);
            }

            if (diagnostics.Any(d => d.Severity == DiagnosticSeverity.Error))
            {
                return(null);
            }

            if (targetName == LanguageNames.VisualBasic)
            {
                return(VisualBasicNotAvailable);
            }

            if (targetName is not(TargetNames.Run or TargetNames.Verify) && !_decompilers.ContainsKey(targetName))
            {
                throw new NotSupportedException($"Target '{targetName}' is not (yet?) supported by this branch.");
            }

            MemoryStream?assemblyStream = null;
            MemoryStream?symbolStream   = null;

            try {
                assemblyStream = _memoryStreamManager.GetStream();
                if (targetName is TargetNames.Run or TargetNames.IL)
                {
                    symbolStream = _memoryStreamManager.GetStream();
                }

                var compilationStopwatch = session.ShouldReportPerformance() ? Stopwatch.StartNew() : null;
                var compiled             = await _compiler.TryCompileToStreamAsync(assemblyStream, symbolStream, session, diagnostics, cancellationToken).ConfigureAwait(false);

                compilationStopwatch?.Stop();
                if (!compiled.assembly)
                {
                    assemblyStream.Dispose();
                    symbolStream?.Dispose();
                    return(null);
                }

                if (targetName == TargetNames.Verify)
                {
                    assemblyStream.Dispose();
                    symbolStream?.Dispose();
                    return("✔️ Compilation completed.");
                }

                assemblyStream.Seek(0, SeekOrigin.Begin);
                symbolStream?.Seek(0, SeekOrigin.Begin);
                AssemblyLog.Log("1.Compiled", assemblyStream, compiled.symbols ? symbolStream : null);

                var streams = new CompilationStreamPair(assemblyStream, compiled.symbols ? symbolStream : null);
                if (targetName == TargetNames.Run)
                {
                    if (!session.HasContainerExperimentFailed())
                    {
                        try {
                            var output = await _containerExecutor.ExecuteAsync(streams, session, cancellationToken);

                            if (compilationStopwatch != null)
                            {
                                // TODO: Prettify
                                output += $"\n  COMPILATION: {compilationStopwatch.ElapsedMilliseconds,15}ms";
                            }
                            streams.Dispose();
                            _monitor.Metric(ContainerExperimentMetrics.ContainerRunCount, 1);
                            return(output);
                        }
                        catch (Exception ex) {
                            _monitor.Metric(ContainerExperimentMetrics.ContainerFailureCount, 1);
                            _monitor.Exception(ex, session);
                            session.SetContainerExperimentException(ex);
                            assemblyStream.Seek(0, SeekOrigin.Begin);
                            symbolStream?.Seek(0, SeekOrigin.Begin);
                        }
                    }

                    _monitor.Metric(ContainerExperimentMetrics.LegacyRunCount, 1);
                    return(_executor.Execute(streams, session));
                }

                // it's fine not to Dispose() here -- MirrorSharp will dispose it after calling WriteResult()
                return(streams);
            }
            catch {
                assemblyStream?.Dispose();
                symbolStream?.Dispose();
                throw;
            }
        }