2012-04-10 09:52:18 +08:00
|
|
|
|
/*
|
|
|
|
|
Copyright (C) 2011-2012 de4dot@gmail.com
|
|
|
|
|
|
|
|
|
|
This file is part of de4dot.
|
|
|
|
|
|
|
|
|
|
de4dot is free software: you can redistribute it and/or modify
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
de4dot is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with de4dot. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
2012-11-01 00:09:58 +08:00
|
|
|
|
using dot10.DotNet;
|
|
|
|
|
using dot10.DotNet.Emit;
|
2012-04-10 09:52:18 +08:00
|
|
|
|
using de4dot.blocks;
|
|
|
|
|
|
2012-04-30 07:26:10 +08:00
|
|
|
|
namespace de4dot.code.deobfuscators {
|
2012-04-10 09:52:18 +08:00
|
|
|
|
class ConstantsReader {
|
2012-04-30 07:26:10 +08:00
|
|
|
|
protected IInstructions instructions;
|
2012-11-01 00:09:58 +08:00
|
|
|
|
protected IList<Local> locals;
|
|
|
|
|
protected Dictionary<Local, int> localsValuesInt32 = new Dictionary<Local, int>();
|
|
|
|
|
protected Dictionary<Local, long> localsValuesInt64 = new Dictionary<Local, long>();
|
|
|
|
|
protected Dictionary<Local, double> localsValuesDouble = new Dictionary<Local, double>();
|
2012-07-26 02:43:22 +08:00
|
|
|
|
bool emulateConvInstrs;
|
2012-04-10 09:52:18 +08:00
|
|
|
|
|
2012-12-11 04:42:37 +08:00
|
|
|
|
public IEnumerable<KeyValuePair<Local, int>> Locals32 {
|
|
|
|
|
get { return localsValuesInt32; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<KeyValuePair<Local, long>> Locals64 {
|
|
|
|
|
get { return localsValuesInt64; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<KeyValuePair<Local, double>> LocalsDouble {
|
|
|
|
|
get { return localsValuesDouble; }
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-30 07:26:10 +08:00
|
|
|
|
public interface IInstructions {
|
|
|
|
|
int Count { get; }
|
|
|
|
|
Instruction this[int index] { get; }
|
2012-04-10 09:52:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-04-30 07:26:10 +08:00
|
|
|
|
class ListInstructions : IInstructions {
|
|
|
|
|
IList<Instruction> instrs;
|
|
|
|
|
|
|
|
|
|
public int Count {
|
|
|
|
|
get { return instrs.Count; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Instruction this[int index] {
|
|
|
|
|
get { return instrs[index]; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ListInstructions(IList<Instruction> instrs) {
|
|
|
|
|
this.instrs = instrs;
|
|
|
|
|
}
|
2012-04-10 09:52:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-04-30 07:26:10 +08:00
|
|
|
|
class ListInstrs : IInstructions {
|
|
|
|
|
IList<Instr> instrs;
|
|
|
|
|
|
|
|
|
|
public int Count {
|
|
|
|
|
get { return instrs.Count; }
|
2012-04-10 09:52:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-04-30 07:26:10 +08:00
|
|
|
|
public Instruction this[int index] {
|
|
|
|
|
get { return instrs[index].Instruction; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ListInstrs(IList<Instr> instrs) {
|
|
|
|
|
this.instrs = instrs;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-26 02:43:22 +08:00
|
|
|
|
public bool EmulateConvInstructions {
|
|
|
|
|
get { return emulateConvInstrs; }
|
|
|
|
|
set { emulateConvInstrs = value; }
|
2012-05-02 16:47:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-26 02:43:22 +08:00
|
|
|
|
ConstantsReader(IInstructions instructions)
|
|
|
|
|
: this(instructions, true) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ConstantsReader(IInstructions instructions, bool emulateConvInstrs) {
|
|
|
|
|
this.instructions = instructions;
|
|
|
|
|
this.emulateConvInstrs = emulateConvInstrs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ConstantsReader(IList<Instruction> instrs)
|
|
|
|
|
: this(new ListInstructions(instrs)) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ConstantsReader(IList<Instruction> instrs, bool emulateConvInstrs)
|
|
|
|
|
: this(new ListInstructions(instrs), emulateConvInstrs) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ConstantsReader(IList<Instr> instrs)
|
|
|
|
|
: this(new ListInstrs(instrs)) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ConstantsReader(IList<Instr> instrs, bool emulateConvInstrs)
|
|
|
|
|
: this(new ListInstrs(instrs), emulateConvInstrs) {
|
2012-05-02 16:47:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 00:09:58 +08:00
|
|
|
|
public ConstantsReader(MethodDef method)
|
2012-11-06 22:58:55 +08:00
|
|
|
|
: this(method.Body.Instructions) {
|
2012-11-20 00:58:34 +08:00
|
|
|
|
this.locals = method.Body.Variables;
|
2012-04-30 07:26:10 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 00:09:58 +08:00
|
|
|
|
public ConstantsReader(IList<Instr> instrs, IList<Local> locals)
|
2012-05-02 16:47:21 +08:00
|
|
|
|
: this(instrs) {
|
2012-04-30 07:26:10 +08:00
|
|
|
|
this.locals = locals;
|
2012-04-10 09:52:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 00:09:58 +08:00
|
|
|
|
public void setConstantInt32(Local local, int value) {
|
2012-07-31 01:07:25 +08:00
|
|
|
|
localsValuesInt32[local] = value;
|
2012-07-29 20:21:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 00:09:58 +08:00
|
|
|
|
public void setConstantInt32(Local local, uint value) {
|
2012-07-31 01:07:25 +08:00
|
|
|
|
setConstantInt32(local, (int)value);
|
2012-07-29 20:21:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 00:09:58 +08:00
|
|
|
|
public void setConstantInt64(Local local, long value) {
|
2012-07-31 01:07:25 +08:00
|
|
|
|
localsValuesInt64[local] = value;
|
2012-07-29 20:21:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 00:09:58 +08:00
|
|
|
|
public void setConstantInt64(Local local, ulong value) {
|
2012-07-31 01:07:25 +08:00
|
|
|
|
setConstantInt64(local, (long)value);
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-01 00:09:58 +08:00
|
|
|
|
public void setConstantDouble(Local local, double value) {
|
2012-07-31 01:07:25 +08:00
|
|
|
|
localsValuesDouble[local] = value;
|
2012-07-29 20:21:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-04-10 09:52:18 +08:00
|
|
|
|
public bool getNextInt32(ref int index, out int val) {
|
|
|
|
|
for (; index < instructions.Count; index++) {
|
|
|
|
|
var instr = instructions[index];
|
2012-07-31 01:07:25 +08:00
|
|
|
|
if (!isLoadConstantInt32(instr))
|
2012-04-10 09:52:18 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
return getInt32(ref index, out val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-31 01:07:25 +08:00
|
|
|
|
public bool isLoadConstantInt32(Instruction instr) {
|
2012-11-01 00:09:58 +08:00
|
|
|
|
if (instr.IsLdcI4())
|
2012-04-10 09:52:18 +08:00
|
|
|
|
return true;
|
2012-11-01 00:09:58 +08:00
|
|
|
|
if (instr.IsLdloc()) {
|
2012-05-02 16:47:21 +08:00
|
|
|
|
int tmp;
|
2012-07-31 01:07:25 +08:00
|
|
|
|
return getLocalConstantInt32(instr, out tmp);
|
2012-05-02 16:47:21 +08:00
|
|
|
|
}
|
2012-11-01 00:09:58 +08:00
|
|
|
|
if (instr.IsLdarg()) {
|
2012-05-02 16:47:21 +08:00
|
|
|
|
int tmp;
|
2012-07-31 01:07:25 +08:00
|
|
|
|
return getArgConstantInt32(instr, out tmp);
|
2012-05-02 16:47:21 +08:00
|
|
|
|
}
|
|
|
|
|
return false;
|
2012-04-10 09:52:18 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-31 01:07:25 +08:00
|
|
|
|
public bool isLoadConstantInt64(Instruction instr) {
|
2012-07-26 03:06:35 +08:00
|
|
|
|
if (instr.OpCode.Code == Code.Ldc_I8)
|
|
|
|
|
return true;
|
2012-11-01 00:09:58 +08:00
|
|
|
|
if (instr.IsLdloc()) {
|
2012-07-29 20:21:13 +08:00
|
|
|
|
long tmp;
|
2012-07-31 01:07:25 +08:00
|
|
|
|
return getLocalConstantInt64(instr, out tmp);
|
2012-07-26 03:06:35 +08:00
|
|
|
|
}
|
2012-11-01 00:09:58 +08:00
|
|
|
|
if (instr.IsLdarg()) {
|
2012-07-29 20:21:13 +08:00
|
|
|
|
long tmp;
|
2012-07-31 01:07:25 +08:00
|
|
|
|
return getArgConstantInt64(instr, out tmp);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool isLoadConstantDouble(Instruction instr) {
|
|
|
|
|
if (instr.OpCode.Code == Code.Ldc_R8)
|
|
|
|
|
return true;
|
2012-11-01 00:09:58 +08:00
|
|
|
|
if (instr.IsLdloc()) {
|
2012-07-31 01:07:25 +08:00
|
|
|
|
double tmp;
|
|
|
|
|
return getLocalConstantDouble(instr, out tmp);
|
|
|
|
|
}
|
2012-11-01 00:09:58 +08:00
|
|
|
|
if (instr.IsLdarg()) {
|
2012-07-31 01:07:25 +08:00
|
|
|
|
double tmp;
|
|
|
|
|
return getArgConstantDouble(instr, out tmp);
|
2012-07-26 03:06:35 +08:00
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-10 09:52:18 +08:00
|
|
|
|
public bool getInt16(ref int index, out short val) {
|
|
|
|
|
int tmp;
|
|
|
|
|
if (!getInt32(ref index, out tmp)) {
|
|
|
|
|
val = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
val = (short)tmp;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-31 10:40:45 +08:00
|
|
|
|
protected struct ConstantInfo<T> {
|
2012-05-02 16:47:21 +08:00
|
|
|
|
public int index;
|
2012-07-26 02:45:54 +08:00
|
|
|
|
public T constant;
|
|
|
|
|
public ConstantInfo(int index, T constant) {
|
2012-05-02 16:47:21 +08:00
|
|
|
|
this.index = index;
|
|
|
|
|
this.constant = constant;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-31 10:40:45 +08:00
|
|
|
|
protected virtual bool processInstructionInt32(ref int index, Stack<ConstantInfo<int>> stack) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual bool processInstructionInt64(ref int index, Stack<ConstantInfo<long>> stack) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual bool processInstructionDouble(ref int index, Stack<ConstantInfo<double>> stack) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-10 09:52:18 +08:00
|
|
|
|
public bool getInt32(ref int index, out int val) {
|
|
|
|
|
val = 0;
|
|
|
|
|
if (index >= instructions.Count)
|
|
|
|
|
return false;
|
|
|
|
|
|
2012-07-26 02:45:54 +08:00
|
|
|
|
var stack = new Stack<ConstantInfo<int>>();
|
2012-04-10 09:52:18 +08:00
|
|
|
|
|
|
|
|
|
int op1;
|
2012-07-26 02:45:54 +08:00
|
|
|
|
ConstantInfo<int> info1, info2;
|
2012-04-10 09:52:18 +08:00
|
|
|
|
for (; index < instructions.Count; index++) {
|
2012-07-31 10:40:45 +08:00
|
|
|
|
if (processInstructionInt32(ref index, stack)) {
|
|
|
|
|
index--;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2012-04-10 09:52:18 +08:00
|
|
|
|
var instr = instructions[index];
|
|
|
|
|
switch (instr.OpCode.Code) {
|
|
|
|
|
case Code.Conv_I1:
|
2012-07-26 02:43:22 +08:00
|
|
|
|
if (!emulateConvInstrs || stack.Count < 1)
|
2012-04-10 09:52:18 +08:00
|
|
|
|
goto done;
|
2012-07-26 02:45:54 +08:00
|
|
|
|
stack.Push(new ConstantInfo<int>(index, (sbyte)stack.Pop().constant));
|
2012-04-10 09:52:18 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Conv_U1:
|
2012-07-26 02:43:22 +08:00
|
|
|
|
if (!emulateConvInstrs || stack.Count < 1)
|
2012-04-10 09:52:18 +08:00
|
|
|
|
goto done;
|
2012-07-26 02:45:54 +08:00
|
|
|
|
stack.Push(new ConstantInfo<int>(index, (byte)stack.Pop().constant));
|
2012-04-10 09:52:18 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Conv_I2:
|
2012-07-26 02:43:22 +08:00
|
|
|
|
if (!emulateConvInstrs || stack.Count < 1)
|
2012-04-10 09:52:18 +08:00
|
|
|
|
goto done;
|
2012-07-26 02:45:54 +08:00
|
|
|
|
stack.Push(new ConstantInfo<int>(index, (short)stack.Pop().constant));
|
2012-04-10 09:52:18 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Conv_U2:
|
2012-07-26 02:43:22 +08:00
|
|
|
|
if (!emulateConvInstrs || stack.Count < 1)
|
2012-04-10 09:52:18 +08:00
|
|
|
|
goto done;
|
2012-07-26 02:45:54 +08:00
|
|
|
|
stack.Push(new ConstantInfo<int>(index, (ushort)stack.Pop().constant));
|
2012-04-10 09:52:18 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Conv_I4:
|
|
|
|
|
case Code.Conv_U4:
|
2012-07-26 03:06:35 +08:00
|
|
|
|
if (!emulateConvInstrs || stack.Count < 1)
|
2012-07-26 02:43:22 +08:00
|
|
|
|
goto done;
|
2012-07-26 02:45:54 +08:00
|
|
|
|
stack.Push(new ConstantInfo<int>(index, stack.Pop().constant));
|
2012-04-10 09:52:18 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Not:
|
2012-07-26 03:06:35 +08:00
|
|
|
|
if (stack.Count < 1)
|
|
|
|
|
goto done;
|
2012-07-26 02:45:54 +08:00
|
|
|
|
stack.Push(new ConstantInfo<int>(index, ~stack.Pop().constant));
|
2012-04-10 09:52:18 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Neg:
|
2012-07-26 03:06:35 +08:00
|
|
|
|
if (stack.Count < 1)
|
|
|
|
|
goto done;
|
2012-07-26 02:45:54 +08:00
|
|
|
|
stack.Push(new ConstantInfo<int>(index, -stack.Pop().constant));
|
2012-04-10 09:52:18 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Ldloc:
|
|
|
|
|
case Code.Ldloc_S:
|
|
|
|
|
case Code.Ldloc_0:
|
|
|
|
|
case Code.Ldloc_1:
|
|
|
|
|
case Code.Ldloc_2:
|
|
|
|
|
case Code.Ldloc_3:
|
2012-07-31 01:07:25 +08:00
|
|
|
|
if (!getLocalConstantInt32(instr, out op1))
|
2012-04-10 09:52:18 +08:00
|
|
|
|
goto done;
|
2012-07-26 02:45:54 +08:00
|
|
|
|
stack.Push(new ConstantInfo<int>(index, op1));
|
2012-05-02 16:47:21 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Ldarg:
|
|
|
|
|
case Code.Ldarg_S:
|
|
|
|
|
case Code.Ldarg_0:
|
|
|
|
|
case Code.Ldarg_1:
|
|
|
|
|
case Code.Ldarg_2:
|
|
|
|
|
case Code.Ldarg_3:
|
2012-07-31 01:07:25 +08:00
|
|
|
|
if (!getArgConstantInt32(instr, out op1))
|
2012-05-02 16:47:21 +08:00
|
|
|
|
goto done;
|
2012-07-26 02:45:54 +08:00
|
|
|
|
stack.Push(new ConstantInfo<int>(index, op1));
|
2012-04-10 09:52:18 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
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:
|
2012-11-01 00:09:58 +08:00
|
|
|
|
stack.Push(new ConstantInfo<int>(index, instr.GetLdcI4Value()));
|
2012-04-10 09:52:18 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Add:
|
|
|
|
|
if (stack.Count < 2)
|
|
|
|
|
goto done;
|
2012-05-02 16:47:21 +08:00
|
|
|
|
info2 = stack.Pop();
|
|
|
|
|
info1 = stack.Pop();
|
2012-07-26 02:45:54 +08:00
|
|
|
|
stack.Push(new ConstantInfo<int>(index, info1.constant + info2.constant));
|
2012-04-10 09:52:18 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Sub:
|
|
|
|
|
if (stack.Count < 2)
|
|
|
|
|
goto done;
|
2012-05-02 16:47:21 +08:00
|
|
|
|
info2 = stack.Pop();
|
|
|
|
|
info1 = stack.Pop();
|
2012-07-26 02:45:54 +08:00
|
|
|
|
stack.Push(new ConstantInfo<int>(index, info1.constant - info2.constant));
|
2012-04-10 09:52:18 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Xor:
|
|
|
|
|
if (stack.Count < 2)
|
|
|
|
|
goto done;
|
2012-05-02 16:47:21 +08:00
|
|
|
|
info2 = stack.Pop();
|
|
|
|
|
info1 = stack.Pop();
|
2012-07-26 02:45:54 +08:00
|
|
|
|
stack.Push(new ConstantInfo<int>(index, info1.constant ^ info2.constant));
|
2012-04-10 09:52:18 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Or:
|
|
|
|
|
if (stack.Count < 2)
|
|
|
|
|
goto done;
|
2012-05-02 16:47:21 +08:00
|
|
|
|
info2 = stack.Pop();
|
|
|
|
|
info1 = stack.Pop();
|
2012-07-26 02:45:54 +08:00
|
|
|
|
stack.Push(new ConstantInfo<int>(index, info1.constant | info2.constant));
|
2012-04-10 09:52:18 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.And:
|
|
|
|
|
if (stack.Count < 2)
|
|
|
|
|
goto done;
|
2012-05-02 16:47:21 +08:00
|
|
|
|
info2 = stack.Pop();
|
|
|
|
|
info1 = stack.Pop();
|
2012-07-26 02:45:54 +08:00
|
|
|
|
stack.Push(new ConstantInfo<int>(index, info1.constant & info2.constant));
|
2012-05-02 16:47:21 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Mul:
|
|
|
|
|
if (stack.Count < 2)
|
|
|
|
|
goto done;
|
|
|
|
|
info2 = stack.Pop();
|
|
|
|
|
info1 = stack.Pop();
|
2012-07-26 02:45:54 +08:00
|
|
|
|
stack.Push(new ConstantInfo<int>(index, info1.constant * info2.constant));
|
2012-05-02 16:47:21 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Div:
|
|
|
|
|
if (stack.Count < 2)
|
|
|
|
|
goto done;
|
|
|
|
|
info2 = stack.Pop();
|
2012-07-31 18:43:23 +08:00
|
|
|
|
if (info2.constant == 0)
|
|
|
|
|
goto done;
|
2012-05-02 16:47:21 +08:00
|
|
|
|
info1 = stack.Pop();
|
2012-07-26 02:45:54 +08:00
|
|
|
|
stack.Push(new ConstantInfo<int>(index, info1.constant / info2.constant));
|
2012-05-02 16:47:21 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Div_Un:
|
|
|
|
|
if (stack.Count < 2)
|
|
|
|
|
goto done;
|
|
|
|
|
info2 = stack.Pop();
|
2012-07-31 18:43:23 +08:00
|
|
|
|
if (info2.constant == 0)
|
|
|
|
|
goto done;
|
2012-05-02 16:47:21 +08:00
|
|
|
|
info1 = stack.Pop();
|
2012-07-26 02:45:54 +08:00
|
|
|
|
stack.Push(new ConstantInfo<int>(index, (int)((uint)info1.constant / (uint)info2.constant)));
|
2012-04-10 09:52:18 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
goto done;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
done:
|
|
|
|
|
if (stack.Count == 0)
|
|
|
|
|
return false;
|
|
|
|
|
while (stack.Count > 1)
|
|
|
|
|
stack.Pop();
|
2012-05-02 16:47:21 +08:00
|
|
|
|
info1 = stack.Pop();
|
|
|
|
|
index = info1.index + 1;
|
|
|
|
|
val = info1.constant;
|
2012-04-10 09:52:18 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-26 03:06:35 +08:00
|
|
|
|
public bool getInt64(ref int index, out long val) {
|
|
|
|
|
val = 0;
|
|
|
|
|
if (index >= instructions.Count)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var stack = new Stack<ConstantInfo<long>>();
|
|
|
|
|
|
2012-07-29 20:21:13 +08:00
|
|
|
|
long op1;
|
2012-07-26 03:06:35 +08:00
|
|
|
|
ConstantInfo<long> info1, info2;
|
|
|
|
|
for (; index < instructions.Count; index++) {
|
2012-07-31 10:40:45 +08:00
|
|
|
|
if (processInstructionInt64(ref index, stack)) {
|
|
|
|
|
index--;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2012-07-26 03:06:35 +08:00
|
|
|
|
var instr = instructions[index];
|
|
|
|
|
switch (instr.OpCode.Code) {
|
|
|
|
|
case Code.Conv_I1:
|
|
|
|
|
if (!emulateConvInstrs || stack.Count < 1)
|
|
|
|
|
goto done;
|
|
|
|
|
stack.Push(new ConstantInfo<long>(index, (sbyte)stack.Pop().constant));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Conv_U1:
|
|
|
|
|
if (!emulateConvInstrs || stack.Count < 1)
|
|
|
|
|
goto done;
|
|
|
|
|
stack.Push(new ConstantInfo<long>(index, (byte)stack.Pop().constant));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Conv_I2:
|
|
|
|
|
if (!emulateConvInstrs || stack.Count < 1)
|
|
|
|
|
goto done;
|
|
|
|
|
stack.Push(new ConstantInfo<long>(index, (short)stack.Pop().constant));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Conv_U2:
|
|
|
|
|
if (!emulateConvInstrs || stack.Count < 1)
|
|
|
|
|
goto done;
|
|
|
|
|
stack.Push(new ConstantInfo<long>(index, (ushort)stack.Pop().constant));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Conv_I4:
|
|
|
|
|
if (!emulateConvInstrs || stack.Count < 1)
|
|
|
|
|
goto done;
|
|
|
|
|
stack.Push(new ConstantInfo<long>(index, (int)stack.Pop().constant));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Conv_U4:
|
|
|
|
|
if (!emulateConvInstrs || stack.Count < 1)
|
|
|
|
|
goto done;
|
|
|
|
|
stack.Push(new ConstantInfo<long>(index, (uint)stack.Pop().constant));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Conv_I8:
|
|
|
|
|
case Code.Conv_U8:
|
|
|
|
|
if (!emulateConvInstrs || stack.Count < 1)
|
|
|
|
|
goto done;
|
|
|
|
|
stack.Push(new ConstantInfo<long>(index, stack.Pop().constant));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Not:
|
|
|
|
|
if (stack.Count < 1)
|
|
|
|
|
goto done;
|
|
|
|
|
stack.Push(new ConstantInfo<long>(index, ~stack.Pop().constant));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Neg:
|
|
|
|
|
if (stack.Count < 1)
|
|
|
|
|
goto done;
|
|
|
|
|
stack.Push(new ConstantInfo<long>(index, -stack.Pop().constant));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Ldloc:
|
|
|
|
|
case Code.Ldloc_S:
|
|
|
|
|
case Code.Ldloc_0:
|
|
|
|
|
case Code.Ldloc_1:
|
|
|
|
|
case Code.Ldloc_2:
|
|
|
|
|
case Code.Ldloc_3:
|
2012-07-31 01:07:25 +08:00
|
|
|
|
if (!getLocalConstantInt64(instr, out op1))
|
2012-07-26 03:06:35 +08:00
|
|
|
|
goto done;
|
|
|
|
|
stack.Push(new ConstantInfo<long>(index, op1));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Ldarg:
|
|
|
|
|
case Code.Ldarg_S:
|
|
|
|
|
case Code.Ldarg_0:
|
|
|
|
|
case Code.Ldarg_1:
|
|
|
|
|
case Code.Ldarg_2:
|
|
|
|
|
case Code.Ldarg_3:
|
2012-07-31 01:07:25 +08:00
|
|
|
|
if (!getArgConstantInt64(instr, out op1))
|
2012-07-26 03:06:35 +08:00
|
|
|
|
goto done;
|
|
|
|
|
stack.Push(new ConstantInfo<long>(index, op1));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Ldc_I8:
|
|
|
|
|
stack.Push(new ConstantInfo<long>(index, (long)instr.Operand));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Add:
|
|
|
|
|
if (stack.Count < 2)
|
|
|
|
|
goto done;
|
|
|
|
|
info2 = stack.Pop();
|
|
|
|
|
info1 = stack.Pop();
|
|
|
|
|
stack.Push(new ConstantInfo<long>(index, info1.constant + info2.constant));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Sub:
|
|
|
|
|
if (stack.Count < 2)
|
|
|
|
|
goto done;
|
|
|
|
|
info2 = stack.Pop();
|
|
|
|
|
info1 = stack.Pop();
|
|
|
|
|
stack.Push(new ConstantInfo<long>(index, info1.constant - info2.constant));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Xor:
|
|
|
|
|
if (stack.Count < 2)
|
|
|
|
|
goto done;
|
|
|
|
|
info2 = stack.Pop();
|
|
|
|
|
info1 = stack.Pop();
|
|
|
|
|
stack.Push(new ConstantInfo<long>(index, info1.constant ^ info2.constant));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Or:
|
|
|
|
|
if (stack.Count < 2)
|
|
|
|
|
goto done;
|
|
|
|
|
info2 = stack.Pop();
|
|
|
|
|
info1 = stack.Pop();
|
|
|
|
|
stack.Push(new ConstantInfo<long>(index, info1.constant | info2.constant));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.And:
|
|
|
|
|
if (stack.Count < 2)
|
|
|
|
|
goto done;
|
|
|
|
|
info2 = stack.Pop();
|
|
|
|
|
info1 = stack.Pop();
|
|
|
|
|
stack.Push(new ConstantInfo<long>(index, info1.constant & info2.constant));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Mul:
|
|
|
|
|
if (stack.Count < 2)
|
|
|
|
|
goto done;
|
|
|
|
|
info2 = stack.Pop();
|
|
|
|
|
info1 = stack.Pop();
|
|
|
|
|
stack.Push(new ConstantInfo<long>(index, info1.constant * info2.constant));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Div:
|
|
|
|
|
if (stack.Count < 2)
|
|
|
|
|
goto done;
|
|
|
|
|
info2 = stack.Pop();
|
2012-07-31 18:43:23 +08:00
|
|
|
|
if (info2.constant == 0)
|
|
|
|
|
goto done;
|
2012-07-26 03:06:35 +08:00
|
|
|
|
info1 = stack.Pop();
|
|
|
|
|
stack.Push(new ConstantInfo<long>(index, info1.constant / info2.constant));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Div_Un:
|
|
|
|
|
if (stack.Count < 2)
|
|
|
|
|
goto done;
|
|
|
|
|
info2 = stack.Pop();
|
2012-07-31 18:43:23 +08:00
|
|
|
|
if (info2.constant == 0)
|
|
|
|
|
goto done;
|
2012-07-26 03:06:35 +08:00
|
|
|
|
info1 = stack.Pop();
|
|
|
|
|
stack.Push(new ConstantInfo<long>(index, (int)((uint)info1.constant / (uint)info2.constant)));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
goto done;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
done:
|
|
|
|
|
if (stack.Count == 0)
|
|
|
|
|
return false;
|
|
|
|
|
while (stack.Count > 1)
|
|
|
|
|
stack.Pop();
|
|
|
|
|
info1 = stack.Pop();
|
|
|
|
|
index = info1.index + 1;
|
|
|
|
|
val = info1.constant;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-31 01:07:25 +08:00
|
|
|
|
public bool getDouble(ref int index, out double val) {
|
|
|
|
|
val = 0;
|
|
|
|
|
if (index >= instructions.Count)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
var stack = new Stack<ConstantInfo<double>>();
|
|
|
|
|
|
|
|
|
|
double op1;
|
|
|
|
|
ConstantInfo<double> info1, info2;
|
|
|
|
|
for (; index < instructions.Count; index++) {
|
2012-07-31 10:40:45 +08:00
|
|
|
|
if (processInstructionDouble(ref index, stack)) {
|
|
|
|
|
index--;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2012-07-31 01:07:25 +08:00
|
|
|
|
var instr = instructions[index];
|
|
|
|
|
switch (instr.OpCode.Code) {
|
|
|
|
|
case Code.Conv_R4:
|
|
|
|
|
if (!emulateConvInstrs || stack.Count < 1)
|
|
|
|
|
goto done;
|
|
|
|
|
stack.Push(new ConstantInfo<double>(index, (float)stack.Pop().constant));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Conv_R8:
|
|
|
|
|
if (!emulateConvInstrs || stack.Count < 1)
|
|
|
|
|
goto done;
|
|
|
|
|
stack.Push(new ConstantInfo<double>(index, stack.Pop().constant));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Neg:
|
|
|
|
|
if (stack.Count < 1)
|
|
|
|
|
goto done;
|
|
|
|
|
stack.Push(new ConstantInfo<double>(index, -stack.Pop().constant));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Ldloc:
|
|
|
|
|
case Code.Ldloc_S:
|
|
|
|
|
case Code.Ldloc_0:
|
|
|
|
|
case Code.Ldloc_1:
|
|
|
|
|
case Code.Ldloc_2:
|
|
|
|
|
case Code.Ldloc_3:
|
|
|
|
|
if (!getLocalConstantDouble(instr, out op1))
|
|
|
|
|
goto done;
|
|
|
|
|
stack.Push(new ConstantInfo<double>(index, op1));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Ldarg:
|
|
|
|
|
case Code.Ldarg_S:
|
|
|
|
|
case Code.Ldarg_0:
|
|
|
|
|
case Code.Ldarg_1:
|
|
|
|
|
case Code.Ldarg_2:
|
|
|
|
|
case Code.Ldarg_3:
|
|
|
|
|
if (!getArgConstantDouble(instr, out op1))
|
|
|
|
|
goto done;
|
|
|
|
|
stack.Push(new ConstantInfo<double>(index, op1));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Ldc_R4:
|
|
|
|
|
stack.Push(new ConstantInfo<double>(index, (float)instr.Operand));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Ldc_R8:
|
|
|
|
|
stack.Push(new ConstantInfo<double>(index, (double)instr.Operand));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Add:
|
|
|
|
|
if (stack.Count < 2)
|
|
|
|
|
goto done;
|
|
|
|
|
info2 = stack.Pop();
|
|
|
|
|
info1 = stack.Pop();
|
|
|
|
|
stack.Push(new ConstantInfo<double>(index, info1.constant + info2.constant));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Sub:
|
|
|
|
|
if (stack.Count < 2)
|
|
|
|
|
goto done;
|
|
|
|
|
info2 = stack.Pop();
|
|
|
|
|
info1 = stack.Pop();
|
|
|
|
|
stack.Push(new ConstantInfo<double>(index, info1.constant - info2.constant));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Mul:
|
|
|
|
|
if (stack.Count < 2)
|
|
|
|
|
goto done;
|
|
|
|
|
info2 = stack.Pop();
|
|
|
|
|
info1 = stack.Pop();
|
|
|
|
|
stack.Push(new ConstantInfo<double>(index, info1.constant * info2.constant));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Div:
|
|
|
|
|
if (stack.Count < 2)
|
|
|
|
|
goto done;
|
|
|
|
|
info2 = stack.Pop();
|
|
|
|
|
info1 = stack.Pop();
|
|
|
|
|
stack.Push(new ConstantInfo<double>(index, info1.constant / info2.constant));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case Code.Div_Un:
|
|
|
|
|
if (stack.Count < 2)
|
|
|
|
|
goto done;
|
|
|
|
|
info2 = stack.Pop();
|
|
|
|
|
info1 = stack.Pop();
|
|
|
|
|
stack.Push(new ConstantInfo<double>(index, (int)((uint)info1.constant / (uint)info2.constant)));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
goto done;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
done:
|
|
|
|
|
if (stack.Count == 0)
|
|
|
|
|
return false;
|
|
|
|
|
while (stack.Count > 1)
|
|
|
|
|
stack.Pop();
|
|
|
|
|
info1 = stack.Pop();
|
|
|
|
|
index = info1.index + 1;
|
|
|
|
|
val = info1.constant;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual bool getLocalConstantInt32(Instruction instr, out int value) {
|
2012-04-10 09:52:18 +08:00
|
|
|
|
value = 0;
|
2012-05-02 16:47:21 +08:00
|
|
|
|
if (locals == null)
|
|
|
|
|
return false;
|
2012-11-01 00:09:58 +08:00
|
|
|
|
var local = instr.GetLocal(locals);
|
2012-04-10 09:52:18 +08:00
|
|
|
|
if (local == null)
|
|
|
|
|
return false;
|
2012-11-01 00:09:58 +08:00
|
|
|
|
if (local.Type.ElementType != ElementType.I4 && local.Type.ElementType != ElementType.U4)
|
2012-04-10 09:52:18 +08:00
|
|
|
|
return false;
|
2012-07-31 01:07:25 +08:00
|
|
|
|
return localsValuesInt32.TryGetValue(local, out value);
|
2012-04-10 09:52:18 +08:00
|
|
|
|
}
|
2012-05-02 16:47:21 +08:00
|
|
|
|
|
2012-07-31 01:07:25 +08:00
|
|
|
|
protected virtual bool getArgConstantInt32(Instruction instr, out int value) {
|
2012-05-02 16:47:21 +08:00
|
|
|
|
value = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2012-07-26 03:06:35 +08:00
|
|
|
|
|
2012-07-31 01:07:25 +08:00
|
|
|
|
protected virtual bool getLocalConstantInt64(Instruction instr, out long value) {
|
2012-07-26 03:06:35 +08:00
|
|
|
|
value = 0;
|
|
|
|
|
if (locals == null)
|
|
|
|
|
return false;
|
2012-11-01 00:09:58 +08:00
|
|
|
|
var local = instr.GetLocal(locals);
|
2012-07-26 03:06:35 +08:00
|
|
|
|
if (local == null)
|
|
|
|
|
return false;
|
2012-11-01 00:09:58 +08:00
|
|
|
|
if (local.Type.ElementType != ElementType.I8 && local.Type.ElementType != ElementType.U8)
|
2012-07-26 03:06:35 +08:00
|
|
|
|
return false;
|
2012-07-31 01:07:25 +08:00
|
|
|
|
return localsValuesInt64.TryGetValue(local, out value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual bool getArgConstantInt64(Instruction instr, out long value) {
|
|
|
|
|
value = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected virtual bool getLocalConstantDouble(Instruction instr, out double value) {
|
|
|
|
|
value = 0;
|
|
|
|
|
if (locals == null)
|
|
|
|
|
return false;
|
2012-11-01 00:09:58 +08:00
|
|
|
|
var local = instr.GetLocal(locals);
|
2012-07-31 01:07:25 +08:00
|
|
|
|
if (local == null)
|
|
|
|
|
return false;
|
2012-11-01 00:09:58 +08:00
|
|
|
|
if (local.Type.ElementType != ElementType.R8)
|
2012-07-31 01:07:25 +08:00
|
|
|
|
return false;
|
|
|
|
|
return localsValuesDouble.TryGetValue(local, out value);
|
2012-07-26 03:06:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-31 01:07:25 +08:00
|
|
|
|
protected virtual bool getArgConstantDouble(Instruction instr, out double value) {
|
2012-07-26 03:06:35 +08:00
|
|
|
|
value = 0;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2012-04-10 09:52:18 +08:00
|
|
|
|
}
|
|
|
|
|
}
|