arm32 virtual stub generation [Legacy Jit + CoreRT]
In genCodeForCall function in Legacy GTF_CALL_M_VIRTSTUB_REL_INDIRECT case generated some code for jumping to RhpInitialDynamicInterfaceDispatch in CoreRT. It eventually descends into the RhpUniversalTransition function, then to RhpCidResolve. In asm windows implementation UniversalTransition.asm for arm32 it assumes that the CellDispatchInfo structure in the r12 register. But there it is exact not that. More like the desired value in the register r4, but RhpCidResolve crashes with r4 too, but a little later. So far I'm looking at an implementation for x64, but there desired register with the correct pointer. Are there any ideas how to fix this?
@sergign60 @jkotas @MichalStrehovsky please take a look
In CoreCLR ARM ABI, the "stub indirection cell" is passed in r4. The interface callsite looks like this:
movv r4, lo(indirection cell)
movt r4, hi(indirection cell)
ldr rX, [r4]
blx rX
In CoreRT (.NET Native for UWP on ARM), the "stub indirection cell" is passed in r12. The interface callsite looks like this:
movv r12, lo(indirection cell)
movt r12, hi(indirection cell)
ldr rX, [r12]
blx rX
The CoreCLR convention burns callee saved register that may slow down code overall.
I would stick with the CoreRT for UWP convention for CoreRT Unix ARM, as https://github.com/dotnet/corert/pull/4265#discussion_r131144518 suggested. It is better than the CoreCLR ABI. Does it sound reasonably to you? If it does, you will need to modify the JIT to emit the above sequence for CoreRT ABI.
@jkotas @MichalStrehovsky I need your help
the first call of RhpInitialDynamicInterfaceDispatch comes from the function
Dump of assembler code for function S_P_TypeLoader_System_Collections_Generic_LowLevelDictionary_2<System___Canon__System___Canon>__Find:
0x0017ba94 <+0>: stmdb sp!, {r4, r10, r11, lr}
0x0017ba98 <+4>: sub sp, dotnet/coreclr#48 ; 0x30
0x0017ba9a <+6>: add.w r11, sp, dotnet/coreclr#56 ; 0x38
0x0017ba9e <+10>: sub.w r4, r11, dotnet/coreclr#48 ; 0x30
0x0017baa2 <+14>: mov.w r10, dotnet/coreclr#5
0x0017baa6 <+18>: movs r2, #0
0x0017baa8 <+20>: movs r3, #0
0x0017baaa <+22>: stmia r4!, {r2, r3}
0x0017baac <+24>: subs.w r10, r10, dotnet/coreclr#1
0x0017bab0 <+28>: bhi.n 0x17baaa <S_P_TypeLoader_System_Collections_Generic_LowLevelDictionary_2<System___Canon__System___Canon>__Find+22>
0x0017bab2 <+30>: str r0, [sp, dotnet/coreclr#20]
0x0017bab4 <+32>: str r1, [sp, dotnet/coreclr#16]
0x0017bab6 <+34>: nop
0x0017bab8 <+36>: ldr r0, [sp, dotnet/coreclr#20]
0x0017baba <+38>: ldr r1, [sp, dotnet/coreclr#16]
0x0017babc <+40>: movs r2, #0
0x0017babe <+42>: bl 0x1a0e04 <S_P_TypeLoader_System_Collections_Generic_LowLevelDictionary_2<SystemRhpInitialDynamicInterfaceDispatch___Canon__System___Canon>__GetBucket>
0x0017bac2 <+46>: str r0, [sp, dotnet/coreclr#32]
0x0017bac4 <+48>: ldr r3, [sp, dotnet/coreclr#32]
0x0017bac6 <+50>: str r3, [sp, dotnet/runtime#3870] ; 0x2c
0x0017bac8 <+52>: ldr r3, [sp, dotnet/coreclr#20]
0x0017baca <+54>: ldr r3, [r3, dotnet/coreclr#4]
0x0017bacc <+56>: str r3, [sp, #0]
0x0017bace <+58>: ldr r3, [sp, dotnet/runtime#3870] ; 0x2c
0x0017bad0 <+60>: ldr r2, [sp, #0]
0x0017bad2 <+62>: ldr r2, [r2, dotnet/coreclr#4]
0x0017bad4 <+64>: cmp r3, r2RhpInitialDynamicInterfaceDispatch
0x0017bad6 <+66>: bcc.n 0x17badc <S_P_TypeLoader_System_Collections_Generic_LowLevelDictionary_2<System___Canon__System___Canon>__Find+72>
0x0017bad8 <+68>: bl 0xf0018 <S_P_CoreLib_Internal_Runtime_CompilerHelpers_ThrowHelpers__ThrowIndexOutOfRangeException>
0x0017badc <+72>: ldr r3, [sp, #0]
0x0017bade <+74>: ldr r2, [sp, dotnet/runtime#3870] ; 0x2c
0x0017bae0 <+76>: add.w r3, r3, r2, lsl dotnet/coreclr#2
0x0017bae4 <+80>: ldr r3, [r3, dotnet/coreclr#8]
0x0017bae6 <+82>: str r3, [sp, dotnet/coreclr#12]
0x0017bae8 <+84>: nop
0x0017baea <+86>: b.n 0x17bb2a <S_P_TypeLoader_System_Collections_Generic_LowLevelDictionary_2<System___Canon__System___Canon>__Find+150>
0x0017baec <+88>: nop
0x0017baee <+90>: ldr r3, [sp, dotnet/coreclr#12]
0x0017baf0 <+92>: ldr r3, [r3, dotnet/coreclr#4]
0x0017baf2 <+94>: str r3, [sp, dotnet/coreclr#4]
0x0017baf4 <+96>: ldr r3, [sp, dotnet/coreclr#20]
0x0017baf6 <+98>: ldr r0, [r3, #0]
0x0017baf8 <+100>: bl 0x19368 <__GenericLookupFromType_S_P_TypeLoader_System_Collections_Generic_LowLevelDictionary_2<System___Canon__System___Canon>_DispatchCell_S_P_CoreLib_System_IEquatable_1<TKey_System___Canon>__Equals>
0x0017bafc <+104>: str r0, [sp, dotnet/runtime#3860]
0x0017bafe <+106>: ldr r0, [sp, dotnet/coreclr#16]
0x0017bb00 <+108>: ldr r1, [sp, dotnet/coreclr#4]
0x0017bb02 <+110>: ldr r3, [sp, dotnet/runtime#3860]
0x0017bb04 <+112>: mov r4, r3
0x0017bb06 <+114>: ldr r3, [r4, #0]
0x0017bb08 <+116>: blx r3
0x0017bb0a <+118>: uxtb r0, r0
0x0017bb0c <+120>: str r0, [sp, dotnet/coreclr#24]
0x0017bb0e <+122>: ldrb.w r3, [sp, dotnet/coreclr#24]
0x0017bb12 <+126>: str r3, [sp, dotnet/runtime#3868] ; 0x28
0x0017bb14 <+128>: ldr r3, [sp, dotnet/runtime#3868] ; 0x28
0x0017bb16 <+130>: cmp r3, #0
0x0017bb18 <+132>: beq.n 0x17bb22 <S_P_TypeLoader_System_Collections_Generic_LowLevelDictionary_2<System___Canon__System___Canon>__Find+142>
0x0017bb1a <+134>: ldr r3, [sp, dotnet/coreclr#12]
0x0017bb1c <+136>: str r3, [sp, dotnet/coreclr#8]
0x0017bb1e <+138>: nop
0x0017bb20 <+140>: b.n 0x17bb46 <S_P_TypeLoader_System_Collections_Generic_LowLevelDictionary_2<System___Canon__System___Canon>__Find+178>
0x0017bb22 <+142>: ldr r3, [sp, dotnet/coreclr#12]
0x0017bb24 <+144>: ldr r3, [r3, dotnet/coreclr#12]
0x0017bb26 <+146>: str r3, [sp, dotnet/coreclr#12]
0x0017bb28 <+148>: nop
0x0017bb2a <+150>: ldr r3, [sp, dotnet/coreclr#12]
0x0017bb2c <+152>: cmp r3, #0
0x0017bb2e <+154>: bne.n 0x17bb34 <S_P_TypeLoader_System_Collections_Generic_LowLevelDictionary_2<System___Canon__System___Canon>__Find+160>
0x0017bb30 <+156>: movs r3, #0
0x0017bb32 <+158>: b.n 0x17bb36 <S_P_TypeLoader_System_Collections_Generic_LowLevelDictionary_2<System___Canon__System___Canon>__Find+162>
0x0017bb34 <+160>: movs r3, dotnet/coreclr#1
0x0017bb36 <+162>: str r3, [sp, dotnet/coreclr#36] ; 0x24
0x0017bb38 <+164>: ldr r3, [sp, dotnet/coreclr#36] ; 0x24
0x0017bb3a <+166>: cmp r3, #0
0x0017bb3c <+168>: bne.n 0x17baec <S_P_TypeLoader_System_Collections_Generic_LowLevelDictionary_2<System___Canon__System___Canon>__Find+88>
0x0017bb3e <+170>: movs r3, #0
0x0017bb40 <+172>: str r3, [sp, dotnet/coreclr#8]
0x0017bb42 <+174>: nop
0x0017bb44 <+176>: b.n 0x17bb46 <S_P_TypeLoader_System_Collections_Generic_LowLevelDictionary_2<System___Canon__System___Canon>__Find+178>
0x0017bb46 <+178>: ldr r0, [sp, dotnet/coreclr#8]
0x0017bb48 <+180>: add sp, dotnet/coreclr#48 ; 0x30
0x0017bb4a <+182>: ldmia.w sp!, {r4, r10, r11, pc}
End of assembler dump.
The appropriate fragment of code is
0x0017bafc <+104>: str r0, [sp, dotnet/runtime#3860]
0x0017bafe <+106>: ldr r0, [sp, dotnet/coreclr#16]
0x0017bb00 <+108>: ldr r1, [sp, dotnet/coreclr#4]
0x0017bb02 <+110>: ldr r3, [sp, dotnet/runtime#3860]
0x0017bb04 <+112>: mov r4, r3
0x0017bb06 <+114>: ldr r3, [r4, #0]
0x0017bb08 <+116>: blx r3 <=== go to RhpInitialDynamicInterfaceDispatch
0x0017bb0a <+118>: uxtb r0, r0
but I don't see a code to set r12 value that should be used further:
LEAF_ENTRY RhpInitialDynamicInterfaceDispatch, _TEXT
b C_FUNC(RhpInterfaceDispatchSlow)
LEAF_END RhpInitialDynamicInterfaceDispatch, _TEXT
LEAF_ENTRY RhpInterfaceDispatchSlow, _TEXT
str r12, [sp, #-8] <====
ldr r12, =C_FUNC(RhpCidResolve)
str r12, [sp, #-4]
b C_FUNC(RhpUniversalTransition_DebugStepTailCall)
LEAF_END RhpInterfaceDispatchSlow, _TEXT
I have tracked the place where the r12 value is set before this. It is the following chain of calls
0x0017babe <+42>: bl 0x1a0e04 <S_P_TypeLoader_System_Collections_Generic_LowLevelDictionary_2<System___Canon__System___Canon>__GetBucket>
0x1a0e66 <S_P_TypeLoader_System_Collections_Generic_LowLevelDictionary_2<System___Canon__System___Canon>__GetBucket+98>: bl 0x28384 <RhpIMod(Int32, Int32)>
0x283b2 <RhpIMod(Int32, Int32)+46>: bl 0xeb71c <__aeabi_idivmod>
0xeb724 <__aeabi_idivmod+8>: bl 0xeb48e <__divsi3+6>
Dump of assembler code for function __divsi3:
0x000eb488 <+0>: cmp r1, #0
0x000eb48a <+2>: beq.w 0xeb70a <__divsi3+642> ; unpredictable <IT:mi>
0x000eb48e <+6>: eor.w r12, r0, r1 <===
0x000eb492 <+10>: it mi
...
I guess that this is a temporary value that can not be used in RhpInitialDynamicInterfaceDispatch
So the question is where the correct value of r12 should be set?
Thanks in advance (sorry for long listing)
The dispatch cell contents is different for ARM. We might need the same for ARMEL.
The dispatch cell contents is different for ARM. We might need the same for ARMEL.
Yes. In addition to that, you will also need to fix the JIT to follow the CoreRT convention that passes the indirection cell in r12. The above code fragment needs to be like:
ldr r12, [sp, dotnet/runtime#3860] <- dispatch cell
ldr r3, [r12, #0]
blx r3
Look for VirtualStubParamInfo in RyuJIT. R4 needs to be R12 for CoreRT ABI.
@MichalStrehovsky @jkotas thanks!
https://github.com/dotnet/coreclr/pull/13335
@MichalStrehovsky @jkotas
Just now the simplest test fails because the value of interfacePointerValue in the first call of InterfaceDispatchCell::GetDispatchCellInfo (src/Native/Runtime/inc/rhbinder.h) is incorrect
cellInfo.InterfaceType = (EEType*)interfacePointerValue;
p/x (unsigned int)&m_pCache
$11 = 0x3c9fb4
p/x (int)m_pCache
$12 = 0x3ce11f
p/x ((unsigned int)&m_pCache + m_pCache)
$17 = 0x7980d3
x interfacePointerValue
0x7980d0: Cannot access memory at address 0x7980d0
As I understand m_pCache is initialized by the wrong value. Unfortunately I cannot find where this initialization occurs. Could you help me with this? Thanks in advance
It is pre-initialized in the compiler here: https://github.com/dotnet/corert/blob/master/src/ILCompiler.Compiler/src/Compiler/DependencyAnalysis/InterfaceDispatchCellNode.cs#L92
I guess that IMAGE_REL_BASED_RELPTR32 relocs are not handled in the objectwriter for ARM correctly.
I guess that
IMAGE_REL_BASED_RELPTR32relocs are not handled in the objectwriter for ARM correctly.
Other thing to point out is that the managed portion of the object writer currently maps this reloc to IMAGE_REL_BASED_REL32 to work around the lack of native ObjectWriter's understanding of it(https://github.com/dotnet/corert/blob/bc60b0667fe6cac44f591752ce0070342106fda9/src/ILCompiler.Compiler/src/Compiler/DependencyAnalysis/ObjectWriter.cs#L199).
@MichalStrehovsky @jkotas thanks!
https://github.com/dotnet/coreclr/pull/13400
https://github.com/dotnet/corert/pull/4337
@BredPet I think that this issue can be closed