Repository: pytorch
Issue: Weak Symbols Resolution Causes Segmentation Fault in External Libraries
Labels: triaged

## 🐛 Bug

PyTorch causes segfault in internals of PyArrow during reading a parquet file.

## To Reproduce

In order to reproduce, run the code below.
```python
#   filename: script.py

import torch as T
import pyarrow.parquet

tbl = pyarrow.parquet.read_table(tablename)
```
During execution of the code above the segmentaion fault occurs and core is dumped. In fact, register `rax` points to a wrong place in instruction `lock xadd DWORD PTR [rax],edx`. The related stacktrace is below.
```
(gdb) bt
#0    0x00007f551323a9ce in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() + 0x1e ()
                         at .env/lib/python3.7/site-packages/pyarrow/./libarrow.so.13
#1    0x00007f5513258b85 in std::vector<std::shared_ptr<arrow::Buffer>, std::allocator<std::shared_ptr<arrow::Buffer> > >::~vector() + 0x35 ()
                         at .env/lib/python3.7/site-packages/pyarrow/./libarrow.so.13
#2    0x00007f5513258fc5 in std::_Sp_counted_ptr_inplace<arrow::ArrayData, std::allocator<arrow::ArrayData>, (__gnu_cxx::_Lock_policy)2>::_M_dispose() + 0x55 ()
                         at .env/lib/python3.7/site-packages/pyarrow/./libarrow.so.13
#3    0x00007f551323a9e9 in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() + 0x39 ()
                         at .env/lib/python3.7/site-packages/pyarrow/./libarrow.so.13
#4    0x00007f551323a9e9 in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() + 0x39 ()
                         at .env/lib/python3.7/site-packages/pyarrow/./libarrow.so.13
#5    0x00007f55132d1855 in std::_Sp_counted_ptr_inplace<arrow::ChunkedArray, std::allocator<arrow::ChunkedArray>, (__gnu_cxx::_Lock_policy)2>::_M_dispose() + 0x45 ()
                         at .env/lib/python3.7/site-packages/pyarrow/./libarrow.so.13
#6    0x00007f5513ba9589 in std::_Sp_counted_ptr_inplace<arrow::Column, std::allocator<arrow::Column>, (__gnu_cxx::_Lock_policy)2>::_M_dispose() + 0xc9 ()
                         at .env/lib/python3.7/site-packages/pyarrow/lib.cpython-37m-x86_64-linux-gnu.so
#7    0x00007f551323a9e9 in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release() + 0x39 ()
                         at .env/lib/python3.7/site-packages/pyarrow/./libarrow.so.13
#8    0x00007f55132d18d5 in arrow::SimpleTable::~SimpleTable() + 0x45 ()
                         at .env/lib/python3.7/site-packages/pyarrow/./libarrow.so.13
#9    0x00007f5513ac706a in __pyx_tp_dealloc_7pyarrow_3lib_Table(_object*) + 0xda ()
                         at .env/lib/python3.7/site-packages/pyarrow/lib.cpython-37m-x86_64-linux-gnu.so
#10   0x00007f554082ecb8 in No symbol matches 0x00007f554082ecb8. () at /usr/lib/libpython3.7m.so.1.0
```

## Expected behavior

First of all, no segfault should happen.  Secondly, PyTorch shared libraries need better isolation of exported symbols. I think that this issue is related to PyTorch not PyArrow since PyTorch exports some weak symbols with defaults which replace symbols defined in `libstdc++.so` as showen in the section _Additional Context_.

## Environment

```bash
pip3 install pyarrow
pip3 install https://download.pytorch.org/whl/cpu/torch-1.1.0-cp37-cp37m-linux_x86_64.whl
```

 - PyTorch Version (e.g., 1.0): `1.1.0`
 - OS (e.g., Linux): `5.1.15-arch1-1-ARCH`
 - How you installed PyTorch (`conda`, `pip`, source): `pip`
 - Build command you used (if compiling from source): `none`
 - Python version: `3.7.3`
 - CUDA/cuDNN version: `none`
 - GPU models and configuration: `none`

## Additional context

Preloading of `libstdc++.so.6` solves the issue.
```bash
$ LD_PRELOAD=/usr/lib/libstdc++.so.6.0.26 python script.py
```
So it seems obvious that the issue is related to relocation of weak symbols. I have tried to figure out what symbols are resolved in a wrong way.
```bash
$ LD_DEBUG=bindings python script.py 2>&1 | grep pyarrow | grep torch | c++filt | sed 's/.*`//' | sed "s/'.*//" | sort | uniq
...
std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_add_ref_copy()
std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_destroy()
std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release()
std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_weak_release()
typeinfo for std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>
typeinfo name for std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>
vtable for std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>
...
```
One can see that ref counter is resolved to PyTorch libraries. The complete list of [symbols](https://github.com/pytorch/pytorch/files/3349946/symbols.txt) is attached. I hope that it will be helpful.