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] Incorrect target method resolution in ldvirtftn+delegate creation #83003

Closed
vargaz opened this issue Mar 5, 2023 · 1 comment · Fixed by #83004
Closed

[mono] Incorrect target method resolution in ldvirtftn+delegate creation #83003

vargaz opened this issue Mar 5, 2023 · 1 comment · Fixed by #83004
Assignees

Comments

@vargaz
Copy link
Contributor

vargaz commented Mar 5, 2023

Description

Testcase:

using System;

class Foo
{
	public virtual void foo () {
		Console.WriteLine ("BASE");
	}
}

class Derived : Foo
{
	void foo2 (Action a) {
		a ();
	}

	public override void foo () {
		Console.WriteLine ("DERIVED");
		foo2 (base.foo);
	}

	public static void Main(string[] args) {
		var d = new Derived ();
		d.foo ();
    }
}

Expected results:

DERIVED
BASE

Actual result: recursive calls to Derived:foo () causing a stack overflow and crash.
Environment: dotnet/runtime master on arm64

This is caused by this code path in mini-trampolines.c:

		if (delegate->method_ptr == NULL && tramp_info->method == NULL && delegate->target != NULL && method->flags & METHOD_ATTRIBUTE_VIRTUAL) {
			/* tramp_info->method == NULL happens when someone asks us to JIT some delegate's
			 * Invoke method (see compile_special).  In that case if method is virtual, the target
			 * could be some derived class, so we need to find the correct override.
			 */

This only happens on arm64 because arm64 doesn't have fast virtual delegate invoke trampolines, so we bail out of
handle_delegate_ctor ():

		if (invoke_context_used || !mono_get_delegate_virtual_invoke_impl (mono_method_signature_internal (invoke), target_method_context_used ? NULL : method))
			return NULL;

Reproduction Steps

...

Expected behavior

...

Actual behavior

...

Regression?

...

Known Workarounds

...

Configuration

...

Other information

No response

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Mar 5, 2023
@vargaz vargaz self-assigned this Mar 5, 2023
@vargaz vargaz added area-Codegen-JIT-mono and removed untriaged New issue has not been triaged by the area owner labels Mar 5, 2023
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Mar 5, 2023
vargaz added a commit to vargaz/runtime that referenced this issue Mar 6, 2023
Use delegate->method_is_virtual instead of complicated conditions.
That flag is set by the code generated in handle_delegate_ctor () if
an ldvirtftn instruction was skipped, and thus virtual method
resolution needs to be done at call time.

Fixes dotnet#83003.
vargaz added a commit that referenced this issue Mar 8, 2023
…83004)

Use delegate->method_is_virtual instead of complicated conditions.
That flag is set by the code generated in handle_delegate_ctor () if
an ldvirtftn instruction was skipped, and thus virtual method
resolution needs to be done at call time.

Fixes #83003.
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Mar 8, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Apr 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant