-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gcc: Provide _sbrk implementation compatible with RTX #48
Conversation
I verified that the hang issue I was seeing when building and running the mbed official networking tests with GCC_ARM was related to this issue reported on the mbed forums: http://mbed.org/forum/mbed/topic/3803/?page=1#comment-18934 If you are using the 4.7 2013q1 update of GCC_ARM or newer then it will have a _sbrk() implementation which checks the new top of heap pointer against the current thread SP, stack pointer. See this GCC_ARM related thread for more information: https://answers.launchpad.net/gcc-arm-embedded/+question/218972 When using RTX RTOS threads, the thread's stack pointer can easily point to an address which is below the current top of heap so this check will incorrectly fail the allocation. I have added a _sbrk() implementation to the mbed SDK which checks the heap pointer against the MSP instead of the current thread SP. I have only enabled this for TOOLCHAIN_GCC_ARM as this is the only GCC based toolchain that I am sure requires this.
extern "C" caddr_t _sbrk(int incr) { | ||
static unsigned char* heap = (unsigned char*)&__end__; | ||
unsigned char* prev_heap = heap; | ||
unsigned char* new_heap = heap + incr; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be a good idea to align the new_heap pointer, as it happens for example here:
Not sure if this is a hard requirement, but it seems to be a bit safer.
Good catch, I can add this alignment but I don't think it is required. Both newlib and newlib-nano contain code to 8 byte align the pointers that they return from their respective malloc() implementations (MALLOC_ALIGN for newlib-nano and MALLOC_ALIGNMENT for newlib). 8-byte aligning the addresses returned from _sbrk() doesn't necessarily align the pointer finally returned from malloc() since the heap code inserts chunk headers at the beginning of its allocations. That means that only malloc() really controls the final alignment of the pointer returned to the application. I don't know if that Code Sourcery implementation is a good example to use anyway since I think it has a few flaws:
As I am reading through this code, I do wonder if it would be better to perform an extra check at the beginning of _sbrk() to see if the stack pointer is already lower than the heap pointer and call abort() if it is since there is already likely to be stack/heap corruption and continuing will only worsen the problem? |
You're right that malloc() should automatically align the pointer that it returns, adding some alignment code here might just make the code a bit safer. That said, I agree that this is a bit on the paranoid side of things, so we can leave the code as-is. |
Ok, at this point I plan to leave this pull request as is. |
gcc: Provide _sbrk implementation compatible with RTX
Thanks! |
When secure session is deleted, transaction callback must be called to inform request sender that response is not going to be received.
allow changing database at runtime and add checking for initialisation
Fixed SD card intialization failure
Use .mbedignore to exclude not needed components.
Add TI SDK for wifi
Updating mbed-os to mbed-os-5.3.4
…from d182fa4b3a..b8e6ed9def b8e6ed9def Update copyright (ARMmbed#48) git-subtree-dir: features/nanostack/sal-stack-nanostack-eventloop git-subtree-split: b8e6ed9defafad47d16ab2bd717a801cc4105ddd
…from d182fa4b3a..b8e6ed9def b8e6ed9def Update copyright (ARMmbed#48) git-subtree-dir: features/nanostack/sal-stack-nanostack-eventloop git-subtree-split: b8e6ed9defafad47d16ab2bd717a801cc4105ddd
…ges from fb20d3f32c..60c1fb61af 60c1fb61af Merge remote-tracking branch 'origin/master' into release_for_mbed_os 6c8166d77b Sync from Mbed OS (ARMmbed#50) b8e6ed9def Update copyright (ARMmbed#48) git-subtree-dir: connectivity/nanostack/sal-stack-nanostack-eventloop git-subtree-split: 60c1fb61afb5825fd6eac150eadded9fdc3047a8
…ges from fb20d3f32c..60c1fb61af 60c1fb61af Merge remote-tracking branch 'origin/master' into release_for_mbed_os 6c8166d77b Sync from Mbed OS (ARMmbed#50) b8e6ed9def Update copyright (ARMmbed#48) git-subtree-dir: connectivity/nanostack/sal-stack-nanostack-eventloop git-subtree-split: 60c1fb61afb5825fd6eac150eadded9fdc3047a8
I verified that the hang issue I was seeing when building and running
the mbed official networking tests with GCC_ARM was related to this
issue reported on the mbed forums:
http://mbed.org/forum/mbed/topic/3803/?page=1#comment-18934
If you are using the 4.7 2013q1 update of GCC_ARM or newer then it
will have a _sbrk() implementation which checks the new top of heap
pointer against the current thread SP, stack pointer.
See this GCC_ARM related thread for more information:
https://answers.launchpad.net/gcc-arm-embedded/+question/218972
When using RTX RTOS threads, the thread's stack pointer can easily
point to an address which is below the current top of heap so this
check will incorrectly fail the allocation.
I have added a _sbrk() implementation to the mbed SDK which checks the
heap pointer against the MSP instead of the current thread SP. I have
only enabled this for TOOLCHAIN_GCC_ARM as this is the only GCC based
toolchain that I am sure requires this.