g_assert (info->client_info.stack_end);
aligned_stack_start = (void*)(mword) ALIGN_TO ((mword)info->client_info.stack_start, SIZEOF_VOID_P);
+#ifdef HOST_WIN32
+ /* Windows uses a guard page before the committed stack memory pages to detect when the
+ stack needs to be grown. If we suspend a thread just after a function prolog has
+ decremented the stack pointer to point into the guard page but before the thread has
+ been able to read or write to that page, starting the stack scan at aligned_stack_start
+ will raise a STATUS_GUARD_PAGE_VIOLATION and the process will crash. This code uses
+ VirtualQuery() to determine whether stack_start points into the guard page and then
+ updates aligned_stack_start to point at the next non-guard page. */
+ MEMORY_BASIC_INFORMATION mem_info;
+ SIZE_T result = VirtualQuery(info->client_info.stack_start, &mem_info, sizeof(mem_info));
+ g_assert (result != 0);
+ if (mem_info.Protect & PAGE_GUARD) {
+ aligned_stack_start = ((char*) mem_info.BaseAddress) + mem_info.RegionSize;
+ }
+#endif
g_assert (info->client_info.suspend_done);
SGEN_LOG (3, "Scanning thread %p, range: %p-%p, size: %zd, pinned=%zd", info, info->client_info.stack_start, info->client_info.stack_end, (char*)info->client_info.stack_end - (char*)info->client_info.stack_start, sgen_get_pinned_count ());