The problem was that when using clang + asan, we do not get a correct value for the thread stack as some local variables are not allocated at the normal stack. It looks like that for example clang 18.1.3, when compiling with -O2 -fsanitize=addressan it puts local variables and things allocated by alloca() in other areas than on the stack. The following code shows the issue Thread 6 "mariadbd" hit Breakpoint 3, do_handle_one_connection (connect=0x5080000027b8, put_in_cache=<optimized out>) at sql/sql_connect.cc:1399 THD *thd; 1399 thd->thread_stack= (char*) &thd; (gdb) p &thd (THD **) 0x7fffedee7060 (gdb) p $sp (void *) 0x7fffef4e7bc0 The address of thd is 24M away from the stack pointer (gdb) info reg ... rsp 0x7fffef4e7bc0 0x7fffef4e7bc0 ... r13 0x7fffedee7060 140737185214560 r13 is pointing to the address of the thd. Probably some kind of "local stack" used by the sanitizer I have verified this with gdb on a recursive call that calls alloca() in a loop. In this case all objects was stored in a local heap, not on the stack. To solve this issue in a portable way, I have added two functions: my_get_stack_pointer() returns the address of the current stack pointer. The code is using asm instructions for intel 32/64 bit, powerpc, arm 32/64 bit and sparc 32/64 bit. Supported compilers are gcc, clang and MSVC. For MSVC 64 bit we are using _AddressOfReturnAddress() As a fallback for other compilers/arch we use the address of a local variable. my_get_stack_bounds() that will return the address of the base stack and stack size using pthread_attr_getstack() or NtCurrentTed() with fallback to using the address of a local variable and user provided stack size. Server changes are: - Moving setting of thread_stack to THD::store_globals() using my_get_stack_bounds(). - Removing setting of thd->thread_stack, except in functions that allocates a lot on the stack before calling store_globals(). When using estimates for stack start, we reduce stack_size with MY_STACK_SAFE_MARGIN (8192) to take into account the stack used before calling store_globals(). I also added a unittest, stack_allocation-t, to verify the new code. Reviewed-by: Sergei Golubchik <serg@mariadb.org>
120 lines
3.6 KiB
C
120 lines
3.6 KiB
C
/* Copyright (c) 2024, MariaDB Corporation.
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation; version 2 of the License.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program; if not, write to the Free Software
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
|
|
|
|
#include <my_global.h>
|
|
#include <my_sys.h>
|
|
#include <my_stack_alloc.h>
|
|
#include <my_pthread.h>
|
|
#include <my_alloca.h>
|
|
#include <tap.h>
|
|
|
|
/*
|
|
Test of stack detection
|
|
The test is run with a stacks of STACK_ALLOC_SMALL_BLOCK_SIZE+1 and
|
|
STACK_ALLOC_SMALL_BLOCK_SIZE-1.
|
|
This is becasue of the function alloc_on_stack() has
|
|
different limits of how much it will allocate from the stack
|
|
based on the allocation size.
|
|
*/
|
|
|
|
/*
|
|
Common stack size in MariaDB. Cannot be bigger than system default
|
|
stack (common is 8M)
|
|
*/
|
|
size_t my_stack_size= 299008;
|
|
size_t stack_allocation_total= 0;
|
|
extern long call_counter;
|
|
long call_counter;
|
|
|
|
ATTRIBUTE_NOINLINE
|
|
int test_stack(void *stack_start, void *stack_end, int iteration, size_t stack_allocation)
|
|
{
|
|
void *res, *stack;
|
|
my_bool must_be_freed;
|
|
|
|
stack= my_get_stack_pointer(&must_be_freed);
|
|
if (stack_start < stack_end)
|
|
{
|
|
if (stack < stack_start || stack > stack_end)
|
|
return 1;
|
|
}
|
|
else
|
|
{
|
|
if (stack < stack_end || stack > stack_start)
|
|
return 1;
|
|
}
|
|
alloc_on_stack(stack_end, res, must_be_freed, stack_allocation);
|
|
bfill(res, stack_allocation, (char) iteration);
|
|
if (!must_be_freed)
|
|
{
|
|
stack_allocation_total+= stack_allocation;
|
|
test_stack(stack_start, stack_end, iteration+1, stack_allocation);
|
|
}
|
|
else
|
|
my_free(res); /* Was allocated with my_malloc */
|
|
call_counter++; /* Avoid tail recursion optimization */
|
|
return 0;
|
|
}
|
|
|
|
void test_stack_detection(int stage, size_t stack_allocation)
|
|
{
|
|
void *stack_start, *stack_end;
|
|
int res;
|
|
my_get_stack_bounds(&stack_start, &stack_end,
|
|
(void*) &stack_start, my_stack_size);
|
|
stack_allocation_total= 0;
|
|
res= test_stack(stack_start, stack_end, 1, stack_allocation);
|
|
if (!res)
|
|
ok(1, "%ld bytes allocated on stack of size %ld with %ld alloc size",
|
|
stack_allocation_total,
|
|
(long) available_stack_size(stack_start, stack_end),
|
|
(long) stack_allocation);
|
|
else
|
|
ok(0, "stack checking failed");
|
|
}
|
|
|
|
pthread_handler_t thread_stack_check(void *arg __attribute__((unused)))
|
|
{
|
|
my_thread_init();
|
|
test_stack_detection(1, STACK_ALLOC_SMALL_BLOCK_SIZE-1);
|
|
test_stack_detection(2, STACK_ALLOC_SMALL_BLOCK_SIZE+1);
|
|
my_thread_end();
|
|
pthread_exit(0);
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc __attribute__((unused)), char **argv)
|
|
{
|
|
pthread_attr_t thr_attr;
|
|
pthread_t check_thread;
|
|
void *value;
|
|
|
|
MY_INIT(argv[0]);
|
|
|
|
plan(4);
|
|
test_stack_detection(3, STACK_ALLOC_SMALL_BLOCK_SIZE-1);
|
|
test_stack_detection(4, STACK_ALLOC_SMALL_BLOCK_SIZE+1);
|
|
|
|
/* Create a thread and run the same test */
|
|
(void) pthread_attr_init(&thr_attr);
|
|
pthread_attr_setscope(&thr_attr,PTHREAD_SCOPE_SYSTEM);
|
|
(void) my_setstacksize(&thr_attr, my_stack_size);
|
|
pthread_create(&check_thread, &thr_attr, thread_stack_check, 0);
|
|
pthread_join(check_thread, &value);
|
|
(void) pthread_attr_destroy(&thr_attr);
|
|
|
|
my_end(0);
|
|
return exit_status();
|
|
}
|