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); }
internal void CreateMutexWithGuaranteedCleanup(bool initiallyOwned, string name, out bool createdNew, Win32Native.SECURITY_ATTRIBUTES secAttrs) { RuntimeHelpers.CleanupCode backoutCode = new RuntimeHelpers.CleanupCode(this.MutexCleanupCode); Mutex.MutexCleanupInfo mutexCleanupInfo = new Mutex.MutexCleanupInfo(null, false); Mutex.MutexTryCodeHelper mutexTryCodeHelper = new Mutex.MutexTryCodeHelper(initiallyOwned, mutexCleanupInfo, name, secAttrs, this); RuntimeHelpers.TryCode code = new RuntimeHelpers.TryCode(mutexTryCodeHelper.MutexTryCode); RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(code, backoutCode, mutexCleanupInfo); createdNew = mutexTryCodeHelper.m_newMutex; }
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); }
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); }
[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); }
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 static GlobalizationAssembly GetGlobalizationAssembly(Assembly assembly) { GlobalizationAssembly assembly2 = (GlobalizationAssembly)m_assemblyHash[assembly]; if (assembly2 == null) { RuntimeHelpers.TryCode code = new RuntimeHelpers.TryCode(GlobalizationAssembly.CreateGlobalizationAssembly); RuntimeHelpers.ExecuteCodeWithLock(typeof(CultureTableRecord), code, assembly); assembly2 = (GlobalizationAssembly)m_assemblyHash[assembly]; } return(assembly2); }
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); }
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()); }
/*=================================GetGlobalizationAssembly========================== **Action: Return the GlobalizationAssembly instance for the specified assembly. ** Every assembly should have one and only one instance of GlobalizationAssembly. **Returns: An instance of GlobalizationAssembly. **Arguments: **Exceptions: **============================================================================*/ internal static GlobalizationAssembly GetGlobalizationAssembly(Assembly assembly) { GlobalizationAssembly ga; if ((ga = (GlobalizationAssembly)m_assemblyHash[assembly]) == null) { // This is intentionally taking a process-global lock on an // internal Type, using ExecuteCodeWithLock to guarantee we // release the lock in a stack overflow safe way. RuntimeHelpers.TryCode createGlobalizationAssem = new RuntimeHelpers.TryCode(CreateGlobalizationAssembly); RuntimeHelpers.ExecuteCodeWithLock(typeof(CultureTableRecord), createGlobalizationAssem, assembly); ga = (GlobalizationAssembly)m_assemblyHash[assembly]; } BCLDebug.Assert(ga != null, "GlobalizationAssembly was not created"); return(ga); }
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; }
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}"); } }
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; }
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); }
public static void ExecuteCodeWithGuaranteedCleanup(RuntimeHelpers.TryCode code, RuntimeHelpers.CleanupCode backoutCode, object userData) { }
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 ExecuteWithLockHelper(object lockObject, RuntimeHelpers.TryCode userCode, object userState) { this.m_lockObject = lockObject; this.m_userCode = userCode; this.m_userState = userState; }
/*=================================GetGlobalizationAssembly========================== **Action: Return the GlobalizationAssembly instance for the specified assembly. ** Every assembly should have one and only one instance of GlobalizationAssembly. **Returns: An instance of GlobalizationAssembly. **Arguments: **Exceptions: ============================================================================*/ internal static GlobalizationAssembly GetGlobalizationAssembly(Assembly assembly) { GlobalizationAssembly ga; if ((ga = (GlobalizationAssembly)m_assemblyHash[assembly]) == null) { // This is intentionally taking a process-global lock on an // internal Type, using ExecuteCodeWithLock to guarantee we // release the lock in a stack overflow safe way. RuntimeHelpers.TryCode createGlobalizationAssem = new RuntimeHelpers.TryCode(CreateGlobalizationAssembly); RuntimeHelpers.ExecuteCodeWithLock(typeof(CultureTableRecord), createGlobalizationAssem, assembly); ga = (GlobalizationAssembly)m_assemblyHash[assembly]; } BCLDebug.Assert(ga != null, "GlobalizationAssembly was not created"); return (ga); }
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; }
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; }
internal static GlobalizationAssembly GetGlobalizationAssembly(Assembly assembly) { GlobalizationAssembly assembly2 = (GlobalizationAssembly) m_assemblyHash[assembly]; if (assembly2 == null) { RuntimeHelpers.TryCode code = new RuntimeHelpers.TryCode(GlobalizationAssembly.CreateGlobalizationAssembly); RuntimeHelpers.ExecuteCodeWithLock(typeof(CultureTableRecord), code, assembly); assembly2 = (GlobalizationAssembly) m_assemblyHash[assembly]; } return assembly2; }
public void StackOverFlowTester() { try { _tryCode = new RuntimeHelpers.TryCode(this.GenerateStackOverFlow); _cleanupCode = new RuntimeHelpers.CleanupCode(this.CleanupCode); } finally { RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(Program._tryCode, Program._cleanupCode, 1); } }