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

    <tr>
        <th>Summary</th>
        <td>
            stack limit support
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          mat-c
      </td>
    </tr>
</table>

<pre>
    Hi,

today llvm (and clang, rustc, ...) do not offer the option to check stack limit on hardware where stack probe is not possible.
For example mcu without mmu or hw limit. mpu allow some check, but have some limitation.

gcc have -fstack-limit-register and -fstack-limit-symbol option. This allow to put the stack limit in a register or a global variable.

arm documentation give some example how to implement software stack limit : 
* https://developer.arm.com/documentation/dui0041/c/Thumb-Procedure-Call-Standard/Function-entry-examples/Checking-for-stack-limit-violations?lang=en
* https://developer.arm.com/documentation/dui0041/c/ARM-Procedure-Call-Standard/Function-entry-examples/Checking-for-stack-limit-violations?lang=en

arm proposition is to have stack limit value at least 256 bytes above it.
This allow for small stack allocation (less than 256) to have a quick check that could use high register by doing direct comparison without arithmetic instruction.
The 256 (STACK_GUARD) could be a configure option to limit stack that is reserved.

For small stack allocation (less than STACK_GUARD)
```
if (stack_pointer < stack_limit)
    abort();
```

For bigger frame (more than STACK_GUARD) or vla, we need to compute the new stack position
```
if (stack_pointer - Framesize < stack_limit)
 abort();
```

See [1] for some arm assembly.

llvm support have already some support for emitting prologue according to stack allocation (split stack, stack probe, ...).


Could it be possible to implement stack limit check the same way ?


[1]

register in thumb1 : 64 bits (2 * 16 + 32) instruction
```
        CMP     sp, sl
        BHS     no_ovf
        BL abort
no_ovf
```

symbol in thumb1 : 128 bits (4 * 16 + 32 + 32) instruction
```
       LDR r7, = stack_limit_addr
       LDR r7, [r7]
 CMP     sp, r7
        BHS     no_ovf
        BL abort
no_ovf
```



in thumbv2,  with   sub.w instruction we could have a small STACK_GUARD.
We can do arithmetic operation and use high register.

if stack allocation fit in 12 bits register version
```
 add.w, ip, sl, # Framesize
  CMP     sp, ip
...
```

if stack allocation fit in 12 bits symbol version
```
  movw ip, :lower16:stack_limit
  movt ip, :upper16:stack_limit
  ldr ip, [ip]
  add.w, ip, ip, # Framesize
  CMP sp, ip
...
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8Vt2OozgTfRrnpgQCk9-LXJC08o30zUij6V7tZcvgCnjHYNY2ZLNPvypw0nRPz-6sNNpWJBpcrjpVderYwjlVtYh7tjqw1cNC9L42dt8IH5WLwsjr_oNi_MiSnCW5N1JcQeuhAca3opVQatFWjB_B9s6X9E8cx4zvQBpojQdzPqMFXyOYzivTgjdQ1lh-BedF-RW0apQH00ItrLwIi3Cp0WJY7awpEJQbXXXGOVVojFmSn4wF_EM0nUZoyh4uytem99A0PRgL9WVyHEPT9SC0NhdwpsEpNKEseg-1GHD6PBoLwhdPiVZlOS1H5xFJNFpEFivlPFqg1F8vuWtTGB2yjOGpVi4E9ga63o81mOesWhBwd2gsCKi0KYSGQVglQqIsyYVtQJqyb7CdMEKlbsBvNainOIpeyA6cOfuxnPOQLMuBfPIcau87x7Kc8RPjJ4kDatOhjYVt4tI09G0ekt57lSTLlPFTyfjpqe6bIvpsTYmytxgdhdbRoxetFFYyfjr1bUkbI2y9vUYBp2P8dKQWqLaKzsZG8woOyugxmGPZaaRV9oDtT8Kbf_n0X6GdOtZZ0xmnxn4pR82Z6DZrxyB0jyA8aBTOA1-tobh6dCAKMyAoTwSYEelsLLhGaB280NdyYgTjW43Oga9FS45oAm8hBfzeq_JrGDtfCw-l6bWE3iHUqqpfWFhcQRrVViCVxZLsmk5Y5Ux7nzBhla8b9KoE1Tpv-_I2Nk81jjkwvn18yo__f_7fL_mXB0IyhSsISmnas6p6O9eDqRpTTiM85cCiQzugDDNw-sHUXwemvesk_JJcncl4dPDcGdVSxiw7Ti6fRxTTJgCgHljP-Ja-ZIc3ngKiQlUVWjhb0SC5b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