[cfe-dev] D12689: [libc++][static linking] std streams are not initialized prior to their use in static object constructors

Evgeny Astigeevich via cfe-dev cfe-dev at lists.llvm.org
Thu Sep 10 05:36:15 PDT 2015


Ping.
Could anyone review the patch?

Kind regards,
Evgeny Astigeevich

-----Original Message-----
From: Evgeny Astigeevich [mailto:evgeny.astigeevich at arm.com]
Sent: 08 September 2015 11:00
To: Evgeny Astigeevich
Cc: cfe-commits at lists.llvm.org
Subject: [PATCH] D12689: [libc++][static linking] std streams are not initialized prior to their use in static object constructors

eastig created this revision.
eastig added a subscriber: cfe-commits.

When an executable (e.g. for bare metal environment) is statically linked  with libc++ the following code might not work as expected:

```
#include <iostream>

class Foo {
public:
  Foo(int n) {
    std::cout << "Hello World - " << n << std::endl;
  }
};

Foo f(5);

int main() {
  return 0;
}
```
The program hangs or crashes because 'std::cout' is not initialized before 'Foo f(5)'.

The standard says:

> 27.3 Standard iostream objects
> Header <iostream> synopsis
> (2) ... The objects are constructed, and the associations are
> established at some time prior to or during first time an object of
> class basic_ios<charT,traits>::Init is constructed, and in any case before the body of main begins execution [264]. The objects are not destroyed during program execution [265].
>

And footnote 265 says:

> Constructors and destructors for static objects can access these
> objects to read input from stdin or write output to stdout or stderr.

The similar issue was raised more than year ago. A patch was proposed but not committed. See discussion of it here:

[[ http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20130304/075363.html | initial patch ]] [[ http://lists.cs.uiuc.edu/pipermail/cfe-commits/Week-of-Mon-20130325/077130.html | review, suggestion for #ifdef ]]

The proposed patch caused initialization/deinitialization of the std streams as many times as the static Init objects were created.
The current patch fixes this issue. A number of uses is counted by means of __shared_count. It is used instead of a 'int' variable because it is thread-safe. The standard allows multi-threaded initialization of static objects from different compilation modules.



http://reviews.llvm.org/D12689

Files:
  include/ios
  src/iostream.cpp


-- IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium.  Thank you.

ARM Limited, Registered office 110 Fulbourn Road, Cambridge CB1 9NJ, Registered in England & Wales, Company No:  2557590
ARM Holdings plc, Registered office 110 Fulbourn Road, Cambridge CB1 9NJ, Registered in England & Wales, Company No:  2548782


More information about the cfe-dev mailing list