Skip to content

Ensure Thread stack is 8 byte aligned #5405

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

Merged
merged 1 commit into from
Nov 2, 2017

Conversation

c1728p9
Copy link
Contributor

@c1728p9 c1728p9 commented Oct 30, 2017

Ensure both the stack and stack size used in the Thread class are aligned to 8 bytes. This prevents the runtime error "Thread 0 error -11: Unknown" due to incorrect stack alignment.

@c1728p9
Copy link
Contributor Author

c1728p9 commented Oct 30, 2017

/morph build

@mbed-ci
Copy link

mbed-ci commented Oct 30, 2017

Build : SUCCESS

Build number : 381
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/5405/

Triggering tests

/morph test
/morph uvisor-test

@mbed-ci
Copy link

mbed-ci commented Oct 30, 2017

@0xc0170 0xc0170 requested review from bulislaw, pan- and a user October 31, 2017 08:51
rtos/Thread.cpp Outdated
@@ -36,15 +45,21 @@ namespace rtos {

void Thread::constructor(osPriority priority,
uint32_t stack_size, unsigned char *stack_mem, const char *name) {

const uintptr_t unaligned_mem = (uintptr_t)stack_mem;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be better using a reinterpret_cast.

rtos/Thread.cpp Outdated
_attr.name = name ? name : "application_unnamed_thread";
_attr.stack_mem = (uint32_t*)stack_mem;
_attr.stack_mem = (uint32_t*)aligned_mem;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this as a static_cast

@@ -23,6 +23,15 @@

#include "mbed.h"
#include "rtos/rtos_idle.h"
#include "mbed_assert.h"

#define ALIGN_UP(pos, align) ((pos) % (align) ? (pos) + ((align) - (pos) % (align)) : (pos))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to complicate it with conditionals, how about standard: (pos + align - 1) & ~(align - 1)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I used conditionals is so this works with all numbers and not just powers of 2.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok fair enough.

Copy link
Member

@bulislaw bulislaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to have!

@adbridge
Copy link
Contributor

@c1728p9 please address the review comments

@c1728p9
Copy link
Contributor Author

c1728p9 commented Oct 31, 2017

Updated this pr to use reinterpret_cast and static_cast.

@c1728p9
Copy link
Contributor Author

c1728p9 commented Oct 31, 2017

/morph build

@ghost
Copy link

ghost commented Oct 31, 2017

Hm. Sorry, my bad, the static_cast broke everything.

Ensure both the stack and stack size used in the Thread class are
aligned to 8 bytes. This prevents the runtime error
"Thread 0 error -11: Unknown" due to incorrect stack alignment.
@c1728p9
Copy link
Contributor Author

c1728p9 commented Oct 31, 2017

Changed it to use reinterpret_cast for both.

@c1728p9
Copy link
Contributor Author

c1728p9 commented Oct 31, 2017

/morph build

@mbed-ci
Copy link

mbed-ci commented Oct 31, 2017

Build : SUCCESS

Build number : 395
Build artifacts/logs : http://mbed-os.s3-website-eu-west-1.amazonaws.com/?prefix=builds/5405/

Triggering tests

/morph test
/morph uvisor-test

@mbed-ci
Copy link

mbed-ci commented Oct 31, 2017

Copy link
Member

@pan- pan- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to have ALIGN_UP and ALIGN_DOWN as functions rather than macros ?

Those macros will yield an invalid result if the pos in input is a pointer rather than an integer type due to pointer arithmetic.

@c1728p9
Copy link
Contributor Author

c1728p9 commented Nov 1, 2017

@pan- these macros won't yield an invalid result if you use a pointer. You'll get a compiler error as a function or macro if pos is a pointer, as % cannot be used on pointers. Without this fix PRs on master may fail the test tests-mbedmicro-rtos-mbed-threads randomly (but deterministically) due to slight ram changes causing the stack passed to Thread being unaligned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants