-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[.NET 9] DllImport with Custom Marshaler Passes non-null address when null
value for field is specified.
#109033
Comments
null
is specified.
null
is specified.null
value for field is specified.
null
value for field is specified.null
value for field is specified.
Tagging subscribers to this area: @dotnet/interop-contrib |
You are using the wrong marshalling mechanism. To marshal a string as UTF8 in .NET Standard, you can either specify |
That is right. I am also aware that there are better ways to express such a requirement in modern .NET. I used this code specifically as an example because this is the wrapping code that mono/CppSharp produces by default when a C/C++ header lists a I simply ran across the issue while working (using) one of these projects. Given there didn't seem to be a relevant entry in list of breaking changes for .NET 9, I assumed this was unintentional, or not noted. |
The |
runtime/src/coreclr/System.Private.CoreLib/src/System/StubHelpers.cs Lines 800 to 806 in dfc2b85
Unclear if this is in .NET 9, but the above branch should also have |
@AaronRobinsonMSFT Yes, I originally ran into this bug in RC2 last weekend. I stated this as a regression i.e. it's a commit between Preview 1 and Preview 2 |
@Sewer56 Ah. Thanks for tracking down the general area. This is likely related to some of our many conversions from native to managed code - these kinds of bugs are too easy to introduce in this conversion. We'll get it addressed in .NET 9, but it will probably go out in an update and not GA for .NET 9. |
That's alright, thanks for a timely response. I imagine the timeline for GA would be very tight here, since dotNETConf is 3 weeks away; so that's within expectation. Hopefully all goes well for another smooth launch 👍 |
Oh, it did actually ship in release; unexpected and very welcome, thank you! |
Description
In .NET 9 (Preview 2 and newer), passing
null
to a parameter which uses aCustomMarshaler
results in a non-zero value passed to native code. This can lead to memory corruption or invalid memory reads, depending on what the native code does with the value.Reproduction Steps
Attached reproduction: cimgui-stub.zip
To Reproduce
Run the
csharp
project (csharp.csproj
) provided.When ran with .NET 9 Preview 1 or older, the result is:
When ran with .NET 9 Preview 2 or newer the result is:
When
null
is passed to a string parameter with anyCustomMarshaler
specified, the generatedDllImport
stub passes a non-zero address. (Note: Marshaler is skipped, so implementation of marshaler used withCustomMarshaler
is not relevant)To Compile the Rust Library/ Native Code
[Note: I've included an x64 precompiled DLL in the
csharp
folder out of the box, this is only needed if you want to change the native DllImport target, e.g. reduce param count for debugging]rustup
: https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.execimgui_export
cargo build --release
target/release/cimgui_export.dll
csharp
folder, replacing the existing file.Expected behavior
format address
should be0
(null). Instead an unexpected address is used.Actual behavior
format address
is not0
(null)Regression?
.NET 9 (Preview 1) and older work fine.
Regression is introduced in Preview 2.
Known Workarounds
No response
Configuration
N/A
This reproduces in both x86 and x64 on Windows.
Other information
The reproduction is small enough that it can be included here for convenience:
Rust part:
I can across this issue when trying older code with .NET 9 that used
dear imgui
bindings made withCppSharp
. So in the repro I tried to model that.The text was updated successfully, but these errors were encountered: