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

Marshal.SizeOf of bool and char report wrong value #113599

Closed
MichalStrehovsky opened this issue Mar 17, 2025 · 5 comments · Fixed by #113602
Closed

Marshal.SizeOf of bool and char report wrong value #113599

MichalStrehovsky opened this issue Mar 17, 2025 · 5 comments · Fixed by #113602
Labels
area-NativeAOT-coreclr in-pr There is an active PR which will close this issue when it is merged

Comments

@MichalStrehovsky
Copy link
Member

Their results seem to vary too:

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

Console.WriteLine($"[bool] unsafe: {Unsafe.SizeOf<bool>()}, marshal: {Marshal.SizeOf<bool>()}");
Console.WriteLine($"[char] unsafe: {Unsafe.SizeOf<char>()}, marshal: {Marshal.SizeOf<char>()}");

CoreCLR:

[bool] unsafe: 1, marshal: 1
[char] unsafe: 2, marshal: 2

Native AOT:

[bool] unsafe: 1, marshal: 4
[char] unsafe: 2, marshal: 1

.NET 9 and 10p3

Originally posted by @am11 in #113589

Copy link
Contributor

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Mar 17, 2025
@MichalStrehovsky
Copy link
Member Author

This is a very old bug. TODO for it was added in dotnet/corert#5707 (comment).

I'm actually not sure where the worms are, I would fix it by adding is primitive and not bool or char but we could have done that 7 years ago too so I'm likely missing context... so just filing as a bug.

@jkotas
Copy link
Member

jkotas commented Mar 17, 2025

I would fix it by adding is primitive and not bool or char

I think it would be fine if it fixed this bug, but I doubt that it is going to fix the problem.

NativeAOT returning 4 for bool means that it is treated as marshalled, but CoreCLR treats it as blittable (?).

@MichalStrehovsky
Copy link
Member Author

NativeAOT returning 4 for bool means that it is treated as marshalled, but CoreCLR treats it as blittable (?).

I think @am11 has the results swapped, I'm seeing this on CoreCLR:

[bool] unsafe: 1, marshal: 4
[char] unsafe: 2, marshal: 1

And this on native AOT:

[bool] unsafe: 1, marshal: 1
[char] unsafe: 2, marshal: 2

I.e. it's marshalled on CoreCLR, but blittable on native AOT.

@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 Mar 17, 2025
@am11
Copy link
Member

am11 commented Mar 17, 2025

I think @am11 has the results swapped

Ah, I copied nativeaot output first. (in my defense, it was late at night and I was focused on 32 byte delta when this other difference showed up 😅)

@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Mar 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-NativeAOT-coreclr in-pr There is an active PR which will close this issue when it is merged
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

3 participants