[Mono-dev] Crazy StackAlloc

Jambunathan K kjambunathan.devel at gmail.com
Thu May 7 09:04:45 EDT 2009


With reference to the example down below, the behaviour on Mono is down below.
The behaviour is different for each of the 3 runs.

My contentions is that StackOverflowException should be caught right
within the __Main__ and that the runtime shouldn't unwind to parent
stack in order to get itself
another 64K of free stack.

Implementing localloc as a sub-procedure rather than an 'inlined code'
(see mono_emit_stack_alloc) it would be possible for the runtime to do

1. Catch StackOverflowException right within Main ()

or

2. Mimic MS behaviour of returning a NULL.


===== RUN 1 =====

kjambunathan at kjambunathan-desktop:~/src/ws/mono-2.4/mono$ mono
StackAllocCrazy.exe
Trying 1073741824 bytes ...
Caught System.OverflowException: Number overflow.
  at T.Main () [0x00000]
Trying 536870912 bytes ...
Caught System.OverflowException: Number overflow.
  at T.Main () [0x00000]
Trying 268435456 bytes ...
Stack overflow in unmanaged: IP: 0x8073bc8, fault addr: 0xbf344f4c

Unhandled Exception: System.StackOverflowException: The requested
operation caused a stack overflow.
  at (wrapper managed-to-native) System.MonoType:getFullName (bool,bool)
  at System.MonoType.ToString () [0x00000]
  at System.Exception.get_ClassName () [0x00000]
  at System.Exception.ToString () [0x00000]

===== RUN 2 =====

kjambunathan at kjambunathan-desktop:~/src/ws/mono-2.4/mono$ mono
StackAllocCrazy.exe
Trying 1073741824 bytes ...
Caught System.OverflowException: Number overflow.
  at T.Main () [0x00000]
Trying 536870912 bytes ...
Caught System.OverflowException: Number overflow.
  at T.Main () [0x00000]
Trying 268435456 bytes ...
Stack overflow in unmanaged: IP: 0x80b8232, fault addr: 0xbf656ffc

Unhandled Exception: System.StackOverflowException: The requested
operation caused a stack overflow.
  at (wrapper managed-to-native) System.MonoType:getFullName (bool,bool)
  at System.MonoType.ToString () [0x00000]
  at System.Exception.get_ClassName () [0x00000]
  at System.Exception.ToString () [0x00000]

===== RUN 3 =====

kjambunathan at kjambunathan-desktop:~/src/ws/mono-2.4/mono$ mono
StackAllocCrazy.exe
Trying 1073741824 bytes ...
Caught System.OverflowException: Number overflow.
  at T.Main () [0x00000]
Trying 536870912 bytes ...
Caught System.OverflowException: Number overflow.
  at T.Main () [0x00000]
Trying 268435456 bytes ...

Unhandled Exception: System.StackOverflowException: The requested
operation caused a stack overflow.
  at T.Main () [0x00000]

Jambunathan K.

On Thu, May 7, 2009 at 5:22 PM, Jambunathan K
<kjambunathan.devel at gmail.com> wrote:
> I was little out of my mind and I tried crazy stackalloc both on Mono
> and Windows XP.
>
> The intention was to find out the max stack size available. There are
> a couple of surprises both Windows and Mono. Both the runtimes was
> behaving differently from my expectations.
>
> On Windows, there is __no__ StackOverflowException as one would expect.
> There was only a NullReferenceException.
>
> On Mono, there is a StackOverflowException with the runtime getting
> starved stack space.
>
> My mono version is 2.4. My .NET - 1.1 environment is atleast 4 years old.
>
> Please don't question the reasonableness of stackalloc size. What
> interests me is the runtime behaviour.
>
> You might have to play around with 'size' to get the runtime to behave crazily.
>
> Output on Windows is down below. Audience can check out the behaviour
> on Mono for
> themselves.
>
> C:\Documents and Settings\Owner>type StackAllocCrazy.cs
> using System;
>
> class T
> {
>
>        public unsafe static void Main ()
>        {
>        int size = 1 * 1024 * 1024 * 1024;
>
>        while (size != 0) {
>                Console.WriteLine ("Trying " + size + " bytes ...");
>
>                try {
>                        long *buf = stackalloc long[size];
>
>                        for (int i = 0; i < size; ++i)
>                                buf[i] = 0;
>
>                } catch (Exception e) {
>                        Console.WriteLine ("Caught " + e);
>                        size /= 2;
>                        continue;
>                }
>
>                Console.WriteLine ("Can stackalloc " + size + " longs");
>                break;
>        }
>        }
> }
>
> C:\Documents and Settings\Owner>csc /unsafe StackAllocCrazy.cs
> Microsoft (R) Visual C# .NET Compiler version 7.10.3052.4
> for Microsoft (R) .NET Framework version 1.1.4322
> Copyright (C) Microsoft Corporation 2001-2002. All rights reserved.
>
>
> C:\Documents and Settings\Owner>StackAllocCrazy.exe
> Trying 1073741824 bytes ...
> Caught System.NullReferenceException: Object reference not set to an instance of
>  an object.
>   at T.Main()
> Trying 536870912 bytes ...
> Caught System.NullReferenceException: Object reference not set to an instance of
>  an object.
>   at T.Main()
> Trying 268435456 bytes ...
>
>
> Regards,
> Jambunathan K.
>


More information about the Mono-devel-list mailing list