コード例 #1
0
        /// <summary>
        /// Sets the Length of the stream.
        /// </summary>
        /// <param name="value"></param>
        public override void SetLength(long value)
        {
            if (value < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(value), SR.ArgumentOutOfRange_NeedNonNegNum);
            }
            if (_buffer != null)
            {
                throw new NotSupportedException(SR.NotSupported_UmsSafeBuffer);
            }

            EnsureNotClosed();
            EnsureWriteable();

            if (value > _capacity)
            {
                throw new IOException(SR.IO_FixedCapacity);
            }

            long pos = Interlocked.Read(ref _position);
            long len = Interlocked.Read(ref _length);

            if (value > len)
            {
                unsafe
                {
                    NativeMemory.Clear(_mem + len, (nuint)(value - len));
                }
            }
            Interlocked.Exchange(ref _length, value);
            if (pos > value)
            {
                Interlocked.Exchange(ref _position, value);
            }
        }
コード例 #2
0
        internal static unsafe void *CoTaskMemAllocAndZeroMemory(int size)
        {
            byte *ptr = (byte *)Marshal.AllocCoTaskMem(size);

            // Marshal.AllocCoTaskMem will throw OOMException if out of memory
            Debug.Assert(ptr != null);

            NativeMemory.Clear(ptr, (uint)size);
            return(ptr);
        }
コード例 #3
0
        private unsafe ref byte InvokeWithManyArguments(
            IntPtr methodToCall, ref byte thisArg, ref byte ret,
            object?[] parameters, BinderBundle binderBundle, bool wrapInTargetInvocationException)
        {
            int argCount = _argumentCount;

            // We don't check a max stack size since we are invoking a method which
            // naturally requires a stack size that is dependent on the arg count\size.
            IntPtr *pStorage = stackalloc IntPtr[2 * argCount];

            NativeMemory.Clear(pStorage, (nuint)(2 * argCount) * (nuint)sizeof(IntPtr));

            ByReference *pByRefStorage = (ByReference *)(pStorage + argCount);

            RuntimeImports.GCFrameRegistration regArgStorage   = new(pStorage, (uint)argCount, areByRefs : false);
            RuntimeImports.GCFrameRegistration regByRefStorage = new(pByRefStorage, (uint)argCount, areByRefs : true);

            try
            {
                RuntimeImports.RhRegisterForGCReporting(&regArgStorage);
                RuntimeImports.RhRegisterForGCReporting(&regByRefStorage);

                CheckArguments(ref Unsafe.As <IntPtr, object>(ref *pStorage), pByRefStorage, parameters, binderBundle);

                try
                {
                    ret = ref RawCalliHelper.Call(InvokeThunk, (void *)methodToCall, ref thisArg, ref ret, pByRefStorage);
                    DebugAnnotations.PreviousCallContainsDebuggerStepInCode();
                }
                catch (Exception e) when(wrapInTargetInvocationException)
                {
                    throw new TargetInvocationException(e);
                }
                finally
                {
                    if (_needsCopyBack)
                    {
                        CopyBack(ref Unsafe.As <IntPtr, object>(ref *pStorage), parameters);
                    }
                }
            }
            finally
            {
                RuntimeImports.RhUnregisterForGCReporting(&regByRefStorage);
                RuntimeImports.RhUnregisterForGCReporting(&regArgStorage);
            }

            return(ref ret);
        }
コード例 #4
0
        public void ClearWithSizeEqualTo0ShouldNoOpTest(int offset)
        {
            byte *ptr = (byte *)NativeMemory.AlignedAlloc(512, 8);

            Assert.True(ptr != null);
            Assert.True((nuint)ptr % 8 == 0);

            new Span <byte>(ptr, 512).Fill(0b10101010);

            NativeMemory.Clear(ptr + offset, 0);

            Assert.Equal(-1, new Span <byte>(ptr, 512).IndexOfAnyExcept((byte)0b10101010));

            NativeMemory.AlignedFree(ptr);
        }
コード例 #5
0
        public void ClearTest(int size, int offset)
        {
            byte *ptr = (byte *)NativeMemory.AlignedAlloc((nuint)(size + offset), 8);

            Assert.True(ptr != null);
            Assert.True((nuint)ptr % 8 == 0);

            new Span <byte>(ptr, size + offset).Fill(0b10101010);

            NativeMemory.Clear(ptr + offset, (nuint)size);

            Assert.Equal(-1, new Span <byte>(ptr + offset, size).IndexOfAnyExcept((byte)0));

            NativeMemory.AlignedFree(ptr);
        }
コード例 #6
0
        public void ClearWithExactRangeTest(int size, int offset)
        {
            int headLength = offset;
            int bodyLength = size;
            int tailLength = 512 - headLength - bodyLength;
            int headOffset = 0;
            int bodyOffset = headLength;
            int tailOffset = headLength + bodyLength;

            byte *ptr = (byte *)NativeMemory.AlignedAlloc(512, 8);

            Assert.True(ptr != null);
            Assert.True((nuint)ptr % 8 == 0);

            new Span <byte>(ptr, 512).Fill(0b10101010);

            NativeMemory.Clear(ptr + bodyOffset, (nuint)bodyLength);

            Assert.Equal(-1, new Span <byte>(ptr + headOffset, headLength).IndexOfAnyExcept((byte)0b10101010));
            Assert.Equal(-1, new Span <byte>(ptr + bodyOffset, bodyLength).IndexOfAnyExcept((byte)0));
            Assert.Equal(-1, new Span <byte>(ptr + tailOffset, tailLength).IndexOfAnyExcept((byte)0b10101010));

            NativeMemory.AlignedFree(ptr);
        }
コード例 #7
0
        public void ClearWithNullPointerAndZeroByteCountTest()
        {
            NativeMemory.Clear(null, 0);

            // This test method just needs to check that no exceptions are thrown
        }
コード例 #8
0
        internal unsafe void WriteCore(ReadOnlySpan <byte> buffer)
        {
            EnsureNotClosed();
            EnsureWriteable();

            long pos = Interlocked.Read(ref _position);  // Use a local to avoid a race condition
            long len = Interlocked.Read(ref _length);
            long n   = pos + buffer.Length;

            // Check for overflow
            if (n < 0)
            {
                throw new IOException(SR.IO_StreamTooLong);
            }

            if (n > _capacity)
            {
                throw new NotSupportedException(SR.IO_FixedCapacity);
            }

            if (_buffer == null)
            {
                // Check to see whether we are now expanding the stream and must
                // zero any memory in the middle.
                if (pos > len)
                {
                    NativeMemory.Clear(_mem + len, (nuint)(pos - len));
                }

                // set length after zeroing memory to avoid race condition of accessing unzeroed memory
                if (n > len)
                {
                    Interlocked.Exchange(ref _length, n);
                }
            }

            if (_buffer != null)
            {
                long bytesLeft = _capacity - pos;
                if (bytesLeft < buffer.Length)
                {
                    throw new ArgumentException(SR.Arg_BufferTooSmall);
                }

                byte *pointer = null;
                try
                {
                    _buffer.AcquirePointer(ref pointer);
                    Buffer.Memmove(ref *(pointer + pos + _offset), ref MemoryMarshal.GetReference(buffer), (nuint)buffer.Length);
                }
                finally
                {
                    if (pointer != null)
                    {
                        _buffer.ReleasePointer();
                    }
                }
            }
            else
            {
                Buffer.Memmove(ref *(_mem + pos), ref MemoryMarshal.GetReference(buffer), (nuint)buffer.Length);
            }

            Interlocked.Exchange(ref _position, n);
            return;
        }