[Mono-dev] How To Convince Mono To Allocate Big Arrays

Luis F. Ortiz LuisOrtiz at verizon.net
Thu May 8 13:06:45 EDT 2008


On May 7, 2008, at 10:50 PM, Rodrigo Kumpera wrote:
> Hi Luis,
>
> To have your patch integrated, some changes are needed. First, we  
> want to default to 32bits sized arrays on 64bits machines, so your  
> changes must be conditionally compiled. To help with that some  
> changed to your patch are due. Next are some comments about it:
>
> ...
>
> -    if (CHECK_MUL_OVERFLOW_UN (n, elem_size))
> -        mono_gc_out_of_memory (MYGUINT32_MAX);
> +    if (CHECK_MUL_OVERFLOW_UN (n, elem_size)) {
> +        g_print("CHECK_MUL_OVERFLOW_UN(%zd,%zd) failed\n",n,  
> elem_size);
> +        mono_gc_out_of_memory (MYGUINT_MAX);
> +        }
>
> If you find that keeping such debug code is really important, you  
> should follow the same pattern of the rest of the project. Take a  
> look at how DEBUG_IMT is used on object.c.

This was an oversight on my part and should have never made it out.

> @@ -3548,34 +3559,30 @@
>      /* A single dimensional array with a 0 lower bound is the same  
> as an szarray */
>      if (array_class->rank == 1 && ((array_class->byval_arg.type ==  
> MONO_TYPE_SZARRAY) || (lower_bounds && lower_bounds [0] == 0))) {
>          len = lengths [0];
> -        if ((int) len < 0)
> -            arith_overflow ();
> Why are you removing overflow checks here?

That is an error on my part.  It should have remained.

> @@ -562,6 +607,26 @@
>      if (this->bounds == NULL)
>          return this->max_length;
>
> +    length = this->bounds [dimension].length;
> +    if (length > G_MAXINT32)
> +            mono_raise_exception (mono_get_exception_overflow ());
> +
> +    return length;
> +}
>
> Why throwing an exception here? I'm not sure this is the way to go,  
> unfortunately this is an area underspecified on ecma. Not that  
> truncating is a good option either.

Now that you are making me look at this code, there is a bug there.     
When there are no bounds, the length temporary should be set to this- 
 >max_length and the
same overflow  test applied.   The overflow test should be  
conditionally compiled when some preprocessor variable is defined, as  
per your previous suggestion.

If a big array is passed into a function that is not prepared to  
handle it, when it tries to get the length, it could end up with a  
negative number or a positive number
which is waay to small.   Though ECMA has under-specified here,  
raising an exception seems to only possible course.

/Ortiz/Luis


More information about the Mono-devel-list mailing list