Std::regex_replace not work with libtorch 1.7.0

I upgrade libtorch to 1.7.0 these days,and I find std::regex_replace not work with libtorch 1.7.0. I reproduce this issue through minimal code below:

1. I compile a shared library

main.cpp:

#include <regex>
#include <iostream>

void func()
{
    std::cout << std::regex_replace("{layer}", std::regex("layer"), "0") << std::endl;
};

then compile using g++:

g++ -fPIC -std=c++17 -shared main.cpp -o libmain.so

2. libtorch code

torch_test.cpp:

#include <torch/torch.h>
#include <iostream>

void func();

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

  • situation one
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=./

run torch_test, and the output is below:

$ ./torch_test 
{layer}
cuda:0

the std::regex_replace doesn’t work well

  • situation two

I change the libmain.so linking order

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=./

run torch_test, and the output is below:

$ ./torch_test 
{0}
[1]    2828402 segmentation fault (core dumped)  ./torch_test

the std::regex_replace works well, but libtorch code core dump.

Recompiling the libtorch could solve the problem.

I’m seeing the same issue using libtorch 1.8.0, compiling with clang 11.1 on Ubuntu focal. If std::regex_replace is used at all before loading a torchscript model then there will be a segfault when torch::jit::load is called to load the model.

Here’s the relevant part of the stacktrace for the segfault from gdb:

#0  std::__detail::_State<char>::_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<std::__cxx11::regex_traits<char> >::_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<std::__cxx11::regex_traits<char> >::_Compiler(char const*, char const*, std::locale const&, 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&) const ()
   from /usr/local/lib/libtorch-1/libtorch_cpu.so
#9  0x00007fffdf150769 in torch::jit::ScriptTypeParser::parseType(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) () from /usr/local/lib/libtorch-1/libtorch_cpu.so
#10 0x00007fffdf3e2b4b in torch::jit::SourceImporter::loadType(c10::QualifiedName const&) 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<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) ()
   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<char, std::char_traits<char>, std::allocator<char> > const&, c10::optional<std::function<c10::StrongTypePtr (c10::QualifiedName const&)> >, c10::optional<std::function<c10::intrusive_ptr<c10::ivalue::Object, c10::detail::intrusive_target_default_null_type<c10::ivalue::Object> > (c10::StrongTypePtr, c10::IValue)> >, c10::optional<c10::Device>, caffe2::serialize::PyTorchStreamReader&) () 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<caffe2::serialize::ReadAdapterInterface>, c10::optional<c10::Device>, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&) () from /usr/local/lib/libtorch-1/libtorch_cpu.so
#20 0x00007fffdf3d4be5 in torch::jit::load(std::istream&, c10::optional<c10::Device>, std::unordered_map<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::hash<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::equal_to<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >&) ()
   from /usr/local/lib/libtorch-1/libtorch_cpu.so
#21 0x00007fffdf3d4cef in torch::jit::load(std::istream&, c10::optional<c10::Device>) () from /usr/local/lib/libtorch-1/libtorch_cpu.so

same problem with you T_T :sob: :sob: :sob: