Ejemplo n.º 1
0
        private DataModel ApplyFileData(Dom.Action action, Data data)
        {
            byte[] fileBytes = null;

            for (int i = 0; i < 5 && fileBytes == null; ++i)
            {
                try
                {
                    fileBytes = File.ReadAllBytes(data.FileName);
                }
                catch (Exception ex)
                {
                    logger.Debug("Failed to open '{0}'. {1}", data.FileName, ex.Message);
                }
            }

            if (fileBytes == null)
            {
                throw new CrackingFailure(null, null);
            }

            // Note: We need to find the origional data model to use.  Re-using
            // a data model that has been cracked into will fail in odd ways.
            var dataModel = GetNewDataModel(action);

            // Crack the file
            DataCracker cracker = new DataCracker();

            cracker.CrackData(dataModel, new BitStream(fileBytes));

            return(dataModel);
        }
Ejemplo n.º 2
0
        protected void Action_Finished(Dom.Action action)
        {
            var dom = action.parent.parent.parent as Dom.Dom;

            if (action.dataModel == null)
            {
                var models = action.parameters.Select(a => a.dataModel).Where(a => a != null);
                if (!models.Any())
                {
                    return;
                }

                foreach (var model in models)
                {
                    SaveDataModel(dom, model);
                }
            }
            else
            {
                SaveDataModel(dom, action.dataModel);
            }

            if (cloneActions)
            {
                actions.Add(ObjectCopier.Clone(action));
            }
            else
            {
                actions.Add(action);
            }
        }
Ejemplo n.º 3
0
        private void SyncDataSet(Dom.Action action)
        {
            System.Diagnostics.Debug.Assert(_iteration != 0);

            // Compute the iteration we need to switch on
            uint switchIteration = GetSwitchIteration();

            foreach (var item in action.outputData)
            {
                // Note: use the model name, not the instance name so
                // we only set the data set once for re-enterant states.
                var modelName = item.modelName;

                if (!_dataSets.Contains(modelName))
                {
                    return;
                }

                var val = _dataSets[modelName];

                // If the last switch was within the current iteration range then we don't have to switch.
                if (switchIteration == val.Iteration)
                {
                    return;
                }

                // Don't switch files if we are only using a single file :)
                if (val.Options.Count < 2)
                {
                    return;
                }

                do
                {
                    var opt = _randomDataSet.Choice(val.Options);

                    try
                    {
                        // Apply the data set option
                        item.Apply(opt);

                        // Save off the last switch iteration
                        val.Iteration = switchIteration;

                        // Done!
                        return;
                    }
                    catch (PeachException ex)
                    {
                        logger.Debug(ex.Message);
                        logger.Debug("Unable to apply data '{0}', removing from sample list.", opt.name);
                        val.Options.Remove(opt);
                    }
                }while (val.Options.Count > 0);

                throw new PeachException("Error, RandomStrategy was unable to apply data for \"" + item.dataModel.fullName + "\"");
            }
        }
Ejemplo n.º 4
0
        private DataModel AppleFieldData(Dom.Action action, Data data)
        {
            // Note: We need to find the origional data model to use.  Re-using
            // a data model that has been cracked into will fail in odd ways.
            var dataModel = GetNewDataModel(action);

            // Apply the fields
            data.ApplyFields(dataModel);

            return(dataModel);
        }
Ejemplo n.º 5
0
 void Action_Starting(Dom.Action action)
 {
     foreach (var item in action.outputData)
     {
         var elem = item.dataModel.find(parent.fullName);
         if (elem != null)
         {
             elem.Invalidate();
             GetRandom(elem, action, true);
         }
     }
 }
Ejemplo n.º 6
0
        void Action_Starting(Dom.Action action)
        {
            if (action.type != ActionType.Output)
            {
                return;
            }

            var elem = action.dataModel.find(parent.fullName);

            if (elem != null)
            {
                elem.Invalidate();
                GetRandom(elem, action, true);
            }
        }
Ejemplo n.º 7
0
        protected override void Action_Finished(Dom.Action action)
        {
            var rec = states.Last().actions.Last();

            if (rec.models == null)
            {
                return;
            }

            foreach (var model in rec.models)
            {
                if (model.mutations.Count == 0)
                {
                    model.mutations = null;
                }
            }
        }
Ejemplo n.º 8
0
        private DataModel GetNewDataModel(Dom.Action action)
        {
            var referenceName = action.dataModel.referenceName;

            if (referenceName == null)
            {
                referenceName = action.dataModel.name;
            }

            var sm = action.parent.parent;

            Dom.Dom dom = _context.dom;

            int i = sm.name.IndexOf(':');

            if (i > -1)
            {
                string prefix = sm.name.Substring(0, i);

                Dom.Dom other;
                if (!_context.dom.ns.TryGetValue(prefix, out other))
                {
                    throw new PeachException("Unable to locate namespace '" + prefix + "' in state model '" + sm.name + "'.");
                }

                dom = other;
            }

            // Need to take namespaces into account when searching for the model
            var baseModel = dom.getRef <DataModel>(referenceName, a => a.dataModels);

            var dataModel = baseModel.Clone() as DataModel;

            dataModel.isReference   = true;
            dataModel.referenceName = referenceName;

            return(dataModel);
        }
Ejemplo n.º 9
0
        protected void Action_Finished(Dom.Action action)
        {
            if (!action.allData.Any())
            {
                return;
            }

            var dom = action.parent.parent.parent as Dom.Dom;

            foreach (var item in action.allData)
            {
                SaveDataModel(dom, item.dataModel);
            }

            if (cloneActions)
            {
                actions.Add(ObjectCopier.Clone(action));
            }
            else
            {
                actions.Add(action);
            }
        }
Ejemplo n.º 10
0
        static Variant GetRandom(DataElement elem, Dom.Action action, bool update)
        {
            Dom.Number num = elem as Dom.Number;
            if (num == null && !(elem is Dom.String && elem.Hints.ContainsKey("NumericalString")))
            {
                throw new PeachException("SequenceRandomFixup has non numeric parent '" + elem.fullName + "'.");
            }

            string key = "SequenceRandomFixup." + elem.fullName;

            Dom.Dom dom = action.parent.parent.parent as Dom.Dom;
            object  obj;

            if (!dom.context.iterationStateStore.TryGetValue(key, out obj))
            {
                obj = elem.DefaultValue;
            }

            Variant var = obj as Variant;

            System.Diagnostics.Debug.Assert(var != null);

            if (!update)
            {
                return(var);
            }

            Random rng = (Random)dom.context.iterationStateStore["SequenceRandomFixup"];

            dynamic random;

            if (num != null)
            {
                if (num.Signed)
                {
                    if (num.MaxValue == long.MaxValue)
                    {
                        random = rng.NextInt64();
                    }
                    else
                    {
                        random = rng.Next((long)num.MinValue, (long)num.MaxValue + 1);
                    }
                }
                else
                {
                    if (num.MaxValue == ulong.MaxValue)
                    {
                        random = rng.NextUInt64();
                    }
                    else
                    {
                        random = rng.Next((ulong)num.MinValue, (ulong)num.MaxValue + 1);
                    }
                }
            }
            else
            {
                random = rng.NextInt32();
            }

            var = new Variant(random);
            dom.context.iterationStateStore[key] = var;
            return(var);
        }
Ejemplo n.º 11
0
 void Action_Starting(Dom.Action action)
 {
     Assert.AreEqual(false, started);
     started = true;
 }
Ejemplo n.º 12
0
 void Action_Finished(Dom.Action action)
 {
     Assert.AreEqual(false, finished);
     finished = true;
 }
Ejemplo n.º 13
0
        private void SyncDataSet(Dom.Action action)
        {
            System.Diagnostics.Debug.Assert(_iteration != 0);

            // Only sync <Data> elements if the action has a data model
            if (action.dataModel == null)
            {
                return;
            }

            string         key = GetDataModelName(action);
            DataSetTracker val = null;

            if (!_dataSets.TryGetValue(key, out val))
            {
                return;
            }

            // If the last switch was within the current iteration range then we don't have to switch.
            uint switchIteration = GetSwitchIteration();

            if (switchIteration == val.iteration)
            {
                return;
            }

            // Don't switch files if we are only using a single file :)
            if (val.options.Count < 2)
            {
                return;
            }

            DataModel dataModel = null;

            // Some of our sample files may not crack.  Loop through them until we
            // find a good sample file.
            while (val.options.Count > 0 && dataModel == null)
            {
                Data option = _randomDataSet.Choice(val.options);

                if (option.DataType == DataType.File)
                {
                    try
                    {
                        dataModel = ApplyFileData(action, option);
                    }
                    catch (CrackingFailure)
                    {
                        logger.Debug("Removing " + option.FileName + " from sample list.  Unable to crack.");
                        val.options.Remove(option);
                    }
                }
                else if (option.DataType == DataType.Fields)
                {
                    try
                    {
                        dataModel = AppleFieldData(action, option);
                    }
                    catch (PeachException)
                    {
                        logger.Debug("Removing " + option.name + " from sample list.  Unable to apply fields.");
                        val.options.Remove(option);
                    }
                }
            }

            if (dataModel == null)
            {
                throw new PeachException("Error, RandomStrategy was unable to load data for model \"" + action.dataModel.fullName + "\"");
            }

            // Set new data model
            action.dataModel = dataModel;

            // Generate all values;
            var ret = action.dataModel.Value;

            System.Diagnostics.Debug.Assert(ret != null);

            // Store copy of new origional data model
            action.origionalDataModel = action.dataModel.Clone() as DataModel;

            // Save our current state
            val.iteration = switchIteration;
        }