public override IClrObjMappingModel ReadEntity(ClrObject obj, ModelMapperFactory factory)
        {
            if (obj.IsNullObj)
            {
                return(null);
            }

            ClrObject keyClrObj = obj.GetRefFld("key");
            ClrObject valClrObj = obj.GetRefFld("val");

            if (keyClrObj.IsNullObj)
            {
                return(null);
            }

            IClrObjMappingModel keyObj = factory.BuildModel(keyClrObj);

            if (keyObj is EmptyClrObjectModel)
            {
                return(null);
            }

            IClrObjMappingModel valObj = factory.BuildModel(valClrObj);

            return(new DictionaryEntryModel(keyObj, valObj));
        }
        public static IDictionary <ClrObject, ClrObject> ProcessReferenceConcurrentDictionary(ClrObject dictionary)
        {
            var heap           = dictionary.Type.Heap;
            var m_bucketsArray = dictionary.GetRefFldChained(new[] { "m_tables", "m_buckets" }).Value;

            var totalElements = m_bucketsArray.Type.GetArrayLength(m_bucketsArray.Address);

            var result = new Dictionary <ClrObject, ClrObject>();

            for (int i = 0; i < totalElements; i++)
            {
                var nodeAddress = (ulong)m_bucketsArray.Type.GetArrayElementValue(m_bucketsArray.Address, i);

                var type = heap.GetObjectType(nodeAddress);

                if (type == null)
                {
                    continue;
                }
                var node = new ClrObject(nodeAddress, type);

                var key = node.GetRefFld("m_key");

                if (key.IsNullObj)
                {
                    continue;
                }
                var value = node.GetRefFld("m_value");

                result.Add(key, value);
            }

            return(result);
        }
Beispiel #3
0
        public static ClrObject GetRefFldOrAutoProperty(this ClrObject clrObject, string fieldName)
        {
            if (clrObject.HasSameNameField(fieldName))
            {
                return(clrObject.GetRefFld(fieldName));
            }

            var autoProp = MemoryDiagnostics.StringUtil.ProduceAutoPropertyName(fieldName);

            return(clrObject.GetRefFld(autoProp));
        }
Beispiel #4
0
        private static ClrObject ExtractCacheManager(ClrObject cacheManagerLazyResetable)
        {
            var type = cacheManagerLazyResetable.Type;

            if (type.GetFieldByName("value") != null)
            {
                return(cacheManagerLazyResetable.GetRefFld("value"));
            }
            else if (type.GetFieldByName("m_boxed") != null)
            {
                var lazy = cacheManagerLazyResetable.GetRefFld("m_boxed");
                return(lazy.GetRefFld("m_value"));
            }
            throw new InvalidOperationException($"{cacheManagerLazyResetable.HexAddress} of {type.Name} has weird field layout so could not fetch data from it");
        }
        public static IClrObjMappingModel EnumerateList(ClrObject clrObject, ModelMapperFactory factory)
        {
            var result = new ArrayMappingModel
            {
                Obj = clrObject
            };
            ClrObject fld = clrObject.GetRefFld("_items");

            if (fld.Type == null)
            {
                return(result);
            }
            ClrType tp = fld.Type.ComponentType;

            if ((!tp.IsValueClass) && (!tp.IsString))
            {
                // TODO: add support of reading strings.
                List <ClrObject> enumeration = ClrCollectionHelper.EnumerateListOfRefTypes(clrObject);
                foreach (ClrObject o in enumeration)
                {
                    IClrObjMappingModel model = factory.BuildModel(o);
                    result.Elements.Add(model);
                }
            }

            return(result);
        }
Beispiel #6
0
        public static IDList Read(ClrObject obj)
        {
            if (obj.IsNullObj)
            {
                return(null);
            }

            ClrObject idsRef = obj.GetRefFld("m_ids");

            if (idsRef.IsNullObj)
            {
                return(null);
            }

            List <ClrObject> ids = idsRef.Type?.Name?.Contains("ArrayList") == true?ClrCollectionHelper.EnumerateArrayList(idsRef) :
                                       ClrCollectionHelper.EnumerateListOfRefTypes(idsRef);

            var res = new IDList();

            foreach (ClrObject id in ids)
            {
                res.Add(IDReader.Read(id));
            }

            return(res);
        }
Beispiel #7
0
        public override IClrObjMappingModel ReadEntity(ClrObject obj, ModelMapperFactory factory)
        {
            ClrObject fld = obj.GetRefFld(entriesTableFldName);
            List <Tuple <ClrObject, ClrObject> > values = ClrCollectionHelper.EnumerateHashtable(fld);

            var hashtable = new Hashtable(values.Count);

            foreach (var keyValuePair in values)
            {
                ClrObject val = keyValuePair.Item2;
                if (val.IsNullObj)
                {
                    continue;
                }


                var       key   = val.GetStringFld("Key");
                ClrObject value = val.GetRefFld("Value");

                if (key == null || value.IsNullObj)
                {
                    continue;
                }

                IClrObjMappingModel valModel = factory.BuildModel(value);
                hashtable.Add(key, valModel);
            }

            var hashTableMapping = new HashtableMappingModel(hashtable, obj);

            return(hashTableMapping);
        }
        public override IClrObjMappingModel ReadEntity(ClrObject obj, ModelMapperFactory factory)
        {
            // TODO: remove the hack
            var fld = obj.GetRefFld("<Values>k__BackingField");

            if (fld.IsNullObj)
            {
                return(null);
            }
            var values = factory.BuildModel(fld) as HashtableMappingModel;

            if (values == null)
            {
                return(null);
            }

            var rs = new SitecoreMVCRenderingPropertiesModel
            {
                Obj              = obj,
                Cacheable        = string.Equals(values[@"Cacheable"] as string, "1", StringComparison.OrdinalIgnoreCase),
                Cache_VaryByData = string.Equals(values[@"Cache_VaryByData"] as string, "1", StringComparison.OrdinalIgnoreCase),
                CacheKey         = values[@"CacheKey"] as string ?? "[NoCacheKey]"
            };

            ID.TryParse(values[@"RenderingItemPath"], out rs.RenderingItemPath);
            return(rs);
        }
Beispiel #9
0
        public override IClrObjMappingModel ReadEntity(ClrObject obj, ModelMapperFactory factory)
        {
            var field = obj.GetRefFld("_data");
            IClrObjMappingModel safeDictionary;

            if (!factory.CollectionEnumerator.TryProcessSpecialCollection(field, out safeDictionary))
            {
                return(null);
            }

            var casted = safeDictionary as HashtableMappingModel;

            if (casted == null)
            {
                return(null);
            }

            var model = new ArrayMappingModel();

            foreach (IClrObjMappingModel key in casted.Elements.Keys)
            {
                model.AddElement(key);
            }

            return(model);
        }
Beispiel #10
0
        public static Dictionary <ClrObject, ClrObject> ReadClrObjects(ClrObject obj)
        {
            if (obj.Type == null)
            {
                return(new Dictionary <ClrObject, ClrObject>());
            }

            ClrObject buckets = obj.GetRefFld("buckets", false);

            if (buckets.Type == null)
            {
                return(new Dictionary <ClrObject, ClrObject>());
            }

            List <Tuple <ClrObject, ClrObject> > enumerated = ClrCollectionHelper.EnumerateHashtable(obj);

            var dict = new Dictionary <ClrObject, ClrObject>(enumerated.Count);

            foreach (var tuple in enumerated)
            {
                dict.Add(tuple.Item1, tuple.Item2);
            }

            return(dict);
        }
Beispiel #11
0
        public override bool SupportTransformation([ClrObjAndTypeNotEmpty] ClrObject set)
        {
            if (!base.SupportTransformation(set) || !set.Type.Name.Contains("Sitecore.Collections.ConcurrentSet"))
            {
                return(false);
            }

            var dictionary     = set.GetRefFld("container");
            var m_bucketsArray = dictionary.GetRefFld("m_tables").GetRefFld("m_buckets");

            if (m_bucketsArray.Type == null)
            {
                return(false);
            }

            var mBucketType = m_bucketsArray.Type;

            if (mBucketType.IsArray)
            {
                var keyType = mBucketType.ComponentType.Fields.Where(f => f.Name == "m_key").FirstOrDefault();

                return(keyType?.IsObjectReference == true);
            }

            return(false);
        }
        public static bool IsGenericList(ClrObject clrObject)
        {
            if ((clrObject.Type == null) || (!clrObject.Type.Name.Contains("System.Collections.Generic.List")))
            {
                return(false);
            }

            ClrObject fld = clrObject.GetRefFld("_items");

            return((fld.Type != null) && (fld.Type.IsArray));
        }
        /// <summary>
        ///   Reads the identifier field.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <param name="fldName">Name of the field to read <see cref="ID" /> from.</param>
        /// <returns></returns>
        public static ID ReadIdField(ClrObject obj, string fldName)
        {
            if (obj.IsNullObj)
            {
                return(null);
            }

            ClrObject sitecoreIDref = obj.GetRefFld(fldName, false);

            return(Read(sitecoreIDref));
        }
Beispiel #14
0
        public override IClrObjMappingModel ReadEntity(ClrObject obj, [NotNull] ModelMapperFactory factory)
        {
            Assert.ArgumentNotNull(factory, "factory");

            if (obj.IsNullObj)
            {
                return(EmptyClrObjectModel.Instance);
            }

            var result = new EventHandlerModel
            {
                Obj    = obj,
                target = obj.GetRefFld("_target")
            };

            ClrObject invocationList = obj.GetRefFld(@"_invocationList");

            if (invocationList.IsNullObj)
            {
                // Not multicast delegate
                ClrMethod methodDescriptor;

                if (!TryReadMethodPtr(obj, out methodDescriptor))
                {
                    if (!TryResolveMethodFromTarget(obj, out methodDescriptor))
                    {
                        return(result);
                    }
                }

                result.Info = methodDescriptor;
                return(result);
            }

            // Multicast Delegate
            // TODO: Consider changing ArrayMapping
            result.InvokationList = factory.BuildModel(invocationList) as ArrayMappingModel;

            return(result);
        }
        public override void Compute()
        {
            ClrAssert.ObjectNotNullTypeNotEmpty(this.Obj);

            this.Value = Obj.GetStringFld(fieldName: "_stringValue") ?? string.Empty;

            ClrObject multi = Obj.GetRefFld("_multiValue");

            if (multi.IsNullObj)
            {
                return;
            }

            ClrObject arrayList = multi.GetRefFld("_entriesArray");

            if (arrayList.IsNullObj)
            {
                return;
            }

            List <ClrObject> buckets = ClrCollectionHelper.EnumerateArrayList(arrayList);

            foreach (ClrObject bucket in buckets)
            {
                bucket.ReEvaluateType();
                ClrObject val = bucket.GetRefFld("Value");
                val.ReEvaluateType();
                if (val.IsNullObj)
                {
                    continue;
                }

                List <ClrObject> nestedValues = ClrCollectionHelper.EnumerateArrayList(val);
                foreach (ClrObject nestedValue in nestedValues)
                {
                    nestedValue.ReEvaluateType();
                    if (nestedValue.IsNullObj || (nestedValue.Type == null))
                    {
                        continue;
                    }

                    object tmpvalue = nestedValue.Type.GetValue(nestedValue.Address);
                    if (tmpvalue is string)
                    {
                        this.Value += tmpvalue;
                    }
                }
            }

            // var buckets = ClrCollectionHelper.EnumerateArrayOfRefTypes(arrayList);
            // buckets.
        }
Beispiel #16
0
        public override IClrObjMappingModel ReadEntity(ClrObject obj, ModelMapperFactory factory)
        {
            ClrObject hashtable = obj.GetRefFld("_hashtable");

            if (hashtable.IsNullObj)
            {
                return(null);
            }

            return(this._hashtableReader.SupportTransformation(hashtable) ? this._hashtableReader.ReadEntity(hashtable, factory) : null);

            // TODO : change in future.
        }
        public override IClrObjMappingModel ReadEntity(ClrObject obj, ModelMapperFactory factory)
        {
            var keys = obj.GetRefFld("items");

            if (keys.IsNullObj)
            {
                return(new ArrayMappingModel()
                {
                    Obj = obj
                });
            }
            return(GenericListReader.EnumerateList(keys, factory));
        }
Beispiel #18
0
        public override IClrObjMappingModel ReadEntity(ClrObject clrObject, ModelMapperFactory factory)
        {
            var _itemsRef = clrObject.GetRefFld("_items");

            if (_itemsRef.Type == null)
            {
                return new ArrayMappingModel
                       {
                           Obj = clrObject
                       }
            }
            ;
            return(base.ReadEntity(_itemsRef, factory));
        }
Beispiel #19
0
        private bool TryResolveMethodFromTarget(ClrObject obj, out ClrMethod methodDescriptor)
        {
            methodDescriptor = null;
            var target     = obj.GetRefFld("_target");
            var _methodPtr = (ulong)(long)obj.GetSimpleFld <object>("_methodPtr");

            // TODO: Handle empty types types ?
            if (_methodPtr == 0 || target.IsNullObj || target.Type == null)
            {
                return(false);
            }
            methodDescriptor = target.Type.Methods.FirstOrDefault(methodInfo => methodInfo.NativeCode == _methodPtr);

            return(methodDescriptor != null);
        }
Beispiel #20
0
        public static IDList Read(ClrObject obj, string fieldName)
        {
            if (obj.IsNullObj)
            {
                return(null);
            }

            if (obj.Type == null)
            {
                return(null);
            }

            ClrObject listPointer = obj.GetRefFld(fieldName);

            return(Read(listPointer));
        }
Beispiel #21
0
        public static Dictionary <ClrObject, ClrObject> Read(ClrObject obj, string fldName)
        {
            if (obj.IsNullObj)
            {
                return(null);
            }

            obj.ReEvaluateType();
            if (obj.Type == null)
            {
                return(null);
            }

            ClrObject fld = obj.GetRefFld(fldName);

            return(Read(fld));
        }
Beispiel #22
0
        public static Dictionary <ClrObject, ClrObject> Read(ClrObject obj)
        {
            if (obj.IsNullObj)
            {
                return(null);
            }

            obj.ReEvaluateType();
            if (obj.Type == null)
            {
                return(null);
            }

            ClrObject hashtable = obj.GetRefFld("_hashtable");

            return(HashtableReader.ReadClrObjects(hashtable));
        }
        protected virtual bool MapFieldValueToModel(FieldInfo fieldInfo, ClrObject clrObject, IClrObjMappingModel model)
        {
            object val;

            bool read = this.ReadFieldValue(fieldInfo, clrObject, out val);

            if (read && (val != null))
            {
                if (val is RecursionDetectedModel)
                {
                    ClrObject value = clrObject.GetRefFld(fieldInfo.Name);

                    this.InvokeOnLeavingRecursion(value.Address, o => fieldInfo.SetValue(model, o));
                }
                else
                {
                    if (val is EmptyClrObjectModel)
                    {
#if TRACE
                        if (val.GetType().IsInstanceOfType(typeof(NoConverterForType)))
                        {
                            Trace.TraceInformation("Converter for {0} field of {1} object was not found", fieldInfo.Name, clrObject.Address.ToString("x8"));
                            if (Debugger.IsAttached)
                            {
                                Debugger.Break();
                            }
                        }
#endif
                        if (fieldInfo.FieldType.IsInstanceOfType(val))
                        {
                            fieldInfo.SetValue(model, val);
                        }

                        return(true);
                    }

                    fieldInfo.SetValue(model, val);
                }
            }

            return(read);
        }
Beispiel #24
0
        public override IClrObjMappingModel ReadEntity(ClrObject set, ModelMapperFactory factory)
        {
            var       heap      = set.Type.Heap;
            ClrObject container = set.GetRefFld("container");

            var m_bucketsArray = container.GetRefFld("m_tables").GetRefFld("m_buckets");

            var totalElements = m_bucketsArray.Type.GetArrayLength(m_bucketsArray.Address);

            var result = new ArrayMappingModel
            {
                Obj = set
            };

            for (int i = 0; i < totalElements; i++)
            {
                var nodeAddress = (ulong)m_bucketsArray.Type.GetArrayElementValue(m_bucketsArray.Address, i);

                if (nodeAddress == 0)
                {
                    continue;
                }

                var type = heap.GetObjectType(nodeAddress);

                if (type == null)
                {
                    continue;
                }
                var node = new ClrObject(nodeAddress, type);

                var key = node.GetRefFld("m_key");
                if (!key.IsNullObj)
                {
                    result.AddElement(factory.BuildModel(key));
                }
            }

            return(result);
        }
Beispiel #25
0
        public override IEnumerable <ClrObject> EnumerateObjectsFromSource([NotNull] ClrRuntime runtime)
        {
            var cacheManagerLazyResetableStream = base.EnumerateObjectsFromSource(runtime);

            foreach (var cacheManagerLazyResetable in cacheManagerLazyResetableStream)
            {
                ClrObject cacheManager = ExtractCacheManager(cacheManagerLazyResetable);
                if (cacheManager.Type?.GetFieldByName("cacheReferences") != null)
                {
                    var cacheReferences = cacheManager.GetRefFld("cacheReferences");
                    var cachesByNames   = (IEnumerable)modelMapper.BuildModel(cacheReferences);

                    foreach (DictionaryEntry cacheByNameModel in cachesByNames)
                    {
                        var cachesRegisteredByName = (IEnumerable)cacheByNameModel.Value;
                        foreach (IClrObjMappingModel cache in cachesRegisteredByName)
                        {
                            yield return(cache.Obj);
                        }
                    }
                }
            }
        }
Beispiel #26
0
        public override IClrObjMappingModel ReadEntity(ClrObject obj, ModelMapperFactory factory)
        {
            ClrObject tablesFld = obj.GetRefFld("m_tables");

            ClrObject bucketsFld = tablesFld.GetRefFld("m_buckets");
            ClrType   type       = bucketsFld.Type;
            ClrHeap   heap       = type.Heap;

            IEnumerable <ClrObject> values = ClrCollectionHelper.EnumerateArrayOfRefTypes(bucketsFld).Where(t => !t.IsNullObj);


            ClrInstanceField valueField = type.ComponentType.GetFieldByName("m_value");

            ClrInstanceField keyField = type.ComponentType.GetFieldByName("m_key");
            var hashtableModel        = new HashtableMappingModel
            {
                Obj = obj
            };

            foreach (ClrObject val in values)
            {
                ClrObject keyObj = val.GetRefFld("m_key");
                if (keyObj.IsNullObj)
                {
                    continue;
                }

                ClrObject valObj = val.GetRefFld("m_value");

                hashtableModel.Elements.Add(
                    key: factory.BuildModel(keyObj),
                    value: factory.BuildModel(valObj));
            }

            return(hashtableModel);
        }
        public override IClrObjMappingModel ReadEntity(ClrObject obj, ModelMapperFactory factory)
        {
            ClrObject _items = obj.GetRefFld("_items");

            return(_reader.SupportTransformation(_items) ? _reader.ReadEntity(_items, factory) : null);
        }
        /// <summary>
        ///   <paramref name="sourceObj" /> is expected to be a plain object - not collection.
        /// </summary>
        /// <param name="targetSetFldInfo">The field information.</param>
        /// <param name="sourceObj">The color object.</param>
        /// <param name="val">The value.</param>
        /// <returns><c>true</c> if field read correctly;<c>false</c> otherwise.</returns>
        protected virtual bool ReadFieldValue(
            [NotNull] FieldInfo targetSetFldInfo,
            [CanBeNull] ClrObject sourceObj,
            [CanBeNull] out object val)
        {
            Assert.ArgumentNotNull(targetSetFldInfo, "targetSetFldInfo");

            val = null;

            string fldName = targetSetFldInfo.Name;

            try
            {
                if (targetSetFldInfo.FieldType == typeof(string))
                {
                    val = sourceObj.GetStringSafe(fldName);
                    return(true);
                }

                // F.e. Int, float ...
                if (targetSetFldInfo.FieldType.IsPrimitive)
                {
                    // TODO: NMI test. Migration to newer CLR
                    var fld = sourceObj.Type.GetFieldByName(fldName);
                    val = fld.GetValue(sourceObj.Address);

                    // val = sourceObj.Type.GetFieldValue(sourceObj.Address, new[] { fldName });
                    // = sourceObj.Type.GetTypeToMapOn(sourceObj.GetValueFld(fldName).Address);
                    return(true);
                }

                if (targetSetFldInfo.FieldType.IsEnum)
                {
                    // TODO: NMI test. Migration to newer CLR
                    var fld = sourceObj.Type.GetFieldByName(fldName);
                    val = fld.GetValue(sourceObj.Address);
                    return(true);

                    // return sourceObj.Type.TryGetFieldValue(sourceObj.Address, new[] { fldName }, out val);
                }

                // Try read primitive types like Guid, ID, DateTime .
                // And return read value as an instance of primitive type.
                if (this.PrimitiveKnownTypesFieldRdr.TryCalcFieldValue(targetSetFldInfo, sourceObj, out val))
                {
                    return(true);
                }

                // Read pointer for the object reference
                ClrObject value = sourceObj.GetRefFld(fldName);

                // Read value through recursion.
                val = this.BuildModel(value);

                return(true);
            }
            catch (Exception ex)
            {
                // TODO: Consider returning true and an exception.
                return(false);
            }
        }
        public override IClrObjMappingModel ReadEntity(ClrObject obj, ModelMapperFactory factory)
        {
            var hashtableModel = new HashtableMappingModel
            {
                Obj = obj
            };

            ClrObject entriesField = obj.GetRefFld(entriesFldName);

            if (entriesField == null || entriesField.Type == null)
            {
                // Object contains this field, but value is null.
                // Most likely dictionary is either in the middle of construction, or destruction.
                // Anyway we can assume it is empty.
                return(hashtableModel);
            }

            ClrType type = entriesField.Type;

            ClrHeap heap = type.Heap;

            var componentType = type.ComponentType;

            if (componentType == null)
            {
                // TODO :Try reload type.
                return(null);
            }

            ClrInstanceField valueField = componentType.GetFieldByName("value");

            ClrInstanceField keyField = componentType.GetFieldByName("key");

            int len = type.GetArrayLength(entriesField.Address);

            ulong entriesFieldAddress = entriesField.Address;

            for (int i = 0; i < len; i++)
            {
                ulong addr = type.GetArrayElementAddress(entriesFieldAddress, i);

                if (addr == 0)
                {
                    continue;
                }
                try
                {
                    var key = keyField.GetValue(addr, true);
                    if (!(key is ulong))
                    {
                        continue;
                    }
                    var keyPointer = (ulong)key;
                    if (keyPointer == 0)
                    {
                        continue;
                    }

                    var val = valueField.GetValue(addr, true);

                    ulong valuePointer;
                    if (val is ulong)
                    {
                        valuePointer = (ulong)val;
                    }
                    else
                    {
                        valuePointer = 0;
                    }

                    var keyObj = new ClrObject(keyPointer, heap);
                    var valObj = new ClrObject(valuePointer, heap);

                    hashtableModel.Elements.Add(factory.BuildModel(keyObj),
                                                factory.BuildModel(valObj));
                }
                catch (Exception)
                {
                    Trace.TraceError("Count not read {0} object", obj.HexAddress);

                    // Do nothing for now
                }
            }

            return(hashtableModel);
        }