-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Conversation
/morph build |
Build : SUCCESSBuild number : 381 Triggering tests/morph test |
Test : SUCCESSBuild number : 184 |
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; |
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.
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; |
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.
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)) |
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.
We don't need to complicate it with conditionals, how about standard: (pos + align - 1) & ~(align - 1)
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.
The reason I used conditionals is so this works with all numbers and not just powers of 2.
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.
Ok fair enough.
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.
Good to have!
@c1728p9 please address the review comments |
Updated this pr to use reinterpret_cast and static_cast. |
/morph build |
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.
Changed it to use reinterpret_cast for both. |
/morph build |
Build : SUCCESSBuild number : 395 Triggering tests/morph test |
Test : SUCCESSBuild number : 193 |
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.
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.
@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 |
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.