libtorch > 1.9.1 produces segfault on Qt5 gui application exit
Labels: high priority, module: crash, module: cpp, triaged

### 🐛 Describe the bug

We are developing a Qt5 based C++ application which is using the C++ API of libtorch to interact with pytorch accordingly. Since libtorch/pytorch version greater than 1.9.1 we noticed that our application exits with a segmentation fault on normal exit for no apparent reason related to the internals of that particular application.

After some deeper investigation we created a very simple example Qt5 application which can be used to reproduce that issue accordingly. For the sake of reproducibility and debugging we will share this simple Qt5 application here.

Here is the output that simple application `ptest` generates on command-line after creating a simple Qt5 based message box and then having closed it interactively:

```sh
$ ./ptest
 0.4288  0.0194  0.2141
 0.1779  0.1772  0.2024
[ CPUFloatType{2,3} ]
(... GUI opened, continuing after user closes requester ...)
will SegFault afterwards with libtorch > 1.9.1...
Segmentation fault
```

Here is the gdb output on that particular example application `ptest`:

```gdb
Thread 1 "ptest" received signal SIGSEGV, Segmentation fault.
0x00007fffdf42447e in __GI___libc_free (mem=0x1) at ./malloc/malloc.c:3368
3368	./malloc/malloc.c: No such file or directory.
(gdb) bt
#0  0x00007fffdf42447e in __GI___libc_free (mem=0x1) at ./malloc/malloc.c:3368
#1  0x00007fffe60197a7 in llvm::cl::Option::~Option() ()
   from /usr/local/libtorch-2.1.0.dev20230616+cpu/lib/libtorch_cpu.so
#2  0x00007fffdf3c4a56 in __cxa_finalize (d=0x7ffff7679000) at ./stdlib/cxa_finalize.c:83
#3  0x00007fffe182c703 in __do_global_dtors_aux ()
   from /usr/local/libtorch-2.1.0.dev20230616+cpu/lib/libtorch_cpu.so
#4  0x00007fffffffdc20 in ?? ()
#5  0x00007ffff7fc924e in _dl_fini () at ./elf/dl-fini.c:142
Backtrace stopped: frame did not save the PC
```

As one can see, the application seem to run into a segfault right when it is trying to cleanup memory which is/was probably allocated by libtorch during its initialization (`__do_global_dtors_aux`). In addition, we have tested that particular `ptest` app up to the latest 2.1.0.dev20230616 version available for public download from download.pytorch.org. In addition, we also walked down the road until version 1.9.0 and could verify that starting with version 1.11.0 of libtorch/pytorch (1.10.0 does not link correctly due to a bug in that particular version) it started to segfault with the same backtrace pointing to `__do_global_dtors_aux` and `llvm::cl::Option::~Option()` in `libtorch_cpu.so`.

In addition, I also ran valgrind over the test program to check for anything suspicious and found the following references to libtorch:

```
will SegFault afterwards with libtorch > 1.9.1...
==332300== Invalid free() / delete / delete[] / realloc()
==332300==    at 0x484B27F: free (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==332300==    by 0x8A21C0E: llvm::cl::Option::~Option() (in /usr/local/libtorch-1.11.0+cpu/lib/libtorch_cpu.so)
==332300==    by 0x1C96BA55: __cxa_finalize (cxa_finalize.c:83)
==332300==    by 0x5893752: ??? (in /usr/local/libtorch-1.11.0+cpu/lib/libtorch_cpu.so)
==332300==    by 0x400624D: _dl_fini (dl-fini.c:142)
==332300==    by 0x1C96B494: __run_exit_handlers (exit.c:113)
==332300==    by 0x1C96B60F: exit (exit.c:143)
==332300==    by 0x1C94FD96: (below main) (libc_start_call_main.h:74)
==332300==  Address 0x1 is not stack'd, malloc'd or (recently) free'd
==332300== 
==332300== Invalid free() / delete / delete[] / realloc()
==332300==    at 0x484B27F: free (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==332300==    by 0x170F982D: llvm::cl::opt<llvm::FunctionSummary::ForceSummaryHotnessType, true, llvm::cl::parser<llvm::FunctionSummary::ForceSummaryHotnessType> >::~opt() (in /usr/local/libtorch-1.11.0+cpu/lib/libtorch_cpu.so)
==332300==    by 0x1C96BA55: __cxa_finalize (cxa_finalize.c:83)
==332300==    by 0x5893752: ??? (in /usr/local/libtorch-1.11.0+cpu/lib/libtorch_cpu.so)
==332300==    by 0x400624D: _dl_fini (dl-fini.c:142)
==332300==    by 0x1C96B494: __run_exit_handlers (exit.c:113)
==332300==    by 0x1C96B60F: exit (exit.c:143)
==332300==    by 0x1C94FD96: (below main) (libc_start_call_main.h:74)
==332300==  Address 0x800000003 is not stack'd, malloc'd or (recently) free'd
``` 

Please find here the source code of that simple test application (`main.cpp`):

```c++
#include <torch/torch.h>

#include <QApplication>
#include <QMessageBox>

// including torch headers after Qt headers (instead of before) leads
// to compile error
//#include <torch/torch.h>

#include <iostream>

int main(int argc, char *argv[])
{
  // required to link libtorch
  torch::Tensor tensor = torch::rand({2, 3});
  std::cout << tensor << std::endl;

  QApplication app(argc, argv);
  QMessageBox::critical(NULL, "Error", "About to crash!");
  
  // also crashes when used here
  //torch::Tensor tensor = torch::rand({2, 3});
  //std::cout << tensor << std::endl;

  std::cout << "will SegFault afterwards with libtorch > 1.9.1..." << std::endl;

  return 0;
}
```

as well as the corresponding Qt5 project file (`ptest.pro`):

```pro
TEMPLATE = app

QT += widgets

TARGET = ptest
DESTDIR = ./

QMAKE_CXXFLAGS *= -ggdb

#TORCH_VERSION = 1.9.0+cpu # works
#TORCH_VERSION = 1.9.1+cpu # works
#TORCH_VERSION = 1.10.0+cpu # does not link (https://github.com/pytorch/pytorch/issues/72653)
#TORCH_VERSION = 1.10.1+cpu # does not link (https://github.com/pytorch/pytorch/issues/72653)
#TORCH_VERSION = 1.10.2+cpu # does not link (https://github.com/pytorch/pytorch/issues/72653)
TORCH_VERSION = 1.11.0+cpu # crashes
#TORCH_VERSION = 1.12.0+cpu # crashes
#TORCH_VERSION = 1.12.1+cpu # crashes
#TORCH_VERSION = 1.13.0+cpu # crashes
#TORCH_VERSION = 1.13.1+cpu # crashes
#TORCH_VERSION = 2.1.0.dev20230616+cpu # crashes

QMAKE_RPATHDIR *= /usr/local/libtorch-$${TORCH_VERSION}/lib/
QMAKE_CXXFLAGS *= -I/usr/local/libtorch-$${TORCH_VERSION}/include/
QMAKE_CXXFLAGS *= -I/usr/local/libtorch-$${TORCH_VERSION}/include/torch/csrc/api/include/
LIBS *= -L/usr/local/libtorch-$${TORCH_VERSION}/lib -ltorch_cpu -lc10

SOURCES += main.cpp
```

or alternatively the corresponding `CMakeLists.txt` file for cmake-based compilation:
```cmake
cmake_minimum_required(VERSION 3.18 FATAL_ERROR)
project(ptest)

set(CMAKE_AUTOMOC ON)

find_package(Torch REQUIRED)
find_package(Qt5 COMPONENTS Widgets REQUIRED)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(ptest main.cpp)
target_compile_features(ptest PUBLIC cxx_range_for)
target_link_libraries(ptest PRIVATE "${TORCH_LIBRARIES}" Qt5::Widgets)
set_property(TARGET ptest PROPERTY CXX_STANDARD 17)
```

### Versions

```
Collecting environment information...
PyTorch version: 2.0.1+cu117
Is debug build: False
CUDA used to build PyTorch: 11.7
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.2 LTS (x86_64)
GCC version: (Ubuntu 11.3.0-1ubuntu1~22.04.1) 11.3.0
Clang version: 14.0.0-1ubuntu1
CMake version: version 3.26.3
Libc version: glibc-2.35

Python version: 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0] (64-bit runtime)
Python platform: Linux-5.15.107-2-pve-x86_64-with-glibc2.35
Is CUDA available: False
CUDA runtime version: 11.7.99
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: Could not collect
Nvidia driver version: Could not collect
cuDNN version: Probably one of the following:
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn.so.8.6.0
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_adv_infer.so.8.6.0
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_adv_train.so.8.6.0
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_cnn_infer.so.8.6.0
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_cnn_train.so.8.6.0
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_ops_infer.so.8.6.0
/usr/local/cuda-11.7/targets/x86_64-linux/lib/libcudnn_ops_train.so.8.6.0
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:                    x86_64
CPU op-mode(s):                  32-bit, 64-bit
Address sizes:                   46 bits physical, 48 bits virtual
Byte Order:                      Little Endian
CPU(s):                          64
On-line CPU(s) list:             0-59
Off-line CPU(s) list:            60-63
Vendor ID:                       GenuineIntel
Model name:                      Intel(R) Xeon(R) Gold 6130 CPU @ 2.10GHz
CPU family:                      6
Model:                           85
Thread(s) per core:              2
Core(s) per socket:              16
Socket(s):                       2
Stepping:                        4
BogoMIPS:                        4200.00
Flags:                           fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single pti intel_ppin ssbd mba ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts pku ospke md_clear flush_l1d arch_capabilities
Virtualization:                  VT-x
L1d cache:                       1 MiB (32 instances)
L1i cache:                       1 MiB (32 instances)
L2 cache:                        32 MiB (32 instances)
L3 cache:                        44 MiB (2 instances)
NUMA node(s):                    2
NUMA node0 CPU(s):               0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62
NUMA node1 CPU(s):               1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,63
Vulnerability Itlb multihit:     KVM: Mitigation: VMX disabled
Vulnerability L1tf:              Mitigation; PTE Inversion; VMX conditional cache flushes, SMT vulnerable
Vulnerability Mds:               Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Meltdown:          Mitigation; PTI
Vulnerability Mmio stale data:   Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Retbleed:          Mitigation; IBRS
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; IBRS, IBPB conditional, STIBP conditional, RSB filling, PBRSB-eIBRS Not affected
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Mitigation; Clear CPU buffers; SMT vulnerable

Versions of relevant libraries:
[pip3] numpy==1.23.3
[pip3] numpydoc==1.2
[pip3] pytorch-lightning==2.0.2
[pip3] torch==2.0.1
[pip3] torchaudio==2.0.2
[pip3] torchmetrics==0.11.4
[pip3] torchvision==0.15.2
[pip3] triton==2.0.0
```

cc @ezyang @gchanan @zou3519 @jbschlosser