Example #1
0
        public static Value[] getInitializedArray(int arraySize, MethodDefinition method, ref int newarrIndex, Code stelemOpCode)
        {
            var resultValueArray = new Value[arraySize];

            var emulator = new InstructionEmulator(method.HasThis, false, method.Parameters, method.Body.Variables);
            var theArray = new UnknownValue();
            emulator.push(theArray);

            var instructions = method.Body.Instructions;
            int i;
            for (i = newarrIndex + 1; i < instructions.Count; i++) {
                var instr = instructions[i];
                if (instr.OpCode.FlowControl != FlowControl.Next)
                    break;
                if (instr.OpCode.Code == Code.Newarr)
                    break;

                if (instr.OpCode.Code == stelemOpCode) {
                    var value = emulator.pop();
                    var index = emulator.pop() as Int32Value;
                    var array = emulator.pop();
                    if (ReferenceEquals(array, theArray) && index != null && index.allBitsValid()) {
                        if (0 <= index.value && index.value < resultValueArray.Length)
                            resultValueArray[index.value] = value;
                    }
                }
                else
                    emulator.emulate(instr);
            }
            if (i != newarrIndex + 1)
                i--;
            newarrIndex = i;

            return resultValueArray;
        }
        public static Value[] getInitializedArray(int arraySize, MethodDef method, ref int newarrIndex, Code stelemOpCode)
        {
            var resultValueArray = new Value[arraySize];

            var emulator = new InstructionEmulator(method);
            var theArray = new UnknownValue();
            emulator.push(theArray);

            var instructions = method.Body.Instructions;
            int i;
            for (i = newarrIndex + 1; i < instructions.Count; i++) {
                var instr = instructions[i];
                if (instr.OpCode.FlowControl != FlowControl.Next)
                    break;
                if (instr.OpCode.Code == Code.Newarr)
                    break;
                switch (instr.OpCode.Code) {
                case Code.Newarr:
                case Code.Newobj:
                    goto done;

                case Code.Stloc:
                case Code.Stloc_S:
                case Code.Stloc_0:
                case Code.Stloc_1:
                case Code.Stloc_2:
                case Code.Stloc_3:
                case Code.Starg:
                case Code.Starg_S:
                case Code.Stsfld:
                case Code.Stfld:
                    if (emulator.peek() == theArray && i != newarrIndex + 1 && i != newarrIndex + 2)
                        goto done;
                    break;
                }

                if (instr.OpCode.Code == stelemOpCode) {
                    var value = emulator.pop();
                    var index = emulator.pop() as Int32Value;
                    var array = emulator.pop();
                    if (ReferenceEquals(array, theArray) && index != null && index.allBitsValid()) {
                        if (0 <= index.value && index.value < resultValueArray.Length)
                            resultValueArray[index.value] = value;
                    }
                }
                else
                    emulator.emulate(instr);
            }
            done:
            if (i != newarrIndex + 1)
                i--;
            newarrIndex = i;

            return resultValueArray;
        }