Page MenuHomePhabricator

[MCContext] Don't use getenv inside class constructor

Authored by igor-laevsky on Jun 17 2016, 7:30 AM.



In our llvm use case we are observing rare crashes inside MCContext constructor. Apparently they were caused by the non thread safe getenv call. This change handles this problem by moving getenv call into initialisation of a static command line option. It's slightly better then explicit locks and less complicated than adding new constructor argument.

This issue was already discussed sometime ago (see but I haven't found any evidence that it was fixed back then.

Event Timeline

igor-laevsky retitled this revision from to [MCContext] Don't use getenv inside class constructor.
igor-laevsky updated this object.
igor-laevsky added reviewers: rnk, enderby, rafael.
igor-laevsky added a subscriber: llvm-commits.
rnk accepted this revision.Jun 17 2016, 7:48 AM
rnk edited edge metadata.


This revision is now accepted and ready to land.Jun 17 2016, 7:48 AM
This revision was automatically updated to reflect the committed changes.

How is calling getenv() in a global static initializer any better? Seems a step backwards.

rnk added a comment.Jun 17 2016, 9:29 AM

Hopefully programs don't start threads during static initialization, so the
getenv call isn't racy with setenv. OTOH, programs that use setenv and
threads are already busted, and we can't really help them.