<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/101943>101943</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            llvm17/18/trunk Insufficient DWARF debug_frame information for CMSE none-secure call 
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          dearlam
      </td>
    </tr>
</table>

<pre>
    Cortex-M55 (or Cortex-M33 I assume)
with -mcmse -gdwarf-4 -fomit-frame-pointer

 

The DWARF FDE for a function with attribute _\_\_attribute\_\_((cmse_nonsecure_entry))_
that calls via a function pointer with attribute _\_\_attribute\_\_((cmse_nonsecure_call))_
does not describe all movements to register SP (stack pointer)  which is the Canonical Frame Address
register (Dwarf_regnum_13) in the CIE.

This means it is not possible for a debugger to correctly unwind and show the call stack from callee to caller
(and beyond) because it looks in (Secure) stack memory for the pushed LR (link register) contents at the CFA offset the
DWARF describes, which is incorrect.

Compile with

_clang -c -march=armv8.1m.main+mve -target arm-none-eabi -Os -mcmse -gdwarf-4 -fomit-frame-pointer -mfloat-abi=hard -o sec_to_nonsec_call.o sec_to_nonsec_call.c_

(or -mcpu=cortex-m55, maybe also -I or -isystem for the path to arm_cmse.h header with a strange install)

 
sec_to_nonsec_call.c:
```
#include <arm_cmse.h>
#define CALL_ATTRIB __attribute__((cmse_nonsecure_call))
#define ENTRY_ATTRIB __attribute__((cmse_nonsecure_entry))
typedef void (*NS_ENTRY_POINT)(unsigned int) CALL_ATTRIB;
volatile NS_ENTRY_POINT nonsec_func = 0;
ENTRY_ATTRIB
void my_sec_func(unsigned int a_param)
{
    if (nonsec_func != 0)
    {
        nonsec_func(a_param);
    }

```
then disassemble
```
llvm-objdump -d --mcpu=cortex-m55 sec_to_nonsec_call.o > sec_to_nonsec_call.o.disasm.txt 
```
  

and decode the DWARF
```
  llvm-objdump --dwarf=frames --mcpu=cortex-m55 sec_to_nonsec_call.o > sec_to_nonsec_call.o.objdump_frames.txt 
```
 

[ or in newer llvm versions
```
  llvm-dwarfdump.exe --debug-frame --mcpu=cortex-m55 sec_to_nonsec_call.o > sec_to_nonsec_call.o.dwarfdump_frames.txt 
```
]

See there is no corresponding _DW_CFA_def_cfa_offset:+32_ and _DW_CFA_def_cfa_offset:+136_
(CIE Data alignment is -4)
for instructions that move the SP register matching the function call 'nonsec_func(a_param);'
```
 push.w    {r4, r5, r6, r7, r8, r9, r10, r11}
 bic       r1, r1, #0x1
 sub       sp, #0x88
 vlstm     sp 
 clrm      {r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, apsr}
 blxns     r1
```
The problem can be more easily observed here
[Compiler Explorer 9Yx3c7hnT](https://godbolt.org/z/9Yx3c7hnT)

Looking at the directives in the emitted assembler, neither the
   _push.w  \{r4, r5, r6, r7, r8, r9, r10, r11}_
nor the
   _sub     sp, #136_
- which both move the CFA/SP to lower memory addresses -
have matching directives, where one could reasonably expect
    _.cfi_def_cfa_offset 32 ;// 8*4_
and
    _.cfi_def_cfa_offset 136 ;// 34 *4_
respectively.

This is using trunk (as I haven't tried an llvm 19.1.0 rc1 yet).

It also reproduces with llvm 17.0.6 and llvm 18.1.8.
Using Compiler Explorer it also exists for versions back to llvm armv7a-clang 11.0.0
Version 10.0.1 does not support the cmse attributes.
Using _-mfix-cmse-cve-2021-35465_ for VLLDM, where supported, makes no difference as that is the restore part of the code.

I believe the bug is caused by ARMExpandPseudoInsts.cpp
 [ARMExpandPseudoInsts.cpp line 1555ff](https://github.com/llvm/llvm-project/blob/d8b985c9490664f7ec923e59cf6c603d998179ae/llvm/lib/Target/ARM/ARMExpandPseudoInsts.cpp#L1555)

where function _ARMExpandPseudo::CMSEPushCalleeSaves()_
uses _ARM::t2STMDB_UPD_
and also where function _ARMExpandPseudo::CMSESaveClearFPRegsV81()_ makes space for the later _VLSTM_
near
```
    BuildMI(MBB, MBBI, DL, TII->get(ARM::tSUBspi), ARM::SP)
        .addReg(ARM::SP)
        .addImm(CMSE_FP_SAVE_SIZE >> 2)
        .add(predOps(ARMCC::AL)); 
```
does not use sequences to add Call Frame Information
(as used by [ARMBaseInstrInfo.cpp line 6468ff](https://github.com/llvm/llvm-project/blob/55255669077b191043b1a8920107890815151835/llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp#L6468)
)
 
```
   MachineFunction &MF = *MBB.getParent();

  int64_t StackPosEntry =
      MF.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr,      new_8byte_aligned_offset));

  BuildMI(MBB, It, DebugLoc(), get(ARM::CFI_INSTRUCTION))
      .addCFIIndex(StackPosEntry)
      .setMIFlags(MachineInstr::FrameSetup); 
```
Similarly _ARMExpandPseudo::CMSERestoreFPRegsV81()_ does not adjust the CFA, despite the SP register moving back to higher memory addresses, where I'd expect similar statements but with negative value and {_}MachineInstr::FrameDestroy{_}.

_CMSEPopCalleeSaves()_ also makes no CFA adjustments.

Note there are also _ARMExpandPseudo::CMSESaveClearFPRegsV8()_ and _CMSERestoreFPRegsV8()_ similarly lacking.

PARTIAL WORKAROUND?
Remove _-fomit-frame-pointer_ (or use {_}-fno-omit-frame-pointer{_}).

This means the SP is copied to register r7 and a .setfp r7 directive
```
        .setfp  r7, sp
        mov     r7, sp
        .cfi_def_cfa_register r7
```
This in turn leads to the corresponding DWARF changing the CFA base register with
DW_CFA_def_cfa_register: reg7
so adjustments to SP by the setup code are of course possible and the link register's value can be found in memory, in theory...

BUT the later CLRM wipes out the R7 contents in order to leak nothing at the _blxns_
transition from secure to none-secure.
I think this is just as Requirement 54 of
[https://github.com/ARM-software/acle/releases/download/r2024Q2/cmse-1.4.pdf]
expects.

So, sadly, it is not even a partial workaround.

Moreover, use of framepointer grows code size so it cannot be a project default.

This being for embedded target we need to be able to debug exactly what we will ship.
Our debugger is able to recognise the FNC_RETURN pattern created by blxns instruction and has privileged access to the banked Secure stack memory in house.

And there's no requirement to clear register SP because it is banked, so does not leak information as section 4.3.1 'Information leakage' of cmse-1.4 describes.

Indeed for none secure calls with stack based arguments or return value the toolchain needs to take significant steps to validate and copy these from one stack to another.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysWVtz27qu_jXKC8YaS_JFfsiDL_GM58Rtjp2uNfu8aCgRsrkikdok5ST7158BKd8SN-3ae3dauZZJEADxAR9IZozYScT7YDgLhos71tq90vccma5YfZcr_n4_V9riW289HEIQp0rD8UWSwAqYMW2NQTwJ-ougP30Vdg-9uqgNQm_HX5kuewPolaoWtldqVmOvUUJa1H5895zHwbR_-eZ5j7D4c7pZwnLxAKXSwKBsZWGFkuAWYdZqkbcWIQuGc_fv9Or4IojTIE5JmUwqabBoNWYorX4nheNJ5heze2ahYFVl4CDY5Uqdrv_hiiT6akGu0IBUFjiaQoscgVUV1OqANUprwCrQuBOGlt4-kduNZcXLUZ0gnoB32eteFHsQBuweYc6kkqJgFSzJ0TDlXKMxfsmTvCBOF7QtmcadbOssSkickF7E6iG83gZhoEYmDQhL65DSjTJG5BV228Ixb3c71KR1obTGwlbv0MpXITkwycHs1auTTn4Ab0mpVe2-I7p59L9jSMQpzcrxXUlOuuVYsNYgaVAp9WJI2SBOt865NMCLrLFW-t0pRYs1rdkjh8cNja2EfDm5lKYUSlrnama94cspqLI06L56RXz8HbfIBPEcTv4WsjP1yl1zVTeiQhcul--zomJyB70CejXTxT5IFkzXhzSM6rBmQgbxrD4g9CzTO7TAdN2TSmIPWS6g9938HqKgV5eVYrbHchEkiz3THHoKDBaZVV04ulgMb74ssitIOqj36qJpg2RReMjXwyF5oWbvLmSNgt4KaJgw78ZifXY-s3vaV6brjDQP97BHxk9IAmM1kzsEIY3t0PGTfHBL0yA5Dhz1u7-d0omQRdVyhCCZnxcPkofTAI6lkAjz6eNjNn1-3qxmkJ1xnP0KwR_lPHx73vzj9wVdJJ8u97w3yLGEgxIc3JTpt23mpT59X317doPTVrpEzUFIS_F7oX6QzLyog6qY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