1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

ASAN: Support coroutine context switching and stack tracing (#4153)

For coroutine, we should use `__sanitizer_start_switch_fiber` which
similar to`VALGRIND_STACK_REGISTER`, see
https://github.com/google/sanitizers/issues/189#issuecomment-1346243598
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>)
```

For primordial coroutine, 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.

---

Co-authored-by: john <hondaxiao@tencent.com>
This commit is contained in:
Winlin 2024-08-22 17:12:39 +08:00 committed by GitHub
parent 55610cf689
commit 8f48a0e2d1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 236 additions and 15 deletions

View file

@ -135,7 +135,7 @@ int st_poll(struct pollfd *pds, int npds, st_utime_t timeout)
}
void _st_vp_schedule(void)
void _st_vp_schedule(_st_thread_t *from)
{
_st_thread_t *thread;
@ -159,7 +159,7 @@ void _st_vp_schedule(void)
/* Resume the thread */
thread->state = _ST_ST_RUNNING;
_st_restore_context(thread);
_st_restore_context(from, thread);
}
@ -368,6 +368,11 @@ int st_thread_join(_st_thread_t *thread, void **retvalp)
void _st_thread_main(void)
{
_st_thread_t *thread = _st_this_thread;
#ifdef MD_ASAN
/* Switch from other thread to this new created thread. */
_st_asan_finish_switch(thread);
#endif
/*
* Cap the stack by zeroing out the saved return address register
@ -572,7 +577,7 @@ void _st_vp_check_clock(void)
/* Make thread runnable */
ST_ASSERT(!(thread->flags & _ST_FL_IDLE_THREAD));
thread->state = _ST_ST_RUNNABLE;
// Insert at the head of RunQ, to execute timer first.
/* Insert at the head of RunQ, to execute timer first. */
st_clist_insert_after(&thread->links, &_st_this_vp.run_q);
}
}
@ -589,7 +594,7 @@ void st_thread_yield()
/* Check sleep queue for expired threads */
_st_vp_check_clock();
// If not thread in RunQ to yield to, ignore and continue to run.
/* If not thread in RunQ to yield to, ignore and continue to run. */
if (_st_this_vp.run_q.next == &_st_this_vp.run_q) {
return;
}
@ -598,11 +603,11 @@ void st_thread_yield()
++_st_stat_thread_yield2;
#endif
// Append thread to the tail of RunQ, we will back after all threads executed.
/* Append thread to the tail of RunQ, we will back after all threads executed. */
me->state = _ST_ST_RUNNABLE;
st_clist_insert_before(&me->links, &_st_this_vp.run_q);
// Yield to other threads in the RunQ.
/* Yield to other threads in the RunQ. */
_st_switch_context(me);
}
@ -664,8 +669,9 @@ _st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, int joinabl
thread->arg = arg;
/* Note that we must directly call rather than call any functions. */
if (_st_md_cxt_save(thread->context))
if (_st_md_cxt_save(thread->context)) {
_st_thread_main();
}
MD_GET_SP(thread) = (long)(stack->sp);
/* If thread is joinable, allocate a termination condition variable */