Ejemplo n.º 1
0
        static unsafe object to_object(TysosType enum_type, object value)
        {
            /* Convert value (of an integral type) to an enum of type 'enum_type'
             * and return its boxed value
             */

            if (enum_type == null)
            {
                throw new ArgumentNullException("enumType");
            }
            if (value == null)
            {
                throw new ArgumentException("value");
            }

            /* The incoming value type 'value' is already boxed.
             *
             * We can therefore just copy its data to a new enum object
             */

            void *value_obj   = CastOperations.ReinterpretAsPointer(value);
            void *value_vtbl  = *(void **)value_obj;
            int   value_csize = *(int *)((byte *)value_vtbl + ClassOperations.GetVtblTypeSizeOffset());

            void *ret_obj = MemoryOperations.GcMalloc(value_csize);

            MemoryOperations.MemCpy(ret_obj, value_obj, value_csize);

            void *ret_vtbl = *(void **)((byte *)CastOperations.ReinterpretAsPointer(enum_type) + ClassOperations.GetSystemTypeImplOffset());

            *(void **)ret_obj = ret_vtbl;
            *(void **)((byte *)ret_obj + ClassOperations.GetMutexLockOffset()) = null;

            return(CastOperations.ReinterpretAsObject(ret_obj));
        }
Ejemplo n.º 2
0
        static unsafe bool InternalStrcpy(byte *dest, int destPos, byte *src, int srcPos, int count)
        {
            /* Get size of src and dest */
            int srcLength  = *(int *)(src + StringOperations.GetLengthOffset());
            int destLength = *(int *)(dest + StringOperations.GetLengthOffset());

            /* Ensure the source and destination are big enough */
            if (destPos < 0)
            {
                return(false);
            }
            if (srcPos < 0)
            {
                return(false);
            }
            if (count < 0)
            {
                return(false);
            }
            if (destPos + count > destLength)
            {
                return(false);
            }
            if (srcPos + count > srcLength)
            {
                return(false);
            }

            /* Do the copy */
            MemoryOperations.MemCpy((void *)(dest + StringOperations.GetDataOffset() + destPos * 2),
                                    (void *)(src + StringOperations.GetDataOffset() + srcPos * 2), count * 2);

            return(true);
        }
Ejemplo n.º 3
0
        static unsafe void InitializeArray(void *arr, void *fld_handle)
        {
            System.Diagnostics.Debugger.Log(0, "libsupcs", "InitializeArray: arr: " + ((ulong)arr).ToString("X16") + ", fld_handle: " + ((ulong)fld_handle).ToString("X16"));
            void *dst = *(void **)((byte *)arr + ArrayOperations.GetInnerArrayOffset());

            /* Get total number of elements, and hence data size */
            int *sizes = *(int **)((byte *)arr + ArrayOperations.GetSizesOffset());
            int  rank  = *(int *)((byte *)arr + ArrayOperations.GetRankOffset());

            if (rank == 0)
            {
                return;
            }

            int size = sizes[0];

            for (int i = 1; i < rank; i++)
            {
                size *= sizes[i];
            }

            int len = size * *(int *)((byte *)arr + ArrayOperations.GetElemSizeOffset());

            /* Field Typeinfos hava a pointer to the stored data as their third element */
            void *src = ((void **)fld_handle)[2];

            System.Diagnostics.Debugger.Log(0, "libsupcs", "InitializeArray: src: " + ((ulong)src).ToString("X16") +
                                            ", dest: " + ((ulong)dst).ToString("X16") + ", len: " + len.ToString());

            MemoryOperations.MemCpy(dst, src, len);
        }
Ejemplo n.º 4
0
        static unsafe void StringCtor(byte *str, char *value, int startIndex, int length)
        {
            void *src = value + startIndex;
            int   len = length * sizeof(char);
            void *dst = str + StringOperations.GetDataOffset();

            MemoryOperations.MemCpy(dst, src, len);
        }
Ejemplo n.º 5
0
        static unsafe void StringCtor(byte *str, char[] srcArr, int startIndex, int length)
        {
            void *src = (byte *)MemoryOperations.GetInternalArray(srcArr) + sizeof(char) * startIndex;
            int   len = length * sizeof(char);
            void *dst = str + StringOperations.GetDataOffset();

            MemoryOperations.MemCpy(dst, src, len);
        }
Ejemplo n.º 6
0
        static unsafe void StringCtor(byte *str, char[] srcArr)
        {
            void *src = MemoryOperations.GetInternalArray(srcArr);
            int   len = srcArr.Length * sizeof(char);
            void *dst = str + StringOperations.GetDataOffset();

            MemoryOperations.MemCpy(dst, src, len);
        }
Ejemplo n.º 7
0
        static unsafe void CopyToNative(void *srcArr, int startIndex, void *dst, int length)
        {
            var  esize  = *(int *)((byte *)srcArr + ArrayOperations.GetElemSizeOffset());
            int *lovals = *(int **)((byte *)srcArr + ArrayOperations.GetLoboundsOffset());

            var src = *(byte **)((byte *)srcArr + ArrayOperations.GetInnerArrayOffset()) + (startIndex - lovals[0]) * esize;

            MemoryOperations.MemCpy(dst, src, length * esize);
        }
Ejemplo n.º 8
0
        static unsafe void CopyToManaged(void *src, void *dstArr, int startIndex, int length)
        {
            var  esize  = *(int *)((byte *)dstArr + ArrayOperations.GetElemSizeOffset());
            int *lovals = *(int **)((byte *)dstArr + ArrayOperations.GetLoboundsOffset());

            var dst = *(byte **)((byte *)dstArr + ArrayOperations.GetInnerArrayOffset()) + (startIndex - lovals[0]) * esize;

            System.Diagnostics.Debugger.Log(0, null, "libsupcs: Array.CopyToManaged: src: " + ((ulong)src).ToString("X16") +
                                            ", dstArr: " + ((ulong)dstArr).ToString("X16") +
                                            ", startIndex: " + startIndex.ToString() +
                                            ", length: " + length.ToString() +
                                            ", esize: " + esize.ToString() +
                                            ", lovals[0]: " + lovals[0].ToString() +
                                            ", dst: " + ((ulong)dst).ToString("X16"));

            MemoryOperations.MemCpy(dst, src, length * esize);
        }
Ejemplo n.º 9
0
        static unsafe void *get_value(void *obj)
        {
            /* we can copy the entire enum object to an instance
             * of the underlying type and just change the
             * vtbl
             */

            byte *enum_vtbl = *(byte **)obj;
            int   enum_size = *(int *)(enum_vtbl + ClassOperations.GetVtblTypeSizeOffset());

            void **enum_ti = *(void ***)enum_vtbl;
            void * ut_vtbl = *(enum_ti + 1);

            void *new_obj = MemoryOperations.GcMalloc(enum_size);

            MemoryOperations.MemCpy(new_obj, obj, enum_size);

            *(void **)new_obj = ut_vtbl;
            *(void **)((byte *)new_obj + ClassOperations.GetMutexLockOffset()) = null;

            return(new_obj);
        }
Ejemplo n.º 10
0
        static unsafe void *GetValueImpl(void *arr, int pos)
        {
            /* Get the element type of the array */
            void *et = *(void **)((byte *)arr + ArrayOperations.GetElemTypeOffset());

            /* Get a pointer to the source data */
            var   elem_size = *(int *)((byte *)arr + ArrayOperations.GetElemSizeOffset());
            void *ia        = *(void **)((byte *)arr + ArrayOperations.GetInnerArrayOffset());
            void *sptr      = (void *)((byte *)ia + pos * elem_size);

            /* Is this a value type? In which case we need to return a boxed value */
            void *extends = *(void **)((byte *)et + ClassOperations.GetVtblExtendsVtblPtrOffset());

            if (extends == OtherOperations.GetStaticObjectAddress("_Zu1L") ||
                extends == OtherOperations.GetStaticObjectAddress("_ZW6System4Enum"))
            {
                /* This is a value type.  We need to read the size of the element,
                 * create a new object of the appropriate size and copy the data
                 * into it */
                byte *ret = (byte *)MemoryOperations.GcMalloc(elem_size + ClassOperations.GetBoxedTypeDataOffset());
                *(void **)(ret + ClassOperations.GetVtblFieldOffset()) = et;
                *(ulong *)(ret + ClassOperations.GetMutexLockOffset()) = 0;

                /* Avoid calls to memcpy if possible */
                switch (elem_size)
                {
                case 1:
                    *(byte *)(ret + ClassOperations.GetBoxedTypeDataOffset()) = *(byte *)sptr;
                    return(ret);

                case 2:
                    *(ushort *)(ret + ClassOperations.GetBoxedTypeDataOffset()) = *(ushort *)sptr;
                    return(ret);

                case 4:
                    *(uint *)(ret + ClassOperations.GetBoxedTypeDataOffset()) = *(uint *)sptr;
                    return(ret);

                case 8:
                    if (OtherOperations.GetPointerSize() >= 8)
                    {
                        *(ulong *)(ret + ClassOperations.GetBoxedTypeDataOffset()) = *(ulong *)sptr;
                        return(ret);
                    }
                    else
                    {
                        *(uint *)(ret + ClassOperations.GetBoxedTypeDataOffset())     = *(uint *)sptr;
                        *(uint *)(ret + ClassOperations.GetBoxedTypeDataOffset() + 4) = *(uint *)((byte *)sptr + 4);
                        return(ret);
                    }

                case 16:
                    if (OtherOperations.GetPointerSize() >= 8)
                    {
                        *(ulong *)(ret + ClassOperations.GetBoxedTypeDataOffset())     = *(ulong *)sptr;
                        *(ulong *)(ret + ClassOperations.GetBoxedTypeDataOffset() + 8) = *(ulong *)((byte *)sptr + 8);
                        return(ret);
                    }
                    else
                    {
                        *(uint *)(ret + ClassOperations.GetBoxedTypeDataOffset())      = *(uint *)sptr;
                        *(uint *)(ret + ClassOperations.GetBoxedTypeDataOffset() + 4)  = *(uint *)((byte *)sptr + 4);
                        *(uint *)(ret + ClassOperations.GetBoxedTypeDataOffset() + 8)  = *(uint *)((byte *)sptr + 8);
                        *(uint *)(ret + ClassOperations.GetBoxedTypeDataOffset() + 12) = *(uint *)((byte *)sptr + 12);
                        return(ret);
                    }
                }

                /* Do data copy via memcpy */
                MemoryOperations.MemCpy(ret + ClassOperations.GetBoxedTypeDataOffset(),
                                        sptr, elem_size);
                return(ret);
            }
            else
            {
                /* Its a reference type, so just return the pointer */
                return(*(void **)sptr);
            }
        }