Skip to content
Merged
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
49 changes: 34 additions & 15 deletions Confuser.Core/ConfuserAssemblyResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,40 @@ public AssemblyDef Resolve(IAssembly assembly, ModuleDef sourceModule) {
}

if (resolvedAssemblyDef?.Name == "netstandard" && 0 < resolvedAssemblyDef.ManifestModule.ExportedTypes.Count) {
// Move types from AssemblyRef to here
// Classic netstandard facades forward to mscorlib, which actually defines the types.
// Moving those types in keeps the confused module referencing netstandard only.
// CoreCLR publish output is different: netstandard.dll only forwards to System.Runtime,
// and that assembly forwards again to System.Private.CoreLib. Clearing the forwards
// there hides System.Object and breaks later analysis.
Comment on lines +48 to +52

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Collapse this block to a one-line reference; the code below is self-describing:

Suggested change
// Classic netstandard facades forward to mscorlib, which actually defines the types.
// Moving those types in keeps the confused module referencing netstandard only.
// CoreCLR publish output is different: netstandard.dll only forwards to System.Runtime,
// and that assembly forwards again to System.Private.CoreLib. Clearing the forwards
// there hides System.Object and breaks later analysis.
// https://github.com/mcpolo99/ConfuserExx/pull/102

var module = resolvedAssemblyDef.ManifestModule;
var newTypes = new List<TypeDef>();
var allAssemblyRefs = new List<AssemblyDef>();

module.ExportedTypes.Clear();

var referenced = new List<AssemblyDef>();
foreach (var assemblyRef in module.GetAssemblyRefs()) {
var subAss =
InternalExactResolver.Resolve(assemblyRef, module) ??
InternalFuzzyResolver.Resolve(assemblyRef, module);
allAssemblyRefs.Add(subAss);
foreach (var subModule in subAss?.Modules) {
foreach (var defType in subModule.Types) {
if (subAss != null)
referenced.Add(subAss);
}

// Only rewrite when a referenced assembly actually defines System.Object
// (the mscorlib case). CoreCLR facades only forward it onward.
Comment on lines +63 to +64

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DefinesSystemObject + the early return already say this; drop the comment:

Suggested change
// Only rewrite when a referenced assembly actually defines System.Object
// (the mscorlib case). CoreCLR facades only forward it onward.

if (!referenced.Any(DefinesSystemObject))
return resolvedAssemblyDef;

var newTypes = new List<TypeDef>();
module.ExportedTypes.Clear();

foreach (var subAss in referenced) {
foreach (var subModule in subAss.Modules) {
foreach (var defType in subModule.Types)
newTypes.Add(defType);
}
subModule.Types.Clear();
foreach (var defType in newTypes) {
foreach (var defType in newTypes)
module.Types.Add(defType);
}
newTypes.Clear();
}
}

// Remove them because their types has been removed.
foreach (var subAss in allAssemblyRefs) {
// Their types now live on netstandard, so they must not stay cached.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop; Remove(...) is clear enough:

Suggested change
// Their types now live on netstandard, so they must not stay cached.

InternalExactResolver.Remove(subAss);
InternalFuzzyResolver.Remove(subAss);
}
Expand All @@ -79,6 +87,17 @@ public AssemblyDef Resolve(IAssembly assembly, ModuleDef sourceModule) {
return resolvedAssemblyDef;
}

static bool DefinesSystemObject(AssemblyDef assembly) {
foreach (var module in assembly.Modules) {
foreach (var type in module.Types) {
if (type.Namespace == "System" && type.Name == "Object")
return true;
}
}

return false;
}

public void Clear() {
InternalExactResolver.Clear();
InternalFuzzyResolver.Clear();
Expand Down