This is the mail archive of the newlib@sourceware.org mailing list for the newlib project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Race condition in initialisation of stdio standard streams


Hi,

I believe there is a race condition in the initialisation and use of
recursive locks used in the _stdin, _stdout, and _stderr files. In
__sinit() in findfp.c, the __sdidinit field is set to 1 before the actual
initialisation is done. Other threads could read this field (e.g. by using
the CHECK_INIT() macro in local.h) and proceed to access one of the
standard files before it is initialised. This can result in unwanted
behaviour due to calling _flockfile() before the lock is setup.

Such behaviour can be reproduced by simultaneously calling a function that
will result in initialisation of a standard file in multiple threads (on a
SMP system). For example if _stdout is not yet initialised and printf() is
called on multiple threads simultaneously, sometimes only one thread will
print output.

This behaviour can be fixed by moving the setting of __sdidinit in
__sinit() to after the standard files are initialised, putting it
immediately before the call to __sinit_lock_release(). Preferably there
should be a memory barrier before setting __sdidinit also.

Regards,
Seán Harte


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]