[interp] fix array_element_size intrinsic (#17857)
commit058d157648f593f41a2afbf878c0f401ecb804b7
authorBernhard Urban-Forster <lewurm@gmail.com>
Thu, 21 Nov 2019 10:56:03 +0000 (21 11:56 +0100)
committermonojenkins <jo.shields+jenkins@xamarin.com>
Thu, 21 Nov 2019 10:56:03 +0000 (21 11:56 +0100)
tree8bbd75316d6042bc13b90f0eb47d33e7777dfb31
parentb6e4ddd73c436ce23ae7036a7170f59b545e46b7
[interp] fix array_element_size intrinsic (#17857)

[interp] fix array_element_size intrinsic

`mono_class_array_element_size` gives us the size of the provided MonoClass if it would be an array element. But here we want the element size of a given array's MonoClass. That's what `mono_array_element_size` is returning.

This leads to all kind of weird crashes otherwise, specifically here:
https://github.com/mono/mono/blob/2c20649539ac16e069a65f2a750c793eb341e50f/netcore/System.Private.CoreLib/src/System/Array.Mono.cs#L64

Which is then used later to compute the size to memset in order to clear the content of an array. If it's larger than it should be, then this will cause memory corruption. For example this would crash eventually:

```csharp
using System;
using System.Collections.Generic;

namespace HelloWorld {
    class Program {
        static void Main(string[] args) {
            for (int j = 0; j < 0x1000; j++) {
                var d = new Dictionary<string, string> ();
                for (int i = 0; i < 197; i++)
                    d.Add (i + "", i + "foo");
                d.Clear ();
            }
        }
    }
}
```

Thanks to @EgorBo for reporting.
mono/mini/interp/interp.c