Using GCC Undefined Behavior Sanitizer

2019-01-26 05:45发布

Today I have read an article about GCC Undefined Behavior Sanitizer (ubsan). However, when I follow steps there (add -fsanitize=undefined to my code), the compiler (GCC 4.9.2 on Ubuntu 15.04) says that some references are not defined:

||=== Build: Debug in Entangle (compiler: GNU GCC Compiler) ===|
obj/Debug/EntangleApp.o||In function `EntangleApp::OnInit()':|
/home/ilya/Projects/Entangle/EntangleApp.cpp|31|undefined reference to `__ubsan_handle_type_mismatch'|
/home/ilya/Projects/Entangle/EntangleApp.cpp|31|undefined reference to `__ubsan_handle_load_invalid_value'|
/home/ilya/Projects/Entangle/EntangleApp.cpp|32|undefined reference to `__ubsan_handle_type_mismatch'|
/home/ilya/Projects/Entangle/EntangleApp.cpp|34|undefined reference to `__ubsan_handle_type_mismatch'|
/home/ilya/Projects/Entangle/EntangleApp.cpp|34|undefined reference to `__ubsan_handle_load_invalid_value'|
/home/ilya/Projects/Entangle/EntangleApp.cpp|34|undefined reference to `__ubsan_handle_type_mismatch'|
/home/ilya/Projects/Entangle/EntangleApp.cpp|34|undefined reference to `__ubsan_handle_load_invalid_value'|
/home/ilya/Projects/Entangle/EntangleApp.cpp|35|undefined reference to `__ubsan_handle_type_mismatch'|
/home/ilya/Projects/Entangle/EntangleApp.cpp|37|undefined reference to `__ubsan_handle_type_mismatch'|
/home/ilya/Projects/Entangle/EntangleApp.cpp|37|undefined reference to `__ubsan_handle_load_invalid_value'|
/home/ilya/Projects/Entangle/EntangleApp.cpp|43|undefined reference to `__ubsan_handle_type_mismatch'|
/home/ilya/Projects/Entangle/EntangleApp.cpp|43|undefined reference to `__ubsan_handle_load_invalid_value'|
/home/ilya/Projects/Entangle/EntangleApp.cpp|52|undefined reference to `__ubsan_handle_type_mismatch'|
/home/ilya/Projects/Entangle/EntangleApp.cpp|52|undefined reference to `__ubsan_handle_type_mismatch'|
obj/Debug/EntangleApp.o||In function `EntangleApp::OnCmdLineParsed(wxCmdLineParser&)':|
/home/ilya/Projects/Entangle/EntangleApp.cpp|75|undefined reference to `__ubsan_handle_type_mismatch'|
obj/Debug/EntangleApp.o||In function `EntangleApp::OnRun()':|
/home/ilya/Projects/Entangle/EntangleApp.cpp|128|undefined reference to `__ubsan_handle_type_mismatch'|
/home/ilya/Projects/Entangle/EntangleApp.cpp|128|undefined reference to `__ubsan_handle_load_invalid_value'|
obj/Debug/EntangleApp.o||In function `wxPrivate::UntypedBufferData::~UntypedBufferData()':|
/usr/include/wx-3.0/wx/buffer.h|43|undefined reference to `__ubsan_handle_type_mismatch'|
/usr/include/wx-3.0/wx/buffer.h|43|undefined reference to `__ubsan_handle_load_invalid_value'|
/usr/include/wx-3.0/wx/buffer.h|44|undefined reference to `__ubsan_handle_type_mismatch'|
obj/Debug/EntangleApp.o||In function `std::exception::exception()':|
/usr/include/c++/4.9/exception|63|undefined reference to `__ubsan_handle_type_mismatch'|
obj/Debug/EntangleApp.o||In function `wxCriticalSectionLocker::wxCriticalSectionLocker(wxCriticalSection&)':|
/usr/include/wx-3.0/wx/thread.h|305|undefined reference to `__ubsan_handle_type_mismatch'|
/usr/include/wx-3.0/wx/thread.h|307|undefined reference to `__ubsan_handle_type_mismatch'|
obj/Debug/EntangleApp.o||In function `wxCriticalSectionLocker::~wxCriticalSectionLocker()':|
/usr/include/wx-3.0/wx/thread.h|312|undefined reference to `__ubsan_handle_type_mismatch'|
obj/Debug/EntangleApp.o:/usr/include/wx-3.0/wx/thread.h|767|more undefined references to `__ubsan_handle_type_mismatch' follow|
obj/Debug/EntangleApp.o||In function `wxThreadHelperThread::Entry()':|
/usr/include/wx-3.0/wx/thread.h|775|undefined reference to `__ubsan_handle_load_invalid_value'|
/usr/include/wx-3.0/wx/thread.h|776|undefined reference to `__ubsan_handle_type_mismatch'|
/usr/include/wx-3.0/wx/thread.h|776|undefined reference to `__ubsan_handle_type_mismatch'|
obj/Debug/EntangleApp.o||In function `std::type_info::name() const':|
/usr/include/c++/4.9/typeinfo|100|undefined reference to `__ubsan_handle_type_mismatch'|
/usr/include/c++/4.9/typeinfo|100|undefined reference to `__ubsan_handle_type_mismatch'|
/usr/include/c++/4.9/typeinfo|100|undefined reference to `__ubsan_handle_type_mismatch'|
obj/Debug/EntangleApp.o:/usr/include/c++/4.9/typeinfo|100|more undefined references to `__ubsan_handle_type_mismatch' follow|
obj/Debug/EntangleApp.o||In function `wxObjectEventFunctor::operator()(wxEvtHandler*, wxEvent&)':|
/usr/include/wx-3.0/wx/event.h|3757|undefined reference to `__ubsan_handle_add_overflow'|
/usr/include/wx-3.0/wx/event.h|3757|undefined reference to `__ubsan_handle_type_mismatch'|
/usr/include/wx-3.0/wx/event.h|3757|undefined reference to `__ubsan_handle_type_mismatch'|
obj/Debug/EntangleApp.o||In function `wxLocale::wxLocale()':|
/usr/include/wx-3.0/wx/intl.h|154|undefined reference to `__ubsan_handle_type_mismatch'|
obj/Debug/EntangleApp.o||In function `wxAppConsoleBase::CallOnInit()':|
/usr/include/wx-3.0/wx/app.h|93|undefined reference to `__ubsan_handle_type_mismatch'|
/usr/include/wx-3.0/wx/app.h|93|undefined reference to `__ubsan_handle_type_mismatch'|
obj/Debug/EntangleApp.o:/usr/include/wx-3.0/wx/app.h|592|more undefined references to `__ubsan_handle_type_mismatch' follow|
obj/Debug/EntangleApp.o||In function `wxAppBase::IsActive() const':|
/usr/include/wx-3.0/wx/app.h|592|undefined reference to `__ubsan_handle_load_invalid_value'|
obj/Debug/EntangleApp.o||In function `wxAppBase::SetTopWindow(wxWindow*)':|
/usr/include/wx-3.0/wx/app.h|595|undefined reference to `__ubsan_handle_type_mismatch'|
obj/Debug/EntangleApp.o||In function `EntangleApp::EntangleApp()':|
/home/ilya/Projects/Entangle/EntangleApp.h|19|undefined reference to `__ubsan_handle_type_mismatch'|
/home/ilya/Projects/Entangle/EntangleApp.h|19|undefined reference to `__ubsan_handle_type_mismatch'|
/home/ilya/Projects/Entangle/EntangleApp.h|19|undefined reference to `__ubsan_handle_type_mismatch'|
/home/ilya/Projects/Entangle/EntangleApp.h|19|undefined reference to `__ubsan_handle_type_mismatch'|
obj/Debug/EntangleApp.o:/home/ilya/Projects/Entangle/EntangleFrame.h|32|more undefined references to `__ubsan_handle_type_mismatch' follow|
obj/Debug/EntangleApp.o||In function `std::_Vector_base<unsigned long long, std::allocator<unsigned long long> >::~_Vector_base()':|
/usr/include/c++/4.9/bits/stl_vector.h|161|undefined reference to `__ubsan_handle_sub_overflow'|
/usr/include/c++/4.9/bits/stl_vector.h|161|undefined reference to `__ubsan_handle_type_mismatch'|
obj/Debug/EntangleApp.o||In function `std::_Vector_base<unsigned long long, std::allocator<unsigned long long> >::_Vector_impl::_Vector_impl()':|
/usr/include/c++/4.9/bits/stl_vector.h|87|undefined reference to `__ubsan_handle_type_mismatch'|
/usr/include/c++/4.9/bits/stl_vector.h|87|undefined reference to `__ubsan_handle_type_mismatch'|
||More errors follow but not being shown.|
||Edit the max errors limit in compiler options...|
||=== Build failed: 50 error(s), 0 warning(s) (0 minute(s), 23 second(s)) ===|

How can I fix this?

2条回答
倾城 Initia
2楼-- · 2019-01-26 05:56

I have installed libubsan0 and lib64ubsan0 and added -lubsan option, and my application compiled successfully!

查看更多
男人必须洒脱
3楼-- · 2019-01-26 06:23

The very same article you cited, as seen at its new home, already provides the answer, but you didn't notice it... bold mine:

GCC recently (version 4.9) gained Undefined Behavior Sanitizer (ubsan), a run-time checker for the C and C++ languages. In order to check your program with ubsan, compile and link the program with -fsanitize=undefined option.

I think the correct way to solve this is not to manually link with ubsan, but instead to simply also pass -fsanitize=undefined to the linker, not just the compiler. Sure, reinventing the wheel might work for you, but it's not needed, and there might be a reason that there is a specific linker flag for this, instead.

Certainly, that works: I was suddenly getting piles of undefined reference errors and, after giving up and just not using it for months, yugr's answer here pointed me in the right direction: I was only passing -fsanitize=undefined to meson.add_project_arguments(), but those only go to the compiler; I also needed to pass it to meson.add_project_link_arguments(). After adding that, the errors are gone, and the program links.

查看更多
登录 后发表回答