Repository: pytorch
Issue: Libtorch segfault when used with libqpOASES 
Labels: module: build,triaged

## 🐛 Bug

I'm trying to use pytorch C++ to solve an optimization problem formulated as quadratic program solved with qpOASES, but it seems like some sort of linking error related to BLAS is triggering a segfault. Not sure if this is strictly a libtorch problem (or the bazel linker setup) but it's a bit over my head so any guidance would be greatly appreciated

## To Reproduce

As far as I can tell all that needs to be done to reproduce this is to link against qpOASES and libtorch in the same binary using Bazel. I can try to provide a minimal example if that would be helpful. 

Here's an ASan output for the segfault (you can see the sgemm_ call looks like it's inside libqpOASES):

```
==29495==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x61200020abc0 at pc 0x7f103e407e2d bp 0x7f10273d3f90 sp 0x7f10273d3738
WRITE of size 17179869440 at 0x61200020abc0 thread T5 (control)
    #0 0x7f103e407e2c  (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x66e2c)
    #1 0x7f103da0fd7f in sgemm_ (/home/pedro/control/.bazel-cache/f7ef1140462b94ef7f70149772f48db7/execroot/__main__/bazel-out/k8-dbg-asan/bin/src/../_solib_k8/_U_S_Ssrc_Ssolvers_Squadratic_Uprogramming_SqpOASES_CqpOASES_Ulib___Usrc_Ssolvers_Squadratic_Uprogramming_SqpOASES/libqpOASES.so+0x5ed7f)
    #2 0x7f102fbb8f70 in THFloatBlas_gemm (/home/pedro/control/.bazel-cache/f7ef1140462b94ef7f70149772f48db7/execroot/__main__/bazel-out/k8-dbg-asan/bin/src/../_solib_k8/_U@pytorch_S_S_Cpytorch___Uexternal_Spytorch_Slibtorch_Slib/libtorch.so+0x16d4f70)
    #3 0x7f102f8a098d in THFloatTensor_addmmImpl(c10::TensorImpl*, c10::TensorImpl*, c10::TensorImpl*, c10::TensorImpl*, float, float) [clone .constprop.789] (/home/pedro/control/.bazel-cache/f7ef1140462b94ef7f70149772f48db7/execroot/__main__/bazel-out/k8-dbg-asan/bin/src/../_solib_k8/_U@pytorch_S_S_Cpytorch___Uexternal_Spytorch_Slibtorch_Slib/libtorch.so+0x13bc98d)
    #4 0x7f102f8a0dfe in THFloatTensor_addmm (/home/pedro/control/.bazel-cache/f7ef1140462b94ef7f70149772f48db7/execroot/__main__/bazel-out/k8-dbg-asan/bin/src/../_solib_k8/_U@pytorch_S_S_Cpytorch___Uexternal_Spytorch_Slibtorch_Slib/libtorch.so+0x13bcdfe)
    #5 0x7f102f753775 in at::native::legacy::cpu::_th_mm(at::Tensor const&, at::Tensor const&) (/home/pedro/control/.bazel-cache/f7ef1140462b94ef7f70149772f48db7/execroot/__main__/bazel-out/k8-dbg-asan/bin/src/../_solib_k8/_U@pytorch_S_S_Cpytorch___Uexternal_Spytorch_Slibtorch_Slib/libtorch.so+0x126f775)
    #6 0x7f102f69bc8b in at::CPUType::(anonymous namespace)::mm(at::Tensor const&, at::Tensor const&) (/home/pedro/control/.bazel-cache/f7ef1140462b94ef7f70149772f48db7/execroot/__main__/bazel-out/k8-dbg-asan/bin/src/../_solib_k8/_U@pytorch_S_S_Cpytorch___Uexternal_Spytorch_Slibtorch_Slib/libtorch.so+0x11b7c8b)
    #7 0x7f102f6a0eb3 in c10::detail::wrap_kernel_functor_unboxed_<c10::detail::WrapRuntimeKernelFunctor_<at::Tensor (*)(at::Tensor const&, at::Tensor const&), at::Tensor, c10::guts::typelist::typelist<at::Tensor const&, at::Tensor const&> >, at::Tensor (at::Tensor const&, at::Tensor const&)>::call(c10::OperatorKernel*, at::Tensor const&, at::Tensor const&) (/home/pedro/control/.bazel-cache/f7ef1140462b94ef7f70149772f48db7/execroot/__main__/bazel-out/k8-dbg-asan/bin/src/../_solib_k8/_U@pytorch_S_S_Cpytorch___Uexternal_Spytorch_Slibtorch_Slib/libtorch.so+0x11bceb3)
    #8 0x7f102f13bcd4 in std::result_of<c10::Dispatcher::callUnboxed<at::Tensor, at::Tensor const&, at::Tensor const&>(c10::OperatorHandle const&, at::Tensor const&, at::Tensor const&) const::{lambda(c10::DispatchTable const&)#1} (c10::DispatchTable const&)>::type c10::LeftRight<c10::DispatchTable>::read<c10::Dispatcher::callUnboxed<at::Tensor, at::Tensor const&, at::Tensor const&>(c10::OperatorHandle const&, at::Tensor const&, at::Tensor const&) const::{lambda(c10::DispatchTable const&)#1}>(c10::Dispatcher::callUnboxed<at::Tensor, at::Tensor const&, at::Tensor const&>(c10::OperatorHandle const&, at::Tensor const&, at::Tensor const&) const::{lambda(c10::DispatchTable const&)#1}&&) const (/home/pedro/control/.bazel-cache/f7ef1140462b94ef7f70149772f48db7/execroot/__main__/bazel-out/k8-dbg-asan/bin/src/../_solib_k8/_U@pytorch_S_S_Cpytorch___Uexternal_Spytorch_Slibtorch_Slib/libtorch.so+0xc57cd4)
    #9 0x7f10315eadb8 in torch::autograd::VariableType::(anonymous namespace)::mm(at::Tensor const&, at::Tensor const&) (/home/pedro/control/.bazel-cache/f7ef1140462b94ef7f70149772f48db7/execroot/__main__/bazel-out/k8-dbg-asan/bin/src/../_solib_k8/_U@pytorch_S_S_Cpytorch___Uexternal_Spytorch_Slibtorch_Slib/libtorch.so+0x3106db8)
    #10 0x7f102f6a0eb3 in c10::detail::wrap_kernel_functor_unboxed_<c10::detail::WrapRuntimeKernelFunctor_<at::Tensor (*)(at::Tensor const&, at::Tensor const&), at::Tensor, c10::guts::typelist::typelist<at::Tensor const&, at::Tensor const&> >, at::Tensor (at::Tensor const&, at::Tensor const&)>::call(c10::OperatorKernel*, at::Tensor const&, at::Tensor const&) (/home/pedro/control/.bazel-cache/f7ef1140462b94ef7f70149772f48db7/execroot/__main__/bazel-out/k8-dbg-asan/bin/src/../_solib_k8/_U@pytorch_S_S_Cpytorch___Uexternal_Spytorch_Slibtorch_Slib/libtorch.so+0x11bceb3)
    #11 0x7f102f13bcd4 in std::result_of<c10::Dispatcher::callUnboxed<at::Tensor, at::Tensor const&, at::Tensor const&>(c10::OperatorHandle const&, at::Tensor const&, at::Tensor const&) const::{lambda(c10::DispatchTable const&)#1} (c10::DispatchTable const&)>::type c10::LeftRight<c10::DispatchTable>::read<c10::Dispatcher::callUnboxed<at::Tensor, at::Tensor const&, at::Tensor const&>(c10::OperatorHandle const&, at::Tensor const&, at::Tensor const&) const::{lambda(c10::DispatchTable const&)#1}>(c10::Dispatcher::callUnboxed<at::Tensor, at::Tensor const&, at::Tensor const&>(c10::OperatorHandle const&, at::Tensor const&, at::Tensor const&) const::{lambda(c10::DispatchTable const&)#1}&&) const (/home/pedro/control/.bazel-cache/f7ef1140462b94ef7f70149772f48db7/execroot/__main__/bazel-out/k8-dbg-asan/bin/src/../_solib_k8/_U@pytorch_S_S_Cpytorch___Uexternal_Spytorch_Slibtorch_Slib/libtorch.so+0xc57cd4)
    #12 0x7f102f21535e in at::Tensor::mm(at::Tensor const&) const (/home/pedro/control/.bazel-cache/f7ef1140462b94ef7f70149772f48db7/execroot/__main__/bazel-out/k8-dbg-asan/bin/src/../_solib_k8/_U@pytorch_S_S_Cpytorch___Uexternal_Spytorch_Slibtorch_Slib/libtorch.so+0xd3135e)
    #13 0x7f102f201043 in at::native::matmul(c10::optional<at::Tensor>, at::Tensor const&, at::Tensor const&) (/home/pedro/control/.bazel-cache/f7ef1140462b94ef7f70149772f48db7/execroot/__main__/bazel-out/k8-dbg-asan/bin/src/../_solib_k8/_U@pytorch_S_S_Cpytorch___Uexternal_Spytorch_Slibtorch_Slib/libtorch.so+0xd1d043)
    #14 0x7f102f2026e9 in at::native::matmul(at::Tensor const&, at::Tensor const&) (/home/pedro/control/.bazel-cache/f7ef1140462b94ef7f70149772f48db7/execroot/__main__/bazel-out/k8-dbg-asan/bin/src/../_solib_k8/_U@pytorch_S_S_Cpytorch___Uexternal_Spytorch_Slibtorch_Slib/libtorch.so+0xd1e6e9)
    #15 0x7f102f777db4 in at::TypeDefault::matmul(at::Tensor const&, at::Tensor const&) (/home/pedro/control/.bazel-cache/f7ef1140462b94ef7f70149772f48db7/execroot/__main__/bazel-out/k8-dbg-asan/bin/src/../_solib_k8/_U@pytorch_S_S_Cpytorch___Uexternal_Spytorch_Slibtorch_Slib/libtorch.so+0x1293db4)
    #16 0x7f103132667d in torch::autograd::VariableType::(anonymous namespace)::matmul(at::Tensor const&, at::Tensor const&) (/home/pedro/control/.bazel-cache/f7ef1140462b94ef7f70149772f48db7/execroot/__main__/bazel-out/k8-dbg-asan/bin/src/../_solib_k8/_U@pytorch_S_S_Cpytorch___Uexternal_Spytorch_Slibtorch_Slib/libtorch.so+0x2e4267d)
    #17 0x7f102f6a0eb3 in c10::detail::wrap_kernel_functor_unboxed_<c10::detail::WrapRuntimeKernelFunctor_<at::Tensor (*)(at::Tensor const&, at::Tensor const&), at::Tensor, c10::guts::typelist::typelist<at::Tensor const&, at::Tensor const&> >, at::Tensor (at::Tensor const&, at::Tensor const&)>::call(c10::OperatorKernel*, at::Tensor const&, at::Tensor const&) (/home/pedro/control/.bazel-cache/f7ef1140462b94ef7f70149772f48db7/execroot/__main__/bazel-out/k8-dbg-asan/bin/src/../_solib_k8/_U@pytorch_S_S_Cpytorch___Uexternal_Spytorch_Slibtorch_Slib/libtorch.so+0x11bceb3)
    #18 0x7f1031c053ce in torch::nn::functional::linear(at::Tensor const&, at::Tensor const&, at::Tensor const&) (/home/pedro/control/.bazel-cache/f7ef1140462b94ef7f70149772f48db7/execroot/__main__/bazel-out/k8-dbg-asan/bin/src/../_solib_k8/_U@pytorch_S_S_Cpytorch___Uexternal_Spytorch_Slibtorch_Slib/libtorch.so+0x37213ce)
    #19 0x7f1031c048fd in torch::nn::LinearImpl::forward(at::Tensor const&) (/home/pedro/control/.bazel-cache/f7ef1140462b94ef7f70149772f48db7/execroot/__main__/bazel-out/k8-dbg-asan/bin/src/../_solib_k8/_U@pytorch_S_S_Cpytorch___Uexternal_Spytorch_Slibtorch_Slib/libtorch.so+0x37208fd)
...
    #29 0x5597db8a8efa in _M_invoke<0> /usr/include/c++/9/thread:244
    #30 0x5597db8a8efa in operator() /usr/include/c++/9/thread:251
    #31 0x5597db8a8efa in _M_run /usr/include/c++/9/thread:195
    #32 0x7f103e09186f  (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0xd086f)
    #33 0x7f102e0c86da in start_thread /build/glibc-OTsEL5/glibc-2.27/nptl/pthread_create.c:463
    #34 0x7f102dbd988e in __clone (/lib/x86_64-linux-gnu/libc.so.6+0x12188e)

0x61200020abc0 is located 0 bytes to the right of 256-byte region [0x61200020aac0,0x61200020abc0)
allocated by thread T5 (control) here:
    #0 0x7f103e4ade0d in posix_memalign (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x10ce0d)
    #1 0x7f103d53cd57 in c10::alloc_cpu(unsigned long) (/home/pedro/control/.bazel-cache/f7ef1140462b94ef7f70149772f48db7/execroot/__main__/bazel-out/k8-dbg-asan/bin/src/../_solib_k8/_U@pytorch_S_S_Cpytorch___Uexternal_Spytorch_Slibtorch_Slib/libc10.so+0x10d57)

Thread T5 (control) created by T0 (ar-control) here:
    #0 0x7f103e3dae71 in __interceptor_pthread_create (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x39e71)
    #1 0x7f103e091b04 in std::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)()) (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0xd0b04)
...
    #4 0x5597d8f8fa65 in main src/main.cpp:133
    #5 0x7f102dad9b96 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)

SUMMARY: AddressSanitizer: heap-buffer-overflow (/usr/lib/x86_64-linux-gnu/libasan.so.5+0x66e2c) 
Shadow bytes around the buggy address:
  0x0c2480039520: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00
  0x0c2480039530: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2480039540: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c2480039550: fa fa fa fa fa fa fa fa 00 00 00 00 00 00 00 00
  0x0c2480039560: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c2480039570: 00 00 00 00 00 00 00 00[fa]fa fa fa fa fa fa fa
  0x0c2480039580: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c2480039590: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c24800395a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c24800395b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c24800395c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
  Shadow gap:              cc
==29495==ABORTING
```

## Environment

 - PyTorch Version (e.g., 1.0): 1.4.0, cpu-only, cxx11 abi
 - OS (e.g., Linux): Ubuntu 18.04
 - How you installed PyTorch (`conda`, `pip`, source): CPU only release binary
 - Build command you used (if compiling from source): n/a
 - Python version: n/a 
 - CUDA/cuDNN version: none
 - GPU models and configuration: none