Ejemplo n.º 1
0
        public unsafe Mutex(bool initiallyOwned, string name, out bool createdNew, MutexSecurity mutexSecurity)
        {
            if ((name != null) && (260 < name.Length))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", new object[] { name }));
            }
            Win32Native.SECURITY_ATTRIBUTES structure = null;
            if (mutexSecurity != null)
            {
                structure = new Win32Native.SECURITY_ATTRIBUTES {
                    nLength = Marshal.SizeOf(structure)
                };
                byte[] securityDescriptorBinaryForm = mutexSecurity.GetSecurityDescriptorBinaryForm();
                byte * pDest = stackalloc byte[(IntPtr)securityDescriptorBinaryForm.Length];
                Buffer.memcpy(securityDescriptorBinaryForm, 0, pDest, 0, securityDescriptorBinaryForm.Length);
                structure.pSecurityDescriptor = pDest;
            }
            RuntimeHelpers.CleanupCode backoutCode = new RuntimeHelpers.CleanupCode(this.MutexCleanupCode);
            MutexCleanupInfo           cleanupInfo = new MutexCleanupInfo(null, false);
            MutexTryCodeHelper         helper      = new MutexTryCodeHelper(initiallyOwned, cleanupInfo, name, structure, this);

            RuntimeHelpers.TryCode code = new RuntimeHelpers.TryCode(helper.MutexTryCode);
            RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(code, backoutCode, cleanupInfo);
            createdNew = helper.m_newMutex;
        }
            internal String GetResourceString(String key)
            {
                if (key == null || key.Length == 0)
                {
                    BCLDebug.Assert(false, "Environment::GetResourceString with null or empty key.  Bug in caller, or weird recursive loading problem?");
                    return("[Resource lookup failed - null or empty resource name]");
                }

                // We have a somewhat common potential for infinite
                // loops with mscorlib's ResourceManager.  If "potentially dangerous"
                // code throws an exception, we will get into an infinite loop
                // inside the ResourceManager and this "potentially dangerous" code.
                // Potentially dangerous code includes the IO package, CultureInfo,
                // parts of the loader, some parts of Reflection, Security (including
                // custom user-written permissions that may parse an XML file at
                // class load time), assembly load event handlers, etc.  Essentially,
                // this is not a bounded set of code, and we need to fix the problem.
                // Fortunately, this is limited to mscorlib's error lookups and is NOT
                // a general problem for all user code using the ResourceManager.

                // The solution is to make sure only one thread at a time can call
                // GetResourceString.  Also, since resource lookups can be
                // reentrant, if the same thread comes into GetResourceString
                // twice looking for the exact same resource name before
                // returning, we're going into an infinite loop and we should
                // return a bogus string.

                GetResourceStringUserData userData = new GetResourceStringUserData(this, key);

                RuntimeHelpers.TryCode     tryCode     = new RuntimeHelpers.TryCode(GetResourceStringCode);
                RuntimeHelpers.CleanupCode cleanupCode = new RuntimeHelpers.CleanupCode(GetResourceStringBackoutCode);

                RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(tryCode, cleanupCode, userData);
                return(userData.m_retVal);
            }
Ejemplo n.º 3
0
 public unsafe Mutex(bool initiallyOwned, string name, out bool createdNew, MutexSecurity mutexSecurity)
 {
     if ((name != null) && (260 < name.Length))
     {
         throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", new object[] { name }));
     }
     Win32Native.SECURITY_ATTRIBUTES secAttrs = null;
     if (mutexSecurity != null)
     {
         secAttrs = new Win32Native.SECURITY_ATTRIBUTES {
             nLength = Marshal.SizeOf(secAttrs)
         };
         byte[] securityDescriptorBinaryForm = mutexSecurity.GetSecurityDescriptorBinaryForm();
         byte* pDest = stackalloc byte[1 * securityDescriptorBinaryForm.Length];
         Buffer.memcpy(securityDescriptorBinaryForm, 0, pDest, 0, securityDescriptorBinaryForm.Length);
         secAttrs.pSecurityDescriptor = pDest;
     }
     SafeWaitHandle mutexHandle = null;
     bool newMutex = false;
     RuntimeHelpers.CleanupCode backoutCode = new RuntimeHelpers.CleanupCode(this.MutexCleanupCode);
     MutexCleanupInfo cleanupInfo = new MutexCleanupInfo(mutexHandle, false);
     RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(delegate (object userData) {
         RuntimeHelpers.PrepareConstrainedRegions();
         try
         {
         }
         finally
         {
             if (initiallyOwned)
             {
                 cleanupInfo.inCriticalRegion = true;
                 Thread.BeginThreadAffinity();
                 Thread.BeginCriticalRegion();
             }
         }
         int errorCode = 0;
         RuntimeHelpers.PrepareConstrainedRegions();
         try
         {
         }
         finally
         {
             errorCode = CreateMutexHandle(initiallyOwned, name, secAttrs, out mutexHandle);
         }
         if (mutexHandle.IsInvalid)
         {
             mutexHandle.SetHandleAsInvalid();
             if (((name != null) && (name.Length != 0)) && (6 == errorCode))
             {
                 throw new WaitHandleCannotBeOpenedException(Environment.GetResourceString("Threading.WaitHandleCannotBeOpenedException_InvalidHandle", new object[] { name }));
             }
             __Error.WinIOError(errorCode, name);
         }
         newMutex = errorCode != 0xb7;
         this.SetHandleInternal(mutexHandle);
         this.hasThreadAffinity = true;
     }, backoutCode, cleanupInfo);
     createdNew = newMutex;
 }
Ejemplo n.º 4
0
 internal void CreateMutexWithGuaranteedCleanup(bool initiallyOwned, string name, out bool createdNew, Win32Native.SECURITY_ATTRIBUTES secAttrs)
 {
     RuntimeHelpers.CleanupCode backoutCode        = new RuntimeHelpers.CleanupCode(this.MutexCleanupCode);
     Mutex.MutexCleanupInfo     cleanupInfo        = new Mutex.MutexCleanupInfo((SafeWaitHandle)null, false);
     Mutex.MutexTryCodeHelper   mutexTryCodeHelper = new Mutex.MutexTryCodeHelper(initiallyOwned, cleanupInfo, name, secAttrs, this);
     RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(new RuntimeHelpers.TryCode(mutexTryCodeHelper.MutexTryCode), backoutCode, (object)cleanupInfo);
     createdNew = mutexTryCodeHelper.m_newMutex;
 }
Ejemplo n.º 5
0
 internal static void RunInternal(SecurityContext securityContext, ContextCallback callBack, object state)
 {
     if (SecurityContext.cleanupCode == null)
     {
         SecurityContext.tryCode     = new RuntimeHelpers.TryCode(SecurityContext.runTryCode);
         SecurityContext.cleanupCode = new RuntimeHelpers.CleanupCode(SecurityContext.runFinallyCode);
     }
     SecurityContext.SecurityContextRunData securityContextRunData = new SecurityContext.SecurityContextRunData(securityContext, callBack, state);
     RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(SecurityContext.tryCode, SecurityContext.cleanupCode, (object)securityContextRunData);
 }
Ejemplo n.º 6
0
        [System.Security.SecurityCritical]  // auto-generated
        internal static void RunInternal(SecurityContext securityContext, ContextCallback callBack, Object state)
        {
            if (cleanupCode == null)
            {
                tryCode     = new RuntimeHelpers.TryCode(runTryCode);
                cleanupCode = new RuntimeHelpers.CleanupCode(runFinallyCode);
            }
            SecurityContextRunData runData = new SecurityContextRunData(securityContext, callBack, state);

            RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(tryCode, cleanupCode, runData);
        }
Ejemplo n.º 7
0
        internal static void RunInternal(ExecutionContext executionContext, ContextCallback callback, object state)
        {
            if (cleanupCode == null)
            {
                tryCode     = new RuntimeHelpers.TryCode(ExecutionContext.runTryCode);
                cleanupCode = new RuntimeHelpers.CleanupCode(ExecutionContext.runFinallyCode);
            }
            ExecutionContextRunData userData = new ExecutionContextRunData(executionContext, callback, state);

            RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(tryCode, cleanupCode, userData);
        }
Ejemplo n.º 8
0
        public Mutex(bool initiallyOwned, String name, out bool createdNew)
        {
            if (null != name && System.IO.Path.MAX_PATH < name.Length)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", name));
            }
            Win32Native.SECURITY_ATTRIBUTES secAttrs = null;
            SafeWaitHandle mutexHandle = null;
            bool           newMutex    = false;

            RuntimeHelpers.CleanupCode cleanupCode = new RuntimeHelpers.CleanupCode(MutexCleanupCode);
            MutexCleanupInfo           cleanupInfo = new MutexCleanupInfo(mutexHandle, false);

            RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(
                delegate(object userData)  {  // try block
                RuntimeHelpers.PrepareConstrainedRegions();
                try {
                }
                finally {
                    if (initiallyOwned)
                    {
                        cleanupInfo.inCriticalRegion = true;
                        Thread.BeginThreadAffinity();
                        Thread.BeginCriticalRegion();
                    }
                }

                int errorCode = 0;
                RuntimeHelpers.PrepareConstrainedRegions();
                try {
                }
                finally {
                    errorCode = CreateMutexHandle(initiallyOwned, name, secAttrs, out mutexHandle);
                }

                if (mutexHandle.IsInvalid)
                {
                    mutexHandle.SetHandleAsInvalid();
                    if (null != name && 0 != name.Length && Win32Native.ERROR_INVALID_HANDLE == errorCode)
                    {
                        throw new WaitHandleCannotBeOpenedException(Environment.GetResourceString("Threading.WaitHandleCannotBeOpenedException_InvalidHandle", name));
                    }
                    __Error.WinIOError(errorCode, name);
                }
                newMutex = errorCode != Win32Native.ERROR_ALREADY_EXISTS;
                SetHandleInternal(mutexHandle);
                mutexHandle.SetAsMutex();

                hasThreadAffinity = true;
            },
                cleanupCode,
                cleanupInfo);
            createdNew = newMutex;
        }
			internal string GetResourceString(string key, CultureInfo culture)
			{
				if (key == null || key.Length == 0)
				{
					return "[Resource lookup failed - null or empty resource name]";
				}
				Environment.ResourceHelper.GetResourceStringUserData getResourceStringUserData = new Environment.ResourceHelper.GetResourceStringUserData(this, key, culture);
				RuntimeHelpers.TryCode code = new RuntimeHelpers.TryCode(this.GetResourceStringCode);
				RuntimeHelpers.CleanupCode backoutCode = new RuntimeHelpers.CleanupCode(this.GetResourceStringBackoutCode);
				RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(code, backoutCode, getResourceStringUserData);
				return getResourceStringUserData.m_retVal;
			}
            internal string GetResourceString(string key, CultureInfo culture)
            {
                if ((key == null) || (key.Length == 0))
                {
                    return("[Resource lookup failed - null or empty resource name]");
                }
                GetResourceStringUserData userData = new GetResourceStringUserData(this, key, culture);

                RuntimeHelpers.TryCode     code        = new RuntimeHelpers.TryCode(this.GetResourceStringCode);
                RuntimeHelpers.CleanupCode backoutCode = new RuntimeHelpers.CleanupCode(this.GetResourceStringBackoutCode);
                RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(code, backoutCode, userData);
                return(userData.m_retVal);
            }
Ejemplo n.º 11
0
        internal void CreateMutexWithGuaranteedCleanup(bool initiallyOwned, String name, out bool createdNew, Win32Native.SECURITY_ATTRIBUTES secAttrs)
        {
            RuntimeHelpers.CleanupCode cleanupCode   = new RuntimeHelpers.CleanupCode(MutexCleanupCode);
            MutexCleanupInfo           cleanupInfo   = new MutexCleanupInfo(null, false);
            MutexTryCodeHelper         tryCodeHelper = new MutexTryCodeHelper(initiallyOwned, cleanupInfo, name, secAttrs, this);

            RuntimeHelpers.TryCode tryCode = new RuntimeHelpers.TryCode(tryCodeHelper.MutexTryCode);
            RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(
                tryCode,
                cleanupCode,
                cleanupInfo);
            createdNew = tryCodeHelper.m_newMutex;
        }
Ejemplo n.º 12
0
        public Mutex(bool initiallyOwned, String name, out bool createdNew)
        {
            if(null != name && System.IO.Path.MAX_PATH < name.Length)
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong",name));
            }            
            Win32Native.SECURITY_ATTRIBUTES secAttrs = null;
            SafeWaitHandle mutexHandle = null;
            bool newMutex = false;
            RuntimeHelpers.CleanupCode cleanupCode = new RuntimeHelpers.CleanupCode(MutexCleanupCode);
            MutexCleanupInfo cleanupInfo = new MutexCleanupInfo(mutexHandle, false);            
            RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(
                delegate(object userData)  {  // try block                
                    RuntimeHelpers.PrepareConstrainedRegions();
                    try {
                    }
                    finally {
                        if (initiallyOwned) {
                            cleanupInfo.inCriticalRegion = true;
                            Thread.BeginThreadAffinity();
                            Thread.BeginCriticalRegion();
                        }
                    }

                    int errorCode = 0;                    
                    RuntimeHelpers.PrepareConstrainedRegions();
                    try {
                    }
                    finally {
                        errorCode = CreateMutexHandle(initiallyOwned, name, secAttrs, out mutexHandle);
                    }                    

                    if (mutexHandle.IsInvalid) {
                        mutexHandle.SetHandleAsInvalid();
                        if(null != name && 0 != name.Length && Win32Native.ERROR_INVALID_HANDLE == errorCode)
                            throw new WaitHandleCannotBeOpenedException(Environment.GetResourceString("Threading.WaitHandleCannotBeOpenedException_InvalidHandle", name));
                        __Error.WinIOError(errorCode, name);
                    }
                    newMutex = errorCode != Win32Native.ERROR_ALREADY_EXISTS;
                    SetHandleInternal(mutexHandle);
                    mutexHandle.SetAsMutex();

                    hasThreadAffinity = true;

                },
                cleanupCode,
                cleanupInfo);             
                createdNew = newMutex;

}
Ejemplo n.º 13
0
 public static void Run(CompressedStack compressedStack, ContextCallback callback, object state)
 {
     if (compressedStack == null)
     {
         throw new ArgumentException(Environment.GetResourceString("Arg_NamedParamNull"), "compressedStack");
     }
     if (CompressedStack.cleanupCode == null)
     {
         CompressedStack.tryCode     = new RuntimeHelpers.TryCode(CompressedStack.runTryCode);
         CompressedStack.cleanupCode = new RuntimeHelpers.CleanupCode(CompressedStack.runFinallyCode);
     }
     CompressedStack.CompressedStackRunData compressedStackRunData = new CompressedStack.CompressedStackRunData(compressedStack, callback, state);
     RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(CompressedStack.tryCode, CompressedStack.cleanupCode, (object)compressedStackRunData);
 }
        public static void Run(CompressedStack compressedStack, ContextCallback callback, Object state)
        {
            if (compressedStack == null)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_NamedParamNull"), "compressedStack");
            }
            Contract.EndContractBlock();
            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);
        }
            static int Main(string[] args)
            {
                GCD gcd = new GCD();
                RuntimeHelpers.TryCode t = new RuntimeHelpers.TryCode(gcd.TryCode0);
                RuntimeHelpers.CleanupCode c = new RuntimeHelpers.CleanupCode(gcd.CleanupCode0);
                int val = 21;
                try
                {
                    RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(t, c, val);
                }
                catch (Exception Ex)
                {

                }

                return gcd.GetExitCode();
            }
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static int Main(string[] args)
        {
            GCD gcd = new GCD();

            RuntimeHelpers.TryCode     t = new RuntimeHelpers.TryCode(gcd.TryCode0);
            RuntimeHelpers.CleanupCode c = new RuntimeHelpers.CleanupCode(gcd.CleanupCode0);
            int val = 21;

            try
            {
                RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(t, c, val);
            }
            catch (Exception Ex)
            {
            }

            return(gcd.GetExitCode());
        }
Ejemplo n.º 17
0
        internal void CreateMutexWithGuaranteedCleanup(bool initiallyOwned, String name, out bool createdNew, Win32Native.SECURITY_ATTRIBUTES secAttrs)
        {
#if FEATURE_LEGACYNETCF
            if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
            {
                name = WinCEObjectNameQuirk(name);
            }
#endif

            RuntimeHelpers.CleanupCode cleanupCode   = new RuntimeHelpers.CleanupCode(MutexCleanupCode);
            MutexCleanupInfo           cleanupInfo   = new MutexCleanupInfo(null, false);
            MutexTryCodeHelper         tryCodeHelper = new MutexTryCodeHelper(initiallyOwned, cleanupInfo, name, secAttrs, this);
            RuntimeHelpers.TryCode     tryCode       = new RuntimeHelpers.TryCode(tryCodeHelper.MutexTryCode);
            RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(
                tryCode,
                cleanupCode,
                cleanupInfo);
            createdNew = tryCodeHelper.m_newMutex;
        }
Ejemplo n.º 18
0
        private void Initialize()
        {
            unsafe
            {
                _gcHandle = new GCHandle();
                var insecurePointer = IntPtr.Zero;

                void code(object userData)
                {
                    Value = new string((char)0, _secureString.Length);
                    Action alloc = delegate { _gcHandle = GCHandle.Alloc(Value, GCHandleType.Pinned); };

                    alloc.ExecuteInConstrainedRegion();

                    Action toBSTR = delegate { insecurePointer = SecureStringMarshal.SecureStringToGlobalAllocUnicode(_secureString); };

                    toBSTR.ExecuteInConstrainedRegion();

                    var value       = (char *)_gcHandle.AddrOfPinnedObject();
                    var charPointer = (char *)insecurePointer;

                    for (int i = 0; i < _secureString.Length; i++)
                    {
                        value[i] = charPointer[i];
                    }
                }

                RuntimeHelpers.CleanupCode cleanup = delegate
                {
                    if (insecurePointer != IntPtr.Zero)
                    {
                        Marshal.ZeroFreeGlobalAllocUnicode(insecurePointer);
                    }
                };

#pragma warning disable SYSLIB0004 // Type or member is obsolete
                RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(code, cleanup, null);
#pragma warning restore SYSLIB0004 // Type or member is obsolete
            }
        }
Ejemplo n.º 19
0
        private static void Main()
        {
            Console.WriteLine("Danger");
            try
            {
                Danger();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.WriteLine();

            Console.WriteLine("Safety");
            try
            {
                SafeCode();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.WriteLine();

            // NOTE: Еще один способ гарантированной очистки
            RuntimeHelpers.TryCode tryCode = data =>
            {
                Console.WriteLine("Try code");
            };

            RuntimeHelpers.CleanupCode cleanupCode = (data, thrown) =>
            {
                Console.WriteLine("Cleanup code");
            };

            RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(tryCode, cleanupCode, null);
        }
    public static void Run()
    {
        GCD gcd = new GCD();

        RuntimeHelpers.TryCode     t = new RuntimeHelpers.TryCode(gcd.TryCode0);
        RuntimeHelpers.CleanupCode c = new RuntimeHelpers.CleanupCode(gcd.CleanupCode0);
        int val = 21;

        try
        {
            RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(t, c, val);
        }
        catch (Exception Ex)
        {
        }

        int res = gcd.GetExitCode();

        if (res != 100)
        {
            throw new Exception($"{nameof(ExecuteCodeWithGuaranteedCleanupTest)} failed. Result: {res}");
        }
    }
Ejemplo n.º 21
0
    public static void Run()
    {
        GCD gcd = new GCD();

        RuntimeHelpers.TryCode     t = new RuntimeHelpers.TryCode(gcd.TryCode0);
        RuntimeHelpers.CleanupCode c = new RuntimeHelpers.CleanupCode(gcd.CleanupCode0);
        int val = 21;

        try
        {
#pragma warning disable SYSLIB0004 // CER is obsolete
            RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(t, c, val);
#pragma warning restore SYSLIB0004
        }
        catch (Exception Ex)
        {
        }

        int res = gcd.GetExitCode();
        if (res != 100)
        {
            throw new Exception($"{nameof(ExecuteCodeWithGuaranteedCleanupTest)} failed. Result: {res}");
        }
    }
 public unsafe Mutex(bool initiallyOwned, string name, out bool createdNew, MutexSecurity mutexSecurity)
 {
     if ((name != null) && (260 < name.Length))
     {
         throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", new object[] { name }));
     }
     Win32Native.SECURITY_ATTRIBUTES structure = null;
     if (mutexSecurity != null)
     {
         structure = new Win32Native.SECURITY_ATTRIBUTES {
             nLength = Marshal.SizeOf(structure)
         };
         byte[] securityDescriptorBinaryForm = mutexSecurity.GetSecurityDescriptorBinaryForm();
         byte* pDest = stackalloc byte[(IntPtr) securityDescriptorBinaryForm.Length];
         Buffer.memcpy(securityDescriptorBinaryForm, 0, pDest, 0, securityDescriptorBinaryForm.Length);
         structure.pSecurityDescriptor = pDest;
     }
     RuntimeHelpers.CleanupCode backoutCode = new RuntimeHelpers.CleanupCode(this.MutexCleanupCode);
     MutexCleanupInfo cleanupInfo = new MutexCleanupInfo(null, false);
     MutexTryCodeHelper helper = new MutexTryCodeHelper(initiallyOwned, cleanupInfo, name, structure, this);
     RuntimeHelpers.TryCode code = new RuntimeHelpers.TryCode(helper.MutexTryCode);
     RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(code, backoutCode, cleanupInfo);
     createdNew = helper.m_newMutex;
 }
Ejemplo n.º 23
0
        [System.Security.SecurityCritical]  // auto-generated
        internal static void RunInternal(SecurityContext securityContext, ContextCallback callBack, Object state)
        {
            if (cleanupCode == null)
            {
                tryCode = new RuntimeHelpers.TryCode(runTryCode);
                cleanupCode = new RuntimeHelpers.CleanupCode(runFinallyCode);
            }
            SecurityContextRunData runData = new SecurityContextRunData(securityContext, callBack, state);
            RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(tryCode, cleanupCode, runData);

        }
Ejemplo n.º 24
0
        public unsafe Mutex(bool initiallyOwned, string name, out bool createdNew, MutexSecurity mutexSecurity)
        {
            if ((name != null) && (260 < name.Length))
            {
                throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong", new object[] { name }));
            }
            Win32Native.SECURITY_ATTRIBUTES secAttrs = null;
            if (mutexSecurity != null)
            {
                secAttrs = new Win32Native.SECURITY_ATTRIBUTES {
                    nLength = Marshal.SizeOf(secAttrs)
                };
                byte[] securityDescriptorBinaryForm = mutexSecurity.GetSecurityDescriptorBinaryForm();
                byte * pDest = stackalloc byte[1 * securityDescriptorBinaryForm.Length];
                Buffer.memcpy(securityDescriptorBinaryForm, 0, pDest, 0, securityDescriptorBinaryForm.Length);
                secAttrs.pSecurityDescriptor = pDest;
            }
            SafeWaitHandle mutexHandle = null;
            bool           newMutex    = false;

            RuntimeHelpers.CleanupCode backoutCode = new RuntimeHelpers.CleanupCode(this.MutexCleanupCode);
            MutexCleanupInfo           cleanupInfo = new MutexCleanupInfo(mutexHandle, false);

            RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(delegate(object userData) {
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                }
                finally
                {
                    if (initiallyOwned)
                    {
                        cleanupInfo.inCriticalRegion = true;
                        Thread.BeginThreadAffinity();
                        Thread.BeginCriticalRegion();
                    }
                }
                int errorCode = 0;
                RuntimeHelpers.PrepareConstrainedRegions();
                try
                {
                }
                finally
                {
                    errorCode = CreateMutexHandle(initiallyOwned, name, secAttrs, out mutexHandle);
                }
                if (mutexHandle.IsInvalid)
                {
                    mutexHandle.SetHandleAsInvalid();
                    if (((name != null) && (name.Length != 0)) && (6 == errorCode))
                    {
                        throw new WaitHandleCannotBeOpenedException(Environment.GetResourceString("Threading.WaitHandleCannotBeOpenedException_InvalidHandle", new object[] { name }));
                    }
                    __Error.WinIOError(errorCode, name);
                }
                newMutex = errorCode != 0xb7;
                this.SetHandleInternal(mutexHandle);
                this.hasThreadAffinity = true;
            }, backoutCode, cleanupInfo);
            createdNew = newMutex;
        }
Ejemplo n.º 25
0
 public static void ExecuteCodeWithGuaranteedCleanup(RuntimeHelpers.TryCode code, RuntimeHelpers.CleanupCode backoutCode, object userData)
 {
 }
Ejemplo n.º 26
0
 internal void CreateMutexWithGuaranteedCleanup(bool initiallyOwned, String name, out bool createdNew, Win32Native.SECURITY_ATTRIBUTES secAttrs)
 {
     RuntimeHelpers.CleanupCode cleanupCode = new RuntimeHelpers.CleanupCode(MutexCleanupCode);
     MutexCleanupInfo cleanupInfo = new MutexCleanupInfo(null, false);
     MutexTryCodeHelper tryCodeHelper = new MutexTryCodeHelper(initiallyOwned, cleanupInfo, name, secAttrs, this);
     RuntimeHelpers.TryCode tryCode = new RuntimeHelpers.TryCode(tryCodeHelper.MutexTryCode);
     RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(
         tryCode,
         cleanupCode,
         cleanupInfo);             
     createdNew = tryCodeHelper.m_newMutex;
 }
            internal String GetResourceString(String key, CultureInfo culture)  {
                if (key == null || key.Length == 0) {
                    Contract.Assert(false, "Environment::GetResourceString with null or empty key.  Bug in caller, or weird recursive loading problem?");
                    return "[Resource lookup failed - null or empty resource name]";
                }

#if FEATURE_SPLIT_RESOURCES
                if (UseFallback()) {
                    return null;
                }
#endif // FEATURE_SPLIT_RESOURCES

                // We have a somewhat common potential for infinite 
                // loops with mscorlib's ResourceManager.  If "potentially dangerous"
                // code throws an exception, we will get into an infinite loop
                // inside the ResourceManager and this "potentially dangerous" code.
                // Potentially dangerous code includes the IO package, CultureInfo,
                // parts of the loader, some parts of Reflection, Security (including 
                // custom user-written permissions that may parse an XML file at
                // class load time), assembly load event handlers, etc.  Essentially,
                // this is not a bounded set of code, and we need to fix the problem.
                // Fortunately, this is limited to mscorlib's error lookups and is NOT
                // a general problem for all user code using the ResourceManager.
                
                // The solution is to make sure only one thread at a time can call 
                // GetResourceString.  Also, since resource lookups can be 
                // reentrant, if the same thread comes into GetResourceString
                // twice looking for the exact same resource name before 
                // returning, we're going into an infinite loop and we should 
                // return a bogus string.  

                GetResourceStringUserData userData = new GetResourceStringUserData(this, key, culture);

                RuntimeHelpers.TryCode tryCode = new RuntimeHelpers.TryCode(GetResourceStringCode);
                RuntimeHelpers.CleanupCode cleanupCode = new RuntimeHelpers.CleanupCode(GetResourceStringBackoutCode);

                RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(tryCode, cleanupCode, userData);
                return userData.m_retVal;
            }
 internal static void RunInternal(ExecutionContext executionContext, ContextCallback callback, object state)
 {
     if (cleanupCode == null)
     {
         tryCode = new RuntimeHelpers.TryCode(ExecutionContext.runTryCode);
         cleanupCode = new RuntimeHelpers.CleanupCode(ExecutionContext.runFinallyCode);
     }
     ExecutionContextRunData userData = new ExecutionContextRunData(executionContext, callback, state);
     RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(tryCode, cleanupCode, userData);
 }
Ejemplo n.º 29
0
        public unsafe Mutex(bool initiallyOwned, String name, out bool createdNew, MutexSecurity mutexSecurity)
        {
            if(null != name && System.IO.Path.MAX_PATH < name.Length)
            { 
                throw new ArgumentException(Environment.GetResourceString("Argument_WaitHandleNameTooLong",name));
            } 
            Contract.EndContractBlock(); 
            Win32Native.SECURITY_ATTRIBUTES secAttrs = null;
#if !FEATURE_PAL && FEATURE_MACL 
            // For ACL's, get the security descriptor from the MutexSecurity.
            if (mutexSecurity != null) {

                secAttrs = new Win32Native.SECURITY_ATTRIBUTES(); 
                secAttrs.nLength = (int)Marshal.SizeOf(secAttrs);
 
                byte[] sd = mutexSecurity.GetSecurityDescriptorBinaryForm(); 
                byte* pSecDescriptor = stackalloc byte[sd.Length];
                Buffer.memcpy(sd, 0, pSecDescriptor, 0, sd.Length); 
                secAttrs.pSecurityDescriptor = pSecDescriptor;
            }
#endif
 

            RuntimeHelpers.CleanupCode cleanupCode = new RuntimeHelpers.CleanupCode(MutexCleanupCode); 
            MutexCleanupInfo cleanupInfo = new MutexCleanupInfo(null, false); 
            MutexTryCodeHelper tryCodeHelper = new MutexTryCodeHelper(initiallyOwned, cleanupInfo, name, secAttrs, this);
            RuntimeHelpers.TryCode tryCode = new RuntimeHelpers.TryCode(tryCodeHelper.MutexTryCode); 
            RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(
                tryCode,
                cleanupCode,
                cleanupInfo); 
                createdNew = tryCodeHelper.m_newMutex;
 
        } 
Ejemplo n.º 30
0
 public static void Run(CompressedStack compressedStack, ContextCallback callback, Object state)
 {
     
     if (compressedStack == null )
     {
         throw new ArgumentException(Environment.GetResourceString("Arg_NamedParamNull"),"compressedStack");
     }
     Contract.EndContractBlock();
     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);
 }
Ejemplo n.º 31
0
        internal void CreateMutexWithGuaranteedCleanup(bool initiallyOwned, String name, out bool createdNew, Win32Native.SECURITY_ATTRIBUTES secAttrs)
        {
#if FEATURE_LEGACYNETCF
            if (CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
                name = WinCEObjectNameQuirk(name);
#endif

            RuntimeHelpers.CleanupCode cleanupCode = new RuntimeHelpers.CleanupCode(MutexCleanupCode);
            MutexCleanupInfo cleanupInfo = new MutexCleanupInfo(null, false);
            MutexTryCodeHelper tryCodeHelper = new MutexTryCodeHelper(initiallyOwned, cleanupInfo, name, secAttrs, this);
            RuntimeHelpers.TryCode tryCode = new RuntimeHelpers.TryCode(tryCodeHelper.MutexTryCode);
            RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(
                tryCode,
                cleanupCode,
                cleanupInfo);             
            createdNew = tryCodeHelper.m_newMutex;
        }
Ejemplo n.º 32
-1
 public void StackOverFlowTester()
 {
     try
     {
         _tryCode = new RuntimeHelpers.TryCode(this.GenerateStackOverFlow);
         _cleanupCode = new RuntimeHelpers.CleanupCode(this.CleanupCode);
     }
     finally
     {
         RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(Program._tryCode, Program._cleanupCode, 1);
     }
 }