From 9c450d7f0763ece959df3364f0c16ad8e683296b Mon Sep 17 00:00:00 2001 From: walon Date: Mon, 19 May 2025 13:40:13 +0800 Subject: [PATCH] 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 --- .../ConfigurableObfuscationPolicy.cs | 20 +++++++++++++++++++ .../Editor/Utils/PlatformUtil.cs | 18 +++++++++++++++++ .../Editor/Utils/PlatformUtil.cs.meta | 11 ++++++++++ 3 files changed, 49 insertions(+) create mode 100644 Obfuz/Packages/com.code-philosophy.obfuz/Editor/Utils/PlatformUtil.cs create mode 100644 Obfuz/Packages/com.code-philosophy.obfuz/Editor/Utils/PlatformUtil.cs.meta diff --git a/Obfuz/Packages/com.code-philosophy.obfuz/Editor/ObfusPasses/CallObfus/ConfigurableObfuscationPolicy.cs b/Obfuz/Packages/com.code-philosophy.obfuz/Editor/ObfusPasses/CallObfus/ConfigurableObfuscationPolicy.cs index 5105331..37c801b 100644 --- a/Obfuz/Packages/com.code-philosophy.obfuz/Editor/ObfusPasses/CallObfus/ConfigurableObfuscationPolicy.cs +++ b/Obfuz/Packages/com.code-philosophy.obfuz/Editor/ObfusPasses/CallObfus/ConfigurableObfuscationPolicy.cs @@ -360,12 +360,32 @@ namespace Obfuz.ObfusPasses.CallObfus 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) { if (IsInWhiteList(calledMethod)) { 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); if (currentInLoop && rule.obfuscateCallInLoop == false) { diff --git a/Obfuz/Packages/com.code-philosophy.obfuz/Editor/Utils/PlatformUtil.cs b/Obfuz/Packages/com.code-philosophy.obfuz/Editor/Utils/PlatformUtil.cs new file mode 100644 index 0000000..3a38d23 --- /dev/null +++ b/Obfuz/Packages/com.code-philosophy.obfuz/Editor/Utils/PlatformUtil.cs @@ -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; + } + } +} diff --git a/Obfuz/Packages/com.code-philosophy.obfuz/Editor/Utils/PlatformUtil.cs.meta b/Obfuz/Packages/com.code-philosophy.obfuz/Editor/Utils/PlatformUtil.cs.meta new file mode 100644 index 0000000..ec195e6 --- /dev/null +++ b/Obfuz/Packages/com.code-philosophy.obfuz/Editor/Utils/PlatformUtil.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 85d01014c084c56498d292d3b16351d2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: