Exemplo n.º 1
0
        public void GetObjectData_Null()
        {
            StreamingContext sc = new StreamingContext();
            CompressedStack  cs = CompressedStack.Capture();

            cs.GetObjectData(null, sc);
        }
Exemplo n.º 2
0
        public static void Capture_GetCompressedStack_CreateCopy_Test()
        {
            CompressedStack compressedStack = CompressedStack.Capture();

            Assert.NotNull(compressedStack);
            Assert.NotNull(compressedStack.CreateCopy());
            Assert.NotNull(CompressedStack.GetCompressedStack());
            Assert.NotNull(CompressedStack.GetCompressedStack().CreateCopy());
        }
Exemplo n.º 3
0
 public void CreateCopy()
 {
     CompressedStack cs1 = CompressedStack.Capture ();
     CompressedStack cs2 = cs1.CreateCopy ();
     Assert.IsFalse (cs1.Equals (cs2), "cs1.Equals (cs2)");
     Assert.IsFalse (cs2.Equals (cs1), "cs2.Equals (cs1)");
     Assert.IsFalse (cs1.GetHashCode () == cs2.GetHashCode (), "GetHashCode");
     Assert.IsFalse (Object.ReferenceEquals (cs1, cs2), "ReferenceEquals");
 }
Exemplo n.º 4
0
        public void GetCompressedStack()
        {
            CompressedStack cs1 = CompressedStack.GetCompressedStack ();
            Assert.IsNotNull (cs1, "GetCompressedStack");

            CompressedStack cs2 = CompressedStack.Capture ();
            Assert.IsNotNull (cs2, "Capture");

            Assert.IsFalse (cs1.Equals (cs2), "cs1.Equals (cs2)");
            Assert.IsFalse (cs2.Equals (cs1), "cs2.Equals (cs1)");
            Assert.IsFalse (cs1.GetHashCode () == cs2.GetHashCode (), "GetHashCode");
        }
        /// <summary>Captures the security context for the current thread.</summary>
        /// <returns>A <see cref="T:System.Security.SecurityContext" /> object representing the security context for the current thread.</returns>
        public static SecurityContext Capture()
        {
            SecurityContext securityContext = Thread.CurrentThread.ExecutionContext.SecurityContext;

            if (securityContext.FlowSuppressed)
            {
                return(null);
            }
            return(new SecurityContext
            {
                _capture = true,
                _winid = WindowsIdentity.GetCurrentToken(),
                _stack = CompressedStack.Capture()
            });
        }
Exemplo n.º 6
0
        // static methods

        static public SecurityContext Capture()
        {
            SecurityContext sc = Thread.CurrentThread.ExecutionContext.SecurityContext;

            if (sc.FlowSuppressed)
            {
                return(null);
            }

            SecurityContext capture = new SecurityContext();

            capture._capture = true;
            capture._winid   = WindowsIdentity.GetCurrentToken();
            capture._stack   = CompressedStack.Capture();
            return(capture);
        }
Exemplo n.º 7
0
        public static void SerializationTest()
        {
            CompressedStack compressedStack = CompressedStack.Capture();

            Assert.Throws <ArgumentNullException>(() => compressedStack.GetObjectData(null, new StreamingContext()));

            var binaryFormatter = new BinaryFormatter();
            var memoryStream    = new MemoryStream();

            binaryFormatter.Serialize(memoryStream, compressedStack);
            memoryStream.Close();
            byte[] binaryData = memoryStream.ToArray();

            memoryStream    = new MemoryStream(binaryData);
            compressedStack = (CompressedStack)binaryFormatter.Deserialize(memoryStream);
            memoryStream.Close();
        }
Exemplo n.º 8
0
        public static void RunTest()
        {
            CompressedStack compressedStack = CompressedStack.Capture();

            Assert.Throws <NullReferenceException>(() => CompressedStack.Run(compressedStack, null, null));

            var    obj         = new object();
            Thread mainThread  = Thread.CurrentThread;
            bool   callbackRan = false;

            CompressedStack.Run(
                compressedStack,
                state =>
            {
                Assert.Same(obj, state);
                Assert.Same(mainThread, Thread.CurrentThread);
                callbackRan = true;
            },
                obj);
            Assert.True(callbackRan);
        }
Exemplo n.º 9
0
        public static void MiscellaneousTest()
        {
            Thread.BeginCriticalRegion();
            Thread.EndCriticalRegion();
            Thread.BeginThreadAffinity();
            Thread.EndThreadAffinity();

            ThreadTestHelpers.RunTestInBackgroundThread(() =>
            {
                // TODO: Port tests for these once all of the necessary interop APIs are available
                Thread.CurrentThread.DisableComObjectEagerCleanup();
                Marshal.CleanupUnusedObjectsInCurrentContext();
            });

#pragma warning disable 618 // obsolete members
            Assert.Throws <InvalidOperationException>(() => Thread.CurrentThread.GetCompressedStack());
            Assert.Throws <InvalidOperationException>(() => Thread.CurrentThread.SetCompressedStack(CompressedStack.Capture()));
#pragma warning restore 618 // obsolete members

            Thread.MemoryBarrier();

            var ad = Thread.GetDomain();
            Assert.NotNull(ad);
            Assert.Equal(AppDomain.CurrentDomain, ad);
            Assert.Equal(ad.Id, Thread.GetDomainID());

            Thread.SpinWait(int.MinValue);
            Thread.SpinWait(-1);
            Thread.SpinWait(0);
            Thread.SpinWait(1);
            Thread.Yield();
        }
Exemplo n.º 10
0
 public void Run_Capture()
 {
     Assert.IsFalse(success, "pre-check");
     CompressedStack.Run(CompressedStack.Capture(), new ContextCallback(Callback), true);
     Assert.IsTrue(success, "post-check");
 }
Exemplo n.º 11
0
        public static void MiscellaneousTest()
        {
            Thread.BeginCriticalRegion();
            Thread.EndCriticalRegion();
            Thread.BeginThreadAffinity();
            Thread.EndThreadAffinity();

#pragma warning disable 618 // obsolete members
            Assert.Throws <InvalidOperationException>(() => Thread.CurrentThread.GetCompressedStack());
            Assert.Throws <InvalidOperationException>(() => Thread.CurrentThread.SetCompressedStack(CompressedStack.Capture()));
#pragma warning restore 618 // obsolete members

            Thread.MemoryBarrier();

            var ad = Thread.GetDomain();
            Assert.NotNull(ad);
            Assert.Equal(AppDomain.CurrentDomain, ad);
            Assert.Equal(ad.Id, Thread.GetDomainID());

            Thread.SpinWait(int.MinValue);
            Thread.SpinWait(-1);
            Thread.SpinWait(0);
            Thread.SpinWait(1);
            Thread.Yield();
        }
Exemplo n.º 12
0
        [System.Security.SecurityCritical]  // auto-generated
        private unsafe void Init(String name,
                                 MethodAttributes attributes,
                                 CallingConventions callingConvention,
                                 Type returnType,
                                 Type[] signature,
                                 Type owner,
                                 Module m,
                                 bool skipVisibility,
                                 bool transparentMethod,
                                 ref StackCrawlMark stackMark)
        {
            DynamicMethod.CheckConsistency(attributes, callingConvention);

            // check and store the signature
            if (signature != null)
            {
                m_parameterTypes = new RuntimeType[signature.Length];
                for (int i = 0; i < signature.Length; i++)
                {
                    if (signature[i] == null)
                    {
                        throw new ArgumentException(Environment.GetResourceString("Arg_InvalidTypeInSignature"));
                    }
                    m_parameterTypes[i] = signature[i].UnderlyingSystemType as RuntimeType;
                    if (m_parameterTypes[i] == null || !(m_parameterTypes[i] is RuntimeType) || m_parameterTypes[i] == (RuntimeType)typeof(void))
                    {
                        throw new ArgumentException(Environment.GetResourceString("Arg_InvalidTypeInSignature"));
                    }
                }
            }
            else
            {
                m_parameterTypes = Array.Empty <RuntimeType>();
            }

            // check and store the return value
            m_returnType = (returnType == null) ? (RuntimeType)typeof(void) : returnType.UnderlyingSystemType as RuntimeType;
            if ((m_returnType == null) || !(m_returnType is RuntimeType) || m_returnType.IsByRef)
            {
                throw new NotSupportedException(Environment.GetResourceString("Arg_InvalidTypeInRetType"));
            }

            if (transparentMethod)
            {
                Contract.Assert(owner == null && m == null, "owner and m cannot be set for transparent methods");
                m_module = GetDynamicMethodsModule();
                if (skipVisibility)
                {
                    m_restrictedSkipVisibility = true;
                }

#if FEATURE_COMPRESSEDSTACK
                m_creationContext = CompressedStack.Capture();
#endif // FEATURE_COMPRESSEDSTACK
            }
            else
            {
                Contract.Assert(m != null || owner != null, "PerformSecurityCheck should ensure that either m or owner is set");
                Contract.Assert(m == null || !m.Equals(s_anonymouslyHostedDynamicMethodsModule), "The user cannot explicitly use this assembly");
                Contract.Assert(m == null || owner == null, "m and owner cannot both be set");

                if (m != null)
                {
                    m_module = m.ModuleHandle.GetRuntimeModule(); // this returns the underlying module for all RuntimeModule and ModuleBuilder objects.
                }
                else
                {
                    RuntimeType rtOwner = null;
                    if (owner != null)
                    {
                        rtOwner = owner.UnderlyingSystemType as RuntimeType;
                    }

                    if (rtOwner != null)
                    {
                        if (rtOwner.HasElementType || rtOwner.ContainsGenericParameters ||
                            rtOwner.IsGenericParameter || rtOwner.IsInterface)
                        {
                            throw new ArgumentException(Environment.GetResourceString("Argument_InvalidTypeForDynamicMethod"));
                        }

                        m_typeOwner = rtOwner;
                        m_module    = rtOwner.GetRuntimeModule();
                    }
                }

                m_skipVisibility = skipVisibility;
            }

            // initialize remaining fields
            m_ilGenerator  = null;
            m_fInitLocals  = true;
            m_methodHandle = null;

            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

#if FEATURE_APPX
            if (AppDomain.ProfileAPICheck)
            {
                if (m_creatorAssembly == null)
                {
                    m_creatorAssembly = RuntimeAssembly.GetExecutingAssembly(ref stackMark);
                }

                if (m_creatorAssembly != null && !m_creatorAssembly.IsFrameworkAssembly())
                {
                    m_profileAPICheck = true;
                }
            }
#endif // FEATURE_APPX

            m_dynMethod = new RTDynamicMethod(this, name, attributes, callingConvention);
        }
Exemplo n.º 13
0
 private CompressedStack GetCompressedStackUnmanaged()
 {
     return(CompressedStack.Capture());
     // the Deny disappears with this stack frame but we got a capture of it
 }
 private void Init(string name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] signature, Type owner, System.Reflection.Module m, bool skipVisibility, bool transparentMethod)
 {
     CheckConsistency(attributes, callingConvention);
     if (signature != null)
     {
         this.m_parameterTypes = new RuntimeType[signature.Length];
         for (int i = 0; i < signature.Length; i++)
         {
             if (signature[i] == null)
             {
                 throw new ArgumentException(Environment.GetResourceString("Arg_InvalidTypeInSignature"));
             }
             this.m_parameterTypes[i] = signature[i].UnderlyingSystemType as RuntimeType;
             if (((this.m_parameterTypes[i] == null) || !this.m_parameterTypes[i].IsRuntimeType) || (this.m_parameterTypes[i] == typeof(void)))
             {
                 throw new ArgumentException(Environment.GetResourceString("Arg_InvalidTypeInSignature"));
             }
         }
     }
     else
     {
         this.m_parameterTypes = new RuntimeType[0];
     }
     this.m_returnType = (returnType == null) ? ((RuntimeType)typeof(void)) : (returnType.UnderlyingSystemType as RuntimeType);
     if (((this.m_returnType == null) || !this.m_returnType.IsRuntimeType) || this.m_returnType.IsByRef)
     {
         throw new NotSupportedException(Environment.GetResourceString("Arg_InvalidTypeInRetType"));
     }
     if (transparentMethod)
     {
         this.m_module = GetDynamicMethodsModule();
         if (skipVisibility)
         {
             this.m_restrictedSkipVisibility = true;
             this.m_creationContext          = CompressedStack.Capture();
         }
     }
     else
     {
         if (m != null)
         {
             this.m_module = m.ModuleHandle.GetRuntimeModule();
         }
         else if (((owner != null) && (owner.UnderlyingSystemType != null)) && owner.UnderlyingSystemType.IsRuntimeType)
         {
             this.m_typeOwner = owner.UnderlyingSystemType.TypeHandle.GetRuntimeType();
             if ((this.m_typeOwner.HasElementType || this.m_typeOwner.ContainsGenericParameters) || (this.m_typeOwner.IsGenericParameter || this.m_typeOwner.IsInterface))
             {
                 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidTypeForDynamicMethod"));
             }
             this.m_module = (RuntimeModule)this.m_typeOwner.Module;
         }
         this.m_skipVisibility = skipVisibility;
     }
     this.m_ilGenerator  = null;
     this.m_fInitLocals  = true;
     this.m_methodHandle = null;
     if (name == null)
     {
         throw new ArgumentNullException("name");
     }
     this.m_dynMethod = new RTDynamicMethod(this, name, attributes, callingConvention);
 }