Skip to content

_sbrk heap & stack collision check is risky #9542

Closed
@alzix

Description

@alzix

Description

Reopening old discussion #1561 as the SW and HW has changed since last revisited.

Stack and Heap collision checked in _sbrk implementation by comparing to get_MSP() is risky.

                +-----------+ 0x00000000
                |           |
                |           |
                +-----------+
                |           |
                |    heap   |
                |     |     |
                |     |     |
                |     V     |
                +-----------+ <-- MSPLIM (ARMv8-M)
                |     ^     |
                |     |     |
                |     |     |
                |   stack   |
                |           |
   get_MSP() -->|           |
                |           |
                +-----------+
                |           |
                |           |
                +-----------+ 0xFFFFFFFF

In current mbed-os implementation MSP is used mainly for during initialization sequence and by interrupt handlers. MSP size can be predicted and carefully chosen by system integrator according to the application use case.
Current implementation assumes dynamic stack limit watermark and makes system unpredictable. Big allocations may work before stack is full but fail afterwards.

In addition as mentioned by @pan- #1561 (comment) , the check is not atomic and we do know for sure that MSP is used by IRQ handlers. This can lead to corruption.

Now we have new HW which was not considered during last discussion. For ARMv8-M we have stack limit registers. These registers will trigger fault once push operations are executed. This dynamic stack limit approach makes them unusable, as regular write to memory will be permitted.

Issue request type

[ ] Question
[x] Enhancement
[ ] Bug

CC @sg- @erezlandau

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions