Skip to content

Commit 4436fb1

Browse files
keith-packardcarlescufi
authored andcommitted
subsys/logging: Add compiler barriers to msg stack allocation
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]>
1 parent ca2f491 commit 4436fb1

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

include/zephyr/logging/log_msg.h

+12
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,17 @@ enum z_log_msg_mode {
198198
Z_LOG_MSG2_ALIGNMENT), \
199199
sizeof(uint32_t))
200200

201+
/*
202+
* With Zephyr SDK 0.14.2, aarch64-zephyr-elf-gcc (10.3.0) fails to ensure $sp
203+
* is below the active memory during message construction. As a result,
204+
* interrupts happening in the middle of that process can end up smashing active
205+
* data and causing a logging fault. Work around this by inserting a compiler
206+
* barrier after the allocation and before any use to make sure GCC moves the
207+
* stack pointer soon enough
208+
*/
209+
210+
#define Z_LOG_ARM64_VLA_PROTECT() compiler_barrier()
211+
201212
#define Z_LOG_MSG2_STACK_CREATE(_cstr_cnt, _domain_id, _source, _level, _data, _dlen, ...) \
202213
do { \
203214
int _plen; \
@@ -211,6 +222,7 @@ do { \
211222
} \
212223
struct log_msg *_msg; \
213224
Z_LOG_MSG2_ON_STACK_ALLOC(_msg, Z_LOG_MSG2_LEN(_plen, 0)); \
225+
Z_LOG_ARM64_VLA_PROTECT(); \
214226
if (_plen) { \
215227
CBPRINTF_STATIC_PACKAGE(_msg->data, _plen, \
216228
_plen, Z_LOG_MSG2_ALIGN_OFFSET, flags, \

0 commit comments

Comments
 (0)