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

[.NET] TypeDefinition.Fields (and possibly other member getters) isn't thread-safe #299

Closed
SamboyCoding opened this issue Apr 22, 2022 · 3 comments
Labels
bug dotnet Issues related to AsmResolver.DotNet
Milestone

Comments

@SamboyCoding
Copy link
Sponsor Contributor

Describe the bug

As discussed in discord, calling

moduleDef.GetAllTypes().AsParallel().SelectMany(t => t.Fields).Count();

Throws the following exception

System.BadImageFormatException: Field 0400016E is not in the range of a declaring type.
   at AsmResolver.ThrowErrorListener.RegisterException(Exception exception)
   at AsmResolver.DotNet.Serialized.SerializedFieldDefinition.GetDeclaringType()
   at AsmResolver.LazyVariable`1.InitializeValue()
   at AsmResolver.DotNet.FieldDefinition.AsmResolver.Collections.IOwnedCollectionElement<AsmResolver.DotNet.TypeDefinition>.get_Owner()
   at AsmResolver.Collections.OwnedCollection`2.AssertNotNullAndHasNoOwner(TItem item)
   at AsmResolver.Collections.OwnedCollection`2.OnInsertItem(Int32 index, TItem item)
   at AsmResolver.DotNet.Serialized.SerializedTypeDefinition.CreateMemberCollection[TMember](MetadataRange range)
   at AsmResolver.DotNet.Serialized.SerializedTypeDefinition.GetFields()
   at AsmResolver.DotNet.TypeDefinition.get_Fields()
   at System.Linq.Parallel.SelectManyQueryOperator`3.SelectManyQueryOperatorEnumerator`1.MoveNext(TOutput& currentElement, Pair`2& currentKey)

While calling the same thing without the AsParallel() works as expected.

To Reproduce

Load a module, and call moduleDef.GetAllTypes().AsParallel().SelectMany(t => t.Fields).Count(). As this is a thread-safety issue, it's possible you may have to call it multiple times to reproduce the issue.

Expected behavior

The total number of field definitions in the module is returned, but (hopefully) faster than doing so single-threaded.

Screenshots

N/A

Platform

  • OS: Windows 11
  • AsmResolver Version: 4.9.0

Additional context
N/A

@SamboyCoding SamboyCoding changed the title [.NET] TypeDefinition.Fields isn't thread-safe [.NET] TypeDefinition.Fields (and possibly other member getters) isn't thread-safe Apr 22, 2022
@Washi1337
Copy link
Owner

Potential fix has been pushed in #300. Could you verify this indeed solves the issue?

@Washi1337 Washi1337 added the dotnet Issues related to AsmResolver.DotNet label Apr 23, 2022
@Washi1337 Washi1337 added this to the 4.11.0 milestone Apr 23, 2022
@SamboyCoding
Copy link
Sponsor Contributor Author

Yep, that seems to fix the issue.

@Washi1337
Copy link
Owner

Perfect. Thanks for spotting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug dotnet Issues related to AsmResolver.DotNet
Projects
None yet
Development

No branches or pull requests

2 participants