不混淆被BurstCompile函数直接或者间接调用的函数的代码,但仍然混淆函数名。

main
walon 2025-07-14 11:45:15 +08:00
parent 083ddd3035
commit 6a4f84a9b0
5 changed files with 115 additions and 10 deletions

View File

@ -13,6 +13,7 @@ namespace Obfuz.Editor
public const string ObfuzScopeFullName = "Obfuz.ObfuzScope";
public const string EncryptFieldAttributeFullName = "Obfuz.EncryptFieldAttribute";
public const string GeneratedEncryptionVirtualMachineFullName = "Obfuz.EncryptionVM.GeneratedEncryptionVirtualMachine";
public const string EmbeddedAttributeFullName = "Microsoft.CodeAnalysis.EmbeddedAttribute";

View File

@ -9,10 +9,12 @@ namespace Obfuz
public class ObfuscationMethodWhitelist
{
private readonly ObfuzIgnoreScopeComputeCache _obfuzComputeCache;
private readonly BurstCompileComputeCache _burstCompileComputeCache;
public ObfuscationMethodWhitelist(ObfuzIgnoreScopeComputeCache obfuzComputeCache)
public ObfuscationMethodWhitelist(ObfuzIgnoreScopeComputeCache obfuzComputeCache, BurstCompileComputeCache burstCompileComputeCache)
{
_obfuzComputeCache = obfuzComputeCache;
_burstCompileComputeCache = burstCompileComputeCache;
}
public bool IsInWhiteList(ModuleDef module)
@ -31,7 +33,7 @@ namespace Obfuz
private bool DoesMethodContainsRuntimeInitializeOnLoadMethodAttributeAndLoadTypeGreaterEqualAfterAssembliesLoaded(MethodDef method)
{
CustomAttribute ca = method.CustomAttributes.Find("UnityEngine.RuntimeInitializeOnLoadMethodAttribute");
CustomAttribute ca = method.CustomAttributes.Find(ConstValues.RuntimeInitializedOnLoadMethodAttributeFullName);
if (ca != null && ca.ConstructorArguments.Count > 0)
{
RuntimeInitializeLoadType loadType = (RuntimeInitializeLoadType)ca.ConstructorArguments[0].Value;
@ -46,10 +48,10 @@ namespace Obfuz
public bool IsInWhiteList(MethodDef method)
{
TypeDef typeDef = method.DeclaringType;
if (IsInWhiteList(typeDef))
{
return true;
}
//if (IsInWhiteList(typeDef))
//{
// return true;
//}
if (method.Name.StartsWith(ConstValues.ObfuzInternalSymbolNamePrefix))
{
return true;
@ -58,12 +60,12 @@ namespace Obfuz
{
return true;
}
CustomAttribute ca = method.CustomAttributes.Find("UnityEngine.RuntimeInitializeOnLoadMethodAttribute");
CustomAttribute ca = method.CustomAttributes.Find(ConstValues.RuntimeInitializedOnLoadMethodAttributeFullName);
if (DoesMethodContainsRuntimeInitializeOnLoadMethodAttributeAndLoadTypeGreaterEqualAfterAssembliesLoaded(method))
{
return true;
}
if (method.CustomAttributes.Find(ConstValues.BurstCompileFullName) != null)
if (method.CustomAttributes.Find(ConstValues.BurstCompileFullName) != null || _burstCompileComputeCache.IsBurstCompileMethodOrReferencedByBurstCompileMethod(method))
{
return true;
}
@ -98,7 +100,7 @@ namespace Obfuz
//{
// return true;
//}
if (type.FullName == "Obfuz.EncryptionVM.GeneratedEncryptionVirtualMachine")
if (type.FullName == ConstValues.GeneratedEncryptionVirtualMachineFullName)
{
return true;
}

View File

@ -299,7 +299,7 @@ namespace Obfuz
obfuzIgnoreScopeComputeCache = obfuzIgnoreScopeComputeCache,
whiteList = new ObfuscationMethodWhitelist(obfuzIgnoreScopeComputeCache),
whiteList = new ObfuscationMethodWhitelist(obfuzIgnoreScopeComputeCache, new BurstCompileComputeCache(modulesToObfuscate, allObfuscationRelativeModules)),
passPolicy = _passPolicy,
};
ObfuscationPassContext.Current = _ctx;

View File

@ -0,0 +1,91 @@
using dnlib.DotNet;
using NUnit.Framework;
using System.Collections.Generic;
namespace Obfuz.Utils
{
public class BurstCompileComputeCache
{
private readonly List<ModuleDef> _modulesToObfuscate;
private readonly List<ModuleDef> _allObfuscationRelativeModules;
private readonly HashSet<MethodDef> _burstCompileMethods = new HashSet<MethodDef>();
private readonly HashSet<MethodDef> _burstCompileRelativeMethods = new HashSet<MethodDef>();
public BurstCompileComputeCache(List<ModuleDef> modulesToObfuscate, List<ModuleDef> allObfuscationRelativeModules)
{
_modulesToObfuscate = modulesToObfuscate;
_allObfuscationRelativeModules = allObfuscationRelativeModules;
Build();
}
private void BuildBurstCompileMethods()
{
foreach (var module in _allObfuscationRelativeModules)
{
foreach (var type in module.GetTypes())
{
bool hasBurstCompileAttribute = MetaUtil.HasBurstCompileAttribute(type);
foreach (var method in type.Methods)
{
if (hasBurstCompileAttribute || MetaUtil.HasBurstCompileAttribute(method))
{
_burstCompileMethods.Add(method);
}
}
}
}
}
private void CollectBurstCompileReferencedMethods()
{
var modulesToObfuscateSet = new HashSet<ModuleDef>(_modulesToObfuscate);
var allObfuscationRelativeModulesSet = new HashSet<ModuleDef>(_allObfuscationRelativeModules);
var pendingWalking = new Queue<MethodDef>(_burstCompileMethods);
var visitedMethods = new HashSet<MethodDef>();
while (pendingWalking.Count > 0)
{
var method = pendingWalking.Dequeue();
if (!visitedMethods.Add(method))
{
continue; // Skip already visited methods
}
if (modulesToObfuscateSet.Contains(method.Module))
{
_burstCompileRelativeMethods.Add(method);
}
if (!method.HasBody)
{
continue;
}
// Check for calls to other methods
foreach (var instruction in method.Body.Instructions)
{
if (instruction.OpCode.Code == dnlib.DotNet.Emit.Code.Call ||
instruction.OpCode.Code == dnlib.DotNet.Emit.Code.Callvirt)
{
MethodDef calledMethod = ((IMethod)instruction.Operand).ResolveMethodDef();
if (calledMethod == null || !allObfuscationRelativeModulesSet.Contains(calledMethod.Module) || visitedMethods.Contains(calledMethod))
{
continue; // Skip if the method could not be resolved
}
pendingWalking.Enqueue(calledMethod);
}
}
}
}
private void Build()
{
BuildBurstCompileMethods();
CollectBurstCompileReferencedMethods();
}
public bool IsBurstCompileMethodOrReferencedByBurstCompileMethod(MethodDef method)
{
return _burstCompileRelativeMethods.Contains(method);
}
}
}

View File

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