fix: fix the bug that ConstEncryptPass::TryObfuscateInstruction didn't obfuscation ldc_i4_xxx because their OperandType is None

before-split
walon 2025-05-19 12:55:26 +08:00
parent 6306a5604a
commit a40a45e8aa
1 changed files with 51 additions and 65 deletions

View File

@ -49,85 +49,71 @@ namespace Obfuz.ObfusPasses.ConstEncrypt
{ {
bool currentInLoop = block.inLoop; bool currentInLoop = block.inLoop;
ConstCachePolicy constCachePolicy = _dataObfuscatorPolicy.GetMethodConstCachePolicy(method); ConstCachePolicy constCachePolicy = _dataObfuscatorPolicy.GetMethodConstCachePolicy(method);
switch (inst.OpCode.OperandType) bool needCache = currentInLoop ? constCachePolicy.cacheConstInLoop : constCachePolicy.cacheConstNotInLoop;
switch (inst.OpCode.Code)
{ {
case OperandType.InlineI: case Code.Ldc_I4:
case OperandType.InlineI8: case Code.Ldc_I4_S:
case OperandType.ShortInlineI: case Code.Ldc_I4_0:
case OperandType.ShortInlineR: case Code.Ldc_I4_1:
case OperandType.InlineR: 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; int value = inst.GetLdcI4Value();
object operand = inst.Operand; if (_dataObfuscatorPolicy.NeedObfuscateInt(method, currentInLoop, value))
if (operand is int)
{ {
int value = (int)operand; _dataObfuscator.ObfuscateInt(method, needCache, value, outputInstructions);
if (_dataObfuscatorPolicy.NeedObfuscateInt(method, currentInLoop, value)) return true;
{
_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;
}
} }
return false; 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; string value = (string)inst.Operand;
if (_dataObfuscatorPolicy.NeedObfuscateString(method, currentInLoop, value)) if (_dataObfuscatorPolicy.NeedObfuscateString(method, currentInLoop, value))
{ {
bool needCache = currentInLoop ? constCachePolicy.cacheStringInLoop : constCachePolicy.cacheStringNotInLoop;
_dataObfuscator.ObfuscateString(method, needCache, value, outputInstructions); _dataObfuscator.ObfuscateString(method, needCache, value, outputInstructions);
return true; return true;
} }
return false; return false;
} }
case OperandType.InlineMethod: case Code.Call:
{ {
//if (((IMethod)inst.Operand).FullName == "System.Void System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(System.Array,System.RuntimeFieldHandle)") //if (((IMethod)inst.Operand).FullName == "System.Void System.Runtime.CompilerServices.RuntimeHelpers::InitializeArray(System.Array,System.RuntimeFieldHandle)")
//{ //{