Example #1
0
        public DynamicGenerator(int seed, ModuleDefinition mod, bool useMath = false)
        {
            rand = new Random(seed);

            Module = mod;

            injection = AssemblyDefinition.ReadAssembly(typeof(Iid).Assembly.Location);
            injection.MainModule.ReadSymbols();

            ExpGen  = new ExpressionGenerator(rand.Next(500000), Module);
            MathGen = new MathGenerator(rand.Next(500000), Module);

            emit = Instruction.Create(OpCodes.Call, Module.Import(typeof(System.Reflection.Emit.ILGenerator).GetMethod("Emit", new Type[] {
                typeof(System.Reflection.Emit.OpCode)
            })));

            int expNum = rand.Next(5, 50);

            for (int i = 0; i < expNum; i++)
            {
                ExpGen = new ExpressionGenerator(rand.Next(50000), Module);
                Expression ex = ExpGen.Generate(rand.Next(5, 14));

                int        evald = ExpressionEvaluator.Evaluate(ex, 5);
                Expression exR   = ExpressionInverser.InverseExpression(ex);

                CreateDynamic(ex, exR, 5, evald, useMath);
            }
        }
Example #2
0
        public StringGenerator(int seed, ModuleDefinition mod)
        {
            rand = new Random(seed);

            Module = mod;

            LengthCall  = Instruction.Create(OpCodes.Call, Module.Import(typeof(String).GetMethod("get_Length", Type.EmptyTypes)));
            StringEmpty = Instruction.Create(OpCodes.Ldsfld, Module.Import(typeof(String).GetField("Empty")));

            ExpGen  = new ExpressionGenerator(rand.Next(500000), Module);
            MathGen = new MathGenerator(rand.Next(500000), Module);
            DynGen  = new DynamicGenerator(rand.Next(500000), Module);
        }
Example #3
0
        //-----------------------------------------------------------

        public static void init( )
        {
            last_label_no         = 0;
            last_temp_no_internal = 0;
            current_context       = null;
            current_program_unit  = null;
            current_routine_decl  = null;

            string suffix = "";
            string output = "";

            if (options.Output != null)
            {
                output = options.Output.Substring(0);
            }
            else if (options.OutputAssembly != null)
            {
                output = options.OutputAssembly.Substring(0);
            }

            int slash_index = output.LastIndexOf('/');

            if (slash_index == -1)
            {
                slash_index = output.LastIndexOf('\\');
            }
            int dot_index = output.LastIndexOf('.');

            if (dot_index == -1)
            {
                dot_index = output.Length;
            }
            if (slash_index == -1)
            {
                suffix = output.Substring(slash_index + 1, dot_index);
            }
            else
            {
                suffix = output.Substring(slash_index + 1, dot_index - slash_index + 1);
            }
            suffix.Replace("-", "_minus_");

            globalMath         = new MathGenerator(suffix);
            _kernelRegistry    = new KernelRegistry();
            _operationRegistry = new OperationRegistry();
            // current_namespace_decl = null;

            globalTree = null; //-- do not do: it's required afterwards for reporting
        }
Example #4
0
    void Awake()
    {
        //Our singleton pattern
        if (instance != null && instance != this)
        {
            // destroy the gameobject if an instance of this exist already
            Destroy(gameObject);
        }
        else
        {
            //Set our instance to this object/instance
            instance = this;
        }

        generator = GetComponent <MathGenerator>();
        questionGO.SetActive(false);
    }
Example #5
0
        public void Mutate(Random rand, MethodBody body, ModuleDefinition mod)
        {
            
            MathGenerator mg = new MathGenerator(Environment.TickCount, mod);
            body.SimplifyMacros();
            ILProcessor ilp = body.GetILProcessor();

            //foreach (var i in body.Instructions)
            for (int x = 0; x < body.Instructions.Count; x++)
            {
                Instruction i = body.Instructions[x];
                FieldReference field = i.Operand as FieldReference;
                if (field != null && field.DeclaringType.FullName == "Mutation")
                {
                    switch (field.Name)
                    {
                        case "Key0I":
                            i.Operand = IntKeys[0]; goto case "I";
                        case "Key1I":
                            i.Operand = IntKeys[1]; goto case "I";
                        case "Key2I":
                            i.Operand = IntKeys[2]; goto case "I";
                        case "Key3I":
                            i.Operand = IntKeys[3]; goto case "I";
                        case "Key4I":
                            i.Operand = IntKeys[4]; goto case "I";
                        case "Key5I":
                            i.Operand = IntKeys[5]; goto case "I";
                        case "Key6I":
                            i.Operand = IntKeys[6]; goto case "I";
                        case "Key7I":
                            i.Operand = IntKeys[7]; goto case "I";

                        case "Key0L":
                            i.Operand = LongKeys[0]; goto case "L";
                        case "Key1L":
                            i.Operand = LongKeys[1]; goto case "L";
                        case "Key2L":
                            i.Operand = LongKeys[2]; goto case "L";
                        case "Key3L":
                            i.Operand = LongKeys[3]; goto case "L";
                        case "Key4L":
                            i.Operand = LongKeys[4]; goto case "L";
                        case "Key5L":
                            i.Operand = LongKeys[5]; goto case "L";
                        case "Key6L":
                            i.Operand = LongKeys[6]; goto case "L";
                        case "Key7L":
                            i.Operand = LongKeys[7]; goto case "L";

                        case "Key0S":
                            i.Operand = StringKeys[0]; goto case "S";
                        case "Key1S":
                            i.Operand = StringKeys[1]; goto case "S";
                        case "Key2S":
                            i.Operand = StringKeys[2]; goto case "S";
                        case "Key3S":
                            i.Operand = StringKeys[3]; goto case "S";

                        case "Key0Delayed":
                            if (IsDelayed)
                            {
                                i.Operand = DelayedKeys[0];
                                goto case "I";
                            }
                            else
                                Delayed0 = i;
                            break;
                        case "Key1Delayed":
                            if (IsDelayed)
                            {
                                i.Operand = DelayedKeys[1];
                                goto case "I";
                            }
                            else
                                Delayed1 = i;
                            break;


                        case "I":

                            i.OpCode = OpCodes.Ldc_I4;

                            int target = (int)i.Operand;

                            if (ObfuscationHelper.StringGen != null)
                            {
                                List<Instruction> keyInsts = new List<Instruction>();
                                if (ObfuscationHelper.StringGen.Module != mod)
                                {
                                    keyInsts = ObfuscationHelper.StringGen.GenerateLevels(body.Method, target, rand.Next(0, 2), 5, false, mod);
                                }
                                else
                                {
                                    keyInsts = ObfuscationHelper.StringGen.GenerateLevels(body.Method, target, rand.Next(0, 2), 5);
                                }

                                ilp.Replace(i, keyInsts[0]);

                                keyInsts = keyInsts.Skip(1).Reverse().ToList();
                                foreach (Instruction a in keyInsts)
                                {
                                    body.Instructions.Insert(x + 1, a);
                                }
                                x += keyInsts.Count;
                            }
                            else
                            {
                                List<Instruction> keyInsts = mg.GenerateLevels(target, OpCodes.Ldc_I4, rand.Next(0, 6));

                                ilp.Replace(i, keyInsts[0]);

                                keyInsts = keyInsts.Skip(1).Reverse().ToList();
                                foreach (Instruction a in keyInsts)
                                {
                                    body.Instructions.Insert(x + 1, a);
                                }
                                x += keyInsts.Count;
                            }

                            /*
                            List<Instruction> genInsts = mg.GenerateLevels(target, OpCodes.Ldc_I4, rand.Next(0, 6));

                            Instruction tmp = null;

                            foreach (Instruction a in genInsts)
                            {
                                if (genInsts[0] == a)
                                {
                                    // first statement
                                    ilp.Replace(i, a);
                                    tmp = a;
                                    continue;
                                }
                                ilp.InsertAfter(tmp, a);
                                tmp = a;
                            }
                            x += genInsts.Count;*/


                            break;
                        case "L":
                            i.OpCode = OpCodes.Ldc_I8; break;
                        case "S":
                            i.OpCode = OpCodes.Ldstr; break;
                    }
                }
                MethodReference method = i.Operand as MethodReference;
                if (method != null && method.DeclaringType.FullName == "Mutation")
                {
                    if (method.Name == "Placeholder")
                        Placeholder = i;
                    else if (method.Name == "DeclaringType")
                    {
                        i.OpCode = OpCodes.Ldtoken;
                        i.Operand = body.Method.DeclaringType;
                    }
                    else if (method.Name == "Method")
                    {
                        var str = (string)i.Previous.Operand;
                        i.OpCode = OpCodes.Ldtoken;
                        i.Operand = body.Method.DeclaringType.Methods.Single(m => m.Name == str);
                        i.Previous.OpCode = OpCodes.Nop;
                        i.Previous.Operand = null;
                    }
                    else if (method.Name == "Break")
                    {
                        i.OpCode = OpCodes.Break;
                        i.Operand = null;
                    }
                }
            }

            for (int i = 0; i < body.Variables.Count; i++)
            {
                int x = rand.Next(0, body.Variables.Count);
                var tmp = body.Variables[i];
                body.Variables[i] = body.Variables[x];
                body.Variables[x] = tmp;
            }

            int iteration = rand.Next(20, 35);
            while (iteration > 0)
            {
                MutateCode(rand, body);
                iteration--;
            }
        }