You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Variant static interface dispatch crashes the runtime #78621
@lambdageek could you point me at the logic in Mono that is choosing a specific implementation.
In particular, I see that FooBarBaz implements IBaz, IFoo<Mid>, IBar<Mid>, IFoo<Base> and IBar<Derived>. For the cases which crash (which is obviously a runtime bug), there are 2 implementations which could be chosen, such as IBaz.GetString or IFoo<Base>.GetString As far as I can tell from the definition of More Specific type from the default interfaces feature, IBaz and IFoo<Base> are unrelated types even though they will both satisfy IBar<Mid>.GetString and so are considered equally specific, and therefore dispatch should simply throw an AmbiguousImplementationException.
@AlekseyTs Do you agree that this should throw at runtime with an AmbiguousImplementationException?
Would we expect a different behavior from variant dispatch on instance default interface methods? If I change the above program to use instance methods (and pass a this) - program is at the bottom for posterity, I get:
IIRC we had a discussion about ambiguities in the variant cases for instance methods here dotnet/coreclr#21355 (comment) and over emails that I cannot find. This is all just repressed memories at this point.
Do you agree that this should throw at runtime with an AmbiguousImplementationException?
I do not think I agree. Variance equivalence is not part of the most specific type rule. There is only one implementation of IFoo<Mid>.GetString in the picture from that perspective.
@lambdageek could you point me at the logic in Mono that is choosing a specific implementation.
In general the code is class-setup-vtable.c: mono_class_setup_vtable_general is driving. Explicit overrides are considered by apply_override which builds up a conflict_map if there are multiple candidates - at this point we just collect the possible implementations - we don't attempt to prune anything. Just the existence of a conflict map is not indication of a conflict. We call handle_dim_conflicts to consider all the entries in the conflict map for each declaration and only note an actual conflict if there is not a single most specific class left. We store the list of conflicting declarations on the MonoClass. This all happens more or less independently of filling in the vtable slots.
At JIT time if there is a call to a marked ambiguous declaration we replace the call by a throw.
So even in the ambiguous case the vtable slot points to some implementation. We just count on the execution engine to use the ambiguous declarations list and to never to emit a call to that slot.
I haven't traced this specific example to understand what Mono is doing, but it seems like the conflict map would mark at least some of the declarations as ambiguous. On the other hand the vtable will have some arbitrary implementation in there (probably in declaration order - but I haven't checked). I suspect the JIT-time check "are we calling a method declaration that has been marked as ambiguous" isn't working right for some of these cases so we end up with a call to whatever was left in the vtable.
The other possibility is that we're not computing the ambiguous declarations correctly and so we don't consider that there are conflicts here.
note: The cases for GetTheFooString<FooBarBaz, Derived>(new FooBarBaz()) and GetTheBarString<FooBarBaz, Base>(new FooBarBaz()) are different from coreclr
Ok, so the universal agreement is that this should behave exactly like the instance case does, and that Mono disagrees with CoreCLR in what that exactly means. I'll flesh these test cases out, and make a final test case (that follows CoreCLR behavior, as it looks generally more correct to me than the Mono stuff(Especially as it is more likely customers have taken a dependency on CoreCLR behavior). Also, there is no mechanism for an ambiguous match to occur in this part of the code in CoreCLR, so I may put that in another test case.
Activity
am11 commentedon Nov 21, 2022
Fwiw, mono does the right thing. e.g. with blazorwasm net8.0 app,
paste this code in
Pages/Index.razorand the browser will render:
MichalStrehovsky commentedon Nov 22, 2022
Cc @trylek @davidwrighton for static virtual bugs
davidwrighton commentedon Jul 9, 2023
@lambdageek could you point me at the logic in Mono that is choosing a specific implementation.
In particular, I see that
FooBarBazimplementsIBaz,IFoo<Mid>,IBar<Mid>,IFoo<Base>andIBar<Derived>. For the cases which crash (which is obviously a runtime bug), there are 2 implementations which could be chosen, such asIBaz.GetStringorIFoo<Base>.GetStringAs far as I can tell from the definition of More Specific type from the default interfaces feature,IBazandIFoo<Base>are unrelated types even though they will both satisfyIBar<Mid>.GetStringand so are considered equally specific, and therefore dispatch should simply throw anAmbiguousImplementationException.@AlekseyTs Do you agree that this should throw at runtime with an
AmbiguousImplementationException?MichalStrehovsky commentedon Jul 10, 2023
Would we expect a different behavior from variant dispatch on instance default interface methods? If I change the above program to use instance methods (and pass a this) - program is at the bottom for posterity, I get:
IIRC we had a discussion about ambiguities in the variant cases for instance methods here dotnet/coreclr#21355 (comment) and over emails that I cannot find. This is all just repressed memories at this point.
Click to expand the C# program
AlekseyTs commentedon Jul 10, 2023
I do not think I agree. Variance equivalence is not part of the most specific type rule. There is only one implementation of
IFoo<Mid>.GetStringin the picture from that perspective.lambdageek commentedon Jul 10, 2023
In general the code is
class-setup-vtable.c:mono_class_setup_vtable_generalis driving. Explicit overrides are considered byapply_overridewhich builds up aconflict_mapif there are multiple candidates - at this point we just collect the possible implementations - we don't attempt to prune anything. Just the existence of a conflict map is not indication of a conflict. We callhandle_dim_conflictsto consider all the entries in the conflict map for each declaration and only note an actual conflict if there is not a single most specific class left. We store the list of conflicting declarations on the MonoClass. This all happens more or less independently of filling in the vtable slots.At JIT time if there is a call to a marked ambiguous declaration we replace the call by a throw.
So even in the ambiguous case the vtable slot points to some implementation. We just count on the execution engine to use the ambiguous declarations list and to never to emit a call to that slot.
I haven't traced this specific example to understand what Mono is doing, but it seems like the conflict map would mark at least some of the declarations as ambiguous. On the other hand the vtable will have some arbitrary implementation in there (probably in declaration order - but I haven't checked). I suspect the JIT-time check "are we calling a method declaration that has been marked as ambiguous" isn't working right for some of these cases so we end up with a call to whatever was left in the vtable.
The other possibility is that we're not computing the ambiguous declarations correctly and so we don't consider that there are conflicts here.
For the instance testcase from #78621 (comment)
Mono gives:
note: The cases for
GetTheFooString<FooBarBaz, Derived>(new FooBarBaz())andGetTheBarString<FooBarBaz, Base>(new FooBarBaz())are different from coreclrdavidwrighton commentedon Jul 10, 2023
Ok, so the universal agreement is that this should behave exactly like the instance case does, and that Mono disagrees with CoreCLR in what that exactly means. I'll flesh these test cases out, and make a final test case (that follows CoreCLR behavior, as it looks generally more correct to me than the Mono stuff(Especially as it is more likely customers have taken a dependency on CoreCLR behavior). Also, there is no mechanism for an ambiguous match to occur in this part of the code in CoreCLR, so I may put that in another test case.
MichalStrehovsky commentedon Jul 10, 2023
Sounds good - Mono is known to have differences in this area even prior to default/static interface methods (mono/mono#6312).
Fix virtual static dispatch for variant interfaces when using default…