Example #1
0
        internal static void RunInternal(ExecutionContext executionContext, ContextCallback callback, object state, bool preserveSyncCtx)
        {
            if (!executionContext.IsPreAllocatedDefault)
            {
                executionContext.isNewCapture = false;
            }
            Thread currentThread          = Thread.CurrentThread;
            ExecutionContextSwitcher ecsw = new ExecutionContextSwitcher();

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                ExecutionContext.Reader executionContextReader = currentThread.GetExecutionContextReader();
                if ((executionContextReader.IsNull || executionContextReader.IsDefaultFTContext(preserveSyncCtx)) && (SecurityContext.CurrentlyInDefaultFTSecurityContext(executionContextReader) && executionContext.IsDefaultFTContext(preserveSyncCtx)) && executionContextReader.HasSameLocalValues(executionContext))
                {
                    ExecutionContext.EstablishCopyOnWriteScope(currentThread, true, ref ecsw);
                }
                else
                {
                    if (executionContext.IsPreAllocatedDefault)
                    {
                        executionContext = new ExecutionContext();
                    }
                    ecsw = ExecutionContext.SetExecutionContext(executionContext, preserveSyncCtx);
                }
                callback(state);
            }
            finally
            {
                ecsw.Undo();
            }
        }
Example #2
0
        internal void Undo()
        {
            if (this.thread == null)
            {
                return;
            }
            Thread thread = this.thread;

            if (this.hecsw != null)
            {
                HostExecutionContextSwitcher.Undo(this.hecsw);
            }
            ExecutionContext.Reader executionContextReader = thread.GetExecutionContextReader();
            ExecutionContext.Reader reader = this.outerEC;
            int num = this.outerECBelongsToScope ? 1 : 0;

            thread.SetExecutionContext(reader, num != 0);
            if (this.scsw.currSC != null)
            {
                this.scsw.Undo();
            }
            if (this.wiIsValid)
            {
                SecurityContext.RestoreCurrentWI(this.outerEC, executionContextReader, this.wi, this.cachedAlwaysFlowImpersonationPolicy);
            }
            this.thread = (Thread)null;
            ExecutionContext.OnAsyncLocalContextChanged(executionContextReader.DangerousGetRawExecutionContext(), this.outerEC.DangerousGetRawExecutionContext());
        }
        internal static void RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, bool preserveSyncCtx)
        {
            Contract.Assert(executionContext != null);
            if (executionContext.IsPreAllocatedDefault)
            {
                Contract.Assert(executionContext.IsDefaultFTContext(preserveSyncCtx));
            }
            else
            {
                Contract.Assert(executionContext.isNewCapture);
                executionContext.isNewCapture = false;
            }

            Thread currentThread          = Thread.CurrentThread;
            ExecutionContextSwitcher ecsw = default(ExecutionContextSwitcher);

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                ExecutionContext.Reader ec = currentThread.GetExecutionContextReader();
                if ((ec.IsNull || ec.IsDefaultFTContext(preserveSyncCtx)) &&
    #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK
                    SecurityContext.CurrentlyInDefaultFTSecurityContext(ec) &&
    #endif // #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK
                    executionContext.IsDefaultFTContext(preserveSyncCtx))
                {
                    // Neither context is interesting, so we don't need to set the context.
                    // We do need to reset any changes made by the user's callback,
                    // so here we establish a "copy-on-write scope".  Any changes will
                    // result in a copy of the context being made, preserving the original
                    // context.
                    EstablishCopyOnWriteScope(currentThread, true, ref ecsw);
                }
                else
                {
                    if (executionContext.IsPreAllocatedDefault)
                    {
                        executionContext = executionContext.CreateCopy();
                    }
                    ecsw = SetExecutionContext(executionContext, preserveSyncCtx);
                }

                //
                // Call the user's callback
                //
                callback(state);
            }
            finally
            {
                ecsw.Undo(currentThread);
            }
        }
Example #4
0
        internal static ExecutionContext Capture(ref StackCrawlMark stackMark, ExecutionContext.CaptureOptions options)
        {
            ExecutionContext.Reader executionContextReader = Thread.CurrentThread.GetExecutionContextReader();
            if (executionContextReader.IsFlowSuppressed)
            {
                return((ExecutionContext)null);
            }
            SecurityContext        securityContext        = SecurityContext.Capture(executionContextReader, ref stackMark);
            HostExecutionContext   executionContext1      = HostExecutionContextManager.CaptureHostExecutionContext();
            SynchronizationContext synchronizationContext = (SynchronizationContext)null;
            LogicalCallContext     logicalCallContext     = (LogicalCallContext)null;

            if (!executionContextReader.IsNull)
            {
                if ((options & ExecutionContext.CaptureOptions.IgnoreSyncCtx) == ExecutionContext.CaptureOptions.None)
                {
                    synchronizationContext = executionContextReader.SynchronizationContext == null ? (SynchronizationContext)null : executionContextReader.SynchronizationContext.CreateCopy();
                }
                if (executionContextReader.LogicalCallContext.HasInfo)
                {
                    logicalCallContext = executionContextReader.LogicalCallContext.Clone();
                }
            }
            Dictionary <IAsyncLocal, object> dictionary = (Dictionary <IAsyncLocal, object>)null;
            List <IAsyncLocal> asyncLocalList           = (List <IAsyncLocal>)null;

            if (!executionContextReader.IsNull)
            {
                dictionary     = executionContextReader.DangerousGetRawExecutionContext()._localValues;
                asyncLocalList = executionContextReader.DangerousGetRawExecutionContext()._localChangeNotifications;
            }
            if ((options & ExecutionContext.CaptureOptions.OptimizeDefaultCase) != ExecutionContext.CaptureOptions.None && securityContext == null && (executionContext1 == null && synchronizationContext == null) && ((logicalCallContext == null || !logicalCallContext.HasInfo) && (dictionary == null && asyncLocalList == null)))
            {
                return(ExecutionContext.s_dummyDefaultEC);
            }
            ExecutionContext executionContext2 = new ExecutionContext();

            executionContext2.SecurityContext = securityContext;
            if (executionContext2.SecurityContext != null)
            {
                executionContext2.SecurityContext.ExecutionContext = executionContext2;
            }
            executionContext2._hostExecutionContext     = executionContext1;
            executionContext2._syncContext              = synchronizationContext;
            executionContext2.LogicalCallContext        = logicalCallContext;
            executionContext2._localValues              = dictionary;
            executionContext2._localChangeNotifications = asyncLocalList;
            executionContext2.isNewCapture              = true;
            return(executionContext2);
        }
Example #5
0
        internal static ExecutionContext Capture(ref StackCrawlMark stackMark, ExecutionContext.CaptureOptions options)
        {
            ExecutionContext.Reader executionContextReader = Thread.CurrentThread.GetExecutionContextReader();
            if (executionContextReader.IsFlowSuppressed)
            {
                return(null);
            }
            SecurityContext        securityContext        = SecurityContext.Capture(executionContextReader, ref stackMark);
            HostExecutionContext   hostExecutionContext   = HostExecutionContextManager.CaptureHostExecutionContext();
            SynchronizationContext synchronizationContext = null;
            LogicalCallContext     logicalCallContext     = null;

            if (!executionContextReader.IsNull)
            {
                if ((options & ExecutionContext.CaptureOptions.IgnoreSyncCtx) == ExecutionContext.CaptureOptions.None)
                {
                    synchronizationContext = ((executionContextReader.SynchronizationContext == null) ? null : executionContextReader.SynchronizationContext.CreateCopy());
                }
                if (executionContextReader.LogicalCallContext.HasInfo)
                {
                    logicalCallContext = executionContextReader.LogicalCallContext.Clone();
                }
            }
            IAsyncLocalValueMap asyncLocalValueMap = null;

            IAsyncLocal[] array = null;
            if (!executionContextReader.IsNull)
            {
                asyncLocalValueMap = executionContextReader.DangerousGetRawExecutionContext()._localValues;
                array = executionContextReader.DangerousGetRawExecutionContext()._localChangeNotifications;
            }
            if ((options & ExecutionContext.CaptureOptions.OptimizeDefaultCase) != ExecutionContext.CaptureOptions.None && securityContext == null && hostExecutionContext == null && synchronizationContext == null && (logicalCallContext == null || !logicalCallContext.HasInfo) && asyncLocalValueMap == null && array == null)
            {
                return(ExecutionContext.s_dummyDefaultEC);
            }
            ExecutionContext executionContext = new ExecutionContext();

            executionContext.SecurityContext = securityContext;
            if (executionContext.SecurityContext != null)
            {
                executionContext.SecurityContext.ExecutionContext = executionContext;
            }
            executionContext._hostExecutionContext     = hostExecutionContext;
            executionContext._syncContext              = synchronizationContext;
            executionContext.LogicalCallContext        = logicalCallContext;
            executionContext._localValues              = asyncLocalValueMap;
            executionContext._localChangeNotifications = array;
            executionContext.isNewCapture              = true;
            return(executionContext);
        }
Example #6
0
        internal static ExecutionContextSwitcher SetExecutionContext(ExecutionContext executionContext, bool preserveSyncCtx)
        {
            StackCrawlMark           stackMark = StackCrawlMark.LookForMyCaller;
            ExecutionContextSwitcher executionContextSwitcher = new ExecutionContextSwitcher();
            Thread currentThread = Thread.CurrentThread;

            ExecutionContext.Reader executionContextReader = currentThread.GetExecutionContextReader();
            executionContextSwitcher.thread  = currentThread;
            executionContextSwitcher.outerEC = executionContextReader;
            executionContextSwitcher.outerECBelongsToScope = currentThread.ExecutionContextBelongsToCurrentScope;
            if (preserveSyncCtx)
            {
                executionContext.SynchronizationContext = executionContextReader.SynchronizationContext;
            }
            executionContext.SynchronizationContextNoFlow = executionContextReader.SynchronizationContextNoFlow;
            currentThread.SetExecutionContext(executionContext, true);
            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                ExecutionContext.OnAsyncLocalContextChanged(executionContextReader.DangerousGetRawExecutionContext(), executionContext);
                SecurityContext securityContext1 = executionContext.SecurityContext;
                if (securityContext1 != null)
                {
                    SecurityContext.Reader securityContext2 = executionContextReader.SecurityContext;
                    executionContextSwitcher.scsw = SecurityContext.SetSecurityContext(securityContext1, securityContext2, false, ref stackMark);
                }
                else if (!SecurityContext.CurrentlyInDefaultFTSecurityContext(executionContextSwitcher.outerEC))
                {
                    SecurityContext.Reader securityContext2 = executionContextReader.SecurityContext;
                    executionContextSwitcher.scsw = SecurityContext.SetSecurityContext(SecurityContext.FullTrustSecurityContext, securityContext2, false, ref stackMark);
                }
                HostExecutionContext executionContext1 = executionContext.HostExecutionContext;
                if (executionContext1 != null)
                {
                    executionContextSwitcher.hecsw = HostExecutionContextManager.SetHostExecutionContextInternal(executionContext1);
                }
            }
            catch
            {
                executionContextSwitcher.UndoNoThrow();
                throw;
            }
            return(executionContextSwitcher);
        }
        [System.Security.SecurityCritical]  // auto-generated
        static internal ExecutionContext Capture(ref StackCrawlMark stackMark, CaptureOptions options)
        {
            ExecutionContext.Reader ecCurrent = Thread.CurrentThread.GetExecutionContextReader();

            // check to see if Flow is suppressed
            if (ecCurrent.IsFlowSuppressed)
            {
                return(null);
            }

            //
            // Attempt to capture context.  There may be nothing to capture...
            //

#if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK
            // capture the security context
            SecurityContext secCtxNew = SecurityContext.Capture(ecCurrent, ref stackMark);
#endif // #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK
#if FEATURE_CAS_POLICY
            // capture the host execution context
            HostExecutionContext hostCtxNew = HostExecutionContextManager.CaptureHostExecutionContext();
#endif // FEATURE_CAS_POLICY

#if FEATURE_SYNCHRONIZATIONCONTEXT
            SynchronizationContext syncCtxNew = null;
#endif
            LogicalCallContext logCtxNew = null;

            if (!ecCurrent.IsNull)
            {
#if FEATURE_SYNCHRONIZATIONCONTEXT
                // capture the [....] context
                if (0 == (options & CaptureOptions.IgnoreSyncCtx))
                {
                    syncCtxNew = (ecCurrent.SynchronizationContext == null) ? null : ecCurrent.SynchronizationContext.CreateCopy();
                }
#endif // #if FEATURE_SYNCHRONIZATIONCONTEXT

                // copy over the Logical Call Context
                if (ecCurrent.LogicalCallContext.HasInfo)
                {
                    logCtxNew = ecCurrent.LogicalCallContext.Clone();
                }
            }

            //
            // If we didn't get anything but defaults, and we're allowed to return the
            // dummy default EC, don't bother allocating a new context.
            //
            if (0 != (options & CaptureOptions.OptimizeDefaultCase) &&
#if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK
                secCtxNew == null &&
#endif
#if FEATURE_CAS_POLICY
                hostCtxNew == null &&
#endif // FEATURE_CAS_POLICY
#if FEATURE_SYNCHRONIZATIONCONTEXT
                syncCtxNew == null &&
#endif // #if FEATURE_SYNCHRONIZATIONCONTEXT
                (logCtxNew == null || !logCtxNew.HasInfo))
            {
                return(s_dummyDefaultEC);
            }

            //
            // Allocate the new context, and fill it in.
            //
            ExecutionContext ecNew = new ExecutionContext();
#if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK
            ecNew.SecurityContext = secCtxNew;
            if (ecNew.SecurityContext != null)
            {
                ecNew.SecurityContext.ExecutionContext = ecNew;
            }
#endif
#if FEATURE_CAS_POLICY
            ecNew._hostExecutionContext = hostCtxNew;
#endif // FEATURE_CAS_POLICY
#if FEATURE_SYNCHRONIZATIONCONTEXT
            ecNew._syncContext = syncCtxNew;
#endif // #if FEATURE_SYNCHRONIZATIONCONTEXT
            ecNew.LogicalCallContext = logCtxNew;
            ecNew.isNewCapture       = true;

            return(ecNew);
        }
        internal void Undo(Thread currentThread)
        {
            //
            // Don't use an uninitialized switcher, or one that's already been used.
            //
            if (thread == null)
            {
                return; // Don't do anything
            }
            Contract.Assert(currentThread == this.thread);

            //
            // Restore the HostExecutionContext before restoring the ExecutionContext.
            //
#if FEATURE_CAS_POLICY
            if (hecsw != null)
            {
                HostExecutionContextSwitcher.Undo(hecsw);
            }
#endif // FEATURE_CAS_POLICY

            //
            // restore the saved Execution Context.  Note that this will also restore the
            // SynchronizationContext, Logical/IllogicalCallContext, etc.
            //
#if !FEATURE_PAL && FEATURE_IMPERSONATION
            ExecutionContext.Reader innerEC = currentThread.GetExecutionContextReader();
#endif
            currentThread.SetExecutionContext(outerEC, outerECBelongsToScope);

#if DEBUG
            try
            {
                currentThread.ForbidExecutionContextMutation = true;
#endif
            //
            // Tell the SecurityContext to do the side-effects of restoration.
            //
#if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK
            if (scsw.currSC != null)
            {
                // Any critical failure inside scsw will cause FailFast
                scsw.Undo();
            }
#endif // #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK

#if !FEATURE_PAL && FEATURE_IMPERSONATION
            if (wiIsValid)
            {
                SecurityContext.RestoreCurrentWI(outerEC, innerEC, wi, cachedAlwaysFlowImpersonationPolicy);
            }
#endif

            thread = null;     // this will prevent the switcher object being used again
#if DEBUG
        }

        finally
        {
            currentThread.ForbidExecutionContextMutation = false;
        }
#endif
        }
        [HandleProcessCorruptedStateExceptions]             //
#endif // FEATURE_CORRUPTING_EXCEPTIONS
        internal static ExecutionContextSwitcher SetExecutionContext(ExecutionContext executionContext, bool preserveSyncCtx)
        {
#if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK
            StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
#endif // #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK

            Contract.Assert(executionContext != null);
            Contract.Assert(executionContext != s_dummyDefaultEC);

            // Set up the switcher object to return;
            ExecutionContextSwitcher ecsw = new ExecutionContextSwitcher();

            Thread currentThread            = Thread.CurrentThread;
            ExecutionContext.Reader outerEC = currentThread.GetExecutionContextReader();

            ecsw.thread  = currentThread;
            ecsw.outerEC = outerEC;
            ecsw.outerECBelongsToScope = currentThread.ExecutionContextBelongsToCurrentScope;

            if (preserveSyncCtx)
            {
                executionContext.SynchronizationContext = outerEC.SynchronizationContext;
            }
            executionContext.SynchronizationContextNoFlow = outerEC.SynchronizationContextNoFlow;

            currentThread.SetExecutionContext(executionContext, belongsToCurrentScope: true);

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
#if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK
                //set the security context
                SecurityContext sc = executionContext.SecurityContext;
                if (sc != null)
                {
                    // non-null SC: needs to be set
                    SecurityContext.Reader prevSeC = outerEC.SecurityContext;
                    ecsw.scsw = SecurityContext.SetSecurityContext(sc, prevSeC, false, ref stackMark);
                }
                else if (!SecurityContext.CurrentlyInDefaultFTSecurityContext(ecsw.outerEC))
                {
                    // null incoming SC, but we're currently not in FT: use static FTSC to set
                    SecurityContext.Reader prevSeC = outerEC.SecurityContext;
                    ecsw.scsw = SecurityContext.SetSecurityContext(SecurityContext.FullTrustSecurityContext, prevSeC, false, ref stackMark);
                }
#endif // #if FEATURE_IMPERSONATION || FEATURE_COMPRESSEDSTACK
#if FEATURE_CAS_POLICY
                // set the Host Context
                HostExecutionContext hostContext = executionContext.HostExecutionContext;
                if (hostContext != null)
                {
                    ecsw.hecsw = HostExecutionContextManager.SetHostExecutionContextInternal(hostContext);
                }
#endif // FEATURE_CAS_POLICY
            }
            catch
            {
                ecsw.UndoNoThrow();
                throw;
            }
            return(ecsw);
        }
 //public Thread Thread { get { return m_ec._thread; } }
 public bool IsSame(ExecutionContext.Reader other)
 {
     return(m_ec == other.m_ec);
 }