[Openmp-dev] The OpenMP runtime crashes multi-threaded programs which call getenv(...)

Johannes Doerfert via Openmp-dev openmp-dev at lists.llvm.org
Mon Sep 21 10:19:33 PDT 2020


Hi Joel Daniels,

thanks for reporting this. I put it on our agenda for the next weekly 
OpenMP in LLVM call, Wednesday 9am CDT:
https://docs.google.com/document/d/1Tz8WFN13n7yJ-SCE0Qjqf9LmjGUw0dWO9Ts1ss4YOdg/edit?usp=sharing

Feel free to join if you are interested.

Cheers,
  Johannes



On 9/19/20 6:25 PM, Daniels, Joel via Openmp-dev wrote:
> Hi OpenMP Dev Team,
>
> On startup, the OpenMP runtime currently calls the libc function setenv. At that point the call stack looks like:
>
>     setenv: glibc
>     __kmp_env_set: https://github.com/llvm/llvm-project/blob/57b08b0944046a6a57ee9b7b479181f548a5b9b4/openmp/runtime/src/kmp_environment.cpp#L176
>     __kmp_register_library_startup: https://github.com/llvm/llvm-project/blob/cde8f4c164a27670ebe60a1969d486393336d778/openmp/runtime/src/kmp_runtime.cpp#L6383
>
> Calling setenv from one thread while another thread calls getenv will cause a crash. The GLIBC manual ( https://www.gnu.org/software/libc/manual/html_node/Environment-Access.html#Environment-Access ) says:
>
>     "Modifications of environment variables are not allowed in multi-threaded programs."
>
> Crashes related to setenv and getenv have been empirically observed by others, see for example: https://rachelbythebay.com/w/2017/01/30/env/ .
>
> We have a multithreaded program which loads (via dlopen) a library which uses Intel's MKL library which in turn depends on the OpenMP runtime.  We have observed random crashes in threads calling "getenv" which occur at the same time that another thread is initializing the OpenMP runtime.
>
> We are currently looking at hacks (like LD_PRELOAD of a library that replaces getenv and setenv with threadsafe ones) to get around this but I thought we should flag the issue upstream to you guys as well.
>
> OpenMP seems to be attempting to use environment variables as a method of identifying when multiple copies of the runtime have been loaded. I'm not sure under what scenarios this happens, but if such a guard is needed then I would hope this could be handled via a global mutex protected static symbol instead?
>
> Thanks!
> Joel Daniels
>
>
> --------------------------------------------------------------------------------
> NOTICE: Morgan Stanley is not acting as a municipal advisor and the opinions or views contained herein are not intended to be, and do not constitute, advice within the meaning of Section 975 of the Dodd-Frank Wall Street Reform and Consumer Protection Act. If you have received this communication in error, please destroy all electronic and paper copies and notify the sender immediately. Mistransmission is not intended to waive confidentiality or privilege. Morgan Stanley reserves the right, to the extent permitted under applicable law, to monitor electronic communications. This message is subject to terms available at the following link: http://www.morganstanley.com/disclaimers  If you cannot access these links, please notify us by reply message and we will send the contents to you. By communicating with Morgan Stanley you consent to the foregoing and to the voice recording of conversations with personnel of Morgan Stanley.
>
> You may have certain rights regarding the information that Morgan Stanley collects about you.  Please see our Privacy Pledge https://www.morganstanley.com/privacy-pledge for more information about your rights.
> _______________________________________________
> Openmp-dev mailing list
> Openmp-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/openmp-dev


More information about the Openmp-dev mailing list