Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DefineUninitializedData on TypeBuilder in System.Reflection.Emit uses typeof(ValueType) internally even when using a MetadataLoadContext #105772

Closed
TrueLunacy opened this issue Jul 31, 2024 · 3 comments · Fixed by #105903
Assignees
Labels
area-System.Reflection in-pr There is an active PR which will close this issue when it is merged
Milestone

Comments

@TrueLunacy
Copy link
Contributor

TrueLunacy commented Jul 31, 2024

Description

DefineUninitializedData on TypeBuilder in System.Reflection.Emit uses typeof(ValueType) internally, even when using a MetadataLoadContext.

This causes type.IsValueType to return false even though it is true, which causes invalid IL to be emitted (fields are being marked class when they should be marked valuetype, causing failures to load)

Reproduction Steps

using System.Reflection;
using System.Reflection.Emit;

var pathres = new PathAssemblyResolver([typeof(object).Assembly.Location]);
var mlc = new MetadataLoadContext(pathres, typeof(object).Assembly.GetName().Name);

PersistedAssemblyBuilder pab = new PersistedAssemblyBuilder(
    new AssemblyName("MyAssembly"),
    mlc.CoreAssembly!
);

ModuleBuilder mb = pab.DefineDynamicModule("Module");
TypeBuilder tb = mb.DefineType("Type");

var field = tb.DefineUninitializedData("UninitializedDataField", 100, FieldAttributes.Public);
var type = field.FieldType.BaseType;

Console.WriteLine(type.IsValueType);
Console.WriteLine(type);
Console.WriteLine(type == typeof(ValueType));

var contextValueType = mlc.CoreAssembly!.GetType("ValueType")!;
Console.WriteLine(type == contextValueType);
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.Reflection.MetadataLoadContext" Version="8.0.0" />
  </ItemGroup>

</Project>

Expected behavior

type.IsValueType should return true.
type == typeof(ValueType) should return false.
type == contextValueType should return true.

Actual behavior

type.IsValueType returns false.
type == typeof(ValueType) returns true.
type == contextValueType returns false.

Regression?

Unknown

Known Workarounds

No response

Configuration

Arch Linux x64, dotnet 9.0.100-rc.1.24381.3

Other information

As far as I can tell, this appears to be because DefineDataHelper in TypeBuilderImpl.cs uses typeof(ValueType) to define its helper type.

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jul 31, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-reflection
See info in area-owners.md if you want to be subscribed.

@TrueLunacy TrueLunacy changed the title DefineInitializedData on TypeBuilder in System.Reflection.Emit uses typeof(ValueType) internally even when using a MetadataLoadContext DefineUninitializedData on TypeBuilder in System.Reflection.Emit uses typeof(ValueType) internally even when using a MetadataLoadContext Jul 31, 2024
@steveharter
Copy link
Member

See also #103796 which fixed an issue for this. Regression?

@buyaa-n buyaa-n added this to the 9.0.0 milestone Aug 1, 2024
@buyaa-n buyaa-n removed the untriaged New issue has not been triaged by the area owner label Aug 1, 2024
@buyaa-n
Copy link
Contributor

buyaa-n commented Aug 2, 2024

See also #103796 which fixed an issue for this. Regression?

Not a regression, but similar bug

@dotnet-policy-service dotnet-policy-service bot added the in-pr There is an active PR which will close this issue when it is merged label Aug 2, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Sep 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Reflection in-pr There is an active PR which will close this issue when it is merged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants