Skip to content

Commit ea76d3f

Browse files
committed
rt: Add rust_task::call_on_c_stack
1 parent 1e2fe4a commit ea76d3f

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

src/rt/rust_task.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ void task_start_wrapper(spawn_args *a)
194194

195195
// The cleanup work needs lots of stack
196196
cleanup_args ca = {a, threw_exception};
197-
task->thread->c_context.call_and_change_stacks(&ca, (void*)cleanup_task);
197+
task->call_on_c_stack(&ca, (void*)cleanup_task);
198198

199199
task->ctx.next->swap(task->ctx);
200200
}
@@ -699,7 +699,18 @@ Returns true if we're currently running on the Rust stack
699699
*/
700700
bool
701701
rust_task::on_rust_stack() {
702-
return sp_in_stk_seg(get_sp(), stk);
702+
uintptr_t sp = get_sp();
703+
bool in_first_segment = sp_in_stk_seg(sp, stk);
704+
if (in_first_segment) {
705+
return true;
706+
} else if (stk->next != NULL) {
707+
// This happens only when calling the upcall to delete
708+
// a stack segment
709+
bool in_second_segment = sp_in_stk_seg(sp, stk->next);
710+
return in_second_segment;
711+
} else {
712+
return false;
713+
}
703714
}
704715

705716
void
@@ -713,6 +724,12 @@ rust_task::config_notify(chan_handle chan) {
713724
notify_chan = chan;
714725
}
715726

727+
void
728+
rust_task::call_on_c_stack(void *args, void *fn_ptr) {
729+
I(thread, on_rust_stack());
730+
thread->c_context.call_and_change_stacks(args, fn_ptr);
731+
}
732+
716733
//
717734
// Local Variables:
718735
// mode: C++

src/rt/rust_task.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ rust_task : public kernel_owned<rust_task>, rust_cond
181181
void check_stack_canary();
182182

183183
void config_notify(chan_handle chan);
184+
185+
void call_on_c_stack(void *args, void *fn_ptr);
184186
};
185187

186188
//

src/rt/rust_upcall.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ inline void
4747
call_upcall_on_c_stack(void *args, void *fn_ptr) {
4848
check_stack_alignment();
4949
rust_task *task = rust_task_thread::get_task();
50-
rust_task_thread *thread = task->thread;
51-
thread->c_context.call_and_change_stacks(args, fn_ptr);
50+
task->call_on_c_stack(args, fn_ptr);
5251
}
5352

5453
extern "C" void record_sp(void *limit);
@@ -69,11 +68,10 @@ upcall_call_shim_on_c_stack(void *args, void *fn_ptr) {
6968
// stack.
7069
record_sp(0);
7170

72-
rust_task_thread *thread = task->thread;
7371
try {
74-
thread->c_context.call_and_change_stacks(args, fn_ptr);
72+
task->call_on_c_stack(args, fn_ptr);
7573
} catch (...) {
76-
A(thread, false, "Native code threw an exception");
74+
A(task->thread, false, "Native code threw an exception");
7775
}
7876

7977
task = rust_task_thread::get_task();

0 commit comments

Comments
 (0)