Libtorch Segmentation Fault when running torch::jit::load [BUG]

Jackson_Scott: <p>Potential Bug, I am desperate for some help with this.</p>
<aside class="onebox githubissue">
  <header class="source">

      <a href="https://github.com/pytorch/pytorch/issues/49460" target="_blank" rel="noopener nofollow ugc">github.com/pytorch/pytorch</a>
  </header>

  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/issues/49460" target="_blank" rel="noopener nofollow ugc">Libtorch: Segmentation fault when running torch::jit::load</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2020-12-16" data-time="15:17:05" data-timezone="UTC">03:17PM - 16 Dec 20 UTC</span>
      </div>


      <div class="user">
        <a href="https://github.com/jarledo" target="_blank" rel="noopener nofollow ugc">
          <img alt="jarledo" src="https://avatars.githubusercontent.com/u/57762990?v=4" class="onebox-avatar-inline" width="20" height="20">
          jarledo
        </a>
      </div>
    </div>

    <div class="labels">
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">oncall: jit</span>
        <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">weeks</span>
    </div>
  </div>
</div>

  <div class="github-row">
    <p class="github-body-container">## 🐛 Bug

When trying to load some of our models exported from Python using `t<span class="show-more-container"><a href="" rel="noopener" class="show-more">…</a></span><span class="excerpt hidden">orch.jit.trace` in a C++ program using `torch::jit::load("/path/to/model")` it causes a segmentation fault. The segfault seems to happen in `torch::jit::SourceImporterImpl::attributeAssignmentSpecialHandlingHack`

## To Reproduce

I'm unable to generate a minimal example that causes the segmentation fault. It only happens with some of our models when linked to our custom application. The unit tests that load the models work just fine, so perhaps there is some access to memory location that has not been allocated that only triggers during very specific linking with our application?

A stack trace from gdb:
```
Thread 1 "myprogram" received signal SIGSEGV, Segmentation fault.
0x0000555555742bf9 in std::enable_if&lt;std::__detail::__is_contiguous_iter&lt;__gnu_cxx::__normal_iterator&lt;char const*, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt;::value, std::__cxx11::sub_match&lt;__gnu_cxx::__normal_iterator&lt;char const*, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt;::__string_view&gt;::type std::__cxx11::sub_match&lt;__gnu_cxx::__normal_iterator&lt;char const*, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt;::_M_str&lt;__gnu_cxx::__normal_iterator&lt;char const*, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt;() const ()
#0  0x0000555555742bf9 in std::enable_if&lt;std::__detail::__is_contiguous_iter&lt;__gnu_cxx::__normal_iterator&lt;char const*, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt;::value, std::__cxx11::sub_match&lt;__gnu_cxx::__normal_iterator&lt;char const*, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt;::__string_view&gt;::type std::__cxx11::sub_match&lt;__gnu_cxx::__normal_iterator&lt;char const*, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt;::_M_str&lt;__gnu_cxx::__normal_iterator&lt;char const*, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt;() const ()
#1  0x0000555555741e06 in std::__cxx11::sub_match&lt;__gnu_cxx::__normal_iterator&lt;char const*, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt;::compare(std::__cxx11::sub_match&lt;__gnu_cxx::__normal_iterator&lt;char const*, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt; const&amp;) const ()
#2  0x0000555555741014 in bool std::__cxx11::operator==&lt;__gnu_cxx::__normal_iterator&lt;char const*, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt;(std::__cxx11::sub_match&lt;__gnu_cxx::__normal_iterator&lt;char const*, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt; const&amp;, std::__cxx11::sub_match&lt;__gnu_cxx::__normal_iterator&lt;char const*, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt; const&amp;) ()
#3  0x00005555557402f8 in std::__cxx11::regex_iterator&lt;__gnu_cxx::__normal_iterator&lt;char const*, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, char, std::__cxx11::regex_traits&lt;char&gt; &gt;::operator==(std::__cxx11::regex_iterator&lt;__gnu_cxx::__normal_iterator&lt;char const*, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, char, std::__cxx11::regex_traits&lt;char&gt; &gt; const&amp;) const ()
#4  0x00007fffe96d504f in std::back_insert_iterator&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; std::regex_replace&lt;std::back_insert_iterator&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, __gnu_cxx::__normal_iterator&lt;char const*, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::__cxx11::regex_traits&lt;char&gt;, char&gt;(std::back_insert_iterator&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, __gnu_cxx::__normal_iterator&lt;char const*, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, __gnu_cxx::__normal_iterator&lt;char const*, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::__cxx11::basic_regex&lt;char, std::__cxx11::regex_traits&lt;char&gt; &gt; const&amp;, char const*, std::regex_constants::match_flag_type) ()
   from /opt/libtorch/lib/libtorch_cpu.so
#5  0x00007fffe97f7349 in torch::jit::SourceImporterImpl::attributeAssignmentSpecialHandlingHack(c10::QualifiedName const&amp;, torch::jit::Assign const&amp;) ()
   from /opt/libtorch/lib/libtorch_cpu.so
#6  0x00007fffe97f9e66 in torch::jit::SourceImporterImpl::importClass(c10::QualifiedName const&amp;, torch::jit::ClassDef const&amp;, bool) () from /opt/libtorch/lib/libtorch_cpu.so
#7  0x00007fffe97fa9fb in torch::jit::SourceImporterImpl::importNamedType(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, torch::jit::ClassDef const&amp;) () from /opt/libtorch/lib/libtorch_cpu.so
#8  0x00007fffe97fae05 in torch::jit::SourceImporterImpl::findNamedType(c10::QualifiedName const&amp;) () from /opt/libtorch/lib/libtorch_cpu.so
#9  0x00007fffe97faeea in torch::jit::SourceImporterImpl::resolveType(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, torch::jit::SourceRange const&amp;)
    () from /opt/libtorch/lib/libtorch_cpu.so
#10 0x00007fffe9568821 in torch::jit::ScriptTypeParser::parseTypeFromExpr(torch::jit::Expr const&amp;) const () from /opt/libtorch/lib/libtorch_cpu.so
#11 0x00007fffe9568ae9 in torch::jit::ScriptTypeParser::parseType(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;) ()
   from /opt/libtorch/lib/libtorch_cpu.so
#12 0x00007fffe97ecfbb in torch::jit::SourceImporter::loadType(c10::QualifiedName const&amp;) const () from /opt/libtorch/lib/libtorch_cpu.so
#13 0x00007fffe97d912b in std::_Function_handler&lt;c10::StrongTypePtr (c10::QualifiedName const&amp;), torch::jit::(anonymous namespace)::ScriptModuleDeserializer::readArchive(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;)::{lambda(c10::QualifiedName const&amp;)#1}&gt;::_M_invoke(std::_Any_data const&amp;, c10::QualifiedName const&amp;) ()
   from /opt/libtorch/lib/libtorch_cpu.so
#14 0x00007fffe982e16b in torch::jit::Unpickler::readGlobal(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;) () from /opt/libtorch/lib/libtorch_cpu.so
#15 0x00007fffe9832c02 in torch::jit::Unpickler::readInstruction() ()
   from /opt/libtorch/lib/libtorch_cpu.so
#16 0x00007fffe9835840 in torch::jit::Unpickler::run() ()
   from /opt/libtorch/lib/libtorch_cpu.so
#17 0x00007fffe9835df1 in torch::jit::Unpickler::parse_ivalue() ()
   from /opt/libtorch/lib/libtorch_cpu.so
#18 0x00007fffe97d9cc2 in torch::jit::readArchiveAndTensors(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;, c10::optional&lt;std::function&lt;c10::StrongTypePtr (c10::QualifiedName const&amp;)&gt; &gt;, c10::optional&lt;std::function&lt;c10::intrusive_ptr&lt;c10::ivalue::Object, c10::detail::intrusive_target_default_null_type&lt;c10::ivalue::Object&gt; &gt; (c10::StrongTypePtr, c10::IValue)&gt; &gt;, c10::optional&lt;c10::Device&gt;, caffe2::serialize::PyTorchStreamReader&amp;)
    () from /opt/libtorch/lib/libtorch_cpu.so
#19 0x00007fffe97d9fdd in torch::jit::(anonymous namespace)::ScriptModuleDeserializer::readArchive(std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;) [clone .constprop.789] () from /opt/libtorch/lib/libtorch_cpu.so
#20 0x00007fffe97dc905 in torch::jit::(anonymous namespace)::ScriptModuleDeserializer::deserialize(c10::optional&lt;c10::Device&gt;, std::unordered_map&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::hash&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::equal_to&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::allocator&lt;std::pair&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt; &gt;&amp;) [clone .constprop.788] ()
   from /opt/libtorch/lib/libtorch_cpu.so
#21 0x00007fffe97dced9 in torch::jit::load(std::unique_ptr&lt;caffe2::serialize::ReadAdapterInterface, std::default_delete&lt;caffe2::serialize::ReadAdapterInterface&gt; &gt;, c10::optional&lt;c10::Device&gt;, std::unordered_map&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::hash&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::equal_to&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::allocator&lt;std::pair&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt; &gt;&amp;) () from /opt/libtorch/lib/libtorch_cpu.so
#22 0x00007fffe97df755 in torch::jit::load(std::istream&amp;, c10::optional&lt;c10::Device&gt;, std::unordered_map&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::hash&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::equal_to&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;, std::allocator&lt;std::pair&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt; &gt;&amp;) ()
   from /opt/libtorch/lib/libtorch_cpu.so
```

## Expected behavior

The program should never cause segmentation fault.

## Environment

 - PyTorch Version (e.g., 1.0): Libtorch v1.7.1, both cpu and Cuda versions produce same error
 - OS (e.g., Linux): Ubuntu 20.04 and Ubuntu 18.04
 - How you installed PyTorch (`conda`, `pip`, source): Binary download of libtorch
 - CUDA/cuDNN version: CPU and CUDA 11.0 with cuDNN 8.0.5
 - GPU models and configuration: CPU and RTX 2070
 - Any other relevant information: Tested on several computers in several different docker environments

## Additional context

Would it be helpful if I attempt to build libtorch from source with debug symbols to produce a better stack trace in gdb and inspect local variables?


cc @gmagogsfm</span></p>
  </div>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>I have run into the exact issue linked above.</p>
<p>My reproducable example:</p>
<ul>
<li>Redhat 7.9</li>
<li>Matlab 2020a</li>
<li>libtorch 1.7.1</li>
</ul>
<p>I create a .so which has a single function inside, all this function does is call torch::jit::load. Matlab loads in the .so through calllib(), when the .so function is called and torch::jit::load is invoked a segmentation fault will result and everything will crash in the exact way that has been linked. It seems that this is an open bug with several other individuals facing this same problem. Could a staff member step in to take a look at this?</p>