Skip to content
This repository was archived by the owner on Nov 1, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/TypeSystem/src/Common/FieldLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -546,10 +546,13 @@ internal void ComputeInstanceFieldLayout()
_instanceFieldAlignment = computedLayout.FieldAlignment;
_instanceByteCount = computedLayout.ByteCount;

foreach (var fieldAndOffset in computedLayout.Offsets)
if (computedLayout.Offsets != null)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that was an actual bug discovered by the tests :).

{
Debug.Assert(fieldAndOffset.Field.OwningType == this);
fieldAndOffset.Field.InitializeOffset(fieldAndOffset.Offset);
foreach (var fieldAndOffset in computedLayout.Offsets)
{
Debug.Assert(fieldAndOffset.Field.OwningType == this);
fieldAndOffset.Field.InitializeOffset(fieldAndOffset.Offset);
}
}

_fieldLayoutFlags.AddFlags(FieldLayoutFlags.HasInstanceFieldLayout);
Expand Down
38 changes: 38 additions & 0 deletions src/TypeSystem/tests/CoreTestAssembly/CoreTestAssembly.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<OutputType>Library</OutputType>
<AssemblyName>CoreTestAssembly</AssemblyName>

<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<IsCoreAssembly>true</IsCoreAssembly>

<!--
Need to avoid target platform being empty because that would drag in an mscorlib design-time
facade into the references and break us.
-->
<TargetPlatformIdentifier>Portable</TargetPlatformIdentifier>
<TargetFrameworkIdentifier>.NETPortable</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkProfile>Profile7</TargetFrameworkProfile>
<TargetFrameworkMonikerDisplayName>.NET Portable Subset</TargetFrameworkMonikerDisplayName>
<ImplicitlyExpandTargetFramework>false</ImplicitlyExpandTargetFramework>

</PropertyGroup>

<!-- Default configurations to help VS understand the configurations -->
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
</PropertyGroup>

<ItemGroup>
<Compile Include="Platform.cs" />
<Compile Include="InstanceFieldLayout.cs" />
</ItemGroup>

<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
</Project>
132 changes: 132 additions & 0 deletions src/TypeSystem/tests/CoreTestAssembly/InstanceFieldLayout.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Runtime.InteropServices;

#pragma warning disable 169

namespace ContainsPointers
{
struct NoPointers
{
int int1;
byte byte1;
char char1;
}

struct StillNoPointers
{
NoPointers noPointers1;
bool bool1;
}

class ClassNoPointers
{
char char1;
}

struct HasPointers
{
string string1;
}

struct FieldHasPointers
{
HasPointers hasPointers1;
}

class ClassHasPointers
{
ClassHasPointers classHasPointers1;
}

class BaseClassHasPointers : ClassHasPointers
{
}

public class ClassHasIntArray
{
int[] intArrayField;
}

public class ClassHasArrayOfClassType
{
ClassNoPointers[] classTypeArray;
}
}

namespace Explicit
{
[StructLayout(LayoutKind.Explicit)]
class Class1
{
static int Stat;
[FieldOffset(4)]
bool Bar;
[FieldOffset(10)]
char Baz;
}

[StructLayout(LayoutKind.Explicit)]
class Class2 : Class1
{
[FieldOffset(0)]
int Lol;
[FieldOffset(20)]
byte Omg;
}

[StructLayout(LayoutKind.Explicit, Size = 40)]
class ExplicitSize : Class1
{
[FieldOffset(0)]
int Lol;
[FieldOffset(20)]
byte Omg;
}

[StructLayout(LayoutKind.Explicit)]
public class ExplicitEmptyClass
{
}

[StructLayout(LayoutKind.Explicit)]
public struct ExplicitEmptyStruct
{
}
}

namespace Sequential
{
class Class1
{
int MyInt;
bool MyBool;
char MyChar;
string MyString;
byte[] MyByteArray;
Class1 MyClass1SelfRef;
}

class Class2 : Class1
{
int MyInt2;
}

struct Struct0
{
bool b1;
bool b2;
bool b3;
int i1;
string s1;
}

struct Struct1
{
Struct0 MyStruct0;
bool MyBool;
}
}

83 changes: 83 additions & 0 deletions src/TypeSystem/tests/CoreTestAssembly/Platform.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

#pragma warning disable 649

namespace System
{
// Dummy core types to allow us compiling this assembly as a core library so that the type
// system tests don't have a dependency on a real core library.

// We might need to bring in some extra things (Interface lists? Virtual methods on Object?),
// but let's postpone that until actually needed.

public class Object
{
internal IntPtr m_pEEType;
}

public struct Void { }
public struct Boolean { }
public struct Char { }
public struct SByte { }
public struct Byte { }
public struct Int16 { }
public struct UInt16 { }
public struct Int32 { }
public struct UInt32 { }
public struct Int64 { }
public struct UInt64 { }
public struct IntPtr { }
public struct UIntPtr { }
public struct Single { }
public struct Double { }
public abstract class ValueType { }
public abstract class Enum : ValueType { }
public struct Nullable<T> where T : struct { }

public sealed class String { }
public abstract class Array { }
public abstract class Delegate { }
public abstract class MulticastDelegate : Delegate { }

public struct RuntimeTypeHandle { }
public struct RuntimeMethodHandle { }
public struct RuntimeFieldHandle { }

public class Attribute { }
}

namespace System.Runtime.InteropServices
{
public enum LayoutKind
{
Sequential = 0, // 0x00000008,
Explicit = 2, // 0x00000010,
Auto = 3, // 0x00000000,
}

public sealed class StructLayoutAttribute : Attribute
{
internal LayoutKind _val;

public StructLayoutAttribute(LayoutKind layoutKind)
{
_val = layoutKind;
}

public LayoutKind Value { get { return _val; } }
public int Pack;
public int Size;
}

public sealed class FieldOffsetAttribute : Attribute
{
private int _val;
public FieldOffsetAttribute(int offset)
{
_val = offset;
}
public int Value { get { return _val; } }
}
}

Loading