Bug in save_and_disable_interrupts() #1644
Labels
No labels
bazel
bluetooth
board settings
boot_stage2
bug
build
c++
can't reproduce
clang
clib-interface
cmsis
code cleanliness
code size
compiler version issues
cyw43_driver
debug
discussion
documentation
duplicate
dynamic_clock
elf2uf2
enhancement
errata
filesystem
flash layout
floating point
FreeRTOS
hardware_adc
hardware_claim
hardware_clocks
hardware_dma
hardware_exception
hardware_flash
hardware_gpio
hardware_i2c
hardware_irq
hardware_pio
hardware_pwm
hardware_rcp
hardware_rtc
hardware_spi
hardware_sync
hardware_uart
hardware_vreg
hardware_watchdog
hardware_xosc
help wanted
host
IAR
installation
invalid
linker scripts
lto
lwip
mbedtls
missing API
needs reporter detail
networking
newlib
newlib-multicore
non-c
non-cmake
non-gcc
openocd
orphan PR
OS integration
pico_async_context
pico_base
pico_binary_info
pico_flash
picolibc
pico_lwip
pico_malloc
pico_multicore
pico_printf
pico_runtime
pico_stdio
pico_stdio_usb
pico_sync
pico_time
pico_util
pioasm
port
question
review
review2
rtos interop
runtime init
static library
svd
test
tinyusb
vscode
wifi integration
wontfix
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-raspberrypi/pico-sdk#1644
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
The save_and_disable_interrupts function is buggy for two reasons. Either one is not a bug in itself, but combined they produce the following problem.
The first problem :
The function
save_and_disable_interruptsis :__force_inline static uint32_t save_and_disable_interrupts(void) { uint32_t status; pico_default_asm_volatile( "mrs %0, PRIMASK\n" "cpsid i" : "=r" (status) ::); return status; }The inline assembly statement does not include the
: "memory"statement at the end. Without this, the compiler is not prevented from reordering and optimizing stuff.The second problem is that the function is declared inline. Coupled with problem number one, what it does is that when the body of the function is "copied" in place by the compiler, the compiler can then start optimizing things. Because there is no ~implicit instruction barrier with the "memory", then the compiler can reorder things around.
I came accross a situation there the following code would not work with optimization :
`
uint32_t counter = 0;
...
uint32_t irq_save = save_and_disable_interrupts();
{
counter--;
}
restore_interrupts(irq_save);
`
The problem was that an interrupt modifying
counterwould pop and execute in the middle of the critical zone above like so :uint32_t irq_save = save_and_disable_interrupts(); { counter--; <-------------- Interrupt happening in the middle of the read-modify-write } restore_interrupts(irq_save);There is a bit of semantic wiggle room here (or lack of documentation)... in that you could argue that it is the caller's job to prevent compiler re-ordering (is this just an intrinsic?). The synchronization primitives in the SDK already do that, so don't have an issue.
Still, i do tend to agree that this is surprising, but I would want to check if this affects code generation on existing code negatively first.
agree after thought that this should be fixed
fixed