Definitely an Edge Case

Posted by drcforbin on September 19, 2006

It seems that not even Remotesoft's .NET Obfuscator is immune from bugs caused by edge cases. Seems that they have some trouble with generic classes overloaded with covariant returns.

I managed to simplify the problem I'm running into...here's my example:

namespace TestTemplate
{
    class ClassX { }

    class ClassA<ObjectType>
        where ObjectType : class
    {
        public virtual ObjectType PrintAndReturn( )
        {
            Console.WriteLine( "This should never be called" );
            return null;
        }
    }

    class ClassB : ClassA<ClassX>
    {
        public override ClassX PrintAndReturn( )
        {
            Console.WriteLine( "This _should_ be called" );
            return null;
        }
    }

    class Program
    {
        static void Main( string[] args )
        {
            ClassB b = new ClassB( );
            b.PrintAndReturn( );
        }
    }
}

In the above case, it should (and does) print "This should be called". After obfuscating the resulting assembly, using the 2.0 version of the obfuscator, it prints "This should never be called". It seems that following obfuscation, ClassB.PrintAndReturn is given a different name than ClassA.PrintAndReturn, so the overloading doesn't work and the base class method is called instead. Here's the obfuscator's log:

.module TestTemplate
.namespace TestTemplate
{
    .class private ClassX => A.A
    {
        .method public void .ctor()
    }

    .class private ClassA`1 => A.a
    {
        .method public virtual !0 PrintAndReturn() => A
        .method public void .ctor()
    }

    .class private ClassB => B.A
    {
        .method public virtual class TestTemplate.ClassX PrintAndReturn() => a
        .method public void .ctor()
    }

    .class private Program => a.A
    {
        .method private static void Main(string[] args) => A
        .method public void .ctor()
    }
}

I've sent them some email about the problem, and in the meantime, I've set it to not obfuscate methods that are overloaded this way (I have explicitly marked the methods by name).

Will post an update when I get a response.

Update: I no longer recommend using Remotesoft's products.

Update 2: End the tyranny of buggy obfuscation products and poor technical support. Use Obfuscar instead.