Std::regex_replace not work with libtorch 1.7.0

SixerWang: <p>I upgrade libtorch to 1.7.0 these days，and I find <code>std::regex_replace</code> not work with libtorch 1.7.0. I reproduce this issue through minimal code below:</p>
<p><strong>1. I compile a shared library</strong></p>
<p>main.cpp:</p>
<pre><code class="lang-auto">#include &lt;regex&gt;
#include &lt;iostream&gt;

void func()
{
    std::cout &lt;&lt; std::regex_replace("{layer}", std::regex("layer"), "0") &lt;&lt; std::endl;
};
</code></pre>
<p>then compile using g++:</p>
<pre><code class="lang-auto">g++ -fPIC -std=c++17 -shared main.cpp -o libmain.so

</code></pre>
<p><strong>2. libtorch code</strong></p>
<p>torch_test.cpp:</p>
<pre><code class="lang-auto">#include &lt;torch/torch.h&gt;
#include &lt;iostream&gt;

void func();

int main()
{
    func();
    setenv("CUDA_VISIBLE_DEVICES", "0,1", 1);
    auto device = torch::Device("cuda:0");
    std::cout &lt;&lt; device &lt;&lt; std::endl;
}

</code></pre>
<ul>
<li><strong>situation one</strong></li>
</ul>
<pre><code class="lang-auto">g++ -g -std=c++17 torch_test.cpp -o torch_test -I/opt/3rd/libtorch/include -I/opt/3rd/libtorch/include/torch/csrc/api/include -ltorch_cpu -ltorch_cuda -lc10 -lmain -L/opt/3rd/libtorch/lib -L./ -Wl,-rpath=/opt/3rd/libtorch/lib -Wl,-rpath=./
</code></pre>
<p>run <code>torch_test</code>, and the output is below:</p>
<pre><code class="lang-auto">$ ./torch_test 
{layer}
cuda:0
</code></pre>
<p>the <code>std::regex_replace</code> doesn’t work well</p>
<ul>
<li><strong>situation two</strong></li>
</ul>
<p>I change the <code>libmain.so</code> linking order</p>
<pre><code class="lang-auto">g++ -g -std=c++17 torch_test.cpp -o torch_test -I/opt/3rd/libtorch/include -I/opt/3rd/libtorch/include/torch/csrc/api/include -lmain -ltorch_cpu -ltorch_cuda -lc10 -L/opt/3rd/libtorch/lib -L./ -Wl,-rpath=/opt/3rd/libtorch/lib -Wl,-rpath=./
</code></pre>
<p>run <code>torch_test</code>, and the output is below:</p>
<pre><code class="lang-auto">$ ./torch_test 
{0}
[1]    2828402 segmentation fault (core dumped)  ./torch_test
</code></pre>
<p>the <code>std::regex_replace</code> works well, but libtorch code core dump.</p>

SixerWang: <p>Recompiling the libtorch could solve the problem.</p>

benjamingorman: <p>I’m seeing the same issue using libtorch 1.8.0, compiling with clang 11.1 on Ubuntu focal. If <code>std::regex_replace</code> is used at all before loading a torchscript model then there will be a segfault when <code>torch::jit::load</code> is called to load the model.</p>
<p>Here’s the relevant part of the stacktrace for the segfault from gdb:</p>
<pre><code class="lang-auto">#0  std::__detail::_State&lt;char&gt;::_M_opcode (this=0x94ce81b480053a10)
    at /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/regex_automaton.h:166
#1  0x00007ffff7c7882f in std::__detail::_NFA&lt;std::__cxx11::regex_traits&lt;char&gt; &gt;::_M_eliminate_dummy (this=0x2111fa0)
    at /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/bits/regex_automaton.tcc:182
#2  0x00007fffdf1cc828 in std::__detail::_Compiler&lt;std::__cxx11::regex_traits&lt;char&gt; &gt;::_Compiler(char const*, char const*, std::locale const&amp;, std::regex_constants::syntax_option_type) () from /usr/local/lib/libtorch-1/libtorch_cpu.so
#3  0x00007fffdf3ed405 in ?? () from /usr/local/lib/libtorch-1/libtorch_cpu.so
#4  0x00007fffdf3f0d1c in ?? () from /usr/local/lib/libtorch-1/libtorch_cpu.so
#5  0x00007fffdf3f164b in ?? () from /usr/local/lib/libtorch-1/libtorch_cpu.so
#6  0x00007fffdf3f1a55 in ?? () from /usr/local/lib/libtorch-1/libtorch_cpu.so
#7  0x00007fffdf3f1b3a in ?? () from /usr/local/lib/libtorch-1/libtorch_cpu.so
#8  0x00007fffdf1504a1 in torch::jit::ScriptTypeParser::parseTypeFromExpr(torch::jit::Expr const&amp;) const ()
   from /usr/local/lib/libtorch-1/libtorch_cpu.so
#9  0x00007fffdf150769 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 /usr/local/lib/libtorch-1/libtorch_cpu.so
#10 0x00007fffdf3e2b4b in torch::jit::SourceImporter::loadType(c10::QualifiedName const&amp;) const ()
   from /usr/local/lib/libtorch-1/libtorch_cpu.so
#11 0x00007fffdf3ce7cb in ?? () from /usr/local/lib/libtorch-1/libtorch_cpu.so
#12 0x00007fffdf41e8c2 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 /usr/local/lib/libtorch-1/libtorch_cpu.so
#13 0x00007fffdf422cca in torch::jit::Unpickler::readInstruction() () from /usr/local/lib/libtorch-1/libtorch_cpu.so
#14 0x00007fffdf4249e8 in torch::jit::Unpickler::run() () from /usr/local/lib/libtorch-1/libtorch_cpu.so
#15 0x00007fffdf424c0e in torch::jit::Unpickler::parse_ivalue() () from /usr/local/lib/libtorch-1/libtorch_cpu.so
#16 0x00007fffdf3ce441 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 /usr/local/lib/libtorch-1/libtorch_cpu.so
#17 0x00007fffdf3ce6e0 in ?? () from /usr/local/lib/libtorch-1/libtorch_cpu.so
#18 0x00007fffdf3d1587 in ?? () from /usr/local/lib/libtorch-1/libtorch_cpu.so
#19 0x00007fffdf3d1c24 in torch::jit::load(std::shared_ptr&lt;caffe2::serialize::ReadAdapterInterface&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 /usr/local/lib/libtorch-1/libtorch_cpu.so
#20 0x00007fffdf3d4be5 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 /usr/local/lib/libtorch-1/libtorch_cpu.so
#21 0x00007fffdf3d4cef in torch::jit::load(std::istream&amp;, c10::optional&lt;c10::Device&gt;) () from /usr/local/lib/libtorch-1/libtorch_cpu.so
</code></pre>

Just_Li: <p>same problem with you T_T <img src="https://discuss.pytorch.org/images/emoji/apple/sob.png?v=12" title=":sob:" class="emoji" alt=":sob:" loading="lazy" width="20" height="20"> <img src="https://discuss.pytorch.org/images/emoji/apple/sob.png?v=12" title=":sob:" class="emoji" alt=":sob:" loading="lazy" width="20" height="20"> <img src="https://discuss.pytorch.org/images/emoji/apple/sob.png?v=12" title=":sob:" class="emoji" alt=":sob:" loading="lazy" width="20" height="20"></p>