Imported Upstream version 4.0.0~alpha1

Former-commit-id: 806294f5ded97629b74c85c09952f2a74fe182d9
This commit is contained in:
Jo Shields
2015-04-07 09:35:12 +01:00
parent 283343f570
commit 3c1f479b9d
22469 changed files with 2931443 additions and 869343 deletions

View File

@@ -0,0 +1,37 @@
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Activation.Interop
{
using System;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using Microsoft.Win32.SafeHandles;
#pragma warning disable 618 // have not moved to the v4 security model yet
[SecurityCritical(SecurityCriticalScope.Everything)]
#pragma warning restore 618
sealed class SafeCloseHandleCritical : SafeHandleZeroOrMinusOneIsInvalid
{
const string KERNEL32 = "kernel32.dll";
SafeCloseHandleCritical()
: base(true)
{
}
protected override bool ReleaseHandle()
{
return CloseHandle(handle);
}
[DllImport(KERNEL32, ExactSpelling = true, SetLastError = true)]
[SuppressUnmanagedCodeSecurity]
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
[ResourceExposure(ResourceScope.None)]
extern static bool CloseHandle(IntPtr handle);
}
}

View File

@@ -0,0 +1,85 @@
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Activation.Interop
{
using System;
using System.Runtime;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using Microsoft.Win32.SafeHandles;
#pragma warning disable 618 // have not moved to the v4 security model yet
[SecurityCritical(SecurityCriticalScope.Everything)]
#pragma warning restore 618
sealed class SafeHGlobalHandleCritical : SafeHandleZeroOrMinusOneIsInvalid
{
SafeHGlobalHandleCritical()
: base(true)
{
}
// 0 is an Invalid Handle
SafeHGlobalHandleCritical(IntPtr handle)
: base(true)
{
Fx.Assert(handle == IntPtr.Zero, "SafeHGlobalHandleCritical constructor can only be called with IntPtr.Zero.");
SetHandle(handle);
}
protected override bool ReleaseHandle()
{
Marshal.FreeHGlobal(handle);
return true;
}
public static SafeHGlobalHandleCritical InvalidHandle
{
get { return new SafeHGlobalHandleCritical(IntPtr.Zero); }
}
public static SafeHGlobalHandleCritical AllocHGlobal(string s)
{
byte[] bytes = DiagnosticUtility.Utility.AllocateByteArray(checked((s.Length + 1) * 2));
Encoding.Unicode.GetBytes(s, 0, s.Length, bytes, 0);
return AllocHGlobal(bytes);
}
public static SafeHGlobalHandleCritical AllocHGlobal(byte[] bytes)
{
SafeHGlobalHandleCritical result = AllocHGlobal(bytes.Length);
Marshal.Copy(bytes, 0, result.DangerousGetHandle(), bytes.Length);
return result;
}
public static SafeHGlobalHandleCritical AllocHGlobal(uint cb)
{
// The cast could overflow to minus.
// Unfortunately, Marshal.AllocHGlobal only takes int.
return AllocHGlobal((int)cb);
}
public static SafeHGlobalHandleCritical AllocHGlobal(int cb)
{
if (cb < 0)
{
throw FxTrace.Exception.ArgumentOutOfRange("cb", cb, SR.ValueMustBeNonNegative);
}
SafeHGlobalHandleCritical result = new SafeHGlobalHandleCritical();
// CER
RuntimeHelpers.PrepareConstrainedRegions();
try { }
finally
{
IntPtr ptr = Marshal.AllocHGlobal(cb);
result.SetHandle(ptr);
}
return result;
}
}
}

View File

@@ -0,0 +1,44 @@
//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
namespace System.ServiceModel.Activation.Interop
{
using System;
using System.Security;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Runtime.Versioning;
using System.Runtime;
[SuppressUnmanagedCodeSecurity]
static class SafeNativeMethods
{
public const int ERROR_NO_TOKEN = 1008;
const string ADVAPI32 = "advapi32.dll";
const string KERNEL32 = "kernel32.dll";
[DllImport(ADVAPI32, SetLastError = true, EntryPoint = "OpenThreadToken")]
[ResourceExposure(ResourceScope.None)]
static extern bool OpenThreadTokenCritical(
[In] IntPtr ThreadHandle,
[In] TokenAccessLevels DesiredAccess,
[In] bool OpenAsSelf,
[Out] out SafeCloseHandleCritical TokenHandle);
[DllImport(KERNEL32, SetLastError = true)]
[ResourceExposure(ResourceScope.None)]
static extern IntPtr GetCurrentThread();
[Fx.Tag.SecurityNote(Critical = "Calls two safe native methods: GetCurrentThread and OpenThreadToken." +
"Marshal.GetLastWin32Error captures current thread token in a SecurityCritical field.")]
[SecurityCritical]
internal static bool OpenCurrentThreadTokenCritical(TokenAccessLevels desiredAccess, bool openAsSelf, out SafeCloseHandleCritical tokenHandle, out int error)
{
bool result = OpenThreadTokenCritical(GetCurrentThread(), desiredAccess, openAsSelf, out tokenHandle);
error = Marshal.GetLastWin32Error();
return result;
}
}
}