重构 ObfuzIgnore计算,使用 ObfuzIgnoreScopeComputeCache提升计算性能

before-split
walon 2025-05-29 22:20:27 +08:00
parent 84ed5b127a
commit b113364214
11 changed files with 215 additions and 147 deletions

View File

@ -15,9 +15,11 @@ namespace Obfuz.ObfusPasses.FieldEncrypt
} }
private readonly XmlFieldRuleParser<ObfuscationRule> _configParser; private readonly XmlFieldRuleParser<ObfuscationRule> _configParser;
private readonly ObfuzIgnoreScopeComputeCache _obfuzIgnoreScopeComputeCache;
public ConfigurableEncryptPolicy(List<string> toObfuscatedAssemblyNames, List<string> configFiles) public ConfigurableEncryptPolicy(ObfuzIgnoreScopeComputeCache obfuzIgnoreScopeComputeCache, List<string> toObfuscatedAssemblyNames, List<string> configFiles)
{ {
_obfuzIgnoreScopeComputeCache = obfuzIgnoreScopeComputeCache;
_configParser = new XmlFieldRuleParser<ObfuscationRule>(toObfuscatedAssemblyNames, ParseRule, null); _configParser = new XmlFieldRuleParser<ObfuscationRule>(toObfuscatedAssemblyNames, ParseRule, null);
_configParser.LoadConfigs(configFiles); _configParser.LoadConfigs(configFiles);
} }
@ -33,7 +35,7 @@ namespace Obfuz.ObfusPasses.FieldEncrypt
{ {
return true; return true;
} }
if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(field, field.DeclaringType, ObfuzScope.Field)) if (_obfuzIgnoreScopeComputeCache.HasSelfOrInheritObfuzIgnoreScope(field, field.DeclaringType, ObfuzScope.Field))
{ {
return false; return false;
} }

View File

@ -29,7 +29,7 @@ namespace Obfuz.ObfusPasses.FieldEncrypt
{ {
var ctx = ObfuscationPassContext.Current; var ctx = ObfuscationPassContext.Current;
_memoryEncryptor = new DefaultFieldEncryptor(ctx.encryptionScopeProvider, ctx.moduleEntityManager, _settings); _memoryEncryptor = new DefaultFieldEncryptor(ctx.encryptionScopeProvider, ctx.moduleEntityManager, _settings);
_encryptionPolicy = new ConfigurableEncryptPolicy(ctx.coreSettings.assembliesToObfuscate, _settings.ruleFiles); _encryptionPolicy = new ConfigurableEncryptPolicy(ctx.obfuzIgnoreScopeComputeCache, ctx.coreSettings.assembliesToObfuscate, _settings.ruleFiles);
} }
public override void Stop() public override void Stop()

View File

@ -5,6 +5,13 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
{ {
public class SystemRenamePolicy : ObfuscationPolicyBase public class SystemRenamePolicy : ObfuscationPolicyBase
{ {
private readonly ObfuzIgnoreScopeComputeCache _obfuzIgnoreScopeComputeCache;
public SystemRenamePolicy(ObfuzIgnoreScopeComputeCache obfuzIgnoreScopeComputeCache)
{
_obfuzIgnoreScopeComputeCache = obfuzIgnoreScopeComputeCache;
}
private bool IsFullIgnoreObfuscatedType(TypeDef typeDef) private bool IsFullIgnoreObfuscatedType(TypeDef typeDef)
{ {
return typeDef.FullName == "Obfuz.ObfuzIgnoreAttribute" || typeDef.FullName == "Obfuz.ObfuzScope" || typeDef.FullName == "Obfuz.EncryptFieldAttribute"; return typeDef.FullName == "Obfuz.ObfuzIgnoreAttribute" || typeDef.FullName == "Obfuz.ObfuzScope" || typeDef.FullName == "Obfuz.EncryptFieldAttribute";
@ -22,7 +29,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
return false; return false;
} }
if (MetaUtil.HasObfuzIgnoreScope(typeDef, ObfuzScope.TypeName) || MetaUtil.HasEnclosingObfuzIgnoreScope(typeDef.DeclaringType, ObfuzScope.TypeName)) if (_obfuzIgnoreScopeComputeCache.HasSelfOrEnclosingObfuzIgnoreScope(typeDef, ObfuzScope.TypeName))
{ {
return false; return false;
} }
@ -40,7 +47,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
return false; return false;
} }
if (MetaUtil.HasSelfOrInheritPropertyOrEventOrOrTypeDefIgnoreMethodName(methodDef)) if (_obfuzIgnoreScopeComputeCache.HasSelfOrInheritPropertyOrEventOrOrTypeDefIgnoreMethodName(methodDef))
{ {
return false; return false;
} }
@ -53,7 +60,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
{ {
return false; return false;
} }
if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(fieldDef, fieldDef.DeclaringType, ObfuzScope.Field)) if (_obfuzIgnoreScopeComputeCache.HasSelfOrInheritObfuzIgnoreScope(fieldDef, fieldDef.DeclaringType, ObfuzScope.Field))
{ {
return false; return false;
} }
@ -70,7 +77,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
{ {
return false; return false;
} }
if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(propertyDef, propertyDef.DeclaringType, ObfuzScope.PropertyName)) if (_obfuzIgnoreScopeComputeCache.HasSelfOrInheritObfuzIgnoreScope(propertyDef, propertyDef.DeclaringType, ObfuzScope.PropertyName))
{ {
return false; return false;
} }
@ -83,7 +90,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus.Policies
{ {
return false; return false;
} }
if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(eventDef, eventDef.DeclaringType, ObfuzScope.EventName)) if (_obfuzIgnoreScopeComputeCache.HasSelfOrInheritObfuzIgnoreScope(eventDef, eventDef.DeclaringType, ObfuzScope.EventName))
{ {
return false; return false;
} }

View File

@ -77,7 +77,7 @@ namespace Obfuz.ObfusPasses.SymbolObfus
var totalRenamePolicies = new List<IObfuscationPolicy> var totalRenamePolicies = new List<IObfuscationPolicy>
{ {
new SupportPassPolicy(ctx.passPolicy), new SupportPassPolicy(ctx.passPolicy),
new SystemRenamePolicy(), new SystemRenamePolicy(ctx.obfuzIgnoreScopeComputeCache),
new UnityRenamePolicy(), new UnityRenamePolicy(),
obfuscateRuleConfig, obfuscateRuleConfig,
}; };

View File

@ -12,6 +12,12 @@ namespace Obfuz
{ {
public class ObfuscationMethodWhitelist public class ObfuscationMethodWhitelist
{ {
private readonly ObfuzIgnoreScopeComputeCache _obfuzComputeCache;
public ObfuscationMethodWhitelist(ObfuzIgnoreScopeComputeCache obfuzComputeCache)
{
_obfuzComputeCache = obfuzComputeCache;
}
public bool IsInWhiteList(ModuleDef module) public bool IsInWhiteList(ModuleDef module)
{ {
@ -52,7 +58,7 @@ namespace Obfuz
{ {
return true; return true;
} }
if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(method, typeDef, ObfuzScope.MethodBody)) if (_obfuzComputeCache.HasSelfOrInheritObfuzIgnoreScope(method, typeDef, ObfuzScope.MethodBody))
{ {
return true; return true;
} }
@ -80,7 +86,7 @@ namespace Obfuz
{ {
return true; return true;
} }
if (MetaUtil.HasSelfOrInheritObfuzIgnoreScope(type, type.DeclaringType, ObfuzScope.TypeName)) if (_obfuzComputeCache.HasSelfOrInheritObfuzIgnoreScope(type, type.DeclaringType, ObfuzScope.TypeName))
{ {
return true; return true;
} }

View File

@ -68,6 +68,7 @@ namespace Obfuz
public AssemblyCache assemblyCache; public AssemblyCache assemblyCache;
public List<ModuleDef> modulesToObfuscate; public List<ModuleDef> modulesToObfuscate;
public List<ModuleDef> allObfuscationRelativeModules; public List<ModuleDef> allObfuscationRelativeModules;
public ObfuzIgnoreScopeComputeCache obfuzIgnoreScopeComputeCache;
public EncryptionScopeProvider encryptionScopeProvider; public EncryptionScopeProvider encryptionScopeProvider;
public ConstFieldAllocator constFieldAllocator; public ConstFieldAllocator constFieldAllocator;

View File

@ -283,6 +283,7 @@ namespace Obfuz
EncryptionScopeProvider encryptionScopeProvider = CreateEncryptionScopeProvider(); EncryptionScopeProvider encryptionScopeProvider = CreateEncryptionScopeProvider();
var moduleEntityManager = new GroupByModuleEntityManager(); var moduleEntityManager = new GroupByModuleEntityManager();
var obfuzIgnoreScopeComputeCache = new ObfuzIgnoreScopeComputeCache();
var rvaDataAllocator = new RvaDataAllocator(encryptionScopeProvider, moduleEntityManager); var rvaDataAllocator = new RvaDataAllocator(encryptionScopeProvider, moduleEntityManager);
var constFieldAllocator = new ConstFieldAllocator(encryptionScopeProvider, rvaDataAllocator, moduleEntityManager); var constFieldAllocator = new ConstFieldAllocator(encryptionScopeProvider, rvaDataAllocator, moduleEntityManager);
_ctx = new ObfuscationPassContext _ctx = new ObfuscationPassContext
@ -294,10 +295,11 @@ namespace Obfuz
moduleEntityManager = moduleEntityManager, moduleEntityManager = moduleEntityManager,
encryptionScopeProvider = encryptionScopeProvider, encryptionScopeProvider = encryptionScopeProvider,
obfuzIgnoreScopeComputeCache = obfuzIgnoreScopeComputeCache,
rvaDataAllocator = rvaDataAllocator, rvaDataAllocator = rvaDataAllocator,
constFieldAllocator = constFieldAllocator, constFieldAllocator = constFieldAllocator,
whiteList = new ObfuscationMethodWhitelist(), whiteList = new ObfuscationMethodWhitelist(obfuzIgnoreScopeComputeCache),
passPolicy = _passPolicy, passPolicy = _passPolicy,
}; };
ObfuscationPassContext.Current = _ctx; ObfuscationPassContext.Current = _ctx;

View File

@ -830,141 +830,6 @@ namespace Obfuz.Utils
return result.ToString(); return result.ToString();
} }
public static ObfuzScope? GetObfuzIgnoreScope(IHasCustomAttribute obj)
{
var ca = obj.CustomAttributes.FirstOrDefault(c => c.AttributeType.FullName == "Obfuz.ObfuzIgnoreAttribute");
if (ca == null)
{
return null;
}
var scope = (ObfuzScope)ca.ConstructorArguments[0].Value;
return scope;
}
public static bool HasObfuzIgnoreScope(IHasCustomAttribute obj, ObfuzScope targetScope)
{
ObfuzScope? objScope = GetObfuzIgnoreScope(obj);
if (objScope == null)
{
return false;
}
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("ApplyToNestedTypes", false);
if (applyToNestedTypesArg != null && !(bool)applyToNestedTypesArg.Value)
{
return false;
}
}
return (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)
{
return HasObfuzIgnoreScope(obj, targetScope) || HasDeclaringOrEnclosingMemberObfuzIgnoreScope(declaringType, targetScope);
}
public static bool HasSelfOrInheritPropertyOrEventOrOrTypeDefObfuzIgnoreScope(MethodDef obj, ObfuzScope targetScope)
{
if (HasObfuzIgnoreScope(obj, targetScope))
{
return true;
}
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) public static bool HasCompilerGeneratedAttribute(IHasCustomAttribute obj)
{ {
return obj.CustomAttributes.Find("System.Runtime.CompilerServices.CompilerGeneratedAttribute") != null; return obj.CustomAttributes.Find("System.Runtime.CompilerServices.CompilerGeneratedAttribute") != null;

View File

@ -0,0 +1,172 @@
using dnlib.DotNet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Obfuz.Utils
{
public class ObfuzIgnoreScopeComputeCache
{
private readonly CachedDictionary<IHasCustomAttribute, ObfuzScope?> _selfObfuzIgnoreScopeCache;
private readonly CachedDictionary<TypeDef, ObfuzScope?> _enclosingObfuzIgnoreScopeCache;
private readonly CachedDictionary<TypeDef, ObfuzScope?> _declaringOrEnclosingMemberObfuzIgnoreScopeCache;
public ObfuzIgnoreScopeComputeCache()
{
_selfObfuzIgnoreScopeCache = new CachedDictionary<IHasCustomAttribute, ObfuzScope?>(GetObfuzIgnoreScope);
_enclosingObfuzIgnoreScopeCache = new CachedDictionary<TypeDef, ObfuzScope?>(GetEnclosingObfuzIgnoreScope);
_declaringOrEnclosingMemberObfuzIgnoreScopeCache = new CachedDictionary<TypeDef, ObfuzScope?>(GetDeclaringOrEnclosingMemberObfuzIgnoreScope);
}
private ObfuzScope? GetObfuzIgnoreScope(IHasCustomAttribute obj)
{
var ca = obj.CustomAttributes.FirstOrDefault(c => c.AttributeType.FullName == "Obfuz.ObfuzIgnoreAttribute");
if (ca == null)
{
return null;
}
var scope = (ObfuzScope)ca.ConstructorArguments[0].Value;
return scope;
}
private ObfuzScope? GetEnclosingObfuzIgnoreScope(TypeDef typeDef)
{
TypeDef cur = typeDef.DeclaringType;
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 ? (ObfuzScope?)scope : null;
}
cur = cur.DeclaringType;
}
return null;
}
private ObfuzScope? GetDeclaringOrEnclosingMemberObfuzIgnoreScope(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;
if (cur != typeDef)
{
CANamedArgument applyToNestedTypesArg = ca.GetNamedArgument("ApplyToNestedTypes", false);
if (applyToNestedTypesArg != null && !(bool)applyToNestedTypesArg.Value)
{
return null;
}
}
return scope;
}
cur = cur.DeclaringType;
}
return null;
}
private bool HasObfuzIgnoreScope(IHasCustomAttribute obj, ObfuzScope targetScope)
{
ObfuzScope? objScope = _selfObfuzIgnoreScopeCache.GetValue(obj);
return objScope != null && (objScope & targetScope) != 0;
}
private bool HasEnclosingObfuzIgnoreScope(TypeDef typeDef, ObfuzScope targetScope)
{
ObfuzScope? enclosingScope = _enclosingObfuzIgnoreScopeCache.GetValue(typeDef);
return enclosingScope != null && (enclosingScope & targetScope) != 0;
}
private bool HasDeclaringOrEnclosingMemberObfuzIgnoreScope(TypeDef typeDef, ObfuzScope targetScope)
{
if (typeDef == null)
{
return false;
}
ObfuzScope? declaringOrEnclosingScope = _declaringOrEnclosingMemberObfuzIgnoreScopeCache.GetValue(typeDef);
return declaringOrEnclosingScope != null && (declaringOrEnclosingScope & targetScope) != 0;
}
public bool HasSelfOrEnclosingObfuzIgnoreScope(TypeDef typeDef, ObfuzScope targetScope)
{
return HasObfuzIgnoreScope(typeDef, targetScope) || HasEnclosingObfuzIgnoreScope(typeDef, targetScope);
}
public bool HasSelfOrInheritObfuzIgnoreScope(IHasCustomAttribute obj, TypeDef declaringType, ObfuzScope targetScope)
{
return HasObfuzIgnoreScope(obj, targetScope) || HasDeclaringOrEnclosingMemberObfuzIgnoreScope(declaringType, targetScope);
}
public bool HasSelfOrInheritPropertyOrEventOrOrTypeDefObfuzIgnoreScope(MethodDef obj, ObfuzScope targetScope)
{
if (HasObfuzIgnoreScope(obj, targetScope))
{
return true;
}
TypeDef declaringType = obj.DeclaringType;
foreach (var propertyDef in declaringType.Properties)
{
if (HasObfuzIgnoreScope(propertyDef, targetScope))
{
return true;
}
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)
{
if (HasObfuzIgnoreScope(eventDef, targetScope))
{
return true;
}
return HasObfuzIgnoreScope(eventDef, targetScope) || HasDeclaringOrEnclosingMemberObfuzIgnoreScope(declaringType, targetScope);
}
}
return HasDeclaringOrEnclosingMemberObfuzIgnoreScope(declaringType, targetScope);
}
public 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);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2f64b9a72981c0e45a03501db02e6538
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -14,6 +14,8 @@ namespace Obfuz
public bool ApplyToNestedTypes { get; set; } = true; public bool ApplyToNestedTypes { get; set; } = true;
public bool ApplyToChildTypes { get; set; } = false;
public ObfuzIgnoreAttribute(ObfuzScope scope = ObfuzScope.All) public ObfuzIgnoreAttribute(ObfuzScope scope = ObfuzScope.All)
{ {
this.Scope = scope; this.Scope = scope;