radtek / obfuscar

Automatically exported from code.google.com/p/obfuscar
0 stars 0 forks source link

Problem with Attributes and type renaming #18

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Here's the code : 

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    public class BarAttribute : Attribute
    {
        public BarAttribute(Type type)
        {
            //saves type or whatever
        }
    }

    public interface IFoo
    {
        void Baz();
    }

    [Bar(typeof(IFoo))]
    class Foo : IFoo
    {
        public void Baz()
        {
            Console.WriteLine("I R bAb00n!");
        }
    }

    class Class1
    {
        public static void Main()
        {
            try
            {
                Type type = typeof(Foo);
                object[] moduleAttributes = type.GetCustomAttributes(typeof(BarAttribute), false);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
}

2. Compile and then obfuscate.
3. Run.

What is the expected output? What do you see instead?
expected : No output at all.
instead : exception details - 
         System.TypeLoadException: Could not load type 'ConsoleApplication1.IFoo' from assembly 'ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
            at System.Reflection.CustomAttribute._CreateCaObject(Void* pModule, Void* pCtor, Byte** ppBlob, Byte* pEndBlob, Int32* pcNamedArgs)
            at System.Reflection.CustomAttribute.CreateCaObject(Module module, RuntimeMethodHandle ctor, IntPtr& blob, IntPtr blobEnd, Int32& namedArgs)
            at System.Reflection.CustomAttribute.GetCustomAttributes(Module decoratedModule, Int32 decoratedMetadataToken, Int32 pcaCount, RuntimeType attributeFilterType, Boolean 
mustBeInheritable, IList derivedAttributes)
            at System.Reflection.CustomAttribute.GetCustomAttributes(RuntimeType type, RuntimeType caType, Boolean inherit)
            at System.RuntimeType.GetCustomAttributes(Type attributeType, Boolean inherit)
            at A.b.A()

What version of the product are you using? On what operating system?
1.4.0. Windows XP.

Please provide any additional information below.
See attached reflector screenshot. It's pretty obvious what's going on - the 
reference to IFoo in the attribute constructor is not renamed.

Original issue reported on code.google.com by silve...@gmail.com on 21 Jul 2009 at 3:02

Attachments:

GoogleCodeExporter commented 9 years ago
Here's an inefficient but working fix - In Obfuscar.cs, RenameType(), after the 
first 
foreach, add this :

 foreach (TypeDefinition typeDef in info.Definition.MainModule.Types)
            {
                foreach (CustomAttribute attrib in typeDef.CustomAttributes)
                {
                    for (int i = 0; i < attrib.ConstructorParameters.Count; i++)
                    {
                        string str;
                        if (!string.IsNullOrEmpty(str = 
attrib.ConstructorParameters[i] as string) &&
                            str == string.Format("{0}.{1}", type.Namespace, 
type.Name))
                        {
                            attrib.ConstructorParameters[i] = newTypeKey.Fullname;
                        }
                    }
                }
            }

Original comment by silve...@gmail.com on 22 Jul 2009 at 10:47

GoogleCodeExporter commented 9 years ago
I made a fix for that, which is part of version 1.5 now.
It uses a lookup dictionary, so it should be more efficient.
Additionally it works on types from other assemblies and on property and field
initializers in attribute constructors.

Original comment by webbi...@gmail.com on 6 Nov 2009 at 3:05