[Mono-dev] Managed pointers and explicit layout, bug or feature?

Gabriele Svelto gabriele.svelto at gmail.com
Thu Oct 26 11:46:02 EDT 2006


 Hi everybody,
I was using mono for testing the weirdest corner cases of the ECMA335
standard to see how the VM would react to those cases. Since mono
seems to use BoehmGC I tried to put a reference to a managed object
into a field purposedly unaligned by using explicit layout. Since
BoehmGC scans for pointers only on naturally aligned boundaries I was
wondering if the object would get collected even if it was alive under
this condition, here's my code:

using System;
using System.Runtime.InteropServices;

public class Z {
    ~Z() {
        Console.WriteLine("Hello, world!");
    }
}

[StructLayout(LayoutKind.Explicit)]
public struct X {
    [FieldOffset(0)] public short a;
    [FieldOffset(2)] public Z z; // Unaligned reference
}

class Y {
    static X test() {
        X x = new X();
        x.z = new Z();
        return x;
    }

    static void test2(X x) {
        Console.WriteLine("Object: " + x);
    }

    static void Main() {
        X t1 = test();
        System.GC.Collect();
        System.GC.Collect();
        System.GC.WaitForPendingFinalizers();
        test2(t1);
    }
}:

Now what I get is something I hadn't expected, an assertion:

** ERROR **: file object.c: line 534 (compute_class_bitmap): assertion
failed: ((field->offset % sizeof(gpointer)) == 0)

It seems that mono won't allow me to put an unaligned reference inside
an object... Is this correct? I looked both inside the ECMA 334 and
335 standards and I didn't find anything preventing me from having an
unaligned reference field inside an object but maybe I'm wrong... What
do you think about this? mcs doesn't give me any warning when
compiling the code. The test was done with mono-1.1.17

 Gabriele



More information about the Mono-devel-list mailing list