예제 #1
0
        public void Bitmask()
        {
            OpCodeBitmask branches = new OpCodeBitmask();

            branches.Set(Code.Beq);
            branches.Set(Code.Beq_S);
            branches.Set(Code.Bge);
            branches.Set(Code.Bge_S);
            branches.Set(Code.Bge_Un);
            branches.Set(Code.Bge_Un_S);
            branches.Set(Code.Bgt);
            branches.Set(Code.Bgt_S);
            branches.Set(Code.Bgt_Un);
            branches.Set(Code.Bgt_Un_S);
            branches.Set(Code.Ble);
            branches.Set(Code.Ble_S);
            branches.Set(Code.Ble_Un);
            branches.Set(Code.Ble_Un_S);
            branches.Set(Code.Blt);
            branches.Set(Code.Blt_S);
            branches.Set(Code.Blt_Un);
            branches.Set(Code.Blt_Un_S);
            branches.Set(Code.Bne_Un);
            branches.Set(Code.Bne_Un_S);
            branches.Set(Code.Br);
            branches.Set(Code.Br_S);
            branches.Set(Code.Brfalse);
            branches.Set(Code.Brfalse_S);
            branches.Set(Code.Brtrue);
            branches.Set(Code.Brtrue_S);
            Console.WriteLine(branches);
        }
        static protected bool IsApplicable(MethodDefinition method)
        {
            // we only check methods with a body
            if ((method == null) || !method.HasBody)
            {
                return(false);
            }

            // we don't check System.Single and System.Double
            // special case for handling mscorlib.dll
            if (method.DeclaringType.IsFloatingPoint())
            {
                return(false);
            }

            // rule applies only if the method contains Call[virt] (calls to Equals)
            OpCodeBitmask mask = OpCodeEngine.GetBitmask(method);

            if (OpCodeBitmask.Calls.Intersect(mask))
            {
                return(true);
            }
            // *or* Ceq instructions
            return(mask.Get(Code.Ceq));
        }
        public RuleResult CheckMethod(MethodDefinition method)
        {
            // Rule does not apply if the method has no IL
            if (!method.HasBody)
            {
                return(RuleResult.DoesNotApply);
            }
            //
            // We need to check all methods which has any of the following opcodes: call, stfld or stsfld.
            //
            OpCodeBitmask bitmask = OpCodeEngine.GetBitmask(method);

            if (!applicable_method_bitmask.Intersect(bitmask))
            {
                return(RuleResult.DoesNotApply);
            }

            /* Unfortunately it's possible to generate IL this rule will choke on, especially when
             * using non-standard compilers or obfuscators. */
            try
            {
                return(CheckMethodUnsafe(method));
            }
            catch (Exception ex)
            {
                // FIXME: This problem should be reported some other way, it's not really a failure.
                // Pending implementation of "analysis warnings", as mentioned here (post #21):
                // http://groups.google.com/group/gendarme/browse_frm/thread/c37d157ae0c9682/57f89f3abf14f2fd?tvc=1&q=Gendarme+2.6+Preview+1+is+ready+for+download#57f89f3abf14f2fd
                Runner.Report(method, Severity.Low, Confidence.Low,
                              String.Format(CultureInfo.CurrentCulture, "An exception occurred while verifying this method. " +
                                            "This failure can probably be ignored, it's most likely due to an " +
                                            "uncommon code sequence in the method the rule didn't understand. {0}", ex.Message));
                return(RuleResult.Failure);
            }
        }
 public void Bitmask ()
 {
     OpCodeBitmask unbox = new OpCodeBitmask ();
     unbox.Set (Code.Unbox);
     unbox.Set (Code.Unbox_Any);
     Console.WriteLine (unbox);
 }
예제 #5
0
        public void GenerateBitmask()
        {
            OpCodeBitmask throwers  = new OpCodeBitmask();
            OpCodeBitmask alwaysBad = new OpCodeBitmask();
            OpCodeBitmask overflow  = new OpCodeBitmask();
            OpCodeBitmask casts     = new OpCodeBitmask();

            foreach (Code code in AlwaysBad)
            {
                throwers.Set(code);
                alwaysBad.Set(code);
            }

            foreach (Code code in Casts)
            {
                casts.Set(code);
                throwers.Set(code);
            }
            throwers.Set(Code.Throw);

            foreach (Code code in Overflow)
            {
                throwers.Set(code);
                overflow.Set(code);
            }

            Console.WriteLine("throwers: {0}", throwers);
            Console.WriteLine("alwaysBad: {0}", alwaysBad);
            Console.WriteLine("overflow: {0}", overflow);
            Console.WriteLine("casts: {0}", casts);
        }
예제 #6
0
        public RuleResult CheckMethod(MethodDefinition method)
        {
            // rule only applies to methods with IL and exceptions handlers
            if (!method.HasBody || !method.Body.HasExceptionHandlers)
            {
                return(RuleResult.DoesNotApply);
            }

            // and when the IL contains a Throw instruction (Rethrow is fine)
            OpCodeBitmask mask = OpCodeEngine.GetBitmask(method);

            if (!mask.Get(Code.Throw))
            {
                return(RuleResult.DoesNotApply);
            }

            // we can use a faster code path when no branches are present in the method
            if (mask.Intersect(branches))
            {
                Branches(method);
            }
            else
            {
                Branchless(method);
            }

            warned_offsets_in_method.Clear();

            return(Runner.CurrentRuleResult);
        }
예제 #7
0
        public RuleResult CheckMethod(MethodDefinition method)
        {
            if (!method.HasBody)
            {
                return(RuleResult.DoesNotApply);
            }

            //is there any interesting opcode in the method?
            OpCodeBitmask calls = OpCodeBitmask.Calls;

            if (!calls.Intersect(OpCodeEngine.GetBitmask(method)))
            {
                return(RuleResult.DoesNotApply);
            }

            foreach (Instruction ins in method.Body.Instructions)
            {
                if (!calls.Get(ins.OpCode.Code))
                {
                    continue;
                }

                CheckCall(method, ins, (MethodReference)ins.Operand);
            }

            return(Runner.CurrentRuleResult);
        }
예제 #8
0
        public void Bitmask()
        {
            OpCodeBitmask constant = new OpCodeBitmask();

            constant.Set(Code.Ldstr);
            constant.Set(Code.Ldc_I4);
            constant.Set(Code.Ldc_I4_0);
            constant.Set(Code.Ldc_I4_1);
            constant.Set(Code.Ldc_I4_2);
            constant.Set(Code.Ldc_I4_3);
            constant.Set(Code.Ldc_I4_4);
            constant.Set(Code.Ldc_I4_5);
            constant.Set(Code.Ldc_I4_6);
            constant.Set(Code.Ldc_I4_7);
            constant.Set(Code.Ldc_I4_8);
            constant.Set(Code.Ldc_I4_M1);
            constant.Set(Code.Ldc_I4_S);
            constant.Set(Code.Ldc_I8);
            constant.Set(Code.Ldc_R4);
            constant.Set(Code.Ldc_R8);
            Console.WriteLine(constant);

            OpCodeBitmask convert = new OpCodeBitmask();

            convert.Set(Code.Conv_I);
            convert.Set(Code.Conv_I1);
            convert.Set(Code.Conv_I2);
            convert.Set(Code.Conv_I4);
            convert.Set(Code.Conv_I8);
            convert.Set(Code.Conv_Ovf_I);
            convert.Set(Code.Conv_Ovf_I_Un);
            convert.Set(Code.Conv_Ovf_I1);
            convert.Set(Code.Conv_Ovf_I1_Un);
            convert.Set(Code.Conv_Ovf_I2);
            convert.Set(Code.Conv_Ovf_I2_Un);
            convert.Set(Code.Conv_Ovf_I4);
            convert.Set(Code.Conv_Ovf_I4_Un);
            convert.Set(Code.Conv_Ovf_I8);
            convert.Set(Code.Conv_Ovf_I8_Un);
            convert.Set(Code.Conv_Ovf_U);
            convert.Set(Code.Conv_Ovf_U_Un);
            convert.Set(Code.Conv_Ovf_U1);
            convert.Set(Code.Conv_Ovf_U1_Un);
            convert.Set(Code.Conv_Ovf_U2);
            convert.Set(Code.Conv_Ovf_U2_Un);
            convert.Set(Code.Conv_Ovf_U4);
            convert.Set(Code.Conv_Ovf_U4_Un);
            convert.Set(Code.Conv_Ovf_U8);
            convert.Set(Code.Conv_Ovf_U8_Un);
            convert.Set(Code.Conv_R_Un);
            convert.Set(Code.Conv_R4);
            convert.Set(Code.Conv_R8);
            convert.Set(Code.Conv_U);
            convert.Set(Code.Conv_U1);
            convert.Set(Code.Conv_U2);
            convert.Set(Code.Conv_U4);
            convert.Set(Code.Conv_U8);
            Console.WriteLine(convert);
        }
예제 #9
0
        public void Bitmask()
        {
            OpCodeBitmask mask = new OpCodeBitmask();

            mask.Set(Code.Div);
            mask.Set(Code.Div_Un);
            Console.WriteLine(mask);
        }
        public void Bitmask()
        {
            OpCodeBitmask mask = new OpCodeBitmask();

            mask.Set(Code.Stfld);
            mask.Set(Code.Stelem_Ref);
            Console.WriteLine(mask);
        }
        public void GenerateBitmask()
        {
            OpCodeBitmask bitmask = new OpCodeBitmask();

            bitmask.Set(Code.Newobj);
            bitmask.Set(Code.Throw);
            Console.WriteLine(bitmask);
        }
        public void Bitmask()
        {
            OpCodeBitmask casts = new OpCodeBitmask();

            casts.Set(Code.Castclass);
            casts.Set(Code.Isinst);
            Console.WriteLine(casts);
        }
        public void BuildRemainder()
        {
            OpCodeBitmask remainder = new OpCodeBitmask();

            remainder.Set(Code.Rem);
            remainder.Set(Code.Rem_Un);
            Console.WriteLine(remainder);
        }
        public void Bitmask()
        {
            OpCodeBitmask mask = new OpCodeBitmask();

            mask.Set(Code.Isinst);
            mask.Set(Code.Ldnull);
            Console.WriteLine(mask);
        }
예제 #15
0
        public void Bitmask()
        {
            OpCodeBitmask mask = new OpCodeBitmask();

            mask.Set(Code.Mul);
            mask.Set(Code.Mul_Ovf);
            mask.Set(Code.Mul_Ovf_Un);
            Console.WriteLine(mask);
        }
예제 #16
0
        public void Bitmask()
        {
            OpCodeBitmask zero = new OpCodeBitmask();

            zero.Set(Code.Ldc_I4);
            zero.Set(Code.Ldc_I4_S);
            zero.Set(Code.Ldc_I4_0);
            Console.WriteLine(zero);
        }
예제 #17
0
        private void Bitmask()
        {
            OpCodeBitmask opcodes_mask = new OpCodeBitmask();

            opcodes_mask.Set(Code.Ldftn);
            opcodes_mask.Set(Code.Call);
            opcodes_mask.Set(Code.Callvirt);

            Console.WriteLine(opcodes_mask);
        }
 static OpCodeBitmask BuildCallsAndNewobjOpCodeBitmask()
 {
                 #if true
     return(new OpCodeBitmask(0x8000000000, 0x4400000000000, 0x0, 0x0));
                 #else
     OpCodeBitmask mask = new OpCodeBitmask();
     mask.UnionWith(OpCodeBitmask.Calls);
     mask.Set(Code.Newobj);
     return(mask);
                 #endif
 }
예제 #19
0
        private static OpCodeBitmask ConditionsBitmask()
        {
            OpCodeBitmask mask = new OpCodeBitmask();

            mask.Set(Code.Brtrue);
            mask.Set(Code.Brtrue_S);
            mask.Set(Code.Brfalse);
            mask.Set(Code.Brfalse_S);
            Console.WriteLine("ConditionsBitmask : " + mask);
            return(mask);
        }
예제 #20
0
        public void Bitmask()
        {
            OpCodeBitmask fields = new OpCodeBitmask();

            fields.Set(Code.Ldfld);
            fields.Set(Code.Ldflda);
            fields.Set(Code.Ldsfld);
            fields.Set(Code.Ldsflda);
            fields.Set(Code.Stfld);
            fields.Set(Code.Stsfld);
            Console.WriteLine(fields);
        }
예제 #21
0
        private void Bitmask()
        {
            OpCodeBitmask mask = new OpCodeBitmask();

            mask.Set(Code.Call);
            mask.Set(Code.Callvirt);
            mask.Set(Code.Ldfld);
            mask.Set(Code.Ldflda);
            mask.Set(Code.Stfld);
            mask.Set(Code.Newobj);
            Console.WriteLine(mask);
        }
예제 #22
0
        public void Convert()
        {
            OpCodeBitmask Convert8 = new OpCodeBitmask(0x0, 0x0, 0x0, 0x0);

            Convert8.Set(Code.Conv_I8);
            Convert8.Set(Code.Conv_U8);
            Convert8.Set(Code.Conv_Ovf_I8);
            Convert8.Set(Code.Conv_Ovf_I8_Un);
            Convert8.Set(Code.Conv_Ovf_U8);
            Convert8.Set(Code.Conv_Ovf_U8_Un);
            Console.WriteLine(Convert8);
        }
예제 #23
0
        private static OpCodeBitmask BuildInterlockedFriendlyOpCodeBitmask()
        {
#if true
            return(new OpCodeBitmask(0x80063FFFFFFFFF, 0x3f00000001800000, 0x1F30000000000000, 0x5F80));
#else
            OpCodeBitmask mask = new OpCodeBitmask();
            mask.UnionWith(OpCodeBitmask.FlowControlBranch);
            mask.UnionWith(OpCodeBitmask.FlowControlReturn);
            //locals
            mask.UnionWith(OpCodeBitmask.LoadLocal);
            mask.UnionWith(OpCodeBitmask.StoreLocal);
            //arguments
            mask.UnionWith(OpCodeBitmask.LoadArgument);
            mask.UnionWith(OpCodeBitmask.StoreArgument);
            //fields
            mask.Set(Code.Ldfld);
            mask.Set(Code.Ldflda);
            mask.Set(Code.Stfld);
            mask.Set(Code.Ldsfld);
            mask.Set(Code.Ldsflda);
            mask.Set(Code.Stsfld);
            //constants
            mask.Set(Code.Ldnull);
            mask.Set(Code.Ldc_I4_M1);
            mask.Set(Code.Ldc_I4_0);
            mask.Set(Code.Ldc_I4_1);
            mask.Set(Code.Ldc_I4_2);
            mask.Set(Code.Ldc_I4_3);
            mask.Set(Code.Ldc_I4_4);
            mask.Set(Code.Ldc_I4_5);
            mask.Set(Code.Ldc_I4_6);
            mask.Set(Code.Ldc_I4_7);
            mask.Set(Code.Ldc_I4_8);
            mask.Set(Code.Ldc_I4_S);
            mask.Set(Code.Ldc_I4);
            mask.Set(Code.Ldc_I8);
            mask.Set(Code.Ldc_R4);
            mask.Set(Code.Ldc_R8);
            //safe
            mask.Set(Code.Dup);
            mask.Set(Code.Pop);
            mask.Set(Code.Nop);
            mask.Set(Code.Break);
            //could be replace by interlocked call
            mask.Set(Code.Add);
            mask.Set(Code.Add_Ovf);
            mask.Set(Code.Add_Ovf_Un);
            mask.Set(Code.Sub);
            mask.Set(Code.Sub_Ovf);
            mask.Set(Code.Sub_Ovf_Un);
            return(mask);
#endif
        }
예제 #24
0
        private static OpCodeBitmask ComparisonsBitmask()
        {
            OpCodeBitmask mask = new OpCodeBitmask();

            mask.Set(Code.Cgt);
            mask.Set(Code.Ceq);
            mask.Set(Code.Clt);
            mask.Set(Code.Ble);
            mask.Set(Code.Ble_S);
            mask.Set(Code.Bge);
            mask.Set(Code.Bge_S);
            Console.WriteLine("ComparisonsBitmask : " + mask);
            return(mask);
        }
예제 #25
0
        protected override void CheckMethod(MethodDefinition method, bool abstractWarning)
        {
            if ((method == null) || !method.HasBody)
            {
                return;
            }

            OpCodeBitmask bitmask = OpCodeEngine.GetBitmask(method);

            // method must have a CALL[VIRT] and either STFLD or STELEM_REF
            if (!bitmask.Intersect(OpCodeBitmask.Calls) || !bitmask.Intersect(StoreFieldBitmask))
            {
                return;
            }

            foreach (Instruction ins in method.Body.Instructions)
            {
                MethodReference mr = (ins.Operand as MethodReference);
                if (mr == null || mr.DeclaringType.IsNative())
                {
                    continue;
                }

                FieldDefinition field = null;
                Instruction     next  = ins.Next;
                if (next.Is(Code.Stfld))
                {
                    field = next.Operand as FieldDefinition;
                }
                else if (next.Is(Code.Stobj) || next.Is(Code.Stind_I))
                {
                    Instruction origin = next.TraceBack(method);
                    if (origin.Is(Code.Ldelema))
                    {
                        origin = origin.TraceBack(method);
                        if (origin != null)
                        {
                            field = origin.Operand as FieldDefinition;
                        }
                    }
                }

                if (field != null && FieldCandidates.Contains(field))
                {
                    Runner.Report(field, Severity.High, Confidence.High,
                                  abstractWarning ? AbstractTypeMessage : TypeMessage);
                }
            }
        }
예제 #26
0
		void OnMethodBody (object sender, EngineEventArgs e)
		{
			MethodBody body = (sender as MethodBody);
			MethodDefinition method = body.Method;
			// some runners, like the wizard, can run this engine several times
			// and, in this case, the result won't change
			if (bitmasks.ContainsKey (method))
				return;

			OpCodeBitmask mask = new OpCodeBitmask ();
			foreach (Instruction ins in body.Instructions) {
				mask.Set (ins.OpCode.Code);
			}
			bitmasks.Add (method, mask);
		}
예제 #27
0
        public RuleResult CheckMethod(MethodDefinition method)
        {
            if (!method.HasBody)
            {
                return(RuleResult.DoesNotApply);
            }

            // exclude methods that don't have calls
            OpCodeBitmask mask = OpCodeEngine.GetBitmask(method);

            if (!OpCodeBitmask.Calls.Intersect(mask))
            {
                return(RuleResult.DoesNotApply);
            }
            // *and* methods that don't convert into an [u]int64
            if (!Convert8.Intersect(mask))
            {
                return(RuleResult.DoesNotApply);
            }

            foreach (Instruction ins in method.Body.Instructions)
            {
                if (ins.OpCode.FlowControl != FlowControl.Call)
                {
                    continue;
                }

                if (!IsInt64BitsToDouble(ins.Operand as MethodReference))
                {
                    continue;
                }

                // if the previous call convert a value into a long (int64)
                // then it's likely that the API is being misused
                switch (ins.Previous.OpCode.Code)
                {
                case Code.Conv_I8:
                case Code.Conv_U8:
                case Code.Conv_Ovf_I8:
                case Code.Conv_Ovf_I8_Un:
                case Code.Conv_Ovf_U8:
                case Code.Conv_Ovf_U8_Un:
                    Runner.Report(method, ins, Severity.High, Confidence.High);
                    break;
                }
            }
            return(Runner.CurrentRuleResult);
        }
        private void Bitmask()
        {
            OpCodeBitmask mask = new OpCodeBitmask();

            mask.Set(Code.Starg);
            mask.Set(Code.Starg_S);
            mask.Set(Code.Stloc_0);
            mask.Set(Code.Stloc_1);
            mask.Set(Code.Stloc_2);
            mask.Set(Code.Stloc_3);
            mask.Set(Code.Stloc);
            mask.Set(Code.Stloc_S);
            mask.Set(Code.Stfld);
            mask.Set(Code.Stsfld);

            Console.WriteLine(mask);
        }
        public RuleResult CheckMethod(MethodDefinition method)
        {
            if (!method.HasBody)
            {
                return(RuleResult.DoesNotApply);
            }

            OpCodeBitmask bitmask = OpCodeEngine.GetBitmask(method);

            // is there any Call or Callvirt instructions in the method ?
            if (!OpCodeBitmask.Calls.Intersect(bitmask))
            {
                return(RuleResult.DoesNotApply);
            }
            // is there a Ldnull instruction in the method ?
            if (!bitmask.Get(Code.Ldnull))
            {
                return(RuleResult.DoesNotApply);
            }

            foreach (Instruction ins in method.Body.Instructions)
            {
                switch (ins.OpCode.Code)
                {
                case Code.Call:
                case Code.Callvirt:
                    // if we're calling bool type.Equals({anytype})
                    if (!Equals.Matches(ins.Operand as MethodReference))
                    {
                        continue;
                    }

                    // and that the previous, real, instruction is loading a null value
                    // note: check the first parameter (not the instance)
                    Instruction source = ins.TraceBack(method, -1);
                    if ((source != null) && (source.OpCode.Code == Code.Ldnull))
                    {
                        Runner.Report(method, ins, Severity.Low, Confidence.High);
                    }

                    break;
                }
            }

            return(Runner.CurrentRuleResult);
        }
예제 #30
0
        public RuleResult CheckMethod(MethodDefinition method)
        {
            // rule doesn't not apply to methods without code (e.g. p/invokes)
            if (!method.HasBody)
            {
                return(RuleResult.DoesNotApply);
            }

            // is there any Call or Callvirt instructions in the method
            OpCodeBitmask calls = OpCodeBitmask.Calls;

            if (!calls.Intersect(OpCodeEngine.GetBitmask(method)))
            {
                return(RuleResult.DoesNotApply);
            }

            // go!

            // we look for a call to String.Length property (since it's much easier
            // than checking a string being compared to null)
            foreach (Instruction current in method.Body.Instructions)
            {
                if (!calls.Get(current.OpCode.Code))
                {
                    continue;
                }

                MethodReference mr = (current.Operand as MethodReference);
                if (mr.IsNamed("System", "String", "get_Length"))
                {
                    // now that we found it we check that
                    // 1 - we previously did a check null on the same value (that we already know is a string)
                    Instruction branch = PreLengthCheck(method, current.Previous);
                    if (branch == null)
                    {
                        continue;
                    }
                    // 2 - we compare the return value (length) with 0
                    if (PostLengthCheck(current.Next, branch))
                    {
                        Runner.Report(method, current, Severity.Medium, Confidence.High);
                    }
                }
            }
            return(Runner.CurrentRuleResult);
        }
예제 #31
0
        public void BuildBitmask()
        {
            OpCodeBitmask bitmask = new OpCodeBitmask();

            bitmask.Set(Code.Ldind_I);
            bitmask.Set(Code.Ldind_I1);
            bitmask.Set(Code.Ldind_I2);
            bitmask.Set(Code.Ldind_I4);
            bitmask.Set(Code.Ldind_I8);
            bitmask.Set(Code.Ldind_R4);
            bitmask.Set(Code.Ldind_R8);
            bitmask.Set(Code.Ldind_U1);
            bitmask.Set(Code.Ldind_U2);
            bitmask.Set(Code.Ldind_U4);
            Console.WriteLine("LoadIndirect = {0}", bitmask);

            bitmask.ClearAll();
            bitmask.Set(Code.Stind_I);
            bitmask.Set(Code.Stind_I1);
            bitmask.Set(Code.Stind_I2);
            bitmask.Set(Code.Stind_I4);
            bitmask.Set(Code.Stind_I8);
            bitmask.Set(Code.Stind_R4);
            bitmask.Set(Code.Stind_R8);
            Console.WriteLine("StoreIndirect = {0}", bitmask);

            bitmask.ClearAll();
            bitmask.Set(Code.Bge);
            bitmask.Set(Code.Bge_S);
            bitmask.Set(Code.Bge_Un);
            bitmask.Set(Code.Bge_Un_S);
            bitmask.Set(Code.Bgt);
            bitmask.Set(Code.Bgt_S);
            bitmask.Set(Code.Bgt_Un);
            bitmask.Set(Code.Bgt_Un_S);
            bitmask.Set(Code.Ble);
            bitmask.Set(Code.Ble_S);
            bitmask.Set(Code.Ble_Un);
            bitmask.Set(Code.Ble_Un_S);
            bitmask.Set(Code.Blt);
            bitmask.Set(Code.Blt_S);
            bitmask.Set(Code.Blt_Un);
            bitmask.Set(Code.Blt_Un_S);
            Console.WriteLine("GreaterOrLesserThan = {0}", bitmask);
        }
		public void Bitmask ()
		{
			OpCodeBitmask constant = new OpCodeBitmask ();
			constant.Set (Code.Ldstr);
			constant.Set (Code.Ldc_I4);
			constant.Set (Code.Ldc_I4_0);
			constant.Set (Code.Ldc_I4_1);
			constant.Set (Code.Ldc_I4_2);
			constant.Set (Code.Ldc_I4_3);
			constant.Set (Code.Ldc_I4_4);
			constant.Set (Code.Ldc_I4_5);
			constant.Set (Code.Ldc_I4_6);
			constant.Set (Code.Ldc_I4_7);
			constant.Set (Code.Ldc_I4_8);
			constant.Set (Code.Ldc_I4_M1);
			constant.Set (Code.Ldc_I4_S);
			constant.Set (Code.Ldc_I8);
			constant.Set (Code.Ldc_R4);
			constant.Set (Code.Ldc_R8);
			Console.WriteLine (constant);

			OpCodeBitmask convert = new OpCodeBitmask ();
			convert.Set (Code.Conv_I);
			convert.Set (Code.Conv_I1);
			convert.Set (Code.Conv_I2);
			convert.Set (Code.Conv_I4);
			convert.Set (Code.Conv_I8);
			convert.Set (Code.Conv_Ovf_I);
			convert.Set (Code.Conv_Ovf_I_Un);
			convert.Set (Code.Conv_Ovf_I1);
			convert.Set (Code.Conv_Ovf_I1_Un);
			convert.Set (Code.Conv_Ovf_I2);
			convert.Set (Code.Conv_Ovf_I2_Un);
			convert.Set (Code.Conv_Ovf_I4);
			convert.Set (Code.Conv_Ovf_I4_Un);
			convert.Set (Code.Conv_Ovf_I8);
			convert.Set (Code.Conv_Ovf_I8_Un);
			convert.Set (Code.Conv_Ovf_U);
			convert.Set (Code.Conv_Ovf_U_Un);
			convert.Set (Code.Conv_Ovf_U1);
			convert.Set (Code.Conv_Ovf_U1_Un);
			convert.Set (Code.Conv_Ovf_U2);
			convert.Set (Code.Conv_Ovf_U2_Un);
			convert.Set (Code.Conv_Ovf_U4);
			convert.Set (Code.Conv_Ovf_U4_Un);
			convert.Set (Code.Conv_Ovf_U8);
			convert.Set (Code.Conv_Ovf_U8_Un);
			convert.Set (Code.Conv_R_Un);
			convert.Set (Code.Conv_R4);
			convert.Set (Code.Conv_R8);
			convert.Set (Code.Conv_U);
			convert.Set (Code.Conv_U1);
			convert.Set (Code.Conv_U2);
			convert.Set (Code.Conv_U4);
			convert.Set (Code.Conv_U8);
			Console.WriteLine (convert);
		}
		private void Bitmask ()
		{
			OpCodeBitmask mask = new OpCodeBitmask ();
			mask.Set (Code.Call);
			mask.Set (Code.Callvirt);
			mask.Set (Code.Ldfld);
			mask.Set (Code.Ldflda);
			mask.Set (Code.Stfld);
			mask.Set (Code.Newobj);
			Console.WriteLine (mask);
		}
예제 #34
0
		public void Bitmask ()
		{
			OpCodeBitmask bitmask = new OpCodeBitmask ();
			bitmask.Set (Code.Isinst);
			bitmask.Set (Code.Ldnull);
			bitmask.Set (Code.Ceq);
			Console.WriteLine (bitmask);
		}
		public void Bitmask ()
		{
			OpCodeBitmask fields = new OpCodeBitmask ();
			fields.Set (Code.Ldfld);
			fields.Set (Code.Ldflda);
			fields.Set (Code.Ldsfld);
			fields.Set (Code.Ldsflda);
			fields.Set (Code.Stfld);
			fields.Set (Code.Stsfld);
			Console.WriteLine (fields);
		}
예제 #36
0
		public void Bitmask ()
		{
			OpCodeBitmask mask = new OpCodeBitmask ();
			mask.Set (Code.Ldarg);
			mask.Set (Code.Ldarg_0);
			mask.Set (Code.Ldarg_1);
			mask.Set (Code.Ldarg_2);
			mask.Set (Code.Ldarg_3);
			mask.Set (Code.Ldarg_S);
			mask.Set (Code.Ldarga);
			mask.Set (Code.Ldarga_S);
			mask.Set (Code.Ldc_I4);
			mask.Set (Code.Ldc_I4_0);
			mask.Set (Code.Ldc_I4_1);
			mask.Set (Code.Ldc_I4_2);
			mask.Set (Code.Ldc_I4_3);
			mask.Set (Code.Ldc_I4_4);
			mask.Set (Code.Ldc_I4_5);
			mask.Set (Code.Ldc_I4_6);
			mask.Set (Code.Ldc_I4_7);
			mask.Set (Code.Ldc_I4_8);
			mask.Set (Code.Ldc_I4_M1);
			mask.Set (Code.Ldc_I4_S);
			mask.Set (Code.Ldc_I8);
			mask.Set (Code.Ldc_R4);
			mask.Set (Code.Ldc_R8);
			mask.Set (Code.Ldelem_Any);
			mask.Set (Code.Ldelem_I);
			mask.Set (Code.Ldelem_I1);
			mask.Set (Code.Ldelem_I2);
			mask.Set (Code.Ldelem_I4);
			mask.Set (Code.Ldelem_I8);
			mask.Set (Code.Ldelem_R4);
			mask.Set (Code.Ldelem_R8);
			mask.Set (Code.Ldelem_Ref);
			mask.Set (Code.Ldelem_U1);
			mask.Set (Code.Ldelem_U2);
			mask.Set (Code.Ldelem_U4);
			mask.Set (Code.Ldelema);
			mask.Set (Code.Ldfld);
			mask.Set (Code.Ldflda);
			mask.Set (Code.Ldftn);
			mask.Set (Code.Ldind_I);
			mask.Set (Code.Ldind_I1);
			mask.Set (Code.Ldind_I2);
			mask.Set (Code.Ldind_I4);
			mask.Set (Code.Ldind_I8);
			mask.Set (Code.Ldind_R4);
			mask.Set (Code.Ldind_R8);
			mask.Set (Code.Ldind_Ref);
			mask.Set (Code.Ldind_U1);
			mask.Set (Code.Ldind_U2);
			mask.Set (Code.Ldind_U4);
			mask.Set (Code.Ldlen);
			mask.Set (Code.Ldloc);
			mask.Set (Code.Ldloc_0);
			mask.Set (Code.Ldloc_1);
			mask.Set (Code.Ldloc_2);
			mask.Set (Code.Ldloc_3);
			mask.Set (Code.Ldloc_S);
			mask.Set (Code.Ldloca);
			mask.Set (Code.Ldloca_S);
			mask.Set (Code.Ldnull);
			mask.Set (Code.Ldobj);
			mask.Set (Code.Ldsfld);
			mask.Set (Code.Ldsflda);
			mask.Set (Code.Ldstr);
			mask.Set (Code.Ldtoken);
			mask.Set (Code.Ldvirtftn);
			Console.WriteLine (mask);
		}
		public void Bitmask ()
		{
			OpCodeBitmask mask = new OpCodeBitmask ();
			mask.Set (Code.Stfld);
			mask.Set (Code.Stelem_Ref);
			Console.WriteLine (mask);
		}
예제 #38
0
		private void Bitmask ()
		{
			OpCodeBitmask opcodes_mask = new OpCodeBitmask ();
			
			opcodes_mask.Set (Code.Ldftn);
			opcodes_mask.Set (Code.Call);
			opcodes_mask.Set (Code.Callvirt);
			
			Console.WriteLine (opcodes_mask);
		}
예제 #39
0
		public void BuildBitmask ()
		{
			OpCodeBitmask bitmask = new OpCodeBitmask ();
			bitmask.Set (Code.Ldind_I);
			bitmask.Set (Code.Ldind_I1);
			bitmask.Set (Code.Ldind_I2);
			bitmask.Set (Code.Ldind_I4);
			bitmask.Set (Code.Ldind_I8);
			bitmask.Set (Code.Ldind_R4);
			bitmask.Set (Code.Ldind_R8);
			bitmask.Set (Code.Ldind_U1);
			bitmask.Set (Code.Ldind_U2);
			bitmask.Set (Code.Ldind_U4);
			Console.WriteLine ("LoadIndirect = {0}", bitmask);

			bitmask.ClearAll ();
			bitmask.Set (Code.Stind_I);
			bitmask.Set (Code.Stind_I1);
			bitmask.Set (Code.Stind_I2);
			bitmask.Set (Code.Stind_I4);
			bitmask.Set (Code.Stind_I8);
			bitmask.Set (Code.Stind_R4);
			bitmask.Set (Code.Stind_R8);
			Console.WriteLine ("StoreIndirect = {0}", bitmask);

			bitmask.ClearAll ();
			bitmask.Set (Code.Bge);
			bitmask.Set (Code.Bge_S);
			bitmask.Set (Code.Bge_Un);
			bitmask.Set (Code.Bge_Un_S);
			bitmask.Set (Code.Bgt);
			bitmask.Set (Code.Bgt_S);
			bitmask.Set (Code.Bgt_Un);
			bitmask.Set (Code.Bgt_Un_S);
			bitmask.Set (Code.Ble);
			bitmask.Set (Code.Ble_S);
			bitmask.Set (Code.Ble_Un);
			bitmask.Set (Code.Ble_Un_S);
			bitmask.Set (Code.Blt);
			bitmask.Set (Code.Blt_S);
			bitmask.Set (Code.Blt_Un);
			bitmask.Set (Code.Blt_Un_S);
			Console.WriteLine ("GreaterOrLesserThan = {0}", bitmask);
		}
		public void BuildConversion ()
		{
			OpCodeBitmask convert = new OpCodeBitmask ();
			convert.Set (Code.Conv_I);
			convert.Set (Code.Conv_I1);
			convert.Set (Code.Conv_I2);
			convert.Set (Code.Conv_I4);
			convert.Set (Code.Conv_I8);
			convert.Set (Code.Conv_Ovf_I);
			convert.Set (Code.Conv_Ovf_I_Un);
			convert.Set (Code.Conv_Ovf_I1);
			convert.Set (Code.Conv_Ovf_I1_Un);
			convert.Set (Code.Conv_Ovf_I2);
			convert.Set (Code.Conv_Ovf_I2_Un);
			convert.Set (Code.Conv_Ovf_I4);
			convert.Set (Code.Conv_Ovf_I4_Un);
			convert.Set (Code.Conv_Ovf_I8);
			convert.Set (Code.Conv_Ovf_I8_Un);
			convert.Set (Code.Conv_Ovf_U);
			convert.Set (Code.Conv_Ovf_U_Un);
			convert.Set (Code.Conv_Ovf_U1);
			convert.Set (Code.Conv_Ovf_U1_Un);
			convert.Set (Code.Conv_Ovf_U2);
			convert.Set (Code.Conv_Ovf_U2_Un);
			convert.Set (Code.Conv_Ovf_U4);
			convert.Set (Code.Conv_Ovf_U4_Un);
			convert.Set (Code.Conv_Ovf_U8);
			convert.Set (Code.Conv_Ovf_U8_Un);
			convert.Set (Code.Conv_Ovf_U);
			convert.Set (Code.Conv_Ovf_U1);
			convert.Set (Code.Conv_Ovf_U2);
			convert.Set (Code.Conv_Ovf_U4);
			convert.Set (Code.Conv_Ovf_U8);
			Console.WriteLine (convert);
		}
		public void Bitmask ()
		{
			OpCodeBitmask mask = new OpCodeBitmask ();
			mask.Set (Code.Stfld);
			mask.Set (Code.Stsfld);
			/* if/when the local variables case is fixed 
			mask.Set (Code.Stloc_0);
			mask.Set (Code.Stloc_1);
			mask.Set (Code.Stloc_2);
			mask.Set (Code.Stloc_3);
			mask.Set (Code.Stloc);
			mask.Set (Code.Stloc_S); */
			mask.Set (Code.Starg);
			mask.Set (Code.Starg_S);
			Console.WriteLine (mask);
		}
예제 #42
0
		public void Bitmask ()
		{
			OpCodeBitmask zero = new OpCodeBitmask ();
			zero.Set (Code.Ldc_I4);
			zero.Set (Code.Ldc_I4_S);
			zero.Set (Code.Ldc_I4_0);
			Console.WriteLine (zero);
		}
		public void Bitmask ()
		{
			OpCodeBitmask mask = new OpCodeBitmask ();
			mask.Set (Code.Mul);
			mask.Set (Code.Mul_Ovf);
			mask.Set (Code.Mul_Ovf_Un);
			Console.WriteLine (mask);
		}
		public void BuildRemainder ()
		{
			OpCodeBitmask remainder = new OpCodeBitmask ();
			remainder.Set (Code.Rem);
			remainder.Set (Code.Rem_Un);
			Console.WriteLine (remainder);
		}
		public void Bitmask ()
		{
			OpCodeBitmask casts = new OpCodeBitmask ();
			casts.Set (Code.Castclass);
			casts.Set (Code.Isinst);
			Console.WriteLine (casts);
		}
		public void Convert ()
		{
			OpCodeBitmask Convert8 = new OpCodeBitmask (0x0, 0x0, 0x0, 0x0);
			Convert8.Set (Code.Conv_I8);
			Convert8.Set (Code.Conv_U8);
			Convert8.Set (Code.Conv_Ovf_I8);
			Convert8.Set (Code.Conv_Ovf_I8_Un);
			Convert8.Set (Code.Conv_Ovf_U8);
			Convert8.Set (Code.Conv_Ovf_U8_Un);
			Console.WriteLine (Convert8);
		}
		static OpCodeBitmask BuildCallsAndNewobjOpCodeBitmask ()
		{
			#if true
				return new OpCodeBitmask (0x8000000000, 0x4400000000000, 0x0, 0x0);
			#else
				OpCodeBitmask mask = new OpCodeBitmask ();
				mask.UnionWith (OpCodeBitmask.Calls);
				mask.Set (Code.Newobj);
				return mask;
			#endif
		}
예제 #48
0
 private static OpCodeBitmask ComparisonsBitmask()
 {
     OpCodeBitmask mask = new OpCodeBitmask ();
     mask.Set (Code.Cgt);
     mask.Set (Code.Ceq);
     mask.Set (Code.Clt);
     mask.Set (Code.Ble);
     mask.Set (Code.Ble_S);
     mask.Set (Code.Bge);
     mask.Set (Code.Bge_S);
     Console.WriteLine ("ComparisonsBitmask : " + mask);
     return mask;
 }
		private static OpCodeBitmask BuildInterlockedFriendlyOpCodeBitmask ()
		{
#if true
			return new OpCodeBitmask (0x80063FFFFFFFFF, 0x3f00000001800000, 0x1F30000000000000, 0x5F80);
#else
			OpCodeBitmask mask = new OpCodeBitmask ();
			mask.UnionWith (OpCodeBitmask.FlowControlBranch);
			mask.UnionWith (OpCodeBitmask.FlowControlReturn);
			//locals
			mask.UnionWith (OpCodeBitmask.LoadLocal);
			mask.UnionWith (OpCodeBitmask.StoreLocal);
			//arguments
			mask.UnionWith (OpCodeBitmask.LoadArgument);
			mask.UnionWith (OpCodeBitmask.StoreArgument);
			//fields
			mask.Set (Code.Ldfld);
			mask.Set (Code.Ldflda);
			mask.Set (Code.Stfld);
			mask.Set (Code.Ldsfld);
			mask.Set (Code.Ldsflda);
			mask.Set (Code.Stsfld);
			//constants
			mask.Set (Code.Ldnull);
			mask.Set (Code.Ldc_I4_M1);
			mask.Set (Code.Ldc_I4_0);
			mask.Set (Code.Ldc_I4_1);
			mask.Set (Code.Ldc_I4_2);
			mask.Set (Code.Ldc_I4_3);
			mask.Set (Code.Ldc_I4_4);
			mask.Set (Code.Ldc_I4_5);
			mask.Set (Code.Ldc_I4_6);
			mask.Set (Code.Ldc_I4_7);
			mask.Set (Code.Ldc_I4_8);
			mask.Set (Code.Ldc_I4_S);
			mask.Set (Code.Ldc_I4);
			mask.Set (Code.Ldc_I8);
			mask.Set (Code.Ldc_R4);
			mask.Set (Code.Ldc_R8);
			//safe
			mask.Set (Code.Dup);
			mask.Set (Code.Pop);
			mask.Set (Code.Nop);
			mask.Set (Code.Break);
			//could be replace by interlocked call
			mask.Set (Code.Add);
			mask.Set (Code.Add_Ovf);
			mask.Set (Code.Add_Ovf_Un);
			mask.Set (Code.Sub);
			mask.Set (Code.Sub_Ovf);
			mask.Set (Code.Sub_Ovf_Un);
			return mask;
#endif
		}
		public void GenerateBitmask ()
		{
			OpCodeBitmask bitmask = new OpCodeBitmask ();
			bitmask.Set (Code.Newobj);
			bitmask.Set (Code.Throw);
			Console.WriteLine (bitmask);
		}
		public void Bitmask ()
		{
			OpCodeBitmask null_compare = new OpCodeBitmask ();
			null_compare.Set (Code.Brfalse);
			null_compare.Set (Code.Brfalse_S);
			null_compare.Set (Code.Brtrue);
			null_compare.Set (Code.Brtrue_S);
			Console.WriteLine (null_compare);

			OpCodeBitmask check = new OpCodeBitmask ();
			check.UnionWith (OpCodeBitmask.LoadElement);
			check.UnionWith (OpCodeBitmask.LoadIndirect);
			check.Set (Code.Ldlen);
			check.Set (Code.Ldfld);
			check.Set (Code.Ldflda);
			check.Set (Code.Stfld);
			Console.WriteLine (check);
		}
		public void Bitmask ()
		{
			OpCodeBitmask mask = new OpCodeBitmask ();
			mask.Set (Code.Div);
			mask.Set (Code.Div_Un);
			Console.WriteLine (mask);
		}
		private void Bitmask ()
		{
			OpCodeBitmask mask = new OpCodeBitmask ();
			
			mask.Set (Code.Starg);
			mask.Set (Code.Starg_S);
			mask.Set (Code.Stloc_0);
			mask.Set (Code.Stloc_1);
			mask.Set (Code.Stloc_2);
			mask.Set (Code.Stloc_3);
			mask.Set (Code.Stloc);
			mask.Set (Code.Stloc_S);
			mask.Set (Code.Stfld);
			mask.Set (Code.Stsfld);
			
			Console.WriteLine (mask);
		}
		public void Bitmask ()
		{
			OpCodeBitmask branches = new OpCodeBitmask ();
			branches.Set (Code.Brfalse);
			branches.Set (Code.Brfalse_S);
			branches.Set (Code.Brtrue);
			branches.Set (Code.Brtrue_S);
			branches.Set (Code.Bne_Un);
			branches.Set (Code.Bne_Un_S);
			branches.Set (Code.Beq);
			branches.Set (Code.Beq_S);
			Console.WriteLine (branches);
		}
예제 #55
0
 private static OpCodeBitmask ConditionsBitmask()
 {
     OpCodeBitmask mask = new OpCodeBitmask ();
     mask.Set (Code.Brtrue);
     mask.Set (Code.Brtrue_S);
     mask.Set (Code.Brfalse);
     mask.Set (Code.Brfalse_S);
     Console.WriteLine ("ConditionsBitmask : " + mask);
     return mask;
 }
		public static void GenerateBitmasks ()
		{
			OpCodeBitmask bitmask;
			
			bitmask = new OpCodeBitmask ();
			bitmask.Set (Code.Stfld);
			bitmask.Set (Code.Stsfld);
			bitmask.Set (Code.Call);
			bitmask.Set (Code.Callvirt);
			applicable_method_bitmask = bitmask;
			Console.WriteLine ("applicable_method_bitmask: {0}", bitmask);
			
			bitmask = new OpCodeBitmask ();
			// a list of safe instructions, which under no circumstances
			// (for verifiable code) can cause an exception to be raised.
			bitmask = new OpCodeBitmask ();
			bitmask.Set (Code.Nop);
			bitmask.Set (Code.Ret);
			bitmask.Set (Code.Ldloc);
			bitmask.Set (Code.Ldloc_0);
			bitmask.Set (Code.Ldloc_1);
			bitmask.Set (Code.Ldloc_2);
			bitmask.Set (Code.Ldloc_3);
			bitmask.Set (Code.Ldloc_S);
			bitmask.Set (Code.Ldloca);
			bitmask.Set (Code.Ldloca_S);
			// This one is needed to load static fields for return values 
			// a method which returns IntPtr should be able to ldsfld IntPtr.Zero
			bitmask.Set (Code.Ldsfld); 
			// bitmask.Set (Code.Ldsflda); // Not quite sure about this one, leaving it out for now.
			bitmask.Set (Code.Leave);
			bitmask.Set (Code.Leave_S);
			bitmask.Set (Code.Endfilter);
			bitmask.Set (Code.Endfinally);
			bitmask.Set (Code.Ldc_I4);
			bitmask.Set (Code.Ldc_I4_0);
			bitmask.Set (Code.Ldc_I4_1);
			bitmask.Set (Code.Ldc_I4_2);
			bitmask.Set (Code.Ldc_I4_3);
			bitmask.Set (Code.Ldc_I4_4);
			bitmask.Set (Code.Ldc_I4_5);
			bitmask.Set (Code.Ldc_I4_6);
			bitmask.Set (Code.Ldc_I4_7);
			bitmask.Set (Code.Ldc_I4_8);
			bitmask.Set (Code.Ldc_I4_M1);
			bitmask.Set (Code.Ldc_I8);
			bitmask.Set (Code.Ldc_R4);
			bitmask.Set (Code.Ldc_R8);
			bitmask.Set (Code.Ldarg);
			bitmask.Set (Code.Ldarg_0);
			bitmask.Set (Code.Ldarg_1);
			bitmask.Set (Code.Ldarg_2);
			bitmask.Set (Code.Ldarg_3);
			bitmask.Set (Code.Ldarg_S);
			bitmask.Set (Code.Stloc);
			bitmask.Set (Code.Stloc_0);
			bitmask.Set (Code.Stloc_1);
			bitmask.Set (Code.Stloc_2);
			bitmask.Set (Code.Stloc_3);
			bitmask.Set (Code.Stloc_S);
			bitmask.Set (Code.Stobj); /* Can throw TypeLoadException. This is required to properly initialize out parameters which are ValueTypes */
			bitmask.Set (Code.Ldnull);
			bitmask.Set (Code.Initobj);
			bitmask.Set (Code.Pop);
			// The stind* instructions can raise an exception:
			// "NullReferenceException is thrown if addr is not naturally aligned for the argument type implied by the instruction suffix."
			// Not sure how we can verify that the alignment is correct, and this instruction is emitted by the compiler for byref/out parameters.
			// Not marking this instructions as safe would mean that it would be impossible to fix a delegate that takes 
			// a byref/out parameter.
			bitmask.Set (Code.Stind_I);
			bitmask.Set (Code.Stind_I1);
			bitmask.Set (Code.Stind_I2);
			bitmask.Set (Code.Stind_I4);
			bitmask.Set (Code.Stind_I8);
			bitmask.Set (Code.Stind_R4);
			bitmask.Set (Code.Stind_R8);
			bitmask.Set (Code.Stind_Ref);
			safe_instructions = bitmask;
			Console.WriteLine ("safe_instructions: {0}", bitmask);			
		}
		public void Bitmask ()
		{
			OpCodeBitmask unbox = new OpCodeBitmask ();
			unbox.Set (Code.Unbox);
			unbox.Set (Code.Unbox_Any);
			Console.WriteLine (unbox);
		}