-
Notifications
You must be signed in to change notification settings - Fork 32
[feat] New assembly resolving pipeline + some nuget resolving heuristics #169
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
Open
mxprshn
wants to merge
1
commit into
VSharp-team:master
Choose a base branch
from
mxprshn:assembly-resolving-fixes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -92,7 +92,7 @@ private static Statistics StartExploration(List<MethodBase> methods, string resu | |
|
||
void HandleInternalFail(MethodBase method, Exception exception) | ||
{ | ||
Console.WriteLine($"EXCEPTION | {method.DeclaringType}.{method.Name} | {exception.GetType().Name} {exception.Message}"); | ||
Logger.printLogString(Logger.Error, $"Internal exception | {method.DeclaringType}.{method.Name} | {exception.GetType().Name} {exception.Message}"); | ||
} | ||
|
||
foreach (var method in methods) | ||
|
@@ -172,18 +172,52 @@ public static Statistics Cover(Type type, int timeout = -1, string outputDirecto | |
public static Statistics Cover(Assembly assembly, int timeout = -1, string outputDirectory = "") | ||
{ | ||
AssemblyManager.Load(assembly); | ||
List<MethodBase> methods; | ||
BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | | ||
BindingFlags.DeclaredOnly; | ||
methods = new List<MethodBase>(); | ||
|
||
foreach (var t in assembly.GetTypes()) | ||
var methods = new List<MethodBase>(); | ||
var types = new List<Type>(); | ||
|
||
try | ||
{ | ||
types.AddRange(assembly.GetTypes()); | ||
} | ||
catch (ReflectionTypeLoadException e) | ||
{ | ||
foreach (var loaderException in e.LoaderExceptions) | ||
{ | ||
Logger.printLogString(Logger.Error, $"Cannot load type: {loaderException?.Message}"); | ||
} | ||
|
||
foreach (var type in e.Types) | ||
{ | ||
if (type is not null) | ||
{ | ||
types.Add(type); | ||
} | ||
} | ||
} | ||
|
||
foreach (var t in types) | ||
{ | ||
if (t.IsPublic) | ||
if (t.IsPublic || t.IsNestedPublic) | ||
{ | ||
foreach (var m in t.GetMethods(bindingFlags)) | ||
{ | ||
if (m.GetMethodBody() != null) | ||
global::System.Reflection.MethodBody methodBody = null; | ||
var hasByRefLikes = true; | ||
|
||
try | ||
{ | ||
methodBody = m.GetMethodBody(); | ||
hasByRefLikes = Reflection.hasByRefLikes(m); | ||
Comment on lines
+212
to
+213
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Насколько я понял, эксепшен может произойти во время "m.GetMethodBody()", тогда логичнее вначале вычислить "Reflection.hasByRefLikes(m)", если это необходимо |
||
} | ||
catch (Exception e) | ||
{ | ||
Logger.printLogString(Logger.Error, $"Cannot get method info: {e.Message}"); | ||
} | ||
|
||
if (methodBody is not null && !hasByRefLikes) | ||
methods.Add(m); | ||
} | ||
} | ||
|
72 changes: 72 additions & 0 deletions
72
VSharp.CSharpUtils/AssemblyResolving/AssemblyResolverUtils.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
|
||
namespace VSharp.CSharpUtils.AssemblyResolving | ||
{ | ||
public static class AssemblyResolverUtils | ||
{ | ||
public static string FindAssemblyWithName(DirectoryInfo directory, AssemblyName targetName) | ||
{ | ||
bool IsTargetAssembly(FileInfo assemblyFile) | ||
{ | ||
try | ||
{ | ||
var foundName = AssemblyName.GetAssemblyName(assemblyFile.FullName); | ||
return foundName.Name == targetName.Name && | ||
foundName.Version == targetName.Version && | ||
foundName.ContentType == targetName.ContentType; | ||
} | ||
catch (Exception) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
var found = directory.EnumerateFiles("*.dll").FirstOrDefault(IsTargetAssembly); | ||
|
||
if (found is not null) | ||
{ | ||
return found.FullName; | ||
} | ||
|
||
foreach (var subDir in directory.EnumerateDirectories()) | ||
{ | ||
var foundInSubDir = FindAssemblyWithName(subDir, targetName); | ||
if (foundInSubDir is not null) | ||
{ | ||
return foundInSubDir; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static string GetBaseNuGetDirectory() | ||
{ | ||
var baseDirectory = Environment.GetEnvironmentVariable("NUGET_PACKAGES"); | ||
|
||
if (!string.IsNullOrEmpty(baseDirectory)) | ||
{ | ||
return baseDirectory; | ||
} | ||
|
||
if (OperatingSystem.IsWindows()) | ||
{ | ||
baseDirectory = Environment.GetEnvironmentVariable("USERPROFILE"); | ||
} | ||
else | ||
{ | ||
baseDirectory = Environment.GetEnvironmentVariable("HOME"); | ||
} | ||
|
||
if (string.IsNullOrEmpty(baseDirectory)) | ||
{ | ||
return null; | ||
} | ||
|
||
return Path.Combine(baseDirectory, ".nuget", "packages"); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
VSharp.CSharpUtils/AssemblyResolving/CompositeAssemblyResolver.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
|
||
namespace VSharp.CSharpUtils.AssemblyResolving | ||
{ | ||
public class CompositeAssemblyResolver : IAssemblyResolver | ||
{ | ||
private readonly List<IAssemblyResolver> _resolvers = new(); | ||
|
||
public CompositeAssemblyResolver(params IAssemblyResolver[] resolvers) | ||
{ | ||
_resolvers.AddRange(resolvers); | ||
} | ||
|
||
public string Resolve(AssemblyName assemblyName) | ||
{ | ||
foreach (var resolver in _resolvers) | ||
{ | ||
var resolved = resolver.Resolve(assemblyName); | ||
if (resolved is not null) | ||
{ | ||
return resolved; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
VSharp.CSharpUtils/AssemblyResolving/CurrentDirectoryAssemblyResolver.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System.IO; | ||
using System.Reflection; | ||
|
||
namespace VSharp.CSharpUtils.AssemblyResolving | ||
{ | ||
public class CurrentDirectoryAssemblyResolver : IAssemblyResolver | ||
{ | ||
private readonly string _directoryPath; | ||
|
||
public CurrentDirectoryAssemblyResolver(string directoryPath) | ||
{ | ||
_directoryPath = directoryPath; | ||
} | ||
|
||
public string Resolve(AssemblyName assemblyName) | ||
{ | ||
var dllPath = Path.Combine(_directoryPath, $"{assemblyName.Name}.dll"); | ||
|
||
if (File.Exists(dllPath)) | ||
{ | ||
return dllPath; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using System.Reflection; | ||
|
||
namespace VSharp.CSharpUtils.AssemblyResolving | ||
{ | ||
public interface IAssemblyResolver | ||
{ | ||
string Resolve(AssemblyName assemblyName); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
VSharp.CSharpUtils/AssemblyResolving/MicrosoftDependencyModelAssemblyResolver.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Microsoft.Extensions.DependencyModel; | ||
using Microsoft.Extensions.DependencyModel.Resolution; | ||
|
||
namespace VSharp.CSharpUtils.AssemblyResolving | ||
{ | ||
public class MicrosoftDependencyModelAssemblyResolver : IAssemblyResolver | ||
{ | ||
private readonly List<ICompilationAssemblyResolver> _resolvers = new(); | ||
private readonly DependencyContext _context; | ||
|
||
public MicrosoftDependencyModelAssemblyResolver(DependencyContext context, params ICompilationAssemblyResolver[] resolvers) | ||
{ | ||
_resolvers.AddRange(resolvers); | ||
_context = context; | ||
} | ||
|
||
public string Resolve(AssemblyName assemblyName) | ||
{ | ||
if (_context is null) | ||
{ | ||
return null; | ||
} | ||
|
||
var compilationLibrary = _context.CompileLibraries.FirstOrDefault(l => | ||
l.Name.Equals(assemblyName.Name, StringComparison.OrdinalIgnoreCase)); | ||
|
||
if (compilationLibrary is null) | ||
{ | ||
var runtimeLibrary = _context.RuntimeLibraries.FirstOrDefault(l => | ||
l.Name.Equals(assemblyName.Name, StringComparison.OrdinalIgnoreCase)); | ||
|
||
if (runtimeLibrary is not null) | ||
{ | ||
compilationLibrary = new CompilationLibrary( | ||
runtimeLibrary.Type, | ||
runtimeLibrary.Name, | ||
runtimeLibrary.Version, | ||
runtimeLibrary.Hash, | ||
runtimeLibrary.RuntimeAssemblyGroups.SelectMany(g => g.AssetPaths), | ||
runtimeLibrary.Dependencies, | ||
runtimeLibrary.Serviceable | ||
); | ||
} | ||
} | ||
|
||
if (compilationLibrary is null) | ||
{ | ||
return null; | ||
} | ||
|
||
var assemblies = new List<string>(); | ||
|
||
foreach (ICompilationAssemblyResolver resolver in _resolvers) | ||
{ | ||
try | ||
{ | ||
if (resolver.TryResolveAssemblyPaths(compilationLibrary, assemblies)) | ||
{ | ||
return assemblies[0]; | ||
} | ||
} | ||
catch { } | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Кажется, что по умолчанию лучше это выставить в false, и назвать "hasRefLikes", если имеются ввиду ref-like структуры
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
В .NET оно называется именно "ByRefLike", поэтому название можно оставить