Version: QP/C 8.1.6, upstream tag v8.1.6
Port: Unmodified ports/arm-cm/qk/gnu, Cortex-M0
Environment: QEMU microbit
Configuration: Q_UNSAFE not defined, QP_API_VERSION set to 9999; QTicker at priority 1 (TICKER_PRIO), timeout recipient AO at priority 2 (RECEIVER_PRIO) by default.
When tick processing runs in a lower-priority thread under a preemptive kernel (such as QTicker under QK) and a higher-priority AO rearms an expired one-shot timer from its timeout handler, QTimeEvt_tick_() imports that timer back into the current scan. A one-tick rearming expires again immediately within the same tick-processing call: for example, a handler that rearms for 1 tick up to a bounded retry count exhausts all retries within a single tick. With unbounded rearming, QTimeEvt_tick_() never returns, so the ticker's priority level and every lower one, including idle, never run again.
The reproducer below uses the official ARM QK port, including its PendSV_Handler and NMI_Handler. It provides application code and minimal startup/runtime support.
The current tick should deliver the due timeout and return normally.
Rearming that expired one-shot from its timeout handler should not make it expire again within the same tick-processing call. Its new countdown should begin with a subsequent tick.
Requirements: arm-none-eabi-gcc and qemu-system-arm supporting the microbit machine.
Tested with Arm GNU Toolchain 15.3.Rel2 and QEMU 11.1.3.
Save the seven files below in one directory. Obtain upstream QP/C:
git clone --depth 1 --branch v8.1.6 https://github.com/QuantumLeaps/qpc.git qpc
From the directory containing the reproducer files:
sh run.sh ./qpc
The application constructs everything before starting the AOs, arms its timer from the recipient's initial transition, and starts framework execution. QK_onIdle() then pends one actual SysTick exception via SCB->ICSR (PENDSTSET).
SysTick's automatic hardware counter is left disabled so semihosting output latency cannot cause extra ticks to accumulate during handler execution; another tick is pended only after execution returns to QK_onIdle().
One SysTick trigger produces:
timeout: trigger=1 delivery=1
timeout: trigger=1 delivery=2
timeout: trigger=1 delivery=3
timeout: trigger=1 delivery=4
timeout: trigger=1 delivery=5
timeout: trigger=1 delivery=6
timeout: trigger=1 delivery=7
timeout: trigger=1 delivery=8
STOP: application observation limit; no QP assertion
The application deliberately exits after eight deliveries to provide a finite reproduction. Tick processing has not returned to idle, no second tick has been triggered, and no QP assertion has fired.
The control flow described below repeats without a terminating condition if this application observation limit is removed.
Additional cases:
# Use QTimeEvt_rearm() instead of QTimeEvt_armX():
sh run.sh ./qpc -DUSE_REARM=1
# Rearm the one-shot for two ticks:
sh run.sh ./qpc -DDELAY=2U
sh run.sh ./qpc -DDELAY=2U -DUSE_REARM=1
# Control: place QTicker above the recipient AO:
sh run.sh ./qpc -DTICKER_PRIO=2U -DRECEIVER_PRIO=1U
sh run.sh ./qpc -DTICKER_PRIO=2U -DRECEIVER_PRIO=1U -DDELAY=2U
# Control: use a one-tick periodic timer:
sh run.sh ./qpc -DPERIODIC_CONTROL=1
Results:
QTicker above the recipient (-DTICKER_PRIO=2U -DRECEIVER_PRIO=1U), the same state machine delivers on triggers 1, 2, 3, and 4 (DELAY=1U) and triggers 1 and 3 (DELAY=2U), returning to idle with failures=0 and status 0.overflow.c runs the same one-tick rearming with the SysTick hardware counter enabled, no delivery limit, and no per-delivery output. It shares runtime.c, qp_config.h, and microbit.ld with the main reproducer:
sh run_overflow.sh ./qpc
sh run_overflow.sh ./qpc -DTICKER_PRIO=2U -DRECEIVER_PRIO=1U # control
Tick processing never returns after the first tick. Each later tick is counted by QTicker_trig_(), and the 257th trips Q_REQUIRE_INCRIT(950, nTicks < 0xFFU) (qf_actq.c:471):
QP assertion: qf_actq 950 triggers=257 deliveries=351747 idles=39604 idles_at_first_tick=39604
The timeout was delivered about 350,000 times in that window, and QK_onIdle() never ran again after the first tick. Exact delivery and idle counts vary between runs; the trigger count and assertion identifier do not. With QTicker above the recipient, the same program delivers once per tick and keeps reaching idle:
idle reached 1000000 times; triggers=25 deliveries=25 idles=1000000 idles_at_first_tick=39866
In the field, this shows up as a tick-overrun assertion in QTicker_trig_() rather than as a timer problem.
In QTimeEvt_tick_(), QTimeEvt_expire_() unlinks the expired one-shot and clears QTE_FLAG_IS_LINKED before the timeout event is posted (qf_time.c:428-431).
Posting the timeout makes the higher-priority recipient ready, preempting the ticker. When the timeout handler rearms the timer, QTimeEvt_armX() (or QTimeEvt_rearm()) sees QTE_FLAG_IS_LINKED == 0U and places it on the freshly armed list, QTimeEvt_timeEvtHead_[tickRate].act (qf_time.c:121-131, qf_time.c:235-248).
When the ticker resumes and reaches the end of its current list, QTimeEvt_tick_() imports QTimeEvt_timeEvtHead_[tickRate].act into the same for (;;) scan (qf_time.c:316-325). A two-tick rearming is immediately decremented once before the next tick arrives; a one-tick rearming immediately expires again.
Its recipient then rearms it again, repeating the sequence within the original tick-processing call.
The same path should apply wherever tick processing runs in a thread that a timeout recipient can preempt (for example, QXK or RTOS ports using QTicker), but only the QK port has been tested here.
#include <stddef.h>
#include "qpc.h"
#ifndef DELAY
#define DELAY 1U
#endif
#ifndef USE_REARM
#define USE_REARM 0
#endif
#ifndef PERIODIC_CONTROL
#define PERIODIC_CONTROL 0
#endif
#ifndef TICKER_PRIO
#define TICKER_PRIO 1U
#endif
#ifndef RECEIVER_PRIO
#define RECEIVER_PRIO 2U
#endif
#define SCB_ICSR (*(volatile uint32_t *)0xE000ED04U)
#define SCB_ICSR_PENDSTSET (1U << 26U)
void print_text(char const *text);
void print_number(unsigned value);
_Noreturn void finish(unsigned status);
enum { TIMEOUT_SIG = Q_USER_SIG };
typedef struct {
QActive super;
QTimeEvt timer;
} Receiver;
static Receiver receiver;
static QTicker ticker;
static QEvtPtr queue[4];
static volatile unsigned triggers;
static unsigned deliveries;
static unsigned failures;
static QState running(Receiver * const me, QEvt const * const e) {
if (e->sig == TIMEOUT_SIG) {
++deliveries;
print_text("timeout: trigger="); print_number(triggers);
print_text(" delivery="); print_number(deliveries); print_text("\n");
if (triggers != 1U + (deliveries - 1U)
* (PERIODIC_CONTROL ? 1U : DELAY)) {
++failures;
}
if (deliveries == 8U) {
print_text("STOP: application observation limit; no QP assertion\n");
finish(1U);
}
#if !PERIODIC_CONTROL
#if USE_REARM
(void)QTimeEvt_rearm(&me->timer, DELAY);
#else
QTimeEvt_armX(&me->timer, DELAY, 0U);
#endif
#endif
return Q_HANDLED();
}
return Q_SUPER(&QHsm_top);
}
static QState initial(Receiver * const me, void const * const par) {
(void)par;
QTimeEvt_armX(&me->timer, 1U, PERIODIC_CONTROL ? 1U : 0U);
return Q_TRAN(&running);
}
void SysTick_Handler(void) {
QK_ISR_ENTRY();
++triggers;
QTICKER_TRIG(&ticker, NULL);
QK_ISR_EXIT();
}
void QF_onStartup(void) {}
void QF_onCleanup(void) {}
void QK_onIdle(void) {
if (triggers == 4U) {
print_text("idle resumed after four triggers; failures=");
print_number(failures); print_text("\n");
finish(failures != 0U);
}
// SysTick's hardware counter is disabled; pend one SysTick exception.
SCB_ICSR = SCB_ICSR_PENDSTSET;
__asm volatile ("dsb\n isb" ::: "memory");
}
Q_NORETURN Q_onError(char const * const module, int_t const id) {
print_text("QP assertion: "); print_text(module); print_text(" ");
print_number((unsigned)id); print_text("\n"); finish(2U);
}
int main(void) {
QF_init();
QTicker_ctor(&ticker, 0U);
QActive_ctor(&receiver.super, Q_STATE_CAST(&initial));
QTimeEvt_ctorX(&receiver.timer, &receiver.super, TIMEOUT_SIG, 0U);
QActive_start(&ticker.super, TICKER_PRIO, NULL, 0U, NULL, 0U, NULL);
QActive_start(&receiver.super, RECEIVER_PRIO,
queue, Q_DIM(queue), NULL, 0U, NULL);
return QF_run();
}
#include <stddef.h>
#include <stdint.h>
extern uint32_t data_load, data_start, data_end, bss_start, bss_end, stack_top;
extern int main(void);
void reset(void);
void unexpected(void);
extern void NMI_Handler(void), PendSV_Handler(void), SysTick_Handler(void);
struct VectorTable {
uint32_t *initial_sp;
void (*exceptions[15])(void);
};
__attribute__((section(".vectors"), used))
struct VectorTable const vectors = {
&stack_top,
{
reset, NMI_Handler, unexpected, unexpected,
unexpected, unexpected, unexpected, unexpected,
unexpected, unexpected, unexpected, unexpected,
unexpected, PendSV_Handler, SysTick_Handler
}
};
static void host_call(unsigned operation, void const *argument) {
register unsigned r0 __asm("r0") = operation;
register void const *r1 __asm("r1") = argument;
__asm volatile ("bkpt 0xab" : "+r"(r0) : "r"(r1) : "memory");
}
void print_text(char const *text) {
host_call(4U, text);
}
void print_number(unsigned value) {
static unsigned const powers[] = {
1000000000U, 100000000U, 10000000U, 1000000U, 100000U,
10000U, 1000U, 100U, 10U, 1U
};
char text[11];
unsigned used = 0U;
for (unsigned i = 0; i < 10U; ++i) {
char digit = '0';
while (value >= powers[i]) {
value -= powers[i];
++digit;
}
if (digit != '0' || used != 0U || i == 9U) {
text[used++] = digit;
}
}
text[used] = 0;
print_text(text);
}
_Noreturn void finish(unsigned status) {
uint32_t const result[2] = { 0x20026U, status };
for (;;) {
host_call(0x20U, result);
}
}
void unexpected(void) {
print_text("unexpected exception\n");
finish(3U);
}
void reset(void) {
uint32_t const *source = &data_load;
for (uint32_t *p = &data_start; p < &data_end; ++p) {
*p = *source++;
}
for (uint32_t *p = &bss_start; p < &bss_end; ++p) {
*p = 0U;
}
(void)main();
finish(3U);
}
void *memset(void *p, int value, size_t count) {
unsigned char *s = p;
while (count--) {
*s++ = (unsigned char)value;
}
return p;
}
void *memcpy(void *p, void const *q, size_t count) {
unsigned char *s = p;
unsigned char const *t = q;
while (count--) {
*s++ = *t++;
}
return p;
}
#ifndef QP_CONFIG_H_
#define QP_CONFIG_H_
#define QP_API_VERSION 9999
#define QF_MAX_ACTIVE 2U
#define QF_MAX_TICK_RATE 1U
#define QF_MAX_EPOOL 1U
#define QF_TIMEEVT_CTR_SIZE 4U
#define QF_EQUEUE_CTR_SIZE 1U
#define QF_MPOOL_CTR_SIZE 2U
#endif
ENTRY(reset)
MEMORY {
flash (rx) : ORIGIN = 0, LENGTH = 256K
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 16K
}
SECTIONS {
.text : {
KEEP(*(.vectors))
*(.text*)
*(.rodata*)
. = ALIGN(4);
} > flash
data_load = LOADADDR(.data);
.data : {
. = ALIGN(4);
data_start = .;
*(.data*)
. = ALIGN(4);
data_end = .;
} > ram AT > flash
.bss : {
. = ALIGN(4);
bss_start = .;
*(.bss*)
*(COMMON)
. = ALIGN(4);
bss_end = .;
} > ram
stack_top = ORIGIN(ram) + LENGTH(ram);
}
#!/bin/sh
set -eu
# Usage: sh run.sh /path/to/upstream-qpc [compiler -D options...]
qp=$(cd "$1" && pwd)
shift
cd "$(dirname "$0")"
arm-none-eabi-gcc -std=c11 -Wpedantic -mcpu=cortex-m0 -mthumb -Os \
-ffreestanding -nostdlib -Wall -Wextra -Werror -I. -I"$qp/include" \
-I"$qp/ports/arm-cm/qk/gnu" "$@" \
repro.c runtime.c "$qp"/src/qf/*.c "$qp/src/qk/qk.c" \
"$qp/ports/arm-cm/qk/gnu/qk_port.c" -T microbit.ld -lgcc -o repro.elf
exec qemu-system-arm -M microbit -nographic \
-semihosting-config enable=on,target=native -kernel repro.elf
#include <stddef.h>
#include "qpc.h"
#ifndef TICKER_PRIO
#define TICKER_PRIO 1U
#endif
#ifndef RECEIVER_PRIO
#define RECEIVER_PRIO 2U
#endif
#ifndef RELOAD
#define RELOAD 9999U
#endif
#define SYST_CSR (*(volatile uint32_t *)0xE000E010U)
#define SYST_RVR (*(volatile uint32_t *)0xE000E014U)
#define SYST_CVR (*(volatile uint32_t *)0xE000E018U)
void print_text(char const *text);
void print_number(unsigned value);
_Noreturn void finish(unsigned status);
enum { TIMEOUT_SIG = Q_USER_SIG };
typedef struct { QActive super; QTimeEvt timer; } Receiver;
static Receiver receiver;
static QTicker ticker;
static QEvtPtr queue[4];
static volatile unsigned triggers;
static volatile unsigned deliveries;
static volatile unsigned idles;
static volatile unsigned idles_at_first_tick;
static void report(void) {
print_text(" triggers="); print_number(triggers);
print_text(" deliveries="); print_number(deliveries);
print_text(" idles="); print_number(idles);
print_text(" idles_at_first_tick="); print_number(idles_at_first_tick);
print_text("\n");
}
static QState running(Receiver * const me, QEvt const * const e) {
if (e->sig == TIMEOUT_SIG) {
++deliveries; // no printing: no semihosting latency
QTimeEvt_armX(&me->timer, 1U, 0U);
return Q_HANDLED();
}
return Q_SUPER(&QHsm_top);
}
static QState initial(Receiver * const me, void const * const par) {
(void)par;
QTimeEvt_armX(&me->timer, 1U, 0U);
return Q_TRAN(&running);
}
void SysTick_Handler(void) {
QK_ISR_ENTRY();
if (triggers == 0U) {
idles_at_first_tick = idles;
}
++triggers;
QTICKER_TRIG(&ticker, NULL);
QK_ISR_EXIT();
}
void QF_onStartup(void) {
SYST_RVR = RELOAD;
SYST_CVR = 0U;
SYST_CSR = 0x7U; // ENABLE | TICKINT | CLKSOURCE=cpu
}
void QF_onCleanup(void) {}
void QK_onIdle(void) {
++idles;
if (idles == 1000000U) {
print_text("idle reached 1000000 times;"); report(); finish(0U);
}
}
Q_NORETURN Q_onError(char const * const module, int_t const id) {
SYST_CSR = 0U;
print_text("QP assertion: "); print_text(module); print_text(" ");
print_number((unsigned)id); report(); finish(2U);
}
int main(void) {
QF_init();
QTicker_ctor(&ticker, 0U);
QActive_ctor(&receiver.super, Q_STATE_CAST(&initial));
QTimeEvt_ctorX(&receiver.timer, &receiver.super, TIMEOUT_SIG, 0U);
QActive_start(&ticker.super, TICKER_PRIO, NULL, 0U, NULL, 0U, NULL);
QActive_start(&receiver.super, RECEIVER_PRIO, queue, Q_DIM(queue),
NULL, 0U, NULL);
return QF_run();
}
#!/bin/sh
set -eu
# Usage: sh run_overflow.sh /path/to/upstream-qpc [compiler -D options...]
qp=$(cd "$1" && pwd)
shift
cd "$(dirname "$0")"
arm-none-eabi-gcc -std=c11 -Wpedantic -mcpu=cortex-m0 -mthumb -Os \
-ffreestanding -nostdlib -Wall -Wextra -Werror -I. -I"$qp/include" \
-I"$qp/ports/arm-cm/qk/gnu" "$@" \
overflow.c runtime.c "$qp"/src/qf/*.c "$qp/src/qk/qk.c" \
"$qp/ports/arm-cm/qk/gnu/qk_port.c" -T microbit.ld -lgcc -o overflow.elf
exec qemu-system-arm -M microbit -nographic \
-semihosting-config enable=on,target=native -kernel overflow.elf
Anonymous