Issue created by Sebastian Huber: 
https://gitlab.rtems.org/rtems/rtos/rtems/-/work_items/5840



`_Once()` issues its acquire fence before the load of the once state:

```c
_Atomic_Fence( ATOMIC_ORDER_ACQUIRE );

if ( RTEMS_PREDICT_FALSE( *once_state != ONCE_STATE_COMPLETE ) ) {
```

An acquire fence orders the loads which precede it against the
accesses which follow it. Here no load of the state precedes it. A
caller which reads `ONCE_STATE_COMPLETE` on the fast path therefore
has no order between this load and its later reads of the data which
the initialization routine wrote. On an SMP system with a weakly
ordered processor, such as Arm, AArch64, RISC-V or PowerPC, the
caller can return from `pthread_once()` and read stale data. The
release fence before the store of `ONCE_STATE_COMPLETE` is correct.

The fast path also reads the state without the mutex, while other
threads write it under the mutex. The state is a plain
`unsigned char`, so these accesses are a data race in the sense of
C11.

Every user of `pthread_once()` and of `call_once()` is affected,
for example the initialization of the password and group database,
of the block device buffer and of the shell. The defect is present in
6 and in `main`.

The repair accesses the state through relaxed atomic operations and
issues the acquire fence after the load which reads
`ONCE_STATE_COMPLETE`. The score gains the type `Atomic_Uchar` with a
load and a store operation for this. Neither operation needs
libatomic on any RTEMS target.

The bug report was created using Claude Code.

-- 
View it on GitLab: https://gitlab.rtems.org/rtems/rtos/rtems/-/work_items/5840
You're receiving this email because of your account on gitlab.rtems.org. 
Unsubscribe from this thread: 
https://gitlab.rtems.org/-/namespace/49/sent_notifications/6-0q8udynsp489o5hzvyiz4uiix-1d/unsubscribe
 | Manage all notifications: https://gitlab.rtems.org/-/profile/notifications | 
Help: https://gitlab.rtems.org/help


_______________________________________________
bugs mailing list
[email protected]
http://lists.rtems.org/mailman/listinfo/bugs

Reply via email to