Pass down "stack start" variables from closer to the top of the stack

The implementation of `native_thread_init_stack` for the various
threading models can use the address of a local variable as part of the
calculation of the machine stack extents:

* pthreads uses it as a lower-bound on the start of the stack, because
  glibc (and maybe other libcs) can store its own data on the stack
  before calling into user code on thread creation.
* win32 uses it as an argument to VirtualQuery, which gets the extent of
  the memory mapping which contains the variable

However, the local being used for this is actually allocated _inside_
the `native_thread_init_stack` frame; that means the caller might
allocate a VALUE on the stack that actually lies outside the bounds
stored in machine.stack_{start,end}.

A local variable from one level above the topmost frame that stores
VALUEs on the stack must be drilled down into the call to
`native_thread_init_stack` to be used in the calculation. This probably
doesn't _really_ matter for the win32 case (they'll be in the same
memory mapping so VirtualQuery should return the same thing), but
definitely could matter for the pthreads case.

[Bug #20001]
This commit is contained in:
KJ Tsanaktsidis 2023-11-12 13:24:55 +11:00
parent 6a45320c25
commit 4ba8f0dc99
9 changed files with 33 additions and 22 deletions

View file

@ -582,7 +582,7 @@ rb_native_cond_destroy(rb_nativethread_cond_t *cond)
}
void
ruby_init_stack(volatile VALUE *addr)
ruby_init_stack(volatile void *addr)
{
}
@ -594,20 +594,20 @@ COMPILER_WARNING_PUSH
COMPILER_WARNING_IGNORED(-Wmaybe-uninitialized)
#endif
static inline SIZE_T
query_memory_basic_info(PMEMORY_BASIC_INFORMATION mi)
query_memory_basic_info(PMEMORY_BASIC_INFORMATION mi, void *local_in_parent_frame)
{
return VirtualQuery(mi, mi, sizeof(*mi));
return VirtualQuery(local_in_parent_frame, mi, sizeof(*mi));
}
COMPILER_WARNING_POP
static void
native_thread_init_stack(rb_thread_t *th)
native_thread_init_stack(rb_thread_t *th, void *local_in_parent_frame)
{
MEMORY_BASIC_INFORMATION mi;
char *base, *end;
DWORD size, space;
CHECK_ERR(query_memory_basic_info(&mi));
CHECK_ERR(query_memory_basic_info(&mi, local_in_parent_frame));
base = mi.AllocationBase;
end = mi.BaseAddress;
end += mi.RegionSize;
@ -638,7 +638,7 @@ thread_start_func_1(void *th_ptr)
rb_thread_t *th = th_ptr;
volatile HANDLE thread_id = th->nt->thread_id;
native_thread_init_stack(th);
native_thread_init_stack(th, &th);
th->nt->interrupt_event = CreateEvent(0, TRUE, FALSE, 0);
/* run */