Skip to content

Commit

Permalink
JIT: use explicit cast away GC when passing byref to unmanaged callees (
Browse files Browse the repository at this point in the history
dotnet#107811)

Revise the fix done in dotnet#39105 to make the cast from byref to native int via
an explicit cast, rather than retyping the node.

Fixes dotnet#107045.
  • Loading branch information
AndyAyersMS authored Sep 17, 2024
1 parent 3992788 commit f5acabe
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/coreclr/jit/importercalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2046,9 +2046,7 @@ void Compiler::impPopArgsForUnmanagedCall(GenTreeCall* call, CORINFO_SIG_INFO* s
// call - The call
//
// Remarks:
// This makes use of the fact that TYP_I_IMPL <-> TYP_BYREF casts are
// implicit in JIT IR, allowing us to change the types directly without
// inserting a cast node.
// Make the "casting away" of GC explicit here instead of retyping.
//
void Compiler::impRetypeUnmanagedCallArgs(GenTreeCall* call)
{
Expand All @@ -2059,15 +2057,16 @@ void Compiler::impRetypeUnmanagedCallArgs(GenTreeCall* call)
// We should not be passing gc typed args to an unmanaged call.
if (varTypeIsGC(argNode->TypeGet()))
{
// Tolerate byrefs by retyping to native int.
// Tolerate byrefs by casting to native int.
//
// This is needed or we'll generate inconsistent GC info
// for this arg at the call site (gc info says byref,
// pinvoke sig says native int).
//
if (argNode->TypeGet() == TYP_BYREF)
{
argNode->ChangeType(TYP_I_IMPL);
GenTree* cast = gtNewCastNode(TYP_I_IMPL, argNode, false, TYP_I_IMPL);
arg.SetEarlyNode(cast);
}
else
{
Expand Down

0 comments on commit f5acabe

Please sign in to comment.