robotology / osqp-eigen

Simple Eigen-C++ wrapper for OSQP library
https://robotology.github.io/osqp-eigen/
BSD 3-Clause "New" or "Revised" License
385 stars 114 forks source link

Segmentation fault (core dumped) on MPCExample #81

Closed tombelv closed 3 years ago

tombelv commented 3 years ago

Hi, I've just installed osqp-eigen and tried to run the example, which compiles fine but does not run. In particular I get a segmentation fault when calling

solver.initSolver()

on line

https://github.com/robotology/osqp-eigen/blob/64cfeb69d9d3669019c55e07af3e69522569297f/example/src/MPCExample.cpp#L267

My system is based on Ubuntu 20.04 with osqp version 0.6.2 installed from source.

Running the tests i get

Running tests...
Test project /home/tom/osqp-eigen/build
    Start 1: SparseMatrixUnitTests
1/5 Test #1: SparseMatrixUnitTests ............   Passed    0.00 sec
    Start 2: QPUnitTests
2/5 Test #2: QPUnitTests ......................***Exception: SegFault  0.22 sec
    Start 3: UpdateMatricesUnitTests
3/5 Test #3: UpdateMatricesUnitTests ..........***Exception: SegFault  0.20 sec
    Start 4: MPCUnitTests
4/5 Test #4: MPCUnitTests .....................***Exception: SegFault  0.21 sec
    Start 5: MPCUpdateMatricesUnitTests
5/5 Test #5: MPCUpdateMatricesUnitTests .......***Exception: SegFault  0.21 sec

20% tests passed, 4 tests failed out of 5

Total Test time (real) =   0.85 sec

The following tests FAILED:
      2 - QPUnitTests (SEGFAULT)
      3 - UpdateMatricesUnitTests (SEGFAULT)
      4 - MPCUnitTests (SEGFAULT)
      5 - MPCUpdateMatricesUnitTests (SEGFAULT)
Errors while running CTest
make: *** [Makefile:107: test] Error 8 

Do you have any pointers where I did something wrong or if there is a problem with osqp-eigen? Thanks in advance.

S-Dafarra commented 3 years ago

Hi @tombelv, thanks for reporting this.

Can you try running the tests compiling in DEBUG mode to see if we can get more information?

cc @GiulioRomualdi

tombelv commented 3 years ago

Can you try running the tests compiling in DEBUG mode to see if we can get more information?

I'm a beginner at CMake so I apologize if I'm doing something wrong, but running

cmake -DBUILD_TESTING=ON -DCMAKE_BUILD_TYPE=Debug ../
make
make test

I get the same result

S-Dafarra commented 3 years ago

Can you try running the tests compiling in DEBUG mode to see if we can get more information?

I'm a beginner at CMake so I apologize if I'm doing something wrong, but running

cmake -DBUILD_TESTING=ON -DCMAKE_BUILD_TYPE=Debug ../
make
make test

I get the same result

No need to apologize :wink: I have quickly checked on my setup (running Ubuntu 20.04) but I could not replicate it. Can you try to install valgrind with sudo apt install valgrind and then run one of the tests using valgrind, e.g.

cd to_where_you_compiled_osqp_eigen
cd bin
valgrind ./QPUnitTests

and attach here the output. That could tell us a bit more about what may be causing the segmentation fault.

tombelv commented 3 years ago

Here is the output

==51488== Memcheck, a memory error detector
==51488== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==51488== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==51488== Command: ./QPUnitTests
==51488== 
[OsqpEigen::Data::setHessianMatrix] Please set the number of variables before add the hessian matrix.
==51488== Conditional jump or move depends on uninitialised value(s)
==51488==    at 0x4BC2E5A: validate_data (in /home/tom/Thesis/acados/lib/libosqp.so)
==51488==    by 0x4BC68E6: osqp_setup (in /home/tom/Thesis/acados/lib/libosqp.so)
==51488==    by 0x4BB0C3C: OsqpEigen::Solver::initSolver() (Solver.cpp:83)
==51488==    by 0x110460: ____C_A_T_C_H____T_E_S_T____0() (QPTest.cpp:51)
==51488==    by 0x4A549D1: Catch::TestInvokerAsFunction::invoke() const (catch.hpp:14261)
==51488==    by 0x4A53BF6: Catch::TestCase::invoke() const (catch.hpp:14100)
==51488==    by 0x4A4DE99: Catch::RunContext::invokeActiveTestCase() (catch.hpp:12959)
==51488==    by 0x4A4DBCC: Catch::RunContext::runCurrentTest(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (catch.hpp:12932)
==51488==    by 0x4A4C642: Catch::RunContext::runTest(Catch::TestCase const&) (catch.hpp:12693)
==51488==    by 0x4A4F85D: Catch::(anonymous namespace)::TestGroup::execute() (catch.hpp:13287)
==51488==    by 0x4A50C9E: Catch::Session::runInternal() (catch.hpp:13493)
==51488==    by 0x4A50994: Catch::Session::run() (catch.hpp:13449)
==51488== 
==51488== Use of uninitialised value of size 8
==51488==    at 0x4BC2E71: validate_data (in /home/tom/Thesis/acados/lib/libosqp.so)
==51488==    by 0x4BC68E6: osqp_setup (in /home/tom/Thesis/acados/lib/libosqp.so)
==51488==    by 0x4BB0C3C: OsqpEigen::Solver::initSolver() (Solver.cpp:83)
==51488==    by 0x110460: ____C_A_T_C_H____T_E_S_T____0() (QPTest.cpp:51)
==51488==    by 0x4A549D1: Catch::TestInvokerAsFunction::invoke() const (catch.hpp:14261)
==51488==    by 0x4A53BF6: Catch::TestCase::invoke() const (catch.hpp:14100)
==51488==    by 0x4A4DE99: Catch::RunContext::invokeActiveTestCase() (catch.hpp:12959)
==51488==    by 0x4A4DBCC: Catch::RunContext::runCurrentTest(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (catch.hpp:12932)
==51488==    by 0x4A4C642: Catch::RunContext::runTest(Catch::TestCase const&) (catch.hpp:12693)
==51488==    by 0x4A4F85D: Catch::(anonymous namespace)::TestGroup::execute() (catch.hpp:13287)
==51488==    by 0x4A50C9E: Catch::Session::runInternal() (catch.hpp:13493)
==51488==    by 0x4A50994: Catch::Session::run() (catch.hpp:13449)
==51488== 
==51488== Conditional jump or move depends on uninitialised value(s)
==51488==    at 0x4BC2E75: validate_data (in /home/tom/Thesis/acados/lib/libosqp.so)
==51488==    by 0x4BC68E6: osqp_setup (in /home/tom/Thesis/acados/lib/libosqp.so)
==51488==    by 0x4BB0C3C: OsqpEigen::Solver::initSolver() (Solver.cpp:83)
==51488==    by 0x110460: ____C_A_T_C_H____T_E_S_T____0() (QPTest.cpp:51)
==51488==    by 0x4A549D1: Catch::TestInvokerAsFunction::invoke() const (catch.hpp:14261)
==51488==    by 0x4A53BF6: Catch::TestCase::invoke() const (catch.hpp:14100)
==51488==    by 0x4A4DE99: Catch::RunContext::invokeActiveTestCase() (catch.hpp:12959)
==51488==    by 0x4A4DBCC: Catch::RunContext::runCurrentTest(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (catch.hpp:12932)
==51488==    by 0x4A4C642: Catch::RunContext::runTest(Catch::TestCase const&) (catch.hpp:12693)
==51488==    by 0x4A4F85D: Catch::(anonymous namespace)::TestGroup::execute() (catch.hpp:13287)
==51488==    by 0x4A50C9E: Catch::Session::runInternal() (catch.hpp:13493)
==51488==    by 0x4A50994: Catch::Session::run() (catch.hpp:13449)
==51488== 
==51488== Conditional jump or move depends on uninitialised value(s)
==51488==    at 0x4E78AD8: __vfprintf_internal (vfprintf-internal.c:1687)
==51488==    by 0x4F2F0EA: __printf_chk (printf_chk.c:33)
==51488==    by 0x4BC2FAF: validate_data (in /home/tom/Thesis/acados/lib/libosqp.so)
==51488==    by 0x4BC68E6: osqp_setup (in /home/tom/Thesis/acados/lib/libosqp.so)
==51488==    by 0x4BB0C3C: OsqpEigen::Solver::initSolver() (Solver.cpp:83)
==51488==    by 0x110460: ____C_A_T_C_H____T_E_S_T____0() (QPTest.cpp:51)
==51488==    by 0x4A549D1: Catch::TestInvokerAsFunction::invoke() const (catch.hpp:14261)
==51488==    by 0x4A53BF6: Catch::TestCase::invoke() const (catch.hpp:14100)
==51488==    by 0x4A4DE99: Catch::RunContext::invokeActiveTestCase() (catch.hpp:12959)
==51488==    by 0x4A4DBCC: Catch::RunContext::runCurrentTest(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (catch.hpp:12932)
==51488==    by 0x4A4C642: Catch::RunContext::runTest(Catch::TestCase const&) (catch.hpp:12693)
==51488==    by 0x4A4F85D: Catch::(anonymous namespace)::TestGroup::execute() (catch.hpp:13287)
==51488== 
==51488== Use of uninitialised value of size 8
==51488==    at 0x4E5C81B: _itoa_word (_itoa.c:179)
==51488==    by 0x4E786F4: __vfprintf_internal (vfprintf-internal.c:1687)
==51488==    by 0x4F2F0EA: __printf_chk (printf_chk.c:33)
==51488==    by 0x4BC2FAF: validate_data (in /home/tom/Thesis/acados/lib/libosqp.so)
==51488==    by 0x4BC68E6: osqp_setup (in /home/tom/Thesis/acados/lib/libosqp.so)
==51488==    by 0x4BB0C3C: OsqpEigen::Solver::initSolver() (Solver.cpp:83)
==51488==    by 0x110460: ____C_A_T_C_H____T_E_S_T____0() (QPTest.cpp:51)
==51488==    by 0x4A549D1: Catch::TestInvokerAsFunction::invoke() const (catch.hpp:14261)
==51488==    by 0x4A53BF6: Catch::TestCase::invoke() const (catch.hpp:14100)
==51488==    by 0x4A4DE99: Catch::RunContext::invokeActiveTestCase() (catch.hpp:12959)
==51488==    by 0x4A4DBCC: Catch::RunContext::runCurrentTest(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (catch.hpp:12932)
==51488==    by 0x4A4C642: Catch::RunContext::runTest(Catch::TestCase const&) (catch.hpp:12693)
==51488== 
==51488== Conditional jump or move depends on uninitialised value(s)
==51488==    at 0x4E5C82D: _itoa_word (_itoa.c:179)
==51488==    by 0x4E786F4: __vfprintf_internal (vfprintf-internal.c:1687)
==51488==    by 0x4F2F0EA: __printf_chk (printf_chk.c:33)
==51488==    by 0x4BC2FAF: validate_data (in /home/tom/Thesis/acados/lib/libosqp.so)
==51488==    by 0x4BC68E6: osqp_setup (in /home/tom/Thesis/acados/lib/libosqp.so)
==51488==    by 0x4BB0C3C: OsqpEigen::Solver::initSolver() (Solver.cpp:83)
==51488==    by 0x110460: ____C_A_T_C_H____T_E_S_T____0() (QPTest.cpp:51)
==51488==    by 0x4A549D1: Catch::TestInvokerAsFunction::invoke() const (catch.hpp:14261)
==51488==    by 0x4A53BF6: Catch::TestCase::invoke() const (catch.hpp:14100)
==51488==    by 0x4A4DE99: Catch::RunContext::invokeActiveTestCase() (catch.hpp:12959)
==51488==    by 0x4A4DBCC: Catch::RunContext::runCurrentTest(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (catch.hpp:12932)
==51488==    by 0x4A4C642: Catch::RunContext::runTest(Catch::TestCase const&) (catch.hpp:12693)
==51488== 
==51488== Conditional jump or move depends on uninitialised value(s)
==51488==    at 0x4E793A8: __vfprintf_internal (vfprintf-internal.c:1687)
==51488==    by 0x4F2F0EA: __printf_chk (printf_chk.c:33)
==51488==    by 0x4BC2FAF: validate_data (in /home/tom/Thesis/acados/lib/libosqp.so)
==51488==    by 0x4BC68E6: osqp_setup (in /home/tom/Thesis/acados/lib/libosqp.so)
==51488==    by 0x4BB0C3C: OsqpEigen::Solver::initSolver() (Solver.cpp:83)
==51488==    by 0x110460: ____C_A_T_C_H____T_E_S_T____0() (QPTest.cpp:51)
==51488==    by 0x4A549D1: Catch::TestInvokerAsFunction::invoke() const (catch.hpp:14261)
==51488==    by 0x4A53BF6: Catch::TestCase::invoke() const (catch.hpp:14100)
==51488==    by 0x4A4DE99: Catch::RunContext::invokeActiveTestCase() (catch.hpp:12959)
==51488==    by 0x4A4DBCC: Catch::RunContext::runCurrentTest(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (catch.hpp:12932)
==51488==    by 0x4A4C642: Catch::RunContext::runTest(Catch::TestCase const&) (catch.hpp:12693)
==51488==    by 0x4A4F85D: Catch::(anonymous namespace)::TestGroup::execute() (catch.hpp:13287)
==51488== 
==51488== Conditional jump or move depends on uninitialised value(s)
==51488==    at 0x4E7886E: __vfprintf_internal (vfprintf-internal.c:1687)
==51488==    by 0x4F2F0EA: __printf_chk (printf_chk.c:33)
==51488==    by 0x4BC2FAF: validate_data (in /home/tom/Thesis/acados/lib/libosqp.so)
==51488==    by 0x4BC68E6: osqp_setup (in /home/tom/Thesis/acados/lib/libosqp.so)
==51488==    by 0x4BB0C3C: OsqpEigen::Solver::initSolver() (Solver.cpp:83)
==51488==    by 0x110460: ____C_A_T_C_H____T_E_S_T____0() (QPTest.cpp:51)
==51488==    by 0x4A549D1: Catch::TestInvokerAsFunction::invoke() const (catch.hpp:14261)
==51488==    by 0x4A53BF6: Catch::TestCase::invoke() const (catch.hpp:14100)
==51488==    by 0x4A4DE99: Catch::RunContext::invokeActiveTestCase() (catch.hpp:12959)
==51488==    by 0x4A4DBCC: Catch::RunContext::runCurrentTest(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (catch.hpp:12932)
==51488==    by 0x4A4C642: Catch::RunContext::runTest(Catch::TestCase const&) (catch.hpp:12693)
==51488==    by 0x4A4F85D: Catch::(anonymous namespace)::TestGroup::execute() (catch.hpp:13287)
==51488== 
ERROR in validate_data: P does not have dimension n x n with n = 1155316
ERROR in osqp_setup: Data validation returned failure
==51488== Conditional jump or move depends on uninitialised value(s)
==51488==    at 0x4BC6035: osqp_solve (in /home/tom/Thesis/acados/lib/libosqp.so)
==51488==    by 0x4BB0D85: OsqpEigen::Solver::solve() (Solver.cpp:118)
==51488==    by 0x110544: ____C_A_T_C_H____T_E_S_T____0() (QPTest.cpp:53)
==51488==    by 0x4A549D1: Catch::TestInvokerAsFunction::invoke() const (catch.hpp:14261)
==51488==    by 0x4A53BF6: Catch::TestCase::invoke() const (catch.hpp:14100)
==51488==    by 0x4A4DE99: Catch::RunContext::invokeActiveTestCase() (catch.hpp:12959)
==51488==    by 0x4A4DBCC: Catch::RunContext::runCurrentTest(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (catch.hpp:12932)
==51488==    by 0x4A4C642: Catch::RunContext::runTest(Catch::TestCase const&) (catch.hpp:12693)
==51488==    by 0x4A4F85D: Catch::(anonymous namespace)::TestGroup::execute() (catch.hpp:13287)
==51488==    by 0x4A50C9E: Catch::Session::runInternal() (catch.hpp:13493)
==51488==    by 0x4A50994: Catch::Session::run() (catch.hpp:13449)
==51488==    by 0x4A9189C: int Catch::Session::run<char>(int, char const* const*) (catch.hpp:13171)
==51488== 
==51488== Use of uninitialised value of size 8
==51488==    at 0x4BC603B: osqp_solve (in /home/tom/Thesis/acados/lib/libosqp.so)
==51488==    by 0x4BB0D85: OsqpEigen::Solver::solve() (Solver.cpp:118)
==51488==    by 0x110544: ____C_A_T_C_H____T_E_S_T____0() (QPTest.cpp:53)
==51488==    by 0x4A549D1: Catch::TestInvokerAsFunction::invoke() const (catch.hpp:14261)
==51488==    by 0x4A53BF6: Catch::TestCase::invoke() const (catch.hpp:14100)
==51488==    by 0x4A4DE99: Catch::RunContext::invokeActiveTestCase() (catch.hpp:12959)
==51488==    by 0x4A4DBCC: Catch::RunContext::runCurrentTest(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (catch.hpp:12932)
==51488==    by 0x4A4C642: Catch::RunContext::runTest(Catch::TestCase const&) (catch.hpp:12693)
==51488==    by 0x4A4F85D: Catch::(anonymous namespace)::TestGroup::execute() (catch.hpp:13287)
==51488==    by 0x4A50C9E: Catch::Session::runInternal() (catch.hpp:13493)
==51488==    by 0x4A50994: Catch::Session::run() (catch.hpp:13449)
==51488==    by 0x4A9189C: int Catch::Session::run<char>(int, char const* const*) (catch.hpp:13171)
==51488== 
==51488== Invalid read of size 8
==51488==    at 0x4BC603B: osqp_solve (in /home/tom/Thesis/acados/lib/libosqp.so)
==51488==    by 0x4BB0D85: OsqpEigen::Solver::solve() (Solver.cpp:118)
==51488==    by 0x110544: ____C_A_T_C_H____T_E_S_T____0() (QPTest.cpp:53)
==51488==    by 0x4A549D1: Catch::TestInvokerAsFunction::invoke() const (catch.hpp:14261)
==51488==    by 0x4A53BF6: Catch::TestCase::invoke() const (catch.hpp:14100)
==51488==    by 0x4A4DE99: Catch::RunContext::invokeActiveTestCase() (catch.hpp:12959)
==51488==    by 0x4A4DBCC: Catch::RunContext::runCurrentTest(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) (catch.hpp:12932)
==51488==    by 0x4A4C642: Catch::RunContext::runTest(Catch::TestCase const&) (catch.hpp:12693)
==51488==    by 0x4A4F85D: Catch::(anonymous namespace)::TestGroup::execute() (catch.hpp:13287)
==51488==    by 0x4A50C9E: Catch::Session::runInternal() (catch.hpp:13493)
==51488==    by 0x4A50994: Catch::Session::run() (catch.hpp:13449)
==51488==    by 0x4A9189C: int Catch::Session::run<char>(int, char const* const*) (catch.hpp:13171)
==51488==  Address 0x10011a1ac is not stack'd, malloc'd or (recently) free'd
==51488== 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
QPUnitTests is a Catch v2.13.4 host application.
Run with -? for options

-------------------------------------------------------------------------------
QPProblem
-------------------------------------------------------------------------------
/home/tom/osqp-eigen/tests/QPTest.cpp:13
...............................................................................

/home/tom/osqp-eigen/tests/QPTest.cpp:13: FAILED:
  {Unknown expression after the reported line}
due to a fatal error condition:
  SIGSEGV - Segmentation violation signal

===============================================================================
test cases: 1 | 1 failed
assertions: 8 | 7 passed | 1 failed

==51488== 
==51488== Process terminating with default action of signal 11 (SIGSEGV)
==51488==    at 0x4E44169: raise (raise.c:46)
==51488==    by 0x4A45997: Catch::FatalConditionHandler::handleSignal(int) (catch.hpp:10843)
==51488==    by 0x4E4420F: ??? (in /usr/lib/x86_64-linux-gnu/libc-2.31.so)
==51488==    by 0x4BC603A: osqp_solve (in /home/tom/Thesis/acados/lib/libosqp.so)
==51488== 
==51488== HEAP SUMMARY:
==51488==     in use at exit: 13,406 bytes in 163 blocks
==51488==   total heap usage: 2,562 allocs, 2,399 frees, 420,706 bytes allocated
==51488== 
==51488== LEAK SUMMARY:
==51488==    definitely lost: 0 bytes in 0 blocks
==51488==    indirectly lost: 0 bytes in 0 blocks
==51488==      possibly lost: 0 bytes in 0 blocks
==51488==    still reachable: 13,406 bytes in 163 blocks
==51488==         suppressed: 0 bytes in 0 blocks
==51488== Rerun with --leak-check=full to see details of leaked memory
==51488== 
==51488== Use --track-origins=yes to see where uninitialised values come from
==51488== For lists of detected and suppressed errors, rerun with: -s
==51488== ERROR SUMMARY: 23 errors from 11 contexts (suppressed: 0 from 0)
Segmentation fault (core dumped)
S-Dafarra commented 3 years ago

Interestingly, it seems to link to the osqp version in the acados folder. See for example

==51488==    at 0x4BC2E5A: validate_data (in /home/tom/Thesis/acados/lib/libosqp.so)

I suspect you may have two osqp versions in your system.

S-Dafarra commented 3 years ago

Indeed, acados seems to be using a pretty old version of osqp. See https://github.com/acados/acados/tree/master/external. When compiling, OsqpEigen finds the new version, while when running the tests the linker finds the old version in the acados folder.

cc @traversaro

tombelv commented 3 years ago

Nice catch, it is true that I also have acados installed which came with its own osqp version (although I haven't even used it with osqp), so indeed this is a problem with my setup.

That acados folder should be at the bottom of my LD_LIBRARY_PATH so I thought that the system wide installation of osqp would be found first, but it doesn't seem to be the case.

I've removed it from the acados folder, now all tests are working and so is the example.

Thank you very much for the swift help!

S-Dafarra commented 3 years ago

Nice catch, it is true that I also have acados installed which came with its own osqp version (although I haven't even used it with osqp), so indeed this is a problem with my setup.

That acados folder should be at the bottom of my LD_LIBRARY_PATH so I thought that the system wide installation of osqp would be found first, but it doesn't seem to be the case.

I've removed it from the acados folder, now all tests are working and so is the example.

Thank you very much for the swift help!

Awesome! You are welcome!

zmwhit commented 2 years ago

Helpful! I meet just the same problem! damn acados :)