Bug 37515 - Runtime should not allow to have reference and value types overlap each other
Summary: Runtime should not allow to have reference and value types overlap each other
Status: RESOLVED ANSWERED
Alias: None
Product: Runtime
Classification: Mono
Component: General ()
Version: 4.2.0 (C6)
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2016-01-08 11:36 UTC by Sergey Khabibullin
Modified: 2016-01-08 21:59 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED ANSWERED

Description Sergey Khabibullin 2016-01-08 11:36:10 UTC
Code:

class Program
{
    [StructLayout(LayoutKind.Explicit)]
    struct Data
    {
        [FieldOffset(0)]
        public object Field1;

        [FieldOffset(0)]
        public int Field2;
    }

    public static void Main()
    {
        var a = new Data();
        a.Field1 = new object();
        Console.WriteLine(a.Field2);
    }
}

Expected result: TypeLoadException: Could not load type Test.Data' from assembly 'Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because it contains an object field at offset 0 that is incorrectly aligned or overlapped by a non-object field.

Actual result: Code runs and prints an integer which is probably object's address
Comment 1 Aleksey Kliger 2016-01-08 21:59:12 UTC
By default mono does not verify that assemblies are correct.
Run mono with the --verify-all option to get (the admittedly bad) error message:

Unhandled Exception:
System.TypeLoadException: Error verifying Program:Main (): Could not load type .Data at 0x0000
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeLoadException: Error verifying Program:Main (): Could not load type .Data at 0x0000