We need you! We're working hard on the next version of Developer Fusion -
Let us know what you think we should be up to!
- namespace System.Threading
- {
- using System.Security;
- using System.Security.Permissions;
- using System.Runtime.InteropServices;
- using System.Runtime.CompilerServices;
- using System.Runtime.ConstrainedExecution;
- using System.Reflection;
- using System.Collections;
- using System.Threading;
- using System.Runtime.Serialization;
-
-
- internal struct CompressedStackSwitcher : IDisposable
- {
- internal CompressedStack curr_CS;
- internal CompressedStack prev_CS;
- internal IntPtr prev_ADStack;
-
-
- public override bool Equals(object obj)
- {
- if (obj == null || !(obj is CompressedStackSwitcher))
- return false;
- CompressedStackSwitcher sw = (CompressedStackSwitcher)obj;
- return (this.curr_CS == sw.curr_CS && this.prev_CS == sw.prev_CS && this.prev_ADStack == sw.prev_ADStack);
- }
-
- public override int GetHashCode()
- {
- return ToString().GetHashCode();
- }
-
- public static bool operator ==(CompressedStackSwitcher c1, CompressedStackSwitcher c2)
- {
- return c1.Equals(c2);
- }
-
- public static bool operator !=(CompressedStackSwitcher c1, CompressedStackSwitcher c2)
- {
- return !c1.Equals(c2);
- }
- /// <internalonly/>
- void IDisposable.Dispose()
- {
- Undo();
- }
-
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
- internal bool UndoNoThrow()
- {
- try {
- Undo();
- }
- catch {
- return false;
- }
- return true;
- }
-
-
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
- public void Undo()
- {
- if (curr_CS == null && prev_CS == null)
- return;
- if (prev_ADStack != (IntPtr)0)
- CompressedStack.RestoreAppDomainStack(prev_ADStack);
- CompressedStack.SetCompressedStackThread(prev_CS);
-
- prev_CS = null;
- curr_CS = null;
- prev_ADStack = (IntPtr)0;
- }
- }
-
- internal class SafeCompressedStackHandle : SafeHandle
- {
- public SafeCompressedStackHandle() : base(IntPtr.Zero, true)
- {
- }
-
- public override bool IsInvalid {
- get { return handle == IntPtr.Zero; }
- }
-
- protected override bool ReleaseHandle()
- {
- CompressedStack.DestroyDelayedCompressedStack(handle);
- handle = IntPtr.Zero;
- return true;
- }
- }
-
-
-
- [Serializable()]
- public sealed class CompressedStack : ISerializable
- {
-
- private PermissionListSet m_pls;
- private SafeCompressedStackHandle m_csHandle;
-
-
- internal PermissionListSet PLS {
- get { return m_pls; }
- }
-
- internal CompressedStack(SafeCompressedStackHandle csHandle)
- {
- m_csHandle = csHandle;
- }
-
- private CompressedStack(SafeCompressedStackHandle csHandle, PermissionListSet pls)
- {
- this.m_csHandle = csHandle;
- this.m_pls = pls;
- }
-
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
- public void GetObjectData(SerializationInfo info, StreamingContext context)
- {
- if (info == null)
- throw new ArgumentNullException("info");
- CompleteConstruction(null);
- info.AddValue("PLS", this.m_pls);
- }
-
- private CompressedStack(SerializationInfo info, StreamingContext context)
- {
- this.m_pls = (PermissionListSet)info.GetValue("PLS", typeof(PermissionListSet));
- }
-
- internal SafeCompressedStackHandle CompressedStackHandle {
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- get { return m_csHandle; }
- }
-
- [StrongNameIdentityPermissionAttribute(SecurityAction.LinkDemand, PublicKey = "0x00000000000000000400000000000000"), SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
- public static CompressedStack GetCompressedStack()
- {
-
- StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
- return CompressedStack.GetCompressedStack(ref stackMark);
- }
-
- static internal CompressedStack GetCompressedStack(ref StackCrawlMark stackMark)
- {
- CompressedStack cs;
- CompressedStack innerCS = null;
- if (CodeAccessSecurityEngine.QuickCheckForAllDemands()) {
- cs = new CompressedStack(null);
- }
- else if (CodeAccessSecurityEngine.AllDomainsHomogeneousWithNoStackModifiers()) {
- cs = new CompressedStack(null);
- cs.m_pls = PermissionListSet.CreateCompressedState_HG();
- }
- else {
-
- cs = new CompressedStack(GetDelayedCompressedStack(ref stackMark));
- if (cs.CompressedStackHandle != null && IsImmediateCompletionCandidate(cs.CompressedStackHandle, out innerCS)) {
- cs.CompleteConstruction(innerCS);
- DestroyDCSList(cs.CompressedStackHandle);
- }
- }
- return cs;
- }
-
- public static CompressedStack Capture()
- {
- StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
- return GetCompressedStack(ref stackMark);
- }
-
- [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.Infrastructure), DynamicSecurityMethodAttribute()]
- public static void Run(CompressedStack compressedStack, ContextCallback callback, object state)
- {
-
- if (compressedStack == null) {
- throw new ArgumentException(Environment.GetResourceString("Arg_NamedParamNull"), "compressedStack");
- }
- if (cleanupCode == null) {
- tryCode = new RuntimeHelpers.TryCode(runTryCode);
- cleanupCode = new RuntimeHelpers.CleanupCode(runFinallyCode);
- }
-
- CompressedStackRunData runData = new CompressedStackRunData(compressedStack, callback, state);
- RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(tryCode, cleanupCode, runData);
- }
-
- internal class CompressedStackRunData
- {
- internal CompressedStack cs;
- internal ContextCallback callBack;
- internal object state;
- internal CompressedStackSwitcher cssw;
- internal CompressedStackRunData(CompressedStack cs, ContextCallback cb, object state)
- {
- this.cs = cs;
- this.callBack = cb;
- this.state = state;
- this.cssw = new CompressedStackSwitcher();
- }
- }
- static internal void runTryCode(object userData)
- {
- CompressedStackRunData rData = (CompressedStackRunData)userData;
- rData.cssw = SetCompressedStack(rData.cs, GetCompressedStackThread());
- rData.callBack(rData.state);
-
- }
-
- [PrePrepareMethod()]
- static internal void runFinallyCode(object userData, bool exceptionThrown)
- {
- CompressedStackRunData rData = (CompressedStackRunData)userData;
- rData.cssw.Undo();
- }
-
- static internal RuntimeHelpers.TryCode tryCode;
- static internal RuntimeHelpers.CleanupCode cleanupCode;
-
-
- static internal CompressedStackSwitcher SetCompressedStack(CompressedStack cs, CompressedStack prevCS)
- {
- CompressedStackSwitcher cssw = new CompressedStackSwitcher();
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
-
-
- RuntimeHelpers.PrepareConstrainedRegions();
- try {
-
- }
- finally {
-
- SetCompressedStackThread(cs);
- cssw.prev_CS = prevCS;
- cssw.curr_CS = cs;
- cssw.prev_ADStack = SetAppDomainStack(cs);
- }
- }
- catch {
- cssw.UndoNoThrow();
- throw;
-
- }
- return cssw;
- }
-
-
- [ComVisible(false)]
- public CompressedStack CreateCopy()
- {
- return new CompressedStack(this.m_csHandle, this.m_pls);
- }
-
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- static internal IntPtr SetAppDomainStack(CompressedStack cs)
- {
-
- return Thread.CurrentThread.SetAppDomainStack((cs == null ? null : cs.CompressedStackHandle));
- }
-
-
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
- static internal void RestoreAppDomainStack(IntPtr appDomainStack)
- {
- Thread.CurrentThread.RestoreAppDomainStack(appDomainStack);
-
- }
- static internal CompressedStack GetCompressedStackThread()
- {
- ExecutionContext ec = Thread.CurrentThread.GetExecutionContextNoCreate();
- if (ec != null && ec.SecurityContext != null)
- return ec.SecurityContext.CompressedStack;
- return null;
- }
- [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
- static internal void SetCompressedStackThread(CompressedStack cs)
- {
- ExecutionContext ec = Thread.CurrentThread.ExecutionContext;
- if (ec.SecurityContext != null)
- ec.SecurityContext.CompressedStack = cs;
- else if (cs != null) {
- SecurityContext sc = new SecurityContext();
- sc.CompressedStack = cs;
- ec.SecurityContext = sc;
- }
- }
-
-
- internal bool CheckDemand(CodeAccessPermission demand, PermissionToken permToken, RuntimeMethodHandle rmh)
- {
- CompleteConstruction(null);
-
- if (PLS == null)
- return SecurityRuntime.StackHalt;
- else
- return PLS.CheckDemand(demand, permToken, rmh);
-
- }
-
- internal bool CheckSetDemand(PermissionSet pset, RuntimeMethodHandle rmh)
- {
- CompleteConstruction(null);
-
- if (PLS == null)
- return SecurityRuntime.StackHalt;
- else
- return PLS.CheckSetDemand(pset, rmh);
- }
-
-
- internal void GetZoneAndOrigin(ArrayList zoneList, ArrayList originList, PermissionToken zoneToken, PermissionToken originToken)
- {
-
-
- CompleteConstruction(null);
- if (PLS != null)
- PLS.GetZoneAndOrigin(zoneList, originList, zoneToken, originToken);
- return;
- }
-
- internal void CompleteConstruction(CompressedStack innerCS)
- {
- if (PLS != null)
- return;
- PermissionListSet pls = PermissionListSet.CreateCompressedState(this, innerCS);
- lock (this) {
- if (PLS == null)
- m_pls = pls;
- }
- }
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- static internal extern SafeCompressedStackHandle GetDelayedCompressedStack(ref StackCrawlMark stackMark);
-
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- static internal extern void DestroyDelayedCompressedStack(IntPtr unmanagedCompressedStack);
-
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- static internal extern void DestroyDCSList(SafeCompressedStackHandle compressedStack);
-
-
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- static internal extern int GetDCSCount(SafeCompressedStackHandle compressedStack);
-
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- static internal extern bool IsImmediateCompletionCandidate(SafeCompressedStackHandle compressedStack, out CompressedStack innerCS);
-
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- static internal extern DomainCompressedStack GetDomainCompressedStack(SafeCompressedStackHandle compressedStack, int index);
-
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- static internal extern void GetHomogeneousPLS(PermissionListSet hgPLS);
-
-
- }
-
-
-
-
- [Serializable()]
- internal sealed class DomainCompressedStack
- {
-
- private PermissionListSet m_pls;
-
- private bool m_bHaltConstruction;
-
-
-
-
- internal PermissionListSet PLS {
- get { return m_pls; }
- }
-
- internal bool ConstructionHalted {
- get { return m_bHaltConstruction; }
- }
-
-
-
-
- private static DomainCompressedStack CreateManagedObject(IntPtr unmanagedDCS)
- {
- DomainCompressedStack newDCS = new DomainCompressedStack();
- newDCS.m_pls = PermissionListSet.CreateCompressedState(unmanagedDCS, out newDCS.m_bHaltConstruction);
-
- return newDCS;
- }
-
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- static internal extern int GetDescCount(IntPtr dcs);
-
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- static internal extern void GetDomainPermissionSets(IntPtr dcs, out PermissionSet granted, out PermissionSet refused);
-
-
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- static internal extern bool GetDescriptorInfo(IntPtr dcs, int index, out PermissionSet granted, out PermissionSet refused, out Assembly assembly, out FrameSecurityDescriptor fsd);
-
- [MethodImplAttribute(MethodImplOptions.InternalCall)]
- static internal extern bool IgnoreDomain(IntPtr dcs);
- }
-
- }