mirror of
https://github.com/MariaDB/server.git
synced 2025-04-18 21:44:20 +03:00
Fixup bddbef357334
For Windows, the method of finding stack limit is reportedly flaky, and might not work as desired, as documented in https://joeduffyblog.com/2006/07/15/checking-for-sufficient-stack-space "Unfortunately, the StackLimit is only updated as you actually touch pages on the stack, and thus it’s not a reliable way to find out how much uncommitted stack is left." Thus, Windows specific code was removed. It might be added, if we find out that we need it, so far there was no need. Also AIX, the code based on HAVE_PTHREAD_GETATTR_NP was found not to work, (produce false positives of stack overrun), thus the traditional fallback code is used. Also - removed repetitive fallback code - fixed non-portable void pointer arithmethics (GCC-ism) - took into account that pthread_attr_getstack() can fail, - fixed the code for (less common) STACK_DIRECTION > 0. - removed confusing/wrong comments about what "stack base address" means Single Unix Spec, AIX documentation make it clear what that is.
This commit is contained in:
parent
c3a7a3c7a2
commit
e147f8a5ed
@ -40,56 +40,36 @@ extern void my_get_stack_bounds(void **stack_start, void **stack_end,
|
||||
void *fallback_stack_start,
|
||||
size_t fallback_stack_size)
|
||||
{
|
||||
#if defined(__GNUC__) || defined(__clang__) /* GCC or Clang compilers */
|
||||
size_t stack_size;
|
||||
#if defined(HAVE_PTHREAD_GETATTR_NP)
|
||||
#if defined(HAVE_PTHREAD_GETATTR_NP) && !defined(_AIX)
|
||||
/* POSIX-compliant system (Linux, macOS, etc.) */
|
||||
pthread_attr_t attr;
|
||||
pthread_t thread= pthread_self();
|
||||
void *stack_base;
|
||||
|
||||
/* Get the thread attributes */
|
||||
if (pthread_getattr_np(thread, &attr) == 0)
|
||||
{
|
||||
/* Get stack base and size */
|
||||
pthread_attr_getstack(&attr, &stack_base, &stack_size);
|
||||
/*
|
||||
stack_base points to start of the stack region to which the
|
||||
stack grows to
|
||||
*/
|
||||
*stack_start= stack_base - stack_size * STACK_DIRECTION;
|
||||
pthread_attr_destroy(&attr); /* Clean up */
|
||||
}
|
||||
else
|
||||
{
|
||||
/*
|
||||
Fallback:
|
||||
Use the current stack pointer as an approximation of the start
|
||||
*/
|
||||
*stack_start= my_get_stack_pointer(fallback_stack_start);
|
||||
stack_size= (fallback_stack_size -
|
||||
MY_MIN(fallback_stack_size, MY_STACK_SAFE_MARGIN));
|
||||
}
|
||||
void *low_addr, *high_addr= NULL;
|
||||
if (pthread_attr_getstack(&attr, &low_addr, &stack_size) == 0)
|
||||
{
|
||||
high_addr= (char *) low_addr + stack_size;
|
||||
#if STACK_DIRECTION < 0
|
||||
*stack_start= high_addr;
|
||||
*stack_end= low_addr;
|
||||
#else
|
||||
/* Platform does not have pthread_getattr_np */
|
||||
*stack_start= low_addr;
|
||||
*stack_end= high_addr;
|
||||
#endif
|
||||
}
|
||||
pthread_attr_destroy(&attr); /* Clean up */
|
||||
if (high_addr)
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
/* Platform does not have pthread_getattr_np, or fallback */
|
||||
*stack_start= my_get_stack_pointer(fallback_stack_start);
|
||||
stack_size= (fallback_stack_size -
|
||||
MY_MIN(fallback_stack_size, MY_STACK_SAFE_MARGIN));
|
||||
#endif /* defined(HAVE_PTHREAD_GETATTR_NP) */
|
||||
*stack_end= *stack_start + stack_size * STACK_DIRECTION;
|
||||
|
||||
#elif defined(_MSC_VER) && defined(_WIN32)
|
||||
/* Windows platform (MSVC) */
|
||||
NT_TIB* teb= (NT_TIB*)NtCurrentTeb();
|
||||
|
||||
*stack_start= teb->StackBase; /* Start of the stack */
|
||||
*stack_end= teb->StackLimit; /* End of the stack (stack limit) */
|
||||
#else
|
||||
/* Unsupported platform / compiler */
|
||||
*stack_start= my_get_stack_pointer(fallback_stack_start);
|
||||
*stack_end= (*stack_start +
|
||||
(fallback_stack_size -
|
||||
MY_MIN(fallback_stack_size, MY_STACK_SAFE_MARGIN)) *
|
||||
STACK_DIRECTON);
|
||||
#endif /* defined(__GNUC__) || defined(__clang__) */
|
||||
*stack_end= (char *)(*stack_start) + stack_size * STACK_DIRECTION;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user