Washi1337 / OldRod

An automated KoiVM disassembler and devirtualisation utility
GNU General Public License v3.0
345 stars 80 forks source link

Interface implementations are not taken into consideration for type analysis. #47

Open ElektroKill opened 2 years ago

ElektroKill commented 2 years ago

Describe the bug A class's interface implementations are not taken into consideration for tye inference and type conversion insertion.

Example: IMyDisposable interface implements System.IDisposable interface and MyDisposable class implements IMyDisposable interface.

OldRod produces the following code: image The castclass to System.IDisposable is redundant since the parameter which is loaded before it is of type IMyDisposable which implements IDisposable.

If we look at the code before virtualization and then devirtualization we see exactly that. image

Similar behavior can be seen where the aforementioned method is used: image We see another redundant cast to, this time, IMyDisposable. As before this cast is redundant as the MyDisposable class already implements that interface.

If we look at the non-processed code we see the cast is missing: image

To Reproduce Devirtualize the sample provided in the zip file below: sample2.zip

Washi1337 commented 2 years ago

This is a direct result of https://github.com/Washi1337/OldRod/blob/a8dfa553769f5fcef3c5f2338242fa1561525acd/src/OldRod.Core/Recompiler/Transform/TypeHelper.cs#L180 only collecting base types during the construction of the type hierarchy. This "works" because every interface eventually is an object, as was mentioned in #48, so these casts aren't wrong.

Including interfaces in the type hierarchy complicates things, as the inheritance graph is no longer necessarily a tree any more. This makes looking for common base types more involved, especially if there is diverging paths in the inheritance graphs (which path to take? do we take a path or just resort to Object in those cases?).

We'll have to think about what is best here.