Skip to content

Commit

Permalink
Switched to DefaultAssemblyResolver. Fixed the issue ".NET mscorlib i…
Browse files Browse the repository at this point in the history
…s injected to obfuscated assemblies".
  • Loading branch information
lextm committed Feb 5, 2014
1 parent 1fda8fa commit 2537571
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 32 deletions.
11 changes: 8 additions & 3 deletions Obfuscar/AssemblyCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@

namespace Obfuscar
{
class AssemblyCache : BaseAssemblyResolver
class AssemblyCache
{
readonly Project project;
readonly Dictionary<string, AssemblyDefinition> cache =
new Dictionary<string, AssemblyDefinition> ();
public readonly BaseAssemblyResolver Resolver = new DefaultAssemblyResolver ();

public AssemblyCache (Project project)
{
Expand All @@ -54,15 +55,19 @@ public TypeDefinition GetTypeDefinition (TypeReference type)
return gi == null ? null : GetTypeDefinition (gi.ElementType);
}

// try to self resolve, fall back to default resolver
AssemblyDefinition assmDef;
if (cache.ContainsKey (name.FullName)) {
assmDef = cache [name.FullName];
} else {
// try to self resolve, fall back to default resolver
try {
Console.WriteLine ("Trying to resolve dependency: " + name);
assmDef = Resolve (name);
assmDef = Resolver.Resolve (name);
cache [name.FullName] = assmDef;
} catch (FileNotFoundException) {
throw new ObfuscarException ("Unable to resolve dependency: " + name.Name);
}
}

string fullName = null;
while (type.IsNested) {
Expand Down
4 changes: 2 additions & 2 deletions Obfuscar/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -518,15 +518,15 @@ private void LoadAssembly (string filename)
definition = AssemblyDefinition.ReadAssembly (filename, new ReaderParameters {
ReadingMode = Mono.Cecil.ReadingMode.Immediate,
ReadSymbols = readSymbols,
AssemblyResolver = project.Cache
AssemblyResolver = project.Cache.Resolver
});
} catch { // If there's a non-matching pdb next to it, this fails, else just try again
if (!readSymbols)
throw;
definition = AssemblyDefinition.ReadAssembly (filename, new ReaderParameters {
ReadingMode = Mono.Cecil.ReadingMode.Immediate,
ReadSymbols = false,
AssemblyResolver = project.Cache
AssemblyResolver = project.Cache.Resolver
});
}

Expand Down
4 changes: 2 additions & 2 deletions Obfuscar/InheritMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ public InheritMap (Project project)
AssemblyCache cache = new AssemblyCache (project);

foreach (var path in project.ExtraPaths)
cache.AddSearchDirectory (path);
cache.Resolver.AddSearchDirectory (path);

foreach (AssemblyInfo info in project) {
cache.AddSearchDirectory (System.IO.Path.GetDirectoryName (info.Filename));
cache.Resolver.AddSearchDirectory (System.IO.Path.GetDirectoryName (info.Filename));
foreach (TypeDefinition type in info.GetAllTypeDefinitions()) {
if (type.FullName == "<Module>")
continue;
Expand Down
42 changes: 19 additions & 23 deletions Obfuscar/Obfuscator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,13 @@ public void SaveAssemblies ()
else
info.Definition.Write (outName);

if (info.Definition.Name.HasPublicKey) {
if (project.KeyContainerName != null) {
MsNetSigner.SignAssemblyFromKeyContainer (outName, project.KeyContainerName);
} else {
var sn = new StrongName (project.KeyValue);
sn.Sign (outName);
}
if (info.Definition.Name.HasPublicKey)
if (project.KeyContainerName != null) {
MsNetSigner.SignAssemblyFromKeyContainer (outName, project.KeyContainerName);
} else {
var sn = new StrongName (project.KeyValue);
sn.Sign (outName);
}

// TODO: review whether this is still needed
// SignAssembly (info, outName);
}

TypeNameCache.nameCache.Clear ();
Expand All @@ -236,7 +232,7 @@ private void SignAssembly (AssemblyInfo info, string outName)
keyfilepath = null;
foreach (CustomAttribute ca in info.Definition.CustomAttributes) {
if (ca.Constructor.DeclaringType.FullName ==
typeof(System.Reflection.AssemblyKeyFileAttribute).FullName) {
typeof(System.Reflection.AssemblyKeyFileAttribute).FullName) {
keyfilepath = (string)ca.ConstructorArguments [0].Value;
}
}
Expand Down Expand Up @@ -547,7 +543,7 @@ public void RenameTypes ()
for (int j = 0; j < method.Body.Instructions.Count; j++) {
Instruction instruction = method.Body.Instructions [j];
if (instruction.OpCode == OpCodes.Ldstr &&
(string)instruction.Operand == fullName)
(string)instruction.Operand == fullName)
instruction.Operand = newTypeKey.Fullname;
}
}
Expand Down Expand Up @@ -617,7 +613,7 @@ private List<XDocument> GetXamlDocuments (AssemblyDefinition library)
continue;

try {
using (var bamlReader = new XmlBamlReader (stream, new CecilTypeResolver (project.InheritMap.Cache, library)))
using (var bamlReader = new XmlBamlReader (stream, new CecilTypeResolver (project.InheritMap.Cache.Resolver, library)))
result.Add (XDocument.Load (bamlReader));
} catch (ArgumentException) {
} catch (FileNotFoundException) {
Expand Down Expand Up @@ -886,7 +882,7 @@ public void RenameMethods ()

// if we need to skip the method or we don't yet have a name planned for a method, rename it
if ((skiprename != null && m.Status != ObfuscationStatus.Skipped) ||
m.Status == ObfuscationStatus.Unknown) {
m.Status == ObfuscationStatus.Unknown) {
RenameVirtualMethod (info, baseSigNames, sigNames, methodKey, method, skiprename);
}
}
Expand Down Expand Up @@ -947,7 +943,7 @@ public void RenameMethods ()
}

private void RenameVirtualMethod (AssemblyInfo info, Dictionary<TypeKey, Dictionary<ParamSig, NameGroup>> baseSigNames,
Dictionary<ParamSig, NameGroup> sigNames, MethodKey methodKey, MethodDefinition method, string skipRename)
Dictionary<ParamSig, NameGroup> sigNames, MethodKey methodKey, MethodDefinition method, string skipRename)
{
// if method is in a group, look for group key
MethodGroup group = project.InheritMap.GetMethodGroup (methodKey);
Expand Down Expand Up @@ -1018,7 +1014,7 @@ private void RenameVirtualMethod (AssemblyInfo info, Dictionary<TypeKey, Diction
}

NameGroup[] GetNameGroups (Dictionary<TypeKey, Dictionary<ParamSig, NameGroup>> baseSigNames,
IEnumerable<MethodKey> methodKeys, ParamSig sig)
IEnumerable<MethodKey> methodKeys, ParamSig sig)
{
// build unique set of classes in group
HashSet<TypeKey> typeKeys = new HashSet<TypeKey> ();
Expand All @@ -1044,7 +1040,7 @@ string GetNewMethodName (Dictionary<ParamSig, NameGroup> sigNames, MethodKey met

// if it already has a name, return it
if (t.Status == ObfuscationStatus.Renamed ||
t.Status == ObfuscationStatus.WillRename)
t.Status == ObfuscationStatus.WillRename)
return t.StatusText;

// don't mess with methods we decided to skip
Expand Down Expand Up @@ -1125,12 +1121,12 @@ public void HideStrings ()
int nameIndex = 0;

// We get the most used type references
TypeReference systemObjectTypeReference = library.MainModule.Import (typeof(Object));
TypeReference systemVoidTypeReference = library.MainModule.Import (typeof(void));
TypeReference systemStringTypeReference = library.MainModule.Import (typeof(String));
TypeReference systemValueTypeTypeReference = library.MainModule.Import (typeof(ValueType));
TypeReference systemByteTypeReference = library.MainModule.Import (typeof(byte));
TypeReference systemIntTypeReference = library.MainModule.Import (typeof(int));
var systemObjectTypeReference = library.MainModule.TypeSystem.Object;
var systemVoidTypeReference = library.MainModule.TypeSystem.Void;
var systemStringTypeReference = library.MainModule.TypeSystem.String;
var systemValueTypeTypeReference = new TypeReference ("System", "ValueType", library.MainModule, library.MainModule.TypeSystem.Corlib);
var systemByteTypeReference = library.MainModule.TypeSystem.Byte;
var systemIntTypeReference = library.MainModule.TypeSystem.Int32;

// New static class with a method for each unique string we substitute.
TypeDefinition newtype = new TypeDefinition ("<PrivateImplementationDetails>{" + Guid.NewGuid ().ToString ().ToUpper () + "}", null, TypeAttributes.BeforeFieldInit | TypeAttributes.AutoClass | TypeAttributes.AnsiClass | TypeAttributes.BeforeFieldInit, systemObjectTypeReference);
Expand Down
4 changes: 2 additions & 2 deletions ThirdParty/SharedAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
// You can specify all the values or you can use the default the Revision and
// Build Numbers by using the '*' as shown below:

[assembly: AssemblyVersion("2.0.010204.39")]
[assembly: AssemblyVersion("2.0.010205.00")]
[assembly: AssemblyProduct("Obfuscar 2.0 RC")]
#if (!CF)
[assembly: AssemblyFileVersion("2.0.010204.39")]
[assembly: AssemblyFileVersion("2.0.010205.00")]
#endif

0 comments on commit 2537571

Please sign in to comment.