Skip to content

Commit fd16d46

Browse files
committed
[NFC][asan] Replace start_routine_ and arg_ with opaque start_data_ field
start_data_ is platform specific. Reviewed By: kstoimenov Differential Revision: https://reviews.llvm.org/D156298
1 parent 9eb73f9 commit fd16d46

File tree

5 files changed

+45
-20
lines changed

5 files changed

+45
-20
lines changed

compiler-rt/lib/asan/asan_interceptors.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,8 @@ static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
223223
auto self = GetThreadSelf();
224224
auto args = asanThreadArgRetval().GetArgs(self);
225225
t->ThreadStart(GetTid());
226-
thread_return_t retval = t->RunThread();
226+
thread_return_t retval = (*args.routine)(args.arg_retval);
227227
asanThreadArgRetval().Finish(self, retval);
228-
CHECK_EQ(args.arg_retval, t->get_arg());
229228
return retval;
230229
}
231230

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

245244
u32 current_tid = GetCurrentTidOrInvalid();
246-
AsanThread *t =
247-
AsanThread::Create(start_routine, arg, current_tid, &stack, detached);
245+
AsanThread *t = AsanThread::Create(current_tid, &stack, detached);
248246

249247
int result;
250248
{

compiler-rt/lib/asan/asan_mac.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,7 @@ ALWAYS_INLINE
154154
void asan_register_worker_thread(int parent_tid, StackTrace *stack) {
155155
AsanThread *t = GetCurrentThread();
156156
if (!t) {
157-
t = AsanThread::Create(/* start_routine */ nullptr, /* arg */ nullptr,
158-
parent_tid, stack, /* detached */ true);
157+
t = AsanThread::Create(parent_tid, stack, /* detached */ true);
159158
t->Init();
160159
asanThreadRegistry().StartThread(t->tid(), GetTid(), ThreadType::Worker,
161160
nullptr);

compiler-rt/lib/asan/asan_thread.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,27 @@ AsanThreadContext *GetThreadContextByTidLocked(u32 tid) {
9191

9292
// AsanThread implementation.
9393

94-
AsanThread *AsanThread::Create(thread_callback_t start_routine, void *arg,
94+
AsanThread *AsanThread::Create(const void *start_data, uptr data_size,
9595
u32 parent_tid, StackTrace *stack,
9696
bool detached) {
9797
uptr PageSize = GetPageSizeCached();
9898
uptr size = RoundUpTo(sizeof(AsanThread), PageSize);
9999
AsanThread *thread = (AsanThread *)MmapOrDie(size, __func__);
100-
thread->start_routine_ = start_routine;
101-
thread->arg_ = arg;
100+
if (data_size) {
101+
uptr availible_size = (uptr)thread + size - (uptr)(thread->start_data_);
102+
CHECK_LE(data_size, availible_size);
103+
internal_memcpy(thread->start_data_, start_data, data_size);
104+
}
102105
AsanThreadContext::CreateThreadContextArgs args = {thread, stack};
103106
asanThreadRegistry().CreateThread(0, detached, parent_tid, &args);
104107

105108
return thread;
106109
}
107110

111+
void AsanThread::GetStartData(void *out, uptr out_size) const {
112+
internal_memcpy(out, start_data_, out_size);
113+
}
114+
108115
void AsanThread::TSDDtor(void *tsd) {
109116
AsanThreadContext *context = (AsanThreadContext *)tsd;
110117
VReport(1, "T%d TSDDtor\n", context->tid);
@@ -281,11 +288,9 @@ void AsanThread::ThreadStart(tid_t os_id) {
281288
SetAlternateSignalStack();
282289
}
283290

284-
thread_return_t AsanThread::RunThread() { return start_routine_(arg_); }
285-
286291
AsanThread *CreateMainThread() {
287292
AsanThread *main_thread = AsanThread::Create(
288-
/* start_routine */ nullptr, /* arg */ nullptr, /* parent_tid */ kMainTid,
293+
/* parent_tid */ kMainTid,
289294
/* stack */ nullptr, /* detached */ true);
290295
SetCurrentThread(main_thread);
291296
main_thread->ThreadStart(internal_getpid());

compiler-rt/lib/asan/asan_thread.h

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,14 @@ COMPILER_CHECK(sizeof(AsanThreadContext) <= 256);
5959
// AsanThread are stored in TSD and destroyed when the thread dies.
6060
class AsanThread {
6161
public:
62-
static AsanThread *Create(thread_callback_t start_routine, void *arg,
63-
u32 parent_tid, StackTrace *stack, bool detached);
62+
template <typename T>
63+
static AsanThread *Create(const T &data, u32 parent_tid, StackTrace *stack,
64+
bool detached) {
65+
return Create(&data, sizeof(data), parent_tid, stack, detached);
66+
}
67+
static AsanThread *Create(u32 parent_tid, StackTrace *stack, bool detached) {
68+
return Create(nullptr, 0, parent_tid, stack, detached);
69+
}
6470
static void TSDDtor(void *tsd);
6571
void Destroy();
6672

@@ -131,12 +137,18 @@ class AsanThread {
131137

132138
void *extra_spill_area() { return &extra_spill_area_; }
133139

134-
void *get_arg() const { return arg_; }
140+
template <typename T>
141+
void GetStartData(T &data) const {
142+
GetStartData(&data, sizeof(data));
143+
}
135144

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

149+
static AsanThread *Create(const void *start_data, uptr data_size,
150+
u32 parent_tid, StackTrace *stack, bool detached);
151+
140152
void SetThreadStackAndTls(const InitOptions *options);
141153

142154
void ClearShadowForThreadStackAndTLS();
@@ -148,9 +160,9 @@ class AsanThread {
148160
};
149161
StackBounds GetStackBounds() const;
150162

163+
void GetStartData(void *out, uptr out_size) const;
164+
151165
AsanThreadContext *context_;
152-
thread_callback_t start_routine_;
153-
void *arg_;
154166

155167
uptr stack_top_;
156168
uptr stack_bottom_;
@@ -169,6 +181,8 @@ class AsanThread {
169181
AsanStats stats_;
170182
bool unwinding_;
171183
uptr extra_spill_area_;
184+
185+
char start_data_[];
172186
};
173187

174188
// Returns a single instance of registry.

compiler-rt/lib/asan/asan_win.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,20 @@ INTERCEPTOR(int, _except_handler4, void *a, void *b, void *c, void *d) {
131131
}
132132
#endif
133133

134+
struct ThreadStartParams {
135+
thread_callback_t start_routine;
136+
void *arg;
137+
};
138+
134139
static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
135140
AsanThread *t = (AsanThread *)arg;
136141
SetCurrentThread(t);
137142
t->ThreadStart(GetTid());
138-
auto res = t->RunThread();
143+
144+
ThreadStartParams params;
145+
t->GetStartData(params);
146+
147+
auto res = (*params.start_routine)(params.arg);
139148
t->Destroy(); // POSIX calls this from TSD destructor.
140149
return res;
141150
}
@@ -151,8 +160,8 @@ INTERCEPTOR_WINAPI(HANDLE, CreateThread, LPSECURITY_ATTRIBUTES security,
151160
// one. This is a bandaid fix for PR22025.
152161
bool detached = false; // FIXME: how can we determine it on Windows?
153162
u32 current_tid = GetCurrentTidOrInvalid();
154-
AsanThread *t =
155-
AsanThread::Create(start_routine, arg, current_tid, &stack, detached);
163+
ThreadStartParams params = {start_routine, arg};
164+
AsanThread *t = AsanThread::Create(params, current_tid, &stack, detached);
156165
return REAL(CreateThread)(security, stack_size, asan_thread_start, t,
157166
thr_flags, tid);
158167
}

0 commit comments

Comments
 (0)