Repository: pytorch
Issue: pytorch is built without _GLIBCXX_USE_CXX11_ABI and can cause std::regex crashes (probably)
Labels: needs reproduction,module: build,triaged

## 🐛 Bug

In a standard Python 3.6 interpreter, the following snippet is fine:

```
import torch
torch.device('cpu')
```

However, if you run that same snippet from a program that has loaded a python interpreter, you will get a crash during regex compilation due to a massive allocation.  I believe https://github.com/pytorch/pytorch/blob/master/c10/core/Device.cpp#L64 is the pytorch line that initiates the crash.

The exact size of the big allocation changes from run to run, implying memory corruption / use of uninit memory.

https://stackoverflow.com/questions/51382355/stdregex-and-dual-abi is the closest I have found to a description of what might be happening - I suspect building pytorch with _GLIBCXX_USE_CXX11_ABI would fix the issue.

## To Reproduce

Steps to reproduce the behavior (probably):

1. Load a python interpreter into a program that has been built with _GLIBCXX_USE_CXX11_ABI (perhaps it has to use std::regex as well, there are many unknowns)
1. Execute `torch.device('cpu')` (after importing torch of course)

```
>>> import torch
>>> torch.device('cpu')
==29690==WARNING: AddressSanitizer failed to allocate 0x7fbadaab30d9 bytes
==29690==AddressSanitizer's allocator is terminating the process instead of returning 0
==29690==If you don't like this behavior set allocator_may_return_null=1
==29690==AddressSanitizer CHECK failed: ../../../../src/libsanitizer/sanitizer_common/sanitizer_allocator.cc:216 "((0)) != (0)" (0x0, 0x0)
    #0 0x7fbae4258522  (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xf9522)
    #1 0x7fbae42750e9 in __sanitizer::CheckFailed(char const*, int, char const*, unsigned long long, unsigned long long) (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x1160e9)
    #2 0x7fbae425e456  (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xff456)
    #3 0x7fbae425e493  (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xff493)
    #4 0x7fbae4187595  (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x28595)
    #5 0x7fbae41861f7  (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x271f7)
    #6 0x7fbae424eb5e in operator new(unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xefb5e)
    #7 0x7fbada7ffc28 in std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator<char> const&) (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0xd3c28)
    #8 0x7fbada7ffff1 in std::string::_M_mutate(unsigned long, unsigned long, unsigned long) (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0xd3ff1)
    #9 0x7fbada8001ea in std::string::_M_leak_hard() (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0xd41ea)
    #10 0x7fbaa615468b in bool std::__detail::_Compiler<std::regex_traits<char> >::_M_expression_term<false, false>(std::pair<bool, char>&, std::__detail::_BracketMatcher<std::regex_traits<char>, false, false>&) (/home/rkopelman/.local/lib/python3.6/site-packages/torch/lib/libc10.so+0x3368b)
    #11 0x7fbaa61581ee in void std::__detail::_Compiler<std::regex_traits<char> >::_M_insert_bracket_matcher<false, false>(bool) (/home/rkopelman/.local/lib/python3.6/site-packages/torch/lib/libc10.so+0x371ee)
    #12 0x7fbaa6159412 in std::__detail::_Compiler<std::regex_traits<char> >::_M_bracket_expression() (/home/rkopelman/.local/lib/python3.6/site-packages/torch/lib/libc10.so+0x38412)
    #13 0x7fbaa6159bb5 in std::__detail::_Compiler<std::regex_traits<char> >::_M_atom() (/home/rkopelman/.local/lib/python3.6/site-packages/torch/lib/libc10.so+0x38bb5)
    #14 0x7fbaa615a0b7 in std::__detail::_Compiler<std::regex_traits<char> >::_M_alternative() (/home/rkopelman/.local/lib/python3.6/site-packages/torch/lib/libc10.so+0x390b7)
    #15 0x7fbaa615a2c8 in std::__detail::_Compiler<std::regex_traits<char> >::_M_disjunction() (/home/rkopelman/.local/lib/python3.6/site-packages/torch/lib/libc10.so+0x392c8)
    #16 0x7fbaa6159da6 in std::__detail::_Compiler<std::regex_traits<char> >::_M_atom() (/home/rkopelman/.local/lib/python3.6/site-packages/torch/lib/libc10.so+0x38da6)
    #17 0x7fbaa615a0b7 in std::__detail::_Compiler<std::regex_traits<char> >::_M_alternative() (/home/rkopelman/.local/lib/python3.6/site-packages/torch/lib/libc10.so+0x390b7)
    #18 0x7fbaa615a2c8 in std::__detail::_Compiler<std::regex_traits<char> >::_M_disjunction() (/home/rkopelman/.local/lib/python3.6/site-packages/torch/lib/libc10.so+0x392c8)
    #19 0x7fbaa615aa0c in std::__detail::_Compiler<std::regex_traits<char> >::_Compiler(char const*, char const*, std::locale const&, std::regex_constants::syntax_option_type) (/home/rkopelman/.local/lib/python3.6/site-packages/torch/lib/libc10.so+0x39a0c)
    #20 0x7fbaa61486ef in c10::Device::Device(std::string const&) (/home/rkopelman/.local/lib/python3.6/site-packages/torch/lib/libc10.so+0x276ef)
    #21 0x7fbaa6a53b29 in torch::PythonArgs::device(int) (/home/rkopelman/.local/lib/python3.6/site-packages/torch/lib/libtorch_python.so+0x28bb29)
    #22 0x7fbaa6c73701 in THPDevice_pynew(_typeobject*, _object*, _object*) (/home/rkopelman/.local/lib/python3.6/site-packages/torch/lib/libtorch_python.so+0x4ab701)
```

## Expected behavior

Doesn't crash.

## Environment

 - PyTorch Version (e.g., 1.0): 1.7.1
 - OS (e.g., Linux): Ubuntu 18.04
 - How you installed PyTorch (`conda`, `pip`, source): pip
 - Build command you used (if compiling from source): n/a
 - Python version: 3.6
 - CUDA/cuDNN version: n/a
 - GPU models and configuration: n/a
 - Any other relevant information:

## Additional context

This is kind of hand-wavy for sure, but I hope this is enough to attempt a build change without a repro (as I don't have a simple one).  I'm happy to test a rebuild.


cc @malfet @seemethere @walterddr