public HandlerState(HandlerMethod handlerMethod, IList <Block> blocks, int blockIndex, int instructionIndex)
 {
     this.HandlerMethod    = handlerMethod;
     this.Blocks           = blocks;
     this.BlockIndex       = blockIndex;
     this.InstructionIndex = instructionIndex;
 }
 public HandlerState(HandlerMethod handlerMethod, int blockIndex, int instructionIndex)
 {
     this.HandlerMethod    = handlerMethod;
     this.Blocks           = handlerMethod.Blocks.MethodBlocks.GetAllBlocks();
     this.BlockIndex       = blockIndex;
     this.InstructionIndex = instructionIndex;
 }
示例#3
0
        List <CompositeOpCodeHandler> CreateCompositeOpCodeHandlers(CsvmInfo csvmInfo, List <TypeDef> handlers)
        {
            var list = new List <CompositeOpCodeHandler>(handlers.Count);

            foreach (var handler in handlers)
            {
                var execHandler = new HandlerMethod(GetExecMethod(handler));
                list.Add(new CompositeOpCodeHandler(handler, execHandler));
            }

            return(list);
        }
示例#4
0
 public OpCodeHandler(OpCodeHandlerInfo opCodeHandlerInfo, TypeDef handlerType, HandlerMethod execMethod)
 {
     this.OpCodeHandlerInfo = opCodeHandlerInfo;
     this.HandlerType       = handlerType;
     this.ExecMethod        = execMethod;
 }
 public CompositeOpCodeHandler(TypeDef handlerType, HandlerMethod execMethod)
 {
     this.HandlerType        = handlerType;
     this.ExecMethod         = execMethod;
     this.OpCodeHandlerInfos = new List <OpCodeHandlerInfo>();
 }
			public HandlerState(HandlerMethod handlerMethod, IList<Block> blocks, int blockIndex, int instructionIndex) {
				this.HandlerMethod = handlerMethod;
				this.Blocks = blocks;
				this.BlockIndex = blockIndex;
				this.InstructionIndex = instructionIndex;
			}
			public HandlerState(HandlerMethod handlerMethod, int blockIndex, int instructionIndex) {
				this.HandlerMethod = handlerMethod;
				this.Blocks = handlerMethod.Blocks.MethodBlocks.GetAllBlocks();
				this.BlockIndex = blockIndex;
				this.InstructionIndex = instructionIndex;
			}
		static HandlerState CreateHandlerState(HandlerMethod handler, IList<Block> blocks, Block target) {
			return new HandlerState(handler, blocks.IndexOf(target), 0);
		}
		bool Matches(HandlerMethod handler, ref FindHandlerState findState) {
			HandlerState? nextState = null;
			stack.Clear();
			stack.Push(new MatchState(new HandlerState(handler, 0, 0), findState.CompositeState));
			while (stack.Count > 0) {
				var matchState = stack.Pop();

				if (matchState.CompositeState.InstructionIndex == 0) {
					if (findState.VisitedCompositeBlocks.ContainsKey(matchState.CompositeState.BlockIndex))
						continue;
					findState.VisitedCompositeBlocks[matchState.CompositeState.BlockIndex] = true;
				}
				else {
					if (!findState.VisitedCompositeBlocks.ContainsKey(matchState.CompositeState.BlockIndex))
						throw new ApplicationException("Block hasn't been visited");
				}

				if (!Compare(ref matchState.OtherState, ref matchState.CompositeState))
					return false;

				var hblock = matchState.OtherState.Blocks[matchState.OtherState.BlockIndex];
				var hinstrs = hblock.Instructions;
				int hi = matchState.OtherState.InstructionIndex;
				var cblock = matchState.CompositeState.Blocks[matchState.CompositeState.BlockIndex];
				var cinstrs = cblock.Instructions;
				int ci = matchState.CompositeState.InstructionIndex;
				if (hi < hinstrs.Count)
					return false;

				if (ci < cinstrs.Count) {
					if (hblock.CountTargets() != 0)
						return false;
					if (hblock.LastInstr.OpCode.Code == Code.Ret) {
						if (nextState != null)
							return false;
						nextState = matchState.CompositeState;
					}
				}
				else {
					if (cblock.CountTargets() != hblock.CountTargets())
						return false;
					if (cblock.FallThrough != null || hblock.FallThrough != null) {
						if (cblock.FallThrough == null || hblock.FallThrough == null)
							return false;

						var hs = CreateHandlerState(handler, matchState.OtherState.Blocks, hblock.FallThrough);
						var cs = CreateHandlerState(findState.CompositeState.HandlerMethod, findState.CompositeState.Blocks, cblock.FallThrough);
						stack.Push(new MatchState(hs, cs));
					}
					if (cblock.Targets != null || hblock.Targets != null) {
						if (cblock.Targets == null || hblock.Targets == null ||
							cblock.Targets.Count != hblock.Targets.Count)
							return false;

						for (int i = 0; i < cblock.Targets.Count; i++) {
							var hs = CreateHandlerState(handler, matchState.OtherState.Blocks, hblock.Targets[i]);
							var cs = CreateHandlerState(findState.CompositeState.HandlerMethod, findState.CompositeState.Blocks, cblock.Targets[i]);
							stack.Push(new MatchState(hs, cs));
						}
					}
				}
			}

			if (nextState == null) {
				findState.Done = true;
				return true;
			}
			else {
				if (findState.CompositeState.BlockIndex == nextState.Value.BlockIndex &&
					findState.CompositeState.InstructionIndex == nextState.Value.InstructionIndex)
					return false;
				findState.CompositeState = nextState.Value;
				return true;
			}
		}
		List<CompositeOpCodeHandler> CreateCompositeOpCodeHandlers(CsvmInfo csvmInfo, List<TypeDef> handlers) {
			var list = new List<CompositeOpCodeHandler>(handlers.Count);

			foreach (var handler in handlers) {
				var execHandler = new HandlerMethod(GetExecMethod(handler));
				list.Add(new CompositeOpCodeHandler(handler, execHandler));
			}

			return list;
		}
 static HandlerState CreateHandlerState(HandlerMethod handler, IList <Block> blocks, Block target)
 {
     return(new HandlerState(handler, blocks.IndexOf(target), 0));
 }
        bool Matches(HandlerMethod handler, ref FindHandlerState findState)
        {
            HandlerState?nextState = null;

            stack.Clear();
            stack.Push(new MatchState(new HandlerState(handler, 0, 0), findState.CompositeState));
            while (stack.Count > 0)
            {
                var matchState = stack.Pop();

                if (matchState.CompositeState.InstructionIndex == 0)
                {
                    if (findState.VisitedCompositeBlocks.ContainsKey(matchState.CompositeState.BlockIndex))
                    {
                        continue;
                    }
                    findState.VisitedCompositeBlocks[matchState.CompositeState.BlockIndex] = true;
                }
                else
                {
                    if (!findState.VisitedCompositeBlocks.ContainsKey(matchState.CompositeState.BlockIndex))
                    {
                        throw new ApplicationException("Block hasn't been visited");
                    }
                }

                if (!Compare(ref matchState.OtherState, ref matchState.CompositeState))
                {
                    return(false);
                }

                var hblock  = matchState.OtherState.Blocks[matchState.OtherState.BlockIndex];
                var hinstrs = hblock.Instructions;
                int hi      = matchState.OtherState.InstructionIndex;
                var cblock  = matchState.CompositeState.Blocks[matchState.CompositeState.BlockIndex];
                var cinstrs = cblock.Instructions;
                int ci      = matchState.CompositeState.InstructionIndex;
                if (hi < hinstrs.Count)
                {
                    return(false);
                }

                if (ci < cinstrs.Count)
                {
                    if (hblock.CountTargets() != 0)
                    {
                        return(false);
                    }
                    if (hblock.LastInstr.OpCode.Code == Code.Ret)
                    {
                        if (nextState != null)
                        {
                            return(false);
                        }
                        nextState = matchState.CompositeState;
                    }
                }
                else
                {
                    if (cblock.CountTargets() != hblock.CountTargets())
                    {
                        return(false);
                    }
                    if (cblock.FallThrough != null || hblock.FallThrough != null)
                    {
                        if (cblock.FallThrough == null || hblock.FallThrough == null)
                        {
                            return(false);
                        }

                        var hs = CreateHandlerState(handler, matchState.OtherState.Blocks, hblock.FallThrough);
                        var cs = CreateHandlerState(findState.CompositeState.HandlerMethod, findState.CompositeState.Blocks, cblock.FallThrough);
                        stack.Push(new MatchState(hs, cs));
                    }
                    if (cblock.Targets != null || hblock.Targets != null)
                    {
                        if (cblock.Targets == null || hblock.Targets == null ||
                            cblock.Targets.Count != hblock.Targets.Count)
                        {
                            return(false);
                        }

                        for (int i = 0; i < cblock.Targets.Count; i++)
                        {
                            var hs = CreateHandlerState(handler, matchState.OtherState.Blocks, hblock.Targets[i]);
                            var cs = CreateHandlerState(findState.CompositeState.HandlerMethod, findState.CompositeState.Blocks, cblock.Targets[i]);
                            stack.Push(new MatchState(hs, cs));
                        }
                    }
                }
            }

            if (nextState == null)
            {
                findState.Done = true;
                return(true);
            }
            else
            {
                if (findState.CompositeState.BlockIndex == nextState.Value.BlockIndex &&
                    findState.CompositeState.InstructionIndex == nextState.Value.InstructionIndex)
                {
                    return(false);
                }
                findState.CompositeState = nextState.Value;
                return(true);
            }
        }
		public OpCodeHandler(OpCodeHandlerInfo opCodeHandlerInfo, TypeDef handlerType, HandlerMethod execMethod) {
			this.OpCodeHandlerInfo = opCodeHandlerInfo;
			this.HandlerType = handlerType;
			this.ExecMethod = execMethod;
		}
		public CompositeOpCodeHandler(TypeDef handlerType, HandlerMethod execMethod) {
			this.HandlerType = handlerType;
			this.ExecMethod = execMethod;
			this.OpCodeHandlerInfos = new List<OpCodeHandlerInfo>();
		}