Skip to content

Commit b43f216

Browse files
committed
toolchain/gcc: Add 'compiler_barrier' in CODE_UNREACHABLE
GCC 14.3 will happily delete any code that appears before __builtin_unreachable that isn't separated with an obvious branch. That includes __asm__ statements, even those which generate traps. The failure case that I debugged was on x86 in z_check_stack_sentinel. There is a store to restore the sentinel to the correct value just before the ARCH_EXCEPT, and that macro emits 'int $32' followed by CODE_UNREACHABLE. Because the compiler didn't understand that ARCH_EXCEPT was changing execution flow, it decided that the sentinel restoring store 'couldn't' be reached and elided it. I added the compiler barrier to CODE_UNREACHABLE as that construct is required for this problem to occur, but a more surgical fix might be to add :"memory" to the relevant __asm__ statements. Finding and fixing all of those now and forever seems like it would be too burdensome to be worth any possible code improvement. Signed-off-by: Keith Packard <[email protected]>
1 parent 2b6d0f7 commit b43f216

File tree

1 file changed

+4
-1
lines changed
  • include/zephyr/toolchain

1 file changed

+4
-1
lines changed

include/zephyr/toolchain/gcc.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@
118118
__builtin_unreachable(); \
119119
}
120120
#else
121-
#define CODE_UNREACHABLE __builtin_unreachable()
121+
#define CODE_UNREACHABLE do { \
122+
compiler_barrier(); \
123+
__builtin_unreachable(); \
124+
} while(0)
122125
#endif
123126
#define FUNC_NORETURN __attribute__((__noreturn__))
124127

0 commit comments

Comments
 (0)