Closed d-otte closed 1 year ago
Mbed TLS uses malloc liberally. We are trying to reduce the usage of malloc, to better support constrainted or high-reliability systems that don't want dynamic memory allocation, but that concerns low-level crypto code, not key and certificate parsing.
However, in this specific case, it doesn't make sense to me that PEM processing would need optimizing for stack usage. Why would a constrained system use PEM at all?
This is a big issue for systems, which have a quite limited stack (but may have sufficient heap memory). Many embedded system have internal SRAM which is used for the stack, but also external RAM which is used for the heap and static buffers.
There are also systems which have the inverse problem. Plenty of stack space due to other call trees needing more stack, but constrained heap. I'm not sure where the line should be drawn, but this seems a bit at-will.
I've just seen that PR #2118 addresses several oft the functions and used similar approaches. I would like to backport those changes to mbedtls 2.16.
I think we've been too liberal with applying non-critical changes to LTS branches lately, leading to regressions such as https://github.com/ARMmbed/mbedtls/issues/3682. This is not a bug, it's a memory management balance change (which is not even a benefit on all platforms), so I'm opposed to changing the behavior in LTS branches.
Fixed by PR #3464
Some functions in mbedTLS use large amounts of stack (
mbedtls_pk_write_key_pem()
uses 5776 bytes in my configuration of v2.16.9).stack_usage.txt stack_usage.csv.txt
Steps to reproduce the reports
Justification - why does the library need this feature?
This is a big issue for systems, which have a quite limited stack (but may have sufficient heap memory). Many embedded system have internal SRAM which is used for the stack, but also external RAM which is used for the heap and static buffers.
Suggested enhancement
In most cases it is possible to avoid stack allocation and use memory from the heap. I hope to have pull requests ready this week.
Questions Are there special limitations, where heap memory must not be used?