Skip to content

Commit

Permalink
fix uthread test cases in asan
Browse files Browse the repository at this point in the history
The previous implementation of __sanitizer_start_switch_fiber and
__sanitizer_finish_switch_fiber. It assigned the top  instead of the
bottom of the user thread. Also we need to assign the old stack to the
__sanitizer_finish_switch_fiber instead of the new stack.

This patch fixes these two issues.
  • Loading branch information
ChuanqiXu9 committed Nov 23, 2023
1 parent cd12c24 commit de19827
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
24 changes: 13 additions & 11 deletions async_simple/uthread/internal/thread.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@ void __sanitizer_finish_switch_fiber(void* fake_stack_save,
size_t* stack_size_old);
}

inline void start_switch_fiber(jmp_buf_link* context) {
__sanitizer_start_switch_fiber(&context->asan_fake_stack,
inline void start_switch_fiber(jmp_buf_link* context, void **stack) {
__sanitizer_start_switch_fiber(stack,
context->asan_stack_bottom,
context->asan_stack_size);
}

inline void finish_switch_fiber(jmp_buf_link* context) {
__sanitizer_finish_switch_fiber(context->asan_fake_stack,
inline void finish_switch_fiber(jmp_buf_link* context, void *stack) {
__sanitizer_finish_switch_fiber(stack,
&context->asan_stack_bottom,
&context->asan_stack_size);
}

#else

inline void start_switch_fiber(jmp_buf_link* context) {}
inline void finish_switch_fiber(jmp_buf_link* context) {}
inline void start_switch_fiber(jmp_buf_link* context, void *stack) {}
inline void finish_switch_fiber(jmp_buf_link* context, void *stack) {}

#endif // AS_INTERNAL_USE_ASAN

Expand Down Expand Up @@ -85,17 +85,19 @@ inline void jmp_buf_link::switch_in() {
link = std::exchange(g_current_context, this);
if (!link)
AS_UNLIKELY { link = &g_unthreaded_context; }
start_switch_fiber(this);
void *stack_addr = nullptr;
start_switch_fiber(this, &stack_addr);
// `thread` is currently only used in `s_main`
fcontext = _fl_jump_fcontext(fcontext, thread).fctx;
finish_switch_fiber(this);
finish_switch_fiber(link, stack_addr);
}

inline void jmp_buf_link::switch_out() {
g_current_context = link;
start_switch_fiber(link);
void *stack_addr = nullptr;
start_switch_fiber(link, &stack_addr);
link->fcontext = _fl_jump_fcontext(link->fcontext, thread).fctx;
finish_switch_fiber(link);
finish_switch_fiber(this, stack_addr);
}

inline void jmp_buf_link::initial_switch_in_completed() {
Expand Down Expand Up @@ -143,7 +145,7 @@ void thread_context::setup() {
stack_size_, thread_context::s_main);
context_.thread = this;
#ifdef AS_INTERNAL_USE_ASAN
context_.asan_stack_bottom = stack_.get();
context_.asan_stack_bottom = stack_.get() + stack_size_;
context_.asan_stack_size = stack_size_;
#endif
context_.switch_in();
Expand Down
1 change: 0 additions & 1 deletion async_simple/uthread/internal/thread_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ struct jmp_buf_link {
#ifdef AS_INTERNAL_USE_ASAN
const void* asan_stack_bottom = nullptr;
std::size_t asan_stack_size = 0;
void* asan_fake_stack = nullptr;
#endif

public:
Expand Down

0 comments on commit de19827

Please sign in to comment.