15 internal class CompressedStackRunData
21 internal object state;
23 internal CompressedStackSwitcher cssw;
30 cssw =
default(CompressedStackSwitcher);
34 private volatile PermissionListSet m_pls;
37 private volatile SafeCompressedStackHandle m_csHandle;
39 private bool m_canSkipEvaluation;
45 internal bool CanSkipEvaluation
49 return m_canSkipEvaluation;
53 m_canSkipEvaluation = value;
57 internal PermissionListSet PLS => m_pls;
59 internal SafeCompressedStackHandle CompressedStackHandle
62 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
68 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
78 m_csHandle = csHandle;
82 private CompressedStack(SafeCompressedStackHandle csHandle, PermissionListSet pls)
84 m_csHandle = csHandle;
100 CompleteConstruction(
null);
101 info.AddValue(
"PLS", m_pls);
106 m_pls = (PermissionListSet)info.GetValue(
"PLS", typeof(PermissionListSet));
116 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
125 if (CodeAccessSecurityEngine.QuickCheckForAllDemands())
128 compressedStack.CanSkipEvaluation =
true;
132 if (!CodeAccessSecurityEngine.AllDomainsHomogeneousWithNoStackModifiers())
134 compressedStack =
new CompressedStack(
null);
138 return compressedStack;
142 compressedStack.CompressedStackHandle = GetDelayedCompressedStack(ref stackMark, walkStack:
true);
143 if (compressedStack.CompressedStackHandle !=
null && IsImmediateCompletionCandidate(compressedStack.CompressedStackHandle, out innerCS))
147 compressedStack.CompleteConstruction(innerCS);
151 DestroyDCSList(compressedStack.CompressedStackHandle);
156 compressedStack =
new CompressedStack(GetDelayedCompressedStack(ref stackMark, walkStack:
false));
157 compressedStack.m_pls = PermissionListSet.CreateCompressedState_HG();
159 return compressedStack;
165 [SecuritySafeCritical]
168 StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
181 if (compressedStack ==
null)
185 if (cleanupCode ==
null)
187 tryCode = runTryCode;
188 cleanupCode = runFinallyCode;
190 CompressedStackRunData userData =
new CompressedStackRunData(compressedStack, callback, state);
195 internal static void runTryCode(
object userData)
197 CompressedStackRunData compressedStackRunData = (CompressedStackRunData)userData;
198 compressedStackRunData.cssw = SetCompressedStack(compressedStackRunData.cs, GetCompressedStackThread());
199 compressedStackRunData.callBack(compressedStackRunData.state);
204 internal static void runFinallyCode(
object userData,
bool exceptionThrown)
206 CompressedStackRunData compressedStackRunData = (CompressedStackRunData)userData;
207 compressedStackRunData.cssw.Undo();
211 [HandleProcessCorruptedStateExceptions]
212 internal static CompressedStackSwitcher SetCompressedStack(CompressedStack cs, CompressedStack prevCS)
214 CompressedStackSwitcher result =
default(CompressedStackSwitcher);
225 SetCompressedStackThread(cs);
226 result.prev_CS = prevCS;
228 result.prev_ADStack = SetAppDomainStack(cs);
233 result.UndoNoThrow();
240 [SecuritySafeCritical]
248 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
255 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
256 internal static void RestoreAppDomainStack(
IntPtr appDomainStack)
258 Thread.CurrentThread.RestoreAppDomainStack(appDomainStack);
262 internal static CompressedStack GetCompressedStackThread()
264 return Thread.CurrentThread.GetExecutionContextReader().SecurityContext.CompressedStack;
268 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.MayFail)]
269 internal static void SetCompressedStackThread(CompressedStack cs)
271 Thread currentThread = Thread.CurrentThread;
272 if (currentThread.GetExecutionContextReader().SecurityContext.CompressedStack != cs)
274 ExecutionContext mutableExecutionContext = currentThread.GetMutableExecutionContext();
275 if (mutableExecutionContext.SecurityContext !=
null)
277 mutableExecutionContext.SecurityContext.CompressedStack = cs;
282 securityContext.CompressedStack = cs;
283 mutableExecutionContext.SecurityContext = securityContext;
289 internal bool CheckDemand(
CodeAccessPermission demand, PermissionToken permToken, RuntimeMethodHandleInternal rmh)
291 CompleteConstruction(
null);
296 PLS.CheckDemand(demand, permToken, rmh);
301 internal bool CheckDemandNoHalt(
CodeAccessPermission demand, PermissionToken permToken, RuntimeMethodHandleInternal rmh)
303 CompleteConstruction(
null);
308 return PLS.CheckDemand(demand, permToken, rmh);
312 internal bool CheckSetDemand(
PermissionSet pset, RuntimeMethodHandleInternal rmh)
314 CompleteConstruction(
null);
319 return PLS.CheckSetDemand(pset, rmh);
323 internal bool CheckSetDemandWithModificationNoHalt(
PermissionSet pset, out
PermissionSet alteredDemandSet, RuntimeMethodHandleInternal rmh)
325 alteredDemandSet =
null;
326 CompleteConstruction(
null);
331 return PLS.CheckSetDemandWithModification(pset, out alteredDemandSet, rmh);
335 internal void DemandFlagsOrGrantSet(
int flags,
PermissionSet grantSet)
337 CompleteConstruction(
null);
340 PLS.DemandFlagsOrGrantSet(flags, grantSet);
345 internal void GetZoneAndOrigin(
ArrayList zoneList,
ArrayList originList, PermissionToken zoneToken, PermissionToken originToken)
347 CompleteConstruction(
null);
350 PLS.GetZoneAndOrigin(zoneList, originList, zoneToken, originToken);
355 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.MayFail)]
356 internal void CompleteConstruction(CompressedStack innerCS)
360 PermissionListSet pls = PermissionListSet.CreateCompressedState(
this, innerCS);
373 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.MayFail)]
374 internal static extern SafeCompressedStackHandle GetDelayedCompressedStack(ref StackCrawlMark stackMark,
bool walkStack);
378 internal static extern void DestroyDelayedCompressedStack(IntPtr unmanagedCompressedStack);
382 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
383 internal static extern void DestroyDCSList(SafeCompressedStackHandle compressedStack);
387 internal static extern int GetDCSCount(SafeCompressedStackHandle compressedStack);
391 [ReliabilityContract(
Consistency.WillNotCorruptState,
Cer.Success)]
392 internal static extern bool IsImmediateCompletionCandidate(SafeCompressedStackHandle compressedStack, out CompressedStack innerCS);
396 internal static extern DomainCompressedStack GetDomainCompressedStack(SafeCompressedStackHandle compressedStack,
int index);
400 internal static extern void GetHomogeneousPLS(PermissionListSet hgPLS);
static Thread CurrentThread
Gets the currently running thread.
delegate void TryCode(object userData)
Represents a delegate to code that should be run in a try block..
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
delegate void ContextCallback(object state)
Represents a method to be called within a new context.
static void ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, object userData)
Executes code using a T:System.Delegate while using another T:System.Delegate to execute additional c...
void GetObjectData(SerializationInfo info, StreamingContext context)
Sets the T:System.Runtime.Serialization.SerializationInfo object with the logical context information...
Encapsulates and propagates all security-related data for execution contexts transferred across threa...
static void Run(CompressedStack compressedStack, ContextCallback callback, object state)
Runs a method in the specified compressed stack on the current thread.
Describes the source and destination of a given serialized stream, and provides an additional caller-...
Cer
Specifies a method's behavior when called within a constrained execution region.
Provides information about, and means to manipulate, the current environment and platform....
static CompressedStack GetCompressedStack()
Gets the compressed stack for the current thread.
Represents a collection that can contain many different types of permissions.
Defines the underlying structure of all code access permissions.
A platform-specific type that is used to represent a pointer or a handle.
static void PrepareConstrainedRegions()
Designates a body of code as a constrained execution region (CER).
MethodImplOptions
Defines the details of how a method is implemented.
Stores all the data needed to serialize or deserialize an object. This class cannot be inherited.
The exception that is thrown when one of the arguments provided to a method is not valid.
static CompressedStack Capture()
Captures the compressed stack from the current thread.
Allows an object to control its own serialization and deserialization.
delegate void CleanupCode(object userData, bool exceptionThrown)
Represents a method to run when an exception occurs.
Provides methods for setting and capturing the compressed stack on the current thread....
Specifies that the class can be serialized.
Consistency
Specifies a reliability contract.
CompressedStack CreateCopy()
Creates a copy of the current compressed stack.
Provides a set of static methods and properties that provide support for compilers....
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Creates and controls a thread, sets its priority, and gets its status.