Skip to content
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

Merged
merged 1 commit into from
Sep 4, 2013
Merged

gcc: Provide _sbrk implementation compatible with RTX #48

merged 1 commit into from
Sep 4, 2013

Conversation

adamgreen
Copy link
Contributor

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.

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;
Copy link
Contributor

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:

https://github.com/mbedmicro/mbed/blob/master/libraries/mbed/targets/cmsis/TARGET_NXP/TARGET_LPC176X/TOOLCHAIN_GCC_CS/sys.cpp

Not sure if this is a hard requirement, but it seems to be a bit safer.

@adamgreen
Copy link
Contributor Author

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:

  • The _sbrk() routine is expected to return -1 on error and not NULL.
  • The _sbrk() routine should really set the global errno to ENOMEM on error. I do see code in newlib-nano's malloc() implementation which will set errno appropriately if it sees the -1 returned from _sbrk(). I didn't see the same in the standard newlib implementation.
  • I think it would be best if _sbrk() just returns if the heap and stack would collide and not still advance the heap pointer past the stack pointer as seen in the CS routine.
  • The unsigned int type for the incr parameter won't cause any bugs but it would imply the value will never have a negative value. This isn't true with the standard newlib which passes in negative values to free space back to the system.

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?

@bogdanm
Copy link
Contributor

bogdanm commented Sep 3, 2013

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.
Regarding your idea to check _sbrk against the stack pointer, I believe it should work, but it must be tested, especially with the RTOS enabled. I didn't have a close look at our RTOS internals yet and I don't know how it would cope with such a change.

@adamgreen
Copy link
Contributor Author

Ok, at this point I plan to leave this pull request as is.

bogdanm added a commit that referenced this pull request Sep 4, 2013
gcc: Provide _sbrk implementation compatible with RTX
@bogdanm bogdanm merged commit 45565cb into ARMmbed:master Sep 4, 2013
@adamgreen adamgreen deleted the gccRetargetSbrk branch September 4, 2013 08:13
@adamgreen
Copy link
Contributor Author

Thanks!

SeppoTakalo pushed a commit that referenced this pull request Nov 9, 2016
When secure session is deleted, transaction callback must be called
to inform request sender that response is not going to be received.
donatieng pushed a commit to donatieng/mbed-os that referenced this pull request May 23, 2018
allow changing database at runtime and add checking for initialisation
geky pushed a commit to geky/mbed that referenced this pull request Aug 25, 2018
Fixed SD card intialization failure
yossi2le pushed a commit to yossi2le/mbed-os that referenced this pull request Jan 2, 2019
Use .mbedignore to exclude not needed components.
linlingao added a commit to linlingao/mbed-os that referenced this pull request Jul 12, 2019
pan- added a commit to pan-/mbed that referenced this pull request May 29, 2020
Updating mbed-os to mbed-os-5.3.4
artokin pushed a commit to artokin/mbed-os that referenced this pull request Jun 22, 2021
…from d182fa4b3a..b8e6ed9def

b8e6ed9def Update copyright (ARMmbed#48)

git-subtree-dir: features/nanostack/sal-stack-nanostack-eventloop
git-subtree-split: b8e6ed9defafad47d16ab2bd717a801cc4105ddd
artokin pushed a commit to artokin/mbed-os that referenced this pull request Jun 28, 2021
…from d182fa4b3a..b8e6ed9def

b8e6ed9def Update copyright (ARMmbed#48)

git-subtree-dir: features/nanostack/sal-stack-nanostack-eventloop
git-subtree-split: b8e6ed9defafad47d16ab2bd717a801cc4105ddd
artokin added a commit to artokin/mbed-os that referenced this pull request Dec 2, 2021
…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
artokin added a commit to artokin/mbed-os that referenced this pull request Dec 8, 2021
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants