8000 Assembly isolation opt-in at dotnet-script level by hrumhurum · Pull Request #637 · dotnet-script/dotnet-script · GitHub
[go: up one dir, main page]

Skip to content

Assembly isolation opt-in at dotnet-script level #637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Dotnet.Script.Core/Dotnet.Script.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
<PackageReference Include="System.Collections.Immutable" Version="5.0.0" />
<PackageReference Include="System.Reflection.Metadata" Version="5.0.0" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
<!-- The following references are just quick fixes for issue #166 and issue #268 -->
<PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" />
<!-- End of quick fixes -->
<PackageReference Include="StrongNamer" Version="0.2.5" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
Expand Down
2 changes: 0 additions & 2 deletions src/Dotnet.Script.Core/ScriptRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ IntPtr OnLoadingUnmanagedDll(ScriptAssemblyLoadContext sender, ScriptAssemblyLoa
scriptAssemblyLoadContext.Loading += OnLoading;
scriptAssemblyLoadContext.LoadingUnmanagedDll += OnLoadingUnmanagedDll;
}
#endif

#if NETCOREAPP3_0_OR_GREATER
using var contextualReflectionScope = assemblyLoadContext != null ? assemblyLoadContext.EnterContextualReflection() : default;
#endif

Expand Down
9 changes: 8 additions & 1 deletion src/Dotnet.Script.Tests/ScriptExecutionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,17 @@ public void ShouldIgnoreGlobalJsonInScriptFolder()
[Fact]
public void ShouldIsolateScriptAssemblies()
{
var result = ScriptTestRunner.Default.ExecuteFixture("Isolation");
var result = ScriptTestRunner.Default.ExecuteFixture("Isolation", "--isolated-load-context");
Assert.Contains("10.0.0.0", result.output);
}

[Fact]
public void ShouldSetCurrentContextualReflectionContext()
{
var result = ScriptTestRunner.Default.ExecuteFixture("CurrentContextualReflectionContext", "--isolated-load-context");
Assert.Contains("Dotnet.Script.Core.ScriptAssemblyLoadContext", result.output);
}

private static string CreateTestScript(string scriptFolder)
{
string script = @"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using System.Runtime.Loader;

var context = AssemblyLoadContext.CurrentContextualReflectionContext;
Console.WriteLine(context?.ToString() ?? "<null>");
27 changes: 15 additions & 12 deletions src/Dotnet.Script/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ private static int Wain(string[] args)
var debugMode = app.Option(DebugFlagShort + " | " + DebugFlagLong, "Enables debug output.", CommandOptionType.NoValue);
var verbosity = app.Option("--verbosity", " Set the verbosity level of the command. Allowed values are t[trace], d[ebug], i[nfo], w[arning], e[rror], and c[ritical].", CommandOptionType.SingleValue);
var nocache = app.Option("--no-cache", "Disable caching (Restore and Dll cache)", CommandOptionType.NoValue);
var isolatedLoadContext = app.Option("--isolated-load-context", "Use isolated assembly load context", CommandOptionType.NoValue);
var infoOption = app.Option("--info", "Displays environmental information", CommandOptionType.NoValue);

var argsBeforeDoubleHyphen = args.TakeWhile(a => a != "--").ToArray();
var argsAfterDoubleHyphen = args.SkipWhile(a => a != "--").Skip(1).ToArray();
var argsAfterDoubleHyphen = args.SkipWhile(a => a != "--").Skip(1).ToArray();

const string helpOptionTemplate = "-? | -h | --help";
app.HelpOption(helpOptionTemplate);
Expand Down Expand Up @@ -99,7 +100,7 @@ private static int Wain(string[] args)
}

var logFactory = CreateLogFactory(verbosity.Value(), debugMode.HasValue());
var options = new ExecuteCodeCommandOptions(source, cwd.Value(), app.RemainingArguments.Concat(argsAfterDoubleHyphen).ToArray(),configuration.ValueEquals("release", StringComparison.OrdinalIgnoreCase) ? OptimizationLevel.Release : OptimizationLevel.Debug, nocache.HasValue(),packageSources.Values?.ToArray());
var options = new ExecuteCodeCommandOptions(source, cwd.Value(), app.RemainingArguments.Concat(argsAfterDoubleHyphen).ToArray(), configuration.ValueEquals("release", StringComparison.OrdinalIgnoreCase) ? OptimizationLevel.Release : OptimizationLevel.Debug, nocache.HasValue(), packageSources.Values?.ToArray());
return await new ExecuteCodeCommand(ScriptConsole.Default, logFactory).Execute<int>(options);
});
});
Expand Down Expand Up @@ -231,11 +232,15 @@ private static int Wain(string[] args)
return 0;
}

AssemblyLoadContext assemblyLoadContext = null;
if (isolatedLoadContext.HasValue())
assemblyLoadContext = new ScriptAssemblyLoadContext();

if (scriptFile.HasValue)
{
if (interactive.HasValue())
{
return await RunInteractiveWithSeed(file.Value, logFactory, scriptArguments, packageSources.Values?.ToArray());
return await RunInteractiveWithSeed(file.Value, logFactory, scriptArguments, packageSources.Values?.ToArray(), assemblyLoadContext);
}

var fileCommandOptions = new ExecuteScriptCommandOptions
Expand All @@ -248,42 +253,40 @@ private static int Wain(string[] args)
nocache.HasValue()
)
{
AssemblyLoadContext = CreateAssemblyLoadContext()
AssemblyLoadContext = assemblyLoadContext
};

var fileCommand = new ExecuteScriptCommand(ScriptConsole.Default, logFactory);
return await fileCommand.Run<int, CommandLineScriptGlobals>(fileCommandOptions);
}
}
else
{
await RunInteractive(!nocache.HasValue(), logFactory, packageSources.Values?.ToArray());
await RunInteractive(!nocache.HasValue(), logFactory, packageSources.Values?.ToArray(), assemblyLoadContext);
}
return exitCode;
});

return app.Execute(argsBeforeDoubleHyphen);
}

private static async Task<int> RunInteractive(bool useRestoreCache, LogFactory logFactory, string[] packageSources)
private static async Task<int> RunInteractive(bool useRestoreCache, LogFactory logFactory, string[] packageSources, AssemblyLoadContext assemblyLoadContext)
{
var options = new ExecuteInteractiveCommandOptions(null, Array.Empty<string>(), packageSources)
{
AssemblyLoadContext = CreateAssemblyLoadContext()
AssemblyLoadContext = assemblyLoadContext
};
await new ExecuteInteractiveCommand(ScriptConsole.Default, logFactory).Execute(options);
return 0;
}

private async static Task<int> RunInteractiveWithSeed(string file, LogFactory logFactory, string[] arguments, string[] packageSources)
private async static Task<int> RunInteractiveWithSeed(string file, LogFactory logFactory, string[] arguments, string[] packageSources, AssemblyLoadContext assemblyLoadContext)
{
var options = new ExecuteInteractiveCommandOptions(new ScriptFile(file), arguments, packageSources)
{
AssemblyLoadContext = CreateAssemblyLoadContext()
AssemblyLoadContext = assemblyLoadContext
};
await new ExecuteInteractiveCommand(ScriptConsole.Default, logFactory).Execute(options);
return 0;
}

static AssemblyLoadContext CreateAssemblyLoadContext() => new ScriptAssemblyLoadContext();
}
}
0