Skip to content
Snippets Groups Projects
  • Tomasz Grabiec's avatar
    3f053b8f
    Fix lack of locking in libc in file and memstream related operations · 3f053b8f
    Tomasz Grabiec authored
    
    Spotted by Nadav: libc.threaded field is not set but is used in
    several 'if' statements when setting the lock_owner field.
    
    When 'libc.threaded' is false then 'lock_owner' of a FILE is set to a
    special value which indicates no locking. This field is initially set
    to 0 and the original musl code had a logic which upon creation of the
    first thread set it to true and adjusted 'lock_owner' field of all
    open files to the value of libc.main_thread.  In OSv we had no such
    logic which resulted in no locking of the FILE structure.
    
    This patch fixes the issue by using threaded mode from the very
    beginning. We also do not rely anymore on posix thread existence so
    that stdlib can be used very early in the boot process without
    unexpected behavior. It is used (rightfully or not) for example in
    ramdisk_init(). We do not have to hold the pthread id in the
    'lock_owner' field because the mutex already tracks the owner and we
    can do the check using 'mutex_owned()' function.
    
    This patch also gets rid of a magic value STDIO_SINGLETHREADED, which
    is of type pthread_t and was used to disable locking when it was known
    to be not necessary. A new field is introduced named 'no_locking'
    which serves this purpose.
    
    Signed-off-by: default avatarTomasz Grabiec <tgrabiec@cloudius-systems.com>
    Signed-off-by: default avatarPekka Enberg <penberg@cloudius-systems.com>
    3f053b8f
    History
    Fix lack of locking in libc in file and memstream related operations
    Tomasz Grabiec authored
    
    Spotted by Nadav: libc.threaded field is not set but is used in
    several 'if' statements when setting the lock_owner field.
    
    When 'libc.threaded' is false then 'lock_owner' of a FILE is set to a
    special value which indicates no locking. This field is initially set
    to 0 and the original musl code had a logic which upon creation of the
    first thread set it to true and adjusted 'lock_owner' field of all
    open files to the value of libc.main_thread.  In OSv we had no such
    logic which resulted in no locking of the FILE structure.
    
    This patch fixes the issue by using threaded mode from the very
    beginning. We also do not rely anymore on posix thread existence so
    that stdlib can be used very early in the boot process without
    unexpected behavior. It is used (rightfully or not) for example in
    ramdisk_init(). We do not have to hold the pthread id in the
    'lock_owner' field because the mutex already tracks the owner and we
    can do the check using 'mutex_owned()' function.
    
    This patch also gets rid of a magic value STDIO_SINGLETHREADED, which
    is of type pthread_t and was used to disable locking when it was known
    to be not necessary. A new field is introduced named 'no_locking'
    which serves this purpose.
    
    Signed-off-by: default avatarTomasz Grabiec <tgrabiec@cloudius-systems.com>
    Signed-off-by: default avatarPekka Enberg <penberg@cloudius-systems.com>