Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash (SEGV) when used with UndefinedBehaviorSanitizer #117

Open
hexagonrecursion opened this issue Aug 4, 2024 · 0 comments
Open

Crash (SEGV) when used with UndefinedBehaviorSanitizer #117

hexagonrecursion opened this issue Aug 4, 2024 · 0 comments

Comments

@hexagonrecursion
Copy link

UndefinedBehaviorSanitizer is a very handy tool that helped me find and troubleshoot many bugs in C++ projects. Unfortunately hippomocks appear to be incompatible with it.

main.cpp

#include "hippomocks.h"
#include <iostream>

class CSystemUtils {
public:
    virtual char* GetDataPath() {
        return "hello.txt";
    }
    virtual ~CSystemUtils() {}
};

int main(int, char**)
{
    MockRepository m_mocks;
    CSystemUtils *m_systemUtils = m_mocks.Mock<CSystemUtils>();
    m_mocks.OnCall(m_systemUtils, CSystemUtils::GetDataPath).Return("mocked.txt");
    std::cout << m_systemUtils->GetDataPath() << std::endl;
    return 0;
}

Compile

  1. Either clang++ -std=c++20 -fsanitize=undefined main.cpp
  2. of g++ -std=c++20 -fsanitize=undefined main.cpp

Expected

a.out prints mocked.txt when executed

Got

clang:

UndefinedBehaviorSanitizer:DEADLYSIGNAL
==42734==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000002021 (pc 0x7f75b7e9ac97 bp
 0x55d79bb42f80 sp 0x7ffc4c2cc900 T42734)
==42734==The signal is caused by a READ memory access.
    #0 0x7f75b7e9ac97 in __dynamic_cast (/lib/x86_64-linux-gnu/libstdc++.so.6+0xacc97) (BuildId: e37fe1a
879783838de78cbc8c80621fa685d58a2)
    #1 0x55d79bae5f4b in __ubsan::checkDynamicType(void*, void*, unsigned long) (/home/cdda/tmp/a.out+0x
4af4b) (BuildId: 35ce505127e6a2099c0cd91fb29ee70fe2925749)
    #2 0x55d79bae5092 in HandleDynamicTypeCacheMiss(__ubsan::DynamicTypeCacheMissData*, unsigned long, u
nsigned long, __ubsan::ReportOptions) ubsan_handlers_cxx.cpp.o
    #3 0x55d79bae505d in __ubsan_handle_dynamic_type_cache_miss (/home/cdda/tmp/a.out+0x4a05d) (BuildId:
 35ce505127e6a2099c0cd91fb29ee70fe2925749)
    #4 0x55d79bafa9cd in void HippoMocks::MockRepository::BasicRegisterExpect<CSystemUtils>(HippoMocks::
mock<CSystemUtils>*, int, int, void (HippoMocks::base_mock::*)(), int) (/home/cdda/tmp/a.out+0x5f9cd) (B
uildId: 35ce505127e6a2099c0cd91fb29ee70fe2925749)
    #5 0x55d79bae6a3d in HippoMocks::TCall<char*, HippoMocks::NullType, HippoMocks::NullType, HippoMocks
::NullType, HippoMocks::NullType, HippoMocks::NullType, HippoMocks::NullType, HippoMocks::NullType, Hipp
oMocks::NullType, HippoMocks::NullType, HippoMocks::NullType, HippoMocks::NullType, HippoMocks::NullType
, HippoMocks::NullType, HippoMocks::NullType, HippoMocks::NullType, HippoMocks::NullType>& HippoMocks::M
ockRepository::RegisterExpect_<16, CSystemUtils, char*, CSystemUtils>(CSystemUtils*, char* (CSystemUtils
::*)(), HippoMocks::RegistrationType, char const*, char const*, unsigned long) (/home/cdda/tmp/a.out+0x4
ba3d) (BuildId: 35ce505127e6a2099c0cd91fb29ee70fe2925749)
    #6 0x55d79bae62e1 in main (/home/cdda/tmp/a.out+0x4b2e1) (BuildId: 35ce505127e6a2099c0cd91fb29ee70fe
2925749)
    #7 0x7f75b7ae8d8f in __libc_start_call_main csu/../sysdeps/nptl/libc_start_call_main.h:58:16
    #8 0x7f75b7ae8e3f in __libc_start_main csu/../csu/libc-start.c:392:3
    #9 0x55d79babc664 in _start (/home/cdda/tmp/a.out+0x21664) (BuildId: 35ce505127e6a2099c0cd91fb29ee70
fe2925749)

UndefinedBehaviorSanitizer can not provide additional info.
SUMMARY: UndefinedBehaviorSanitizer: SEGV (/lib/x86_64-linux-gnu/libstdc++.so.6+0xacc97) (BuildId: e37fe
1a879783838de78cbc8c80621fa685d58a2) in __dynamic_cast
==42734==ABORTING

gcc:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7e31c97 in __dynamic_cast () from /lib/x86_64-linux-gnu/libstdc++.so.6
(gdb) bt
#0  0x00007ffff7e31c97 in __dynamic_cast () from /lib/x86_64-linux-gnu/libstdc++.so.6
#1  0x00007ffff76795ab in __ubsan::checkDynamicType (Object=Object@entry=0x5555555d0eb0, 
    Type=0x5555555a6aa0 <typeinfo for CSystemUtils>, Hash=4824472795596714896)
    at ../../../../src/libsanitizer/ubsan/ubsan_type_hash_itanium.cpp:232
#2  0x00007ffff76780d6 in HandleDynamicTypeCacheMiss (Data=0x5555555afec0, Pointer=93824992743088, Hash=<optimized out>, Opts=...)
    at ../../../../src/libsanitizer/ubsan/ubsan_handlers_cxx.cpp:36
#3  0x00007ffff767878f in __ubsan::__ubsan_handle_dynamic_type_cache_miss (Data=<optimized out>, Pointer=<optimized out>, 
    Hash=<optimized out>) at ../../../../src/libsanitizer/ubsan/ubsan_handlers_cxx.cpp:87
#4  0x0000555555575964 in main ()
(gdb) 

Versions Tested

master branch (10960d0)
v5.0 (dca4725)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant