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

Store to misaligned address in REPLACE_NEAR macro #21

Open
jiridanek opened this issue Dec 25, 2021 · 1 comment
Open

Store to misaligned address in REPLACE_NEAR macro #21

jiridanek opened this issue Dec 25, 2021 · 1 comment
Labels

Comments

@jiridanek
Copy link
Contributor

I am using amd64 architecture. I enabled AddressSanitizer and UndefinedBehavior sanitizer in GCC. My tests produce the following undefined behavior error:

cpp_stub.h:244:13: runtime error: store to misaligned address 0x0000004bc2e1 for type 'int', which requires 4 byte alignment
0x0000004bc2e1: note: pointer points here
 fd ff ff  e9 25 8a 3e 69 00 68 2b  00 00 00 e9 30 fd ff ff  ff 25 82 3e 69 00 68 2c  00 00 00 e9 20
              ^ 
pc_0x9534d7###func_void Stub::set<int (*)(char*, unsigned long, char const*, __va_list_tag*), int (*)(char*, unsigned long, char const*, ...)>(int (*)(char*, unsigned long, char const*, __va_list_tag*), int (*)(char*, unsigned long, char const*, ...))###file_/home/jdanek/repos/qpid/qpid-dispatch/tests/cpp-stub/cpp_stub.h###line_244###obj_(c_unittests+0x9534d7)
pc_0x94cb4b###func__DOCTEST_ANON_FUNC_2###file_/home/jdanek/repos/qpid/qpid-dispatch/tests/c_unittests/test_terminus.cpp###line_88###obj_(c_unittests+0x94cb4b)
pc_0x7fc0ce###func_doctest::Context::run()###file_/home/jdanek/repos/qpid/qpid-dispatch/tests/c_unittests/doctest.h###line_6486###obj_(c_unittests+0x7fc0ce)
pc_0x7ff7c0###func_main###file_/home/jdanek/repos/qpid/qpid-dispatch/tests/c_unittests/doctest.h###line_6571###obj_(c_unittests+0x7ff7c0)
pc_0x7f11e90db55f###func___libc_start_call_main###file_<null>###line_0###obj_(libc.so.6+0x2d55f)
pc_0x7f11e90db60b###func___libc_start_main_impl###file_<null>###line_0###obj_(libc.so.6+0x2d60b)
pc_0x4bec64###func__start###file_<null>###line_0###obj_(c_unittests+0x4bec64)

The stub.cpp code is

REPLACE_NEAR(this, fn, fn_stub);

The macro is

    //5 byte(jmp rel32)
    #define REPLACE_NEAR(t, fn, fn_stub)\
        *fn = 0xE9;\
        *(int *)(fn + 1) = (int)(fn_stub - fn - CODESIZE_MIN);\
        //CACHEFLUSH((char *)fn, CODESIZE);

Looking into code, there is more statements suffering from this, e.g. *(long long *)(fn + 2) = (long long)fn_stub;\ in the same file.

For a solution, I am thinking that std::copy or memcpy should solve this, but it feels more clumsy than the current code and it may need helper variable.

jiridanek added a commit to jiridanek/qpid-dispatch that referenced this issue Dec 25, 2021
@coolxv
Copy link
Owner

coolxv commented Dec 28, 2021

@jiridanek

    //5 byte(jmp rel32)
    #define REPLACE_NEAR(t, fn, fn_stub)\
        *fn = 0xE9;\
        int an = (int)(fn_stub - fn - CODESIZE_MIN);\
        *(fn + 1) = ((char*)&an)[0];\
        *(fn + 2) = ((char*)&an)[1];\
        *(fn + 3) = ((char*)&an)[2];\
        *(fn + 4) = ((char*)&an)[3];\
        //CACHEFLUSH((char *)fn, CODESIZE);

@coolxv coolxv added the bug label Dec 28, 2021
jiridanek added a commit to jiridanek/qpid-dispatch that referenced this issue Jan 4, 2022
jiridanek added a commit to jiridanek/qpid-dispatch that referenced this issue Jan 7, 2022
jiridanek added a commit to jiridanek/qpid-dispatch that referenced this issue Jan 13, 2022
jiridanek added a commit to jiridanek/qpid-dispatch that referenced this issue Jan 29, 2022
jiridanek added a commit to jiridanek/qpid-dispatch that referenced this issue Jan 29, 2022
jiridanek added a commit to jiridanek/qpid-dispatch that referenced this issue Jan 29, 2022
jiridanek added a commit to jiridanek/qpid-dispatch that referenced this issue Jan 29, 2022
jiridanek added a commit to jiridanek/qpid-dispatch that referenced this issue Jan 29, 2022
jiridanek added a commit to jiridanek/qpid-dispatch that referenced this issue Jan 29, 2022
jiridanek added a commit to jiridanek/qpid-dispatch that referenced this issue Jan 29, 2022
jiridanek added a commit to apache/qpid-dispatch that referenced this issue Jan 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants