Skip to content

Commit

Permalink
[NFC][asan] Replace start_routine_ and arg_ with opaque start_data_ f…
Browse files Browse the repository at this point in the history
…ield

start_data_ is platform specific.

Reviewed By: kstoimenov

Differential Revision: https://reviews.llvm.org/D156298
  • Loading branch information
vitalybuka committed Jul 26, 2023
1 parent 9eb73f9 commit fd16d46
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 20 deletions.
6 changes: 2 additions & 4 deletions compiler-rt/lib/asan/asan_interceptors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,8 @@ static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
auto self = GetThreadSelf();
auto args = asanThreadArgRetval().GetArgs(self);
t->ThreadStart(GetTid());
thread_return_t retval = t->RunThread();
thread_return_t retval = (*args.routine)(args.arg_retval);
asanThreadArgRetval().Finish(self, retval);
CHECK_EQ(args.arg_retval, t->get_arg());
return retval;
}

Expand All @@ -243,8 +242,7 @@ INTERCEPTOR(int, pthread_create, void *thread, void *attr,
}();

u32 current_tid = GetCurrentTidOrInvalid();
AsanThread *t =
AsanThread::Create(start_routine, arg, current_tid, &stack, detached);
AsanThread *t = AsanThread::Create(current_tid, &stack, detached);

int result;
{
Expand Down
3 changes: 1 addition & 2 deletions compiler-rt/lib/asan/asan_mac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ ALWAYS_INLINE
void asan_register_worker_thread(int parent_tid, StackTrace *stack) {
AsanThread *t = GetCurrentThread();
if (!t) {
t = AsanThread::Create(/* start_routine */ nullptr, /* arg */ nullptr,
parent_tid, stack, /* detached */ true);
t = AsanThread::Create(parent_tid, stack, /* detached */ true);
t->Init();
asanThreadRegistry().StartThread(t->tid(), GetTid(), ThreadType::Worker,
nullptr);
Expand Down
17 changes: 11 additions & 6 deletions compiler-rt/lib/asan/asan_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,27 @@ AsanThreadContext *GetThreadContextByTidLocked(u32 tid) {

// AsanThread implementation.

AsanThread *AsanThread::Create(thread_callback_t start_routine, void *arg,
AsanThread *AsanThread::Create(const void *start_data, uptr data_size,
u32 parent_tid, StackTrace *stack,
bool detached) {
uptr PageSize = GetPageSizeCached();
uptr size = RoundUpTo(sizeof(AsanThread), PageSize);
AsanThread *thread = (AsanThread *)MmapOrDie(size, __func__);
thread->start_routine_ = start_routine;
thread->arg_ = arg;
if (data_size) {
uptr availible_size = (uptr)thread + size - (uptr)(thread->start_data_);
CHECK_LE(data_size, availible_size);
internal_memcpy(thread->start_data_, start_data, data_size);
}
AsanThreadContext::CreateThreadContextArgs args = {thread, stack};
asanThreadRegistry().CreateThread(0, detached, parent_tid, &args);

return thread;
}

void AsanThread::GetStartData(void *out, uptr out_size) const {
internal_memcpy(out, start_data_, out_size);
}

void AsanThread::TSDDtor(void *tsd) {
AsanThreadContext *context = (AsanThreadContext *)tsd;
VReport(1, "T%d TSDDtor\n", context->tid);
Expand Down Expand Up @@ -281,11 +288,9 @@ void AsanThread::ThreadStart(tid_t os_id) {
SetAlternateSignalStack();
}

thread_return_t AsanThread::RunThread() { return start_routine_(arg_); }

AsanThread *CreateMainThread() {
AsanThread *main_thread = AsanThread::Create(
/* start_routine */ nullptr, /* arg */ nullptr, /* parent_tid */ kMainTid,
/* parent_tid */ kMainTid,
/* stack */ nullptr, /* detached */ true);
SetCurrentThread(main_thread);
main_thread->ThreadStart(internal_getpid());
Expand Down
24 changes: 19 additions & 5 deletions compiler-rt/lib/asan/asan_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,14 @@ COMPILER_CHECK(sizeof(AsanThreadContext) <= 256);
// AsanThread are stored in TSD and destroyed when the thread dies.
class AsanThread {
public:
static AsanThread *Create(thread_callback_t start_routine, void *arg,
u32 parent_tid, StackTrace *stack, bool detached);
template <typename T>
static AsanThread *Create(const T &data, u32 parent_tid, StackTrace *stack,
bool detached) {
return Create(&data, sizeof(data), parent_tid, stack, detached);
}
static AsanThread *Create(u32 parent_tid, StackTrace *stack, bool detached) {
return Create(nullptr, 0, parent_tid, stack, detached);
}
static void TSDDtor(void *tsd);
void Destroy();

Expand Down Expand Up @@ -131,12 +137,18 @@ class AsanThread {

void *extra_spill_area() { return &extra_spill_area_; }

void *get_arg() const { return arg_; }
template <typename T>
void GetStartData(T &data) const {
GetStartData(&data, sizeof(data));
}

private:
// NOTE: There is no AsanThread constructor. It is allocated
// via mmap() and *must* be valid in zero-initialized state.

static AsanThread *Create(const void *start_data, uptr data_size,
u32 parent_tid, StackTrace *stack, bool detached);

void SetThreadStackAndTls(const InitOptions *options);

void ClearShadowForThreadStackAndTLS();
Expand All @@ -148,9 +160,9 @@ class AsanThread {
};
StackBounds GetStackBounds() const;

void GetStartData(void *out, uptr out_size) const;

AsanThreadContext *context_;
thread_callback_t start_routine_;
void *arg_;

uptr stack_top_;
uptr stack_bottom_;
Expand All @@ -169,6 +181,8 @@ class AsanThread {
AsanStats stats_;
bool unwinding_;
uptr extra_spill_area_;

char start_data_[];
};

// Returns a single instance of registry.
Expand Down
15 changes: 12 additions & 3 deletions compiler-rt/lib/asan/asan_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,20 @@ INTERCEPTOR(int, _except_handler4, void *a, void *b, void *c, void *d) {
}
#endif

struct ThreadStartParams {
thread_callback_t start_routine;
void *arg;
};

static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
AsanThread *t = (AsanThread *)arg;
SetCurrentThread(t);
t->ThreadStart(GetTid());
auto res = t->RunThread();

ThreadStartParams params;
t->GetStartData(params);

auto res = (*params.start_routine)(params.arg);
t->Destroy(); // POSIX calls this from TSD destructor.
return res;
}
Expand All @@ -151,8 +160,8 @@ INTERCEPTOR_WINAPI(HANDLE, CreateThread, LPSECURITY_ATTRIBUTES security,
// one. This is a bandaid fix for PR22025.
bool detached = false; // FIXME: how can we determine it on Windows?
u32 current_tid = GetCurrentTidOrInvalid();
AsanThread *t =
AsanThread::Create(start_routine, arg, current_tid, &stack, detached);
ThreadStartParams params = {start_routine, arg};
AsanThread *t = AsanThread::Create(params, current_tid, &stack, detached);
return REAL(CreateThread)(security, stack_size, asan_thread_start, t,
thr_flags, tid);
}
Expand Down

0 comments on commit fd16d46

Please sign in to comment.