Skip to content
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

Protect against circular type forwarders #806

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
14 changes: 13 additions & 1 deletion Mono.Cecil/ExportedType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,21 @@ public override string ToString ()
return FullName;
}

private bool reentrancyGuard = false;

public TypeDefinition Resolve ()
{
return module.Resolve (CreateReference ());
if (reentrancyGuard) {
throw new InvalidOperationException ($"Circularity when resolving exported type: '{this}'");
}

reentrancyGuard = true;
try {
return module.Resolve (CreateReference ());
}
finally {
reentrancyGuard = false;
}
}

internal TypeReference CreateReference ()
Expand Down
17 changes: 17 additions & 0 deletions Test/Mono.Cecil.Tests/ResolveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,23 @@ public void TypeForwarder ()
Assert.AreEqual (Platform.OnCoreClr ? "System.Private.CoreLib" : "mscorlib", definition.Module.Assembly.Name.Name);
}

[Test]
public void TypeForwarderCircularity ()
{
var resolver = new CustomResolver();
var parameters = new ReaderParameters { AssemblyResolver = resolver };

var module1 = GetResourceModule("TypeForwarderLoop1.dll", parameters);
var module2 = GetResourceModule("TypeForwarderLoop2.dll", parameters);

resolver.Register(module1.Assembly);
resolver.Register(module2.Assembly);

var reference = new TypeReference("MyNamespace", "MyType", module1, AssemblyNameReference.Parse(module1.Assembly.FullName), false);

Assert.Throws<InvalidOperationException>(() => { _ = reference.Resolve(); });
}

[Test]
public void NestedTypeForwarder ()
{
Expand Down
Binary file not shown.
Binary file not shown.