Exemplo n.º 1
0
        internal static int GetTlsSlotForThread(RuntimeBase runtime, ulong teb)
        {
            const int maxTlsSlot              = 64;
            const int tlsSlotOffset           = 0x1480; // Same on x86 and amd64
            const int tlsExpansionSlotsOffset = 0x1780;
            uint      ptrSize = (uint)runtime.PointerSize;

            ulong lowerTlsSlots = teb + tlsSlotOffset;
            uint  clrTlsSlot    = runtime.GetTlsSlot();

            if (clrTlsSlot == uint.MaxValue)
            {
                return(0);
            }

            ulong tlsSlot = 0;

            if (clrTlsSlot < maxTlsSlot)
            {
                tlsSlot = lowerTlsSlots + ptrSize * clrTlsSlot;
            }
            else
            {
                if (!runtime.ReadPointer(teb + tlsExpansionSlotsOffset, out tlsSlot) || tlsSlot == 0)
                {
                    return(0);
                }

                tlsSlot += ptrSize * (clrTlsSlot - maxTlsSlot);
            }

            ulong clrTls = 0;

            if (!runtime.ReadPointer(tlsSlot, out clrTls))
            {
                return(0);
            }

            // Get thread data;

            uint tlsThreadTypeIndex = runtime.GetThreadTypeIndex();

            if (tlsThreadTypeIndex == uint.MaxValue)
            {
                return(0);
            }

            ulong threadType = 0;

            if (!runtime.ReadPointer(clrTls + ptrSize * tlsThreadTypeIndex, out threadType))
            {
                return(0);
            }

            return((int)threadType);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Gets a string field from the object.  Note that the type must match exactly, as this method
        /// will not do type coercion.  This method will throw an ArgumentException if no field matches
        /// the given name.  It will throw an InvalidOperationException if the field is not
        /// of the correct type.  Lastly, it will throw a MemoryReadException if there was an error reading
        /// the value of this field out of the data target.
        /// </summary>
        /// <param name="fieldName">The name of the field to get the value for.</param>
        /// <returns>The value of the given field or null if this object points to a null value.</returns>
        public string GetStringOrNull(string fieldName)
        {
            if (IsNull)
            {
                return(null);
            }

            ulong       address = GetFieldAddress(fieldName, ClrElementType.String, "string");
            ulong       str;
            RuntimeBase runtime = (RuntimeBase)_type.Heap.Runtime;

            if (!runtime.ReadPointer(address, out str))
            {
                throw new MemoryReadException(address);
            }

            string result;

            if (!runtime.ReadString(str, out result))
            {
                throw new MemoryReadException(str);
            }

            return(result);
        }