PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label stack-overflow. Show all posts
Showing posts with label stack-overflow. Show all posts

Friday, August 5, 2022

[FIXED] How to overcome StackOverflowException bypassing unhandled exception handling in .NET

 August 05, 2022     .net, exception, stack-overflow     No comments   

Issue

After hitting a few StackOverflowExceptions in .NET I noticed they completely bypass the unhandled exception handlers that .NET offers (Application.ThreadException / AppDomain.UnhandledException). This is very disturbing since we have critical cleanup code in those exception handlers.

Is there any way to overcome this?


Solution

There are three kind of so-called "asynchronous exceptions". That are the ThreadAbortException, the OutOfMemoryException and the mentioned StackOverflowException. Those excepions are allowed to occur at any instruction in your code.

And, there's also a way to overcome them:

The easiest is the ThreadAbortException. When the current code executes in a finally-block. ThreadAbortExceptions are kind of "moved" to the end of the finally-block. So everything in a finally-block can't be aborted by a ThreadAbortException.

To avoid an OutOfMemoryException, you have only one possibility: Do not allocate anything on the Heap. This means that you're not allowed to create any new reference-types.

To overcome the StackOverflowException, you need some help from the Framework. This help manifests in Constrained Execution Regions. The required stack is allocated before the actual code is executed and additionally also ensures that the code is already JIT-Compiled and therefor is available for execution.

There are three forms to execute code in Constrained Execution Regions (copied from the BCL Team Blog):

  • ExecuteCodeWithGuaranteedCleanup, a stack-overflow safe form of a try/finally.
  • A try/finally block preceded immediately by a call to RuntimeHelpers.PrepareConstrainedRegions. The try block is not constrained, but all catch, finally, and fault blocks for that try are.
  • As a critical finalizer - any subclass of CriticalFinalizerObject has a finalizer that is eagerly prepared before an instance of the object is allocated.
    • A special case is SafeHandle's ReleaseHandle method, a virtual method that is eagerly prepared before the subclass is allocated, and called from SafeHandle's critical finalizer.

You can find more at these blog posts:

Constrained Execution Regions and other errata [Brian Grunkemeyer] at the BCL Team Blog.

Joe Duffy's Weblog about Atomicity and asynchronous exception failures where he gives a very good overview over asynchronous exceptions and robustness in the .net Framework.



Answered By - Thomas Danecker
Answer Checked By - Terry (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Sunday, June 26, 2022

[FIXED] How to increase stack size when compiling a C++ program using MinGW compiler

 June 26, 2022     c++, compiler-errors, mingw, stack-overflow     No comments   

Issue

I am trying to compile a program that was provided to me. The program tests the run time of the algorithm quicksort when provided different values. I need to increase the size of the stack to run really large numbers.

I read to use the following command: g++ -Wl,--stack,<size>

where size is the number to increase the stack.

However, this isn't working for me. In command prompt when I typed exactly the following:

g++ -Wl,--stack,1000000000

and then hit enter, I get the following message:

C:/mingw/bin/../lib/gcc/mingw32/5.3.0/../../../libmingw32.a(main.o):(.text.startup+0xa0): undefined reference to 'WinMain@16' collect2.exe: error: ld returned 1 exit status

I am not allowed to change the code so my only option is to increase the stack size in command prompt and then run my code.

I don't know what I am doing wrong. Am I typing in the command incorrectly?

How do I increase the stack size in command prompt for a c++ program using MinGW compiler? I am using Windows 10, if that information is helpful.


Solution

In order to change the size of the stack to run a C++ program that may cause a stack overflow, use the following command.

g++ -Wl,--stack,16777216 -o file.exe file.cpp

This increases the stack size to 16MiB, if you need it to be bigger then increase the value from 16777216 bytes to whatever value you need.

Do be sure to replace file.exe and file.cpp with the corresponding names of the file and executable that you are running.

Also note that in the command its -Wl (lower case L)



Answered By - LiveToLearn
Answer Checked By - Willingham (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing