Esempio n. 1
0
        protected virtual void Dispose(bool disposing)
        {
            if (!disposing)
            {
                if (pointer == IntPtr.Zero)
                {
                    return;
                }

                if (requireSecretDisposal)
                {
                    const string exceptionMessage = "FATAL: Reached finalizer for SecureMemorySecret (missing Dispose())";
                    throw new Exception(exceptionMessage + ((creationStackTrace == null) ? string.Empty : Environment.NewLine + creationStackTrace));
                }

                const string warningMessage = "WARN: Reached finalizer for SecureMemorySecret (missing Dispose())";
                Debug.WriteLine(warningMessage + ((creationStackTrace == null) ? string.Empty : Environment.NewLine + creationStackTrace));
            }
            else
            {
                pointerLock.EnterWriteLock();
                try
                {
                    if (pointer == IntPtr.Zero)
                    {
                        return;
                    }
#if DEBUG
                    // TODO Add/uncomment this when we refactor logging to use static creation
                    // log.LogDebug("closing: {pointer}", ptr);
#endif

                    // accessLock isn't needed here since we are holding the pointer lock in write
                    try
                    {
                        allocator.SetReadWriteAccess(pointer, length);
                    }
                    finally
                    {
                        allocator.Free(pointer, length);
                        pointer = IntPtr.Zero;
                    }
                }
                finally
                {
                    pointerLock.ExitWriteLock();
                }
            }
        }
        private void TestAllocSuccess()
        {
            Debug.WriteLine("SecureMemoryAllocatorTest.TestAllocSuccess");
            IntPtr pointer = secureMemoryAllocator.Alloc(1);

            CheckIntPtr(pointer, "ISecureMemoryAllocator.Alloc");

            try
            {
                // just do some sanity checks
                Marshal.WriteByte(pointer, 0, 1);
                Assert.Equal(1, Marshal.ReadByte(pointer, 0));
            }
            finally
            {
                secureMemoryAllocator.Free(pointer, 1);
            }
        }