From 78699d7959867f8718d4fc7e8b12914533df1ebe Mon Sep 17 00:00:00 2001 From: walon Date: Mon, 26 May 2025 19:22:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DObfuzIgnoreAttribute=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Policies/SystemRenamePolicy.cs | 24 ++- .../Editor/Utils/MetaUtil.cs | 149 +++++++++++++++--- .../Runtime/ObfuzIgnoreAttribute.cs | 15 +- 3 files changed, 151 insertions(+), 37 deletions(-) diff --git a/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/Policies/SystemRenamePolicy.cs b/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/Policies/SystemRenamePolicy.cs index aac2747..9917b61 100644 --- a/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/Policies/SystemRenamePolicy.cs +++ b/com.code-philosophy.obfuz/Editor/ObfusPasses/SymbolObfus/Policies/SystemRenamePolicy.cs @@ -5,14 +5,24 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies { public class SystemRenamePolicy : ObfuscationPolicyBase { + private bool IsFullIgnoreObfuscatedType(TypeDef typeDef) + { + return typeDef.FullName == "Obfuz.ObfuzIgnoreAttribute" || typeDef.FullName == "Obfuz.ObfuzScope"; + } + public override bool NeedRename(TypeDef typeDef) { string name = typeDef.Name; - if (name == "" || name == "ObfuzIgnoreAttribute") + if (name == "") { return false; } - if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(typeDef, typeDef.DeclaringType, ObfuzScope.TypeName)) + if (IsFullIgnoreObfuscatedType(typeDef)) + { + return false; + } + + if (MetaUtil.HasObfuzIgnoreScope(typeDef, ObfuzScope.TypeName) || MetaUtil.HasEnclosingObfuzIgnoreScope(typeDef.DeclaringType, ObfuzScope.TypeName)) { return false; } @@ -21,7 +31,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies public override bool NeedRename(MethodDef methodDef) { - if (methodDef.DeclaringType.IsDelegate) + if (methodDef.DeclaringType.IsDelegate || IsFullIgnoreObfuscatedType(methodDef.DeclaringType)) { return false; } @@ -30,7 +40,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies return false; } - if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(methodDef, methodDef.DeclaringType, ObfuzScope.MethodName)) + if (MetaUtil.HasSelfOrInheritPropertyOrEventOrOrTypeDefIgnoreMethodName(methodDef)) { return false; } @@ -39,7 +49,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies public override bool NeedRename(FieldDef fieldDef) { - if (fieldDef.DeclaringType.IsDelegate) + if (fieldDef.DeclaringType.IsDelegate || IsFullIgnoreObfuscatedType(fieldDef.DeclaringType)) { return false; } @@ -56,7 +66,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies public override bool NeedRename(PropertyDef propertyDef) { - if (propertyDef.DeclaringType.IsDelegate) + if (propertyDef.DeclaringType.IsDelegate || IsFullIgnoreObfuscatedType(propertyDef.DeclaringType)) { return false; } @@ -69,7 +79,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies public override bool NeedRename(EventDef eventDef) { - if (eventDef.DeclaringType.IsDelegate) + if (eventDef.DeclaringType.IsDelegate || IsFullIgnoreObfuscatedType(eventDef.DeclaringType)) { return false; } diff --git a/com.code-philosophy.obfuz/Editor/Utils/MetaUtil.cs b/com.code-philosophy.obfuz/Editor/Utils/MetaUtil.cs index 92120f7..f4ca314 100644 --- a/com.code-philosophy.obfuz/Editor/Utils/MetaUtil.cs +++ b/com.code-philosophy.obfuz/Editor/Utils/MetaUtil.cs @@ -816,23 +816,23 @@ namespace Obfuz.Utils return scope; } - public static ObfuzScope? GetSelfOrInheritObfuzIgnoreScope(TypeDef typeDef) - { - TypeDef cur = typeDef; - while (cur != null) - { - var ca = cur.CustomAttributes?.FirstOrDefault(c => c.AttributeType.FullName == "Obfuz.ObfuzIgnoreAttribute"); - if (ca != null) - { - var scope = (ObfuzScope)ca.ConstructorArguments[0].Value; - CANamedArgument inheritByNestedTypesArg = ca.GetNamedArgument("ApplyToMembers", false); - bool inheritByNestedTypes = inheritByNestedTypesArg == null || (bool)inheritByNestedTypesArg.Value; - return cur == typeDef || inheritByNestedTypes ? (ObfuzScope?) scope : null; - } - cur = cur.DeclaringType; - } - return null; - } + //public static ObfuzScope? GetSelfOrInheritObfuzIgnoreScope(TypeDef typeDef) + //{ + // TypeDef cur = typeDef; + // while (cur != null) + // { + // var ca = cur.CustomAttributes?.FirstOrDefault(c => c.AttributeType.FullName == "Obfuz.ObfuzIgnoreAttribute"); + // if (ca != null) + // { + // var scope = (ObfuzScope)ca.ConstructorArguments[0].Value; + // CANamedArgument inheritByNestedTypesArg = ca.GetNamedArgument("ApplyToMembers", false); + // bool inheritByNestedTypes = inheritByNestedTypesArg == null || (bool)inheritByNestedTypesArg.Value; + // return inheritByNestedTypes ? (ObfuzScope?) scope : null; + // } + // cur = cur.DeclaringType; + // } + // return null; + //} public static bool HasObfuzIgnoreScope(IHasCustomAttribute obj, ObfuzScope targetScope) @@ -845,16 +845,121 @@ namespace Obfuz.Utils return objScope != null && (objScope & targetScope) != 0; } + public static bool HasEnclosingObfuzIgnoreScope(TypeDef typeDef, ObfuzScope targetScope) + { + TypeDef cur = typeDef; + while (cur != null) + { + var ca = cur.CustomAttributes?.FirstOrDefault(c => c.AttributeType.FullName == "Obfuz.ObfuzIgnoreAttribute"); + if (ca != null) + { + var scope = (ObfuzScope)ca.ConstructorArguments[0].Value; + CANamedArgument inheritByNestedTypesArg = ca.GetNamedArgument("ApplyToNestedTypes", false); + bool inheritByNestedTypes = inheritByNestedTypesArg == null || (bool)inheritByNestedTypesArg.Value; + return inheritByNestedTypes && (scope & targetScope) != 0; + } + cur = cur.DeclaringType; + } + return false; + } + + public static bool HasDeclaringOrEnclosingMemberObfuzIgnoreScope(TypeDef typeDef, ObfuzScope targetScope) + { + TypeDef cur = typeDef; + while (cur != null) + { + var ca = cur.CustomAttributes?.FirstOrDefault(c => c.AttributeType.FullName == "Obfuz.ObfuzIgnoreAttribute"); + if (ca != null) + { + var scope = (ObfuzScope)ca.ConstructorArguments[0].Value; + if (cur != typeDef) + { + CANamedArgument applyToNestedTypesArg = ca.GetNamedArgument(cur == typeDef ? "ApplyToMembers" : "ApplyToNestedTypes", false); + if (applyToNestedTypesArg != null && !(bool)applyToNestedTypesArg.Value) + { + return false; + } + } + + CANamedArgument inheritByNestedTypesArg = ca.GetNamedArgument("ApplyToMembers", false); + bool inheritByNestedTypes = inheritByNestedTypesArg == null || (bool)inheritByNestedTypesArg.Value; + return inheritByNestedTypes && (scope & targetScope) != 0; + } + cur = cur.DeclaringType; + } + return false; + } + + //public static bool HasSelfOrInheritObfuzIgnoreScope(TypeDef obj, ObfuzScope targetScope) + //{ + // ObfuzScope? scope = GetSelfOrInheritObfuzIgnoreScope(obj); + // return scope != null && (scope & targetScope) != 0; + //} + public static bool HasSelfOrInheritObfuzIgnoreScope(IHasCustomAttribute obj, TypeDef declaringType, ObfuzScope targetScope) { - ObfuzScope? objScope = GetObfuzIgnoreScope(obj); - if (objScope != null) + return HasObfuzIgnoreScope(obj, targetScope) || HasDeclaringOrEnclosingMemberObfuzIgnoreScope(declaringType, targetScope); + } + + public static bool HasSelfOrInheritPropertyOrEventOrOrTypeDefObfuzIgnoreScope(MethodDef obj, ObfuzScope targetScope) + { + if (HasObfuzIgnoreScope(obj, targetScope)) { - return (objScope & targetScope) != 0; + return true; } - ObfuzScope? parentScope = GetSelfOrInheritObfuzIgnoreScope(declaringType); - return parentScope != null && (parentScope & targetScope) != 0; + TypeDef declaringType = obj.DeclaringType; + + foreach (var propertyDef in declaringType.Properties) + { + if (propertyDef.GetMethod == obj || propertyDef.SetMethod == obj) + { + return HasObfuzIgnoreScope(propertyDef, targetScope) || HasDeclaringOrEnclosingMemberObfuzIgnoreScope(declaringType, targetScope); + } + } + + foreach (var eventDef in declaringType.Events) + { + if (eventDef.AddMethod == obj || eventDef.RemoveMethod == obj) + { + ObfuzScope? eventScope = GetObfuzIgnoreScope(eventDef); + if (eventScope != null && (eventScope & targetScope) != 0) + { + return true; + } + return HasObfuzIgnoreScope(eventDef, targetScope) || HasDeclaringOrEnclosingMemberObfuzIgnoreScope(declaringType, targetScope); + } + } + + return HasDeclaringOrEnclosingMemberObfuzIgnoreScope(declaringType, targetScope); + } + + public static bool HasSelfOrInheritPropertyOrEventOrOrTypeDefIgnoreMethodName(MethodDef obj) + { + if (HasObfuzIgnoreScope(obj, ObfuzScope.MethodName)) + { + return true; + } + + TypeDef declaringType = obj.DeclaringType; + + foreach (var propertyDef in declaringType.Properties) + { + if (propertyDef.GetMethod == obj || propertyDef.SetMethod == obj) + { + return HasSelfOrInheritObfuzIgnoreScope(propertyDef, declaringType, ObfuzScope.PropertyGetterSetterName | ObfuzScope.MethodName); + } + } + + foreach (var eventDef in declaringType.Events) + { + if (eventDef.AddMethod == obj || eventDef.RemoveMethod == obj) + { + return HasSelfOrInheritObfuzIgnoreScope(eventDef, declaringType, ObfuzScope.EventAddRemoveFireName | ObfuzScope.MethodName); + } + } + + return HasDeclaringOrEnclosingMemberObfuzIgnoreScope(declaringType, ObfuzScope.MethodName); } public static bool HasCompilerGeneratedAttribute(IHasCustomAttribute obj) diff --git a/com.code-philosophy.obfuz/Runtime/ObfuzIgnoreAttribute.cs b/com.code-philosophy.obfuz/Runtime/ObfuzIgnoreAttribute.cs index b05062e..9ee448f 100644 --- a/com.code-philosophy.obfuz/Runtime/ObfuzIgnoreAttribute.cs +++ b/com.code-philosophy.obfuz/Runtime/ObfuzIgnoreAttribute.cs @@ -16,15 +16,12 @@ namespace Obfuz MethodParameter = 0x8, MethodBody = 0x10, Method = MethodName | MethodParameter | MethodBody, - PropertyName = 020, - PropertyGetter = 0x40, - PropertySetter = 0x80, - Property = PropertyName | PropertyGetter | PropertySetter, + PropertyName = 0x20, + PropertyGetterSetterName = 0x40, + Property = PropertyName | PropertyGetterSetterName, EventName = 0x100, - EventAdd = 0x200, - EventRemove = 0x400, - EventFire = 0x800, - Event = EventName | EventAdd | EventRemove, + EventAddRemoveFireName = 0x200, + Event = EventName | PropertyGetterSetterName, Module = 0x1000, All = TypeName | Field | Method | Property | Event, } @@ -36,6 +33,8 @@ namespace Obfuz public bool ApplyToMembers { get; set; } = true; + public bool ApplyToNestedTypes { get; set; } = true; + public ObfuzIgnoreAttribute(ObfuzScope scope = ObfuzScope.All) { this.Scope = scope;