Skip to content

Investigation: IJW HRException with 17+ by-ref parameters on x64#127169

Closed
Copilot wants to merge 1 commit intomainfrom
copilot/fix-hrexception-crash-17-parameters
Closed

Investigation: IJW HRException with 17+ by-ref parameters on x64#127169
Copilot wants to merge 1 commit intomainfrom
copilot/fix-hrexception-crash-17-parameters

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 20, 2026

Note

This content was generated by GitHub Copilot.

Calling a C++/CLI managed static with 17+ const T& parameters from native code throws HRException on .NET 9/10 (worked on .NET 8). Reproduction requires Windows + MSVC + IJW, which is not available in this environment, so no code change is being proposed — this PR is an investigation report with a pointer to the likely regression.

Description

// non-x86/IJW fall-through, inserts BEFORE the reverse-PInvoke transition:
GenTree* src   = gtNewLclvNode(lclNum, varDsc->TypeGet());
GenTree* store = gtNewStoreLclVarNode(shadowLclNum, src);
LIR::AsRange(fgFirstBB).InsertAtBeginning(src, store);
  • Hypothesis: with 18 byref params, GS marks all of them as vulnerable pointers (lvIsPtr = 1) and emits a shadow copy per param. The "17" threshold is probably where cumulative frame / stack-arg load first surfaces a latent ordering bug against the P/Invoke-enter helper. This is unverified.
  • Ruled out (by reading, not by running):
    • ijwhost start_runtime_thunk_stub and TheUMEntryPrestub prologues — both preserve the caller's stack-arg region across register/XMM save+restore and TAILJMP_RAX. Alignment and shadow-space math check out for >16 args.
    • patch_vtable_entries / FixupVTables / UMEntryThunk — no arg-count-dependent code paths.
  • Existing IJW tests (src/tests/Interop/IJW/**) do not cover managed static vtfixup entries with ≥17 by-ref parameters, which is consistent with the regression slipping through.

Suggested next steps (for a reviewer with a Windows box)

  • Run the repro with DOTNET_JitDump=IL_STUB* / DOTNET_JitDisasm=IL_STUB* and confirm whether shadow copies for the 18 params are emitted before JIT_REVERSE_PINVOKE_ENTER.
  • If confirmed, generalize the TARGET_X86 && FEATURE_IJW branch in gsCopyIntoShadow (and the matching branch in gsParamsToShadows) to fire for all opts.IsReversePInvoke() targets.
  • Add a regression test under src/tests/Interop/IJW/ covering a C++/CLI static exposed via vtfixup with ≥17 const T& parameters.

cc @AaronRobinsonMSFT @jkoritzinsky.

Copilot AI requested review from Copilot and removed request for Copilot April 20, 2026 16:59
Copilot AI changed the title [WIP] Fix HRException crash on managed function with 17+ parameters Investigation: IJW HRException with 17+ by-ref parameters on x64 Apr 20, 2026
Copilot AI requested a review from AaronRobinsonMSFT April 20, 2026 17:16
@dotnet-policy-service
Copy link
Copy Markdown
Contributor

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

@jkotas
Copy link
Copy Markdown
Member

jkotas commented Apr 21, 2026

Superseded by #127182

@jkotas jkotas closed this Apr 21, 2026
@jkotas jkotas deleted the copilot/fix-hrexception-crash-17-parameters branch April 21, 2026 23:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

[C++/CLI] [IJW] HRException crash when calling a managed function with 17+ parameters from native code (regression from .NET 8)

3 participants