Skip to content

Commit

Permalink
JIT: retype byrefs passed to unmanaged callers as native int
Browse files Browse the repository at this point in the history
Make the jit more robust in cases where the IL producer is passing a byref
to an unmanaged caller, by retyping the argument as native int.

Allows the jit to produce self-consistent GC info and avoid the issues
seen in dotnet#34279, at least for byrefs.

Closes dotnet#39040.
  • Loading branch information
AndyAyersMS committed Jul 10, 2020
1 parent 4ef07c2 commit b333040
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/coreclr/src/jit/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6881,14 +6881,27 @@ void Compiler::impPopArgsForUnmanagedCall(GenTree* call, CORINFO_SIG_INFO* sig)

for (GenTreeCall::Use& argUse : GenTreeCall::UseList(args))
{
// We should not be passing gc typed args to an unmanaged call.
GenTree* arg = argUse.GetNode();
call->gtFlags |= arg->gtFlags & GTF_GLOB_EFFECT;

// We should not be passing gc typed args to an unmanaged call.
if (varTypeIsGC(arg->TypeGet()))
{
assert(!"*** invalid IL: gc type passed to unmanaged call");
// Tolerate byrefs by retyping 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 (arg->TypeGet() == TYP_BYREF)
{
arg->ChangeType(TYP_I_IMPL);
}
else
{
assert(!"*** invalid IL: gc ref passed to unmanaged call");
}
}

call->gtFlags |= arg->gtFlags & GTF_GLOB_EFFECT;
}
}

Expand Down

0 comments on commit b333040

Please sign in to comment.