fix: disable call obfuscation for not public called method when publish mono backend because mono has more strict access checking, trying access private method in other class will raise MethodAccess Exception

before-split
walon 2025-05-19 13:40:13 +08:00
parent 305a56b3db
commit 9c450d7f07
3 changed files with 49 additions and 0 deletions

View File

@ -360,12 +360,32 @@ namespace Obfuz.ObfusPasses.CallObfus
return isWhiteList; return isWhiteList;
} }
private bool IsTypeSelfAndParentPublic(TypeDef type)
{
if (type.DeclaringType != null && !IsTypeSelfAndParentPublic(type.DeclaringType))
{
return false;
}
return type.IsPublic;
}
public override bool NeedObfuscateCalledMethod(MethodDef callerMethod, IMethod calledMethod, bool callVir, bool currentInLoop) public override bool NeedObfuscateCalledMethod(MethodDef callerMethod, IMethod calledMethod, bool callVir, bool currentInLoop)
{ {
if (IsInWhiteList(calledMethod)) if (IsInWhiteList(calledMethod))
{ {
return false; return false;
} }
// mono has more strict access control, calls non-public method will raise exception.
if (PlatformUtil.IsMonoBackend())
{
MethodDef calledMethodDef = calledMethod.ResolveMethodDef();
if (calledMethodDef != null && (!calledMethodDef.IsPublic || !IsTypeSelfAndParentPublic(calledMethodDef.DeclaringType)))
{
return false;
}
}
ObfuscationRule rule = GetMethodObfuscationRule(callerMethod); ObfuscationRule rule = GetMethodObfuscationRule(callerMethod);
if (currentInLoop && rule.obfuscateCallInLoop == false) if (currentInLoop && rule.obfuscateCallInLoop == false)
{ {

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEditor;
namespace Obfuz.Utils
{
public static class PlatformUtil
{
public static bool IsMonoBackend()
{
return PlayerSettings.GetScriptingBackend(EditorUserBuildSettings.selectedBuildTargetGroup)
== ScriptingImplementation.Mono2x;
}
}
}

View File

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