Skip to content

Commit

Permalink
subsys/logging: Add compiler barriers to msg stack allocation
Browse files Browse the repository at this point in the history
This ensures that the compiler will have moved the stack pointer below the
stack area where the message will be constructed. Otherwise, the message
can be smashed by an interrupt handler while it is being built.

This bug was found on qemu_cortex_a53 using SDK 0.14.2 with gcc 10.3.0 when
building the samples/subsys/logging/syst/sample.logger.syst.deferred test
under twister using picolibc:

Signed-off-by: Keith Packard <[email protected]>
  • Loading branch information
keith-packard authored and carlescufi committed Jul 13, 2022
1 parent ca2f491 commit 4436fb1
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions include/zephyr/logging/log_msg.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,17 @@ enum z_log_msg_mode {
Z_LOG_MSG2_ALIGNMENT), \
sizeof(uint32_t))

/*
* With Zephyr SDK 0.14.2, aarch64-zephyr-elf-gcc (10.3.0) fails to ensure $sp
* is below the active memory during message construction. As a result,
* interrupts happening in the middle of that process can end up smashing active
* data and causing a logging fault. Work around this by inserting a compiler
* barrier after the allocation and before any use to make sure GCC moves the
* stack pointer soon enough
*/

#define Z_LOG_ARM64_VLA_PROTECT() compiler_barrier()

#define Z_LOG_MSG2_STACK_CREATE(_cstr_cnt, _domain_id, _source, _level, _data, _dlen, ...) \
do { \
int _plen; \
Expand All @@ -211,6 +222,7 @@ do { \
} \
struct log_msg *_msg; \
Z_LOG_MSG2_ON_STACK_ALLOC(_msg, Z_LOG_MSG2_LEN(_plen, 0)); \
Z_LOG_ARM64_VLA_PROTECT(); \
if (_plen) { \
CBPRINTF_STATIC_PACKAGE(_msg->data, _plen, \
_plen, Z_LOG_MSG2_ALIGN_OFFSET, flags, \
Expand Down

0 comments on commit 4436fb1

Please sign in to comment.