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

[TypeName] Nested types should respect MaxNode count #106334

Merged
merged 11 commits into from
Sep 3, 2024

Conversation

adamsitnik
Copy link
Member

@adamsitnik adamsitnik commented Aug 13, 2024

Fixes a bug discovered by @jkotas in #103713 (comment)

fixes #106991


foreach (TypeName genericArgument in GetGenericArguments())
{
result += genericArgument.GetNodeCount();
Copy link
Member

Choose a reason for hiding this comment

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

Should this use checked arithmetic to avoid silent overflows?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, because we can not parse more than Int32.MaxValue nodes because TypeNameParseOptions.MaxNodes is also an Int32.

Copy link
Member

Choose a reason for hiding this comment

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

TypeName.Parse is not the only way to create new type names after the last PR. Try something like this:

using System.Reflection.Metadata;
using System.Collections.Immutable;

var simpleTypeName = TypeName.Parse("A");
var tn = simpleTypeName;

for (int i = 0; i < 20; i++)
{
    tn = simpleTypeName.MakeGenericTypeName(ImmutableArray.Create(tn, tn, tn));
    Console.WriteLine(tn.GetNodeCount());
}

It is going to hit the overflow in this code.

- revert the change that seemed like optimization to make the code more readable
- add a comment explaining why we don't need to check of integer overflows
- add a comment explaining the details of generic nested types node count
…ount

# Conflicts:
#	src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/TypeNameParserHelpers.cs
return null;
}
// If that generic type is a nested type, this needs to be taken into account. Example:
// "Namespace.Declaring+NestedGeneric`1[GenericArg]" requires 5 TypeName instances:
Copy link
Member

@jkotas jkotas Aug 15, 2024

Choose a reason for hiding this comment

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

Namespace.Declaring+NestedGeneric[GenericArg] should only require 4 TypeName instances.

I think this comment is not correct. Also, the implementation of GetNodeCount seems to have a bug - it is double-counting nested TypeName instances.

Copy link
Member Author

Choose a reason for hiding this comment

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

Namespace.Declaring+NestedGeneric[GenericArg] should only require 4 TypeName instances.

Which node from the list below should not be included in your opinion?

- constructed generic type: Namespace.Declaring+NestedGeneric`2[GenericArg] (+1, handled by ParseNextTypeName)
  - declaring type: Namespace.Declaring (+1, handled by TryGetTypeNameInfo)
  - generic type definition: Namespace.Declaring+NestedGeneric`1 (+1, handled by the TryDive above)
     - declaring type: Namespace.Declaring (+1, handled by the if below)
  - generic arguments:
    - simple: GenericArg (+1, handled by ParseNextTypeName)

We define it as the total number of TypeName instances required to represent it:

/// Represents the total number of <see cref="TypeName"/> instances that are used to describe
/// this instance, including any generic arguments or underlying types.

And we have 5 instances.

image

The declaring type of the generic type definition and of the constructed generic type is the same object:

image

Do you suggest that because it's the same object, it should not be included?

Also, the implementation of GetNodeCount seems to have a bug - it is double-counting nested TypeName instances.

It does that on purpose: to include the nodes from generic type definition.

Copy link
Member

@jkotas jkotas Aug 29, 2024

Choose a reason for hiding this comment

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

Do you suggest that because it's the same object, it should not be included?

Yes. If it is the same object, it is the same instance and it should be only counted once.

We have multiple ways to get to that instance through the object model. I do not think that it should be a reason to count it multiple times.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. If it is the same object, it is the same instance and it should be only counted once

You are right. I've changed the implementation and the tests, PTAL.

@jkotas jkotas added the binaryformatter-migration Issues related to the removal of BinaryFormatter and migrations away from it label Aug 26, 2024
@jkotas
Copy link
Member

jkotas commented Aug 26, 2024

I have opened #106991 to make sure that this does not get missed for .NET 9.

@terrajobst terrajobst linked an issue Aug 27, 2024 that may be closed by this pull request
@adamsitnik
Copy link
Member Author

I have opened #106991 to make sure that this does not get missed for .NET 9.

Thank you for doing that and apologies for the delay in responding.

- handle overflows
- don't include the declaring type of generic type definition, as it's the same instance uses by the constructed generic type

and add sth extra: assert for ensuring that GetNodeCount returns the same result as calculated by the parser during parsing
@adamsitnik adamsitnik requested a review from jkotas August 30, 2024 10:39
Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

LGTM otherwise

@@ -301,24 +301,29 @@ public string Name
/// </remarks>
public int GetNodeCount()
{
// This method uses checked arithmetic to avoid silent overflows.
Copy link
Member

Choose a reason for hiding this comment

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

Add test to validate at least one of the overflow cases? It can be an outer loop test since it takes a bit of time.

{
result += GetElementType().GetNodeCount();
}
checked { result++; } // the generic type definition
Copy link
Member

@jkotas jkotas Aug 30, 2024

Choose a reason for hiding this comment

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

Suggested change
checked { result++; } // the generic type definition
result = checked(result + GetGenericTypeDefinition().GetNodeCount());

I think the logic would be easier to understand if it is written like this, and the if (IsNested) check is moved to else if after the IsConstructedGenericType check.

TypeName's form a tree structure and GetNodeCount should be a simple walk over this tree structure. The code as written makes it hard to see.

- reorder the conditions
- add test for int32 overflow
@adamsitnik
Copy link
Member Author

/azp list

Copy link

CI/CD Pipelines for this repository:

@adamsitnik
Copy link
Member Author

/azp run runtime-libraries-coreclr outerloop-linux

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

TypeName genericType = TypeName.Parse("Generic".AsSpan());
TypeName genericArg = TypeName.Parse("Arg".AsSpan());
// Don't allocate Array.MaxLength array as it may make this test flaky (frequent OOMs).
ImmutableArray<TypeName>.Builder genericArgs = ImmutableArray.CreateBuilder<TypeName>(initialCapacity: int.MaxValue / 64);
Copy link
Member

Choose a reason for hiding this comment

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

This does not need to allocate very large array. Instead, it can repeat MakeGenericTypeName multiple times. It will make the test a lot less resource intensive, faster and compatible with 32-bit platforms.

Copy link
Member Author

@adamsitnik adamsitnik Sep 2, 2024

Choose a reason for hiding this comment

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

This does not need to allocate very large array. Instead, it can repeat MakeGenericTypeName multiple times.

I know and in my opinion this is exactly what it's doing right now. But I assume that int.MaxValue / 64 is still very large array for you. What would not be? int.MaxValue / 1024? Or Math.Sqrt(int.MaxValue)?

Copy link
Member

Choose a reason for hiding this comment

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

It can be very small, like 10.

Copy link
Member Author

Choose a reason for hiding this comment

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

Math.Sqrt(int.MaxValue) does what we need, I've pushed the change

@@ -493,6 +581,26 @@ public void GetNodeCountReturnsExpectedValue(Type type, int expected)
}
}

[OuterLoop("It takes a lot of time")]
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.Is64BitProcess))] // 32 is likely to OOM
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.Is64BitProcess))] // 32 is likely to OOM

@adamsitnik
Copy link
Member Author

@MihuBot fuzz TypeName

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

LGTM. Thank you!

@adamsitnik
Copy link
Member Author

/ba-g unrelated

@adamsitnik adamsitnik merged commit 16fe4d4 into dotnet:main Sep 3, 2024
142 of 148 checks passed
@jkotas
Copy link
Member

jkotas commented Sep 3, 2024

@adamsitnik Are you going to port this to release/9.0?

@adamsitnik
Copy link
Member Author

@adamsitnik Are you going to port this to release/9.0?

Yes, I am going to send multiple backport PRs (with the fuzzers, the bug fixes they discovered and this PR)

@adamsitnik
Copy link
Member Author

@adamsitnik Are you going to port this to release/9.0?

Follow up: #107533

carlossanlop pushed a commit that referenced this pull request Sep 12, 2024
* AssemblyNameInfo fuzzer (#107195)

* add initial AssemblyNameInfo Fuzzer

* fix the first bug that it has discovered

* Fix sbyte overflow in TypeName parsing (#107261)

* Add TypeNameFuzzer (#107206)

Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>
Co-authored-by: Adam Sitnik <adam.sitnik@gmail.com>

* [TypeName] Nested types should respect MaxNode count (#106334)

* Improve AssemblyNameInfo Fuzzer (#107257)

---------

Co-authored-by: Buyaa Namnan <bunamnan@microsoft.com>
Co-authored-by: Miha Zupan <mihazupan.zupan1@gmail.com>
jtschuster pushed a commit to jtschuster/runtime that referenced this pull request Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Reflection.Metadata binaryformatter-migration Issues related to the removal of BinaryFormatter and migrations away from it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Nested types do not respect TypeNameParseOptions.MaxNodes
2 participants