Repository: pytorch
Issue: Segfault when running vulkan program linked against libtorch
Labels: triaged,module: vulkan,ciflow/periodic

### 🐛 Describe the bug

Creating a `VulkanInstance` in a program linked to `libtorch` gives a segmentation fault at program exit.

Here is minimal C++ source code to reproduce.

```cpp
#include <vulkan/vulkan.h>

int main()
{
    const uint32_t validationLayerCount = 1;
    const char * validationLayers[] = {
        "VK_LAYER_KHRONOS_validation"
    };
    VkApplicationInfo appInfo = {
        .sType = VK_STRUCTURE_TYPE_APPLICATION_INFO,
        .apiVersion = VK_API_VERSION_1_0
    };
    VkInstanceCreateInfo instanceCreateInfo = {
        .sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
        .pApplicationInfo = &appInfo,
        .enabledLayerCount = validationLayerCount,
        .ppEnabledLayerNames = validationLayers
    };
    VkInstance instance;
    if (vkCreateInstance(&instanceCreateInfo, NULL, &instance) != VK_SUCCESS) {
        return 1;
    }
    vkDestroyInstance(instance, NULL);
}
```

and CMakeList.txt

```cmake
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)

project(main)

find_package(Torch REQUIRED)
find_package(Vulkan REQUIRED)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")

add_executable(main main.cpp)

target_link_libraries(main ${TORCH_LIBRARIES} ${Vulkan_LIBRARIES})
set_property(TARGET main PROPERTY CXX_STANDARD 14)
```

I compile the program with `cmake -DCMAKE_BUILD_TYPE=Debug -S . -B build -DCMAKE_PREFIX_PATH=${LIBTORCH_PATH} && cmake --build build` where `LIBTORCH_PATH` is the path to libtorch downloaded from `https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-2.0.1%2Bcpu.zip` (CXX-11 ABI for CPU only).

I execute the program by running `gdb build/main`. The program quickly segfaults, see stacktrace below (`bt` in gdb session)

```
#0  0x00007fffe02a547e in __GI___libc_free (mem=0x1) at ./malloc/malloc.c:3368
#1  0x00007fffe5f02cbf in llvm::cl::Option::~Option() () from /home/cdeln/lib/libtorch-cpu/lib/libtorch_cpu.so
#2  0x00007fffe0245a56 in __cxa_finalize (d=0x7ffff74ca000) at ./stdlib/cxa_finalize.c:83
#3  0x00007fffe18eb433 in __do_global_dtors_aux () from /home/cdeln/lib/libtorch-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
```

The issue is more or less identical to [this libtorch + sdl bug](https://github.com/pytorch/pytorch/issues/71283).

### Versions

Here is output from `collect_env.py`
```
Collecting environment information...
PyTorch version: 2.0.0+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: Could not collect
CMake version: version 3.26.3
Libc version: glibc-2.35

Python version: 3.10.6 (main, Apr 26 2023, 09:09:46) [GCC 11.3.0] (64-bit runtime)
Python platform: Linux-5.19.0-42-generic-x86_64-with-glibc2.35
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
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:                   39 bits physical, 48 bits virtual
Byte Order:                      Little Endian
CPU(s):                          12
On-line CPU(s) list:             0-11
Vendor ID:                       GenuineIntel
Model name:                      12th Gen Intel(R) Core(TM) i5-1235U
CPU family:                      6
Model:                           154
Thread(s) per core:              2
Core(s) per socket:              10
Socket(s):                       1
Stepping:                        4
CPU max MHz:                     4400.0000
CPU min MHz:                     400.0000
BogoMIPS:                        4992.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 tsc_known_freq pni pclmulqdq dtes64 monitor ds_cpl smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid rdseed adx smap clflushopt clwb intel_pt sha_ni xsaveopt xsavec xgetbv1 xsaves split_lock_detect avx_vnni dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp hwp_pkg_req hfi umip pku ospke waitpkg gfni vaes vpclmulqdq rdpid movdiri movdir64b fsrm md_clear serialize arch_lbr ibt flush_l1d arch_capabilities
L1d cache:                       352 KiB (10 instances)
L1i cache:                       576 KiB (10 instances)
L2 cache:                        6.5 MiB (4 instances)
L3 cache:                        12 MiB (1 instance)
NUMA node(s):                    1
NUMA node0 CPU(s):               0-11
Vulnerability Itlb multihit:     Not affected
Vulnerability L1tf:              Not affected
Vulnerability Mds:               Not affected
Vulnerability Meltdown:          Not affected
Vulnerability Mmio stale data:   Not affected
Vulnerability Retbleed:          Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:        Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:        Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds:             Not affected
Vulnerability Tsx async abort:   Not affected

Versions of relevant libraries:
[pip3] fft-conv-pytorch==1.1.3
[pip3] numpy==1.24.3
[pip3] pytorch3d==0.7.4
[pip3] torch==2.0.0
[pip3] torchvision==0.15.1
[pip3] triton==2.0.0
[conda] Could not collect
```