2019-05-02 22:45:40 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright 2019 faddenSoft
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Reflection;
|
Reboot sandbox when required
Another chapter in the never-ending AppDomain security saga.
If a computer goes to sleep while SourceGen is running with a project
open, life gets confusing when the system wakes up. The keep-alive
timer fires and a ping is sent to the remote AppDomain, successfully.
At the same time, the lease expires on the remote side, and the objects
are discarded (apparently without bothering to query the ILease object).
This failure mode is 100% repeatable.
Since we can't prevent sandbox objects from disappearing, we have to
detect and recover from the problem. Fortunately we don't keep any
necessary state on the plugin side, so we can just tear the whole
thing down and recreate it.
The various methods in ScriptManager now do a "health check" before
making calls into the plugin AppDomain. If the ping attempt fails,
the AppDomain is "rebooted" by destroying it and creating a new one,
reloading all plugins that were in there before. The plugin binaries
*should* still be in the PluginDllCache directory since the ping failure
was due to objects being discarded, not AppDomain shutdown, and Windows
doesn't let you mess with files that hold executable code.
A new "reboot security sandbox" option has been added to the DEBUG
menu to facilitate testing.
The PluginManager's Ping() method gets called more often, but not to
the extent that performance will be affected.
This change also adds a finalizer to DisasmProject, since we're relying
on it to shut down the ScriptManager, and it's relying on callers to
invoke its cleanup function. The finalizer throws an assertion if the
cleanup function doesn't get called.
(Issue #82)
2020-07-19 20:20:18 +00:00
|
|
|
|
using System.Runtime.Remoting;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
using CommonUtil;
|
|
|
|
|
using PluginCommon;
|
|
|
|
|
|
2019-07-20 20:28:10 +00:00
|
|
|
|
namespace SourceGen.Sandbox {
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Maintains a collection of IPlugin instances, or communicates with the remote
|
|
|
|
|
/// PluginManager that holds the collection. Whether the plugins are instantiated
|
|
|
|
|
/// locally depends on how the class is constructed.
|
Reboot sandbox when required
Another chapter in the never-ending AppDomain security saga.
If a computer goes to sleep while SourceGen is running with a project
open, life gets confusing when the system wakes up. The keep-alive
timer fires and a ping is sent to the remote AppDomain, successfully.
At the same time, the lease expires on the remote side, and the objects
are discarded (apparently without bothering to query the ILease object).
This failure mode is 100% repeatable.
Since we can't prevent sandbox objects from disappearing, we have to
detect and recover from the problem. Fortunately we don't keep any
necessary state on the plugin side, so we can just tear the whole
thing down and recreate it.
The various methods in ScriptManager now do a "health check" before
making calls into the plugin AppDomain. If the ping attempt fails,
the AppDomain is "rebooted" by destroying it and creating a new one,
reloading all plugins that were in there before. The plugin binaries
*should* still be in the PluginDllCache directory since the ping failure
was due to objects being discarded, not AppDomain shutdown, and Windows
doesn't let you mess with files that hold executable code.
A new "reboot security sandbox" option has been added to the DEBUG
menu to facilitate testing.
The PluginManager's Ping() method gets called more often, but not to
the extent that performance will be affected.
This change also adds a finalizer to DisasmProject, since we're relying
on it to shut down the ScriptManager, and it's relying on callers to
invoke its cleanup function. The finalizer throws an assertion if the
cleanup function doesn't get called.
(Issue #82)
2020-07-19 20:20:18 +00:00
|
|
|
|
///
|
|
|
|
|
/// One of these will be instantiated when the DisasmProject is created.
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
public class ScriptManager {
|
|
|
|
|
public const string FILENAME_EXT = ".cs";
|
|
|
|
|
public static readonly string FILENAME_FILTER = Res.Strings.FILE_FILTER_CS;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// If true, the DomainManager will use the keep-alive timer hack.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static bool UseKeepAliveHack { get; set; }
|
|
|
|
|
|
Reboot sandbox when required
Another chapter in the never-ending AppDomain security saga.
If a computer goes to sleep while SourceGen is running with a project
open, life gets confusing when the system wakes up. The keep-alive
timer fires and a ping is sent to the remote AppDomain, successfully.
At the same time, the lease expires on the remote side, and the objects
are discarded (apparently without bothering to query the ILease object).
This failure mode is 100% repeatable.
Since we can't prevent sandbox objects from disappearing, we have to
detect and recover from the problem. Fortunately we don't keep any
necessary state on the plugin side, so we can just tear the whole
thing down and recreate it.
The various methods in ScriptManager now do a "health check" before
making calls into the plugin AppDomain. If the ping attempt fails,
the AppDomain is "rebooted" by destroying it and creating a new one,
reloading all plugins that were in there before. The plugin binaries
*should* still be in the PluginDllCache directory since the ping failure
was due to objects being discarded, not AppDomain shutdown, and Windows
doesn't let you mess with files that hold executable code.
A new "reboot security sandbox" option has been added to the DEBUG
menu to facilitate testing.
The PluginManager's Ping() method gets called more often, but not to
the extent that performance will be affected.
This change also adds a finalizer to DisasmProject, since we're relying
on it to shut down the ScriptManager, and it's relying on callers to
invoke its cleanup function. The finalizer throws an assertion if the
cleanup function doesn't get called.
(Issue #82)
2020-07-19 20:20:18 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// If true, this ScriptManager is not using a DomainManager.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool UseMainAppDomain {
|
|
|
|
|
get { return DomainMgr == null; }
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reference to DomainManager, if we're using one.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public DomainManager DomainMgr { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Collection of loaded plugins, if we're not using a DomainManager.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private Dictionary<string, IPlugin> mActivePlugins;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reference to project, from which we can get the file data and project path name.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private DisasmProject mProject;
|
|
|
|
|
|
Reboot sandbox when required
Another chapter in the never-ending AppDomain security saga.
If a computer goes to sleep while SourceGen is running with a project
open, life gets confusing when the system wakes up. The keep-alive
timer fires and a ping is sent to the remote AppDomain, successfully.
At the same time, the lease expires on the remote side, and the objects
are discarded (apparently without bothering to query the ILease object).
This failure mode is 100% repeatable.
Since we can't prevent sandbox objects from disappearing, we have to
detect and recover from the problem. Fortunately we don't keep any
necessary state on the plugin side, so we can just tear the whole
thing down and recreate it.
The various methods in ScriptManager now do a "health check" before
making calls into the plugin AppDomain. If the ping attempt fails,
the AppDomain is "rebooted" by destroying it and creating a new one,
reloading all plugins that were in there before. The plugin binaries
*should* still be in the PluginDllCache directory since the ping failure
was due to objects being discarded, not AppDomain shutdown, and Windows
doesn't let you mess with files that hold executable code.
A new "reboot security sandbox" option has been added to the DEBUG
menu to facilitate testing.
The PluginManager's Ping() method gets called more often, but not to
the extent that performance will be affected.
This change also adds a finalizer to DisasmProject, since we're relying
on it to shut down the ScriptManager, and it's relying on callers to
invoke its cleanup function. The finalizer throws an assertion if the
cleanup function doesn't get called.
(Issue #82)
2020-07-19 20:20:18 +00:00
|
|
|
|
private class LoadedPluginPath {
|
|
|
|
|
public string ScriptIdent { get; private set; }
|
|
|
|
|
public string DllPath { get; private set; }
|
|
|
|
|
|
|
|
|
|
public LoadedPluginPath(string scriptIdent, string dllPath) {
|
|
|
|
|
ScriptIdent = scriptIdent;
|
|
|
|
|
DllPath = dllPath;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// List of paths to loaded plugins. Used if we need to "reboot" the sandbox.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private List<LoadedPluginPath> mLoadedPlugins = new List<LoadedPluginPath>();
|
|
|
|
|
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Constructor.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public ScriptManager(DisasmProject proj) {
|
|
|
|
|
mProject = proj;
|
|
|
|
|
|
|
|
|
|
if (!proj.UseMainAppDomainForPlugins) {
|
Reboot sandbox when required
Another chapter in the never-ending AppDomain security saga.
If a computer goes to sleep while SourceGen is running with a project
open, life gets confusing when the system wakes up. The keep-alive
timer fires and a ping is sent to the remote AppDomain, successfully.
At the same time, the lease expires on the remote side, and the objects
are discarded (apparently without bothering to query the ILease object).
This failure mode is 100% repeatable.
Since we can't prevent sandbox objects from disappearing, we have to
detect and recover from the problem. Fortunately we don't keep any
necessary state on the plugin side, so we can just tear the whole
thing down and recreate it.
The various methods in ScriptManager now do a "health check" before
making calls into the plugin AppDomain. If the ping attempt fails,
the AppDomain is "rebooted" by destroying it and creating a new one,
reloading all plugins that were in there before. The plugin binaries
*should* still be in the PluginDllCache directory since the ping failure
was due to objects being discarded, not AppDomain shutdown, and Windows
doesn't let you mess with files that hold executable code.
A new "reboot security sandbox" option has been added to the DEBUG
menu to facilitate testing.
The PluginManager's Ping() method gets called more often, but not to
the extent that performance will be affected.
This change also adds a finalizer to DisasmProject, since we're relying
on it to shut down the ScriptManager, and it's relying on callers to
invoke its cleanup function. The finalizer throws an assertion if the
cleanup function doesn't get called.
(Issue #82)
2020-07-19 20:20:18 +00:00
|
|
|
|
CreateDomainManager();
|
2019-05-02 22:45:40 +00:00
|
|
|
|
} else {
|
|
|
|
|
mActivePlugins = new Dictionary<string, IPlugin>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Reboot sandbox when required
Another chapter in the never-ending AppDomain security saga.
If a computer goes to sleep while SourceGen is running with a project
open, life gets confusing when the system wakes up. The keep-alive
timer fires and a ping is sent to the remote AppDomain, successfully.
At the same time, the lease expires on the remote side, and the objects
are discarded (apparently without bothering to query the ILease object).
This failure mode is 100% repeatable.
Since we can't prevent sandbox objects from disappearing, we have to
detect and recover from the problem. Fortunately we don't keep any
necessary state on the plugin side, so we can just tear the whole
thing down and recreate it.
The various methods in ScriptManager now do a "health check" before
making calls into the plugin AppDomain. If the ping attempt fails,
the AppDomain is "rebooted" by destroying it and creating a new one,
reloading all plugins that were in there before. The plugin binaries
*should* still be in the PluginDllCache directory since the ping failure
was due to objects being discarded, not AppDomain shutdown, and Windows
doesn't let you mess with files that hold executable code.
A new "reboot security sandbox" option has been added to the DEBUG
menu to facilitate testing.
The PluginManager's Ping() method gets called more often, but not to
the extent that performance will be affected.
This change also adds a finalizer to DisasmProject, since we're relying
on it to shut down the ScriptManager, and it's relying on callers to
invoke its cleanup function. The finalizer throws an assertion if the
cleanup function doesn't get called.
(Issue #82)
2020-07-19 20:20:18 +00:00
|
|
|
|
private void CreateDomainManager() {
|
|
|
|
|
// The project's UseMainAppDomainForPlugins value is theoretically mutable, so
|
|
|
|
|
// don't try to assert it here.
|
|
|
|
|
DomainMgr = new DomainManager(UseKeepAliveHack);
|
|
|
|
|
DomainMgr.CreateDomain("Plugin Domain", PluginDllCache.GetPluginDirPath());
|
|
|
|
|
DomainMgr.PluginMgr.SetFileData(mProject.FileData);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Cleans up, discarding the AppDomain if one was created. Do not continue to use
|
|
|
|
|
/// the object after calling this.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Cleanup() {
|
|
|
|
|
if (DomainMgr != null) {
|
|
|
|
|
DomainMgr.Dispose();
|
|
|
|
|
DomainMgr = null;
|
|
|
|
|
}
|
|
|
|
|
mActivePlugins = null;
|
|
|
|
|
mProject = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Clears the list of plugins. This does not unload assemblies. Call this when
|
|
|
|
|
/// the list of extension scripts configured into the project has changed.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Clear() {
|
|
|
|
|
if (DomainMgr == null) {
|
|
|
|
|
mActivePlugins.Clear();
|
|
|
|
|
} else {
|
Reboot sandbox when required
Another chapter in the never-ending AppDomain security saga.
If a computer goes to sleep while SourceGen is running with a project
open, life gets confusing when the system wakes up. The keep-alive
timer fires and a ping is sent to the remote AppDomain, successfully.
At the same time, the lease expires on the remote side, and the objects
are discarded (apparently without bothering to query the ILease object).
This failure mode is 100% repeatable.
Since we can't prevent sandbox objects from disappearing, we have to
detect and recover from the problem. Fortunately we don't keep any
necessary state on the plugin side, so we can just tear the whole
thing down and recreate it.
The various methods in ScriptManager now do a "health check" before
making calls into the plugin AppDomain. If the ping attempt fails,
the AppDomain is "rebooted" by destroying it and creating a new one,
reloading all plugins that were in there before. The plugin binaries
*should* still be in the PluginDllCache directory since the ping failure
was due to objects being discarded, not AppDomain shutdown, and Windows
doesn't let you mess with files that hold executable code.
A new "reboot security sandbox" option has been added to the DEBUG
menu to facilitate testing.
The PluginManager's Ping() method gets called more often, but not to
the extent that performance will be affected.
This change also adds a finalizer to DisasmProject, since we're relying
on it to shut down the ScriptManager, and it's relying on callers to
invoke its cleanup function. The finalizer throws an assertion if the
cleanup function doesn't get called.
(Issue #82)
2020-07-19 20:20:18 +00:00
|
|
|
|
CheckHealth();
|
2019-05-02 22:45:40 +00:00
|
|
|
|
DomainMgr.PluginMgr.ClearPluginList();
|
|
|
|
|
}
|
Reboot sandbox when required
Another chapter in the never-ending AppDomain security saga.
If a computer goes to sleep while SourceGen is running with a project
open, life gets confusing when the system wakes up. The keep-alive
timer fires and a ping is sent to the remote AppDomain, successfully.
At the same time, the lease expires on the remote side, and the objects
are discarded (apparently without bothering to query the ILease object).
This failure mode is 100% repeatable.
Since we can't prevent sandbox objects from disappearing, we have to
detect and recover from the problem. Fortunately we don't keep any
necessary state on the plugin side, so we can just tear the whole
thing down and recreate it.
The various methods in ScriptManager now do a "health check" before
making calls into the plugin AppDomain. If the ping attempt fails,
the AppDomain is "rebooted" by destroying it and creating a new one,
reloading all plugins that were in there before. The plugin binaries
*should* still be in the PluginDllCache directory since the ping failure
was due to objects being discarded, not AppDomain shutdown, and Windows
doesn't let you mess with files that hold executable code.
A new "reboot security sandbox" option has been added to the DEBUG
menu to facilitate testing.
The PluginManager's Ping() method gets called more often, but not to
the extent that performance will be affected.
This change also adds a finalizer to DisasmProject, since we're relying
on it to shut down the ScriptManager, and it's relying on callers to
invoke its cleanup function. The finalizer throws an assertion if the
cleanup function doesn't get called.
(Issue #82)
2020-07-19 20:20:18 +00:00
|
|
|
|
mLoadedPlugins.Clear();
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Attempts to load the specified plugin. If the plugin is already loaded, this
|
|
|
|
|
/// does nothing. If not, the assembly is loaded and an instance is created.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="scriptIdent">Script identifier.</param>
|
|
|
|
|
/// <param name="report">Report with errors and warnings.</param>
|
|
|
|
|
/// <returns>True on success.</returns>
|
|
|
|
|
public bool LoadPlugin(string scriptIdent, out FileLoadReport report) {
|
|
|
|
|
// Make sure the most recent version is compiled.
|
|
|
|
|
string dllPath = PluginDllCache.GenerateScriptDll(scriptIdent,
|
|
|
|
|
mProject.ProjectPathName, out report);
|
|
|
|
|
if (dllPath == null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (DomainMgr == null) {
|
|
|
|
|
if (mActivePlugins.TryGetValue(scriptIdent, out IPlugin plugin)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
Assembly asm = Assembly.LoadFile(dllPath);
|
|
|
|
|
plugin = PluginDllCache.ConstructIPlugin(asm);
|
|
|
|
|
mActivePlugins.Add(scriptIdent, plugin);
|
|
|
|
|
report = new FileLoadReport(dllPath); // empty report
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
Reboot sandbox when required
Another chapter in the never-ending AppDomain security saga.
If a computer goes to sleep while SourceGen is running with a project
open, life gets confusing when the system wakes up. The keep-alive
timer fires and a ping is sent to the remote AppDomain, successfully.
At the same time, the lease expires on the remote side, and the objects
are discarded (apparently without bothering to query the ILease object).
This failure mode is 100% repeatable.
Since we can't prevent sandbox objects from disappearing, we have to
detect and recover from the problem. Fortunately we don't keep any
necessary state on the plugin side, so we can just tear the whole
thing down and recreate it.
The various methods in ScriptManager now do a "health check" before
making calls into the plugin AppDomain. If the ping attempt fails,
the AppDomain is "rebooted" by destroying it and creating a new one,
reloading all plugins that were in there before. The plugin binaries
*should* still be in the PluginDllCache directory since the ping failure
was due to objects being discarded, not AppDomain shutdown, and Windows
doesn't let you mess with files that hold executable code.
A new "reboot security sandbox" option has been added to the DEBUG
menu to facilitate testing.
The PluginManager's Ping() method gets called more often, but not to
the extent that performance will be affected.
This change also adds a finalizer to DisasmProject, since we're relying
on it to shut down the ScriptManager, and it's relying on callers to
invoke its cleanup function. The finalizer throws an assertion if the
cleanup function doesn't get called.
(Issue #82)
2020-07-19 20:20:18 +00:00
|
|
|
|
CheckHealth();
|
2020-01-21 19:02:36 +00:00
|
|
|
|
IPlugin plugin = DomainMgr.PluginMgr.LoadPlugin(dllPath, scriptIdent,
|
|
|
|
|
out string failMsg);
|
|
|
|
|
if (plugin == null) {
|
|
|
|
|
report.Add(FileLoadItem.Type.Error, "Failed loading plugin: " + failMsg);
|
Reboot sandbox when required
Another chapter in the never-ending AppDomain security saga.
If a computer goes to sleep while SourceGen is running with a project
open, life gets confusing when the system wakes up. The keep-alive
timer fires and a ping is sent to the remote AppDomain, successfully.
At the same time, the lease expires on the remote side, and the objects
are discarded (apparently without bothering to query the ILease object).
This failure mode is 100% repeatable.
Since we can't prevent sandbox objects from disappearing, we have to
detect and recover from the problem. Fortunately we don't keep any
necessary state on the plugin side, so we can just tear the whole
thing down and recreate it.
The various methods in ScriptManager now do a "health check" before
making calls into the plugin AppDomain. If the ping attempt fails,
the AppDomain is "rebooted" by destroying it and creating a new one,
reloading all plugins that were in there before. The plugin binaries
*should* still be in the PluginDllCache directory since the ping failure
was due to objects being discarded, not AppDomain shutdown, and Windows
doesn't let you mess with files that hold executable code.
A new "reboot security sandbox" option has been added to the DEBUG
menu to facilitate testing.
The PluginManager's Ping() method gets called more often, but not to
the extent that performance will be affected.
This change also adds a finalizer to DisasmProject, since we're relying
on it to shut down the ScriptManager, and it's relying on callers to
invoke its cleanup function. The finalizer throws an assertion if the
cleanup function doesn't get called.
(Issue #82)
2020-07-19 20:20:18 +00:00
|
|
|
|
} else {
|
|
|
|
|
mLoadedPlugins.Add(new LoadedPluginPath(scriptIdent, dllPath));
|
2020-01-21 19:02:36 +00:00
|
|
|
|
}
|
2019-05-02 22:45:40 +00:00
|
|
|
|
return plugin != null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Reboot sandbox when required
Another chapter in the never-ending AppDomain security saga.
If a computer goes to sleep while SourceGen is running with a project
open, life gets confusing when the system wakes up. The keep-alive
timer fires and a ping is sent to the remote AppDomain, successfully.
At the same time, the lease expires on the remote side, and the objects
are discarded (apparently without bothering to query the ILease object).
This failure mode is 100% repeatable.
Since we can't prevent sandbox objects from disappearing, we have to
detect and recover from the problem. Fortunately we don't keep any
necessary state on the plugin side, so we can just tear the whole
thing down and recreate it.
The various methods in ScriptManager now do a "health check" before
making calls into the plugin AppDomain. If the ping attempt fails,
the AppDomain is "rebooted" by destroying it and creating a new one,
reloading all plugins that were in there before. The plugin binaries
*should* still be in the PluginDllCache directory since the ping failure
was due to objects being discarded, not AppDomain shutdown, and Windows
doesn't let you mess with files that hold executable code.
A new "reboot security sandbox" option has been added to the DEBUG
menu to facilitate testing.
The PluginManager's Ping() method gets called more often, but not to
the extent that performance will be affected.
This change also adds a finalizer to DisasmProject, since we're relying
on it to shut down the ScriptManager, and it's relying on callers to
invoke its cleanup function. The finalizer throws an assertion if the
cleanup function doesn't get called.
(Issue #82)
2020-07-19 20:20:18 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Reboots the sandbox by discarding the old DomainManager, creating a new one, and
|
|
|
|
|
/// reloading all of the plugins.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>True if no problems were encountered.</returns>
|
|
|
|
|
public bool RebootSandbox() {
|
|
|
|
|
if (DomainMgr == null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
Debug.WriteLine("Rebooting sandbox...");
|
|
|
|
|
|
|
|
|
|
// Discard existing DomainManager, and create a new one.
|
|
|
|
|
DomainMgr.Dispose();
|
|
|
|
|
CreateDomainManager();
|
|
|
|
|
|
|
|
|
|
bool failed = false;
|
|
|
|
|
|
|
|
|
|
// Reload plugins.
|
|
|
|
|
foreach (LoadedPluginPath lpp in mLoadedPlugins) {
|
|
|
|
|
IPlugin plugin = DomainMgr.PluginMgr.LoadPlugin(lpp.DllPath, lpp.ScriptIdent,
|
|
|
|
|
out string failMsg);
|
|
|
|
|
if (plugin == null) {
|
|
|
|
|
// This is unexpected; we're opening a DLL that we recently had open.
|
|
|
|
|
// Not a lot we can do to recover, and we're probably too deep to report
|
|
|
|
|
// a failure to the user.
|
|
|
|
|
Debug.WriteLine("Failed to reopen '" + lpp.DllPath + "': " + failMsg);
|
|
|
|
|
failed = true;
|
|
|
|
|
// continue on to the next one
|
|
|
|
|
} else {
|
|
|
|
|
Debug.WriteLine(" Reloaded " + lpp.ScriptIdent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return failed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Checks the health of the sandbox, and reboots it if it seems unhealthy. Call this
|
|
|
|
|
/// before making any calls into plugins via DomainMgr.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// We're relying on the idea that, if the ping succeeds, the PluginManager instance
|
|
|
|
|
/// will continue to exist for a while. There is some evidence to the contrary -- the
|
|
|
|
|
/// ping issued immediately after the machine wakes up succeeds right before the remote
|
|
|
|
|
/// objects get discarded -- but I'm hoping that's due to a race condition that won't
|
|
|
|
|
/// happen in normal circumstances (because of the keep-alives we send).
|
|
|
|
|
/// </remarks>
|
|
|
|
|
private void CheckHealth() {
|
|
|
|
|
Debug.Assert(DomainMgr != null);
|
|
|
|
|
try {
|
|
|
|
|
DomainMgr.PluginMgr.Ping(111);
|
|
|
|
|
} catch (RemotingException re) {
|
|
|
|
|
Debug.WriteLine("Health check failed: " + re.Message);
|
|
|
|
|
RebootSandbox();
|
|
|
|
|
DomainMgr.PluginMgr.Ping(112);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-02 22:45:40 +00:00
|
|
|
|
public IPlugin GetInstance(string scriptIdent) {
|
|
|
|
|
if (DomainMgr == null) {
|
|
|
|
|
if (mActivePlugins.TryGetValue(scriptIdent, out IPlugin plugin)) {
|
|
|
|
|
return plugin;
|
|
|
|
|
}
|
|
|
|
|
Debug.Assert(false);
|
|
|
|
|
return null;
|
|
|
|
|
} else {
|
Reboot sandbox when required
Another chapter in the never-ending AppDomain security saga.
If a computer goes to sleep while SourceGen is running with a project
open, life gets confusing when the system wakes up. The keep-alive
timer fires and a ping is sent to the remote AppDomain, successfully.
At the same time, the lease expires on the remote side, and the objects
are discarded (apparently without bothering to query the ILease object).
This failure mode is 100% repeatable.
Since we can't prevent sandbox objects from disappearing, we have to
detect and recover from the problem. Fortunately we don't keep any
necessary state on the plugin side, so we can just tear the whole
thing down and recreate it.
The various methods in ScriptManager now do a "health check" before
making calls into the plugin AppDomain. If the ping attempt fails,
the AppDomain is "rebooted" by destroying it and creating a new one,
reloading all plugins that were in there before. The plugin binaries
*should* still be in the PluginDllCache directory since the ping failure
was due to objects being discarded, not AppDomain shutdown, and Windows
doesn't let you mess with files that hold executable code.
A new "reboot security sandbox" option has been added to the DEBUG
menu to facilitate testing.
The PluginManager's Ping() method gets called more often, but not to
the extent that performance will be affected.
This change also adds a finalizer to DisasmProject, since we're relying
on it to shut down the ScriptManager, and it's relying on callers to
invoke its cleanup function. The finalizer throws an assertion if the
cleanup function doesn't get called.
(Issue #82)
2020-07-19 20:20:18 +00:00
|
|
|
|
CheckHealth();
|
2019-05-02 22:45:40 +00:00
|
|
|
|
return DomainMgr.PluginMgr.GetPlugin(scriptIdent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Generates a list of references to instances of loaded plugins.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>Newly-created list of plugin references.</returns>
|
|
|
|
|
public List<IPlugin> GetAllInstances() {
|
2019-12-28 22:00:48 +00:00
|
|
|
|
Dictionary<string, IPlugin> dict;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
if (DomainMgr == null) {
|
2019-12-28 22:00:48 +00:00
|
|
|
|
dict = mActivePlugins;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
} else {
|
Reboot sandbox when required
Another chapter in the never-ending AppDomain security saga.
If a computer goes to sleep while SourceGen is running with a project
open, life gets confusing when the system wakes up. The keep-alive
timer fires and a ping is sent to the remote AppDomain, successfully.
At the same time, the lease expires on the remote side, and the objects
are discarded (apparently without bothering to query the ILease object).
This failure mode is 100% repeatable.
Since we can't prevent sandbox objects from disappearing, we have to
detect and recover from the problem. Fortunately we don't keep any
necessary state on the plugin side, so we can just tear the whole
thing down and recreate it.
The various methods in ScriptManager now do a "health check" before
making calls into the plugin AppDomain. If the ping attempt fails,
the AppDomain is "rebooted" by destroying it and creating a new one,
reloading all plugins that were in there before. The plugin binaries
*should* still be in the PluginDllCache directory since the ping failure
was due to objects being discarded, not AppDomain shutdown, and Windows
doesn't let you mess with files that hold executable code.
A new "reboot security sandbox" option has been added to the DEBUG
menu to facilitate testing.
The PluginManager's Ping() method gets called more often, but not to
the extent that performance will be affected.
This change also adds a finalizer to DisasmProject, since we're relying
on it to shut down the ScriptManager, and it's relying on callers to
invoke its cleanup function. The finalizer throws an assertion if the
cleanup function doesn't get called.
(Issue #82)
2020-07-19 20:20:18 +00:00
|
|
|
|
CheckHealth();
|
2019-12-28 22:00:48 +00:00
|
|
|
|
dict = DomainMgr.PluginMgr.GetActivePlugins();
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
2019-12-28 22:00:48 +00:00
|
|
|
|
List<IPlugin> list = new List<IPlugin>(dict.Count);
|
|
|
|
|
foreach (KeyValuePair<string, IPlugin> kvp in dict) {
|
|
|
|
|
list.Add(kvp.Value);
|
|
|
|
|
}
|
|
|
|
|
return list;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Prepares all active scripts for action.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="appRef">Reference to object providing app services.</param>
|
|
|
|
|
public void PrepareScripts(IApplication appRef) {
|
2019-10-04 23:53:31 +00:00
|
|
|
|
List<PlSymbol> plSyms = GeneratePlSymbolList();
|
2019-05-02 22:45:40 +00:00
|
|
|
|
|
|
|
|
|
if (DomainMgr == null) {
|
2019-10-07 01:13:39 +00:00
|
|
|
|
AddressTranslate addrTrans = new AddressTranslate(mProject.AddrMap);
|
2019-05-02 22:45:40 +00:00
|
|
|
|
foreach (KeyValuePair<string, IPlugin> kvp in mActivePlugins) {
|
2019-10-14 01:19:28 +00:00
|
|
|
|
IPlugin ipl = kvp.Value;
|
|
|
|
|
ipl.Prepare(appRef, mProject.FileData, addrTrans);
|
|
|
|
|
if (ipl is IPlugin_SymbolList) {
|
|
|
|
|
((IPlugin_SymbolList)ipl).UpdateSymbolList(plSyms);
|
|
|
|
|
}
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
Reboot sandbox when required
Another chapter in the never-ending AppDomain security saga.
If a computer goes to sleep while SourceGen is running with a project
open, life gets confusing when the system wakes up. The keep-alive
timer fires and a ping is sent to the remote AppDomain, successfully.
At the same time, the lease expires on the remote side, and the objects
are discarded (apparently without bothering to query the ILease object).
This failure mode is 100% repeatable.
Since we can't prevent sandbox objects from disappearing, we have to
detect and recover from the problem. Fortunately we don't keep any
necessary state on the plugin side, so we can just tear the whole
thing down and recreate it.
The various methods in ScriptManager now do a "health check" before
making calls into the plugin AppDomain. If the ping attempt fails,
the AppDomain is "rebooted" by destroying it and creating a new one,
reloading all plugins that were in there before. The plugin binaries
*should* still be in the PluginDllCache directory since the ping failure
was due to objects being discarded, not AppDomain shutdown, and Windows
doesn't let you mess with files that hold executable code.
A new "reboot security sandbox" option has been added to the DEBUG
menu to facilitate testing.
The PluginManager's Ping() method gets called more often, but not to
the extent that performance will be affected.
This change also adds a finalizer to DisasmProject, since we're relying
on it to shut down the ScriptManager, and it's relying on callers to
invoke its cleanup function. The finalizer throws an assertion if the
cleanup function doesn't get called.
(Issue #82)
2020-07-19 20:20:18 +00:00
|
|
|
|
CheckHealth();
|
2019-10-07 01:13:39 +00:00
|
|
|
|
List<AddressMap.AddressMapEntry> addrEnts = mProject.AddrMap.GetEntryList();
|
|
|
|
|
DomainMgr.PluginMgr.PreparePlugins(appRef, addrEnts, plSyms);
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-01 01:52:33 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Puts scripts back to sleep.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void UnprepareScripts() {
|
|
|
|
|
if (DomainMgr == null) {
|
|
|
|
|
foreach (KeyValuePair<string, IPlugin> kvp in mActivePlugins) {
|
|
|
|
|
IPlugin ipl = kvp.Value;
|
|
|
|
|
ipl.Unprepare();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
Reboot sandbox when required
Another chapter in the never-ending AppDomain security saga.
If a computer goes to sleep while SourceGen is running with a project
open, life gets confusing when the system wakes up. The keep-alive
timer fires and a ping is sent to the remote AppDomain, successfully.
At the same time, the lease expires on the remote side, and the objects
are discarded (apparently without bothering to query the ILease object).
This failure mode is 100% repeatable.
Since we can't prevent sandbox objects from disappearing, we have to
detect and recover from the problem. Fortunately we don't keep any
necessary state on the plugin side, so we can just tear the whole
thing down and recreate it.
The various methods in ScriptManager now do a "health check" before
making calls into the plugin AppDomain. If the ping attempt fails,
the AppDomain is "rebooted" by destroying it and creating a new one,
reloading all plugins that were in there before. The plugin binaries
*should* still be in the PluginDllCache directory since the ping failure
was due to objects being discarded, not AppDomain shutdown, and Windows
doesn't let you mess with files that hold executable code.
A new "reboot security sandbox" option has been added to the DEBUG
menu to facilitate testing.
The PluginManager's Ping() method gets called more often, but not to
the extent that performance will be affected.
This change also adds a finalizer to DisasmProject, since we're relying
on it to shut down the ScriptManager, and it's relying on callers to
invoke its cleanup function. The finalizer throws an assertion if the
cleanup function doesn't get called.
(Issue #82)
2020-07-19 20:20:18 +00:00
|
|
|
|
CheckHealth();
|
2019-12-01 01:52:33 +00:00
|
|
|
|
DomainMgr.PluginMgr.UnpreparePlugins();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-10-14 01:19:28 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns true if any of the plugins report that the before or after label is
|
|
|
|
|
/// significant.
|
|
|
|
|
/// </summary>
|
Reboot sandbox when required
Another chapter in the never-ending AppDomain security saga.
If a computer goes to sleep while SourceGen is running with a project
open, life gets confusing when the system wakes up. The keep-alive
timer fires and a ping is sent to the remote AppDomain, successfully.
At the same time, the lease expires on the remote side, and the objects
are discarded (apparently without bothering to query the ILease object).
This failure mode is 100% repeatable.
Since we can't prevent sandbox objects from disappearing, we have to
detect and recover from the problem. Fortunately we don't keep any
necessary state on the plugin side, so we can just tear the whole
thing down and recreate it.
The various methods in ScriptManager now do a "health check" before
making calls into the plugin AppDomain. If the ping attempt fails,
the AppDomain is "rebooted" by destroying it and creating a new one,
reloading all plugins that were in there before. The plugin binaries
*should* still be in the PluginDllCache directory since the ping failure
was due to objects being discarded, not AppDomain shutdown, and Windows
doesn't let you mess with files that hold executable code.
A new "reboot security sandbox" option has been added to the DEBUG
menu to facilitate testing.
The PluginManager's Ping() method gets called more often, but not to
the extent that performance will be affected.
This change also adds a finalizer to DisasmProject, since we're relying
on it to shut down the ScriptManager, and it's relying on callers to
invoke its cleanup function. The finalizer throws an assertion if the
cleanup function doesn't get called.
(Issue #82)
2020-07-19 20:20:18 +00:00
|
|
|
|
/// <remarks>
|
|
|
|
|
/// This is called when a label is edited, so DisasmProject can decide whether it
|
|
|
|
|
/// needs to re-run the code analyzer.
|
|
|
|
|
/// </remarks>
|
2019-10-14 01:19:28 +00:00
|
|
|
|
public bool IsLabelSignificant(Symbol before, Symbol after) {
|
|
|
|
|
string labelBefore = (before == null) ? string.Empty : before.Label;
|
|
|
|
|
string labelAfter = (after == null) ? string.Empty : after.Label;
|
|
|
|
|
if (DomainMgr == null) {
|
|
|
|
|
foreach (KeyValuePair<string, IPlugin> kvp in mActivePlugins) {
|
|
|
|
|
IPlugin ipl = kvp.Value;
|
|
|
|
|
if (ipl is IPlugin_SymbolList &&
|
|
|
|
|
((IPlugin_SymbolList)ipl).IsLabelSignificant(labelBefore,
|
|
|
|
|
labelAfter)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
Reboot sandbox when required
Another chapter in the never-ending AppDomain security saga.
If a computer goes to sleep while SourceGen is running with a project
open, life gets confusing when the system wakes up. The keep-alive
timer fires and a ping is sent to the remote AppDomain, successfully.
At the same time, the lease expires on the remote side, and the objects
are discarded (apparently without bothering to query the ILease object).
This failure mode is 100% repeatable.
Since we can't prevent sandbox objects from disappearing, we have to
detect and recover from the problem. Fortunately we don't keep any
necessary state on the plugin side, so we can just tear the whole
thing down and recreate it.
The various methods in ScriptManager now do a "health check" before
making calls into the plugin AppDomain. If the ping attempt fails,
the AppDomain is "rebooted" by destroying it and creating a new one,
reloading all plugins that were in there before. The plugin binaries
*should* still be in the PluginDllCache directory since the ping failure
was due to objects being discarded, not AppDomain shutdown, and Windows
doesn't let you mess with files that hold executable code.
A new "reboot security sandbox" option has been added to the DEBUG
menu to facilitate testing.
The PluginManager's Ping() method gets called more often, but not to
the extent that performance will be affected.
This change also adds a finalizer to DisasmProject, since we're relying
on it to shut down the ScriptManager, and it's relying on callers to
invoke its cleanup function. The finalizer throws an assertion if the
cleanup function doesn't get called.
(Issue #82)
2020-07-19 20:20:18 +00:00
|
|
|
|
CheckHealth();
|
2019-10-14 01:19:28 +00:00
|
|
|
|
return DomainMgr.PluginMgr.IsLabelSignificant(labelBefore, labelAfter);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gathers a list of platform symbols from the project's symbol table.
|
|
|
|
|
/// </summary>
|
2019-10-04 23:53:31 +00:00
|
|
|
|
private List<PlSymbol> GeneratePlSymbolList() {
|
|
|
|
|
List<PlSymbol> plSymbols = new List<PlSymbol>();
|
2019-05-02 22:45:40 +00:00
|
|
|
|
SymbolTable symTab = mProject.SymbolTable;
|
|
|
|
|
|
|
|
|
|
foreach (Symbol sym in symTab) {
|
2019-10-04 23:53:31 +00:00
|
|
|
|
PlSymbol.Source plsSource;
|
|
|
|
|
switch (sym.SymbolSource) {
|
|
|
|
|
case Symbol.Source.Platform:
|
|
|
|
|
plsSource = PlSymbol.Source.Platform;
|
|
|
|
|
break;
|
|
|
|
|
case Symbol.Source.Project:
|
|
|
|
|
plsSource = PlSymbol.Source.Project;
|
|
|
|
|
break;
|
|
|
|
|
case Symbol.Source.User:
|
|
|
|
|
plsSource = PlSymbol.Source.User;
|
|
|
|
|
break;
|
|
|
|
|
case Symbol.Source.Variable:
|
|
|
|
|
case Symbol.Source.Auto:
|
|
|
|
|
// don't forward these to plugins
|
|
|
|
|
continue;
|
|
|
|
|
default:
|
|
|
|
|
Debug.Assert(false);
|
|
|
|
|
continue;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
2019-10-04 23:53:31 +00:00
|
|
|
|
PlSymbol.Type plsType;
|
|
|
|
|
switch (sym.SymbolType) {
|
2019-11-13 01:24:41 +00:00
|
|
|
|
case Symbol.Type.NonUniqueLocalAddr:
|
|
|
|
|
// don't forward these to plugins
|
|
|
|
|
continue;
|
2019-10-04 23:53:31 +00:00
|
|
|
|
case Symbol.Type.LocalOrGlobalAddr:
|
|
|
|
|
case Symbol.Type.GlobalAddr:
|
|
|
|
|
case Symbol.Type.GlobalAddrExport:
|
|
|
|
|
case Symbol.Type.ExternalAddr:
|
|
|
|
|
plsType = PlSymbol.Type.Address;
|
|
|
|
|
break;
|
|
|
|
|
case Symbol.Type.Constant:
|
|
|
|
|
plsType = PlSymbol.Type.Constant;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
Debug.Assert(false);
|
|
|
|
|
continue;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-04 23:53:31 +00:00
|
|
|
|
int width = -1;
|
|
|
|
|
string tag = string.Empty;
|
|
|
|
|
if (sym is DefSymbol) {
|
|
|
|
|
DefSymbol defSym = sym as DefSymbol;
|
|
|
|
|
width = defSym.DataDescriptor.Length;
|
|
|
|
|
tag = defSym.Tag;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
plSymbols.Add(new PlSymbol(sym.Label, sym.Value, width, plsSource, plsType, tag));
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-10-04 23:53:31 +00:00
|
|
|
|
return plSymbols;
|
2019-05-02 22:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-28 22:00:48 +00:00
|
|
|
|
#if false
|
2019-11-25 22:27:38 +00:00
|
|
|
|
public delegate bool CheckMatch(IPlugin plugin);
|
|
|
|
|
public IPlugin GetMatchingScript(CheckMatch check) {
|
2019-12-28 22:00:48 +00:00
|
|
|
|
Dictionary<string, IPlugin> plugins;
|
2019-11-25 22:27:38 +00:00
|
|
|
|
if (DomainMgr == null) {
|
2019-12-28 22:00:48 +00:00
|
|
|
|
plugins = mActivePlugins;
|
2019-11-25 22:27:38 +00:00
|
|
|
|
} else {
|
2019-12-28 22:00:48 +00:00
|
|
|
|
plugins = DomainMgr.PluginMgr.GetActivePlugins();
|
|
|
|
|
}
|
|
|
|
|
foreach (IPlugin plugin in plugins.Values) {
|
|
|
|
|
if (check(plugin)) {
|
|
|
|
|
return plugin;
|
2019-11-25 22:27:38 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2019-12-28 22:00:48 +00:00
|
|
|
|
#endif
|
2019-11-25 22:27:38 +00:00
|
|
|
|
|
2019-11-27 02:54:42 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns a list of loaded plugins. Callers should not retain this list, as the
|
|
|
|
|
/// set can change due to user activity.
|
|
|
|
|
/// </summary>
|
2019-12-28 22:00:48 +00:00
|
|
|
|
public Dictionary<string, IPlugin> GetActivePlugins() {
|
2019-11-27 02:54:42 +00:00
|
|
|
|
if (DomainMgr == null) {
|
2019-12-28 22:00:48 +00:00
|
|
|
|
// copy the contents
|
|
|
|
|
Dictionary<string, IPlugin> pdict = new Dictionary<string, IPlugin>();
|
2019-11-27 02:54:42 +00:00
|
|
|
|
foreach (KeyValuePair<string, IPlugin> kvp in mActivePlugins) {
|
2019-12-28 22:00:48 +00:00
|
|
|
|
pdict.Add(kvp.Key, kvp.Value);
|
2019-11-27 02:54:42 +00:00
|
|
|
|
}
|
2019-12-28 22:00:48 +00:00
|
|
|
|
return pdict;
|
2019-11-27 02:54:42 +00:00
|
|
|
|
} else {
|
Reboot sandbox when required
Another chapter in the never-ending AppDomain security saga.
If a computer goes to sleep while SourceGen is running with a project
open, life gets confusing when the system wakes up. The keep-alive
timer fires and a ping is sent to the remote AppDomain, successfully.
At the same time, the lease expires on the remote side, and the objects
are discarded (apparently without bothering to query the ILease object).
This failure mode is 100% repeatable.
Since we can't prevent sandbox objects from disappearing, we have to
detect and recover from the problem. Fortunately we don't keep any
necessary state on the plugin side, so we can just tear the whole
thing down and recreate it.
The various methods in ScriptManager now do a "health check" before
making calls into the plugin AppDomain. If the ping attempt fails,
the AppDomain is "rebooted" by destroying it and creating a new one,
reloading all plugins that were in there before. The plugin binaries
*should* still be in the PluginDllCache directory since the ping failure
was due to objects being discarded, not AppDomain shutdown, and Windows
doesn't let you mess with files that hold executable code.
A new "reboot security sandbox" option has been added to the DEBUG
menu to facilitate testing.
The PluginManager's Ping() method gets called more often, but not to
the extent that performance will be affected.
This change also adds a finalizer to DisasmProject, since we're relying
on it to shut down the ScriptManager, and it's relying on callers to
invoke its cleanup function. The finalizer throws an assertion if the
cleanup function doesn't get called.
(Issue #82)
2020-07-19 20:20:18 +00:00
|
|
|
|
CheckHealth();
|
2019-11-27 02:54:42 +00:00
|
|
|
|
return DomainMgr.PluginMgr.GetActivePlugins();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-02 22:45:40 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// For debugging purposes, get some information about the currently loaded
|
|
|
|
|
/// extension scripts.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public string DebugGetLoadedScriptInfo() {
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
if (DomainMgr == null) {
|
|
|
|
|
foreach (KeyValuePair<string, IPlugin> kvp in mActivePlugins) {
|
|
|
|
|
string loc = kvp.Value.GetType().Assembly.Location;
|
|
|
|
|
sb.Append("[main] ");
|
|
|
|
|
sb.Append(loc);
|
|
|
|
|
sb.Append("\r\n ");
|
|
|
|
|
DebugGetScriptInfo(kvp.Value, sb);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
Reboot sandbox when required
Another chapter in the never-ending AppDomain security saga.
If a computer goes to sleep while SourceGen is running with a project
open, life gets confusing when the system wakes up. The keep-alive
timer fires and a ping is sent to the remote AppDomain, successfully.
At the same time, the lease expires on the remote side, and the objects
are discarded (apparently without bothering to query the ILease object).
This failure mode is 100% repeatable.
Since we can't prevent sandbox objects from disappearing, we have to
detect and recover from the problem. Fortunately we don't keep any
necessary state on the plugin side, so we can just tear the whole
thing down and recreate it.
The various methods in ScriptManager now do a "health check" before
making calls into the plugin AppDomain. If the ping attempt fails,
the AppDomain is "rebooted" by destroying it and creating a new one,
reloading all plugins that were in there before. The plugin binaries
*should* still be in the PluginDllCache directory since the ping failure
was due to objects being discarded, not AppDomain shutdown, and Windows
doesn't let you mess with files that hold executable code.
A new "reboot security sandbox" option has been added to the DEBUG
menu to facilitate testing.
The PluginManager's Ping() method gets called more often, but not to
the extent that performance will be affected.
This change also adds a finalizer to DisasmProject, since we're relying
on it to shut down the ScriptManager, and it's relying on callers to
invoke its cleanup function. The finalizer throws an assertion if the
cleanup function doesn't get called.
(Issue #82)
2020-07-19 20:20:18 +00:00
|
|
|
|
CheckHealth();
|
2019-12-28 22:00:48 +00:00
|
|
|
|
Dictionary<string, IPlugin> plugins = DomainMgr.PluginMgr.GetActivePlugins();
|
|
|
|
|
foreach (IPlugin plugin in plugins.Values) {
|
2019-05-02 22:45:40 +00:00
|
|
|
|
string loc = DomainMgr.PluginMgr.GetPluginAssemblyLocation(plugin);
|
|
|
|
|
sb.AppendFormat("[sub {0}] ", DomainMgr.Id);
|
|
|
|
|
sb.Append(loc);
|
|
|
|
|
sb.Append("\r\n ");
|
|
|
|
|
DebugGetScriptInfo(plugin, sb);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
private void DebugGetScriptInfo(IPlugin plugin, StringBuilder sb) {
|
|
|
|
|
sb.Append(plugin.Identifier);
|
2019-08-11 00:13:25 +00:00
|
|
|
|
sb.Append(":");
|
|
|
|
|
|
|
|
|
|
// The plugin is actually a MarshalByRefObject, so we can't use reflection
|
|
|
|
|
// to gather the list of interfaces.
|
2019-11-25 22:27:38 +00:00
|
|
|
|
// TODO(maybe): add a call that does a reflection query on the remote side
|
2019-10-14 01:19:28 +00:00
|
|
|
|
if (plugin is PluginCommon.IPlugin_SymbolList) {
|
|
|
|
|
sb.Append(" SymbolList");
|
|
|
|
|
}
|
2019-08-11 00:13:25 +00:00
|
|
|
|
if (plugin is PluginCommon.IPlugin_InlineJsr) {
|
|
|
|
|
sb.Append(" InlineJsr");
|
|
|
|
|
}
|
|
|
|
|
if (plugin is PluginCommon.IPlugin_InlineJsl) {
|
|
|
|
|
sb.Append(" InlineJsl");
|
|
|
|
|
}
|
|
|
|
|
if (plugin is PluginCommon.IPlugin_InlineBrk) {
|
|
|
|
|
sb.Append(" InlineBrk");
|
|
|
|
|
}
|
Reboot sandbox when required
Another chapter in the never-ending AppDomain security saga.
If a computer goes to sleep while SourceGen is running with a project
open, life gets confusing when the system wakes up. The keep-alive
timer fires and a ping is sent to the remote AppDomain, successfully.
At the same time, the lease expires on the remote side, and the objects
are discarded (apparently without bothering to query the ILease object).
This failure mode is 100% repeatable.
Since we can't prevent sandbox objects from disappearing, we have to
detect and recover from the problem. Fortunately we don't keep any
necessary state on the plugin side, so we can just tear the whole
thing down and recreate it.
The various methods in ScriptManager now do a "health check" before
making calls into the plugin AppDomain. If the ping attempt fails,
the AppDomain is "rebooted" by destroying it and creating a new one,
reloading all plugins that were in there before. The plugin binaries
*should* still be in the PluginDllCache directory since the ping failure
was due to objects being discarded, not AppDomain shutdown, and Windows
doesn't let you mess with files that hold executable code.
A new "reboot security sandbox" option has been added to the DEBUG
menu to facilitate testing.
The PluginManager's Ping() method gets called more often, but not to
the extent that performance will be affected.
This change also adds a finalizer to DisasmProject, since we're relying
on it to shut down the ScriptManager, and it's relying on callers to
invoke its cleanup function. The finalizer throws an assertion if the
cleanup function doesn't get called.
(Issue #82)
2020-07-19 20:20:18 +00:00
|
|
|
|
if (plugin is PluginCommon.IPlugin_Visualizer_v2) {
|
|
|
|
|
sb.Append(" Visualizer2");
|
|
|
|
|
} else if (plugin is PluginCommon.IPlugin_Visualizer) {
|
2019-11-27 02:54:42 +00:00
|
|
|
|
sb.Append(" Visualizer");
|
2019-11-25 22:27:38 +00:00
|
|
|
|
}
|
2019-05-02 22:45:40 +00:00
|
|
|
|
sb.Append("\r\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|