Skip to content

[LoongArch64] Fix the ArgIteratorTemplate::GetNextOffset() return argOfs for 'ELEMENT_TYPE_VALUETYPE' which is flattened liking struct{Arr[], float}. - #103108

Merged
jkotas merged 2 commits into
dotnet:mainfrom
LuckyXu-HF:main-GC
Jun 6, 2024

Conversation

@LuckyXu-HF

Copy link
Copy Markdown
Contributor

[LoongArch64] Fix the ArgIteratorTemplate::GetNextOffset() return argOfs for 'ELEMENT_TYPE_VALUETYPE' which is flattened liking struct{Arr[], float}.

  • The 'ELEMENT_TYPE_VALUETYPE' is marked to 'TYPE_GC_OTHER' and the first flattened element Arr[] should be GC.
  • This also fixed the assert failure "!CREATE_CHECK_STRING(pMT && pMT->Validate())" under GC=8 of MarshalStructAsLayoutSeq.sh.

…rgOfs for 'ELEMENT_TYPE_VALUETYPE' which is flattened liking struct{Arr[], float}.

* The 'ELEMENT_TYPE_VALUETYPE' is marked to 'TYPE_GC_OTHER' and the first flattened element Arr[] should GC.
* This also fixed the assert failure "!CREATE_CHECK_STRING(pMT && pMT->Validate())" under GC=8 of MarshalStructAsLayoutSeq.sh.
@ghost ghost added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Jun 6, 2024
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jun 6, 2024
@LuckyXu-HF

Copy link
Copy Markdown
Contributor Author

The problem case is liking the following:

  • The fixedArrayTest passed on LA64 will use $a0 $f0.
###### MarshalStructAsLayoutSeq.cs
...
 333     [DllImport("MarshalStructAsParam")]
 334     static extern bool MarshalStructAsParam_AsSeqByValFixedBufferClassificationTest(FixedArrayClassificationTest str, float f);
...
 687                     Console.WriteLine("\tCalling MarshalStructAsParam_AsSeqByValFixedBufferClassificationTest with fixed array...");
 688                     FixedArrayClassificationTest fixedArrayTest = new FixedArrayClassificationTest
 689                     {
 690                         arr = new Int32Wrapper[3]
 691                         {
 692                             new Int32Wrapper { i = 123456 },
 693                             new Int32Wrapper { i = 78910 },
 694                             new Int32Wrapper { i = 1234 }
 695                         },
 696                         f = 56.789f
 697                     };
 698 
 699                     if (!MarshalStructAsParam_AsSeqByValFixedBufferClassificationTest(fixedArrayTest, fixedArrayTest.f))
 700                     {
 701                         Console.WriteLine("\tFAILED! Managed to Native failed in MarshalStructAsParam_AsSeqByValFixedBufferClassificationTest. Expected:True;Actual:False");
 702                         failures++;
 703                     }
...

###### ./src/tests/Interop/StructMarshalling/PInvoke/Struct.cs:
...
510 [StructLayout(LayoutKind.Sequential)]
511 public unsafe struct FixedArrayClassificationTest
512 {   
513     [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
514     public Int32Wrapper[] arr;
515     public float f;
516 }
...

Managed:MarshalStructAsParam_AsSeqByVal(int) calling Managed:MarshalStructAsParam_AsSeqByValFixedBufferClassificationTest(FixedArrayClassificationTest,float):bool will trigger GC, and the parameter FixedArrayClassificationTest is passed by $a0 $f0, which the arr[] stored by $a0 wasn't correctly replaced by GCHeap::Relocate() during gc scan.

  • This is because the argit.GetNextOffset() didn't return a correct offset for the arr[].
#### src/coreclr/vm/frames.cpp:
1375 void TransitionFrame::PromoteCallerStackHelper(promote_func* fn, ScanContext* sc,
1376                                                  MethodDesc *pFunction, MetaSig *pmsig)
1377 {
1378     WRAPPER_NO_CONTRACT;
...
...
1417     int argOffset;
1418     while ((argOffset = argit.GetNextOffset()) != TransitionBlock::InvalidOffset)  <---@@@
1419     {
1420         ArgDestination argDest(dac_cast<PTR_VOID>(pTransitionBlock), argOffset, argit.GetArgLocDescForStructInRegs());
1421         pmsig->GcScanRoots(&argDest, fn, sc);
1422     }
1423 }

The ELEMENT_TYPE_VALUETYPE which is flattened liking struct{Non-float, float} on LA64 should always return the first element's argOfs within ArgIteratorTemplate<ARGITERATOR_BASE>::GetNextOffset().

@LuckyXu-HF

Copy link
Copy Markdown
Contributor Author

cc @shushanhf

@LuckyXu-HF

Copy link
Copy Markdown
Contributor Author

@clamp03 @tomeksowi , as RISC-V has the same parse logic with LA64 in ArgIteratorTemplate<ARGITERATOR_BASE>::GetNextOffset(), I think there is a high probability that RISC-V can also hit this problem. Maybe you'd better check this on RISC-V too.

@shushanhf shushanhf left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM
Thanks

@LuckyXu-HF

Copy link
Copy Markdown
Contributor Author

Hi @jkotas , could you please review this PR? Thank you.

@tomeksowi tomeksowi left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you for heads-up, I'll fix for RISC-V shortly.

Comment thread src/coreclr/vm/callingconvention.h Resolved
delete redundancy condition.
@jkotas
jkotas merged commit 6575440 into dotnet:main Jun 6, 2024
@LuckyXu-HF
LuckyXu-HF deleted the main-GC branch June 7, 2024 01:05
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

arch-loongarch64 community-contribution Indicates that the PR has been added by a community member needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants