From a40a45e8aa1725cfc438febf681d6b875ca72f41 Mon Sep 17 00:00:00 2001 From: walon Date: Mon, 19 May 2025 12:55:26 +0800 Subject: [PATCH] fix: fix the bug that ConstEncryptPass::TryObfuscateInstruction didn't obfuscation ldc_i4_xxx because their OperandType is None --- .../ConstEncrypt/ConstEncryptPass.cs | 116 ++++++++---------- 1 file changed, 51 insertions(+), 65 deletions(-) diff --git a/Obfuz/Packages/com.code-philosophy.obfuz/Editor/ObfusPasses/ConstEncrypt/ConstEncryptPass.cs b/Obfuz/Packages/com.code-philosophy.obfuz/Editor/ObfusPasses/ConstEncrypt/ConstEncryptPass.cs index 1356255..b39d304 100644 --- a/Obfuz/Packages/com.code-philosophy.obfuz/Editor/ObfusPasses/ConstEncrypt/ConstEncryptPass.cs +++ b/Obfuz/Packages/com.code-philosophy.obfuz/Editor/ObfusPasses/ConstEncrypt/ConstEncryptPass.cs @@ -49,85 +49,71 @@ namespace Obfuz.ObfusPasses.ConstEncrypt { bool currentInLoop = block.inLoop; ConstCachePolicy constCachePolicy = _dataObfuscatorPolicy.GetMethodConstCachePolicy(method); - switch (inst.OpCode.OperandType) + bool needCache = currentInLoop ? constCachePolicy.cacheConstInLoop : constCachePolicy.cacheConstNotInLoop; + switch (inst.OpCode.Code) { - case OperandType.InlineI: - case OperandType.InlineI8: - case OperandType.ShortInlineI: - case OperandType.ShortInlineR: - case OperandType.InlineR: + case Code.Ldc_I4: + case Code.Ldc_I4_S: + case Code.Ldc_I4_0: + case Code.Ldc_I4_1: + case Code.Ldc_I4_2: + case Code.Ldc_I4_3: + case Code.Ldc_I4_4: + case Code.Ldc_I4_5: + case Code.Ldc_I4_6: + case Code.Ldc_I4_7: + case Code.Ldc_I4_8: + case Code.Ldc_I4_M1: { - bool needCache = currentInLoop ? constCachePolicy.cacheConstInLoop : constCachePolicy.cacheConstNotInLoop; - object operand = inst.Operand; - if (operand is int) + int value = inst.GetLdcI4Value(); + if (_dataObfuscatorPolicy.NeedObfuscateInt(method, currentInLoop, value)) { - int value = (int)operand; - if (_dataObfuscatorPolicy.NeedObfuscateInt(method, currentInLoop, value)) - { - _dataObfuscator.ObfuscateInt(method, needCache, value, outputInstructions); - return true; - } - } - else if (operand is sbyte) - { - int value = (sbyte)operand; - if (_dataObfuscatorPolicy.NeedObfuscateInt(method, currentInLoop, value)) - { - _dataObfuscator.ObfuscateInt(method, needCache, value, outputInstructions); - return true; - } - } - else if (operand is byte) - { - int value = (byte)operand; - if (_dataObfuscatorPolicy.NeedObfuscateInt(method, currentInLoop, value)) - { - _dataObfuscator.ObfuscateInt(method, needCache, value, outputInstructions); - return true; - } - } - else if (operand is long) - { - long value = (long)operand; - if (_dataObfuscatorPolicy.NeedObfuscateLong(method, currentInLoop, value)) - { - _dataObfuscator.ObfuscateLong(method, needCache, value, outputInstructions); - return true; - } - } - else if (operand is float) - { - float value = (float)operand; - if (_dataObfuscatorPolicy.NeedObfuscateFloat(method, currentInLoop, value)) - { - _dataObfuscator.ObfuscateFloat(method, needCache, value, outputInstructions); - return true; - } - } - else if (operand is double) - { - double value = (double)operand; - if (_dataObfuscatorPolicy.NeedObfuscateDouble(method, currentInLoop, value)) - { - _dataObfuscator.ObfuscateDouble(method, needCache, value, outputInstructions); - return true; - } + _dataObfuscator.ObfuscateInt(method, needCache, value, outputInstructions); + return true; } return false; } - case OperandType.InlineString: + case Code.Ldc_I8: + { + long value = (long)inst.Operand; + if (_dataObfuscatorPolicy.NeedObfuscateLong(method, currentInLoop, value)) + { + _dataObfuscator.ObfuscateLong(method, needCache, value, outputInstructions); + return true; + } + return false; + } + case Code.Ldc_R4: + { + float value = (float)inst.Operand; + if (_dataObfuscatorPolicy.NeedObfuscateFloat(method, currentInLoop, value)) + { + _dataObfuscator.ObfuscateFloat(method, needCache, value, outputInstructions); + return true; + } + return false; + } + case Code.Ldc_R8: + { + double value = (double)inst.Operand; + if (_dataObfuscatorPolicy.NeedObfuscateDouble(method, currentInLoop, value)) + { + _dataObfuscator.ObfuscateDouble(method, needCache, value, outputInstructions); + return true; + } + return false; + } + case Code.Ldstr: { - //RuntimeHelpers.InitializeArray string value = (string)inst.Operand; if (_dataObfuscatorPolicy.NeedObfuscateString(method, currentInLoop, value)) { - bool needCache = currentInLoop ? constCachePolicy.cacheStringInLoop : constCachePolicy.cacheStringNotInLoop; _dataObfuscator.ObfuscateString(method, needCache, value, outputInstructions); return true; } return false; } - case OperandType.InlineMethod: + case Code.Call: { //if (((IMethod)inst.Operand).FullName == "System.Void System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(System.Array,System.RuntimeFieldHandle)") //{ @@ -141,7 +127,7 @@ namespace Obfuz.ObfusPasses.ConstEncrypt // { // if (_encryptedRvaFields.Add(ravFieldDef)) // { - + // } // // remove prev ldtoken instruction