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

[mono][interp] Fix type of args when inlining method #102570

Merged
merged 1 commit into from
May 24, 2024

Conversation

BrzVlad
Copy link
Member

@BrzVlad BrzVlad commented May 22, 2024

The vars allocated from pushing values on the execution stack might not reflect exactly the actual type of the var. Consider this pattern:

	condbr	BB0
	newobj	Derived // push var0 of type Derived
	br	BB1
BB0:
	newobj	Base	// push var1 of type Base
	// here we will end up inserting a `mov var1 -> var0`
BB1:
	// top of stack will be seen as being var0
	call

Because we first reach BB1 with the stack contents of var0, BB1 will end up accessing top of the stack as var0. However the type of var0 at this point is not Derived, since it can also be a Base object. We currently don't update the type of var0, but just update the type information of the top of stack entry when entering BB1. When inlining, after this commit, we use the type information from the stack, rather than the type of the var present on the stack.

In the future we might want to consider updating the type of var0 or creating a new var entirely with the correct type for consistency.

Fixes #102046

Copy link
Contributor

Tagging subscribers to this area: @BrzVlad, @kotlarmilos
See info in area-owners.md if you want to be subscribed.

@BrzVlad
Copy link
Member Author

BrzVlad commented May 22, 2024

Simple repro for this issue:

using System;

public class Base {

	public virtual void Method ()
	{
		Console.WriteLine ("Base");
	}
}

public sealed class Derived : Base {

	public override void Method ()
	{
		Console.WriteLine ("Derived");
	}
}

public class Program {
	public static bool true_var = true;

	public static void CallMethod (Base obj)
	{
		obj.Method ();
	} 

	public static void Main (string[] args)
	{
		// Bug manifests only in optimized code, so we need to tier up method
		for (int i = 0; i < 1001; i++) {
			// Should call Base, interp incorrectly devirts to Derived
			CallMethod (true_var ? new Base () : new Derived ()); 
		}
	}
}

Copy link
Member

@lewing lewing left a comment

Choose a reason for hiding this comment

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

Should a regression test be added somewhere?

@srxqds
Copy link
Contributor

srxqds commented May 23, 2024

need backport .net8.0?

The vars allocated from pushing values on the execution stack might not reflect exactly the actual type of the var. Consider this pattern:

	condbr	BB0
	newobj	Derived // push var0 of type Derived
	br	BB1
BB0:
	newobj	Base	// push var1 of type Base
	// here we will end up inserting a `mov var1 -> var0`
BB1:
	// top of stack will be seen as being var0
	call

Because we first reach BB1 with the stack contents of var0, BB1 will end up accessing top of the stack as var0. However the type of var0 at this point is not Derived, since it can also be a Base object. We currently don't update the type of var0, but just update the type information of the top of stack entry when entering BB1. When inlining, after this commit, we use the type information from the stack, rather than the type of the var present on the stack.

In the future we might want to consider updating the type of var0 or creating a new var entirely with the correct type for consistency.
@lewing lewing merged commit fc5b6e7 into dotnet:main May 24, 2024
77 of 79 checks passed
@srxqds
Copy link
Contributor

srxqds commented May 27, 2024

need backport .net8.0?

?

Ruihan-Yin pushed a commit to Ruihan-Yin/runtime that referenced this pull request May 30, 2024
The vars allocated from pushing values on the execution stack might not reflect exactly the actual type of the var. Consider this pattern:

	condbr	BB0
	newobj	Derived // push var0 of type Derived
	br	BB1
BB0:
	newobj	Base	// push var1 of type Base
	// here we will end up inserting a `mov var1 -> var0`
BB1:
	// top of stack will be seen as being var0
	call

Because we first reach BB1 with the stack contents of var0, BB1 will end up accessing top of the stack as var0. However the type of var0 at this point is not Derived, since it can also be a Base object. We currently don't update the type of var0, but just update the type information of the top of stack entry when entering BB1. When inlining, after this commit, we use the type information from the stack, rather than the type of the var present on the stack.

In the future we might want to consider updating the type of var0 or creating a new var entirely with the correct type for consistency.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

.NET 8 Blazor - Memory Corruption
4 participants