Browse Source

Fix thread-safety issue in UniversalAssemblyResolver

pull/2350/head
Siegfried Pammer 4 years ago
parent
commit
b6da9b23b1
  1. 43
      ICSharpCode.Decompiler/Metadata/UniversalAssemblyResolver.cs

43
ICSharpCode.Decompiler/Metadata/UniversalAssemblyResolver.cs

@ -60,7 +60,7 @@ namespace ICSharpCode.Decompiler.Metadata
decompilerRuntime = DecompilerRuntime.Mono; decompilerRuntime = DecompilerRuntime.Mono;
} }
DotNetCorePathFinder dotNetCorePathFinder; readonly Lazy<DotNetCorePathFinder> dotNetCorePathFinder;
readonly bool throwOnError; readonly bool throwOnError;
readonly PEStreamOptions streamOptions; readonly PEStreamOptions streamOptions;
readonly MetadataReaderOptions metadataOptions; readonly MetadataReaderOptions metadataOptions;
@ -73,13 +73,19 @@ namespace ICSharpCode.Decompiler.Metadata
public void AddSearchDirectory(string directory) public void AddSearchDirectory(string directory)
{ {
directories.Add(directory); directories.Add(directory);
dotNetCorePathFinder?.AddSearchDirectory(directory); if (dotNetCorePathFinder.IsValueCreated)
{
dotNetCorePathFinder.Value.AddSearchDirectory(directory);
}
} }
public void RemoveSearchDirectory(string directory) public void RemoveSearchDirectory(string directory)
{ {
directories.Remove(directory); directories.Remove(directory);
dotNetCorePathFinder?.RemoveSearchDirectory(directory); if (dotNetCorePathFinder.IsValueCreated)
{
dotNetCorePathFinder.Value.RemoveSearchDirectory(directory);
}
} }
public string[] GetSearchDirectories() public string[] GetSearchDirectories()
@ -125,7 +131,7 @@ namespace ICSharpCode.Decompiler.Metadata
this.targetFramework = targetFramework ?? string.Empty; this.targetFramework = targetFramework ?? string.Empty;
this.runtimePack = runtimePack ?? "Microsoft.NETCore.App"; this.runtimePack = runtimePack ?? "Microsoft.NETCore.App";
(targetFrameworkIdentifier, targetFrameworkVersion) = ParseTargetFramework(this.targetFramework); (targetFrameworkIdentifier, targetFrameworkVersion) = ParseTargetFramework(this.targetFramework);
this.dotNetCorePathFinder = new Lazy<DotNetCorePathFinder>(InitDotNetCorePathFinder);
if (mainAssemblyFileName != null) if (mainAssemblyFileName != null)
{ {
string baseDirectory = Path.GetDirectoryName(mainAssemblyFileName); string baseDirectory = Path.GetDirectoryName(mainAssemblyFileName);
@ -223,7 +229,7 @@ namespace ICSharpCode.Decompiler.Metadata
public override bool IsSharedAssembly(IAssemblyReference reference, out string runtimePack) public override bool IsSharedAssembly(IAssemblyReference reference, out string runtimePack)
{ {
return dotNetCorePathFinder.TryResolveDotNetCoreShared(reference, out runtimePack) != null; return dotNetCorePathFinder.Value.TryResolveDotNetCoreShared(reference, out runtimePack) != null;
} }
public string FindAssemblyFile(IAssemblyReference name) public string FindAssemblyFile(IAssemblyReference name)
@ -240,18 +246,7 @@ namespace ICSharpCode.Decompiler.Metadata
case TargetFrameworkIdentifier.NETStandard: case TargetFrameworkIdentifier.NETStandard:
if (IsZeroOrAllOnes(targetFrameworkVersion)) if (IsZeroOrAllOnes(targetFrameworkVersion))
goto default; goto default;
if (dotNetCorePathFinder == null) file = dotNetCorePathFinder.Value.TryResolveDotNetCore(name);
{
if (mainAssemblyFileName == null)
dotNetCorePathFinder = new DotNetCorePathFinder(targetFrameworkIdentifier, targetFrameworkVersion, runtimePack);
else
dotNetCorePathFinder = new DotNetCorePathFinder(mainAssemblyFileName, targetFramework, runtimePack, targetFrameworkIdentifier, targetFrameworkVersion);
foreach (var directory in directories)
{
dotNetCorePathFinder.AddSearchDirectory(directory);
}
}
file = dotNetCorePathFinder.TryResolveDotNetCore(name);
if (file != null) if (file != null)
return file; return file;
goto default; goto default;
@ -267,6 +262,20 @@ namespace ICSharpCode.Decompiler.Metadata
} }
} }
DotNetCorePathFinder InitDotNetCorePathFinder()
{
DotNetCorePathFinder dotNetCorePathFinder;
if (mainAssemblyFileName == null)
dotNetCorePathFinder = new DotNetCorePathFinder(targetFrameworkIdentifier, targetFrameworkVersion, runtimePack);
else
dotNetCorePathFinder = new DotNetCorePathFinder(mainAssemblyFileName, targetFramework, runtimePack, targetFrameworkIdentifier, targetFrameworkVersion);
foreach (var directory in directories)
{
dotNetCorePathFinder.AddSearchDirectory(directory);
}
return dotNetCorePathFinder;
}
string FindWindowsMetadataFile(IAssemblyReference name) string FindWindowsMetadataFile(IAssemblyReference name)
{ {
// Finding Windows Metadata (winmd) is currently only supported on Windows. // Finding Windows Metadata (winmd) is currently only supported on Windows.

Loading…
Cancel
Save