public void convert(MethodDef cilMethod, CsvmMethodData csvmMethod)
        {
            var newInstructions = readInstructions(cilMethod, csvmMethod);
            var newLocals = readLocals(cilMethod, csvmMethod);
            var newExceptions = readExceptions(cilMethod, csvmMethod, newInstructions);

            fixInstructionOperands(newInstructions);
            fixLocals(newInstructions, cilMethod.Body.Variables);
            fixArgs(newInstructions, cilMethod);

            DotNetUtils.restoreBody(cilMethod, newInstructions, newExceptions);

            if (!operandRestorer.restore(cilMethod))
                Logger.w("Failed to restore one or more instruction operands in CSVM method {0:X8}", cilMethod.MDToken.ToInt32());
            restoreConstrainedPrefix(cilMethod);
        }
Ejemplo n.º 2
0
		public List<CsvmMethodData> Read() {
			int numMethods = reader.ReadInt32();
			if (numMethods < 0)
				throw new ApplicationException("Invalid number of methods");
			var methods = new List<CsvmMethodData>(numMethods);

			for (int i = 0; i < numMethods; i++) {
				var csvmMethod = new CsvmMethodData();
				csvmMethod.Guid = new Guid(reader.ReadBytes(16));
				csvmMethod.Token = reader.ReadInt32();
				csvmMethod.Locals = reader.ReadBytes(reader.ReadInt32());
				csvmMethod.Instructions = reader.ReadBytes(reader.ReadInt32());
				csvmMethod.Exceptions = reader.ReadBytes(reader.ReadInt32());
				methods.Add(csvmMethod);
			}

			return methods;
		}
Ejemplo n.º 3
0
        public void Convert(MethodDef cilMethod, CsvmMethodData csvmMethod)
        {
            cilToVmIndex.Clear();
            vmIndexToCil.Clear();

            var newInstructions = ReadInstructions(cilMethod, csvmMethod);
            var newLocals       = ReadLocals(cilMethod, csvmMethod);
            var newExceptions   = ReadExceptions(cilMethod, csvmMethod);

            FixInstructionOperands(newInstructions);
            FixLocals(newInstructions, cilMethod.Body.Variables);
            FixArgs(newInstructions, cilMethod);

            DotNetUtils.RestoreBody(cilMethod, newInstructions, newExceptions);

            if (!operandRestorer.Restore(cilMethod))
            {
                Logger.w("Failed to restore one or more instruction operands in CSVM method {0:X8}", cilMethod.MDToken.ToInt32());
            }
            RestoreConstrainedPrefix(cilMethod);
        }
Ejemplo n.º 4
0
        List <ExceptionHandler> ReadExceptions(MethodDef cilMethod, CsvmMethodData csvmMethod)
        {
            var reader = new BinaryReader(new MemoryStream(csvmMethod.Exceptions));
            var ehs    = new List <ExceptionHandler>();

            if (reader.BaseStream.Length == 0)
            {
                return(ehs);
            }

            int numExceptions = reader.ReadInt32();

            if (numExceptions < 0)
            {
                throw new ApplicationException("Invalid number of exception handlers");
            }

            var gpContext = GenericParamContext.Create(cilMethod);

            for (int i = 0; i < numExceptions; i++)
            {
                var eh = new ExceptionHandler((ExceptionHandlerType)reader.ReadInt32());
                eh.TryStart     = GetInstruction(reader.ReadInt32());
                eh.TryEnd       = GetInstructionEnd(reader.ReadInt32());
                eh.HandlerStart = GetInstruction(reader.ReadInt32());
                eh.HandlerEnd   = GetInstructionEnd(reader.ReadInt32());
                if (eh.HandlerType == ExceptionHandlerType.Catch)
                {
                    eh.CatchType = module.ResolveToken(reader.ReadUInt32(), gpContext) as ITypeDefOrRef;
                }
                else if (eh.HandlerType == ExceptionHandlerType.Filter)
                {
                    eh.FilterStart = GetInstruction(reader.ReadInt32());
                }

                ehs.Add(eh);
            }

            return(ehs);
        }
Ejemplo n.º 5
0
        public List <CsvmMethodData> read()
        {
            int numMethods = reader.ReadInt32();

            if (numMethods < 0)
            {
                throw new ApplicationException("Invalid number of methods");
            }
            var methods = new List <CsvmMethodData>(numMethods);

            for (int i = 0; i < numMethods; i++)
            {
                var csvmMethod = new CsvmMethodData();
                csvmMethod.Guid         = new Guid(reader.ReadBytes(16));
                csvmMethod.Token        = reader.ReadInt32();
                csvmMethod.Locals       = reader.ReadBytes(reader.ReadInt32());
                csvmMethod.Instructions = reader.ReadBytes(reader.ReadInt32());
                csvmMethod.Exceptions   = reader.ReadBytes(reader.ReadInt32());
                methods.Add(csvmMethod);
            }

            return(methods);
        }
Ejemplo n.º 6
0
 protected abstract List <Instruction> ReadInstructions(MethodDef cilMethod, CsvmMethodData csvmMethod);
 protected abstract List<Instruction> ReadInstructions(MethodDef cilMethod, CsvmMethodData csvmMethod);
        List<Local> ReadLocals(MethodDef cilMethod, CsvmMethodData csvmMethod)
        {
            var locals = new List<Local>();
            var reader = new BinaryReader(new MemoryStream(csvmMethod.Locals));

            if (csvmMethod.Locals.Length == 0)
                return locals;

            // v6.0.0.5 sometimes duplicates the last two locals so only check for a negative value.
            int numLocals = reader.ReadInt32();
            if (numLocals < 0)
                throw new ApplicationException("Invalid number of locals");

            var gpContext = GenericParamContext.Create(cilMethod);
            for (int i = 0; i < numLocals; i++)
                locals.Add(new Local(ReadTypeRef(reader, gpContext)));

            return locals;
        }
        List<ExceptionHandler> ReadExceptions(MethodDef cilMethod, CsvmMethodData csvmMethod)
        {
            var reader = new BinaryReader(new MemoryStream(csvmMethod.Exceptions));
            var ehs = new List<ExceptionHandler>();

            if (reader.BaseStream.Length == 0)
                return ehs;

            int numExceptions = reader.ReadInt32();
            if (numExceptions < 0)
                throw new ApplicationException("Invalid number of exception handlers");

            var gpContext = GenericParamContext.Create(cilMethod);
            for (int i = 0; i < numExceptions; i++) {
                var eh = new ExceptionHandler((ExceptionHandlerType)reader.ReadInt32());
                eh.TryStart = GetInstruction(reader.ReadInt32());
                eh.TryEnd = GetInstructionEnd(reader.ReadInt32());
                eh.HandlerStart = GetInstruction(reader.ReadInt32());
                eh.HandlerEnd = GetInstructionEnd(reader.ReadInt32());
                if (eh.HandlerType == ExceptionHandlerType.Catch)
                    eh.CatchType = module.ResolveToken(reader.ReadUInt32(), gpContext) as ITypeDefOrRef;
                else if (eh.HandlerType == ExceptionHandlerType.Filter)
                    eh.FilterStart = GetInstruction(reader.ReadInt32());

                ehs.Add(eh);
            }

            return ehs;
        }
 List<Instruction> readInstructions(MethodDef cilMethod, CsvmMethodData csvmMethod)
 {
     var reader = new BinaryReader(new MemoryStream(csvmMethod.Instructions));
     var instrs = new List<Instruction>();
     uint offset = 0;
     while (reader.BaseStream.Position < reader.BaseStream.Length) {
         int vmOpCode = reader.ReadUInt16();
         var instr = opCodeDetector.Handlers[vmOpCode].Read(reader);
         instr.Offset = offset;
         offset += (uint)getInstructionSize(instr);
         instrs.Add(instr);
     }
     return instrs;
 }