summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/Microsoft/Win32/SafeHandles
diff options
context:
space:
mode:
authorJiyoung Yun <jy910.yun@samsung.com>2016-11-23 19:09:09 +0900
committerJiyoung Yun <jy910.yun@samsung.com>2016-11-23 19:09:09 +0900
commit4b4aad7217d3292650e77eec2cf4c198ea9c3b4b (patch)
tree98110734c91668dfdbb126fcc0e15ddbd93738ca /src/mscorlib/src/Microsoft/Win32/SafeHandles
parentfa45f57ed55137c75ac870356a1b8f76c84b229c (diff)
downloadcoreclr-4b4aad7217d3292650e77eec2cf4c198ea9c3b4b.tar.gz
coreclr-4b4aad7217d3292650e77eec2cf4c198ea9c3b4b.tar.bz2
coreclr-4b4aad7217d3292650e77eec2cf4c198ea9c3b4b.zip
Imported Upstream version 1.1.0upstream/1.1.0
Diffstat (limited to 'src/mscorlib/src/Microsoft/Win32/SafeHandles')
-rw-r--r--src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.cs43
-rw-r--r--src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeFileMappingHandle.cs43
-rw-r--r--src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeFindHandle.cs35
-rw-r--r--src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeLibraryHandle.cs29
-rw-r--r--src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeLocalAllocHandle.cs29
-rw-r--r--src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeRegistryHandle.cs41
-rw-r--r--src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeViewOfFileHandle.cs50
-rw-r--r--src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeWaitHandle.cs47
-rw-r--r--src/mscorlib/src/Microsoft/Win32/SafeHandles/Win32SafeHandles.cs112
9 files changed, 429 insertions, 0 deletions
diff --git a/src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.cs b/src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.cs
new file mode 100644
index 0000000000..ab06347ee4
--- /dev/null
+++ b/src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.cs
@@ -0,0 +1,43 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+/*============================================================
+**
+**
+**
+** A wrapper for file handles
+**
+**
+===========================================================*/
+
+using System;
+using System.Security;
+using System.Security.Permissions;
+using System.Runtime.InteropServices;
+using System.Runtime.CompilerServices;
+using System.Runtime.ConstrainedExecution;
+using System.Runtime.Versioning;
+using Microsoft.Win32;
+
+namespace Microsoft.Win32.SafeHandles {
+
+ [System.Security.SecurityCritical] // auto-generated_required
+ public sealed class SafeFileHandle: SafeHandleZeroOrMinusOneIsInvalid {
+
+ private SafeFileHandle() : base(true)
+ {
+ }
+
+ public SafeFileHandle(IntPtr preexistingHandle, bool ownsHandle) : base(ownsHandle) {
+ SetHandle(preexistingHandle);
+ }
+
+ [System.Security.SecurityCritical]
+ override protected bool ReleaseHandle()
+ {
+ return Win32Native.CloseHandle(handle);
+ }
+ }
+}
+
diff --git a/src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeFileMappingHandle.cs b/src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeFileMappingHandle.cs
new file mode 100644
index 0000000000..5e1b5100bc
--- /dev/null
+++ b/src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeFileMappingHandle.cs
@@ -0,0 +1,43 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+/*============================================================
+**
+**
+**
+** A wrapper for file handles
+**
+**
+===========================================================*/
+using System;
+using System.Security;
+using System.Security.Permissions;
+using System.Runtime.InteropServices;
+using System.Runtime.CompilerServices;
+using System.Runtime.ConstrainedExecution;
+using System.Runtime.Versioning;
+
+namespace Microsoft.Win32.SafeHandles
+{
+ [System.Security.SecurityCritical] // auto-generated
+ internal sealed class SafeFileMappingHandle : SafeHandleZeroOrMinusOneIsInvalid
+ {
+ [System.Security.SecurityCritical] // auto-generated_required
+ internal SafeFileMappingHandle() : base(true) {}
+
+ // 0 is an Invalid Handle
+ [System.Security.SecurityCritical] // auto-generated_required
+ internal SafeFileMappingHandle(IntPtr handle, bool ownsHandle) : base (ownsHandle)
+ {
+ SetHandle(handle);
+ }
+
+ [System.Security.SecurityCritical]
+ override protected bool ReleaseHandle()
+ {
+ return Win32Native.CloseHandle(handle);
+ }
+ }
+}
+
diff --git a/src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeFindHandle.cs b/src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeFindHandle.cs
new file mode 100644
index 0000000000..b24535f997
--- /dev/null
+++ b/src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeFindHandle.cs
@@ -0,0 +1,35 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+/*============================================================
+**
+**
+**
+** A wrapper for find handles
+**
+**
+===========================================================*/
+
+using System;
+using System.Security;
+using System.Security.Permissions;
+using System.Runtime.InteropServices;
+using System.Runtime.CompilerServices;
+using System.Runtime.ConstrainedExecution;
+using Microsoft.Win32;
+
+namespace Microsoft.Win32.SafeHandles {
+ [System.Security.SecurityCritical] // auto-generated
+ internal sealed class SafeFindHandle : SafeHandleZeroOrMinusOneIsInvalid
+ {
+ [System.Security.SecurityCritical] // auto-generated_required
+ internal SafeFindHandle() : base(true) {}
+
+ [System.Security.SecurityCritical]
+ override protected bool ReleaseHandle()
+ {
+ return Win32Native.FindClose(handle);
+ }
+ }
+}
diff --git a/src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeLibraryHandle.cs b/src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeLibraryHandle.cs
new file mode 100644
index 0000000000..d2ea42b14e
--- /dev/null
+++ b/src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeLibraryHandle.cs
@@ -0,0 +1,29 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+namespace Microsoft.Win32 {
+ using Microsoft.Win32;
+ using Microsoft.Win32.SafeHandles;
+ using System;
+ using System.Runtime.CompilerServices;
+ using System.Runtime.ConstrainedExecution;
+ using System.Runtime.InteropServices;
+ using System.Runtime.Serialization;
+ using System.Runtime.Versioning;
+ using System.Security;
+ using System.Security.Permissions;
+ using System.Text;
+
+ [System.Security.SecurityCritical] // auto-generated
+ [HostProtectionAttribute(MayLeakOnAbort = true)]
+ sealed internal class SafeLibraryHandle : SafeHandleZeroOrMinusOneIsInvalid {
+ internal SafeLibraryHandle() : base(true) {}
+
+ [System.Security.SecurityCritical]
+ override protected bool ReleaseHandle()
+ {
+ return UnsafeNativeMethods.FreeLibrary(handle);
+ }
+ }
+}
diff --git a/src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeLocalAllocHandle.cs b/src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeLocalAllocHandle.cs
new file mode 100644
index 0000000000..3eea2b9937
--- /dev/null
+++ b/src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeLocalAllocHandle.cs
@@ -0,0 +1,29 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+namespace Microsoft.Win32.SafeHandles {
+ using System;
+ using System.Runtime.CompilerServices;
+ using System.Runtime.InteropServices;
+ using System.Runtime.ConstrainedExecution;
+
+ [System.Security.SecurityCritical] // auto-generated
+ internal sealed class SafeLocalAllocHandle : SafeBuffer {
+ private SafeLocalAllocHandle () : base(true) {}
+
+ // 0 is an Invalid Handle
+ internal SafeLocalAllocHandle (IntPtr handle) : base (true) {
+ SetHandle(handle);
+ }
+
+ internal static SafeLocalAllocHandle InvalidHandle {
+ get { return new SafeLocalAllocHandle(IntPtr.Zero); }
+ }
+
+ [System.Security.SecurityCritical]
+ override protected bool ReleaseHandle()
+ {
+ return Win32Native.LocalFree(handle) == IntPtr.Zero;
+ }
+ }
+}
diff --git a/src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeRegistryHandle.cs b/src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeRegistryHandle.cs
new file mode 100644
index 0000000000..d0e3f048f2
--- /dev/null
+++ b/src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeRegistryHandle.cs
@@ -0,0 +1,41 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+//
+//
+//
+// Implements Microsoft.Win32.SafeHandles.SafeRegistryHandle
+//
+// ======================================================================================
+namespace Microsoft.Win32.SafeHandles {
+ using System;
+ using System.Security;
+ using System.Security.Permissions;
+ using System.Runtime.InteropServices;
+ using System.Runtime.CompilerServices;
+ using System.Runtime.ConstrainedExecution;
+ using System.Runtime.Versioning;
+
+ [System.Security.SecurityCritical]
+ public sealed class SafeRegistryHandle : SafeHandleZeroOrMinusOneIsInvalid {
+ [System.Security.SecurityCritical]
+ internal SafeRegistryHandle() : base(true) {}
+
+ [System.Security.SecurityCritical]
+ public SafeRegistryHandle(IntPtr preexistingHandle, bool ownsHandle) : base(ownsHandle) {
+ SetHandle(preexistingHandle);
+ }
+
+ [System.Security.SecurityCritical]
+ override protected bool ReleaseHandle() {
+ return (RegCloseKey(handle) == Win32Native.ERROR_SUCCESS);
+ }
+
+ [DllImport(Win32Native.ADVAPI32)]
+ [SuppressUnmanagedCodeSecurity]
+ [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
+ internal static extern int RegCloseKey(IntPtr hKey);
+ }
+}
+
diff --git a/src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeViewOfFileHandle.cs b/src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeViewOfFileHandle.cs
new file mode 100644
index 0000000000..01ec4d2ad8
--- /dev/null
+++ b/src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeViewOfFileHandle.cs
@@ -0,0 +1,50 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+/*============================================================
+**
+**
+**
+** A wrapper for file handles
+**
+**
+===========================================================*/
+using System;
+using System.Security;
+using System.Security.Permissions;
+using System.Runtime.InteropServices;
+using System.Runtime.CompilerServices;
+using System.Runtime.ConstrainedExecution;
+using System.Runtime.Versioning;
+using Microsoft.Win32;
+using Microsoft.Win32.SafeHandles;
+
+namespace Microsoft.Win32.SafeHandles
+{
+ [System.Security.SecurityCritical] // auto-generated
+ internal sealed class SafeViewOfFileHandle : SafeHandleZeroOrMinusOneIsInvalid
+ {
+ [System.Security.SecurityCritical] // auto-generated_required
+ internal SafeViewOfFileHandle() : base(true) {}
+
+ // 0 is an Invalid Handle
+ [System.Security.SecurityCritical] // auto-generated_required
+ internal SafeViewOfFileHandle(IntPtr handle, bool ownsHandle) : base (ownsHandle) {
+ SetHandle(handle);
+ }
+
+ [System.Security.SecurityCritical]
+ override protected bool ReleaseHandle()
+ {
+ if (Win32Native.UnmapViewOfFile(handle))
+ {
+ handle = IntPtr.Zero;
+ return true;
+ }
+
+ return false;
+ }
+ }
+}
+
diff --git a/src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeWaitHandle.cs b/src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeWaitHandle.cs
new file mode 100644
index 0000000000..fa24c96718
--- /dev/null
+++ b/src/mscorlib/src/Microsoft/Win32/SafeHandles/SafeWaitHandle.cs
@@ -0,0 +1,47 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+/*============================================================
+**
+**
+**
+** A wrapper for Win32 events (mutexes, auto reset events, and
+** manual reset events). Used by WaitHandle.
+**
+**
+===========================================================*/
+
+using System;
+using System.Security;
+using System.Security.Permissions;
+using System.Runtime.InteropServices;
+using System.Runtime.CompilerServices;
+using System.Runtime.ConstrainedExecution;
+using System.Runtime.Versioning;
+using Microsoft.Win32;
+using System.Threading;
+
+namespace Microsoft.Win32.SafeHandles {
+
+ [System.Security.SecurityCritical] // auto-generated_required
+ public sealed class SafeWaitHandle : SafeHandleZeroOrMinusOneIsInvalid
+ {
+ // Called by P/Invoke marshaler
+ private SafeWaitHandle() : base(true)
+ {
+ }
+
+ [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
+ public SafeWaitHandle(IntPtr existingHandle, bool ownsHandle) : base(ownsHandle)
+ {
+ SetHandle(existingHandle);
+ }
+
+ [System.Security.SecurityCritical]
+ override protected bool ReleaseHandle()
+ {
+ return Win32Native.CloseHandle(handle);
+ }
+ }
+}
diff --git a/src/mscorlib/src/Microsoft/Win32/SafeHandles/Win32SafeHandles.cs b/src/mscorlib/src/Microsoft/Win32/SafeHandles/Win32SafeHandles.cs
new file mode 100644
index 0000000000..58e0d7ad1d
--- /dev/null
+++ b/src/mscorlib/src/Microsoft/Win32/SafeHandles/Win32SafeHandles.cs
@@ -0,0 +1,112 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+// See the LICENSE file in the project root for more information.
+
+//
+// Abstract derivations of SafeHandle designed to provide the common
+// functionality supporting Win32 handles. More specifically, they describe how
+// an invalid handle looks (for instance, some handles use -1 as an invalid
+// handle value, others use 0).
+//
+// Further derivations of these classes can specialise this even further (e.g.
+// file or registry handles).
+//
+//
+
+namespace Microsoft.Win32.SafeHandles
+{
+ using System;
+ using System.Runtime.InteropServices;
+ using System.Runtime.CompilerServices;
+ using System.Security.Permissions;
+ using System.Runtime.ConstrainedExecution;
+
+ // Class of safe handle which uses 0 or -1 as an invalid handle.
+ [System.Security.SecurityCritical] // auto-generated_required
+#if !FEATURE_CORECLR
+ [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode=true)]
+#endif
+ public abstract class SafeHandleZeroOrMinusOneIsInvalid : SafeHandle
+ {
+ [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
+ protected SafeHandleZeroOrMinusOneIsInvalid(bool ownsHandle) : base(IntPtr.Zero, ownsHandle)
+ {
+ }
+
+#if FEATURE_CORECLR
+ // A default constructor is needed to satisfy CoreCLR inheritence rules. It should not be called at runtime
+ protected SafeHandleZeroOrMinusOneIsInvalid()
+ {
+ throw new NotImplementedException();
+ }
+#endif // FEATURE_CORECLR
+
+ public override bool IsInvalid {
+ [System.Security.SecurityCritical]
+ get { return handle.IsNull() || handle == new IntPtr(-1); }
+ }
+ }
+
+ // Class of safe handle which uses only -1 as an invalid handle.
+ [System.Security.SecurityCritical] // auto-generated_required
+#if !FEATURE_CORECLR
+ [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode=true)]
+#endif
+ public abstract class SafeHandleMinusOneIsInvalid : SafeHandle
+ {
+ [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
+ protected SafeHandleMinusOneIsInvalid(bool ownsHandle) : base(new IntPtr(-1), ownsHandle)
+ {
+ }
+
+#if FEATURE_CORECLR
+ // A default constructor is needed to satisfy CoreCLR inheritence rules. It should not be called at runtime
+ protected SafeHandleMinusOneIsInvalid()
+ {
+ throw new NotImplementedException();
+ }
+#endif // FEATURE_CORECLR
+
+ public override bool IsInvalid {
+ [System.Security.SecurityCritical]
+ get { return handle == new IntPtr(-1); }
+ }
+ }
+
+ // Class of critical handle which uses 0 or -1 as an invalid handle.
+ [System.Security.SecurityCritical] // auto-generated_required
+#if !FEATURE_CORECLR
+ [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode=true)]
+#endif
+ public abstract class CriticalHandleZeroOrMinusOneIsInvalid : CriticalHandle
+ {
+ [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
+ protected CriticalHandleZeroOrMinusOneIsInvalid() : base(IntPtr.Zero)
+ {
+ }
+
+ public override bool IsInvalid {
+ [System.Security.SecurityCritical]
+ get { return handle.IsNull() || handle == new IntPtr(-1); }
+ }
+ }
+
+ // Class of critical handle which uses only -1 as an invalid handle.
+ [System.Security.SecurityCritical] // auto-generated_required
+#if !FEATURE_CORECLR
+ [SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode=true)]
+#endif
+ public abstract class CriticalHandleMinusOneIsInvalid : CriticalHandle
+ {
+ [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
+ protected CriticalHandleMinusOneIsInvalid() : base(new IntPtr(-1))
+ {
+ }
+
+ public override bool IsInvalid {
+ [System.Security.SecurityCritical]
+ get { return handle == new IntPtr(-1); }
+ }
+ }
+
+}