aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKJ Tsanaktsidis <kj@kjtsanaktsidis.id.au>2024-02-24 19:32:17 +1100
committerKJ Tsanaktsidis <kj@kjtsanaktsidis.id.au>2024-03-25 14:57:04 +1100
commit2535a09e85dd68249d9f390db32313ddd482480e (patch)
treef9290f950abf34a82382ecbae8f92fa663deea4a
parent48d3bdddbaeabed5fb6a97bfbe65e250d1383a9c (diff)
downloadruby-2535a09e85dd68249d9f390db32313ddd482480e.tar.gz
Check ASAN fake stacks when marking non-current threads
Currently, we check the values on the machine stack & register state to see if they're actually a pointer to an ASAN fake stack, and mark the values on the fake stack too if required. However, we are only doing that for the _current_ thread (the one actually running the GC), not for any other thread in the program. Make rb_gc_mark_machine_context (which is called for marking non-current threads) perform the same ASAN fake stack handling that mark_current_machine_context performs. [Bug #20310]
-rw-r--r--gc.c20
-rw-r--r--vm.c5
-rw-r--r--vm_core.h2
3 files changed, 18 insertions, 9 deletions
diff --git a/gc.c b/gc.c
index 07c0735a44..282e9df0cf 100644
--- a/gc.c
+++ b/gc.c
@@ -949,7 +949,7 @@ typedef struct rb_objspace {
rb_postponed_job_handle_t finalize_deferred_pjob;
#ifdef RUBY_ASAN_ENABLED
- rb_execution_context_t *marking_machine_context_ec;
+ const rb_execution_context_t *marking_machine_context_ec;
#endif
} rb_objspace_t;
@@ -6406,7 +6406,7 @@ gc_mark_machine_stack_location_maybe(rb_objspace_t *objspace, VALUE obj)
gc_mark_maybe(objspace, obj);
#ifdef RUBY_ASAN_ENABLED
- rb_execution_context_t *ec = objspace->marking_machine_context_ec;
+ const rb_execution_context_t *ec = objspace->marking_machine_context_ec;
void *fake_frame_start;
void *fake_frame_end;
bool is_fake_frame = asan_get_fake_stack_extents(
@@ -6495,13 +6495,25 @@ mark_current_machine_context(rb_objspace_t *objspace, rb_execution_context_t *ec
#endif
void
-rb_gc_mark_machine_stack(const rb_execution_context_t *ec)
+rb_gc_mark_machine_context(const rb_execution_context_t *ec)
{
+ rb_objspace_t *objspace = &rb_objspace;
+#ifdef RUBY_ASAN_ENABLED
+ objspace->marking_machine_context_ec = ec;
+#endif
+
VALUE *stack_start, *stack_end;
+
GET_STACK_BOUNDS(stack_start, stack_end, 0);
RUBY_DEBUG_LOG("ec->th:%u stack_start:%p stack_end:%p", rb_ec_thread_ptr(ec)->serial, stack_start, stack_end);
- rb_gc_mark_locations(stack_start, stack_end);
+ each_stack_location(objspace, ec, stack_start, stack_end, gc_mark_machine_stack_location_maybe);
+ int num_regs = sizeof(ec->machine.regs)/(sizeof(VALUE));
+ each_location(objspace, (VALUE*)&ec->machine.regs, num_regs, gc_mark_machine_stack_location_maybe);
+
+#ifdef RUBY_ASAN_ENABLED
+ objspace->marking_machine_context_ec = NULL;
+#endif
}
static void
diff --git a/vm.c b/vm.c
index 82bf1ac658..882514db41 100644
--- a/vm.c
+++ b/vm.c
@@ -3399,10 +3399,7 @@ rb_execution_context_mark(const rb_execution_context_t *ec)
if (ec->machine.stack_start && ec->machine.stack_end &&
ec != GET_EC() /* marked for current ec at the first stage of marking */
) {
- rb_gc_mark_machine_stack(ec);
- rb_gc_mark_locations((VALUE *)&ec->machine.regs,
- (VALUE *)(&ec->machine.regs) +
- sizeof(ec->machine.regs) / (sizeof(VALUE)));
+ rb_gc_mark_machine_context(ec);
}
rb_gc_mark(ec->errinfo);
diff --git a/vm_core.h b/vm_core.h
index 805df18616..2a9d5f906f 100644
--- a/vm_core.h
+++ b/vm_core.h
@@ -1874,7 +1874,7 @@ void rb_vm_register_special_exception_str(enum ruby_special_exceptions sp, VALUE
#define rb_vm_register_special_exception(sp, e, m) \
rb_vm_register_special_exception_str(sp, e, rb_usascii_str_new_static((m), (long)rb_strlen_lit(m)))
-void rb_gc_mark_machine_stack(const rb_execution_context_t *ec);
+void rb_gc_mark_machine_context(const rb_execution_context_t *ec);
void rb_vm_rewrite_cref(rb_cref_t *node, VALUE old_klass, VALUE new_klass, rb_cref_t **new_cref_ptr);