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

ERROR: LeakSanitizer: detected memory leaks for daemon #3839

Closed
eanfs opened this issue Oct 16, 2023 · 2 comments · Fixed by #3840, #4153 or #4154
Closed

ERROR: LeakSanitizer: detected memory leaks for daemon #3839

eanfs opened this issue Oct 16, 2023 · 2 comments · Fixed by #3840, #4153 or #4154
Assignees
Labels
TransByAI Translated by AI/GPT.

Comments

@eanfs
Copy link

eanfs commented Oct 16, 2023

Describe the bug
Failed to start SRS v5 on Docker

[2023-10-16 04:28:14.306][INFO][1][65m469r8] XCORE-SRS/5.0.185(Bee)
[2023-10-16 04:28:14.306][INFO][1][65m469r8] config parse complete
[2023-10-16 04:28:14.306][INFO][1][65m469r8] you can check log by: tail -n 30 -f /var/log/srs/srs.log
[2023-10-16 04:28:14.306][INFO][1][65m469r8] please check SRS by: ./etc/init.d/srs status

=================================================================
==1==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 40 byte(s) in 1 object(s) allocated from:
    #0 0x560501163106 in __interceptor_calloc (/usr/local/srs/objs/srs+0x4b9106)
    #1 0x560501837fb1 in _st_epoll_init /srs/trunk/objs/Platform-SRS5-Linux-5.15.0-GCC9.4.0-x86_64/st-srs/event.c:877
    #2 0x56050183308c in st_init /srs/trunk/objs/Platform-SRS5-Linux-5.15.0-GCC9.4.0-x86_64/st-srs/sched.c:197
    #3 0x560501388ec8 in srs_st_init() src/protocol/srs_protocol_st.cpp:68
    #4 0x5605016be4aa in SrsThreadPool::setup_thread_locals() src/app/srs_app_threads.cpp:561
    #5 0x5605016c04f1 in SrsThreadPool::start(void*) src/app/srs_app_threads.cpp:776
    #6 0x7f9fa4f7e608 in start_thread (/lib/x86_64-linux-gnu/libpthread.so.0+0x8608)

Indirect leak of 65536 byte(s) in 1 object(s) allocated from:
    #0 0x560501163106 in __interceptor_calloc (/usr/local/srs/objs/srs+0x4b9106)
    #1 0x560501838099 in _st_epoll_init /srs/trunk/objs/Platform-SRS5-Linux-5.15.0-GCC9.4.0-x86_64/st-srs/event.c:893
    #2 0x56050183308c in st_init /srs/trunk/objs/Platform-SRS5-Linux-5.15.0-GCC9.4.0-x86_64/st-srs/sched.c:197
    #3 0x560501388ec8 in srs_st_init() src/protocol/srs_protocol_st.cpp:68
    #4 0x5605016be4aa in SrsThreadPool::setup_thread_locals() src/app/srs_app_threads.cpp:561
    #5 0x5605016c04f1 in SrsThreadPool::start(void*) src/app/srs_app_threads.cpp:776
    #6 0x7f9fa4f7e608 in start_thread (/lib/x86_64-linux-gnu/libpthread.so.0+0x8608)

Indirect leak of 49152 byte(s) in 1 object(s) allocated from:
    #0 0x560501162f08 in __interceptor_malloc (/usr/local/srs/objs/srs+0x4b8f08)
    #1 0x560501838105 in _st_epoll_init /srs/trunk/objs/Platform-SRS5-Linux-5.15.0-GCC9.4.0-x86_64/st-srs/event.c:902
    #2 0x56050183308c in st_init /srs/trunk/objs/Platform-SRS5-Linux-5.15.0-GCC9.4.0-x86_64/st-srs/sched.c:197
    #3 0x560501388ec8 in srs_st_init() src/protocol/srs_protocol_st.cpp:68
    #4 0x5605016be4aa in SrsThreadPool::setup_thread_locals() src/app/srs_app_threads.cpp:561
    #5 0x5605016c04f1 in SrsThreadPool::start(void*) src/app/srs_app_threads.cpp:776
    #6 0x7f9fa4f7e608 in start_thread (/lib/x86_64-linux-gnu/libpthread.so.0+0x8608)

SUMMARY: AddressSanitizer: 114728 byte(s) leaked in 3 allocation(s).
image **Version** docker srs [v5.0.185]

TRANS_BY_GPT4

@winlinvip winlinvip added the TransByAI Translated by AI/GPT. label Oct 16, 2023
@winlinvip winlinvip linked a pull request Oct 16, 2023 that will close this issue
@winlinvip winlinvip reopened this Aug 21, 2024
@winlinvip winlinvip self-assigned this Aug 21, 2024
@winlinvip winlinvip changed the title ERROR: LeakSanitizer: detected memory leaks ERROR: LeakSanitizer: detected memory leaks for daemon Aug 21, 2024
@winlinvip
Copy link
Member

winlinvip commented Aug 22, 2024

For coroutine, we should use __sanitizer_start_switch_fiber which similar toVALGRIND_STACK_REGISTER, see https://www.boost.org/doc/libs/1_78_0/boost/context/fiber_ucontext.hpp and dosemu2/dosemu2@148cc5b and google/sanitizers#189 for details. If not fix this, asan will output warning:

==72269==WARNING: ASan is ignoring requested __asan_handle_no_return: stack type: default top: 0x00016f638000; bottom 0x000106bec000; size: 0x000068a4c000 (1755627520)
False positive error reports may follow
For details see https://github.com/google/sanitizers/issues/189

It will cause asan failed to get the stack, see research/st/asan-switch.cpp for example:

==71611==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x000103600733 at pc 0x0001009d3d7c bp 0x000100b4bd40 sp 0x000100b4bd38
WRITE of size 1 at 0x000103600733 thread T0
    #0 0x1009d3d78 in foo(void*) asan-switch.cpp:13

After fix this issue, it should provide the full stack when crashing:

==73437==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x000103300733 at pc 0x000100693d7c bp 0x00016f76f550 sp 0x00016f76f548
WRITE of size 1 at 0x000103300733 thread T0
    #0 0x100693d78 in foo(void*) asan-switch.cpp:13
    #1 0x100693df4 in main asan-switch.cpp:23
    #2 0x195aa20dc  (<unknown module>)

See google/sanitizers#189 (comment) we should call the asan API correctly.

For the main or primordial coroutine, we should detect the stack pointer, like this:

#include <sys/resource.h>
int main(int argc, char **argv) {
    register void* stack_top asm ("sp");
    struct rlimit limit;
    if (getrlimit (RLIMIT_STACK, &limit) == 0) {
        void* stack_bottom = (char*)stack_top - limit.rlim_cur;
        st_set_primordial_stack(stack_top, stack_bottom);
    }

If not set the stack by st_set_primordial_stack, then the stack is NULL and asan can't get the stack tracing. Note that it's optional and only make it fail to display the stack information, no other errors.

@winlinvip
Copy link
Member

winlinvip commented Aug 22, 2024

By setting the env ASAN_OPTIONS=halt_on_error=0, we can ignore memory leaks, see https://github.com/google/sanitizers/wiki/AddressSanitizerFlags

By setting env ASAN_OPTIONS=detect_leaks=0, we can disable memory leaking detection in parent process when forking for daemon.

In order to set the default asan option, we need to provide a function:

// This function return the default options for asan, before main() is called,
// see https://github.com/google/sanitizers/wiki/AddressSanitizerFlags#run-time-flags
//
// Disable halt on errors by halt_on_error, only print messages, note that it still quit for fatal errors,
// see https://github.com/google/sanitizers/wiki/AddressSanitizerFlags
//
// Disable the memory leaking detect for daemon by detect_leaks,
// see https://github.com/google/sanitizers/wiki/SanitizerCommonFlags
//
// Also disable alloc_dealloc_mismatch for gdb.
extern "C" const char *__asan_default_options() {
    return "halt_on_error=0:detect_leaks=0:alloc_dealloc_mismatch=0";
}

Should never set the env in main(), because asan will load the options before call main().

@winlinvip winlinvip reopened this Aug 22, 2024
@winlinvip winlinvip linked a pull request Aug 22, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
TransByAI Translated by AI/GPT.
Projects
None yet
2 participants