Search Unity

Linux Player: Apps based on Unity 2018.3.0f2 crashe on "std::getline()" from C/C++ native code.

Discussion in 'Linux' started by jan_bajana, Dec 20, 2018.

  1. jan_bajana

    jan_bajana

    Joined:
    Sep 15, 2015
    Posts:
    22
    After we updated our SDK to new Unity version 2018.3.0f2 we have found crashing all applications which were always working before.
    After debuging we found crashes in native C/C++ plugins if they use:
    Code (CSharp):
    1. std::getline(input, line);
    This affects all Unity applications built with 2018.3.0f2.
    So I tried to replicate it on a simple sample, see below.

    Target platform is Linux Ubuntu x64 build:
    System information:
    • g++ (Ubuntu 5.4.1-8ubuntu1) 5.4.1 20170304
    • clang version 4.0.0 (tags/RELEASE_400/rc1)
      Target: x86_64-pc-linux-gnu
      Thread model: posix
    How to replicate? Build a simple native plugin:
    1. Create sample file "vi test.cpp"
    Code (CSharp):
    1. #include <memory.h>
    2. #include <string>
    3. #include <stdio.h>
    4. #include <sstream>
    5. #include <iostream>
    6.  
    7. extern "C" void L7_GetDeviceModel(char* buffer) {
    8.     std::istringstream input;
    9.     input.str("1\n2\n3\n4\n5\n6\n7\n");
    10.     std::string line;
    11.     std::getline(input, line);
    12.     strcpy(buffer, line.c_str());
    13. }
    2. How we built:
    1. sudo g++ -std=c++11 -g -fPIC -o libunitywrapper.so -shared test.cpp
    2. sudo clang++-4.0 -g -fPIC -o libunitywrapper.so -shared test.cpp
    3. sudo g++ -g -fPIC -o libunitywrapper.so -shared test.cpp
    4. sudo g++ -std=gnu++11 -g -fPIC -o libunitywrapper.so -shared test.cpp
    3. How we call in C#:
    Code (CSharp):
    1.             [DllImport("libunitywrapper", CallingConvention = CallingConvention.Cdecl)]
    2.             internal static extern void L7_GetDeviceMake(
    3.                 [MarshalAs(UnmanagedType.LPStr)] StringBuilder buffer);
    You can see full stacktrace in the uploaded log:
    Important part there:
    Code (CSharp):
    1. #8  0x00007f6ff0bbe579 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
    2. #9  0x00007f6ff0be7fe2 in std::__throw_bad_cast() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
    3. #10 0x00007f6ff0be16a1 in std::basic_istream<char, std::char_traits<char> >& std::getline<char, std::char_traits<char>, std::allocator<char> >(std::basic_istream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
    4. #11 0x00007f6fc8c81f01 in L7_GetDeviceModel (buffer=0x3fb7580 "") at test.cpp:12
    Error:
    Code (CSharp):
    1. terminate called after throwing an instance of 'std::bad_cast'
    2.   what():  std::bad_cast
    This bug affects the compatibility of all applications we want to build on Unity 2018.3.0f2.
    Any ideas why it happens? Is that compiler version or C++ language standard?

    Thank you.
     
    Last edited: Dec 21, 2018
  2. jan_bajana

    jan_bajana

    Joined:
    Sep 15, 2015
    Posts:
    22
    Uploaded log with native stacktrace:
     

    Attached Files:

  3. jan_bajana

    jan_bajana

    Joined:
    Sep 15, 2015
    Posts:
    22
    I am adding here source files who want to easy replicate issue:
    C# and CPP plugin file.
     

    Attached Files:

  4. marcdi_unity

    marcdi_unity

    Unity Technologies

    Joined:
    May 29, 2018
    Posts:
    17
    Hi,

    It should now be fixed in version 2018.3.4.

    Linux: Fixed using libstdc++ in native plugins crashing (2018.3 no longer links with libstdc++ statically). (1115024)​
     
  5. jan_bajana

    jan_bajana

    Joined:
    Sep 15, 2015
    Posts:
    22
    Hey marcdi_unity,
    sorry for late answer. Thank you for the fix. All is working for now since 2018.3.4.