The C++ Standard, [stmt.dcl], paragraph 4 [ISO/IEC 14882-2014], states the following:
The zero-initialization (8.5) of all block-scope variables with static storage duration (3.7.1) or thread storage duration (3.7.2) is performed before any other initialization takes place. Constant initialization (3.6.2) of a block-scope entity with static storage duration, if applicable, is performed before its block is first entered. An implementation is permitted to perform early initialization of other block-scope variables with static or thread storage duration under the same conditions that an implementation is permitted to statically initialize a variable with static or thread storage duration in namespace scope (3.6.2). Otherwise such a variable is initialized the first time control passes through its declaration; such a variable is considered initialized upon the completion of its initialization. If the initialization exits by throwing an exception, the initialization is not complete, so it will be tried again the next time control enters the declaration. If control enters the declaration concurrently while the variable is being initialized, the concurrent execution shall wait for completion of the initialization. If control re-enters the declaration recursively while the variable is being initialized, the behavior is undefined.
Do not reenter a function during the initialization of a static variable declaration. If a function is reentered during the constant initialization of a static object inside that function, the behavior of the program is undefined. Infinite recursion is not required to trigger undefined behavior, the function need only recur once as part of the initialization. Due to thread-safe initialization of variables, a single, recursive call will often result in a deadlock due to locking a non-recursive synchronization primitive.
Additionally, the C++ Standard, [basic.start.init], paragraph 2, in part, states the following:
Dynamic initialization of a non-local variable with static storage duration is either ordered or unordered. Definitions of explicitly specialized class template static data members have ordered initialization. Other class template static data members (i.e., implicitly or explicitly instantiated specializations) have unordered initialization. Other non-local variables with static storage duration have ordered initialization. Variables with ordered initialization defined within a single translation unit shall be initialized in the order of their definitions in the translation unit. If a program starts a thread, the subsequent initialization of a variable is unsequenced with respect to the initialization of a variable defined in a different translation unit. Otherwise, the initialization of a variable is indeterminately sequenced with respect to the initialization of a variable defined in a different translation unit. If a program starts a thread, the subsequent unordered initialization of a variable is unsequenced with respect to every other dynamic initialization. Otherwise, the unordered initialization of a variable is indeterminately sequenced with respect to every other dynamic initialization.
Do not create an initialization interdependency between static objects with dynamic initialization unless they are ordered with respect to one another. Unordered initialization, especially prevalent across translation unit boundaries, results in unspecified behavior.
Noncompliant Code Example
This noncompliant example attempts to implement an efficient factorial function using caching. Because the initialization of the static local array
cache involves recursion, the behavior of the function is undefined, even though the recursion is not infinite.
This compliant solution avoids initializing the static local array
cache and instead relies on zero-initialization to determine whether each member of the array has been assigned a value yet and, if not, recursively computes its value. It then returns the cached value when possible or computes the value as needed.
Noncompliant Code Example
In this noncompliant code example, the value of
file1.cpp relies on
c being initialized. However, because
c is defined in a different translation unit (
numWheels, there is no guarantee that
c will be initialized by calling
numWheels is initialized by calling
c.get_num_wheels(). This is often referred to as the "static initialization order fiasco," and the resulting behavior is unspecified.
The value printed to the standard output stream will often rely on the order in which the translation units are linked. For instance, with Clang 3.8.0 on x86 Linux, the command
clang++ file1.cpp file2.cpp && ./a.out will write
clang++ file2.cpp file1.cpp && ./a.out will write
This compliant solution uses the "construct on first use" idiom to resolve the static initialization order issue. The code for
file2.cpp are unchanged; only the static
file1.cpp is moved into the body of a function. Consequently, the initialization of
numWheels is guaranteed to happen when control flows over the point of declaration, ensuring control over the order. The global object
c is initialized before execution of
main() begins, so by the time
get_num_wheels() is called,
c is guaranteed to have already been dynamically initialized.
Recursively reentering a function during the initialization of one of its static objects can result in an attacker being able to cause a crash or denial of service. Indeterminately ordered dynamic initialization can lead to undefined behavior due to accessing an uninitialized object.
|LDRA tool suite|
Avoid initialization order problems across translation units by replacing non-local static objects with local static objects
Subclause 3.6.2, "Initialization of Non-local Variables"